forked from capocchi/DEVSimPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabelGUI.py
More file actions
230 lines (183 loc) · 6.2 KB
/
LabelGUI.py
File metadata and controls
230 lines (183 loc) · 6.2 KB
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
# -*- coding: utf-8 -*-
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# LabelGUI.py ---
# --------------------------------
# Copyright (c) 2020
# L. CAPOCCHI (capocchi@univ-corse.fr)
# SPE Lab - SISU Group - University of Corsica
# --------------------------------
# Version 1.0 last modified: 03/22/20
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GENERAL NOTES AND REMARKS:
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GLOBAL VARIABLES AND FUNCTIONS
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
import sys
import os
import wx
from wx import xrc
### just for individual test
if __name__ == '__main__':
import builtins
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')}
else:
from AttributeEditor import AttributeBase
__res = None
RESFILE = os.path.join(HOME_PATH,'XRC','LabelEditorDialog.xrc')
def __init_resources():
global __res
__res = xrc.EmptyXmlResource()
#home = os.path.abspath(os.path.dirname(sys.argv[0]))
__res.Load(RESFILE)
def get_resources():
""" This function provides access to the XML resources in this module."""
global __res
if __res == None:
__init_resources()
return __res
class LabelDialog(wx.Dialog):
"""
"""
def PreCreate(self, pre):
""" This function is called during the class's initialization.
Override it for custom setup before the window is created usually to
set additional window styles using SetWindowStyle() and SetExtraStyle().
"""
pass
def __init__(self, parent, block=None):
""" Constructor.
"""
wx.Dialog.__init__(self)
### local copy
self.block = block
self.parent = parent
_xrcName = "LabelEditorFrame"
### with Phoenix, no need to pre definde the dialogue windows.
### https://wxpython.org/Phoenix/docs/html/MigrationGuide.html?highlight=postcreate
### no need to ues the self.Create methode because LoadDialog already does what's necessary.
if wx.VERSION_STRING < '4.0':
pre = wx.PreDialog()
self.PreCreate(pre)
get_resources().LoadOnDialog(pre, parent, _xrcName)
self.PostCreate(pre)
else:
# XML Resources can be loaded from a file like this:
res = xrc.XmlResource(RESFILE)
# Now create a panel from the resource data
res.LoadDialog(self, parent, _xrcName)
self.XrcResourceLoadAll()
self.EventBinding()
self.SetProperties()
def EventBinding(self):
""" Event Binding
"""
self.ok_btn.Bind(wx.EVT_BUTTON, self.OnOk)
self.cancel_btn.Bind(wx.EVT_BUTTON, self.OnCancel)
self.m_radioBtn1.Bind(wx.EVT_RADIOBUTTON, self.OnButton)
self.m_radioBtn2.Bind(wx.EVT_RADIOBUTTON, self.OnButton)
self.m_radioBtn3.Bind(wx.EVT_RADIOBUTTON, self.OnButton)
self.label_txtCtrl.Bind(wx.EVT_TEXT, self.OnTextChange)
def XrcResourceLoadAll(self):
"""Loading Resource from XRC file
"""
self.ok_btn = xrc.XRCCTRL(self, 'ok_btn')
self.cancel_btn = xrc.XRCCTRL(self, 'cancel_btn')
self.label_txtCtrl = xrc.XRCCTRL(self, 'label_txtCtrl')
self.m_radioBtn1 = xrc.XRCCTRL(self, 'm_radioBtn1')
self.m_radioBtn2 = xrc.XRCCTRL(self, 'm_radioBtn2')
self.m_radioBtn3 = xrc.XRCCTRL(self, 'm_radioBtn3')
def SetProperties(self):
"""
"""
### default label
txt = self.block.label if self.block else ""
self.label_txtCtrl.SetValue(txt)
if txt != "":
self.old_label = txt
self.old_pos = self.block.label_pos
### update the radio button position depending on the model position label
self.m_radioBtn1.SetValue(self.old_pos == 'center')
self.m_radioBtn2.SetValue(self.old_pos == 'top')
self.m_radioBtn3.SetValue(self.old_pos == 'bottom')
def SetCanvas(self, canvas):
"""
"""
self.canvas = canvas
def OnTextChange(self, evt):
""" Text in CtrlText change
dynamic update of label during edition
"""
txt = self.label_txtCtrl.GetValue()
if hasattr(self, 'canvas') and txt != "" and self.block:
self.block.label=txt
### update of block from canvas
self.canvas.UpdateShapes([self.block])
if isinstance(self.parent, AttributeBase):
### update of label filed in properties dialogue
self.parent._list.SetCellValue(0, 1, self.block.label)
evt.Skip()
def OnButton(self, event):
""" Radio button has been clicked
"""
btn = event.GetEventObject()
label = btn.GetLabel()
if self.block:
### label of radio button and the value of a label_pos attribute are similar ;-)
self.block.label_pos = label
### update of block from canvas
self.canvas.UpdateShapes([self.block])
if isinstance(self.parent, AttributeBase):
### update of label_pos filed in properties dialogue
self.parent._list.SetCellValue(1, 1, self.block.label_pos)
def OnOk(self, evt):
""" Ok button has been clicked
"""
if self.block:
new_val = self.label_txtCtrl.GetValue()
if new_val != "":
self.block.label = new_val
if self.m_radioBtn1.GetValue():
self.block.label_pos = 'center'
elif self.m_radioBtn2.GetValue():
self.block.label_pos = 'top'
else:
self.block.label_pos = 'bottom'
### update of block from canvas
self.canvas.UpdateShapes([self.block])
self.Close()
def OnCancel(self, evt):
""" Cancel button has been clicked
"""
if self.block:
self.block.label = self.old_label
self.block.label_pos = self.old_pos
### update of block from canvas
self.canvas.UpdateShapes([self.block])
self.Destroy()
### ------------------------------------------------------------
class TestApp(wx.App):
""" Testing application
"""
def OnInit(self):
import gettext
dia = LabelDialog(None, None)
if dia.ShowModal() == wx.ID_CANCEL:
dia.Destroy()
else:
dia.Destroy()
return True
def OnQuit(self, event):
self.Close()
if __name__ == '__main__':
app = TestApp(0)
app.MainLoop()