aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/gui/interfedit/blocktypewidget.py
blob: 7127f1d2671743d692bfdc236fb5d8eedfa3a352 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - Block type edit widget
#
# Copyright 2016-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.common.xmlfactory import *

from awlsim.gui.util import *

import re


class BlockTypeWidget_factory(XmlFactory):
	def parser_open(self, tag=None):
		self.inInstanceDBs = False
		self.inDB = False
		self.blockTypeWidget.clear(noChangeSignals=True)
		if tag:
			self.blockTypeWidget.setBlockTypeString(
				blockTypeString=tag.getAttr("type", "FC").upper().strip(),
				noChangeSignals=True)
			self.blockTypeWidget.setBlockName(
				blockName=tag.getAttr("name", "FC 1"),
				noChangeSignals=True)
		XmlFactory.parser_open(self, tag)

	def parser_close(self):
		XmlFactory.parser_close(self)

	def parser_beginTag(self, tag):
		if self.inInstanceDBs:
			if tag.name == "db":
				self.blockTypeWidget.addInstanceDB(tag.getAttr("name", ""))
				self.inDB = True
				return
		else:
			if tag.name == "instance_dbs":
				self.inInstanceDBs = True
				return
		XmlFactory.parser_beginTag(self, tag)

	def parser_endTag(self, tag):
		if self.inInstanceDBs:
			if self.inDB:
				if tag.name == "db":
					self.inDB = False
					return
			else:
				if tag.name == "instance_dbs":
					self.inInstanceDBs = False
					return
		else:
			if tag.name == "blockdecl":
				self.parser_finish()
				return
		XmlFactory.parser_endTag(self, tag)

	def composer_getTags(self):
		blockType, blockName, instanceDBs = self.blockTypeWidget.get()
		instanceDbTags = [ self.Tag(name="instance_dbs",
					    tags=[ self.Tag(name="db",
						   attrs={ "name" : str(dbName) })
						   for dbName in instanceDBs ])
		]
		return [
			self.Tag(name="blockdecl",
				 attrs={
					"type" : str(blockType).upper().strip(),
					"name" : str(blockName),
				 },
				 tags=instanceDbTags),
		]

class BlockValidator(QValidator):
	"""Validator for a block string.
	"""

	__reBase = r'\s*(BLOCKTYPE)\s*(\d+)(.*)'

	def __init__(self, lineEdit=None, blockTypes=()):
		QValidator.__init__(self)
		self.lineEdit = lineEdit
		if lineEdit:
			self.defaultBgColor = lineEdit.palette().color(QPalette.Base)
			self.defaultFgColor = lineEdit.palette().color(QPalette.Text)
		self.blockTypes = blockTypes or ()

	def doFixup(self, input):
		valid, blockStr = True, input
		regex = self.__reBase.replace(r'BLOCKTYPE',
					      r'|'.join(r'(?:' + t + r')'
							for t in self.blockTypes))
		regex = re.compile(regex, re.IGNORECASE | re.DOTALL)
		match = regex.match(blockStr)
		if match:
			if match.group(3).strip():
				valid = False # We have dangling data
			else:
				try:
					blockNr = int(match.group(2))
				except (ValueError, TypeError) as e:
					valid = False # Invalid index
				if blockNr < 0 or blockNr > 0xFFFF:
					valid = False # Invalid index
				blockStr = "%s %s" % (match.group(1).upper(),
						      match.group(2))
		else:
			blockStr = blockStr.strip()
			if blockStr:
				if len(blockStr) >= 3 and\
				   blockStr.startswith('"') and\
				   blockStr.endswith('"') and\
				   blockStr[1:-1].find('"') < 0:
					pass # The name is symbolic
				else:
					if not blockStr.startswith('"'):
						blockStr = blockStr.upper()
					valid = False # We have some crap
		return valid, blockStr

	def fixup(self, input):
		valid, fixedInput = self.doFixup(input)
		if self.lineEdit:
			self.lineEdit.setText(fixedInput)
		return fixedInput

	def validate(self, input, pos):
		valid, fixedInput = self.doFixup(input)

		if self.lineEdit:
			palette = self.lineEdit.palette()
			if valid:
				palette.setColor(QPalette.Base, self.defaultBgColor)
				palette.setColor(QPalette.Text, self.defaultFgColor)
			else:
				palette.setColor(QPalette.Base, getErrorColor())
				palette.setColor(QPalette.Text, QColor("black"))
			self.lineEdit.setPalette(palette)

		state = self.Acceptable
		if input != fixedInput:
			state = self.Intermediate
		return state, input, pos

class BlockListValidator(BlockValidator):
	"""Validator for comma separated block list strings.
	"""

	def doFixup(self, input):
		valid, retList = True, []
		for blockStr in input.split(","):
			v, fix = BlockValidator.doFixup(self, blockStr)
			if not v:
				valid = False
			if fix.strip():
				retList.append(fix)
		return valid, ", ".join(retList)

