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