]> SALOME platform Git repositories - modules/yacs.git/blob - src/pyqt/gui/Appli.py
Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / pyqt / gui / Appli.py
1 """
2 """
3 import sys,os
4 from qt import *
5 import Tree
6 import PanelManager
7 import Icons
8 import Items
9 import adapt
10 import Item
11 import pilot
12 import threading
13 import time
14
15 class Browser(QVBox):
16   def __init__(self,parent,proc):
17     QVBox.__init__(self,parent)
18     self.hSplitter = QSplitter(self,"hSplitter")
19     self.objectBrowser=Tree.Tree(self.hSplitter,self.onSelect)
20     pp=adapt.adapt(proc,Item.Item)
21     self.proc=proc
22     self.pproc=pp
23     self.objectBrowser.additem(pp)
24     self.panelManager=PanelManager.PanelManager(self.hSplitter)
25     self.executor=None
26     self.resume=0
27     self.thr=None
28
29   def onSelect(self,item):
30     #item is instance of Item.Item
31     self.selected=item
32     self.panelManager.setview(item)
33
34   def run(self):
35     if not self.executor:
36       self.executor = pilot.ExecutorSwig()
37     if self.thr and self.thr.isAlive():
38       return
39     #step by step execution mode
40     self.executor.setExecMode(1)
41     #execute it in a thread
42     self.thr = threading.Thread(target=self.executor.RunW, args=(self.proc,0))
43     #as a daemon (no need to join)
44     self.thr.setDaemon(1)
45     #start the thread
46     self.thr.start()
47     self.resume=1
48     #wait pause
49     time.sleep(0.1)
50     self.executor.waitPause()
51     #switch to continue execution mode
52     self.executor.setExecMode(0)
53     #resume it
54     self.executor.resumeCurrentBreakPoint()
55     self.resume=0
56
57   def susp(self):
58     """Suspend or resume an executing schema"""
59     if not self.executor:
60       return
61     if not self.thr.isAlive():
62       return
63
64     if self.resume:
65       #continue execution mode
66       self.executor.setExecMode(0)
67       #if finished stop it
68       #resume it
69       self.executor.resumeCurrentBreakPoint()
70       self.resume=0
71     else:
72       #step by step execution mode
73       self.executor.setExecMode(1)
74       #self.executor.waitPause()
75       self.resume=1
76
77   def step(self):
78     """Step on a paused schema"""
79     if not self.executor:
80       self.executor = pilot.ExecutorSwig()
81     if not self.thr or not self.thr.isAlive():
82       #start in step by step mode
83       self.executor.setExecMode(1)
84       self.thr = threading.Thread(target=self.executor.RunW, args=(self.proc,0))
85       self.thr.setDaemon(1)
86       self.thr.start()
87       self.resume=1
88       return
89
90     #step by step execution mode
91     self.resume=1
92     self.executor.setExecMode(1)
93     #if finished stop it
94     #resume it
95     self.executor.resumeCurrentBreakPoint()
96
97   def stop(self):
98     """Stop the schema"""
99     if not self.executor:
100       return
101     if not self.thr.isAlive():
102       return
103     self.executor.setExecMode(1)
104     self.executor.waitPause()
105     self.executor.resumeCurrentBreakPoint()
106     #self.executor.stopExecution()
107
108 class Appli(QMainWindow):
109   """
110       Appli()
111       Cree la fenetre principale de l'interface utilisateur
112   """
113   def __init__(self):
114     QMainWindow.__init__(self)
115     self.createWidgets()
116     self.initActions()
117     self.initMenus()
118     self.initToolbar()
119     self.initStatusbar()
120     self.initYACS()
121
122   def createWidgets(self):
123     self.tabWidget = QTabWidget(self)
124     self.currentPanel=None
125     self.connect(self.tabWidget, SIGNAL('currentChanged(QWidget *)'),self.handlePanelChanged)
126     self.setCentralWidget(self.tabWidget)
127     self.resize(800,600)
128
129   def handlePanelChanged(self,panel):
130     self.currentPanel=panel
131
132   def initActions(self):
133     self.actions = []
134
135     self.newAct=QAction('New', QIconSet(Icons.get_image("new")), '&New',
136                           QKeySequence("CTRL+N"),self)
137     self.newAct.setStatusTip('Open an empty editor window')
138     self.newAct.setWhatsThis( """<b>New</b>"""
139             """<p>An empty editor window will be created.</p>""")
140     self.newAct.connect(self.newAct,SIGNAL('activated()'), self.handleFile)
141     self.actions.append(self.newAct)
142
143     self.prefAct=QAction('Preferences',QIconSet(Icons.get_image("configure.png")),'&Preferences...',
144                            0, self)
145     self.prefAct.setStatusTip('Set the prefered configuration')
146     self.prefAct.setWhatsThis("""<b>Preferences</b>"""
147                               """<p>Set the configuration items of the application"""
148                               """ with your prefered values.</p>""")
149     self.prefAct.connect(self.prefAct,SIGNAL('activated()'), self.handlePreferences)
150     self.actions.append(self.prefAct)
151
152     self.runAct=QAction('Run',QIconSet(Icons.get_image("run.png")),'&Run',0,self)
153     self.runAct.connect(self.runAct,SIGNAL('activated()'), self.run)
154     self.runAct.setStatusTip('Run the selected schema')
155     self.actions.append(self.runAct)
156
157     self.suspAct=QAction('Suspend/resume',QIconSet(Icons.get_image("suspend-resume.gif")),'&Suspend/resume',0,self)
158     self.suspAct.connect(self.suspAct,SIGNAL('activated()'), self.susp)
159     self.suspAct.setStatusTip('Suspend/resume the selected schema')
160     self.actions.append(self.suspAct)
161
162     self.stepAct=QAction('Step',QIconSet(Icons.get_image("steps.png")),'&Step',0,self)
163     self.stepAct.connect(self.stepAct,SIGNAL('activated()'), self.step)
164     self.stepAct.setStatusTip('Step the selected schema')
165     self.actions.append(self.stepAct)
166
167     self.stopAct=QAction('Stop',QIconSet(Icons.get_image("kill.png")),'&Stop',0,self)
168     self.stopAct.connect(self.stopAct,SIGNAL('activated()'), self.stop)
169     self.stopAct.setStatusTip('Stop the selected schema')
170     self.actions.append(self.stopAct)
171
172   def initMenus(self):
173     menubar = self.menuBar()
174
175     #menu file
176     self.fileMenu=QPopupMenu(self)
177     self.newAct.addTo(self.fileMenu)
178     self.fileMenu.insertItem("&Open", self.openFile)
179     self.fileMenu.insertItem("&Open Salome", self.openSalomeFile)
180     menubar.insertItem('&File',self.fileMenu)
181
182     #menu settings
183     self.settingsMenu = QPopupMenu(self)
184     menubar.insertItem('&Settings', self.settingsMenu)
185     self.settingsMenu.insertTearOffHandle()
186     self.prefAct.addTo(self.settingsMenu)
187
188     #menu Edit
189     self.editMenu = QPopupMenu(self)
190     self.editMenu.insertItem("&Add node", self.addNode)
191     menubar.insertItem('&Edit', self.editMenu)
192
193     #menu Canvas
194     #sous menu layout
195     self.layoutMenu = QPopupMenu(self)
196     self.layoutMenu.insertItem("&Left Right", self.LR)
197     self.canvasMenu = QPopupMenu(self)
198     self.canvasMenu.insertItem("&Zoom in", self.zoomIn)
199     self.canvasMenu.insertItem("Zoom &out", self.zoomOut)
200     self.canvasMenu.insertItem("Layout", self.layoutMenu)
201     self.canvasMenu.insertItem("&Update", self.updateCanvas)
202     menubar.insertItem('&Canvas', self.canvasMenu)
203
204     #menu window
205     self.windowMenu = QPopupMenu(self)
206     menubar.insertItem('&Window', self.windowMenu)
207     self.connect(self.windowMenu, SIGNAL('aboutToShow()'), self.handleWindowMenu)
208
209     #menu help
210     self.help=QPopupMenu(self)
211     menubar.insertItem('&Help',self.help)
212     self.help.insertItem('&About',self.about,Qt.Key_F1)
213     self.help.insertItem('About &Qt',self.aboutQt)
214
215   def initYACS(self):
216     import pilot
217     import loader
218     import salomeloader
219     self.runtime= pilot.getRuntime()
220     self.loader = loader.YACSLoader()
221     self.executor = pilot.ExecutorSwig()
222     self.salomeloader=salomeloader.SalomeLoader()
223
224   def openSalomeFile(self):
225     fn = QFileDialog.getOpenFileName(QString.null,QString.null,self)
226     if fn.isEmpty():
227       self.statusBar().message('Loading aborted',2000)
228       return
229     fileName = str(fn)
230     proc=self.salomeloader.load(fileName)
231
232     panel=Browser(self.tabWidget,proc)
233     self.currentPanel=panel
234     self.tabWidget.addTab( panel,os.path.basename(fileName))
235     self.tabWidget.showPage(panel)
236
237   def openFile(self):
238     fn = QFileDialog.getOpenFileName(QString.null,QString.null,self)
239     if fn.isEmpty():
240       self.statusBar().message('Loading aborted',2000)
241       return
242     fileName = str(fn)
243     proc=self.loader.load(fileName)
244
245     panel=Browser(self.tabWidget,proc)
246     self.currentPanel=panel
247     self.tabWidget.addTab( panel,os.path.basename(fileName))
248     self.tabWidget.showPage(panel)
249
250   def LR(self):
251     if self.currentPanel.selected and isinstance(self.currentPanel.selected,Items.ItemComposedNode):
252       self.currentPanel.selected.layout("LR")
253
254   def updateCanvas(self):
255     if self.currentPanel.selected:#item selected
256       if isinstance(self.currentPanel.selected,Items.ItemComposedNode):
257         #on peut updater
258         self.currentPanel.selected.editor.updateCanvas()
259
260   def addNode(self):
261     if self.currentPanel.selected:#item selected
262       if isinstance(self.currentPanel.selected,Items.ItemComposedNode):
263         #on peut ajouter un noeud
264         self.currentPanel.selected.addNode()
265
266   def zoomIn(self):
267     if self.currentPanel.selected:#item selected
268       if isinstance(self.currentPanel.selected,Items.ItemComposedNode):
269         #on peut zoomer
270         self.currentPanel.selected.editor.zoomIn()
271
272   def zoomOut(self):
273     if self.currentPanel.selected:#item selected
274       if isinstance(self.currentPanel.selected,Items.ItemComposedNode):
275         #on peut dezoomer
276         self.currentPanel.selected.editor.zoomOut()
277
278   def handleFile(self):
279     pass
280
281   def handlePreferences(self):
282     pass
283
284   def handleWindowMenu(self):
285     pass
286
287   def about(self):
288     QMessageBox.about(self,'YACS browser GUI', 'YACS browser GUI')
289
290   def aboutQt(self):
291     QMessageBox.aboutQt(self,'YACS browser GUI')
292
293   def run(self):
294     if self.currentPanel:
295       self.currentPanel.run()
296
297   def susp(self):
298     if self.currentPanel:
299       self.currentPanel.susp()
300
301   def step(self):
302     if self.currentPanel:
303       self.currentPanel.step()
304
305   def stop(self):
306     if self.currentPanel:
307       self.currentPanel.stop()
308
309   def initToolbar(self):
310     tb = QToolBar(self)
311     self.newAct.addTo(tb)
312     self.runAct.addTo(tb)
313     self.suspAct.addTo(tb)
314     self.stepAct.addTo(tb)
315     self.stopAct.addTo(tb)
316     self.toolbars={}
317     self.toolbars['File']=tb
318
319   def initStatusbar(self):
320     sb = self.statusBar()
321     self.SBfile=QLabel(sb)
322     sb.addWidget(self.SBfile)
323     QWhatsThis.add(self.SBfile,
324                    """<p>Partie de la statusbar qui donne le nom"""
325                    """du fichier courant. </p>""")
326     self.SBfile.setText("")
327
328
329 if __name__ == "__main__":
330   from Item import Item
331   app = QApplication(sys.argv)
332   t=Appli()
333   t.objectBrowser.additem(Item("item1"))
334   n=t.objectBrowser.additem(Item("item2"))
335   n.additem(Item("item3"))
336   app.setMainWidget(t)
337   t.show()
338   app.exec_loop()
339
340