From c2266a7447150615ba9218a1c950c2cfa61734e3 Mon Sep 17 00:00:00 2001 From: boulant Date: Wed, 26 Oct 2011 14:25:31 +0000 Subject: [PATCH] IMP: modification of the rule to generate py from ui using pyuic ADD: new file to help in creation of standard dialog box in python context. --- src/GUI_PY/Makefile.am | 30 +++++-- src/GUI_PY/genericdialog.py | 131 ++++++++++++++++++++++++++++++ src/GUI_PY/genericdialog.ui | 89 +++++++++++++++++++++ src/GUI_PY/mytestdialog.py | 154 ++++++++++++++++++++++++++++++++++++ src/GUI_PY/mytestdialog.ui | 69 ++++++++++++++++ src/GUI_PY/selectvars.py | 2 +- 6 files changed, 468 insertions(+), 7 deletions(-) create mode 100644 src/GUI_PY/genericdialog.py create mode 100644 src/GUI_PY/genericdialog.ui create mode 100644 src/GUI_PY/mytestdialog.py create mode 100644 src/GUI_PY/mytestdialog.ui diff --git a/src/GUI_PY/Makefile.am b/src/GUI_PY/Makefile.am index aafc45617..5043bcd7b 100644 --- a/src/GUI_PY/Makefile.am +++ b/src/GUI_PY/Makefile.am @@ -25,15 +25,33 @@ include $(top_srcdir)/adm_local/unix/make_common_starter.am 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 _ui.py when the ui file name is .ui. When +# implementing the dialog, you generally create a third file named +# .py that uses the UI_ classe defined in the +# generated file _ui.py. +%_ui.py:%.ui + $(PYUIC) -x $< -o $@ diff --git a/src/GUI_PY/genericdialog.py b/src/GUI_PY/genericdialog.py new file mode 100644 index 000000000..4ece825e6 --- /dev/null +++ b/src/GUI_PY/genericdialog.py @@ -0,0 +1,131 @@ +# -*- 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(): + 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. + + - 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() diff --git a/src/GUI_PY/genericdialog.ui b/src/GUI_PY/genericdialog.ui new file mode 100644 index 000000000..3b151e58a --- /dev/null +++ b/src/GUI_PY/genericdialog.ui @@ -0,0 +1,89 @@ + + GenericDialog + + + + 0 + 0 + 498 + 352 + + + + Dialog + + + + 9 + + + 6 + + + + + 0 + + + 6 + + + + + QFrame::NoFrame + + + QFrame::Raised + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + + + + + buttonBox + accepted() + GenericDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + GenericDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/GUI_PY/mytestdialog.py b/src/GUI_PY/mytestdialog.py new file mode 100644 index 000000000..2aa9e08e0 --- /dev/null +++ b/src/GUI_PY/mytestdialog.py @@ -0,0 +1,154 @@ +# -*- 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() diff --git a/src/GUI_PY/mytestdialog.ui b/src/GUI_PY/mytestdialog.ui new file mode 100644 index 000000000..ca89393fb --- /dev/null +++ b/src/GUI_PY/mytestdialog.ui @@ -0,0 +1,69 @@ + + MyTestDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + 9 + + + 6 + + + + + 0 + + + 6 + + + + + 0 + + + 6 + + + + + TextLabel + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + diff --git a/src/GUI_PY/selectvars.py b/src/GUI_PY/selectvars.py index d6e511681..e3a741402 100644 --- a/src/GUI_PY/selectvars.py +++ b/src/GUI_PY/selectvars.py @@ -31,7 +31,7 @@ from salome.kernel.parametric import study_exchange_vars # Dialog box for variables selection # # ---------------------------------- # -from SelectVarsDialog import Ui_SelectVarsDialog +from SelectVarsDialog_ui import Ui_SelectVarsDialog class MySelectVarsDialog(Ui_SelectVarsDialog, QtGui.QDialog): -- 2.39.2