From: boulant Date: Thu, 9 Feb 2012 18:33:52 +0000 (+0000) Subject: A new plugin example to illustrate: X-Git-Tag: V6_5_0a1~59 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=18b8e8d425c12040b152925323807d9ca361110f;p=modules%2Fgui.git A new plugin example to illustrate: - the use of non modal windows (with external callback functions connected to the click signals) - the create/destroy on a geom object --- diff --git a/src/SalomeApp/pluginsdemo/Makefile.am b/src/SalomeApp/pluginsdemo/Makefile.am index 1776630c0..d602c6a7e 100644 --- a/src/SalomeApp/pluginsdemo/Makefile.am +++ b/src/SalomeApp/pluginsdemo/Makefile.am @@ -32,7 +32,9 @@ include $(top_srcdir)/adm_local/unix/make_common_starter.am # pluginsdir =$(salomepluginsdir)/demo plugins_PYTHON = \ - salome_plugins.py \ - trihedron.py \ - tubedialog.py \ - tube.py + salome_plugins.py \ + trihedron.py \ + tubedialog.py \ + tubedialogWithApply.py \ + tube.py \ + myhelper.py diff --git a/src/SalomeApp/pluginsdemo/myhelper.py b/src/SalomeApp/pluginsdemo/myhelper.py new file mode 100644 index 000000000..70c94b96f --- /dev/null +++ b/src/SalomeApp/pluginsdemo/myhelper.py @@ -0,0 +1,41 @@ +# -*- coding: iso-8859-1 -*- + +import salome +from salome.geom import geomtools + +def addToStudy(study,shape,name): + ''' + Helper function to add an object in the study. It returns the + associated entry so that this study could be manipulated using + study object functions. + ''' + studyId = study._get_StudyId() + geompy = geomtools.getGeompy(studyId) + entry = geompy.addToStudy( shape, "Tube" ) + return entry + +# Available in SALOME 6.5 only +def displayShape_version65(shapeStudyEntry): + gst = geomtools.GeomStudyTools() + gst.displayShapeByEntry(shapeStudyEntry) + +def eraseShape_version65(shapeStudyEntry): + gst = geomtools.GeomStudyTools() + gst.eraseShapeByEntry(shapeStudyEntry) + +ModeShading = 1 +DisplayMode=ModeShading +def displayShape(shapeStudyEntry): + """This displays the shape specified by its entry in the study""" + geomgui = salome.ImportComponentGUI("GEOM") + geomgui.createAndDisplayFitAllGO(shapeStudyEntry) + geomgui.setDisplayMode(shapeStudyEntry, DisplayMode) + +def eraseShape(shapeStudyEntry): + """ + This erases from the viewers the shape specified by its entry + in the study. + """ + geomgui = salome.ImportComponentGUI("GEOM") + eraseFromAllWindows=True + geomgui.eraseGO(shapeStudyEntry,eraseFromAllWindows) diff --git a/src/SalomeApp/pluginsdemo/salome_plugins.py b/src/SalomeApp/pluginsdemo/salome_plugins.py index 2c3ea43a5..b9de4eb76 100755 --- a/src/SalomeApp/pluginsdemo/salome_plugins.py +++ b/src/SalomeApp/pluginsdemo/salome_plugins.py @@ -72,6 +72,7 @@ if DEMO_IS_ACTIVATED: # dedicated imported modules (tube.py and tubedialog.py). # import tube +import myhelper # A single dialog box is defined and recycled for every call. The # fields are initialized with default values given by the tube factory @@ -81,7 +82,7 @@ dialog = tubedialog.TubeDialog() dialog.setData(tube.DEFAULT_RADIUS, tube.DEFAULT_LENGTH, tube.DEFAULT_WIDTH) def tube_shapewithgui(context): - global tube, dialog + global tube, myhelper, dialog activeStudy = context.study # Get the parameter values from a gui dialog box. If the dialog is @@ -91,6 +92,8 @@ def tube_shapewithgui(context): if dialog.wasOk(): radius, length, width = dialog.getData() shape = tube.createGeometry(activeStudy, radius, length, width) + entry = myhelper.addToStudy(activeStudy, shape, "Tube" ) + myhelper.displayShape(entry) if DEMO_IS_ACTIVATED: salome_pluginsmanager.AddFunction('Tube shape from parameters', @@ -120,6 +123,91 @@ if DEMO_IS_ACTIVATED: 'Creates a tube object from specified parameters', tube_meshwithgui) + +# ------------------------------------------------------------------------- +# Example 2 ter: creation of a geom object with a preview function in +# the dialog box. This use case is more complex from the gui point of +# view because the dialog box is a not modal so that we can have +# interaction with the complete SALOME application. This modal +# situation requires to connect button click signal on global +# functions named the "callback" functions. +# +import tubedialogWithApply +dialogWithApply = tubedialogWithApply.TubeDialogWithApply() +dialogWithApply.setData(tube.DEFAULT_RADIUS, tube.DEFAULT_LENGTH, tube.DEFAULT_WIDTH) +activeStudy = None +previewShapeEntry = None + +def acceptCallback(): + """Action to do when click on Ok""" + global tube, dialogWithApply, activeStudy, previewShapeEntry, deletePreviewShape + dialogWithApply.accept() + + if previewShapeEntry is not None: + deletePreviewShape() + + radius, length, width = dialogWithApply.getData() + shape = tube.createGeometry(activeStudy, radius, length, width) + entry = myhelper.addToStudy(activeStudy, shape, "Tube" ) + myhelper.displayShape(entry) + + +def rejectCallback(): + """Action to do when click on Cancel""" + global tube, dialogWithApply, activeStudy, previewShapeEntry, deletePreviewShape + dialogWithApply.reject() + + if previewShapeEntry is not None: + deletePreviewShape() + +def applyCallback(): + """Action to do when click on Apply""" + global tube, dialogWithApply, activeStudy, previewShapeEntry, deletePreviewShape + # We first have to destroy the currently displayed preview shape. + if previewShapeEntry is not None: + deletePreviewShape() + + # Then we can create the new shape with the new parameter values + radius, length, width = dialogWithApply.getData() + shape = tube.createGeometry(activeStudy, radius, length, width) + previewShapeEntry = myhelper.addToStudy(activeStudy, shape, "Tube" ) + myhelper.displayShape(previewShapeEntry) + +def deletePreviewShape(): + global activeStudy, previewShapeEntry + + from salome.kernel.studyedit import getStudyEditor, getStudyIdFromStudy + from salome.kernel.services import IDToSObject, IDToObject + + # WARN: please be aware that to delete a geom object, you have + # three operations to perform: + # 1. erase the shape from the viewer + # 2. delete the entry in the study + # 3. destroy the geom object + myhelper.eraseShape(previewShapeEntry) + item = IDToSObject(previewShapeEntry) + geomObj = IDToObject(previewShapeEntry) + ste = getStudyEditor(getStudyIdFromStudy(activeStudy)) + ste.removeItem(item,True) + geomObj.Destroy() + previewShapeEntry = None + +dialogWithApply.handleAcceptWith(acceptCallback) +dialogWithApply.handleRejectWith(rejectCallback) +dialogWithApply.handleApplyWith(applyCallback) + +def tube_shapewithguiAndPreview(context): + global tube, dialogWithApply, activeStudy + activeStudy = context.study + dialogWithApply.open() + +if DEMO_IS_ACTIVATED: + salome_pluginsmanager.AddFunction('Tube geometry from parameters with preview', + 'Creates a tube object from specified parameters', + tube_shapewithguiAndPreview) + + + # ------------------------------------------------------------------------- # Example 3: run a shell session in a xterm to get a SALOME python console def runSalomeShellSession(context): diff --git a/src/SalomeApp/pluginsdemo/tube.py b/src/SalomeApp/pluginsdemo/tube.py index 5245b500a..75cad2151 100644 --- a/src/SalomeApp/pluginsdemo/tube.py +++ b/src/SalomeApp/pluginsdemo/tube.py @@ -6,14 +6,16 @@ DEFAULT_RADIUS = 100 DEFAULT_LENGTH = 300 DEFAULT_WIDTH = 20 +from salome.geom import geomtools + def createGeometry(study, radius=DEFAULT_RADIUS, length=DEFAULT_LENGTH, width=DEFAULT_WIDTH): ''' This function creates the geometry on the specified study and with given parameters. ''' print "TUBE: creating the geometry ..." - import geompy - geompy.init_geom(study) + studyId = study._get_StudyId() + geompy = geomtools.getGeompy(studyId) radius_ext = radius radius_int = radius_ext - width @@ -21,19 +23,20 @@ def createGeometry(study, radius=DEFAULT_RADIUS, length=DEFAULT_LENGTH, width=DE CylinderExt = geompy.MakeCylinderRH(radius_ext, length) CylinderInt = geompy.MakeCylinderRH(radius_int, length) Tube = geompy.MakeCut(CylinderExt, CylinderInt) - entry = geompy.addToStudy( Tube, "Tube" ) return Tube - + def createGeometryWithPartition(study, radius=DEFAULT_RADIUS, length=DEFAULT_LENGTH, width=DEFAULT_WIDTH): ''' This function create the geometrical shape with a partition so that the hexaedric algorithm could be used for meshing. ''' - import geompy shape = createGeometry(study,radius,length,width) # We have to create a partition so that we can use an hexaedric # meshing algorithm. + studyId = study._get_StudyId() + geompy = geomtools.getGeompy(studyId) + print "TUBE: creating a partition ..." toolPlane = geompy.MakeFaceHW(2.1*length,2.1*radius,3) partition = geompy.MakePartition([shape], [toolPlane], [], [], geompy.ShapeType["SOLID"], 0, [], 0) diff --git a/src/SalomeApp/pluginsdemo/tubedialogWithApply.py b/src/SalomeApp/pluginsdemo/tubedialogWithApply.py new file mode 100644 index 000000000..d74549696 --- /dev/null +++ b/src/SalomeApp/pluginsdemo/tubedialogWithApply.py @@ -0,0 +1,30 @@ +import sys +from PyQt4 import QtGui +from PyQt4 import QtCore + + +from tubedialog import TubeDialog +class TubeDialogWithApply(TubeDialog): + def setupUi(self): + """ + This setupUi adds a button Apply to execute tasks as preview + """ + TubeDialog.setupUi(self) + self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel| + QtGui.QDialogButtonBox.Apply| + QtGui.QDialogButtonBox.Ok) + + self.setWindowFlags(self.windowFlags() | + QtCore.Qt.WindowStaysOnTopHint) + + + def handleAcceptWith(self,callbackFunction): + QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), callbackFunction) + + def handleRejectWith(self,callbackFunction): + QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), callbackFunction) + + def handleApplyWith(self,callbackFunction): + button = self.buttonBox.button(QtGui.QDialogButtonBox.Apply) + QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"), callbackFunction); +