1# -*- coding: utf-8 -*-
2
3#-------------------------------------------------------------------------------
4
5# This file is part of Code_Saturne, a general-purpose CFD tool.
6#
7# Copyright (C) 1998-2021 EDF S.A.
8#
9# This program is free software; you can redistribute it and/or modify it under
10# the terms of the GNU General Public License as published by the Free Software
11# Foundation; either version 2 of the License, or (at your option) any later
12# version.
13#
14# This program is distributed in the hope that it will be useful, but WITHOUT
15# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17# details.
18#
19# You should have received a copy of the GNU General Public License along with
20# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
21# Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23#-------------------------------------------------------------------------------
24
25"""
26This module contains the following classes and function:
27- OutputSurfacicVariablesView
28"""
29
30#-------------------------------------------------------------------------------
31# Library modules import
32#-------------------------------------------------------------------------------
33
34import logging
35
36#-------------------------------------------------------------------------------
37# Third-party modules
38#-------------------------------------------------------------------------------
39
40from code_saturne.Base.QtCore    import *
41from code_saturne.Base.QtGui     import *
42from code_saturne.Base.QtWidgets import *
43
44#-------------------------------------------------------------------------------
45# Application modules import
46#-------------------------------------------------------------------------------
47
48from code_saturne.model.Common import GuiParam
49from code_saturne.Base.QtPage import from_qvariant, to_text_string
50from code_saturne.Pages.OutputSurfacicVariablesForm import Ui_OutputSurfacicVariablesForm
51from code_saturne.model.OutputControlModel import OutputControlModel
52from code_saturne.model.OutputSurfacicVariablesModel import OutputSurfacicVariablesModel
53from code_saturne.model.OutputSurfacicFieldsModel import OutputSurfacicFieldsModel #AZ
54from code_saturne.Pages.OutputVolumicVariablesView import LabelDelegate
55
56#-------------------------------------------------------------------------------
57# log config
58#-------------------------------------------------------------------------------
59
60logging.basicConfig()
61log = logging.getLogger("OutputSurfacicVariablesView")
62log.setLevel(GuiParam.DEBUG)
63
64#-------------------------------------------------------------------------------
65# StandarItemModelOutput class
66#-------------------------------------------------------------------------------
67
68class StandardItemModelOutput(QStandardItemModel):
69
70    def __init__(self, case, mdl):
71        """
72        """
73        QStandardItemModel.__init__(self)
74
75        self.case = case
76        self.mdl = mdl
77
78        self.setColumnCount(3)
79        self.dataLabel      = []
80        self.dataName       = []
81        self.dataPost       = []
82        self.disableItem    = []
83        self.populateModel()
84
85
86    def populateModel(self):
87        # Data initialization
88
89        for name in self.mdl.list_name:
90            row = self.rowCount()
91            self.setRowCount(row + 1)
92
93            label = self.mdl.dicoLabelName[name]
94            post  = self.mdl.getPostProcessing(label)
95
96            if not OutputControlModel(self.case).isSurfaceWriterActive():
97                self.disableItem.append((row, 1))
98                post = "off"
99
100            self.dataLabel.append(label)
101            self.dataName.append(name)
102            self.dataPost.append(post)
103
104
105    def data(self, index, role):
106        if not index.isValid():
107            return None
108
109        # ToolTips BUG
110        if role == Qt.ToolTipRole:
111            if index.column() == 2:
112                return self.tr("code_saturne keyword: ipstdv")
113
114        # StatusTips
115        if role == Qt.StatusTipRole:
116            if index.column() == 2:
117                return "Post-processing"
118
119        # Display
120        if role == Qt.DisplayRole:
121            row = index.row()
122            if index.column() == 0:
123                return self.dataLabel[row]
124            elif index.column() == 1:
125                return self.dataName[row]
126            else:
127                return None
128
129        # CheckState
130        if role == Qt.CheckStateRole:
131            row = index.row()
132            if index.column() == 2:
133                value = self.dataPost[row]
134                if value == 'on':
135                    return Qt.Checked
136                else:
137                    return Qt.Unchecked
138
139        return None
140
141
142    def flags(self, index):
143        if not index.isValid():
144            return Qt.ItemIsEnabled
145
146        if (index.row(), index.column()) in self.disableItem:
147            return Qt.ItemIsSelectable
148        elif index.column() == 0 :
149            return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
150        elif index.column() == 1 :
151            return  Qt.ItemIsEnabled | Qt.ItemIsSelectable
152        elif index.column() == 2 :
153            return  Qt.ItemIsEnabled | Qt.ItemIsUserCheckable
154        else:
155            return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
156
157
158    def headerData(self, section, orientation, role):
159        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
160            if section == 0:
161                return self.tr("Output label")
162            if section == 1:
163                return self.tr("Internal name")
164            elif section == 2:
165                return self.tr("Post-\nprocessing")
166        return None
167
168
169    def setData(self, index, value, role=None):
170        row = index.row()
171        if index.column() == 0:
172            label = str(from_qvariant(value, to_text_string))
173            if label == "":
174                label = self.dataLabel[row]
175            self.mdl.setPropertyLabel(self.dataLabel[row], label)
176            self.dataLabel[row] = label
177
178        elif index.column() == 2:
179            v = from_qvariant(value, int)
180            if v == Qt.Checked:
181                self.dataPost[row] = "on"
182            else:
183                self.dataPost[row] = "off"
184            if not OutputControlModel(self.case).isSurfaceWriterActive():
185                self.dataPost[row] = "off"
186
187            self.mdl.setPostProcessing(self.dataLabel[row], self.dataPost[row])
188
189        self.dataChanged.emit(index, index)
190        return True
191
192#-------------------------------------------------------------------------------
193# Main class
194#-------------------------------------------------------------------------------
195
196class OutputSurfacicVariablesView(QWidget, Ui_OutputSurfacicVariablesForm):
197    """
198    """
199    def __init__(self, parent, case):
200        """
201        Constructor
202        """
203        QWidget.__init__(self, parent)
204
205        Ui_OutputSurfacicVariablesForm.__init__(self)
206        self.setupUi(self)
207
208        self.case = case
209        self.case.undoStopGlobal()
210
211        if self.case.xmlRootNode().tagName == "Code_Saturne_GUI":
212            self.mdl = OutputSurfacicVariablesModel(self.case)
213        else:
214            self.mdl = OutputSurfacicFieldsModel(self.case)
215
216        self.modelOutput = StandardItemModelOutput(self.case, self.mdl)
217        self.tableViewOutput.setModel(self.modelOutput)
218        self.tableViewOutput.setAlternatingRowColors(True)
219        self.tableViewOutput.resizeColumnToContents(0)
220        self.tableViewOutput.resizeRowsToContents()
221        self.tableViewOutput.setSelectionBehavior(QAbstractItemView.SelectItems)
222        self.tableViewOutput.setSelectionMode(QAbstractItemView.ExtendedSelection)
223        self.tableViewOutput.setEditTriggers(QAbstractItemView.DoubleClicked)
224        if QT_API == "PYQT4":
225            self.tableViewOutput.horizontalHeader().setResizeMode(QHeaderView.Stretch)
226        elif QT_API == "PYQT5":
227            self.tableViewOutput.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
228
229        labelDelegate = LabelDelegate(self.tableViewOutput, self.mdl)
230        self.tableViewOutput.setItemDelegateForColumn(0, labelDelegate)
231
232        self.case.undoStartGlobal()
233
234
235#-------------------------------------------------------------------------------
236# End
237#-------------------------------------------------------------------------------
238