File: SpeciesView.py

package info (click to toggle)
code-saturne 6.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 63,340 kB
  • sloc: ansic: 354,724; f90: 119,812; python: 87,716; makefile: 4,653; cpp: 4,272; xml: 2,839; sh: 1,228; lex: 170; yacc: 100
file content (479 lines) | stat: -rw-r--r-- 16,175 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2019 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------

"""
This module defines the 'Species transport' page.

This module contains the following classes:
- LabelDelegate
- FieldDelegate
- StandardItemModelUserScalar
- SpeciesView
"""

#-------------------------------------------------------------------------------
# Library modules import
#-------------------------------------------------------------------------------

import os, sys, string, types
import logging

#-------------------------------------------------------------------------------
# Third-party modules
#-------------------------------------------------------------------------------

from code_saturne.Base.QtCore    import *
from code_saturne.Base.QtGui     import *
from code_saturne.Base.QtWidgets import *

#-------------------------------------------------------------------------------
# Application modules import
#-------------------------------------------------------------------------------

from code_saturne.Base.QtPage import ComboModel, IntValidator, DoubleValidator, RegExpValidator
from code_saturne.Base.QtPage import to_qvariant, from_qvariant, to_text_string
from Species import Ui_Species
from code_saturne.model.SpeciesModel import SpeciesModel
from code_saturne.model.Common import LABEL_LENGTH_MAX, GuiParam

#-------------------------------------------------------------------------------
# log config
#-------------------------------------------------------------------------------

logging.basicConfig()
log = logging.getLogger("SpeciesView")
log.setLevel(GuiParam.DEBUG)


#-------------------------------------------------------------------------------
# Combo box delegate for the nature of non condensable
#-------------------------------------------------------------------------------

class LabelDelegate(QItemDelegate):
    """
    Use of a QLineEdit in the table.
    """
    def __init__(self, parent=None):
        QItemDelegate.__init__(self, parent)
        self.parent = parent
        self.old_plabel = ""


    def createEditor(self, parent, option, index):
        editor = QLineEdit(parent)
        self.old_label = ""
        rx = "[_a-zA-Z][_A-Za-z0-9]{1," + str(LABEL_LENGTH_MAX-1) + "}"
        self.regExp = QRegExp(rx)
        v = RegExpValidator(editor, self.regExp)
        editor.setValidator(v)
        return editor


    def setEditorData(self, editor, index):
        editor.setAutoFillBackground(True)
        value = from_qvariant(index.model().data(index, Qt.DisplayRole), to_text_string)
        self.old_plabel = str(value)
        editor.setText(value)


    def setModelData(self, editor, model, index):
        if not editor.isModified():
            return

        if editor.validator().state == QValidator.Acceptable:
            new_plabel = str(editor.text())

            if new_plabel in model.mdl.getScalarLabelList():
                default = {}
                default['label']  = self.old_plabel
                default['list']   = model.mdl.getScalarLabelList()
                default['regexp'] = self.regExp
                log.debug("setModelData -> default = %s" % default)

                from code_saturne.Pages.VerifyExistenceLabelDialogView import VerifyExistenceLabelDialogView
                dialog = VerifyExistenceLabelDialogView(self.parent, default)
                if dialog.exec_():
                    result = dialog.get_result()
                    new_plabel = result['label']
                    log.debug("setModelData -> result = %s" % result)
                else:
                    new_plabel = self.old_plabel

            model.setData(index, to_qvariant(new_plabel), Qt.DisplayRole)


#-------------------------------------------------------------------------------
# Combo box delegate for the field selection
#-------------------------------------------------------------------------------

class FieldDelegate(QItemDelegate):
    """
    Use of a combo box in the table.
    """
    def __init__(self, parent, mdl):
        super(FieldDelegate, self).__init__(parent)
        self.parent = parent
        self.mdl    = mdl


    def createEditor(self, parent, option, index):
        editor = QComboBox(parent)
        self.modelCombo = ComboModel(editor, 1, 1)
        for fieldId in self.mdl.getFieldIdList() :
            label     = self.mdl.getLabel(fieldId)
            self.modelCombo.addItem(self.tr(label), label)

        editor.installEventFilter(self)
        return editor


    def setEditorData(self, comboBox, index):
        row = index.row()
        col = index.column()
        string = index.model().getData(index)[col]
        self.modelCombo.setItem(str_model=string)


    def setModelData(self, comboBox, model, index):
        txt = str(comboBox.currentText())
        value = self.modelCombo.dicoV2M[txt]
        log.debug("FieldDelegate value = %s"%value)

        selectionModel = self.parent.selectionModel()
        for idx in selectionModel.selectedIndexes():
            if idx.column() == index.column():
                model.setData(idx, to_qvariant(value), Qt.DisplayRole)


    def tr(self, text):
        return text


