Back to index

salome-gui  6.5.0
mytestdialog.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2010-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 __author__="gboulant"
00022 __date__ ="$31 mars 2010 17:09:53$"
00023 
00024 from mytestdialog_ui import Ui_MyTestDialog
00025 from genericdialog import GenericDialog
00026 
00027 class MyTestDialog(GenericDialog):
00028     """
00029     This class is to illustrate the usage of the GenericDialog to implement
00030     the dialog windows of the application with a common design template provided
00031     by the generic class GenericDialog.
00032     """
00033     def __init__(self, parent=None, name="MyTestDialog"):
00034         GenericDialog.__init__(self, parent, name)
00035         # Set up the user interface from Designer.
00036         self.ui = Ui_MyTestDialog()
00037         # BE CAREFULL HERE, the ui form is NOT put in the global dialog (already
00038         # containing some generic widgets) but in the center panel created in the
00039         # GenericDialog as a void container for the form. The MyTestDialog form
00040         # is supposed here to create only the widgets to be placed in the center
00041         # panel
00042         self.ui.setupUi(self.getPanel())
00043 
00044     #
00045     # We implement here the interface of the MVC pattern
00046     #
00047     def setData(self, name):
00048         """
00049         This function implements the mapping from the data model to the widgets
00050         """
00051         self.ui.txtName.setText(name)
00052 
00053     def checkData(self):
00054         """
00055         This function implements the control to be done on the values contained
00056         in the widgets when trying to validate the dialog window (click OK first
00057         trigs this function).
00058         """
00059         if ( self.ui.txtName.text().trimmed() == "" ):
00060             self.checkDataMessage = "The name can't be void"
00061             return False
00062         return True
00063 
00064     def getData(self):
00065         """
00066         This function implements the mapping from the widgets to the data model
00067         """
00068         name = str(self.ui.txtName.text().trimmed().toUtf8())
00069         # _MEM_: note here that (i) the utf8 format is used and (ii) we must not
00070         # forget to convert to a standard python string (instead of a QString).
00071         return name
00072 
00073 
00074 from PyQt4.QtCore import SIGNAL
00075 class MyTestDialogWithSignals(MyTestDialog):
00076     """
00077     This class is to illustrate the usage of the GenericDialog in the
00078     case where the dialog windows is not modal. In such a case, the
00079     controller must be warn of close events using Qt signals.
00080     """
00081     def __init__(self, parent=None, name="MyTestDialogWithSignals"):
00082         MyTestDialog.__init__(self, parent, name)
00083 
00084     def accept(self):
00085         """
00086         This function is the slot connected to the the OK button
00087         (click event of the OK button). 
00088         """
00089         # The dialog is raised in a non modal mode (for example, to
00090         # get interactivity with the parents windows. Then we have to
00091         # emit a signal to warn the parent observer that the dialog
00092         # has been validated so that it can process the event
00093         MyTestDialog.accept(self)
00094         if self.wasOk():
00095             self.emit(SIGNAL('inputValidated()'))
00096 
00097 
00098 
00099 #
00100 # ==============================================================================
00101 # Basic use case
00102 # ==============================================================================
00103 #
00104 
00105 def TEST_MyTestDialog_modal():
00106     import sys
00107     from PyQt4.QtCore import QObject, SIGNAL, SLOT
00108     from PyQt4.QtGui import QApplication
00109     app = QApplication(sys.argv)
00110     QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
00111 
00112     dlg=MyTestDialog()
00113     dlg.setData("A default name")
00114     dlg.displayAndWait()
00115     if dlg.wasOk():
00116         name = dlg.getData()
00117         print "The name has been modified to",name
00118 
00119 
00120 class DialogListener:
00121     def onProcessEvent(self):
00122         print "onProcessEvent(): OK has been pressed"
00123         import sys
00124         sys.exit(0)
00125         
00126 
00127 def TEST_MyTestDialog_non_modal():
00128     import sys
00129     from PyQt4.QtCore import QObject, SIGNAL, SLOT
00130     from PyQt4.QtGui import QApplication
00131     app = QApplication(sys.argv)
00132     QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
00133 
00134     dlg=MyTestDialogWithSignals()
00135     # This dialog window will emit a inputValidated() signal when the
00136     # OK button is pressed and the data are validated. Then, we
00137     # connect this signal to a local slot so that the event can be
00138     # processed.
00139     dlgListener = DialogListener()
00140     app.connect(dlg, SIGNAL('inputValidated()'), dlgListener.onProcessEvent)
00141     # This connect instruction means that the signal inputValidated()
00142     # emited by the dlg Qt object will raise a call to the slot
00143     # dlgListener.onProcessEvent
00144 
00145     dlg.setData("A default name")
00146     dlg.show()
00147 
00148     app.exec_()
00149 
00150 if __name__ == "__main__":
00151     #TEST_MyTestDialog_modal()
00152     TEST_MyTestDialog_non_modal()