Back to index

salome-gui  6.5.0
genericdialog.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 PyQt4.QtGui import QDialog, QMessageBox
00025 
00026 from genericdialog_ui import Ui_GenericDialog
00027 
00028 class GenericDialog(QDialog):
00029     """
00030     This is an abstract generic dialog box for implementing default and
00031     generic behaviour of dialog windows.
00032     Note that this class can be instantiated but can't be used because the
00033     OK behaviour is a default one indicating that the checkData function should
00034     be implemented in a derived class. The general interface that should be
00035     implemented in derived class is the MVC pattern:
00036 
00037     - setData(<a_data_model_object>):
00038       to initiate the values of the dialog windows from a given data model.
00039 
00040     - boolean checkData():
00041       to verify that the data are valid and notify user if not.
00042 
00043     - <a_data_model_object> getData():
00044       to get the valid data model from values read in the dialog window.
00045     """
00046 
00047     __wasOk = False
00048     checkDataMessage = ""
00049 
00050     def __init__(self,parent = None,name = None,modal = 0,fl = 0):
00051         QDialog.__init__(self,parent)
00052         # Set up the user interface from Designer.
00053         self.__ui = Ui_GenericDialog()
00054         self.__ui.setupUi(self)
00055 
00056     def getPanel(self):
00057         '''
00058         This returns the central panel where to draw the custom dialog
00059         widgets.
00060         '''
00061         return self.__ui.myCenterPanel
00062 
00063     def getButtonBox(self):
00064         return self.__ui.buttonBox
00065 
00066     def accept(self):
00067         """
00068         Slot function connected to the button OK
00069         """
00070         if not self.checkData():
00071             QMessageBox.warning( self, "Alerte", self.checkDataMessage)
00072             return
00073         self.__wasOk = True
00074         self.hide()
00075 
00076     def displayAndWait(self):
00077         """
00078         This function can be used to display the dialog in the case
00079         where the dialog is modal and does not need interactivity with
00080         parent windows and other part of the application. When called,
00081         the dialog is raised visible and keep waiting until the button
00082         OK or the button CANCEL is pressed. Then the flow can go on
00083         (see an example of implementation in the tests functions at
00084         the end of this file).
00085         In the general case, in particular if you need interaction
00086         with the graphical framework (clic on widgets embedded in
00087         other dialogs), you should used instead the show() command
00088         (for a non modal dialog) or the open() command (for a window
00089         modal dialog, i.e. a dialog that can not interact with its
00090         direct parent but can interact with the other parts).
00091         """
00092         self.__wasOk = False
00093         self.exec_()
00094 
00095     def wasOk(self):
00096         """
00097         Return True if the button OK was pressed to close the dialog windows.
00098         """
00099         return self.__wasOk
00100 
00101     def checkData(self):
00102         """
00103         This function should be implemented in a derived class. It should return
00104         True in the case where the data are estimated to be valid data.
00105         """
00106         self.checkDataMessage = "The checkData() function is not implemented yet"
00107         return True
00108 
00109 #
00110 # ==============================================================================
00111 # Basic use cases and unit test functions
00112 # ==============================================================================
00113 #
00114 def TEST_GenericDialog():
00115     import sys
00116     from PyQt4.QtGui import QApplication
00117     from PyQt4.QtCore import QObject, SIGNAL, SLOT
00118     app = QApplication(sys.argv)
00119     QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
00120 
00121     dlg=GenericDialog()
00122     dlg.displayAndWait()
00123     if dlg.wasOk():
00124         print "OK has been pressed"
00125     else:
00126         print "Cancel has been pressed"
00127         
00128 if __name__ == "__main__":
00129     TEST_GenericDialog()