Salome HOME
Updated copyright comment
[modules/gui.git] / src / GUI_PY / genericdialog.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2024  CEA, EDF, 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 from qtsalome import QDialog, QMessageBox
22 from salome.gui.genericdialog_ui import Ui_GenericDialog
23
24
25 class GenericDialog(QDialog):
26     """
27     This is an abstract generic dialog box for implementing default and
28     generic behaviour of dialog windows.
29     Note that this class can be instantiated but can't be used because the
30     OK behaviour is a default one indicating that the checkData function should
31     be implemented in a derived class. The general interface that should be
32     implemented in derived class is the MVC pattern:
33
34     - setData(<a_data_model_object>):
35       to initiate the values of the dialog windows from a given data model.
36
37     - boolean checkData():
38       to verify that the data are valid and notify user if not.
39
40     - <a_data_model_object> getData():
41       to get the valid data model from values read in the dialog window.
42     """
43
44     __wasOk = False
45     checkDataMessage = ""
46
47     def __init__(self,parent = None,name = None,modal = 0,fl = 0):
48         QDialog.__init__(self,parent)
49         # Set up the user interface from Designer.
50         self.__ui = Ui_GenericDialog()
51         self.__ui.setupUi(self)
52
53     def getPanel(self):
54         '''
55         This returns the central panel where to draw the custom dialog
56         widgets.
57         '''
58         return self.__ui.myCenterPanel
59
60     def getButtonBox(self):
61         return self.__ui.buttonBox
62
63     def accept(self):
64         """
65         Slot function connected to the button OK
66         """
67         if not self.checkData():
68             QMessageBox.warning( self, "Alerte", self.checkDataMessage)
69             return
70         self.__wasOk = True
71         self.hide()
72
73     def displayAndWait(self):
74         """
75         This function can be used to display the dialog in the case
76         where the dialog is modal and does not need interactivity with
77         parent windows and other part of the application. When called,
78         the dialog is raised visible and keep waiting until the button
79         OK or the button CANCEL is pressed. Then the flow can go on
80         (see an example of implementation in the tests functions at
81         the end of this file).
82         In the general case, in particular if you need interaction
83         with the graphical framework (clic on widgets embedded in
84         other dialogs), you should used instead the show() command
85         (for a non modal dialog) or the open() command (for a window
86         modal dialog, i.e. a dialog that can not interact with its
87         direct parent but can interact with the other parts).
88         """
89         self.__wasOk = False
90         self.exec_()
91
92     def wasOk(self):
93         """
94         Return True if the button OK was pressed to close the dialog windows.
95         """
96         return self.__wasOk
97
98     def checkData(self):
99         """
100         This function should be implemented in a derived class. It should return
101         True in the case where the data are estimated to be valid data.
102         """
103         self.checkDataMessage = "The checkData() function is not implemented yet"
104         return True
105
106 #
107 # ==============================================================================
108 # Basic use cases and unit test functions
109 # ==============================================================================
110 #
111 def TEST_GenericDialog():
112     import sys
113     from qtsalome import QApplication
114     app = QApplication(sys.argv)
115     app.lastWindowClosed.connect(app.quit)
116
117     dlg=GenericDialog()
118     dlg.displayAndWait()
119     if dlg.wasOk():
120         print("OK has been pressed")
121     else:
122         print("Cancel has been pressed")
123         
124 if __name__ == "__main__":
125     TEST_GenericDialog()