aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/gui/editmdiarea.py
blob: eb23be3828c55fffaaf3d936816a43fc6406cc3c (plain)
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
# -*- coding: utf-8 -*-
#
# AWL simulator - GUI editor MDI area
#
# Copyright 2017-2018 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 *

from awlsim.gui.editwidget import EditWidget
from awlsim.gui.symtabwidget import SymTabView
from awlsim.gui.libtablewidget import LibTableView
from awlsim.gui.fup.fupwidget import FupWidget
from awlsim.gui.sourcecodeedit import SourceCodeEdit
from awlsim.gui.icons import *
from awlsim.gui.util import *
from awlsim.gui.runstate import *
from awlsim.gui.validatorsched import *


__all__ = [
	"EditMdiArea",
]


class EditMdiArea(QMdiArea):
	"""Main editor MDI area.
	"""

	# Signal: Emitted, if the source code changed.
	sourceChanged = Signal()

	# Signal: Keyboard focus in/out event.
	focusChanged = 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)

	# Signal: The visible AWL line range changed
	#         Parameters are: AwlSource, visibleFromLine, visibleToLine
	visibleLinesChanged = Signal(object, int, int)

	def __init__(self, mainWidget):
		QMdiArea.__init__(self, parent=mainWidget)
		self.mainWidget = mainWidget

		self.__onlineDiagMdiSubWin = None

		GuiValidatorSched.get().haveValidationResult.connect(
			self.__handleDocumentValidationResult)

		# Init the editor find dialog.
		SourceCodeEdit.initFindDialog(self)

		self.setViewMode(QMdiArea.SubWindowView)
		self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
		self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
		self.setTabsClosable(True)
		self.setTabsMovable(True)
		self.resetArea()

		self.subWindowActivated.connect(self.__handleSubWindowActivated)
		self.getSimClient().haveException.connect(self.__handleAwlSimError)

	def getMainWidget(self):
		return self.mainWidget

	def getProjectTreeModel(self):
		return self.mainWidget.projectTreeModel

	def getProject(self):
		return self.getProjectTreeModel().getProject()

	def getSimClient(self):
		return self.getMainWidget().getSimClient()

	@property
	def activeOpenSubWindow(self):
		"""Get the currently active and open sub window.
		"""
		mdiSubWin = self.activeSubWindow()
		if not mdiSubWin:
			return None
		if mdiSubWin.isClosing():
			return None
		return mdiSubWin

	def resetArea(self):
		"""Close all MDI sub windows and clear all state.
		"""
		self.__onlineDiagEnabled = False
		self.__guiRunState = None
		for mdiSubWin in list(self.subWindowList()):
			mdiSubWin.forceClose()
			del mdiSubWin
		self._guiSettings = GuiSettings()

	def __handleAwlSimError(self, e):
		for mdiSubWin in self.subWindowList():
			mdiSubWin.handleAwlSimError(e)

	def __handleSubWindowActivated(self, mdiSubWin):
		"""An MDI sub window has just been activated.
		"""
		self.__handleSubWinUndoAvailChanged(mdiSubWin)
		self.__handleSubWinRedoAvailChanged(mdiSubWin)
		self.__handleSubWinCopyAvailChanged(mdiSubWin)
		self.__handleSubWinCutAvailChanged(mdiSubWin)
		self.__handleSubWinPasteAvailChanged(mdiSubWin)
		GuiValidatorSched.get().startAsyncValidation(self.getProject,
							     delaySec=0.5)
		self.__refreshOnlineDiagState()
		self.__setFindDialogReference(mdiSubWin)

	def __handleSubWinFocusChanged(self, mdiSubWin, hasFocus):
		"""Text focus of one sub window has changed.
		"""
		self.focusChanged.emit((self.activeOpenSubWindow is mdiSubWin) and\
				       hasFocus)

	def __handleSubWinUndoAvailChanged(self, mdiSubWin):
		"""Undo-state of one sub window has changed.
		"""
		self.undoAvailableChanged.emit((self.activeOpenSubWindow is mdiSubWin) and\
					       self.undoIsAvailable())

	def __handleSubWinRedoAvailChanged(self, mdiSubWin):
		"""Redo-state of one sub window has changed.
		"""
		self.redoAvailableChanged.emit((self.activeOpenSubWindow is mdiSubWin) and\
					       self.redoIsAvailable())

	def __handleSubWinCopyAvailChanged(self, mdiSubWin):
		"""Copy-state of one sub window has changed.
		"""
		self.copyAvailableChanged.emit((self.activeOpenSubWindow is mdiSubWin) and\
					       self.copyIsAvailable())

	def __handleSubWinCutAvailChanged(self, mdiSubWin):
		"""Cut-state of one sub window has changed.
		"""
		self.cutAvailableChanged.emit((self.activeOpenSubWindow is mdiSubWin) and\
					      self.cutIsAvailable())

	def __handleSubWinPasteAvailChanged(self, mdiSubWin):
		"""Paste-state of one sub window has changed.
		"""
		self.pasteAvailableChanged.emit((self.activeOpenSubWindow is mdiSubWin) and\
					        self.pasteIsAvailable())

	def __newWin(self, mdiSubWin):
		self.addSubWindow(mdiSubWin)
		mdiSubWin.show()

		# Connect signals of sub window.
		mdiSubWin.sourceChanged.connect(self.sourceChanged)
		mdiSubWin.focusChanged.connect(
			lambda hasFocus: self.__handleSubWinFocusChanged(mdiSubWin, hasFocus))
		mdiSubWin.undoAvailableChanged.connect(
			lambda undoAvail: self.__handleSubWinUndoAvailChanged(mdiSubWin))
		mdiSubWin.redoAvailableChanged.connect(
			lambda redoAvail: self.__handleSubWinRedoAvailChanged(mdiSubWin))
		mdiSubWin.copyAvailableChanged.connect(
			lambda copyAvail: self.__handleSubWinCopyAvailChanged(mdiSubWin))
		mdiSubWin.cutAvailableChanged.connect(
			lambda cutAvail: self.__handleSubWinCutAvailChanged(mdiSubWin))
		mdiSubWin.pasteAvailableChanged.connect(
			lambda pasteAvail: self.__handleSubWinPasteAvailChanged(mdiSubWin))
		mdiSubWin.resizeFont.connect(self.__handleSourceCodeFontResize)
		mdiSubWin.validateDocument.connect(
			lambda delaySec: GuiValidatorSched.get().startAsyncValidation(project=self.getProject,
										      delaySec=delaySec))
		mdiSubWin.visibleLinesChanged.connect(self.__handleVisibleLinesChanged)

		self.__handleSubWinFocusChanged(mdiSubWin, True)
		GuiValidatorSched.get().startAsyncValidation(project=self.getProject,
							     delaySec=0.5)
		mdiSubWin.setGuiRunState(self.__guiRunState)

		return mdiSubWin

	def newWin_AWL(self, source):
		return self.__newWin(AwlEditMdiSubWindow(self, source))

	def newWin_FUP(self, source):
		return self.__newWin(FupEditMdiSubWindow(self, source))

	def newWin_KOP(self, source):
		return self.__newWin(KopEditMdiSubWindow(self, source))

	def newWin_SymTab(self, source):
		return self.__newWin(SymTabEditMdiSubWindow(self, source))

	def newWin_Libsel(self, libSelections):
		return self.__newWin(LibSelEditMdiSubWindow(self, libSelections))

	def __handleSourceCodeFontResize(self, bigger):
		self.__sourceCodeFontResize(1 if bigger else -1)

	def __sourceCodeFontResize(self, increment):
		"""Resize the editor font.
		"""
		if increment == 0:
			return
		project = self.getProject()
		font = getDefaultFixedFont()
		fontStr = project.getGuiSettings().getEditorFont()
		if fontStr:
			font.fromString(fontStr)
			font.setStyleHint(QFont.Courier)
		font.setPointSize(font.pointSize() + increment)
		if (increment > 0 and font.pointSize() > 72) or\
		   (increment < 0 and font.pointSize() < 6):
			return
		project.getGuiSettings().setEditorFont(font.toString())
		self.setGuiSettings(project.getGuiSettings())

		self.sourceChanged.emit()

	def __handleDocumentValidationResult(self, exception):
		"""Handle a background source code validator exception.
		"""
		for mdiSubWin in self.subWindowList():
			mdiSubWin.handleDocumentValidationResult(exception)

	def __refreshOnlineDiagState(self):
		"""Refresh the online diagnosis state of the sub windows.
		"""
		activeMdiSubWin = self.activeOpenSubWindow

		# Disable all inactive sub windows.
		for mdiSubWin in self.subWindowList():
			if mdiSubWin.ONLINE_DIAG:
				if not self.__onlineDiagEnabled or\
				   mdiSubWin is not activeMdiSubWin:
					mdiSubWin.enableOnlineDiag(False)

		# Enable the active sub window, if possible.
		if activeMdiSubWin and activeMdiSubWin.ONLINE_DIAG:
			self.__onlineDiagMdiSubWin = activeMdiSubWin
			if self.__onlineDiagEnabled:
				activeMdiSubWin.enableOnlineDiag(True)
			source = activeMdiSubWin.getSource()
			fromLine, toLine = activeMdiSubWin.getVisibleLineRange()
			self.visibleLinesChanged.emit(source, fromLine, toLine)
		else:
			self.__onlineDiagMdiSubWin = None
			self.visibleLinesChanged.emit(None, -1, -1)

	def __handleVisibleLinesChanged(self, mdiSubWin, source, visibleFromLine, visibleToLine):
		self.visibleLinesChanged.emit(source, visibleFromLine, visibleToLine)

	def enableOnlineDiag(self, enabled):
		"""Enable or disable online diagnosis in the active sub window.
		"""
		self.__onlineDiagEnabled = enabled
		self.__refreshOnlineDiagState()

	def handleInsnDump(self, insnDumpMsg):
		mdiSubWin = self.__onlineDiagMdiSubWin
		if mdiSubWin:
			mdiSubWin.handleInsnDump(insnDumpMsg)

	def handleIdentsMsg(self, identsMsg):
		for mdiSubWin in self.subWindowList():
			mdiSubWin.handleIdentsMsg(identsMsg)

	def setGuiRunState(self, runState):
		"""Update the CPU run state.
		runState is a GuiRunState instance.
		"""
		self.__guiRunState = runState
		for mdiSubWin in self.subWindowList():
			mdiSubWin.setGuiRunState(runState)
			if runState in (GuiRunState.STATE_LOAD,
					GuiRunState.STATE_RUN):
				# Clear all errors.
				mdiSubWin.handleAwlSimError(None)

	def undoIsAvailable(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.undoIsAvailable() if mdiSubWin else False

	def undo(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.undo() if mdiSubWin else False

	def redoIsAvailable(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.redoIsAvailable() if mdiSubWin else False

	def redo(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.redo() if mdiSubWin else False

	def cutIsAvailable(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.cutIsAvailable() if mdiSubWin else False

	def cut(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.cut() if mdiSubWin else False

	def copyIsAvailable(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.copyIsAvailable() if mdiSubWin else False

	def copy(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.copy() if mdiSubWin else False

	def pasteIsAvailable(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.pasteIsAvailable() if mdiSubWin else False

	def paste(self, text=None):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.paste(text) if mdiSubWin else False

	def findTextIsAvailable(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.findTextIsAvailable() if mdiSubWin else False

	def findText(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.findText() if mdiSubWin else False

	def findReplaceTextIsAvailable(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.findReplaceTextIsAvailable() if mdiSubWin else False

	def findReplaceText(self):
		mdiSubWin = self.activeOpenSubWindow
		return mdiSubWin.findReplaceText() if mdiSubWin else False

	def __setFindDialogReference(self, mdiSubWin):
		"""Switch the find dialog reference to the new window (or None).
		"""
		findDlg = SourceCodeEdit.getFindDialog()
		if findDlg:
			if mdiSubWin and mdiSubWin.TYPE == mdiSubWin.TYPE_AWL:
				findDlg.setTextEdit(mdiSubWin.editWidget)
			else:
				findDlg.setTextEdit(None)

	def setGuiSettings(self, guiSettings):
		self._guiSettings = guiSettings
		for mdiSubWin in self.subWindowList():
			mdiSubWin.setGuiSettings(guiSettings)

class EditMdiSubWindow(QMdiSubWindow):
	"""Edit MDI sub window base class.
	"""

	EnumGen.start
	TYPE_AWL	= EnumGen.item
	TYPE_FUP	= EnumGen.item
	TYPE_KOP	= EnumGen.item
	TYPE_SYMTAB	= EnumGen.item
	TYPE_LIBSEL	= EnumGen.item
	EnumGen.end
	TYPE		= None

	# True, if this sub window support online diagnosis.
	ONLINE_DIAG	= False

	# Signal: Emitted, if this MDI sub window is about to close.
	closed = Signal(QMdiSubWindow)

	# Signal: Emitted, if the source code changed.
	sourceChanged = Signal()

	# Signal: Keyboard focus in/out event.
	focusChanged = 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)

	# Signal: Change the font size.
	#         If the parameter is True, increase font size.
	resizeFont = Signal(bool)

	# Signal: Validation request.
	#	  A code validation should take place.
	#	  Parameter: Number of seconds to delay before validation start.
	validateDocument = Signal(float)

	# Signal: The visible AWL line range changed
	#         Parameters are: EditMdiSubWindow, AwlSource, visibleFromLine, visibleToLine
	visibleLinesChanged = Signal(object, object, int, int)

	def __init__(self):
		self.__forceClose = False
		self.__isClosing = False
		QMdiSubWindow.__init__(self)
		self.setAttribute(Qt.WA_DeleteOnClose)

	def setWidget(self, childWidget):
		QMdiSubWindow.setWidget(self, childWidget)
		childWidget.setParent(self)

	def closeEvent(self, ev):
		mdiArea = self.mdiArea()
		mainWidget = mdiArea.getMainWidget()
		projectTreeModel = mdiArea.getProjectTreeModel()

		if mainWidget.isDirty():
			# The closed window might contain changes that
			# have not yet been merged with the project.
			# Refresh the project now.
			refreshOk = projectTreeModel.refreshProject()
			if not refreshOk and not self.__forceClose:
				# Refresh failed.
				QMessageBox.critical(self,
					"Failed to refresh project",
					"Failed to refresh project data.\n"
					"Not closing the editor window "
					"to avoid loss of data.")
				# Do not close the sub window.
				ev.ignore()
				return

		# No way back. We are closing.
		self.__isClosing = True
		self.closed.emit(self)
		QMdiSubWindow.closeEvent(self, ev)

	def wheelEvent(self, ev):
		QMdiSubWindow.wheelEvent(self, ev)
		# Always accept the wheel event.
		# This avoids forwarding it to the parent MDI area,
		# if the scroll happened in this MDI sub window.
		ev.accept()

	def updateTitle(self):
		pass

	def forceClose(self):
		self.__forceClose = True
		return self.close()

	def isClosing(self):
		return self.__isClosing

	def getSource(self):
		raise NotImplementedError

	def setSource(self, source):
		raise NotImplementedError

	def importSource(self, fileName=None):
		return False

	def exportSource(self, fileName=None):
		return False

	def undoIsAvailable(self):
		return False

	def undo(self):
		return False

	def redoIsAvailable(self):
		return False

	def redo(self):
		return False

	def cutIsAvailable(self):
		return False

	def cut(self):
		return False

	def copyIsAvailable(self):
		return False

	def copy(self):
		return False

	def pasteIsAvailable(self):
		return False

	def paste(self, text=None):
		return False

	def findTextIsAvailable(self):
		return False

	def findText(self):
		return False

	def findReplaceTextIsAvailable(self):
		return self.findTextIsAvailable()

	def findReplaceText(self):
		return False

	def setGuiSettings(self, guiSettings):
		pass

	def enableOnlineDiag(self, enabled):
		return False

	def getVisibleLineRange(self):
		return (-1, -1)

	def handleInsnDump(self, insnDumpMsg):
		pass

	def handleIdentsMsg(self, identsMsg):
		pass

	def handleAwlSimError(self, exception):
		pass

	def handleDocumentValidationResult(self, exception):
		self.handleAwlSimError(exception)

	def setGuiRunState(self, runState):
		pass

class AwlEditMdiSubWindow(EditMdiSubWindow):
	TYPE		= EditMdiSubWindow.TYPE_AWL
	ONLINE_DIAG	= True

	def __init__(self, mdiArea, source):
		EditMdiSubWindow.__init__(self)

		self.editWidget = EditWidget(self)
		self.editWidget.setSource(source)
		self.editWidget.setSettings(mdiArea._guiSettings)
		self.setWidget(self.editWidget)

		self.editWidget.codeChanged.connect(self.sourceChanged)
		self.editWidget.focusChanged.connect(self.focusChanged)
		self.editWidget.visibleRangeChanged.connect(self.__emitVisibleLinesSignal)
		self.editWidget.undoAvailable.connect(self.undoAvailableChanged)
		self.editWidget.redoAvailable.connect(self.redoAvailableChanged)
		self.editWidget.copyAvailable.connect(self.copyAvailableChanged)
		self.editWidget.copyAvailable.connect(self.cutAvailableChanged)
		self.editWidget.resizeFont.connect(self.resizeFont)
		self.editWidget.validateDocument.connect(
			lambda editWidget: self.validateDocument.emit(0.0))

		self.windowStateChanged.connect(self.__handleWindowStateChange)

		self.updateTitle()

	def sizeHint(self):
		return QSize(600, 500)

	def resizeEvent(self, ev):
		EditMdiSubWindow.resizeEvent(self, ev)
		self.__emitVisibleLinesSignal()

	def __handleWindowStateChange(self, oldState, newState):
		self.__emitVisibleLinesSignal()

	def updateTitle(self):
		title = ""
		source = self.editWidget.getSource()
		if source:
			title = source.name + " (AWL)" +\
				("" if source.enabled else " (DISABLED)")
		self.setWindowTitle(title)
		self.setWindowIcon(getIcon("textsource"))

	def getSource(self):
		return self.editWidget.getSource()

	def setSource(self, source):
		self.editWidget.setSource(source)
		self.updateTitle()
		self.sourceChanged.emit()

	def importSource(self, fileName=None):
		if not fileName:
			fileName, filt = QFileDialog.getOpenFileName(self,
				"Import AWL/STL source", "",
				"AWL source (*.awl);;"
				"All files (*)")
			if not fileName:
				return False
		source = self.getSource().fromFile(name=os.path.basename(fileName),
						   filepath=fileName,
						   compatReEncode=True)
		source.forceNonFileBacked(source.name)
		self.setSource(source)
		return True

	def exportSource(self, fileName=None):
		if not fileName:
			fileName, filt = QFileDialog.getSaveFileName(self,
				"AWL/STL source export", "",
				"AWL/STL source file (*.awl)",
				"*.awl")
			if not fileName:
				return False
			if not fileName.endswith(".awl"):
				fileName += ".awl"
		try:
			safeFileWrite(fileName,
				      self.getSource().compatSourceBytes)
		except AwlSimError as e:
			MessageBox.handleAwlSimError(self,
				"Failed to export source", e)
			return False
		return True

	def undoIsAvailable(self):
		return self.editWidget.undoIsAvailable()

	def undo(self):
		self.editWidget.undo()
		return True

	def redoIsAvailable(self):
		return self.editWidget.redoIsAvailable()

	def redo(self):
		self.editWidget.redo()
		return True

	def cutIsAvailable(self):
		return self.copyIsAvailable()

	def cut(self):
		self.editWidget.cut()
		return True

	def copyIsAvailable(self):
		return self.editWidget.copyIsAvailable()

	def copy(self):
		self.editWidget.copy()
		return True

	def pasteIsAvailable(self):
		return True

	def paste(self, text=None):
		if text:
			self.editWidget.pasteText(text)
		else:
			self.editWidget.paste()
		return True

	def findTextIsAvailable(self):
		return True

	def findText(self):
		self.editWidget.findText()
		return True

	def findReplaceText(self):
		self.editWidget.findReplaceText()
		return True

	def setGuiSettings(self, guiSettings):
		self.editWidget.setSettings(guiSettings)

	def handleAwlSimError(self, exception):
		self.editWidget.handleValidationResult(exception)

	def enableOnlineDiag(self, enabled):
		self.editWidget.enableCpuStats(enabled)
		if not enabled:
			self.editWidget.resetCpuStats()
		return True

	def getVisibleLineRange(self):
		return self.editWidget.getVisibleLineRange()

	def __emitVisibleLinesSignal(self):
		fromLine, toLine = self.getVisibleLineRange()
		source = self.editWidget.getSource()
		self.visibleLinesChanged.emit(self, source, fromLine, toLine)

	def handleInsnDump(self, insnDumpMsg):
		self.editWidget.updateCpuStats_afterInsn(insnDumpMsg)

	def handleIdentsMsg(self, identsMsg):
		self.editWidget.handleIdentsMsg(identsMsg)

	def setGuiRunState(self, runState):
		if runState is not None:
			self.editWidget.runStateChanged(runState)

class FupEditMdiSubWindow(EditMdiSubWindow):
	TYPE = EditMdiSubWindow.TYPE_FUP

	def __init__(self, mdiArea, source):
		EditMdiSubWindow.__init__(self)

		def getSymTabSourcesFunc():
			projectTreeModel = self.mdiArea().getProjectTreeModel()
			project = projectTreeModel.getProject()
			return project.getSymTabSources()

		self.fupWidget = FupWidget(
			parent=self,
			getSymTabSourcesFunc=getSymTabSourcesFunc)
		self.fupWidget.setSource(source)
		self.setWidget(self.fupWidget)

		self.fupWidget.diagramChanged.connect(self.sourceChanged)
		self.fupWidget.diagramChanged.connect(self.__handleDiagramChanged)
		self.fupWidget.undoAvailableChanged.connect(self.undoAvailableChanged)
		self.fupWidget.redoAvailableChanged.connect(self.redoAvailableChanged)
		self.fupWidget.clipboardCopyAvailableChanged.connect(self.copyAvailableChanged)
		self.fupWidget.clipboardCutAvailableChanged.connect(self.cutAvailableChanged)
		self.fupWidget.clipboardPasteAvailableChanged.connect(self.pasteAvailableChanged)

		self.updateTitle()

	def sizeHint(self):
		return QSize(1000, 500)

	def updateTitle(self):
		title = ""
		source = self.fupWidget.getSource()
		if source:
			title = source.name + " (FUP)" +\
				("" if source.enabled else " (DISABLED)")
		self.setWindowTitle(title)
		self.setWindowIcon(getIcon("fup"))

	def getSource(self):
		return self.fupWidget.getSource()

	def setSource(self, source):
		self.fupWidget.setSource(source)
		self.updateTitle()
		self.sourceChanged.emit()

	def importSource(self, fileName=None):
		if not fileName:
			fileName, filt = QFileDialog.getOpenFileName(self,
				"Import FUP/FBD XML source", "",
				"FUP/FBD XML source (*.fupxml);;"
				"All files (*)")
			if not fileName:
				return False
		source = self.getSource().fromFile(name=os.path.basename(fileName),
						   filepath=fileName,
						   compatReEncode=True)
		source.forceNonFileBacked(source.name)
		self.setSource(source)
		return True

	def exportSource(self, fileName=None):
		if not fileName:
			fileName, filt = QFileDialog.getSaveFileName(self,
				"fup/fbd xml source export", "",
				"fup/fbd xml source file (*.fupxml)",
				"*.fupxml")
			if not fileName:
				return False
			if not fileName.endswith(".fupxml"):
				fileName += ".fupxml"
		try:
			safeFileWrite(fileName,
				      self.getSource().compatSourceBytes)
		except AwlSimError as e:
			MessageBox.handleAwlSimError(self,
				"Failed to export source", e)
			return False
		return True

	def undoIsAvailable(self):
		return self.fupWidget.undoIsAvailable()

	def undo(self):
		return self.fupWidget.undo()

	def redoIsAvailable(self):
		return self.fupWidget.redoIsAvailable()

	def redo(self):
		return self.fupWidget.redo()

	def cutIsAvailable(self):
		return self.fupWidget.clipboardCutIsAvailable()

	def cut(self):
		return self.fupWidget.clipboardCut()

	def copyIsAvailable(self):
		return self.fupWidget.clipboardCopyIsAvailable()

	def copy(self):
		return self.fupWidget.clipboardCopy()

	def pasteIsAvailable(self):
		return self.fupWidget.clipboardPasteIsAvailable()

	def paste(self, text=None):
		return self.fupWidget.clipboardPaste(text)

	def handleAwlSimError(self, exception):
		self.fupWidget.handleAwlSimError(exception)

	def __handleDiagramChanged(self):
		self.validateDocument.emit(0.5)

class KopEditMdiSubWindow(EditMdiSubWindow):
	TYPE = EditMdiSubWindow.TYPE_KOP

	def __init__(self, mdiArea, source):
		EditMdiSubWindow.__init__(self)

	pass#TODO

class SymTabEditMdiSubWindow(EditMdiSubWindow):
	TYPE = EditMdiSubWindow.TYPE_SYMTAB

	def __init__(self, mdiArea, source):
		EditMdiSubWindow.__init__(self)

		self.symTabView = SymTabView(self)
		self.symTabView.setSymTab(SymbolTable())
		self.symTabView.model().setSource(source)
		self.setWidget(self.symTabView)

		self.symTabView.focusChanged.connect(self.focusChanged)
		self.symTabView.model().sourceChanged.connect(self.sourceChanged)

		self.updateTitle()

	def sizeHint(self):
		return QSize(650, 400)

	def updateTitle(self):
		title = ""
		model = self.symTabView.model()
		if model:
			source = model.getSource()
			if source:
				title = source.name + " (Symbol-table)" +\
					("" if source.enabled else " (DISABLED)")
		self.setWindowTitle(title)
		self.setWindowIcon(getIcon("tag"))

	def getSource(self):
		return self.symTabView.model().getSource()

	def setSource(self, source):
		self.symTabView.model().setSource(source)
		self.updateTitle()
		self.sourceChanged.emit()

	def importSource(self, fileName=None):
		if not fileName:
			fileName, filt = QFileDialog.getOpenFileName(self,
				"Import symbol table", "",
				"Symbol table file (*.asc);;"
				"All files (*)")
			if not fileName:
				return False
		source = self.getSource().fromFile(name=os.path.basename(fileName),
						   filepath=fileName,
						   compatReEncode=True)
		source.forceNonFileBacked(source.name)
		self.setSource(source)
		return True

	def exportSource(self, fileName=None):
		if not fileName:
			fileName, filt = QFileDialog.getSaveFileName(self,
				"symbol table export", "",
				"symbol table file (*.asc)",
				"*.asc")
			if not fileName:
				return False
			if not fileName.endswith(".asc"):
				fileName += ".asc"
		try:
			safeFileWrite(fileName,
				      self.getSource().compatSourceBytes)
		except AwlSimError as e:
			MessageBox.handleAwlSimError(self,
				"Failed to export source", e)
			return False
		return True

class LibSelEditMdiSubWindow(EditMdiSubWindow):
	TYPE = EditMdiSubWindow.TYPE_LIBSEL

	def __init__(self, mdiArea, libSelections):
		EditMdiSubWindow.__init__(self)

		self.libTabView = LibTableView(model=None, parent=self)
		self.libTabView.model().setLibSelections(libSelections)
		self.setWidget(self.libTabView)

		self.libTabView.focusChanged.connect(self.focusChanged)
		self.libTabView.model().contentChanged.connect(self.sourceChanged)

		self.setWindowTitle("Library selections")
		self.setWindowIcon(getIcon("stdlib"))

	def sizeHint(self):
		return QSize(650, 300)

	def getLibSelections(self):
		return self.libTabView.model().getLibSelections()

	def setLibSelections(self, libSelections):
		self.libTabView.model().setLibSelections(libSelections)
		self.sourceChanged.emit()
bues.ch cgit interface