1 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 from qtsalome import *
28 from salome.kernel.studyedit import getStudyEditor
29 from salome.kernel.parametric import study_exchange_vars
31 # ---------------------------------- #
32 # Dialog box for variables selection #
33 # ---------------------------------- #
35 from SelectVarsDialog_ui import Ui_SelectVarsDialog
37 class MySelectVarsDialog(Ui_SelectVarsDialog, QDialog):
39 def __init__(self, parent = None, modal = 0):
40 QDialog.__init__(self, parent)
41 Ui_SelectVarsDialog.__init__(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)
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])
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"))
68 selectedEntry = entries[0]
69 self._initPotentialVariables(selectedEntry)
71 def _initPotentialVariables(self, entry):
72 sobj = getStudyEditor().study.FindObjectID(entry)
74 QMessageBox.warning(self, self.tr("Error"),
75 self.tr('No item at entry %s' % entry))
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))
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])
90 def addSelectedInputVar(self):
91 for item in self.allInputVarListWidget.selectedItems():
92 self.selectedInputVarListWidget.addItem(QListWidgetItem(item))
94 def removeSelectedInputVar(self):
95 for item in self.selectedInputVarListWidget.selectedItems():
96 self.selectedInputVarListWidget.takeItem(self.selectedInputVarListWidget.row(item))
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);
103 def addSelectedOutputVar(self):
104 for item in self.allOutputVarListWidget.selectedItems():
105 self.selectedOutputVarListWidget.addItem(QListWidgetItem(item))
107 def removeSelectedOutputVar(self):
108 for item in self.selectedOutputVarListWidget.selectedItems():
109 self.selectedOutputVarListWidget.takeItem(self.selectedOutputVarListWidget.row(item))
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);
116 def getSelectedExchangeVariables(self):
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)
128 filename = QFileDialog.getOpenFileName(self, self.tr("Import variables from file"),
130 self.tr("XML Files (*.xml)"))
134 filename = str(filename)
135 exchange_variables = study_exchange_vars.loadExchangeVariablesFromXmlFile(filename)
136 self.setExchangeVariables(exchange_variables)
138 QMessageBox.critical(self, self.tr("Error"),
139 self.tr("Cannot load file %s:\n%s" % (filename, e)))
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)"))
148 filename = str(filename)
149 exchange_variables = self.getSelectedExchangeVariables()
150 exchange_variables.saveToXmlFile(filename)
152 QMessageBox.critical(self, self.tr("Error"),
153 self.tr("Cannot save file %s:\n%s" % (filename, e)))