Salome HOME
Merge from V6_main 01/04/2013
[tools/yacsgen.git] / Examples / pygui1 / pycomposGUI.py
1 # Copyright (C) 2009-2013  EDF R&D
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.
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
20 import os
21
22 from PyQt4.QtCore import *
23 from PyQt4.QtGui import *
24 from PyQt4.QtWebKit import *
25 from PyQt4 import QtCore, QtGui, uic
26
27 import salome
28 import pycompos_ORB
29
30 # Get SALOME PyQt interface
31 import SalomePyQt
32 sgPyQt = SalomePyQt.SalomePyQt()
33
34 # Get SALOME Swig interface
35 import libSALOME_Swig
36 sg = libSALOME_Swig.SALOMEGUI_Swig()
37
38 # object counter
39 __objectid__ = 0
40
41 ###
42 # get active study ID
43 ###
44 def _getStudyId():
45     return sgPyQt.getStudyId()
46
47 ###
48 # get active study
49 ###
50 def _getStudy():
51     studyId = _getStudyId()
52     study = salome.myStudyManager.GetStudyByID( studyId )
53     return study
54
55 # called when module is initialized
56 # return map of popup windows to be used by the module
57 def windows():
58   wm = {}
59   wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea
60   wm[SalomePyQt.WT_PyConsole]     = Qt.BottomDockWidgetArea
61   return wm
62
63 # called when module is initialized
64 # return list of 2d/3d views to be used ny the module
65 def views():
66   return []
67
68 # called when module is activated
69 # returns True if activating is successfull and False otherwise
70 def activate():
71   # create top-level menu
72   mid = sgPyQt.createMenu( "pycompos", -1, 90, sgPyQt.defaultMenuGroup() )
73   # create toolbar
74   tid = sgPyQt.createTool( "pycompos" )
75   # create actions and fill menu and toolbar with actions
76   a = sgPyQt.createAction( 941, "Hello", "Hello", "Show hello dialog box" ,"exec.png")
77   sgPyQt.createMenu( a, mid )
78   sgPyQt.createTool( a, tid )
79   a = sgPyQt.createAction( 942, "Hello2", "Hello2", "Show hello2 dialog box" ,"exec.png")
80   sgPyQt.createMenu( a, mid )
81   sgPyQt.createTool( a, tid )
82   a = sgPyQt.createAction( 943, "Create object", "Create object", "Create object","exec.png" )
83   sgPyQt.createMenu( a, mid )
84   sgPyQt.createTool( a, tid )
85
86   return True
87
88 # called when module is deactivated
89 def deactivate():
90   pass
91
92 _engine=None
93 def getEngine():
94   global _engine
95   if not _engine:
96     _engine= salome.lcc.FindOrLoadComponent( "FactoryServerPy", "pycompos" )
97   return _engine
98
99 ###
100 # Create new object
101 ###
102 def CreateObject():
103     global __objectid__
104     default_name = str( sgPyQt.stringSetting( "pycompos", "def_obj_name", "Object" ).trimmed() )
105     # generate object name
106     __objectid__  = __objectid__ + 1
107     name = "%s_%d" % ( default_name, __objectid__ )
108     if not name: return
109     getEngine().createObject( _getStudy(), name )
110     print getEngine().s1(4,5)
111     print getEngine().ComponentDataType()
112     sg.updateObjBrowser( True )
113
114 class DemoImpl(QtGui.QDialog):
115     def __init__(self, *args):
116         super(DemoImpl, self).__init__(*args)
117
118         uic.loadUi(os.path.join(os.environ["pycompos_ROOT_DIR"],"share","salome","resources","pycompos","demo.ui"), self)
119
120     @QtCore.pyqtSlot()
121     def on_button1_clicked(self):
122         for s in "This is a demo".split(" "):
123             self.list.addItem(s)
124
125 # called when GUI action is activated
126 # action ID is passed as parameter
127 def OnGUIEvent( commandID ):
128   print "pycompos.OnGUIEvent(): command = %d" % commandID
129   if commandID==941:
130     widget=QMainWindow(sgPyQt.getDesktop())
131     web = QWebView(widget)
132     page=os.path.join(os.environ["pycompos_ROOT_DIR"],"share","doc","salome","gui","pycompos","index.html")
133     web.load(QUrl(page))
134     widget.setCentralWidget(web)
135     widget.show()
136
137   elif commandID==942:
138     widget = DemoImpl(sgPyQt.getDesktop())
139     widget.show()
140
141   elif commandID==943:
142     CreateObject()
143
144