-
Notifications
You must be signed in to change notification settings - Fork 19
/
DetachedFrame.py
334 lines (275 loc) · 13.8 KB
/
DetachedFrame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- coding: utf-8 -*-
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# DetachedFrame.py ---
# --------------------------------
# Copyright (c) 2018
# Laurent CAPOCCHI
# University of Corsica
# --------------------------------
# Version 1.0 last modified: 03/07/18
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GENERAL NOTES AND REMARKS:
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GLOBAL VARIABLES AND FUNCTIONS
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
import sys
import os
import wx
import builtins
import gettext
_ = gettext.gettext
if wx.VERSION_STRING >= '4.0':
wx.ST_SIZEGRIP = wx.STB_SIZEGRIP
if __name__ == '__main__':
builtins.__dict__['GUI_FLAG'] = True
builtins.__dict__['HOME_PATH'] = os.path.abspath(os.path.dirname(sys.argv[0]))
builtins.__dict__['DEFAULT_DEVS_DIRNAME'] = "PyDEVS"
builtins.__dict__['DEVS_DIR_PATH_DICT'] = {\
'PyDEVS':os.path.join(os.pardir,'DEVSKernel','PyDEVS'),\
'PyPDEVS':os.path.join(os.pardir,'DEVSKernel','PyPDEVS', 'old')}
import Container
import Menu
import PrintOut
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# CLASS DEFIINTION
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
class DetachedFrame(wx.Frame, PrintOut.Printable):
""" Detached Frame including a diagram.
"""
def __init__(self, parent=None, ID=wx.NewIdRef(), title="", diagram=None, name=""):
""" Constructor.
@parent : window parent of the frame
@ID : ID of the frame
@title : title of the frame
@diagram : diagram included in the canvas embedded in the frame
@name : name of the frame
"""
### inherit call
wx.Frame.__init__( self,
parent,
ID,
title,
wx.DefaultPosition,
wx.Size(600, 450),
name=name,
style=wx.DEFAULT_FRAME_STYLE | wx.CLIP_CHILDREN)
self.default_style = self.GetWindowStyle()
### local Copy
self.title = title
self.parent = parent
self.diagram = diagram
### current abstract level
#=======================================================================
if hasattr(diagram, 'layers') and hasattr(diagram, 'current_level'):
level = diagram.layers[0].current_level
self.diagram = diagram.layers[level]
else:
level = 0
self.diagram = diagram
#=======================================================================
### Canvas Stuff -----------------------------------
self.canvas = Container.ShapeCanvas(self, wx.NewIdRef(), name=title, diagram = self.diagram)
self.canvas.scalex = 1.0
self.canvas.scaley = 1.0
self.transparent = wx.ALPHA_OPAQUE
### ------------------------Diagram parent manager
try:
self.canvas.stockUndo = self.diagram.parent.stockUndo
self.canvas.stockRedo = self.diagram.parent.stockRedo
except Exception:
diagram.SetParent(self.canvas)
self.canvas.stockUndo = []
self.canvas.stockRedo = []
### Menu ToolBar
toolbar = wx.ToolBar(self, wx.NewIdRef(), name='tb', style=wx.TB_HORIZONTAL | wx.NO_BORDER)
toolbar.SetToolBitmapSize((16,16))
if self.parent:
self.toggle_list = wx.GetApp().GetTopWindow().toggle_list
else:
sys.stdout.write(_('Alone mode for DetachedFrame: Connector buttons are not binded\n'))
self.toggle_list = [wx.NewIdRef() for i in range(6)]
if wx.VERSION_STRING < '2.9':
self.tools = [ toolbar.AddTool(Menu.ID_SAVE, wx.Bitmap(os.path.join(ICON_PATH,'save.png')), shortHelpString=_('Save File') ,longHelpString=_('Save the current diagram'), clientData=self.canvas),
toolbar.AddTool(Menu.ID_SAVEAS, wx.Bitmap(os.path.join(ICON_PATH,'save_as.png')), shortHelpString=_('Save File As'), longHelpString=_('Save the diagram with an another name'), clientData=self.canvas),
toolbar.AddTool(wx.ID_UNDO, wx.Bitmap(os.path.join(ICON_PATH,'undo.png')), shortHelpString=_('Undo'), longHelpString=_('Click to go back, hold to see history'),clientData=self.canvas),
toolbar.AddTool(wx.ID_REDO, wx.Bitmap(os.path.join(ICON_PATH,'redo.png')), shortHelpString=_('Redo'), longHelpString=_('Click to go forward, hold to see history'),clientData=self.canvas),
toolbar.AddTool(Menu.ID_ZOOMIN_DIAGRAM, wx.Bitmap(os.path.join(ICON_PATH,'zoom+.png')), shortHelpString=_('Zoom +'), longHelpString=_('Zoom in'),clientData=self.canvas),
toolbar.AddTool(Menu.ID_ZOOMOUT_DIAGRAM, wx.Bitmap(os.path.join(ICON_PATH,'zoom-.png')), shortHelpString=_('Zoom -'), longHelpString=_('Zoom out'),clientData=self.canvas),
toolbar.AddTool(Menu.ID_UNZOOM_DIAGRAM, wx.Bitmap(os.path.join(ICON_PATH,'no_zoom.png')), shortHelpString=_('AnnuleZoom'),longHelpString=_('Initial view'),clientData=self.canvas),
toolbar.AddTool(Menu.ID_PRIORITY_DIAGRAM, wx.Bitmap(os.path.join(ICON_PATH,'priority.png')), shortHelpString=_('Priority'), longHelpString=_('Activation models priority')),
toolbar.AddTool(Menu.ID_CHECK_DIAGRAM, wx.Bitmap(os.path.join(ICON_PATH,'check_master.png')), shortHelpString=_('Check'), longHelpString=_('Check all models')),
toolbar.AddTool(Menu.ID_SIM_DIAGRAM, wx.Bitmap(os.path.join(ICON_PATH,'simulation.png')), shortHelpString=_('Simulation'), longHelpString=_('Simulate the diagram')),
toolbar.AddTool(self.toggle_list[0], wx.Bitmap(os.path.join(ICON_PATH,'direct_connector.png')), shortHelpString=_('Direct'), longHelpString=_('Direct connector'), isToggle=True),
toolbar.AddTool(self.toggle_list[1], wx.Bitmap(os.path.join(ICON_PATH,'square_connector.png')), shortHelpString=_('Square'), longHelpString=_('Square connector'), isToggle=True),
toolbar.AddTool(self.toggle_list[2], wx.Bitmap(os.path.join(ICON_PATH,'linear_connector.png')), shortHelpString=_('Linear'), longHelpString=_('Linear connector'), isToggle=True)
]
else:
self.tools = [ toolbar.AddTool(Menu.ID_SAVE, "", wx.Bitmap(os.path.join(ICON_PATH,'save.png')), wx.NullBitmap, shortHelp=_('Save File') ,longHelp=_('Save the current diagram'), clientData=self.canvas),
toolbar.AddTool(Menu.ID_SAVEAS, "", wx.Bitmap(os.path.join(ICON_PATH,'save_as.png')), wx.NullBitmap, shortHelp=_('Save File As'), longHelp=_('Save the diagram with an another name'), clientData=self.canvas),
toolbar.AddTool(wx.ID_UNDO, "", wx.Bitmap(os.path.join(ICON_PATH,'undo.png')), wx.NullBitmap, shortHelp=_('Undo'), longHelp=_('Click to go back, hold to see history'),clientData=self.canvas),
toolbar.AddTool(wx.ID_REDO, "", wx.Bitmap(os.path.join(ICON_PATH,'redo.png')), wx.NullBitmap, shortHelp=_('Redo'), longHelp=_('Click to go forward, hold to see history'),clientData=self.canvas),
toolbar.AddTool(Menu.ID_ZOOMIN_DIAGRAM, "", wx.Bitmap(os.path.join(ICON_PATH,'zoom+.png')), wx.NullBitmap, shortHelp=_('Zoom +'), longHelp=_('Zoom in'),clientData=self.canvas),
toolbar.AddTool(Menu.ID_ZOOMOUT_DIAGRAM, "", wx.Bitmap(os.path.join(ICON_PATH,'zoom-.png')), wx.NullBitmap, shortHelp=_('Zoom -'), longHelp=_('Zoom out'),clientData=self.canvas),
toolbar.AddTool(Menu.ID_UNZOOM_DIAGRAM, "", wx.Bitmap(os.path.join(ICON_PATH,'no_zoom.png')), wx.NullBitmap, shortHelp=_('AnnuleZoom'), longHelp=_('Initial view'),clientData=self.canvas),
toolbar.AddTool(Menu.ID_PRIORITY_DIAGRAM, "", wx.Bitmap(os.path.join(ICON_PATH,'priority.png')), shortHelp=_('Priority')),
toolbar.AddTool(Menu.ID_CHECK_DIAGRAM, "", wx.Bitmap(os.path.join(ICON_PATH,'check_master.png')), shortHelp=_('Check')),
toolbar.AddTool(Menu.ID_SIM_DIAGRAM, "", wx.Bitmap(os.path.join(ICON_PATH,'simulation.png')), shortHelp=_('Simulation')),
toolbar.AddTool(self.toggle_list[0], "", wx.Bitmap(os.path.join(ICON_PATH,'direct_connector.png')), shortHelp=_('Direct'), kind=wx.ITEM_CHECK),
toolbar.AddTool(self.toggle_list[1], "", wx.Bitmap(os.path.join(ICON_PATH,'square_connector.png')), shortHelp=_('Square'), kind=wx.ITEM_CHECK),
toolbar.AddTool(self.toggle_list[2], "", wx.Bitmap(os.path.join(ICON_PATH,'linear_connector.png')), shortHelp=_('Linear'), kind=wx.ITEM_CHECK)
]
toolbar.EnableTool(wx.ID_UNDO, not self.canvas.stockUndo == [])
toolbar.EnableTool(wx.ID_REDO, not self.canvas.stockRedo == [])
toolbar.InsertSeparator(2)
toolbar.InsertSeparator(5)
toolbar.InsertSeparator(9)
toolbar.InsertSeparator(13)
toolbar.InsertSeparator(17)
toolbar.ToggleTool(self.toggle_list[0],1)
#=======================================================================
### spin control for abstraction hierarchy
if isinstance(diagram, Container.Diagram):
level_label = wx.StaticText(toolbar, -1, _("Level "))
self.spin = wx.SpinCtrl(toolbar, self.toggle_list[3], str(level), (55, 90), (50, -1), min=0, max=10)
toolbar.AddControl(level_label)
toolbar.AddControl(self.spin)
ID_UPWARD = self.toggle_list[4]
ID_DOWNWARD = self.toggle_list[5]
if wx.VERSION_STRING < '2.9':
self.tools.append(toolbar.AddTool(ID_DOWNWARD, wx.Bitmap(os.path.join(ICON_PATH,'downward.png')), shortHelpString=_('Downward rules'), longHelpString=_('Define Downward rules atomic model')))
self.tools.append(toolbar.AddTool(ID_UPWARD, wx.Bitmap(os.path.join(ICON_PATH,'upward.png')), shortHelpString=_('Upward rules'), longHelpString=_('Define Upward rules atomic model')))
else:
self.tools.append(toolbar.AddTool(ID_DOWNWARD, "", wx.Bitmap(os.path.join(ICON_PATH,'downward.png')), shortHelp=_('Downward rules')))
self.tools.append(toolbar.AddTool(ID_UPWARD, "", wx.Bitmap(os.path.join(ICON_PATH,'upward.png')), shortHelp=_('Upward rules')))
### update downward and upward button
toolbar.EnableTool(ID_DOWNWARD, level != 0)
toolbar.EnableTool(ID_UPWARD, level != 0)
#=======================================================================
toolbar.Realize()
self.SetToolBar(toolbar)
### if Detached frame from block (container or Code)
### save, save-as and simulation are disabled
if not isinstance(self.parent, Container.ShapeCanvas):
#toolbar.EnableTool(Menu.ID_SAVE, False)
#toolbar.EnableTool(Menu.ID_SAVEAS, False)
toolbar.EnableTool(Menu.ID_SIM_DIAGRAM, False)
toolbar.EnableTool(Menu.ID_PRIORITY_DIAGRAM, not 'PyPDEVS' in builtins.__dict__['DEFAULT_DEVS_DIRNAME'])
else:
toolbar.EnableTool(Menu.ID_SAVEAS, False)
### Call Printable constructor
PrintOut.Printable.__init__(self, self.canvas)
### vertical box
vbox = wx.BoxSizer(wx.VERTICAL)
#vbox.Add(toolbar, 0, wx.EXPAND, border = 5)
vbox.Add(self.canvas, 1, wx.EXPAND, border = 5)
self.SetSizer(vbox)
self.CenterOnParent()
self.statusbar = self.CreateStatusBar(1, wx.ST_SIZEGRIP)
self.statusbar.SetFieldsCount(3)
self.statusbar.SetStatusWidths([-2, -5, -1])
self.__binding()
def __binding(self):
""" Binding event.
ClOSE event, IDLE event and MOVE event are binding here.
All other event are binding in the main application thanks to general identifiers
NB: ID are defined on the Menu.py file
"""
self.Bind(wx.EVT_CLOSE, self.OnClose)
### Transparent management when the frame is moving
self.Bind(wx.EVT_IDLE, self.OnIdle)
self.Bind(wx.EVT_MOVE, self.OnMove)
self.Bind(wx.EVT_TOOL, self.OnSaveFile, id=Menu.ID_SAVE)
self.Bind(wx.EVT_TOOL, self.OnSaveAsFile, id=Menu.ID_SAVEAS)
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnStayOnTop(self, event):
"""
"""
if self.GetWindowStyle()==self.default_style:
self.SetWindowStyle(wx.CLIP_CHILDREN | wx.STAY_ON_TOP)
else:
self.SetWindowStyle(self.default_style)
def OnSaveFile(self, event):
""" Save button has been clicked
"""
### OnSaveFile of the mainW is activated
mainW = wx.GetApp().GetTopWindow()
mainW.OnSaveFile(event)
def OnSaveAsFile(self, event):
""" Save button has been clicked
"""
### OnSaveAsFile of the mainW is activated
self.diagram.modify = False
Container.Block.OnExport(self.diagram, event)
def OnMove(self, event):
""" alpha manager
"""
if self.transparent == wx.ALPHA_OPAQUE:
self.transparent = 140
try:
self.SetTransparent(self.transparent)
except:
sys.stdout.write(_("No transparency"))
event.Skip()
def OnIdle(self, event):
""" alpha manager
"""
if self.transparent == 140:
self.transparent = wx.ALPHA_OPAQUE
try:
self.SetTransparent(self.transparent)
except:
sys.stderr.write(_("No transparency"))
event.Skip()
def GetCanvas(self):
""" Return the canvas
"""
return self.canvas
def OnClose(self, event):
""" Close event has been received.
"""
canvas = self.GetCanvas()
### bug fixe for windows since wx 4.0
if sys.platform.startswith('win'):
try:
canvas.OnLeftDown(event)
canvas.OnLeftUp(event)
except:
pass
canvas.Refresh()
### Destroy the windows
self.Destroy()
event.Skip()
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# MAIN PROGRAM
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
class TestApp(wx.App):
""" Testing application
"""
def OnInit(self):
""" Init the user interface.
"""
import gettext
#builtins.__dict__['PYDEVS_SIM_STRATEGY_DICT'] = {'original':'SimStrategy1', 'bag-based':'SimStrategy2', 'direct-coupling':'SimStrategy3'}
#builtins.__dict__['PYPDEVS_SIM_STRATEGY_DICT'] = {'original':'SimStrategy4', 'distributed':'SimStrategy5', 'parallel':'SimStrategy6'}
builtins.__dict__['NB_HISTORY_UNDO'] = 5
builtins.__dict__['ICON_PATH']='icons'
builtins.__dict__['ICON_PATH_16_16']=os.path.join(ICON_PATH,'16x16')
builtins.__dict__['_'] = gettext.gettext
diagram = Container.Diagram()
self.frame = DetachedFrame(None, -1, "Test", diagram)
self.frame.Show()
return True
def OnQuit(self, event):
self.Close()
if __name__ == '__main__':
app = TestApp(0)
app.MainLoop()