class BlockTypeWidget(QWidget):
	"""AWL block type edit widget.
	"""

	factory = BlockTypeWidget_factory

	# Signal: Emitted, if the block type changed
	typeChanged = Signal()
	# Signal: Emitted, if the block name changed
	nameChanged = Signal()
	# Signal: Emitted, if the instance DBs changed
	dbChanged = Signal()

	def __init__(self, parent=None):
		QWidget.__init__(self, parent)
		self.setLayout(QGridLayout())
		self.layout().setContentsMargins(QMargins())

		self.__changeSignalsBlocked = Blocker()

		self.typeCombo = QComboBox(self)
		self.typeCombo.addItem("Block type: Function (FC)", "FC")
		self.typeCombo.addItem("Block type: Function block (FB)", "FB")
		self.typeCombo.addItem("Block type: Organization block (OB)", "OB")
		self.layout().addWidget(self.typeCombo, 0, 0)

		self.__prevTypeStr = "FC"

		self.blockNameEdit = QLineEdit(self)
		self.blockNameEdit.setToolTip(
			"Enter the block name here.\n\n"
			"This can be an absolute block name like\n"
			"  FB 42   or   FC 42   or   OB 100\n"
			"or a symbolic block name like\n"
			"  \"My function block\"\n"
			"(The symbolic name must be present in the symbol table.)")
		self.layout().addWidget(self.blockNameEdit, 0, 1)

		self.dbEditLabel = QLabel("DIs:", self)
		self.layout().addWidget(self.dbEditLabel, 0, 2)

		self.dbEdit = QLineEdit(self)
		self.dbEdit.setToolTip(
			"Enter the instance DBs to create here.\n\n"
			"If this field is left empty, no DB will be generated.\n"
			"One DB or a comma separated list of DBs to create can be specified.\n\n"
			"For example:\n"
			"  DB 42\n"
			"or\n"
			"  DB 42, DB 43, DB 44\n"
			"or a symbolic block name like\n"
			"  \"First data block\", \"Second data block\"\n"
			"(The symbolic name must be present in the symbol table.)")
		self.dbEditLabel.setToolTip(self.dbEdit.toolTip())
		self.dbEdit.setValidator(BlockListValidator(self.dbEdit,
							    ("DB", "DI")))
		self.layout().addWidget(self.dbEdit, 0, 3)

		self.layout().setColumnStretch(99, 1)

		self.clear(noChangeSignals=True)

		self.__handleTypeChange(self.typeCombo.currentIndex())
		self.typeCombo.currentIndexChanged.connect(self.__handleTypeChange)
		self.blockNameEdit.textChanged.connect(self.__handleNameChange)
		self.dbEdit.textChanged.connect(self.__handleDBChange)

	def __handleTypeChange(self, comboIndex):
		typeStr = self.typeCombo.itemData(comboIndex)
		self.blockNameEdit.setValidator(BlockValidator(self.blockNameEdit,
							       (typeStr,)))
		if typeStr == "FB":
			self.dbEditLabel.show()
			self.dbEdit.show()
		else:
			self.dbEditLabel.hide()
			self.dbEdit.hide()
		if not self.__changeSignalsBlocked:
			self.typeChanged.emit()
		self.__prevTypeStr = typeStr

	def revertTypeChange(self):
		"""Revert type change. Can only be called from typeChanged signal.
		"""
		index = self.typeCombo.findData(self.__prevTypeStr,
						Qt.UserRole, Qt.MatchFixedString)
		if index >= 0:
			self.typeCombo.setCurrentIndex(index)

	def __handleNameChange(self, text):
		if not self.__changeSignalsBlocked:
			self.nameChanged.emit()

	def __handleDBChange(self, text):
		if not self.__changeSignalsBlocked:
			self.dbChanged.emit()

	def get(self):
		"""Get (blockTypeString, blockName, instanceDBs)
		"""
		typeStr = self.typeCombo.itemData(self.typeCombo.currentIndex())
		nameStr = self.blockNameEdit.text()
		instanceDBs = []
		if typeStr == "FB":
			for db in self.dbEdit.text().split(","):
				instanceDBs.append(db.strip())
		return (typeStr, nameStr, instanceDBs)

	def clear(self, noChangeSignals=False):
		"""Clear content.
		"""
		with self.__changeSignalsBlocked if noChangeSignals else contextlib.nullcontext():
			self.typeCombo.setCurrentIndex(0)
			self.blockNameEdit.setText("FC 1")
			self.dbEdit.clear()

	def setBlockTypeString(self, blockTypeString, noChangeSignals=False):
		"""Set the block type.
		"""
		with self.__changeSignalsBlocked if noChangeSignals else contextlib.nullcontext():
			index = self.typeCombo.findData(blockTypeString,
							Qt.UserRole, Qt.MatchFixedString)
			if index >= 0:
				self.typeCombo.setCurrentIndex(index)
				return True
			return False

	def setBlockName(self, blockName, noChangeSignals=False):
		"""Set the block name.
		"""
		with self.__changeSignalsBlocked if noChangeSignals else contextlib.nullcontext():
			self.blockNameEdit.setText(blockName)

	def addInstanceDB(self, dbName, noChangeSignals=False):
		"""Add an instance DB.
		"""
		if not dbName.strip():
			return False
		with self.__changeSignalsBlocked if noChangeSignals else contextlib.nullcontext():
			typeStr = self.typeCombo.itemData(self.typeCombo.currentIndex())
			if typeStr == "FB":
				if self.dbEdit.text().strip():
					self.dbEdit.setText(self.dbEdit.text() + ", " + dbName)
				else:
					self.dbEdit.setText(dbName)
				return True
			return False
bues.ch cgit interface