Salome HOME
updated copyright message
[modules/gui.git] / src / GUI_PY / mytestdialog.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2023  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 *
22 from salome.gui.genericdialog import GenericDialog
23 from salome.gui.mytestdialog_ui import Ui_MyTestDialog
24
25
26 class MyTestDialog(GenericDialog):
27     """
28     This class is to illustrate the usage of the GenericDialog to implement
29     the dialog windows of the application with a common design template provided
30     by the generic class GenericDialog.
31     """
32     def __init__(self, parent=None, name="MyTestDialog"):
33         GenericDialog.__init__(self, parent, name)
34         # Set up the user interface from Designer.
35         self.ui = Ui_MyTestDialog()
36         # BE CAREFULL HERE, the ui form is NOT put in the global dialog (already
37         # containing some generic widgets) but in the center panel created in the
38         # GenericDialog as a void container for the form. The MyTestDialog form
39         # is supposed here to create only the widgets to be placed in the center
40         # panel
41         self.ui.setupUi(self.getPanel())
42
43     #
44     # We implement here the interface of the MVC pattern
45     #
46     def setData(self, name):
47         """
48         This function implements the mapping from the data model to the widgets
49         """
50         self.ui.txtName.setText(name)
51
52     def checkData(self):
53         """
54         This function implements the control to be done on the values contained
55         in the widgets when trying to validate the dialog window (click OK first
56         trigs this function).
57         """
58         if ( self.ui.txtName.text().trimmed() == "" ):
59             self.checkDataMessage = "The name can't be void"
60             return False
61         return True
62
63     def getData(self):
64         """
65         This function implements the mapping from the widgets to the data model
66         """
67         name = str(self.ui.txtName.text().trimmed().toUtf8())
68         # _MEM_: note here that (i) the utf8 format is used and (ii) we must not
69         # forget to convert to a standard python string (instead of a QString).
70         return name
71
72
73 class MyTestDialogWithSignals(MyTestDialog):
74     """
75     This class is to illustrate the usage of the GenericDialog in the
76     case where the dialog windows is not modal. In such a case, the
77     controller must be warn of close events using Qt signals.
78     """
79
80     inputValidated = pyqtSignal()
81
82     def __init__(self, parent=None, name="MyTestDialogWithSignals"):
83         MyTestDialog.__init__(self, parent, name)
84
85     def accept(self):
86         """
87         This function is the slot connected to the the OK button
88         (click event of the OK button). 
89         """
90         # The dialog is raised in a non modal mode (for example, to
91         # get interactivity with the parents windows. Then we have to
92         # emit a signal to warn the parent observer that the dialog
93         # has been validated so that it can process the event
94         MyTestDialog.accept(self)
95         if self.wasOk():
96             self.inputValidated.emit()
97
98
99
100 #
101 # ==============================================================================
102 # Basic use case
103 # ==============================================================================
104 #
105
106 def TEST_MyTestDialog_modal():
107     import sys
108     from qtsalome import QApplication
109     app = QApplication(sys.argv)
110     app.lastWindowClosed.connect(app.quit)
111
112     dlg=MyTestDialog()
113     dlg.setData("A default name")
114     dlg.displayAndWait()
115     if dlg.wasOk():
116         name = dlg.getData()
117         print("The name has been modified to",name)
118
119
120 class DialogListener:
121     def onProcessEvent(self):
122         print("onProcessEvent(): OK has been pressed")
123         import sys
124         sys.exit(0)
125         
126
127 def TEST_MyTestDialog_non_modal():
128     import sys
129     app = QApplication(sys.argv)
130     app.lastWindowClosed.connect(app.quit)
131
132     dlg=MyTestDialogWithSignals()
133     # This dialog window will emit a inputValidated() signal when the
134     # OK button is pressed and the data are validated. Then, we
135     # connect this signal to a local slot so that the event can be
136     # processed.
137     dlgListener = DialogListener()
138     dlg.inputValidated.connect(dlgListener.onProcessEvent)
139     # This connect instruction means that the signal inputValidated()
140     # emited by the dlg Qt object will raise a call to the slot
141     # dlgListener.onProcessEvent
142
143     dlg.setData("A default name")
144     dlg.show()
145
146     app.exec_()
147
148 if __name__ == "__main__":
149     #TEST_MyTestDialog_modal()
150     TEST_MyTestDialog_non_modal()