Back to index

salome-gui  6.5.0
selectvars.py
Go to the documentation of this file.
00001 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 #
00003 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 #
00006 # This library is free software; you can redistribute it and/or
00007 # modify it under the terms of the GNU Lesser General Public
00008 # License as published by the Free Software Foundation; either
00009 # version 2.1 of the License.
00010 #
00011 # This library is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 # Lesser General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU Lesser General Public
00017 # License along with this library; if not, write to the Free Software
00018 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 #
00020 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 #
00022 
00023 import os
00024 from PyQt4 import QtGui, QtCore
00025 from PyQt4.QtCore import Qt
00026 
00027 import salome
00028 from salome.kernel.studyedit import getStudyEditor
00029 from salome.kernel.parametric import study_exchange_vars
00030 
00031 # ---------------------------------- #
00032 # Dialog box for variables selection #
00033 # ---------------------------------- #
00034 
00035 from SelectVarsDialog_ui import Ui_SelectVarsDialog
00036 
00037 class MySelectVarsDialog(Ui_SelectVarsDialog, QtGui.QDialog):
00038 
00039     def __init__(self, parent = None, modal = 0):
00040         QtGui.QDialog.__init__(self, parent)
00041         Ui_SelectVarsDialog.__init__(self)
00042         self.setupUi(self)
00043         self.connect(self.cancelButton, QtCore.SIGNAL("clicked()"), self.close)
00044         self.connect(self.OKButton, QtCore.SIGNAL("clicked()"), self.accept)
00045         self.connect(self.selectButton, QtCore.SIGNAL("clicked()"), self.initPotentialVariablesFromSelection)
00046         self.connect(self.addInputVarButton, QtCore.SIGNAL("clicked()"), self.addSelectedInputVar)
00047         self.connect(self.removeInputVarButton, QtCore.SIGNAL("clicked()"), self.removeSelectedInputVar)
00048         self.connect(self.newInputVarButton, QtCore.SIGNAL("clicked()"), self.newInputVar)
00049         self.connect(self.addOutputVarButton, QtCore.SIGNAL("clicked()"), self.addSelectedOutputVar)
00050         self.connect(self.removeOutputVarButton, QtCore.SIGNAL("clicked()"), self.removeSelectedOutputVar)
00051         self.connect(self.newOutputVarButton, QtCore.SIGNAL("clicked()"), self.newOutputVar)
00052         self.connect(self.loadVarsButton, QtCore.SIGNAL("clicked()"), self.loadVars)
00053         self.connect(self.saveVarsButton, QtCore.SIGNAL("clicked()"), self.saveVars)
00054         self.refEntry = None
00055 
00056     def setExchangeVariables(self, exchangeVariables):
00057         if exchangeVariables.refEntry is not None:
00058             self._initPotentialVariables(exchangeVariables.refEntry)
00059         self.selectedInputVarListWidget.addItems([x.name for x in exchangeVariables.inputVarList])
00060         self.selectedOutputVarListWidget.addItems([x.name for x in exchangeVariables.outputVarList])
00061 
00062     def initPotentialVariablesFromSelection(self):
00063         entries = salome.sg.getAllSelected()
00064         if len(entries) != 1 :
00065             QtGui.QMessageBox.warning(self, self.tr("Error"),
00066                                       self.tr("One item must be selected in the object browser"))
00067             return
00068         selectedEntry = entries[0]
00069         self._initPotentialVariables(selectedEntry)
00070 
00071     def _initPotentialVariables(self, entry):
00072         sobj = getStudyEditor().study.FindObjectID(entry)
00073         if sobj is None:
00074             QtGui.QMessageBox.warning(self, self.tr("Error"),
00075                                       self.tr('No item at entry %s' % entry))
00076             return
00077         exchangeVariables = study_exchange_vars.getExchangeVariablesFromSObject(sobj)
00078         if exchangeVariables is None:
00079             QtGui.QMessageBox.warning(self, self.tr("Error"),
00080                                       self.tr('Item at entry %s is not a valid '
00081                                               '"Variable List" object' % entry))
00082             return
00083         self.refEntry = entry
00084         self.varListObjLineEdit.setText(sobj.GetName())
00085         self.allInputVarListWidget.clear()
00086         self.allOutputVarListWidget.clear()
00087         self.allInputVarListWidget.addItems([x.name for x in exchangeVariables.inputVarList])
00088         self.allOutputVarListWidget.addItems([x.name for x in exchangeVariables.outputVarList])
00089 
00090     def addSelectedInputVar(self):
00091         for item in self.allInputVarListWidget.selectedItems():
00092             self.selectedInputVarListWidget.addItem(QtGui.QListWidgetItem(item))
00093 
00094     def removeSelectedInputVar(self):
00095         for item in self.selectedInputVarListWidget.selectedItems():
00096             self.selectedInputVarListWidget.takeItem(self.selectedInputVarListWidget.row(item))
00097 
00098     def newInputVar(self):
00099         newItem = QtGui.QListWidgetItem("TO EDIT!")
00100         newItem.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEditable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled)
00101         self.selectedInputVarListWidget.addItem(newItem);
00102 
00103     def addSelectedOutputVar(self):
00104         for item in self.allOutputVarListWidget.selectedItems():
00105             self.selectedOutputVarListWidget.addItem(QtGui.QListWidgetItem(item))
00106 
00107     def removeSelectedOutputVar(self):
00108         for item in self.selectedOutputVarListWidget.selectedItems():
00109             self.selectedOutputVarListWidget.takeItem(self.selectedOutputVarListWidget.row(item))
00110 
00111     def newOutputVar(self):
00112         newItem = QtGui.QListWidgetItem("TO EDIT!")
00113         newItem.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEditable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled)
00114         self.selectedOutputVarListWidget.addItem(newItem);
00115 
00116     def getSelectedExchangeVariables(self):
00117         inputVarList = []
00118         outputVarList = []
00119         for row in range(self.selectedInputVarListWidget.count()):
00120             name = str(self.selectedInputVarListWidget.item(row).text())
00121             inputVarList.append(study_exchange_vars.Variable(name))
00122         for row in range(self.selectedOutputVarListWidget.count()):
00123             name = str(self.selectedOutputVarListWidget.item(row).text())
00124             outputVarList.append(study_exchange_vars.Variable(name))
00125         return study_exchange_vars.ExchangeVariables(inputVarList, outputVarList, self.refEntry)
00126 
00127     def loadVars(self):
00128         filename = QtGui.QFileDialog.getOpenFileName(self, self.tr("Import variables from file"),
00129                                                      os.getenv("HOME"),
00130                                                      self.tr("XML Files (*.xml)"))
00131         if not filename:
00132             return
00133         try:
00134             filename = str(filename)
00135             exchange_variables = study_exchange_vars.loadExchangeVariablesFromXmlFile(filename)
00136             self.setExchangeVariables(exchange_variables)
00137         except Exception, e:
00138             QtGui.QMessageBox.critical(self, self.tr("Error"),
00139                                        self.tr("Cannot load file %s:\n%s" % (filename, e)))
00140 
00141     def saveVars(self):
00142         default = os.path.join(os.getenv("HOME"), "vars.xml")
00143         filename = QtGui.QFileDialog.getSaveFileName(self, self.tr("Export variables to file"),
00144                                                      default, self.tr("XML Files (*.xml)"))
00145         if not filename:
00146             return
00147         try:
00148             filename = str(filename)
00149             exchange_variables = self.getSelectedExchangeVariables()
00150             exchange_variables.saveToXmlFile(filename)
00151         except Exception, e:
00152             QtGui.QMessageBox.critical(self, self.tr("Error"),
00153                                        self.tr("Cannot save file %s:\n%s" % (filename, e)))