forked from capocchi/DEVSimPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreferencesGUI.py
776 lines (621 loc) · 27.9 KB
/
PreferencesGUI.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# -*- coding: utf-8 -*-
import wx
import os
import builtins
import __main__
import shutil
import sys
import inspect
import configparser
import copy
import zipfile
import wx.lib.filebrowsebutton as filebrowse
_ = wx.GetTranslation
if __name__ == '__main__':
builtins.__dict__['HOME_PATH'] = os.getcwd()
builtins.__dict__['DEFAULT_DEVS_DIRNAME'] = 'PyDEVS'
builtins.__dict__['DEVS_DIR_PATH_DICT'] = {'PyDEVS':os.path.join(HOME_PATH,'DEVSKernel','PyDEVS'),
'PyPDEVS_221':os.path.join(HOME_PATH,'DEVSKernel','PyPDEVS','pypdevs221' ,'src'),
'PyPDEVS':os.path.join(HOME_PATH,'DEVSKernel','PyPDEVS','old')}
from HtmlWindow import HtmlFrame
from PluginsGUI import PluginsPanel, GeneralPluginsList, ModelPluginsManager
from Utilities import playSound, GetUserConfigDir, GetWXVersionFromIni, getPYFileListFromInit, AddToInitFile, DelToInitFile
import ReloadModule
import Menu
#----------------------------------------------------------------------
class GeneralPanel(wx.Panel):
""" General preferences panel
"""
def __init__(self, parent):
"""
Constructor.
"""
wx.Panel.__init__(self, parent)
### FileBrowse
self.plugin_dir = filebrowse.DirBrowseButton(self, wx.NewIdRef(), startDirectory=PLUGINS_PATH, labelText=_("Plug-ins directory:"), toolTip=_("Change the plug-ins directory"), dialogTitle=_("Plug-ins directory..."))
self.domain_dir = filebrowse.DirBrowseButton(self, wx.NewIdRef(), startDirectory=DOMAIN_PATH, labelText=_("Library directory:"), toolTip=_("Change the library directory"), dialogTitle=_("Libraries directory..."))
self.out_dir = filebrowse.DirBrowseButton(self, wx.NewIdRef(), startDirectory=OUT_DIR, labelText=_("Output directory:"), toolTip=_("Change the output directory"), dialogTitle=_("Output directory..."))
self.plugin_dir.SetValue(PLUGINS_PATH)
self.domain_dir.SetValue(DOMAIN_PATH)
self.out_dir.SetValue(OUT_DIR)
### StaticText
self.st1 = wx.StaticText(self, wx.NewIdRef(), _("Number of recent file:"))
self.st2 = wx.StaticText(self, wx.NewIdRef(), _("Font size:"))
self.st3 = wx.StaticText(self, wx.NewIdRef(), _("Deep of history item:"))
self.st4 = wx.StaticText(self, wx.NewIdRef(), _("wxPython version:"))
if wx.VERSION_STRING >= '4.0':
self.st1.SetToolTipString = self.st1.SetToolTip
self.st2.SetToolTipString = self.st2.SetToolTip
self.st3.SetToolTipString = self.st3.SetToolTip
self.st4.SetToolTipString = self.st4.SetToolTip
self.st1.SetToolTipString(_("Feel free to change the length of list defining the recent opened files."))
self.st2.SetToolTipString(_("Feel free to change the font size of DEVSimpy."))
self.st3.SetToolTipString(_("Feel free to change the number of item for undo/redo command."))
self.st4.SetToolTipString(_("Feel free to change the version of wxpython used loaded by DEVSimPy."))
### number of opened file
self.nb_opened_file = wx.SpinCtrl(self, wx.NewIdRef(), '')
self.nb_opened_file.SetRange(2, 20)
self.nb_opened_file.SetValue(NB_OPENED_FILE)
### Block font size
self.font_size = wx.SpinCtrl(self, wx.NewIdRef(), '')
self.font_size.SetRange(2, 20)
self.font_size.SetValue(FONT_SIZE)
### number of undo/redo items
self.nb_history_undo = wx.SpinCtrl(self, wx.NewIdRef(), '')
self.nb_history_undo.SetRange(2, 100)
self.nb_history_undo.SetValue(NB_HISTORY_UNDO)
### CheckBox
self.cb1 = wx.CheckBox(self, wx.NewIdRef(), _('Transparency'))
if wx.VERSION_STRING >= '4.0': self.cb1.SetToolTipString = self.cb1.SetToolTip
self.cb1.SetToolTipString(_("Transparency for the detached frame of diagrams"))
self.cb1.SetValue(builtins.__dict__['TRANSPARENCY'])
### wxPython version
wxv = [wx.VERSION_STRING]
self.cb2 = wx.ComboBox(self, wx.NewIdRef(), GetWXVersionFromIni(), choices=wxv, style=wx.CB_READONLY)
if wx.VERSION_STRING >= '4.0': self.cb2.SetToolTipString = self.cb2.SetToolTip
self.cb2.SetToolTipString(_("Default version of wxPython."))
self.default_wxv = self.cb2.GetValue()
### Sizer
box1 = wx.StaticBoxSizer(wx.StaticBox(self, wx.NewIdRef(), _('Properties')), orient=wx.VERTICAL)
vsizer = wx.BoxSizer(wx.VERTICAL)
hsizer = wx.GridSizer(4, 2, 20, 20)
hsizer.AddMany( [ (self.st1, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5),
(self.nb_opened_file, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5),
(self.st3, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5),
(self.nb_history_undo, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5),
(self.st2, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5),
(self.font_size, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5),
(self.st4, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5),
(self.cb2, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5)])
vsizer.Add(self.plugin_dir, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
vsizer.Add(self.domain_dir, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
vsizer.Add(self.out_dir, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
vsizer.Add(hsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
vsizer.Add(self.cb1, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
box1.Add(vsizer, 1, wx.EXPAND)
### Set sizer
self.SetSizer(box1)
self.SetAutoLayout(True)
def OnApply(self, event):
""" Apply change.
"""
### safe copy of default_wxv to manage the wx version changing
default_wxv = copy.copy(self.default_wxv)
self.OnNbOpenedFileChanged(event)
self.OnNbHistoryUndoChanged(event)
self.OnFontSizeChanged(event)
self.OnDomainPathChanged(event)
self.OnPluginsDirChanged(event)
self.OnOutDirChanged(event)
self.OnTransparancyChanged(event)
self.OnwxPythonVersionChanged(event)
### if the version of wx has been changed in OnwxPythonVersionChanged, we inform the user.
if self.default_wxv != default_wxv:
dlg = wx.MessageDialog(self, _("wxPython version has been changed.\nDEVSimPy requires a reboot to load the new version of wxPython."), _('wxPython Version Manager'), wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
###
def OnNbOpenedFileChanged(self, event):
builtins.__dict__['NB_OPENED_FILE'] = self.nb_opened_file.GetValue() # number of recent files
###
def OnNbHistoryUndoChanged(self, event):
builtins.__dict__['NB_HISTORY_UNDO'] = self.nb_history_undo.GetValue() # number of history undo
###
def OnFontSizeChanged(self, event):
builtins.__dict__['FONT_SIZE'] = self.font_size.GetValue() # Block font size
###
def OnDomainPathChanged(self, event):
"""
"""
new_domain_dir = self.domain_dir.GetValue()
old_parent_domain_dir = os.path.dirname(builtins.__dict__['DOMAIN_PATH'])
### if value has been changed, we clean the library control panel
if builtins.__dict__['DOMAIN_PATH'] != new_domain_dir:
### remove the parent of Domain directory of this one is not the devsimpy directory
if old_parent_domain_dir != builtins.__dict__['HOME_PATH']:
if old_parent_domain_dir in sys.path:
sys.path.remove(old_parent_domain_dir)
### remove the path from sys.path in order to update the import process
for path in [p for p in sys.path if builtins.__dict__['DOMAIN_PATH'] in p]:
sys.path.remove(path)
### TODO remove dirname of path from sys.modules ?
### update the builtin
builtins.__dict__['DOMAIN_PATH'] = new_domain_dir
### update all Domain (the process add in sys.path the path invoked when import is used
mainW = wx.GetApp().GetTopWindow()
nb1 = mainW.GetControlNotebook()
tree = nb1.GetTree()
for item in tree.GetItemChildren(tree.GetRootItem()):
tree.RemoveItem(item)
###
def OnPluginsDirChanged(self, event):
builtins.__dict__['PLUGINS_PATH'] = self.plugin_dir.GetValue()
###
def OnOutDirChanged(self, event):
builtins.__dict__['OUT_DIR'] = os.path.basename(self.out_dir.GetValue())
###
def OnTransparancyChanged(self, event):
builtins.__dict__['TRANSPARENCY'] = self.cb1.GetValue()
def OnwxPythonVersionChanged(self, event):
"""
"""
### new value
self.default_wxv = self.cb2.GetValue()
### update the init file into GetUserConfigDir
parser = configparser.ConfigParser()
path = os.path.join(GetUserConfigDir(), 'devsimpy.ini')
parser.read(path)
section, option = ('wxversion', 'to_load')
### if ini file exist we remove old section and option
if os.path.exists(path):
try:
parser.remove_option(section, option)
except:
pass
try:
parser.remove_section(section)
except:
pass
try:
parser.add_section(section)
except:
pass
if not parser.has_section(section):
parser.add_section(section)
parser.set(section, option, self.default_wxv)
parser.write(open(path,'w'))
class SimulationPanel(wx.Panel):
""" Simulation Panel
"""
def __init__(self, parent):
""" Constructor
"""
wx.Panel.__init__(self, parent)
### Sizer
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
hbox4 = wx.BoxSizer(wx.HORIZONTAL)
hbox5 = wx.BoxSizer(wx.HORIZONTAL)
vbox = wx.BoxSizer(wx.VERTICAL)
self.sim_success_sound_path = builtins.__dict__['SIMULATION_SUCCESS_SOUND_PATH']
self.sim_error_sound_path = builtins.__dict__['SIMULATION_ERROR_SOUND_PATH']
### Buttons
self.sim_success_sound_btn = wx.Button(self, wx.NewIdRef(), os.path.basename(self.sim_success_sound_path), (25, 105), name='success')
self.sim_success_sound_btn.Enable(self.sim_success_sound_path is not os.devnull)
if wx.VERSION_STRING >= '4.0': self.sim_success_sound_btn.SetToolTipString = self.sim_success_sound_btn.SetToolTip
self.sim_success_sound_btn.SetToolTipString(_("Press this button in order to change the song arriving at the end of the simulation."))
self.sim_error_sound_btn = wx.Button(self, wx.NewIdRef(), os.path.basename(self.sim_error_sound_path), (25, 105), name='error')
self.sim_error_sound_btn.Enable(self.sim_error_sound_path is not os.devnull)
if wx.VERSION_STRING >= '4.0': self.sim_error_sound_btn.SetToolTipString = self.sim_error_sound_btn.SetToolTip
self.sim_error_sound_btn.SetToolTipString(_("Press this button in order to change the song arriving when an error occur in a model during the simulation."))
self.devs_doc_btn = wx.Button(self, wx.ID_ABOUT, name='doc')
if wx.VERSION_STRING >= '4.0': self.devs_doc_btn.SetToolTipString = self.devs_doc_btn.SetToolTip
self.devs_doc_btn.SetToolTipString(_("Press this button to read the documentation of the selected DEVS package"))
### CheckBox
self.cb1 = wx.CheckBox(self, wx.NewIdRef(), _('Notification'))
if wx.VERSION_STRING >= '4.0': self.cb1.SetToolTipString = self.cb1.SetToolTip
self.cb1.SetToolTipString(_("Notification song is generate when the simulation is over."))
self.cb1.SetValue(self.sim_success_sound_path is not os.devnull)
self.cb2 = wx.CheckBox(self, wx.NewIdRef(), _('No Time Limit'))
if wx.VERSION_STRING >= '4.0': self.cb2.SetToolTipString = self.cb2.SetToolTip
self.cb2.SetValue(builtins.__dict__['NTL'])
self.cb2.SetToolTipString(_("No Time Limit allow the stop of simulation when all of models are idle."))
### StaticText for DEVS Kernel directory
self.txt3 = wx.StaticText(self, wx.NewIdRef(), _("DEVS packages:"))
self.cb3 = wx.ComboBox(self, wx.NewIdRef(), DEFAULT_DEVS_DIRNAME, choices=list(DEVS_DIR_PATH_DICT.keys()), style=wx.CB_READONLY)
if wx.VERSION_STRING >= '4.0': self.cb3.SetToolTipString = self.cb3.SetToolTip
self.cb3.SetToolTipString(_("Default DEVS Kernel package (PyDEVS, PyPDEVS, ect.)."))
self.default_devs_dir = DEFAULT_DEVS_DIRNAME
### StaticText for strategy
self.txt = wx.StaticText(self, wx.NewIdRef(), _("Default strategy:"))
### choice of combo-box depends on the default DEVS package directory
c = list(PYDEVS_SIM_STRATEGY_DICT.keys()) if DEFAULT_DEVS_DIRNAME == 'PyDEVS' else list(PYPDEVS_SIM_STRATEGY_DICT.keys())
self.cb4 = wx.ComboBox(self, wx.NewIdRef(), DEFAULT_SIM_STRATEGY, choices=c, style=wx.CB_READONLY)
if wx.VERSION_STRING >= '4.0': self.cb4.SetToolTipString = self.cb4.SetToolTip
self.cb4.SetToolTipString(_("Default strategy for the simulation algorithm. Please see the DEVSimPy doc for more information of possible strategy."))
self.sim_defaut_strategy = DEFAULT_SIM_STRATEGY
### StaticText
self.sim_defaut_plot_dyn_freq = builtins.__dict__['DEFAULT_PLOT_DYN_FREQ']
self.txt2 = wx.StaticText(self, wx.NewIdRef(), _("Frequency of plotting refresh:"))
self.sc = wx.SpinCtrl(self, wx.NewIdRef(), str(self.sim_defaut_plot_dyn_freq), (55, 90), (60, -1), min=10, max=10000)
if wx.VERSION_STRING >= '4.0': self.sc.SetToolTipString = self.sc.SetToolTip
self.sc.SetToolTipString(_("Default frequency for dynamic plotting."))
### Adding sizer
hbox1.Add(self.cb1, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 15)
hbox1.Add(self.sim_success_sound_btn, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND|wx.ALL, 15)
hbox1.Add(self.sim_error_sound_btn, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND|wx.ALL, 15)
hbox5.Add(self.txt3, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND|wx.ALL, 15)
hbox5.Add(self.cb3, 1, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND|wx.ALL, 15)
hbox5.Add(self.devs_doc_btn, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL)
hbox2.Add(self.txt, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL|wx.EXPAND, 15)
hbox2.Add(self.cb4, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL|wx.EXPAND, 15)
hbox3.Add(self.cb2, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 15)
hbox4.Add(self.txt2, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.ALL, 15)
hbox4.Add(self.sc, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT|wx.ALL, 15)
#hbox4.Add(information, 1, wx.ALIGN_CENTER_VERTICAL, 15)
##hbox4.Add(self.strategy_info, 1, wx.ALIGN_CENTER_VERTICAL, 15)
vbox.Add(hbox1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND|wx.ALL, 10)
vbox.Add(hbox5, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND|wx.ALL, 10)
vbox.Add(hbox2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND|wx.ALL, 10)
vbox.Add(hbox3, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 10)
vbox.Add(hbox4, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 10)
### Set sizer
self.SetSizer(vbox)
self.SetAutoLayout(True)
### Binding
self.sim_success_sound_btn.Bind(wx.EVT_BUTTON, self.OnSelectSound)
self.sim_error_sound_btn.Bind(wx.EVT_BUTTON, self.OnSelectSound)
self.devs_doc_btn.Bind(wx.EVT_BUTTON, self.OnAbout)
self.cb1.Bind(wx.EVT_CHECKBOX, self.onCb1Check)
self.cb4.Bind(wx.EVT_COMBOBOX, self.onCb4)
self.cb3.Bind(wx.EVT_COMBOBOX, self.onCb3)
self.sc.Bind(wx.EVT_SPINCTRL, self.onSc)
def OnAbout(self, evt):
""" Search doc directory into 'doc' directory of DEVS package
"""
### DEVS package
choice = self.cb3.GetValue()
### possible path of doc directory
path = os.path.join(os.path.dirname(builtins.__dict__['DEVS_DIR_PATH_DICT'][choice]), 'doc', 'index.html')
### Html frame
frame = HtmlFrame(self, wx.NewIdRef(), "Doc", (600,600))
### if page exist in <package_dir>/<doc>
if os.path.exists(path):
frame.LoadFile(path)
else:
frame.SetPage(_("<p> %s documentation directory not found! <p>")%choice)
### Show frame
frame.Show()
def OnSelectSound(self, evt):
"""
"""
dlg = wx.FileDialog(wx.GetTopLevelParent(self),
_("Choose a sound file"),
defaultDir = os.path.join(HOME_PATH,'sounds'),
wildcard = _("MP3 files (*.mp3)|*.mp3| WAV files (*.wav)|*.wav"),
style = wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
val = dlg.GetPath()
name = evt.GetEventObject().GetName()
try:
playSound(val)
if name == 'success':
self.sim_success_sound_path = val
elif name == 'error':
self.sim_error_sound_path = val
else:
pass
except NotImplementedError as v:
wx.MessageBox(str(v), _("Exception Message"))
dlg.Destroy()
def onCb1Check(self, evt):
""" CheckBox has been checked
"""
if evt.GetEventObject().GetValue():
self.sim_success_sound_btn.Enable(True)
self.sim_error_sound_btn.Enable(True)
self.sim_success_sound_path = __main__.builtin_dict['SIMULATION_SUCCESS_SOUND_PATH']
self.sim_error_sound_path = __main__.builtin_dict['SIMULATION_ERROR_SOUND_PATH']
else:
self.sim_success_sound_btn.Enable(False)
self.sim_error_sound_btn.Enable(False)
self.sim_success_sound_path = os.devnull
self.sim_error_sound_path = os.devnull
def onCb4(self, evt):
""" ComboBox has been checked
"""
val = evt.GetEventObject().GetValue()
self.sim_defaut_strategy = val
def onCb3(self, evt):
""" ComboBox has been checked
"""
val = evt.GetEventObject().GetValue()
### update cb below cb3
self.cb4.Clear()
if val == 'PyDEVS':
for k in PYDEVS_SIM_STRATEGY_DICT:
self.cb4.Append(k)
self.cb4.SetValue('bag-based')
else:
### PyPDEVS
for k in PYPDEVS_SIM_STRATEGY_DICT:
self.cb4.Append(k)
self.cb4.SetValue('classic')
### update default value for devs dir et sim strategy
self.default_devs_dir = val
self.sim_defaut_strategy = self.cb4.GetValue()
def onSc(self, evt):
""" CheckBox has been checked
"""
val = evt.GetEventObject().GetValue()
self.sim_defaut_plot_dyn_freq = val
def OnApply(self, evt):
""" Apply changes
"""
### Reload DomainBehavior and DomainStructure
if builtins.__dict__['DEFAULT_DEVS_DIRNAME'] != self.default_devs_dir:
### change builtin before recompile the modules
builtins.__dict__['DEFAULT_DEVS_DIRNAME'] = self.default_devs_dir
### recompile the modules.
### recompile DomainInterface.DomainBehavior with all loaded module depending on this one
d = copy.copy(sys.modules)
for m in d:
if d[m]:
try:
clsmembers = inspect.getmembers(d[m], inspect.isclass)
if clsmembers != [] and clsmembers[0][0] == 'DomainBehavior':
module_path = os.path.dirname(d[m].__file__)
### if m come from amd or cmd, pass path to recompile method to differentiate
if zipfile.is_zipfile(module_path):
ReloadModule.recompile(module_path)
else:
ReloadModule.recompile(m)
except Exception as info:
sys.stdout.write("Exception in PreferenceGui")
sys.stdout.write(str(info))
ReloadModule.recompile("DomainInterface.DomainStructure")
ReloadModule.recompile("DomainInterface.MasterModel")
### enable the priority (DEVS select function) icon depending on the selected DEVS kernel
mainW = wx.GetApp().GetTopWindow()
mainW.tb.EnableTool(Menu.ID_PRIORITY_DIAGRAM, not 'PyPDEVS' in builtins.__dict__['DEFAULT_DEVS_DIRNAME'])
builtins.__dict__['SIMULATION_SUCCESS_SOUND_PATH'] = self.sim_success_sound_path
builtins.__dict__['SIMULATION_ERROR_SOUND_PATH'] = self.sim_error_sound_path
builtins.__dict__['DEFAULT_SIM_STRATEGY'] = self.sim_defaut_strategy
builtins.__dict__['DEFAULT_PLOT_DYN_FREQ'] = self.sim_defaut_plot_dyn_freq
builtins.__dict__['NTL'] = self.cb2.GetValue()
class EditorPanel(wx.Panel):
""" Edition Panel
"""
def __init__(self, parent):
""" Constructor
"""
wx.Panel.__init__(self, parent)
vbox = wx.BoxSizer(wx.VERTICAL)
self.cb = wx.CheckBox(self, wx.NewIdRef(), _("Use local programmer software"))
self.cb.SetValue(builtins.__dict__['LOCAL_EDITOR'])
if wx.VERSION_STRING >= '4.0': self.cb.SetToolTipString = self.cb.SetToolTip
self.cb.SetToolTipString(_("This option don't work for the .amd and .cmd file. \n"
"Modification of python file during the simulation is disabled when this checkbox is checked."))
vbox.Add(self.cb, 0, wx.ALL,10)
self.SetSizer(vbox)
def OnApply(self, evt):
""" Apply changes
"""
builtins.__dict__['LOCAL_EDITOR'] = self.cb.IsChecked()
########################################################################
class Preferences(wx.Toolbook):
""" Based Toolbook Preference class
"""
def __init__(self, parent):
"""
Constructor.
"""
wx.Toolbook.__init__(self, parent, wx.NewIdRef(), style=wx.BK_DEFAULT)
### don't try to translate this labels with _() because there are used to find png
L = [('General',"(self)"),('Simulation',"(self)"), ('Editor',"(self)"), ('Plugins',"(self)")]
# make an image list using the LBXX images
il = wx.ImageList(25, 25)
for img in [wx.Bitmap(os.path.join(ICON_PATH, "%s_pref.png"%a[0])) for a in L]:
il.Add(img)
self.AssignImageList(il)
imageIdGenerator = iter(range(il.GetImageCount()))
for p, label in [("%sPanel%s"%(s,str(args)), _(s)) for s,args in L]:
page = eval(p)
self.AddPage(page, label, imageId=next(imageIdGenerator))
### Plug-in page setting (populate is done when page is changed)
self.pluginPanel = self.GetPage(self.GetPageCount()-1)
self.CheckList = GeneralPluginsList(self.pluginPanel.GetRightPanel())
self.pluginPanel.SetPluginsList(self.CheckList)
lpanel = self.pluginPanel.GetLeftPanel()
### Buttons for insert or delete plug-ins
self.addBtn = wx.Button(lpanel, wx.ID_ADD, size=(140, -1))
self.delBtn = wx.Button(lpanel, wx.ID_DELETE, size=(140, -1))
self.refBtn = wx.Button(lpanel, wx.ID_REFRESH, size=(140, -1))
if wx.VERSION_STRING >= '4.0':
self.addBtn.SetToolTipString = self.addBtn.SetToolTip
self.delBtn.SetToolTipString = self.delBtn.SetToolTip
self.refBtn.SetToolTipString = self.refBtn.SetToolTip
self.addBtn.SetToolTipString(_("Add new plug-ins"))
self.delBtn.SetToolTipString(_("Delete all existing plug-ins"))
self.refBtn.SetToolTipString(_("Refresh plug-ins list"))
### add widget to plug-in panel
self.pluginPanel.AddWidget(3, self.addBtn)
self.pluginPanel.AddWidget(4, self.delBtn)
self.pluginPanel.AddWidget(5, self.refBtn)
### Binding
self.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGED, self.OnPageChanged)
self.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGING, self.OnPageChanging)
self.Bind(wx.EVT_BUTTON, self.OnAdd, id=self.addBtn.GetId())
self.Bind(wx.EVT_BUTTON, self.OnDelete, id=self.delBtn.GetId())
self.Bind(wx.EVT_BUTTON, self.OnRefresh, id=self.refBtn.GetId())
def OnPageChanged(self, event):
"""
"""
# old = event.GetOldSelection()
new = event.GetSelection()
# sel = self.GetSelection()
parent = self.GetTopLevelParent()
### plug-ins page
if new == 3:
parent.SetSize((700,500))
else:
parent.SetSize((700,450))
event.Skip()
def OnPageChanging(self, event):
"""
"""
new = event.GetSelection()
### plug-in page
if new == 3:
### list of plug-ins file in plug-in directory
l = list(os.walk(PLUGINS_PATH))
### populate checklist with file in plug-ins directory
wx.CallAfter(self.CheckList.Populate, (l))
event.Skip()
def OnAdd(self, event):
""" Add plug-in
"""
wcd = 'All files (*)|*|Editor files (*.py)|*.py'
open_dlg = wx.FileDialog(self, message=_('Choose a file'), defaultDir=HOME_PATH, defaultFile='', wildcard=wcd, style=wx.OPEN|wx.CHANGE_DIR)
if open_dlg.ShowModal() == wx.ID_OK:
filename = open_dlg.GetPath()
### sure is python file
if filename.endswith(('.py','pyc')):
### Insert item in list
basename,ext = os.path.splitext(os.path.basename(filename))
root = os.path.dirname(filename)
self.CheckList.Importing(root, basename)
### trying to copy file in plug-in directory in order to find it again when the plugins list is populate (depending on the __init__.py file)
try:
shutil.copy2(filename, PLUGINS_PATH)
except Exception as info:
sys.stderr.write(_('ERROR: %s copy failed!\n%s')%(os.path.basename(filename), str(info)))
else:
### rewrite the new __init__.py file that contain the new imported plugin (basename) in order to populate the future generale plugins list
AddToInitFile(PLUGINS_PATH, [basename])
else:
sys.stderr.write(_('ERROR: %s is not a python file.\nOnly python file can be added as plugin.')%(os.path.basename(filename)))
open_dlg.Destroy()
def OnDelete(self, event):
""" Delete plugins item and python source file.
"""
for i in range(self.CheckList.GetItemCount()):
if self.CheckList.IsSelected(i):
### Delete query
dial = wx.MessageDialog(self, _('Do you want to delete the selected %s plugins?'%self.CheckList.GetItemText(i)), _('Plugin MAnager'), wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
if dial.ShowModal() == wx.ID_YES:
### for selected plug-ins
module = self.CheckList.GetPyData(i)[0]
basename,ext = os.path.splitext(os.path.basename(module.__file__))
### delete item
self.CheckList.DeleteItem(i)
### TODO: remove also into __init__.py
### delete the selected plugin from__init__.py
DelToInitFile(PLUGINS_PATH, [basename])
try:
#name, ext = os.path.splitext(module.__file__)
dlg = wx.MessageDialog(self, _('Do you want to remove the corresponding file %s?')%basename, _('Preference Manager'), wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
if dlg.ShowModal() == wx.ID_YES:
os.remove(module.__file__)
except Exception:
sys.stderr.write(_('ERROR: plugin file not deleted!'))
else:
dlg.Destroy()
else:
sys.stderr.write(_('Select plugins to delete'))
dial.Destroy()
def OnRefresh(self, event):
""" Refresh list of plugins.
"""
self.CheckList.Clear()
l = list(os.walk(PLUGINS_PATH))
### populate checklist with file in plug-ins directory
wx.CallAfter(self.CheckList.Populate, (l))
def OnApply(self,evt):
""" Apply button has been pressed and we must take into account all changes for each panel
"""
for page in [self.GetPage(i) for i in range(self.GetPageCount())]:
page.OnApply(evt)
########################################################################
class PreferencesGUI(wx.Frame):
""" DEVSimPy Preferences General User Interface class
"""
def __init__(self, parent, title):
"""
Constructor.
"""
wx.Frame.__init__(self, parent, wx.NewIdRef(), title, style = wx.DEFAULT_FRAME_STYLE | wx.CLIP_CHILDREN)
_icon = wx.EmptyIcon() if wx.VERSION_STRING < '4.0' else wx.Icon()
_icon.CopyFromBitmap(wx.Bitmap(os.path.join(ICON_PATH_16_16, "preferences.png"), wx.BITMAP_TYPE_ANY))
self.SetIcon(_icon)
self.SetMinSize((400,500))
### Panel
panel = wx.Panel(self, wx.NewIdRef())
self.pref = Preferences(panel)
### Buttons
self.cancel = wx.Button(panel, wx.ID_CANCEL)
self.apply = wx.Button(panel, wx.ID_OK)
if wx.VERSION_STRING >= '4.0':
self.apply.SetToolTipString = self.apply.SetToolTip
self.cancel.SetToolTipString = self.cancel.SetToolTip
self.apply.SetToolTipString(_("Apply all changing"))
self.cancel.SetToolTipString(_("Cancel without changing"))
self.apply.SetDefault()
### Sizers
vsizer = wx.BoxSizer(wx.VERTICAL)
hsizer = wx.BoxSizer(wx.HORIZONTAL)
hsizer.Add(self.cancel,0)
hsizer.Add(self.apply,0)
vsizer.Add(self.pref, 1, wx.ALL|wx.EXPAND, 5)
vsizer.Add(hsizer, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
panel.SetSizer(vsizer)
vsizer.Fit(panel)
### Binding
self.Bind(wx.EVT_BUTTON, self.OnApply, id=wx.ID_OK)
self.Bind(wx.EVT_BUTTON, self.OnCancel, id=wx.ID_CANCEL)
self.Bind(wx.EVT_BUTTON, self.OnClose, id=wx.ID_CLOSE)
self.Layout()
self.Center()
def OnApply(self, evt):
""" Apply button has been clicked.
"""
self.pref.OnApply(evt)
self.Close()
def OnCancel(self, evt):
self.Close()
def OnClose(self, evt):
self.Close()
### ------------------------------------------------------------
class TestApp(wx.App):
""" Testing application
"""
def OnInit(self):
import gettext
builtins.__dict__['ICON_PATH'] = os.path.join('icons')
builtins.__dict__['ICON_PATH_16_16'] = os.path.join(ICON_PATH, '16x16')
builtins.__dict__['PLUGINS_PATH'] = os.path.join(HOME_PATH, 'plugins')
builtins.__dict__['DOMAIN_PATH'] = 'Domain'
builtins.__dict__['OUT_DIR'] = 'out'
builtins.__dict__['NB_OPENED_FILE'] = 5
builtins.__dict__['FONT_SIZE'] = 10
builtins.__dict__['NB_HISTORY_UNDO'] = 10
builtins.__dict__['TRANSPARENCY'] = False
builtins.__dict__['SIMULATION_ERROR_SOUND_PATH'] = os.path.join(HOME_PATH,'sounds', 'Simulation-Error.mp3')
builtins.__dict__['SIMULATION_SUCCESS_SOUND_PATH'] = os.path.join(HOME_PATH,'sounds', 'Simulation-Success.mp3')
builtins.__dict__['NTL'] = False
builtins.__dict__['DEFAULT_SIM_STRATEGY'] = 'bag-based'
builtins.__dict__['DEFAULT_PYPDEVS_SIM_STRATEGY'] = 'original'
builtins.__dict__['DEFAULT_PLOT_DYN_FREQ'] = 100
builtins.__dict__['LOCAL_EDITOR'] = False
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__['_'] = gettext.gettext
frame = PreferencesGUI(None, "Test")
frame.Show()
return True
def OnQuit(self, event):
self.Close()
if __name__ == '__main__':
app = TestApp(0)
app.MainLoop()