forked from capocchi/DEVSimPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandaloneGUI.py
More file actions
238 lines (188 loc) · 8.03 KB
/
Copy pathStandaloneGUI.py
File metadata and controls
238 lines (188 loc) · 8.03 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
231
232
233
234
235
236
237
238
# -*- coding: utf-8 -*-
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# StandaloneGUI.py ---
# --------------------------------
# Copyright (c) 2022
# Laurent CAPOCCHI
# (capocchi@univ-corse.fr)
# University of Corsica
# --------------------------------
# Version 1.0 last modified: 03/22/22
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GENERAL NOTES AND REMARKS:
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GLOBAL VARIABLES AND FUNCTIONS
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
import wx
import os
import builtins
import wx.lib.filebrowsebutton as filebrowse
from StandaloneNoGUI import StandaloneNoGUI
from Decorators import BuzyCursorNotification
_ = wx.GetTranslation
class ZipNameValidator(wx.Validator):
""" This validator is used to ensure that the user has entered something
into the text object editor dialog's text field.
"""
def __init__(self):
""" Standard constructor.
"""
wx.Validator.__init__(self)
def Clone(self):
""" Standard cloner.
Note that every validator must implement the Clone() method.
"""
return ZipNameValidator()
def Validate(self, win):
""" Validate the contents of the given text control.
"""
textCtrl = self.GetWindow()
text = textCtrl.GetValue()
if len(text) == 0 :
wx.MessageBox(_("A text object must contain some text!"), "Error")
textCtrl.SetBackgroundColour("pink")
textCtrl.SetFocus()
textCtrl.Refresh()
return False
elif text.endswith('.zip') == 0 :
wx.MessageBox(_("Zip name must end with .zip extention!"), "Error")
textCtrl.SetBackgroundColour("pink")
textCtrl.SetFocus()
textCtrl.Refresh()
return False
else:
textCtrl.SetBackgroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))
textCtrl.Refresh()
return True
def TransferToWindow(self):
""" Transfer data from validator to window.
The default implementation returns False, indicating that an error
occurred. We simply return True, as we don't do any data transfer.
"""
return True # Prevent wxDialog from complaining.
def TransferFromWindow(self):
""" Transfer data from window to validator.
The default implementation returns False, indicating that an error
occurred. We simply return True, as we don't do any data transfer.
"""
return True # Prevent wxDialog from complaining.
class StandaloneGUI(wx.Frame):
""" Class used to generate the standalone version of DEVSimPy-nogui as a zip file.
Args:
wx (wx.Frame): GUI to configure the exportation of the zip file.
"""
def __init__(self, *args, **kw):
"""Constructor.
"""
super(StandaloneGUI, self).__init__(*args, **kw)
### TODO generate yaml from current diagram
self.yaml = ""
self.InitUI()
self.Center()
def InitUI(self):
"""Initialize the wx.Frame
"""
icon = wx.EmptyIcon() if wx.VERSION_STRING < '4.0' else wx.Icon()
icon.CopyFromBitmap(wx.Bitmap(os.path.join(ICON_PATH_16_16, "properties.png"), wx.BITMAP_TYPE_ANY))
self.SetIcon(icon)
self.SetSize((-1, 220))
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
### Zip name field
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
self.st1 = wx.StaticText(panel, label=_('Filename:'))
hbox1.Add(self.st1, flag=wx.RIGHT, border=8)
self._tc = wx.TextCtrl(panel, -1, "devsimpy-nogui-pkg.zip", validator=ZipNameValidator())
hbox1.Add(self._tc, proportion=1)
vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
vbox.Add((-1, 10))
### Zip directory field
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
self._dbb = filebrowse.DirBrowseButton(
panel, -1, size=(450, -1))
hbox2.Add(self._dbb)
vbox.Add(hbox2, flag=wx.LEFT | wx.TOP, border=10)
vbox.Add((-1, 10))
### Options
hbox4 = wx.BoxSizer(wx.HORIZONTAL)
self._cb1 = wx.CheckBox(panel, label=_('Add simulation kernel'))
self._cb2 = wx.CheckBox(panel, label=_('Add Docker file'))
self._cb3 = wx.CheckBox(panel, label=_('No Time Limite'))
hbox4.Add(self._cb1)
hbox4.Add(self._cb2, flag=wx.LEFT, border=10)
hbox4.Add(self._cb3, flag=wx.LEFT, border=10)
vbox.Add(hbox4, flag=wx.LEFT, border=10)
vbox.Add((-1, 10))
hbox5 = wx.BoxSizer(wx.HORIZONTAL)
self._cb4 = wx.CheckBox(panel, label=_('Real Time'))
self.kernel = wx.Choice(panel, -1, choices=["PyDEVS", "PyPDEVS"])
self.kernel.SetSelection(0)
label = wx.StaticText(panel, -1, _("Kernel:"))
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(label, 0, wx.ALIGN_CENTER_VERTICAL)
box.Add(self.kernel, 0, wx.ALIGN_CENTER_VERTICAL, border=5)
hbox5.Add(self._cb4)
hbox5.Add(box, flag=wx.LEFT, border=10)
vbox.Add(hbox5, flag=wx.LEFT, border=10)
vbox.Add((-1, 10))
### Buttons
hbox5 = wx.BoxSizer(wx.HORIZONTAL)
btn1 = wx.Button(panel, wx.ID_OK, _("Ok"), size=(70, 30))
btn2 = wx.Button(panel, wx.ID_CLOSE, _("Close"), size=(70, 30))
hbox5.Add(btn2)
hbox5.Add(btn1, flag=wx.LEFT|wx.BOTTOM)
vbox.Add(hbox5, flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
panel.SetSizer(vbox)
### Binds
self.Bind(wx.EVT_BUTTON, self.OnOk, id=btn1.GetId())
self.Bind(wx.EVT_BUTTON, self.OnClose, id=btn2.GetId())
def SetYAML(self,yaml:str)->None:
""" Set the yaml file
"""
self.yaml = yaml
@BuzyCursorNotification
def OnOk(self, event):
"""
Args:
event (wx.Event): event emitted when the Ok button is clicked
"""
### call validator for zip name textctrl
if self._tc.GetValidator().Validate(self._tc):
zip_name = self._tc.GetLineText(0)
zip_dir = self._dbb.GetValue()
sim_cb = self._cb1.GetValue()
docker_cb = self._cb2.GetValue()
ntl_cb = self._cb3.GetValue()
rt = self._cb3.GetValue()
kernel = self.kernel.GetString(self.kernel.GetSelection())
### call the StandaloneNoGui class to build the package depending on the settings from the frame.
standalone = StandaloneNoGUI(self.yaml,zip_name,outdir=zip_dir,add_sim_kernel=sim_cb,add_dockerfile=docker_cb,sim_time=ntl_cb,rt=rt, kernel=kernel)
### Try to build de zip package of the standalone version of DEVSimPy-nogui
if standalone.BuildZipPackage():
resp = wx.MessageBox(_("Zip file exported!"), _("Information"), wx.OK | wx.ICON_INFORMATION)
if resp == wx.OK:
self.Close()
else:
resp = wx.MessageBox(_("Exportation failed! - Check the yaml file..."), _("Error"), wx.OK | wx.ICON_ERROR)
def OnClose(self, event):
"""Event handler of the close event
Args:
event (wx.Event): wx event
"""
self.Close()
def main():
"""To test the GUI
"""
builtins.__dict__['ICON_PATH_16_16']=os.path.join('icons','16x16')
ex = wx.App()
frame = StandaloneGUI(None, -1, 'Standalone Export')
frame.Show(True)
ex.MainLoop()
if __name__ == '__main__':
main()