ADD: new file to help in creation of standard dialog box in python context.
mypkgpythondir = $(salomepythondir)/salome/gui
# Python modules to be installed
-mypkgpython_PYTHON = __init__.py \
- selectvars.py
+mypkgpython_PYTHON = \
+ __init__.py \
+ SelectVarsDialog_ui.py \
+ selectvars.py \
+ genericdialog_ui.py \
+ genericdialog.py \
+ mytestdialog_ui.py \
+ mytestdialog.py
-PYUIC_FILES = SelectVarsDialog.py
+PYUIC_FILES = \
+ SelectVarsDialog_ui.py \
+ genericdialog_ui.py \
+ mytestdialog_ui.py
nodist_mypkgpython_PYTHON = $(PYUIC_FILES)
CLEANFILES = $(PYUIC_FILES)
-EXTRA_DIST += SelectVarsDialog.ui
+EXTRA_DIST += \
+ SelectVarsDialog.ui \
+ genericdialog.ui \
+ mytestdialog.ui
-%.py:%.ui
- $(PYUIC) $< -o $@
+# This rule indicates that some python files are generated from ui
+# files using the PYUIC utility. The convention is to create a file
+# name <basename>_ui.py when the ui file name is <basename>.ui. When
+# implementing the dialog, you generally create a third file named
+# <basename>.py that uses the UI_<basename> classe defined in the
+# generated file <basename>_ui.py.
+%_ui.py:%.ui
+ $(PYUIC) -x $< -o $@
--- /dev/null
+# -*- coding: iso-8859-1 -*-
+# Copyright (C) 2010 EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+# -* Makefile *-
+#
+
+__author__="gboulant"
+__date__ ="$31 mars 2010 17:09:53$"
+
+from PyQt4.QtGui import QDialog, QMessageBox
+
+from genericdialog_ui import Ui_GenericDialog
+
+class GenericDialog(QDialog):
+ """
+ This is an abstract generic dialog box for implementing default and
+ generic behaviour of dialog windows.
+ Note that this class can be instantiated but can't be used because the
+ OK behaviour is a default one indicating that the checkData function should
+ be implemented in a derived class. The general interface that should be
+ implemented in derived class is the MVC pattern:
+
+ - setData(<a_data_model_object>):
+ to initiate the values of the dialog windows from a given data model.
+
+ - boolean checkData():
+ to verify that the data are valid and notify user if not.
+
+ - <a_data_model_object> getData():
+ to get the valid data model from values read in the dialog window.
+ """
+
+ __wasOk = False
+ checkDataMessage = ""
+
+ def __init__(self,parent = None,name = None,modal = 0,fl = 0):
+ QDialog.__init__(self,parent)
+ # Set up the user interface from Designer.
+ self.__ui = Ui_GenericDialog()
+ self.__ui.setupUi(self)
+
+ def getPanel(self):
+ '''
+ This returns the central panel where to draw the custom dialog
+ widgets.
+ '''
+ return self.__ui.myCenterPanel
+
+ def getButtonBox(self):
+ return self.__ui.buttonBox
+
+ def accept(self):
+ """
+ Slot function connected to the button OK
+ """
+ if not self.checkData():
+ QMessageBox.warning( self, "Alerte", self.checkDataMessage)
+ return
+ self.__wasOk = True
+ self.hide()
+
+ def displayAndWait(self):
+ """
+ This function can be used to display the dialog in the case
+ where the dialog is modal and does not need interactivity with
+ parent windows and other part of the application. When called,
+ the dialog is raised visible and keep waiting until the button
+ OK or the button CANCEL is pressed. Then the flow can go on
+ (see an example of implementation in the tests functions at
+ the end of this file).
+ In the general case, in particular if you need interaction
+ with the graphical framework (clic on widgets embedded in
+ other dialogs), you should used instead the show() command
+ (for a non modal dialog) or the open() command (for a window
+ modal dialog, i.e. a dialog that can not interact with its
+ direct parent but can interact with the other parts).
+ """
+ self.__wasOk = False
+ self.exec_()
+
+ def wasOk(self):
+ """
+ Return True if the button OK was pressed to close the dialog windows.
+ """
+ return self.__wasOk
+
+ def checkData(self):
+ """
+ This function should be implemented in a derived class. It should return
+ True in the case where the data are estimated to be valid data.
+ """
+ self.checkDataMessage = "The checkData() function is not implemented yet"
+ return True
+
+#
+# ==============================================================================
+# Basic use cases and unit test functions
+# ==============================================================================
+#
+def TEST_GenericDialog():
+ import sys
+ from PyQt4.QtGui import QApplication
+ from PyQt4.QtCore import QObject, SIGNAL, SLOT
+ app = QApplication(sys.argv)
+ QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
+
+ dlg=GenericDialog()
+ dlg.displayAndWait()
+ if dlg.wasOk():
+ print "OK has been pressed"
+ else:
+ print "Cancel has been pressed"
+
+if __name__ == "__main__":
+ TEST_GenericDialog()
--- /dev/null
+<ui version="4.0" >
+ <class>GenericDialog</class>
+ <widget class="QDialog" name="GenericDialog" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>498</width>
+ <height>352</height>
+ </rect>
+ </property>
+ <property name="windowTitle" >
+ <string>Dialog</string>
+ </property>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>9</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QFrame" name="myCenterPanel" >
+ <property name="frameShape" >
+ <enum>QFrame::NoFrame</enum>
+ </property>
+ <property name="frameShadow" >
+ <enum>QFrame::Raised</enum>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QDialogButtonBox" name="buttonBox" >
+ <property name="orientation" >
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="standardButtons" >
+ <set>QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok</set>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections>
+ <connection>
+ <sender>buttonBox</sender>
+ <signal>accepted()</signal>
+ <receiver>GenericDialog</receiver>
+ <slot>accept()</slot>
+ <hints>
+ <hint type="sourcelabel" >
+ <x>248</x>
+ <y>254</y>
+ </hint>
+ <hint type="destinationlabel" >
+ <x>157</x>
+ <y>274</y>
+ </hint>
+ </hints>
+ </connection>
+ <connection>
+ <sender>buttonBox</sender>
+ <signal>rejected()</signal>
+ <receiver>GenericDialog</receiver>
+ <slot>reject()</slot>
+ <hints>
+ <hint type="sourcelabel" >
+ <x>316</x>
+ <y>260</y>
+ </hint>
+ <hint type="destinationlabel" >
+ <x>286</x>
+ <y>274</y>
+ </hint>
+ </hints>
+ </connection>
+ </connections>
+</ui>
--- /dev/null
+# -*- coding: iso-8859-1 -*-
+# Copyright (C) 2010 EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+# -* Makefile *-
+#
+
+__author__="gboulant"
+__date__ ="$31 mars 2010 17:09:53$"
+
+from mytestdialog_ui import Ui_MyTestDialog
+from genericdialog import GenericDialog
+
+class MyTestDialog(GenericDialog):
+ """
+ This class is to illustrate the usage of the GenericDialog to implement
+ the dialog windows of the application with a common design template provided
+ by the generic class GenericDialog.
+ """
+ def __init__(self, parent=None, name="MyTestDialog"):
+ GenericDialog.__init__(self, parent, name)
+ # Set up the user interface from Designer.
+ self.ui = Ui_MyTestDialog()
+ # BE CAREFULL HERE, the ui form is NOT put in the global dialog (already
+ # containing some generic widgets) but in the center panel created in the
+ # GenericDialog as a void container for the form. The MyTestDialog form
+ # is supposed here to create only the widgets to be placed in the center
+ # panel
+ self.ui.setupUi(self.getPanel())
+
+ #
+ # We implement here the interface of the MVC pattern
+ #
+ def setData(self, name):
+ """
+ This function implements the mapping from the data model to the widgets
+ """
+ self.ui.txtName.setText(name)
+
+ def checkData(self):
+ """
+ This function implements the control to be done on the values contained
+ in the widgets when trying to validate the dialog window (click OK first
+ trigs this function).
+ """
+ if ( self.ui.txtName.text().trimmed() == "" ):
+ self.checkDataMessage = "The name can't be void"
+ return False
+ return True
+
+ def getData(self):
+ """
+ This function implements the mapping from the widgets to the data model
+ """
+ name = str(self.ui.txtName.text().trimmed().toUtf8())
+ # _MEM_: note here that (i) the utf8 format is used and (ii) we must not
+ # forget to convert to a standard python string (instead of a QString).
+ return name
+
+
+from PyQt4.QtCore import SIGNAL
+class MyTestDialogWithSignals(MyTestDialog):
+ """
+ This class is to illustrate the usage of the GenericDialog in the
+ case where the dialog windows is not modal. In such a case, the
+ controller must be warn of close events using Qt signals.
+ """
+ def __init__(self, parent=None, name="MyTestDialogWithSignals"):
+ MyTestDialog.__init__(self, parent, name)
+
+ def accept(self):
+ """
+ This function is the slot connected to the the OK button
+ (click event of the OK button).
+ """
+ # The dialog is raised in a non modal mode (for example, to
+ # get interactivity with the parents windows. Then we have to
+ # emit a signal to warn the parent observer that the dialog
+ # has been validated so that it can process the event
+ MyTestDialog.accept(self)
+ if self.wasOk():
+ self.emit(SIGNAL('inputValidated()'))
+
+
+
+#
+# ==============================================================================
+# Basic use case
+# ==============================================================================
+#
+
+def TEST_MyTestDialog_modal():
+ import sys
+ from PyQt4.QtCore import QObject, SIGNAL, SLOT
+ from PyQt4.QtGui import QApplication
+ app = QApplication(sys.argv)
+ QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
+
+ dlg=MyTestDialog()
+ dlg.setData("A default name")
+ dlg.displayAndWait()
+ if dlg.wasOk():
+ name = dlg.getData()
+ print "The name has been modified to",name
+
+
+class DialogListener:
+ def onProcessEvent(self):
+ print "onProcessEvent(): OK has been pressed"
+ import sys
+ sys.exit(0)
+
+
+def TEST_MyTestDialog_non_modal():
+ import sys
+ from PyQt4.QtCore import QObject, SIGNAL, SLOT
+ from PyQt4.QtGui import QApplication
+ app = QApplication(sys.argv)
+ QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
+
+ dlg=MyTestDialogWithSignals()
+ # This dialog window will emit a inputValidated() signal when the
+ # OK button is pressed and the data are validated. Then, we
+ # connect this signal to a local slot so that the event can be
+ # processed.
+ dlgListener = DialogListener()
+ app.connect(dlg, SIGNAL('inputValidated()'), dlgListener.onProcessEvent)
+ # This connect instruction means that the signal inputValidated()
+ # emited by the dlg Qt object will raise a call to the slot
+ # dlgListener.onProcessEvent
+
+ dlg.setData("A default name")
+ dlg.show()
+
+ app.exec_()
+
+if __name__ == "__main__":
+ #TEST_MyTestDialog_modal()
+ TEST_MyTestDialog_non_modal()
--- /dev/null
+<ui version="4.0" >
+ <class>MyTestDialog</class>
+ <widget class="QDialog" name="MyTestDialog" >
+ <property name="geometry" >
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>400</width>
+ <height>300</height>
+ </rect>
+ </property>
+ <property name="windowTitle" >
+ <string>Dialog</string>
+ </property>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>9</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <layout class="QVBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <layout class="QHBoxLayout" >
+ <property name="margin" >
+ <number>0</number>
+ </property>
+ <property name="spacing" >
+ <number>6</number>
+ </property>
+ <item>
+ <widget class="QLabel" name="lblName" >
+ <property name="text" >
+ <string>TextLabel</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLineEdit" name="txtName" />
+ </item>
+ </layout>
+ </item>
+ <item>
+ <spacer>
+ <property name="orientation" >
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeHint" >
+ <size>
+ <width>20</width>
+ <height>40</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
# Dialog box for variables selection #
# ---------------------------------- #
-from SelectVarsDialog import Ui_SelectVarsDialog
+from SelectVarsDialog_ui import Ui_SelectVarsDialog
class MySelectVarsDialog(Ui_SelectVarsDialog, QtGui.QDialog):