Salome HOME
15daa9ec46416296f59ce7f33e5f71163fd6be8c
[modules/gui.git] / src / SalomeApp / pluginsdemo / tubedialog.py
1 # Copyright (C) 2010-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 # Author : Guillaume Boulant (EDF)
20
21 import sys
22 from qtsalome import *
23
24 from tubedialog_ui import TubeDialog_UI
25
26
27 class TubeDialog(TubeDialog_UI):
28     def setupUi(self):
29         TubeDialog_UI.setupUi(self)
30         self.handleAcceptWith(self.accept)
31         self.handleRejectWith(self.reject)
32
33     def handleAcceptWith(self,callbackFunction):
34         """This defines the function to be connected to the signal 'accepted()' (click on Ok)"""
35         self.buttonBox.accepted.connect(callbackFunction)
36
37     def handleRejectWith(self,callbackFunction):
38         """This defines the function to be connected to the signal 'rejected()' (click on Cancel)"""
39         self.buttonBox.rejected.connect(callbackFunction)
40
41     def handleApplyWith(self,callbackFunction):
42         """This defines the function to be connected to the signal 'apply()' (click on Apply)"""
43         button = self.buttonBox.button(QDialogButtonBox.Apply)
44         button.clicked.connect(callbackFunction);
45
46     def accept(self):
47         '''Callback function when dialog is accepted (click Ok)'''
48         self._wasOk = True
49         # We should test here the validity of values
50         QDialog.accept(self)
51
52     def reject(self):
53         '''Callback function when dialog is rejected (click Cancel)'''
54         self._wasOk = False
55         QDialog.reject(self)
56
57     def wasOk(self):
58         return self._wasOk
59
60     def setData(self, radius, length, width):
61         self.txtRadius.setText(str(radius))
62         self.txtLength.setText(str(length))
63         self.txtWidth.setText(str(width))
64
65     def getData(self):
66         try:
67             radius=eval(str(self.txtRadius.text()))
68             length=eval(str(self.txtLength.text()))
69             width=eval(str(self.txtWidth.text()))
70         except:
71             print("pb a la saisie")
72
73         return radius, length, width
74
75     
76 class TubeDialogOnTopWithApply(TubeDialog):
77     def setupUi(self):
78         """
79         This setupUi adds a button 'Apply' to execute a processing
80         tasks (ex: preview), and set a flag that keeps the dialog on
81         top of all windows.
82         """
83         TubeDialog.setupUi(self)
84         # Add a button "Apply"
85         self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel|
86                                           QDialogButtonBox.Apply|
87                                           QDialogButtonBox.Ok)
88
89         # Keep the dialog on top of the windows
90         self.setWindowFlags(self.windowFlags() |
91                             Qt.WindowStaysOnTopHint)
92
93
94 #
95 # ======================================================================
96 # Unit test
97 # ======================================================================
98 #
99 def TEST_getData_synchrone():
100     """This use case illustrates the MVC pattern on this simple dialog example""" 
101     tubedialog = TubeDialog()
102     tubedialog.setData(10,50,3)
103     tubedialog.exec_()
104     if tubedialog.wasOk():
105         radius, length, width = tubedialog.getData()
106         print(radius, length, width)
107
108
109 def main( args ):
110     a = QApplication(sys.argv)
111     TEST_getData_synchrone()
112     sys.exit(0)
113
114 if __name__=="__main__":
115     main(sys.argv)