File: QCouplingEditorView.py

package info (click to toggle)
code-saturne 7.0.2%2Brepack-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 62,868 kB
  • sloc: ansic: 395,271; f90: 100,755; python: 86,746; cpp: 6,227; makefile: 4,247; xml: 2,389; sh: 1,091; javascript: 69
file content (417 lines) | stat: -rw-r--r-- 13,569 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
# -*- coding: utf-8 -*-

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

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2021 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 view for coupling parameters handling.
"""
#-------------------------------------------------------------------------------
# Standard modules
#-------------------------------------------------------------------------------

import sys, os, shutil
import logging
from code_saturne.Base.QtCore    import *
from code_saturne.Base.QtGui     import *
from code_saturne.Base.QtWidgets import *

# Check if QString exists
has_qstring = True
try:
    from code_saturne.Base.QtCore import QString
    _fromUtf8 = QString.fromUtf8
except ImportError:
    has_qstring = False
    def _fromUtf8(s):
        return s

    def QString(s):
        return s

try:
    # PyQt5
    from code_saturne.Base.QtWidgets import QMainWindow, QMessageBox, \
        QAction, QFileDialog, QTextEdit, QPlainTextEdit, QSizePolicy, QMenu, QMessageBox
except Exception:
    # PyQt4
    from code_saturne.Base.QtGui import QMainWindow, QMessageBox, \
        QAction, QFileDialog, QTextEdit, QPlainTextEdit, QSizePolicy, QMenu, QMessagBox

from code_saturne.model.Common import GuiParam
#-------------------------------------------------------------------------------
# log config
#-------------------------------------------------------------------------------

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

#-------------------------------------------------------------------------------
# CouplingEditorView class
#-------------------------------------------------------------------------------

_ordered_keys = ['domain', 'solver', 'n_procs_min', 'n_procs_max', 'n_procs_weight']

#-------------------------------------------------------------------------------
# domain_entry class
#-------------------------------------------------------------------------------

class domain_entry(object):
    """
    Class for domain data entry management.
    """

    # ---------------------------------------------------------------
    def __init__(self, data_dict):
        """
        Constructor
        """

        self.name   = None
        self.n_ent  = 0
        self.keys   = []
        self.labels = []
        self.vals   = []

        keys = data_dict.keys()

        for k in _ordered_keys:
            if k in keys:
                self.keys.append(k)
                self.labels.append(self.__create_label__(k))
                self.vals.append(self.__create_line__(data_dict[k]))

                if k in ('domain', 'solver'):
                    self.vals[-1].setEnabled(False)
                    if k == 'domain':
                        self.name = data_dict[k]

                self.n_ent += 1

        for k in keys:
            if k not in _ordered_keys:
                self.keys.append(k)
                self.labels.append(self.__create_label__(k))
                self.vals.append(self.__create_line__(data_dict[k]))
                self.n_ent += 1
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def __create_label__(self, text):

        label = QLabel()

        label.setText(text)
        label.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)

        return label
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def __create_line__(self, val):

        line = QLineEdit()
        line.setText(str(val))

        return line
    # ---------------------------------------------------------------

#-------------------------------------------------------------------------------
# CouplingEditorView class
#-------------------------------------------------------------------------------

class CouplingEditorView(QWidget):

    # ---------------------------------------------------------------
    def __init__(self, parent, cfgfile=None):
        """
        Constructor
        """

        QWidget.__init__(self, parent)

        self.cfgfile=cfgfile

        self.data  = []
        self.pages = []

        self.data_modified = False
        self.file_loaded   = False

        self.initUI(self.cfgfile)
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def load_cfg_file(self, cfgfile=None):
        """
        Load a configuration file
        """

        self.cfgfile = cfgfile
        if self.cfgfile:
            from code_saturne import cs_run_conf
            self.run_conf = cs_run_conf.run_conf(self.cfgfile)
            if self.run_conf:
                for dom in self.run_conf.get_coupling_parameters():
                    self.add_tab_data(dom)

                if self.data != []:
                    self.file_loaded   = True
                self.data_modified = False
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def save_cfg_file(self):
        """
        Save the configuration file
        """

        for d in self.data:
            secname = d.name.lower()
            for entry in range(d.n_ent):
                self.run_conf.set(secname,
                                  str(d.keys[entry]),
                                  str(d.vals[entry].text()))

        self.run_conf.save()
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def initUI(self, cfgfile):
        """
        Create the visual layout
        """

        # Generate dictionary here
        self.load_cfg_file(cfgfile)

        if not self.file_loaded:
           title = self.tr("Warning")
           msg   = self.tr("File %s does not contain coupling parameters." % cfgfile)
           QMessageBox.warning(self, title, msg)
           return

        self.tabwidget = QTabWidget(self)
        vbox = QVBoxLayout()

        # Title
        l1 = QLabel()
        l1.setText("Coupled cases")
        l1.setAlignment(Qt.AlignCenter)
        lfont = QFont()
        lfont.setBold(True)
        l1.setFont(lfont)

        # File
        fileLabel = QLabel()
        fileLabel.setText("Parameters file: ")
        fileLabel.setFont(lfont)
        filePLabel = QLabel()
        filePLabel.setText(cfgfile)
        filePLabel.setFont(lfont)
        fileHbox = QHBoxLayout()
        fileHbox.addWidget(fileLabel)
        fileHbox.addWidget(filePLabel)
        fileHbox.addStretch(1)

        # Final layout
        vbox.addWidget(l1)
        vbox.addLayout(fileHbox)
        vbox.addWidget(self.tabwidget)

        self.setLayout(vbox)

        for i, d in enumerate(self.data):
            dom = d.name
            if dom:
                tab_name = "domain: %s" % dom
            else:
                tab_name = "domain: unknown"

            self.pages.append(self.add_tab(d))
            self.tabwidget.addTab(self.pages[i], tab_name)


        self.tabwidget.setCurrentIndex(0)
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def add_tab_data(self, data):
        """
        Add tab data
        """

        self.data.append(domain_entry(data))
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def add_tab(self, dom_data):
        """
        Add a tab to the tabWidget
        """

        rows = dom_data.n_ent
        cols = 2

        Labels = dom_data.labels
        LineEdits = dom_data.vals

        layout = QGridLayout()

        for row in range(rows):
            layout.addWidget(Labels[row], row, 0)
            layout.addWidget(LineEdits[row]  , row, 1)
            LineEdits[row].textChanged[str].connect(self.dataModifed)

        lSpacer = QLabel()
        lSpacer.setText(30*" ")
        vSpacer = QSpacerItem(40, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
        layout.addWidget(lSpacer, rows, 0)
        layout.addItem(vSpacer, rows, 1)


        tab = QWidget()
        tab.setLayout(layout)

        return tab
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    @pyqtSlot()
    def dataModifed(self):

        self.data_modified = True
    # ---------------------------------------------------------------

#-------------------------------------------------------------------------------
# QCouplingEditor class (Independent window)
#-------------------------------------------------------------------------------

class QCouplingEditor(QMainWindow):
    """
    Independant window
    """

    # ---------------------------------------------------------------
    def __init__(self, parent=None, cfgfile=None, standalone_mode=False):
        """
        Constructor
        """

        super(QCouplingEditor, self).__init__(parent)
        self.setGeometry(50, 50, 500, 300)

        self.setWindowTitle("Coupling parameters editor")
        self.parent = parent
        self.stdalone = standalone_mode

        self.cfgfile = cfgfile

        self.editorView = CouplingEditorView(None, self.cfgfile)

        # If standalone mode, exit if file not loaded
        if self.stdalone:
            if not self.editorView.file_loaded:
                self.close()
                sys.exit(0)
                return None

        # Save file action
        save_img_path = ":/icons/22x22/document-save.png"
        icon_save     = QIcon()
        icon_save.addPixmap(QPixmap(_fromUtf8(save_img_path)),
                            QIcon.Normal,
                            QIcon.Off)
        self.saveFileAction = QAction(icon_save, "Save", self)
        self.saveFileAction.setShortcut("Ctrl+S")
        self.saveFileAction.setStatusTip('Save file')
        self.saveFileAction.triggered.connect(self.saveFile)

        # Close file action
        close_img_path = ":/icons/22x22/process-stop.png"
        icon_close     = QIcon()
        icon_close.addPixmap(QPixmap(_fromUtf8(close_img_path)),
                             QIcon.Normal,
                             QIcon.Off)
        self.closeFileAction = QAction(icon_close, "Close file", self)
        self.closeFileAction.setShortcut("Ctrl+Q")
        self.closeFileAction.setStatusTip('Close opened file')
        self.closeFileAction.triggered.connect(self.closeOpenedFile)

        # Toolbar
        self.toolbar = self.addToolBar("Options")
        self.toolbar.addAction(self.saveFileAction)
        self.toolbar.addAction(self.closeFileAction)

        self.setCentralWidget(self.editorView)
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def saveFile(self):
        """
        Save the configuration file
        """

        self.editorView.save_cfg_file()
        self.editorView.data_modified = False
    # ---------------------------------------------------------------

    # ---------------------------------------------------------------
    def closeOpenedFile(self):
        """
        Close and save if necessary the opened configuration file
        """

        if self.editorView.file_loaded:
            choice = QMessageBox.question(self, 'Coupling parameters editor',
                                          'Exit editor ?',
                                          QMessageBox.Yes | QMessageBox.No)
        else:
            choice = QMessageBox.Yes

        if choice == QMessageBox.Yes:

            if self.editorView.data_modified:
                choice = QMessageBox.question(self, 'Coupling parameters editor',
                                              'Save modifications ?',
                                              QMessageBox.Yes | QMessageBox.No)
                if choice == QMessageBox.Yes:
                    self.saveFile()
                else:
                    pass

            settings = QSettings()
            settings.setValue("MainWindow/Geometry",
                              self.saveGeometry())

            self.editorView.file_loaded = False

            self.close()
            return 0
        else:
            return 1
    # ---------------------------------------------------------------

#-------------------------------------------------------------------------------
# END OF FILE
#-------------------------------------------------------------------------------