aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/gui/util.py
blob: 0c67251f19f390ca73d5eef072e7555420b95cc2 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - GUI utility functions
#
# Copyright 2012-2020 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

import sys
from awlsim_loader.common import *

if isPy2Compat:
	printError("ERROR: Python 2.x is not supported by awlsim-gui. "
		   "Please use Python 3.")
	sys.exit(1)

if isPyPy or isJython:
	printError("ERROR: Running awlsim-gui on the PyPy or Jython interpreter is not supported. "
		   "Please use Python 3.")
	sys.exit(1)

from awlsim_loader.coreclient import *
import awlsim_loader.cython_helper as cython_helper
from awlsim.common.monotonic import monotonic_time #+cimport
from awlsim.common.datatypehelpers import * #+cimport
from awlsim.common.profiler import *
from awlsim.core.datatypes import AwlDataType
from awlsim.core.symbolparser import SymbolTable, Symbol, SymTabParser

import traceback
import xml.sax.saxutils as saxutils

if cython_helper.shouldUseCython():
	print("*** Using accelerated CYTHON core "
	      "(AWLSIM_CYTHON environment variable is set)")

from awlsim.gui.qt_bindings import *


AWLSIM_HOME_DOMAIN = "bues.ch"
AWLSIM_HOME_SUB = "/a/awlsim"
AWLSIM_HOME_URL = "https://" + AWLSIM_HOME_DOMAIN + AWLSIM_HOME_SUB


# Convert an integer to a dual-string
def intToDualString(value, bitWidth):
	string = []
	for bitnr in range(bitWidth - 1, -1, -1):
		string.append('1' if ((value >> bitnr) & 1) else '0')
		if bitnr and bitnr % 4 == 0:
			string.append('_')
	return ''.join(string)

# Get the default fixed font
def getDefaultFixedFont(pointSize=11, bold=False):
	font = QFont()
	font.setStyleHint(QFont.Courier)
	font.setFamily("Courier")
	font.setPointSize(pointSize)
	font.setWeight(QFont.Normal)
	font.setBold(bold)
	return font

def sleepWithEventLoop(seconds, excludeInput=True):
	"""Sleep for 'seconds', but run the Qt event loop to refresh the GUI.
	excludeInput: Do not process user input events.
	"""
	end = monotonic_time() + seconds
	eventFlags = QEventLoop.AllEvents
	if excludeInput:
		eventFlags |= QEventLoop.ExcludeUserInputEvents
	while monotonic_time() < end:
		QApplication.processEvents(eventFlags, 10)
		QThread.msleep(10)

# Color used for errors
def getErrorColor():
	return QColor("#FFC0C0")

def handleFatalException(parentWidget=None):
	text = str(traceback.format_exc())
	print("Fatal exception:\n", text)
	text = saxutils.escape(text)
	QMessageBox.critical(parentWidget,
		"A fatal exception occurred",
		"<pre>"
		"A fatal exception occurred:\n\n"
		"%s\n\n"
		"Awlsim will be terminated."
		"</pre>" % text)
	sys.exit(1)


class MessageBox(QDialog):
	def __init__(self,
		     parent,
		     title,
		     text,
		     verboseText=None,
		     icon=QMessageBox.Critical,
		     okButton=True,
		     continueButton=False,
		     cancelButton=False):
		QDialog.__init__(self, parent)
		self.setLayout(QGridLayout())
		self.setWindowTitle(title)

		self.text = "<pre>" + saxutils.escape(text) + "\n</pre>"
		self.verboseText = None
		if verboseText and verboseText.strip() != text.strip():
			self.verboseText = "<pre>" + saxutils.escape(verboseText) + "\n</pre>"

		self.textBox = QLabel(self)
		self.textBox.setTextInteractionFlags(Qt.TextSelectableByMouse |\
						     Qt.TextSelectableByKeyboard |\
						     Qt.LinksAccessibleByMouse |\
						     Qt.LinksAccessibleByKeyboard)
		self.layout().addWidget(self.textBox, 0, 0, 1, 3)

		if self.verboseText:
			self.verboseCheckBox = QCheckBox("Show verbose information", self)
			self.layout().addWidget(self.verboseCheckBox, 1, 0, 1, 3)
		else:
			self.verboseCheckBox = None

		buttonsLayout = QHBoxLayout()
		if okButton:
			self.okButton = QPushButton("&Ok", self)
			buttonsLayout.addWidget(self.okButton)
		if continueButton:
			self.continueButton = QPushButton("C&ontinue", self)
			buttonsLayout.addWidget(self.continueButton)
		if cancelButton:
			self.cancelButton = QPushButton("&Cancel", self)
			buttonsLayout.addWidget(self.cancelButton)
		self.layout().addLayout(buttonsLayout, 2, 1)

		self.__updateText()

		if okButton:
			self.okButton.released.connect(self.accept)
		if continueButton:
			self.continueButton.released.connect(self.accept)
		if cancelButton:
			self.cancelButton.released.connect(self.reject)
		if self.verboseCheckBox:
			self.verboseCheckBox.stateChanged.connect(self.__updateText)

	def __updateText(self):
		if self.verboseCheckBox and\
		   self.verboseCheckBox.checkState() == Qt.Checked:
			self.textBox.setText(self.verboseText)
		else:
			self.textBox.setText(self.text)

	@classmethod
	def error(cls, parent, text, verboseText=None, **kwargs):
		dlg = cls(parent=parent,
			  title="Awlsim - Error",
			  text=text,
			  verboseText=verboseText,
			  icon=QMessageBox.Critical,
			  **kwargs)
		res = dlg.exec_()
		dlg.deleteLater()
		return res

	@classmethod
	def warning(cls, parent, text, verboseText=None, **kwargs):
		dlg = cls(parent=parent,
			  title="Awlsim - Warning",
			  text=text,
			  verboseText=verboseText,
			  icon=QMessageBox.Warning,
			  **kwargs)
		res = dlg.exec_()
		dlg.deleteLater()
		return res

	awlSimErrorBlocked = Blocker()

	@classmethod
	def handleAwlSimError(cls, parent, description, exception, **kwargs):
		if exception.getSeenByUser() or cls.awlSimErrorBlocked:
			return cls.Accepted
		exception.setSeenByUser()
		def maketext(verbose):
			text = "An exception occurred:"
			if description:
				text += "\n"
				text += "  " + description + "."
			text += "\n\n"
			text += exception.getReport(verbose)
			return text
		return cls.error(parent=parent,
				 text=maketext(False),
				 verboseText=maketext(True),
				 **kwargs)

	@classmethod
	def handleAwlParserError(cls, parent, exception, **kwargs):
		return cls.handleAwlSimError(parent=parent,
					     description=None,
					     exception=exception,
					     **kwargs)
bues.ch cgit interface