Salome HOME
9be5cee62bd2ce4e626445a6651d1ca2ec7abc1b
[modules/gui.git] / src / GUI_PY / genericdialog.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2015  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 __author__="gboulant"
22 __date__ ="$31 mars 2010 17:09:53$"
23
24 from PyQt4.QtGui import QDialog, QMessageBox
25
26 from genericdialog_ui import Ui_GenericDialog
27
28 class GenericDialog(QDialog):
29     """
30     This is an abstract generic dialog box for implementing default and
31     generic behaviour of dialog windows.
32     Note that this class can be instantiated but can't be used because the
33     OK behaviour is a default one indicating that the checkData function should
34     be implemented in a derived class. The general interface that should be
35     implemented in derived class is the MVC pattern:
36
37     - setData(<a_data_model_object>):
38       to initiate the values of the dialog windows from a given data model.
39
40     - boolean checkData():
41       to verify that the data are valid and notify user if not.
42
43     - <a_data_model_object> getData():
44       to get the valid data model from values read in the dialog window.
45     """
46
47     __wasOk = False
48     checkDataMessage = ""
49
50     def __init__(self,parent = None,name = None,modal = 0,fl = 0):
51         QDialog.__init__(self,parent)
52         # Set up the user interface from Designer.
53         self.__ui = Ui_GenericDialog()
54         self.__ui.setupUi(self)
55
56     def getPanel(self):
57         '''
58         This returns the central panel where to draw the custom dialog
59         widgets.
60         '''
61         return self.__ui.myCenterPanel
62
63     def getButtonBox(self):
64         return self.__ui.buttonBox
65
66     def accept(self):
67         """
68         Slot function connected to the button OK
69         """
70         if not self.checkData():
71             QMessageBox.warning( self, "Alerte", self.checkDataMessage)
72             return
73         self.__wasOk = True
74         self.hide()
75
76     def displayAndWait(self):
77         """
78         This function can be used to display the dialog in the case
79         where the dialog is modal and does not need interactivity with
80         parent windows and other part of the application. When called,
81         the dialog is raised visible and keep waiting until the button
82         OK or the button CANCEL is pressed. Then the flow can go on
83         (see an example of implementation in the tests functions at
84         the end of this file).
85         In the general case, in particular if you need interaction
86         with the graphical framework (clic on widgets embedded in
87         other dialogs), you should used instead the show() command
88         (for a non modal dialog) or the open() command (for a window
89         modal dialog, i.e. a dialog that can not interact with its
90         direct parent but can interact with the other parts).
91         """
92         self.__wasOk = False
93         self.exec_()
94
95     def wasOk(self):
96         """
97         Return True if the button OK was pressed to close the dialog windows.
98         """
99         return self.__wasOk
100
101     def checkData(self):
102         """
103         This function should be implemented in a derived class. It should return
104         True in the case where the data are estimated to be valid data.
105         """
106         self.checkDataMessage = "The checkData() function is not implemented yet"
107         return True
108
109 #
110 # ==============================================================================
111 # Basic use cases and unit test functions
112 # ==============================================================================
113 #
114 def TEST_GenericDialog():
115     import sys
116     from PyQt4.QtGui import QApplication
117     from PyQt4.QtCore import QObject, SIGNAL, SLOT
118     app = QApplication(sys.argv)
119     QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
120
121     dlg=GenericDialog()
122     dlg.displayAndWait()
123     if dlg.wasOk():
124         print "OK has been pressed"
125     else:
126         print "Cancel has been pressed"
127         
128 if __name__ == "__main__":
129     TEST_GenericDialog()