]> SALOME platform Git repositories - modules/gui.git/blob - src/GUI_PY/selectvars.py
Salome HOME
Merge branch 'V9_2_2_BR'
[modules/gui.git] / src / GUI_PY / selectvars.py
1 # Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22
23 import os
24
25 from qtsalome import *
26
27 import salome
28 from salome.kernel.studyedit import getStudyEditor
29 from salome.kernel.parametric import study_exchange_vars
30
31 # ---------------------------------- #
32 # Dialog box for variables selection #
33 # ---------------------------------- #
34
35 from SelectVarsDialog_ui import Ui_SelectVarsDialog
36
37 class MySelectVarsDialog(Ui_SelectVarsDialog, QDialog):
38
39     def __init__(self, parent = None, modal = 0):
40         QDialog.__init__(self, parent)
41         Ui_SelectVarsDialog.__init__(self)
42         self.setupUi(self)
43         self.cancelButton.clicked.connect(self.close)
44         self.OKButton.clicked.connect(self.accept)
45         self.selectButton.clicked.connect(self.initPotentialVariablesFromSelection)
46         self.addInputVarButton.clicked.connect(self.addSelectedInputVar)
47         self.removeInputVarButton.clicked.connect(self.removeSelectedInputVar)
48         self.newInputVarButton.clicked.connect(self.newInputVar)
49         self.addOutputVarButton.clicked.connect(self.addSelectedOutputVar)
50         self.removeOutputVarButton.clicked.connect(self.removeSelectedOutputVar)
51         self.newOutputVarButton.clicked.connect( self.newOutputVar)
52         self.loadVarsButton.clicked.connect(self.loadVars)
53         self.saveVarsButtonself.clicked.connect(self.saveVars)
54         self.refEntry = None
55
56     def setExchangeVariables(self, exchangeVariables):
57         if exchangeVariables.refEntry is not None:
58             self._initPotentialVariables(exchangeVariables.refEntry)
59         self.selectedInputVarListWidget.addItems([x.name for x in exchangeVariables.inputVarList])
60         self.selectedOutputVarListWidget.addItems([x.name for x in exchangeVariables.outputVarList])
61
62     def initPotentialVariablesFromSelection(self):
63         entries = salome.sg.getAllSelected()
64         if len(entries) != 1 :
65             QMessageBox.warning(self, self.tr("Error"),
66                                 self.tr("One item must be selected in the object browser"))
67             return
68         selectedEntry = entries[0]
69         self._initPotentialVariables(selectedEntry)
70
71     def _initPotentialVariables(self, entry):
72         sobj = getStudyEditor().study.FindObjectID(entry)
73         if sobj is None:
74             QMessageBox.warning(self, self.tr("Error"),
75                                 self.tr('No item at entry %s' % entry))
76             return
77         exchangeVariables = study_exchange_vars.getExchangeVariablesFromSObject(sobj)
78         if exchangeVariables is None:
79             QMessageBox.warning(self, self.tr("Error"),
80                                 self.tr('Item at entry %s is not a valid '
81                                               '"Variable List" object' % entry))
82             return
83         self.refEntry = entry
84         self.varListObjLineEdit.setText(sobj.GetName())
85         self.allInputVarListWidget.clear()
86         self.allOutputVarListWidget.clear()
87         self.allInputVarListWidget.addItems([x.name for x in exchangeVariables.inputVarList])
88         self.allOutputVarListWidget.addItems([x.name for x in exchangeVariables.outputVarList])
89
90     def addSelectedInputVar(self):
91         for item in self.allInputVarListWidget.selectedItems():
92             self.selectedInputVarListWidget.addItem(QListWidgetItem(item))
93
94     def removeSelectedInputVar(self):
95         for item in self.selectedInputVarListWidget.selectedItems():
96             self.selectedInputVarListWidget.takeItem(self.selectedInputVarListWidget.row(item))
97
98     def newInputVar(self):
99         newItem = QListWidgetItem("TO EDIT!")
100         newItem.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEditable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled)
101         self.selectedInputVarListWidget.addItem(newItem);
102
103     def addSelectedOutputVar(self):
104         for item in self.allOutputVarListWidget.selectedItems():
105             self.selectedOutputVarListWidget.addItem(QListWidgetItem(item))
106
107     def removeSelectedOutputVar(self):
108         for item in self.selectedOutputVarListWidget.selectedItems():
109             self.selectedOutputVarListWidget.takeItem(self.selectedOutputVarListWidget.row(item))
110
111     def newOutputVar(self):
112         newItem = QListWidgetItem("TO EDIT!")
113         newItem.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEditable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled)
114         self.selectedOutputVarListWidget.addItem(newItem);
115
116     def getSelectedExchangeVariables(self):
117         inputVarList = []
118         outputVarList = []
119         for row in range(self.selectedInputVarListWidget.count()):
120             name = str(self.selectedInputVarListWidget.item(row).text())
121             inputVarList.append(study_exchange_vars.Variable(name))
122         for row in range(self.selectedOutputVarListWidget.count()):
123             name = str(self.selectedOutputVarListWidget.item(row).text())
124             outputVarList.append(study_exchange_vars.Variable(name))
125         return study_exchange_vars.ExchangeVariables(inputVarList, outputVarList, self.refEntry)
126
127     def loadVars(self):
128         filename = QFileDialog.getOpenFileName(self, self.tr("Import variables from file"),
129                                                      os.getenv("HOME"),
130                                                      self.tr("XML Files (*.xml)"))
131         if not filename:
132             return
133         try:
134             filename = str(filename)
135             exchange_variables = study_exchange_vars.loadExchangeVariablesFromXmlFile(filename)
136             self.setExchangeVariables(exchange_variables)
137         except Exception as e:
138             QMessageBox.critical(self, self.tr("Error"),
139                                  self.tr("Cannot load file %s:\n%s" % (filename, e)))
140
141     def saveVars(self):
142         default = os.path.join(os.getenv("HOME"), "vars.xml")
143         filename = QFileDialog.getSaveFileName(self, self.tr("Export variables to file"),
144                                                      default, self.tr("XML Files (*.xml)"))
145         if not filename:
146             return
147         try:
148             filename = str(filename)
149             exchange_variables = self.getSelectedExchangeVariables()
150             exchange_variables.saveToXmlFile(filename)
151         except Exception as e:
152             QMessageBox.critical(self, self.tr("Error"),
153                                  self.tr("Cannot save file %s:\n%s" % (filename, e)))