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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
|
# -*- coding: utf-8 -*-
#
# AWL simulator - GUI main window
#
# Copyright 2012-2022 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *
import sys
import os
from awlsim.gui.util import *
from awlsim.gui.editmdiarea import *
from awlsim.gui.projecttreewidget import *
from awlsim.gui.cpuwidget import *
from awlsim.gui.guiconfig import *
from awlsim.gui.cpuconfig import *
from awlsim.gui.linkconfig import *
from awlsim.gui.hwmodconfig import *
from awlsim.gui.icons import *
from awlsim.gui.templatedialog import *
from awlsim.gui.library import *
from awlsim.gui.runstate import *
from awlsim.gui.toolbars import *
from awlsim.gui.validatorsched import *
class LoadProgressDialog(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setLayout(QGridLayout())
self.setContentsMargins(30, 30, 30, 30)
self.setWindowModality(Qt.ApplicationModal)
self.__isShown = False
self.__icon = QLabel(self)
self.layout().addWidget(self.__icon, 0, 0)
self.layout().setColumnMinimumWidth(1, 60)
self.__text = QLabel(self)
self.layout().addWidget(self.__text, 0, 2)
def closeEvent(self, ev):
if self.__isShown:
ev.ignore()
return
QDialog.closeEvent(self, ev)
def showProgress(self):
self.__isShown = True
self.show()
QApplication.processEvents(QEventLoop.ExcludeUserInputEvents, 50)
def hideProgress(self):
self.__isShown = False
self.hide()
QApplication.processEvents(QEventLoop.ExcludeUserInputEvents, 50)
def setGuiRunState(self, guiRunState):
if guiRunState.state == GuiRunState.STATE_LOAD:
self.setWindowTitle("Awlsim - Downloading...")
self.__icon.setPixmap(getIcon("download").pixmap(64, 64))
self.__text.setText("Downloading project to CPU.\n\n"
"Please be patient.\n"
"This might take a few seconds.")
self.showProgress()
else:
self.hideProgress()
class CpuDockWidget(QDockWidget):
def __init__(self, mainWidget, parent=None):
QDockWidget.__init__(self, "", parent)
self.mainWidget = mainWidget
self.setObjectName("CpuDockWidget")
self.toggleViewAction().setIcon(getIcon("cpu"))
self.setFeatures(QDockWidget.DockWidgetMovable |
QDockWidget.DockWidgetFloatable |
QDockWidget.DockWidgetClosable)
self.setAllowedAreas(Qt.AllDockWidgetAreas)
self.setWidget(CpuWidget(mainWidget))
self.topLevelChanged.connect(self.__handleTopLevelChange)
self.__handleTopLevelChange(self.isFloating())
@property
def cpuWidget(self):
return self.widget()
def __handleTopLevelChange(self, floating):
prefix = ""
if floating:
prefix = "%s - " % self.mainWidget.mainWindow.TITLE
self.setWindowTitle("%sCPU view" % prefix)
class ProjectTreeDockWidget(QDockWidget):
def __init__(self, mainWidget, parent=None):
QDockWidget.__init__(self, "", parent)
self.mainWidget = mainWidget
self.setObjectName("ProjectTreeDockWidget")
self.toggleViewAction().setIcon(getIcon("doc_edit"))
self.setFeatures(QDockWidget.DockWidgetMovable |
QDockWidget.DockWidgetFloatable)
self.setAllowedAreas(Qt.AllDockWidgetAreas)
self.projectTreeModel = ProjectTreeModel(mainWidget=mainWidget)
self.projectTreeView = ProjectTreeView(model=self.projectTreeModel,
parent=self)
self.setWidget(self.projectTreeView)
self.topLevelChanged.connect(self.__handleTopLevelChange)
self.__handleTopLevelChange(self.isFloating())
def __handleTopLevelChange(self, floating):
prefix = ""
if floating:
prefix = "%s - " % self.mainWidget.mainWindow.TITLE
self.setWindowTitle("%sProject" % prefix)
class MainWidget(QWidget):
# Signal: Project loaded
projectLoaded = Signal(Project)
# Signal: Dirty-status changed
dirtyChanged = Signal(int)
# Signal: Source text focus changed
textFocusChanged = Signal(bool)
# Signal: UndoAvailable state changed
undoAvailableChanged = Signal(bool)
# Signal: RedoAvailable state changed
redoAvailableChanged = Signal(bool)
# Signal: CopyAvailable state changed
copyAvailableChanged = Signal(bool)
# Signal: CutAvailable state changed
cutAvailableChanged = Signal(bool)
# Signal: PasteAvailable state changed
pasteAvailableChanged = Signal(bool)
# Document dirty levels
EnumGen.start
DIRTY_NO = EnumGen.item
DIRTY_SLIGHT = EnumGen.item
DIRTY_FULL = EnumGen.item
EnumGen.end
def __init__(self, mainWindow, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout(self))
self.mainWindow = mainWindow
self.simClient = GuiAwlSimClient(mainWindow)
self.editMdiArea = EditMdiArea(self)
self.layout().addWidget(self.editMdiArea, 0, 0)
self.filename = None
self.__dirtyLevel = self.DIRTY_NO
self.__guiRunState = GuiRunState()
self.__insnPerSecond = 0.0
self.__avgCycleTime = 0.0
self.__minCycleTime = 0.0
self.__maxCycleTime = 0.0
self.__padCycleTime = 0.0
self.__updateStatusBar()
self.editMdiArea.sourceChanged.connect(self.somethingChanged)
self.editMdiArea.focusChanged.connect(self.textFocusChanged)
self.editMdiArea.undoAvailableChanged.connect(self.undoAvailableChanged)
self.editMdiArea.redoAvailableChanged.connect(self.redoAvailableChanged)
self.editMdiArea.copyAvailableChanged.connect(self.copyAvailableChanged)
self.editMdiArea.cutAvailableChanged.connect(self.cutAvailableChanged)
self.editMdiArea.pasteAvailableChanged.connect(self.pasteAvailableChanged)
@property
def projectTreeModel(self):
return self.mainWindow.projectTreeModel
def getProject(self):
return self.projectTreeModel.getProject()
def isDirty(self):
return self.__dirtyLevel == self.DIRTY_FULL
def setDirty(self, dirtyLevel=DIRTY_FULL, force=False):
if dirtyLevel != self.__dirtyLevel or force:
if (not force and
self.__dirtyLevel == self.DIRTY_FULL and
dirtyLevel == self.DIRTY_SLIGHT):
# Cannot go from full to slight.
return
self.__dirtyLevel = dirtyLevel
self.dirtyChanged.emit(self.__dirtyLevel)
def somethingChanged(self):
self.setDirty(self.DIRTY_FULL)
def getFilename(self):
return self.filename
def getSimClient(self):
return self.simClient
def getCpuWidget(self):
return self.mainWindow.cpuWidget
def __updateCpuViewConfig(self):
"""Update the GuiCpuStateViewSettings objects in the project.
"""
settingsList = []
stateMdiArea = self.getCpuWidget().stateMdi
settingsList.append(stateMdiArea.getSettings())
self.getProject().getGuiSettings().setCpuStateViewSettingsList(settingsList)
def newFile(self, filename=None):
if isWinStandalone:
executableName = "awlsim-gui.exe"
else:
executableName = sys.executable
executable = findExecutable(executableName)
if not executable:
QMessageBox.critical(self,
"Failed to find '%s'" % executableName,
"Could not spawn a new instance.\n"
"Failed to find '%s'" % executableName)
return
if isWinStandalone:
argv = [ executable, ]
else:
argv = [ executable, "-m", "awlsim.gui.startup", ]
if filename:
argv.append(filename)
try:
PopenWrapper(argv, env=AwlSimEnv.getEnv())
except OSError as e:
QMessageBox.critical(self,
"Failed to execute '%s'" % executableName,
"Could not spawn a new instance.\n%s"
"Failed to execute '%s'" % (
str(e), executableName))
return
def loadFile(self, filename, newIfNotExist=False):
if self.isDirty():
res = QMessageBox.question(self,
"Unsaved project",
"The current project is modified and contains unsaved changes.\n "
"Do you want to:\n"
"- Save the project, close it and open the new project\n"
"- Open the new project in a new instance or\n"
"- Discard the changes and open the new project\n"
"- Cancel the operation",
QMessageBox.Save | QMessageBox.Discard |\
QMessageBox.Open | QMessageBox.Cancel,
QMessageBox.Open)
if res == QMessageBox.Save:
if not self.save():
return
elif res == QMessageBox.Discard:
pass
elif res == QMessageBox.Open:
self.newFile(filename)
return
elif res == QMessageBox.Cancel:
return
else:
assert(0)
self.getSimClient().action_goOffline()
self.getCpuWidget().stateMdi.reset()
if not os.path.exists(filename) and newIfNotExist:
# The file does not exist. We implicitly create it.
# The actual file will be created when the project is saved.
isNewProject = True
self.editMdiArea.resetArea()
self.projectTreeModel.reset()
else:
isNewProject = False
try:
self.projectTreeModel.loadProjectFile(filename, self)
guiSettings = self.getProject().getGuiSettings()
for viewSettings in guiSettings.getCpuStateViewSettingsList():
# We currently only have one CPU state view.
# If the project has multiple viewSettings, all but the
# last one will be lost.
self.getCpuWidget().stateMdi.loadFromCpuStateViewSettings(viewSettings)
except AwlSimError as e:
QMessageBox.critical(self,
"Failed to load project file", str(e))
return False
self.filename = filename
if isNewProject or not self.getProject().getProjectFile():
self.setDirty(self.DIRTY_FULL, force=True)
else:
self.setDirty(self.DIRTY_NO, force=True)
self.projectLoaded.emit(self.getProject())
return True
def load(self):
fn, fil = QFileDialog.getOpenFileName(self,
"Open project", "",
"Awlsim project or AWL/STL source (*.awlpro *.awl);;"
"Awlsim project (*.awlpro);;"
"AWL source (*.awl);;"
"All files (*)")
if not fn:
return
self.loadFile(fn, newIfNotExist=False)
def saveFile(self, filename):
try:
self.__updateCpuViewConfig()
res = self.projectTreeModel.saveProjectFile(filename, self)
if res == 0: # Failure
return False
elif res < 0: # Force save-as
return self.save(newFile=True)
except AwlSimError as e:
QMessageBox.critical(self,
"Failed to write project file", str(e))
return False
self.filename = filename
self.setDirty(self.DIRTY_NO, force=True)
return True
def save(self, newFile=False):
if newFile or not self.filename:
fn, fil = QFileDialog.getSaveFileName(self,
"Awlsim project save as", "",
"Awlsim project (*.awlpro)",
"*.awlpro")
if not fn:
return
if not fn.endswith(".awlpro"):
fn += ".awlpro"
return self.saveFile(fn)
else:
return self.saveFile(self.filename)
def guiConfig(self):
dlg = GuiConfigDialog(self.getProject(), self)
dlg.settingsChanged.connect(self.somethingChanged)
if dlg.exec_() == dlg.Accepted:
self.editMdiArea.setGuiSettings(self.getProject().getGuiSettings())
def linkConfig(self):
dlg = LinkConfigDialog(self.getProject(), self)
dlg.settingsChanged.connect(self.somethingChanged)
dlg.exec_()
def cpuConfig(self):
dlg = CpuConfigDialog(self.getProject(), self)
dlg.settingsChanged.connect(self.somethingChanged)
dlg.exec_()
def hwmodConfig(self):
dlg = HwmodConfigDialog(self.getProject(), self)
dlg.settingsChanged.connect(self.somethingChanged)
dlg.exec_()
def __pasteAwlText(self, text):
if not self.editMdiArea.paste(text):
QMessageBox.information(self,
"Please select AWL/STL source",
"Can not paste text.\n\n"
"Please move the text cursor to the place "
"in the AWL/STL code where you want to paste to.")
return False
return True
def __pasteSymbol(self, symbolName, address, dataType, comment):
"""Paste a symbol into one of the available symbol tables.
symbolName: Symbol name string.
address: Symbol address string.
dataType: Symbol type string.
comment: Symbol comment string.
Returns True, if the symbol has successfully been added.
"""
# Parse the symbol.
try:
project = self.getProject()
p = SymTabParser(project.getCpuConf().getConfiguredMnemonics())
symbol = p.parseSym(symbolName, address,
dataType, comment, 0)
except AwlSimError as e:
MessageBox.handleAwlSimError(self,
"Library symbol error", e)
return False
# Try to add the symbol to a symbol table
return self.projectTreeModel.symbolAdd(symbol, parentWidget=self)
def __pasteLibSel(self, libSelection):
"""Paste a library selection into the library selection table.
libSelection: The AwlLibEntrySelection() instance.
Returns True, if the selection has successfully been added.
"""
return self.projectTreeModel.libSelectionAdd(libSelection,
parentWidget=self)
def insertOB(self):
dlg = TemplateDialog.make_OB(self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
self.__pasteAwlText(Templates.getOB(dlg.getBlockNumber(),
dlg.getVerbose()))
dlg.finished.connect(dialogFinished)
dlg.show()
def insertFC(self):
dlg = TemplateDialog.make_FC(self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
self.__pasteAwlText(Templates.getFC(dlg.getBlockNumber(),
dlg.getVerbose()))
dlg.finished.connect(dialogFinished)
dlg.show()
def insertFB(self):
dlg = TemplateDialog.make_FB(self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
self.__pasteAwlText(Templates.getFB(dlg.getBlockNumber(),
dlg.getVerbose()))
dlg.finished.connect(dialogFinished)
dlg.show()
def insertInstanceDB(self):
dlg = TemplateDialog.make_instanceDB(self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
self.__pasteAwlText(Templates.getInstanceDB(dlg.getBlockNumber(),
dlg.getExtraNumber(),
dlg.getVerbose()))
dlg.finished.connect(dialogFinished)
dlg.show()
def insertGlobalDB(self):
dlg = TemplateDialog.make_globalDB(self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
self.__pasteAwlText(Templates.getGlobalDB(dlg.getBlockNumber(),
dlg.getVerbose()))
dlg.finished.connect(dialogFinished)
dlg.show()
def insertUDT(self):
dlg = TemplateDialog.make_UDT(self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
self.__pasteAwlText(Templates.getUDT(dlg.getBlockNumber(),
dlg.getVerbose()))
dlg.finished.connect(dialogFinished)
dlg.show()
def insertFCcall(self):
dlg = TemplateDialog.make_FCcall(self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
self.__pasteAwlText(Templates.getFCcall(dlg.getBlockNumber(),
dlg.getVerbose()))
dlg.finished.connect(dialogFinished)
dlg.show()
def insertFBcall(self):
dlg = TemplateDialog.make_FBcall(self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
self.__pasteAwlText(Templates.getFBcall(dlg.getBlockNumber(),
dlg.getExtraNumber(),
dlg.getVerbose()))
dlg.finished.connect(dialogFinished)
dlg.show()
def openLibrary(self):
dlg = LibraryDialog(self.getProject(), self)
def dialogFinished(result):
if result != QDialog.Accepted:
return
if dlg.pasteText:
# Paste the code.
if not self.__pasteAwlText(dlg.pasteText):
return
if dlg.pasteSymbol:
# Add a symbol to a symbol table.
symbolName, address, dataType, comment = dlg.pasteSymbol
if not self.__pasteSymbol(symbolName, address,
dataType, comment):
return
if dlg.pasteLibSel:
# Add a library selection to the library table.
if not self.__pasteLibSel(dlg.pasteLibSel):
return
dlg.finished.connect(dialogFinished)
dlg.show()
def undo(self):
self.editMdiArea.undo()
def redo(self):
self.editMdiArea.redo()
def cut(self):
self.editMdiArea.cut()
def copy(self):
self.editMdiArea.copy()
def paste(self):
self.editMdiArea.paste()
def findText(self):
self.editMdiArea.findText()
def findReplaceText(self):
self.editMdiArea.findReplaceText()
def openByIdentHash(self, identHash):
projectTreeModel = self.projectTreeModel
index = projectTreeModel.identHashToIndex(identHash)
if index.isValid():
return projectTreeModel.entryActivate(index, parentWidget=self)
return False
def handleGuiRunStateChange(self, guiRunState):
"""CPU RunState changed.
"""
self.__guiRunState = guiRunState
self.__updateStatusBar()
self.mainWindow.loadProgressDialog.setGuiRunState(guiRunState)
def handleCpuStats(self, statsMsg):
"""Received new AwlSimMessage_CPUSTATS.
"""
self.__insnPerSecond = statsMsg.insnPerSecond
self.__avgCycleTime = statsMsg.avgCycleTime
self.__minCycleTime = statsMsg.minCycleTime
self.__maxCycleTime = statsMsg.maxCycleTime
self.__padCycleTime = statsMsg.padCycleTime
self.__updateStatusBar()
def __updateStatusBar(self):
"""Update the main window status bar.
"""
status = []
if self.__guiRunState == GuiRunState.STATE_OFFLINE:
status.append("CPU: offline")
elif self.__guiRunState == GuiRunState.STATE_ONLINE:
status.append("CPU: online / STOP")
elif self.__guiRunState == GuiRunState.STATE_LOAD:
status.append("CPU: loading")
elif self.__guiRunState == GuiRunState.STATE_RUN:
status.append("CPU: RUN")
elif self.__guiRunState == GuiRunState.STATE_EXCEPTION:
status.append("CPU: EXCEPTION")
if self.__guiRunState == GuiRunState.STATE_RUN:
if self.__insnPerSecond > 0.0:
usPerInsnStr = "%.02f" % ((1.0 / self.__insnPerSecond) * 1000000.0)
status.append("%s stmt/s (%s µs/stmt)" % (
floatToHumanReadable(self.__insnPerSecond),
usPerInsnStr))
if (self.__avgCycleTime > 0.0 and
self.__minCycleTime > 0.0 and
self.__maxCycleTime > 0.0):
avgCycleTimeStr = "%.01f" % (self.__avgCycleTime * 1000.0)
minCycleTimeStr = "%.01f" % (self.__minCycleTime * 1000.0)
maxCycleTimeStr = "%.01f" % (self.__maxCycleTime * 1000.0)
padCycleTimeStr = "%.01f" % (self.__padCycleTime * 1000.0)
status.append("OB1: avg: %s ms min: %s ms max: %s ms padding: %s ms" % (
avgCycleTimeStr,
minCycleTimeStr,
maxCycleTimeStr,
padCycleTimeStr))
statusBar = self.mainWindow.statusBar()
statusBar.showMessage(" -- ".join(status))
class MainWindow(QMainWindow):
TITLE = "Awlsim PLC v%s" % VERSION_STRING
@classmethod
def start(cls,
initialAwlSource = None):
# Set basic qapp-details.
# This is important for QSettings.
QApplication.setOrganizationName("awlsim")
QApplication.setOrganizationDomain(AWLSIM_HOME_DOMAIN)
QApplication.setApplicationName("awlsim-gui")
QApplication.setApplicationVersion(VERSION_STRING)
mainwnd = cls(initialAwlSource)
mainwnd.show()
if initialAwlSource and not mainwnd.mainWidget.isDirty():
# Revert back from DIRTY_SLIGHT to DIRTY_NO.
mainwnd.mainWidget.setDirty(mainwnd.mainWidget.DIRTY_NO,
force=True)
return mainwnd
def __init__(self, awlSource=None, parent=None):
QMainWindow.__init__(self, parent)
self.setWindowIcon(getIcon("cpu"))
self.__profiler = None
self.setStatusBar(QStatusBar(self))
self.mainWidget = MainWidget(self, self)
self.cpuDockWidget = CpuDockWidget(self.mainWidget, self)
self.treeDockWidget = ProjectTreeDockWidget(self.mainWidget, self)
self.setCentralWidget(self.mainWidget)
self.setDockOptions(self.dockOptions() | QMainWindow.AllowTabbedDocks)
self.addDockWidget(Qt.LeftDockWidgetArea, self.treeDockWidget)
self.addDockWidget(Qt.BottomDockWidgetArea, self.cpuDockWidget)
self.tb = QToolBar(self)
self.tb.setObjectName("Main QToolBar")
self.tb.setWindowTitle("Main tool bar")
self.tb.toggleViewAction().setIcon(getIcon("prefs"))
self.tb.addAction(getIcon("new"), "New project",
self.mainWidget.newFile)
self.tb.addAction(getIcon("open"), "Open project",
self.mainWidget.load)
self.tbSaveAct = self.tb.addAction(getIcon("save"), "Save project",
self.mainWidget.save)
self.tb.addSeparator()
self.tbUndoAct = self.tb.addAction(getIcon("undo"), "Undo last edit",
self.mainWidget.undo)
self.tbRedoAct = self.tb.addAction(getIcon("redo"), "Redo",
self.mainWidget.redo)
self.tb.addSeparator()
self.tbCutAct = self.tb.addAction(getIcon("cut"), "Cut",
self.mainWidget.cut)
self.tbCopyAct = self.tb.addAction(getIcon("copy"), "Copy",
self.mainWidget.copy)
self.tbPasteAct = self.tb.addAction(getIcon("paste"), "Paste",
self.mainWidget.paste)
self.tb.addSeparator()
self.tbFindAct = self.tb.addAction(getIcon("find"), "Find...",
self.mainWidget.findText)
self.tbFindReplaceAct = self.tb.addAction(getIcon("findreplace"),
"Find and replace...",
self.mainWidget.findReplaceText)
self.tb.addSeparator()
self.tbLibAct = self.tb.addAction(getIcon("stdlib"), "Standard library",
self.mainWidget.openLibrary)
self.tbLibAct.setToolTip("Standard library.\n"
"(Please click into the AWL/STL source code\n"
"at the place where to paste the library call)")
self.addToolBar(Qt.TopToolBarArea, self.tb)
self.ctrlTb = CpuControlToolBar(self)
self.ctrlTb.toggleViewAction().setIcon(getIcon("prefs"))
self.addToolBar(Qt.LeftToolBarArea, self.ctrlTb)
self.inspectTb = CpuInspectToolBar(self)
self.inspectTb.toggleViewAction().setIcon(getIcon("prefs"))
self.addToolBar(Qt.LeftToolBarArea, self.inspectTb)
self.setMenuBar(QMenuBar(self))
menu = QMenu("&File", self)
menu.addAction(getIcon("new"), "&New project",
self.mainWidget.newFile)
menu.addAction(getIcon("open"), "&Open project...",
self.mainWidget.load)
self.saveAct = menu.addAction(getIcon("save"), "&Save project",
self.mainWidget.save)
menu.addAction(getIcon("save"), "&Save project as...",
lambda: self.mainWidget.save(True))
menu.addSeparator()
menu.addAction(getIcon("exit"), "&Exit...", self.close)
self.menuBar().addMenu(menu)
menu = QMenu("&Edit", self)
self.undoAct = menu.addAction(getIcon("undo"), "&Undo",
self.mainWidget.undo)
self.redoAct = menu.addAction(getIcon("redo"), "&Redo",
self.mainWidget.redo)
menu.addSeparator()
self.cutAct = menu.addAction(getIcon("cut"), "&Cut",
self.mainWidget.cut)
self.copyAct = menu.addAction(getIcon("copy"), "&Copy",
self.mainWidget.copy)
self.pasteAct = menu.addAction(getIcon("paste"), "&Paste",
self.mainWidget.paste)
menu.addSeparator()
self.findAct = menu.addAction(getIcon("find"), "&Find...",
self.mainWidget.findText)
self.findReplaceAct = menu.addAction(getIcon("findreplace"),
"Find and r&eplace...",
self.mainWidget.findReplaceText)
self.menuBar().addMenu(menu)
menu = QMenu("&Library", self)
menu.addAction(getIcon("textsource"), "Insert &OB template...",
self.mainWidget.insertOB)
menu.addAction(getIcon("textsource"), "Insert F&C template...",
self.mainWidget.insertFC)
menu.addAction(getIcon("textsource"), "Insert F&B template...",
self.mainWidget.insertFB)
menu.addAction(getIcon("textsource"), "Insert &instance-DB template...",
self.mainWidget.insertInstanceDB)
menu.addAction(getIcon("textsource"), "Insert &DB template...",
self.mainWidget.insertGlobalDB)
menu.addAction(getIcon("textsource"), "Insert &UDT template...",
self.mainWidget.insertUDT)
menu.addSeparator()
menu.addAction(getIcon("textsource"), "Insert FC C&ALL template...",
self.mainWidget.insertFCcall)
menu.addAction(getIcon("textsource"), "Insert FB CA&LL template...",
self.mainWidget.insertFBcall)
menu.addSeparator()
self.libAct = menu.addAction(getIcon("stdlib"), "&Standard library...",
self.mainWidget.openLibrary)
self.menuBar().addMenu(menu)
menu = QMenu("&CPU", self)
menu.addAction(self.ctrlTb.onlineAction)
menu.addAction(self.ctrlTb.resetAction)
menu.addAction(self.ctrlTb.downloadAction)
menu.addAction(self.ctrlTb.downloadSingleAction)
menu.addAction(self.ctrlTb.runAction)
menu.addAction(self.ctrlTb.diagAction)
menu.addSeparator()
menu.addAction(self.inspectTb.blocksAction)
menu.addAction(self.inspectTb.inputsAction)
menu.addAction(self.inspectTb.outputsAction)
menu.addAction(self.inspectTb.flagsAction)
menu.addAction(self.inspectTb.dbAction)
menu.addAction(self.inspectTb.timerAction)
menu.addAction(self.inspectTb.counterAction)
menu.addAction(self.inspectTb.cpuAction)
menu.addAction(self.inspectTb.lcdAction)
self.menuBar().addMenu(menu)
self.__windowMenu = QMenu("&Window", self)
self.menuBar().addMenu(self.__windowMenu)
self.__windowMenu.aboutToShow.connect(self.__buildWindowMenu)
menu = QMenu("&Help", self)
menu.addAction(getIcon("browser"), "Awlsim &homepage...", self.awlsimHomepage)
menu.addSeparator()
menu.addAction(getIcon("cpu"), "&About...", self.about)
menu.addSeparator()
self.__actProfileStart = menu.addAction(getIcon("enable"),
"Start profiling",
self.profileStart)
self.__actProfileStop = menu.addAction(getIcon("disable"),
"Stop profiling",
self.profileStop)
profEnabled = (AwlSimEnv.getProfileLevel() > 0)
self.__actProfileStart.setVisible(profEnabled)
self.__actProfileStop.setVisible(False)
self.menuBar().addMenu(menu)
self.__loadProgressDialog = LoadProgressDialog(self)
self.__sourceTextHasFocus = False
self.__dirtyChanged(MainWidget.DIRTY_NO)
self.__textFocusChanged(False)
self.__undoAvailableChanged(False)
self.__redoAvailableChanged(False)
self.__copyAvailableChanged(False)
self.__cutAvailableChanged(False)
self.__pasteAvailableChanged(False)
client = self.getSimClient()
self.mainWidget.projectLoaded.connect(self.__handleProjectLoaded)
self.mainWidget.dirtyChanged.connect(self.__dirtyChanged)
self.mainWidget.textFocusChanged.connect(self.__textFocusChanged)
self.mainWidget.undoAvailableChanged.connect(self.__undoAvailableChanged)
self.mainWidget.redoAvailableChanged.connect(self.__redoAvailableChanged)
self.mainWidget.copyAvailableChanged.connect(self.__copyAvailableChanged)
self.mainWidget.cutAvailableChanged.connect(self.__cutAvailableChanged)
self.mainWidget.pasteAvailableChanged.connect(self.__pasteAvailableChanged)
self.cpuDockWidget.toggleViewAction().toggled.connect(self.__cpuDockToggled)
self.inspectTb.connectToCpuWidget(self.cpuWidget)
GuiValidatorSched.get().haveValidationResult.connect(self.projectTreeModel.handleAwlSimError)
self.projectTreeModel.projectContentChanged.connect(self.mainWidget.somethingChanged)
client.haveCpuStats.connect(self.mainWidget.handleCpuStats)
client.haveIdentsMsg.connect(self.projectTreeModel.handleIdentsMsg)
client.haveException.connect(self.projectTreeModel.handleAwlSimError)
client.haveIdentsMsg.connect(self.editMdiArea.handleIdentsMsg)
client.haveInsnDump.connect(self.editMdiArea.handleInsnDump)
client.guiRunState.stateChanged.connect(self.editMdiArea.setGuiRunState)
client.guiRunState.stateChanged.connect(self.projectTreeModel.setGuiRunState)
client.guiRunState.stateChanged.connect(self.mainWidget.handleGuiRunStateChange)
if awlSource:
self.mainWidget.loadFile(awlSource, newIfNotExist=True)
self.__restoreState()
@property
def loadProgressDialog(self):
return self.__loadProgressDialog
@property
def editMdiArea(self):
return self.mainWidget.editMdiArea
@property
def projectTreeModel(self):
return self.treeDockWidget.projectTreeModel
@property
def cpuWidget(self):
return self.cpuDockWidget.cpuWidget
def getProject(self):
return self.projectTreeModel.getProject()
def getSimClient(self):
return self.mainWidget.getSimClient()
def __cpuDockToggled(self, cpuDockEnabled):
action = self.inspectTb.toggleViewAction()
if action.isChecked() != cpuDockEnabled:
action.trigger()
action.setEnabled(cpuDockEnabled)
def __buildWindowMenu(self):
"""Rebuild the window menu.
"""
menu = self.__windowMenu
menu.clear()
mdiSubWins = self.editMdiArea.subWindowList()
if mdiSubWins:
activeMdiSubWin = self.editMdiArea.activeOpenSubWindow
for mdiSubWin in mdiSubWins:
def activateWin(mdiSubWin=mdiSubWin):
self.editMdiArea.setActiveSubWindow(mdiSubWin)
action = menu.addAction(mdiSubWin.windowIcon(),
mdiSubWin.windowTitle(),
activateWin)
if mdiSubWin is activeMdiSubWin:
font = action.font()
font.setBold(True)
action.setFont(font)
menu.addSeparator()
def closeActive():
w = self.editMdiArea.activeOpenSubWindow
if w:
w.close()
def closeAllExceptActive():
active = self.editMdiArea.activeOpenSubWindow
for w in self.editMdiArea.subWindowList():
if w is not active:
w.close()
def closeAll():
self.editMdiArea.closeAllSubWindows()
action = menu.addAction(getIcon("doc_close"),
"&Close active window",
closeActive)
action.setEnabled(bool(mdiSubWins))
action = menu.addAction(getIcon("doc_close"),
"Close &all except active",
closeAllExceptActive)
action.setEnabled(bool(mdiSubWins))
action = menu.addAction(getIcon("doc_close"),
"Close a&ll",
closeAll)
action.setEnabled(bool(mdiSubWins))
menu.addSeparator()
menu.addAction(self.cpuDockWidget.toggleViewAction())
menu.addSeparator()
for tb in (self.tb, self.ctrlTb, self.inspectTb):
menu.addAction(tb.toggleViewAction())
def __saveState(self):
settings = QSettings()
# Save the main window state
settings.setValue("gui_main_window_state",
self.saveState(VERSION_ID))
# Save the main window geometry
settings.setValue("gui_main_window_geo",
self.saveGeometry())
def __restoreState(self):
settings = QSettings()
# Restore the main window geometry
geo = settings.value("gui_main_window_geo")
if geo:
self.restoreGeometry(geo)
# Restore the main window state
state = settings.value("gui_main_window_state")
if state:
self.restoreState(state, VERSION_ID)
# Only allow inspect tool bar switching, if the CPU dock is available.
cpuDockEn = self.cpuDockWidget.toggleViewAction().isChecked()
self.inspectTb.toggleViewAction().setEnabled(cpuDockEn)
def __dirtyChanged(self, dirtyLevel):
self.saveAct.setEnabled(dirtyLevel != MainWidget.DIRTY_NO)
self.tbSaveAct.setEnabled(dirtyLevel != MainWidget.DIRTY_NO)
filename = self.mainWidget.getFilename()
if filename:
postfix = " -- " + os.path.basename(filename)
if dirtyLevel == MainWidget.DIRTY_FULL:
postfix += "*"
else:
postfix = ""
self.setWindowTitle("%s%s" % (self.TITLE, postfix))
def __handleProjectLoaded(self, project):
self.__updateLibActions()
self.__updateFindActions()
def __updateFindActions(self):
findAvailable = self.editMdiArea.findTextIsAvailable()
self.tbFindAct.setEnabled(findAvailable)
self.findAct.setEnabled(findAvailable)
replaceAvailable = self.editMdiArea.findReplaceTextIsAvailable()
self.tbFindReplaceAct.setEnabled(replaceAvailable)
self.findReplaceAct.setEnabled(replaceAvailable)
def __updateLibActions(self):
# Enable/disable the library toolbar button.
# The menu library button is always available on purpose.
self.tbLibAct.setEnabled(self.editMdiArea.pasteIsAvailable())
def __textFocusChanged(self, textHasFocus):
self.__sourceTextHasFocus = textHasFocus
self.__updateLibActions()
self.__updateFindActions()
def __undoAvailableChanged(self, undoAvailable):
self.undoAct.setEnabled(undoAvailable)
self.tbUndoAct.setEnabled(undoAvailable)
def __redoAvailableChanged(self, redoAvailable):
self.redoAct.setEnabled(redoAvailable)
self.tbRedoAct.setEnabled(redoAvailable)
def __copyAvailableChanged(self, copyAvailable):
self.copyAct.setEnabled(copyAvailable)
self.tbCopyAct.setEnabled(copyAvailable)
def __cutAvailableChanged(self, cutAvailable):
self.cutAct.setEnabled(cutAvailable)
self.tbCutAct.setEnabled(cutAvailable)
def __pasteAvailableChanged(self, pasteAvailable):
self.pasteAct.setEnabled(pasteAvailable)
self.tbPasteAct.setEnabled(pasteAvailable)
self.__updateLibActions()
def closeEvent(self, ev):
if self.mainWidget.isDirty():
res = QMessageBox.question(self,
"Unsaved AWL/STL code",
"The editor contains unsaved AWL/STL code.\n"
"AWL/STL code will be lost by exiting without saving.",
QMessageBox.Discard | QMessageBox.Save | QMessageBox.Cancel,
QMessageBox.Cancel)
if res == QMessageBox.Save:
if not self.mainWidget.save():
ev.ignore()
return
elif res == QMessageBox.Cancel:
ev.ignore()
return
self.__saveState()
self.getSimClient().shutdown()
ev.accept()
QMainWindow.closeEvent(self, ev)
self.profileStop()
def keyPressEvent(self, ev):
if ev.matches(QKeySequence.Save):
self.mainWidget.save(False)
ev.accept()
return
elif ev.matches(QKeySequence.SaveAs):
self.mainWidget.save(True)
ev.accept()
return
QMainWindow.keyPressEvent(self, ev)
def awlsimHomepage(self):
QDesktopServices.openUrl(QUrl(AWLSIM_HOME_URL, QUrl.StrictMode))
def about(self):
QMessageBox.about(self, "About Awlsim PLC",
"Awlsim PLC version %s\n"
"\n"
"Copyright 2012-2022 Michael Büsch <m@bues.ch>\n"
"\n"
"Project home: %s\n"
"\n"
"\n"
"This program is free software; you can redistribute it and/or modify "
"it under the terms of the GNU General Public License as published by "
"the Free Software Foundation; either version 2 of the License, or "
"(at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful, "
"but WITHOUT ANY WARRANTY; without even the implied warranty of "
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License along "
"with this program; if not, write to the Free Software Foundation, Inc., "
"51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA." %\
(VERSION_STRING, AWLSIM_HOME_URL))
def profileStart(self):
if not self.__profiler:
try:
self.__profiler = Profiler()
self.__profiler.start()
except AwlSimError as e:
MessageBox.handleAwlSimError(self,
"Failed to start profiler.", e)
self.__profiler = None
return
self.__actProfileStart.setVisible(False)
self.__actProfileStop.setVisible(True)
def profileStop(self):
if self.__profiler:
self.__profiler.stop()
printInfo("GUI profiler dump:\n" + self.__profiler.getResult())
self.__profiler = None
self.__actProfileStart.setVisible(True)
self.__actProfileStop.setVisible(False)
|