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