Back to index

salome-gui  6.5.0
tubedialog.py
Go to the documentation of this file.
00001 # Copyright (C) 2010-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 #
00003 # This library is free software; you can redistribute it and/or
00004 # modify it under the terms of the GNU Lesser General Public
00005 # License as published by the Free Software Foundation; either
00006 # version 2.1 of the License.
00007 #
00008 # This library is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 # Lesser General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU Lesser General Public
00014 # License along with this library; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 #
00017 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 #
00019 # Author : Guillaume Boulant (EDF)
00020 
00021 import sys
00022 from PyQt4 import QtGui
00023 from PyQt4 import QtCore
00024 
00025 from tubedialog_ui import TubeDialog_UI
00026 
00027 
00028 class TubeDialog(TubeDialog_UI):
00029     def setupUi(self):
00030         TubeDialog_UI.setupUi(self)
00031         self.handleAcceptWith(self.accept)
00032         self.handleRejectWith(self.reject)
00033 
00034     def handleAcceptWith(self,callbackFunction):
00035         """This defines the function to be connected to the signal 'accepted()' (click on Ok)"""
00036         QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), callbackFunction)
00037 
00038     def handleRejectWith(self,callbackFunction):
00039         """This defines the function to be connected to the signal 'rejected()' (click on Cancel)"""
00040         QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), callbackFunction)
00041 
00042     def handleApplyWith(self,callbackFunction):
00043         """This defines the function to be connected to the signal 'apply()' (click on Apply)"""
00044         button = self.buttonBox.button(QtGui.QDialogButtonBox.Apply)        
00045         QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"), callbackFunction);
00046 
00047     def accept(self):
00048         '''Callback function when dialog is accepted (click Ok)'''
00049         self._wasOk = True
00050         # We should test here the validity of values
00051         QtGui.QDialog.accept(self)
00052 
00053     def reject(self):
00054         '''Callback function when dialog is rejected (click Cancel)'''
00055         self._wasOk = False
00056         QtGui.QDialog.reject(self)
00057 
00058     def wasOk(self):
00059         return self._wasOk
00060 
00061     def setData(self, radius, length, width):
00062         self.txtRadius.setText(str(radius))
00063         self.txtLength.setText(str(length))
00064         self.txtWidth.setText(str(width))
00065 
00066     def getData(self):
00067         try:
00068             radius=eval(str(self.txtRadius.text()))
00069             length=eval(str(self.txtLength.text()))
00070             width=eval(str(self.txtWidth.text()))
00071         except:
00072             print "pb a la saisie"
00073 
00074         return radius, length, width
00075 
00076     
00077 class TubeDialogOnTopWithApply(TubeDialog):
00078     def setupUi(self):
00079         """
00080         This setupUi adds a button 'Apply' to execute a processing
00081         tasks (ex: preview), and set a flag that keeps the dialog on
00082         top of all windows.
00083         """
00084         TubeDialog.setupUi(self)
00085         # Add a button "Apply"
00086         self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|
00087                                           QtGui.QDialogButtonBox.Apply|
00088                                           QtGui.QDialogButtonBox.Ok)
00089 
00090         # Keep the dialog on top of the windows
00091         self.setWindowFlags(self.windowFlags() |
00092                             QtCore.Qt.WindowStaysOnTopHint)
00093 
00094 
00095 #
00096 # ======================================================================
00097 # Unit test
00098 # ======================================================================
00099 #
00100 def TEST_getData_synchrone():
00101     """This use case illustrates the MVC pattern on this simple dialog example""" 
00102     tubedialog = TubeDialog()
00103     tubedialog.setData(10,50,3)
00104     tubedialog.exec_()
00105     if tubedialog.wasOk():
00106         radius, length, width = tubedialog.getData()
00107         print radius, length, width
00108 
00109 
00110 def main( args ):
00111     a = QtGui.QApplication(sys.argv)
00112     TEST_getData_synchrone()
00113     sys.exit(0)
00114 
00115 if __name__=="__main__":
00116     main(sys.argv)
00117