Salome HOME
ddd3fcff7564c94e174e1f95f9bb5a098a49b371
[modules/gui.git] / src / SalomeApp / pluginsdemo / tubedialog.py
1 # Copyright (C) 2010-2014  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 PyQt4 import QtGui
23 from PyQt4 import QtCore
24
25 from tubedialog_ui import TubeDialog_UI
26
27
28 class TubeDialog(TubeDialog_UI):
29     def setupUi(self):
30         TubeDialog_UI.setupUi(self)
31         self.handleAcceptWith(self.accept)
32         self.handleRejectWith(self.reject)
33
34     def handleAcceptWith(self,callbackFunction):
35         """This defines the function to be connected to the signal 'accepted()' (click on Ok)"""
36         QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), callbackFunction)
37
38     def handleRejectWith(self,callbackFunction):
39         """This defines the function to be connected to the signal 'rejected()' (click on Cancel)"""
40         QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), callbackFunction)
41
42     def handleApplyWith(self,callbackFunction):
43         """This defines the function to be connected to the signal 'apply()' (click on Apply)"""
44         button = self.buttonBox.button(QtGui.QDialogButtonBox.Apply)        
45         QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"), callbackFunction);
46
47     def accept(self):
48         '''Callback function when dialog is accepted (click Ok)'''
49         self._wasOk = True
50         # We should test here the validity of values
51         QtGui.QDialog.accept(self)
52
53     def reject(self):
54         '''Callback function when dialog is rejected (click Cancel)'''
55         self._wasOk = False
56         QtGui.QDialog.reject(self)
57
58     def wasOk(self):
59         return self._wasOk
60
61     def setData(self, radius, length, width):
62         self.txtRadius.setText(str(radius))
63         self.txtLength.setText(str(length))
64         self.txtWidth.setText(str(width))
65
66     def getData(self):
67         try:
68             radius=eval(str(self.txtRadius.text()))
69             length=eval(str(self.txtLength.text()))
70             width=eval(str(self.txtWidth.text()))
71         except:
72             print "pb a la saisie"
73
74         return radius, length, width
75
76     
77 class TubeDialogOnTopWithApply(TubeDialog):
78     def setupUi(self):
79         """
80         This setupUi adds a button 'Apply' to execute a processing
81         tasks (ex: preview), and set a flag that keeps the dialog on
82         top of all windows.
83         """
84         TubeDialog.setupUi(self)
85         # Add a button "Apply"
86         self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|
87                                           QtGui.QDialogButtonBox.Apply|
88                                           QtGui.QDialogButtonBox.Ok)
89
90         # Keep the dialog on top of the windows
91         self.setWindowFlags(self.windowFlags() |
92                             QtCore.Qt.WindowStaysOnTopHint)
93
94
95 #
96 # ======================================================================
97 # Unit test
98 # ======================================================================
99 #
100 def TEST_getData_synchrone():
101     """This use case illustrates the MVC pattern on this simple dialog example""" 
102     tubedialog = TubeDialog()
103     tubedialog.setData(10,50,3)
104     tubedialog.exec_()
105     if tubedialog.wasOk():
106         radius, length, width = tubedialog.getData()
107         print radius, length, width
108
109
110 def main( args ):
111     a = QtGui.QApplication(sys.argv)
112     TEST_getData_synchrone()
113     sys.exit(0)
114
115 if __name__=="__main__":
116     main(sys.argv)
117