#-------------------------------------------------------------------------------
# StandardItemModelUserScalar class
#-------------------------------------------------------------------------------

class StandardItemModelUserScalar(QStandardItemModel):

    def __init__(self, parent, mdl):
        """
        """
        QStandardItemModel.__init__(self)

        self.headers = [ self.tr("Label"),
                         self.tr("Carrier Field")]

        self.setColumnCount(len(self.headers))
        self.parent = parent

        self.tooltip = []

        self._data  = []
        self.mdl    = mdl


    def data(self, index, role):
        if not index.isValid():
            return to_qvariant()

        if role == Qt.ToolTipRole:
            return to_qvariant()

        elif role == Qt.DisplayRole:
            data = self._data[index.row()][index.column()]
            if data:
                return to_qvariant(data)
            else:
                return to_qvariant()

        elif role == Qt.TextAlignmentRole:
            return to_qvariant(Qt.AlignCenter)

        return to_qvariant()


    def flags(self, index):
        if not index.isValid():
            return Qt.ItemIsEnabled
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable


    def headerData(self, section, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return to_qvariant(self.headers[section])
        return to_qvariant()


    def setData(self, index, value, role):
        if not index.isValid():
            return Qt.ItemIsEnabled

        # Update the row in the table
        row = index.row()
        col = index.column()
        name = row + 1

        # Label
        if col == 0:
            new_label = from_qvariant(value, to_text_string)
            self._data[row][col] = new_label
            self.mdl.setScalarLabel(name, new_label)
        # field Id
        elif col == 1:
            new_field= from_qvariant(value, to_text_string)
            self._data[row][col] = new_field
            id = self.mdl.getFieldId(self._data[row][col])
            self.mdl.setScalarFieldId(name, id)

        self.dataChanged.emit(index, index)
        return True


    def getData(self, index):
        row = index.row()
        return self._data[row]


    def newItem(self, existing_scalar=None):
        """
        Add/load a scalar in the model.
        """
        row = self.rowCount()

        name = ""
        if existing_scalar == None :
            name = self.mdl.addScalar(row + 1)
        else :
            name = existing_scalar

        fieldId    = self.mdl.getScalarFieldId(row + 1)
        labelfield = self.mdl.getLabel(fieldId)
        label      = self.mdl.getScalarLabel(row + 1)

        scalar = [label, labelfield]

        self._data.append(scalar)
        self.setRowCount(row + 1)


    def deleteItem(self, row):
        """
        Delete the row in the model.
        """
        del self._data[row]
        self.mdl.deleteScalar(row + 1)
        row = self.rowCount()
        self.setRowCount(row - 1)


#-------------------------------------------------------------------------------
# SpeciesView class
#-------------------------------------------------------------------------------

class SpeciesView(QWidget, Ui_Species):
    """
    Species creation layout.
    """
    def __init__(self, parent, case):
        """
        Constructor
        """
        QWidget.__init__(self, parent)

        Ui_Species.__init__(self)
        self.setupUi(self)

        self.case = case
        self.case.undoStopGlobal()
        self.mdl = SpeciesModel(self.case)

        self.tableModelScalar = StandardItemModelUserScalar(self, self.mdl)
        self.tableViewPassifScalar.setModel(self.tableModelScalar)
        self.tableViewPassifScalar.resizeColumnsToContents()
        self.tableViewPassifScalar.resizeRowsToContents()
        if QT_API == "PYQT4":
            self.tableViewPassifScalar.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
            self.tableViewPassifScalar.horizontalHeader().setResizeMode(0,QHeaderView.Stretch)
        elif QT_API == "PYQT5":
            self.tableViewPassifScalar.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
            self.tableViewPassifScalar.horizontalHeader().setSectionResizeMode(0,QHeaderView.Stretch)
        self.tableViewPassifScalar.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.tableViewPassifScalar.setSelectionMode(QAbstractItemView.SingleSelection)

        delegateLabel  = LabelDelegate(self.tableViewPassifScalar)
        delegateField  = FieldDelegate(self.tableViewPassifScalar, self.mdl)

        self.tableViewPassifScalar.setItemDelegateForColumn(0, delegateLabel)
        self.tableViewPassifScalar.setItemDelegateForColumn(1, delegateField)

        # Validators
        validatorDiffusionCoef   = DoubleValidator(self.lineEditDiffusionCoef, min = 0.0)
        validatorSchmidt = DoubleValidator(self.lineEditSchmidt, min = 0.0)
        validatorMin     = DoubleValidator(self.lineEditMinValue, min = 0.0)
        validatorMax     = DoubleValidator(self.lineEditMaxValue, min = 0.0)

        self.lineEditDiffusionCoef.setValidator(validatorDiffusionCoef)
        self.lineEditSchmidt.setValidator(validatorSchmidt)
        self.lineEditMinValue.setValidator(validatorMin)
        self.lineEditMaxValue.setValidator(validatorMax)

        # Connections
        self.pushButtonAdd.clicked.connect(self.slotAddScalar)
        self.pushButtonDelete.clicked.connect(self.slotDeleteScalar)
        self.tableModelScalar.dataChanged.connect(self.dataChanged)
        self.lineEditDiffusionCoef.textChanged[str].connect(self.slotDiffusionCoef)
        self.lineEditSchmidt.textChanged[str].connect(self.slotSchmidt)
        self.lineEditMinValue.textChanged[str].connect(self.slotMinValue)
        self.lineEditMaxValue.textChanged[str].connect(self.slotMaxValue)
        self.checkBoxTimeDepend.clicked.connect(self.slotTimeDepend)
        self.checkBoxDiffusion.clicked.connect(self.slotDiffusion)
        self.tableViewPassifScalar.clicked.connect(self.slotChangeSelection)

        # hide properties by default
        self.groupBoxScalarProperties.hide()

        # load values
        self.currentid = -1

        for scalar in self.mdl.getScalarNameList():
            self.tableModelScalar.newItem(scalar)

        self.case.undoStartGlobal()


    def slotChangeSelection(self, text=None):
        """
        detect change selection to update constant properties
        """
        row = self.tableViewPassifScalar.currentIndex().row()
        self.update(row)


    def dataChanged(self, topLeft, bottomRight):
        for row in range(topLeft.row(), bottomRight.row()+1):
            self.tableViewPassifScalar.resizeRowToContents(row)
        for col in range(topLeft.column(), bottomRight.column()+1):
            self.tableViewPassifScalar.resizeColumnToContents(col)

        row = self.tableViewPassifScalar.currentIndex().row()
        self.update(row)


    def update(self, row):
        """
        show groupBoxScalarProperties if necessary
        """
        self.currentid = row + 1
        self.groupBoxScalarProperties.show()
        self.lineEditDiffusionCoef.setText(str(self.mdl.getDiffusionCoef(self.currentid)))
        self.lineEditSchmidt.setText(str(self.mdl.getSchmidt(self.currentid)))
        self.lineEditMinValue.setText(str(self.mdl.getMinValue(self.currentid)))
        self.lineEditMaxValue.setText(str(self.mdl.getMaxValue(self.currentid)))
        isTimeDep  = self.mdl.getTimeDependStatus(self.currentid) == "on"
        self.checkBoxTimeDepend.setChecked(isTimeDep)
        isDiffus  = self.mdl.getDiffusionStatus(self.currentid) == "on"
        self.checkBoxDiffusion.setChecked(isDiffus)


    @pyqtSlot()
    def slotAddScalar(self):
        """
        Add a scalar
        """
        self.tableViewPassifScalar.clearSelection()
        self.tableModelScalar.newItem()


    @pyqtSlot()
    def slotDeleteScalar(self):
        """
        Delete the a scalar from the list (one by one).
        """
        row = self.tableViewPassifScalar.currentIndex().row()
        if row >= 0 :
            log.debug("slotDeleteScalar -> %s" % row)
            self.tableModelScalar.deleteItem(row)
        self.groupBoxScalarProperties.hide()


    @pyqtSlot(str)
    def slotDiffusionCoef(self, text):
        """
        Update the diffusion coefficient
        """
        if self.lineEditDiffusionCoef.validator().state == QValidator.Acceptable:
            value = from_qvariant(text, float)
            self.mdl.setDiffusionCoef(self.currentid, value)


    @pyqtSlot(str)
    def slotSchmidt(self, text):
        """
        Update the schmidt
        """
        if self.lineEditSchmidt.validator().state == QValidator.Acceptable:
            value = from_qvariant(text, float)
            self.mdl.setSchmidt(self.currentid, value)


    @pyqtSlot(str)
    def slotMinValue(self, text):
        """
        Update the minimum value
        """
        if self.lineEditMinValue.validator().state == QValidator.Acceptable:
            value = from_qvariant(text, float)
            self.mdl.setMinValue(self.currentid, value)


    @pyqtSlot(str)
    def slotMaxValue(self, text):
        """
        Update the maximum value
        """
        if self.lineEditMaxValue.validator().state == QValidator.Acceptable:
            value = from_qvariant(text, float)
            self.mdl.setMaxValue(self.currentid, value)


    @pyqtSlot(bool)
    def slotTimeDepend(self, checked):
        """
        check box for time depend
        """
        status = 'off'
        if checked:
            status = 'on'
        self.mdl.setTimeDependStatus(self.currentid, status)


    @pyqtSlot(bool)
    def slotDiffusion(self, checked):
        """
        check box for diffusion
        """
        status = 'off'
        if checked:
            status = 'on'
        self.mdl.setDiffusionStatus(self.currentid, status)