summaryrefslogtreecommitdiffstats
path: root/awlsim/gui/library.py
blob: 6960f55c8978495b973749ac23c280b2b0317845 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - GUI standard library window
#
# Copyright 2014-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.util import *
from awlsim.gui.icons import *

from awlsim.core.systemblocks.system_sfc import *
from awlsim.core.systemblocks.system_sfb import *
from awlsim.core.systemblocks.tables import SFC_table, SFB_table

from awlsim.library.libentry import *
from awlsim.library.libselection import *


class GenericActionWidget(QWidget):
	# Signal: Code paste request.
	paste = Signal(str)
	# Signal: Add a symbol to the symbol table
	# Arguments: symbolName, address, dataType, comment
	addSymbol = Signal(str, str, str, str)
	# Signal: Add library selection
	addLibrary = Signal(AwlLibEntrySelection)
	# Signal: Finish the library selection
	finish = Signal()

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

	def _pasteCallGeneric(self, targetName, needDB, interfaceFields):
		fields = []
		for ftype in (BlockInterfaceField.FTYPE_IN,
			      BlockInterfaceField.FTYPE_OUT,
			      BlockInterfaceField.FTYPE_INOUT):
			with contextlib.suppress(KeyError):
				fields.extend(interfaceFields[ftype])
		ret = [ "CALL %s%s%s" %\
			(targetName,
			 ", DB ..." if needDB else "",
			 " (" if fields else "")
		]
		for field in fields:
			ret.append("\t%s := ...\t, // %s" %\
				   (field.name, str(field.dataType)))
		if fields:
			ret.append(")")
		ret.append("")
		self.paste.emit("\n".join(ret))

	def _blockToInterfaceText(self, blockIdent, blockSym,
				  verboseText,
				  interfaceFields):
		desc = [ "%s \"%s\"" % (blockIdent, blockSym) ]
		if verboseText:
			desc.append(verboseText)
		desc.append("")
		for ftype, fname in ((BlockInterfaceField.FTYPE_IN, "VAR_INPUT"),
				     (BlockInterfaceField.FTYPE_OUT, "VAR_OUTPUT"),
				     (BlockInterfaceField.FTYPE_INOUT, "VAR_IN_OUT")):
			try:
				fields = interfaceFields[ftype]
			except KeyError:
				continue
			if not fields:
				continue
			desc.append("  " + fname)
			for field in fields:
				field.fieldType = ftype
				desc.append("    %s" % field.varDeclString())
		return "\n".join(desc)

	def defaultPaste(self):
		pass

class SysActionWidget(GenericActionWidget):
	def __init__(self, parent=None):
		GenericActionWidget.__init__(self, parent)

		self.systemBlockCls = None
		self.blockPrefix = None

		self.desc = QLabel(self)
		self.desc.setFont(getDefaultFixedFont())
		self.layout().addWidget(self.desc, 0, 0, 1, 2)

		self.layout().setRowStretch(1, 1)

		label = QLabel("Paste at cursor position:", self)
		self.layout().addWidget(label, 2, 0, 1, 2)

		self.pasteCallSymButton = QPushButton(self)
		self.layout().addWidget(self.pasteCallSymButton, 3, 0)

		self.pasteCallButton = QPushButton(self)
		self.layout().addWidget(self.pasteCallButton, 3, 1)

		self.pasteCallSymButton.released.connect(self.__pasteCallSym)
		self.pasteCallButton.released.connect(self.__pasteCall)

	def updateData(self, prefix, systemBlockCls):
		self.systemBlockCls = systemBlockCls
		self.blockPrefix = prefix

		blockNumber, symbolName, blockDesc = systemBlockCls.name
		desc = self._blockToInterfaceText("%s %d" % (prefix, blockNumber),
						  symbolName, blockDesc,
						  systemBlockCls.interfaceFields)
		self.desc.setText(desc)
		self.pasteCallButton.setText("CALL %s %d" %\
					     (prefix, blockNumber))
		self.pasteCallSymButton.setText("CALL \"%s\"" % symbolName)

	def __pasteCall(self):
		blockNumber, symbolName, blockDesc = self.systemBlockCls.name
		self._pasteCallGeneric("%s %d" % (self.blockPrefix, blockNumber),
				       self.systemBlockCls._isFB,
				       self.systemBlockCls.interfaceFields)
		self.finish.emit()

	def __pasteCallSym(self):
		blockNumber, symbolName, blockDesc = self.systemBlockCls.name
		self._pasteCallGeneric('"%s"' % symbolName,
				       self.systemBlockCls._isFB,
				       self.systemBlockCls.interfaceFields)
		self.addSymbol.emit(symbolName,
				    "%s %s" % (self.blockPrefix, blockNumber),
				    "%s %s" % (self.blockPrefix, blockNumber),
				    blockDesc)
		self.finish.emit()

	def defaultPaste(self):
		self.__pasteCallSym()

class LibActionWidget(GenericActionWidget):
	def __init__(self, parent=None):
		GenericActionWidget.__init__(self, parent)

		self.libEntryCls = None

		self.desc = QLabel(self)
		self.desc.setFont(getDefaultFixedFont())
		self.layout().addWidget(self.desc, 0, 0, 1, 2)

		self.layout().setRowStretch(1, 1)

		label = QLabel("Paste at cursor position:", self)
		self.layout().addWidget(label, 2, 0, 1, 2)

		self.pasteCallSymButton = QPushButton(self)
		self.layout().addWidget(self.pasteCallSymButton, 3, 0)

		self.pasteCallButton = QPushButton(self)
		self.layout().addWidget(self.pasteCallButton, 3, 1)

		self.pasteCodeSymButton = QPushButton(self)
		self.layout().addWidget(self.pasteCodeSymButton, 4, 0)

		self.pasteCodeButton = QPushButton(self)
		self.layout().addWidget(self.pasteCodeButton, 4, 1)

		self.pasteCallSymButton.released.connect(self.__pasteCallSym)
		self.pasteCallButton.released.connect(self.__pasteCall)
		self.pasteCodeSymButton.released.connect(self.__pasteCodeSym)
		self.pasteCodeButton.released.connect(self.__pasteCode)

	def updateData(self, libEntryCls):
		self.libEntryCls = libEntryCls

		prefix = "FC" if libEntryCls._isFC else "FB"
		typeStr = "FUNCTION" if libEntryCls._isFC else "FUNCTION_BLOCK"
		self.blockName = "%s %d" % (prefix, libEntryCls.staticIndex)

		desc = self._blockToInterfaceText(self.blockName,
						  libEntryCls.symbolName,
						  libEntryCls.description,
						  libEntryCls.interfaceFields)
		self.desc.setText(desc)

		self.pasteCodeButton.setText("%s %s" %\
					     (typeStr, self.blockName))
		self.pasteCodeSymButton.setText("%s \"%s\"" %\
						(typeStr, libEntryCls.symbolName))
		self.pasteCallButton.setText("CALL %s" % self.blockName)
		self.pasteCallSymButton.setText("CALL \"%s\"" %\
						libEntryCls.symbolName)

	def __pasteCodeWarning(self):
		res = QMessageBox.warning(self,
			"Paste library code body?",
			"Warning: It is not recommended to paste library "
			"code into the project sources. You should instead "
			"just import the library (via library selection table) "
			"and CALL the imported function.\n\n"
			"See the 'CALL \"%s\"' or 'CALL %s' buttons.\n\n"
			"Do you want to paste the code nevertheless?" % (
			self.libEntryCls.symbolName, self.blockName),
			QMessageBox.Yes | QMessageBox.No,
			QMessageBox.No)
		return res == QMessageBox.Yes

	def __pasteCode(self):
		if not self.__pasteCodeWarning():
			return
		self.paste.emit(self.libEntryCls().getCode(False))
		self.finish.emit()

	def __pasteCodeSym(self):
		if not self.__pasteCodeWarning():
			return
		self.paste.emit(self.libEntryCls().getCode(True))
		self.addSymbol.emit(self.libEntryCls.symbolName,
				    self.blockName,
				    self.blockName,
				    self.libEntryCls.description)
		self.finish.emit()

	def __pasteCall(self):
		self._pasteCallGeneric(self.blockName,
				       self.libEntryCls._isFB,
				       self.libEntryCls.interfaceFields)
		self.addLibrary.emit(self.libEntryCls().makeSelection())
		self.finish.emit()

	def __pasteCallSym(self):
		self._pasteCallGeneric('"%s"' % self.libEntryCls.symbolName,
				       self.libEntryCls._isFB,
				       self.libEntryCls.interfaceFields)
		self.addSymbol.emit(self.libEntryCls.symbolName,
				    self.blockName,
				    self.blockName,
				    self.libEntryCls.description)
		self.addLibrary.emit(self.libEntryCls().makeSelection())
		self.finish.emit()

	def defaultPaste(self):
		self.__pasteCallSym()

class LibraryDialog(QDialog):
	ITEM_SFC	= QListWidgetItem.UserType + 0
	ITEM_SFB	= QListWidgetItem.UserType + 1
	ITEM_LIB_BASE	= QListWidgetItem.UserType + 100

	BLOCK_OFFSET	= QListWidgetItem.UserType + 0xFFFF

	def __init__(self, project, parent=None):
		QDialog.__init__(self, parent)
		self.setLayout(QGridLayout())
		self.setWindowTitle("AWL/STL - Standard library")
		self.setWindowIcon(getIcon("stdlib"))

		self.project = project

		self.pasteText = None
		self.pasteSymbol = None
		self.pasteLibSel = None

		self.currentActionWidget = None
		self.__nr2lib = {}
		self.__nr2entry = {}

		self.libList = QListWidget(self)
		QListWidgetItem("System functions (SFC)", self.libList, self.ITEM_SFC)
		QListWidgetItem("System function blocks (SFB)", self.libList, self.ITEM_SFB)
		for i, libName in enumerate(("IEC",)):
			try:
				lib = AwlLib.getByName(libName)
			except AwlSimError as e:
				MessageBox.handleAwlSimError(self, "Library error", e)
				continue
			self.__nr2lib[self.ITEM_LIB_BASE + i] = lib
			QListWidgetItem(lib.description, self.libList,
					self.ITEM_LIB_BASE + i)
		self.layout().addWidget(self.libList, 0, 0, 3, 1)

		self.libElemList = QListWidget(self)
		self.libElemList.setFont(getDefaultFixedFont())
		self.layout().addWidget(self.libElemList, 0, 1, 3, 1)

		self.iconLabel = QLabel(self)
		self.iconLabel.setAlignment(Qt.AlignHCenter | Qt.AlignTop)
		self.iconLabel.setPixmap(getIcon("stdlib").pixmap(QSize(64, 64)))
		self.layout().addWidget(self.iconLabel, 0, 2)

		self.sysAction = SysActionWidget(self)
		self.sysAction.hide()
		self.layout().addWidget(self.sysAction, 1, 2)

		self.libAction = LibActionWidget(self)
		self.libAction.hide()
		self.layout().addWidget(self.libAction, 2, 2)

		self.libList.currentItemChanged.connect(self.__libItemChanged)
		self.libElemList.currentItemChanged.connect(self.__libElemItemChanged)
		self.libElemList.itemDoubleClicked.connect(self.__libElemDoubleClicked)
		for actionWidget in (self.sysAction, self.libAction):
			actionWidget.paste.connect(self.__actionPaste)
			actionWidget.addSymbol.connect(self.__actionAddSym)
			actionWidget.addLibrary.connect(self.__actionAddLib)
			actionWidget.finish.connect(self.accept)

		self.libList.setCurrentRow(0)

		self.libList.setMinimumWidth(190)
		self.libElemList.setMinimumWidth(350)
		self.iconLabel.setMinimumWidth(400)
		self.sysAction.setMinimumWidth(self.iconLabel.minimumWidth())
		self.libAction.setMinimumWidth(self.iconLabel.minimumWidth())
		self.resize(self.size().width(), 360)

	def __addLibElemList(self, entries):
		# Figure out the padding.
		maxA = maxB = ""
		for number, textA, textB, textC in entries:
			if len(textA) > len(maxA):
				maxA = textA
			if len(textB) > len(maxB):
				maxB = textB
		# Pad the entries and add them to the widget.
		for number, textA, textB, textC in entries:
			textA += " " * (len(maxA) - len(textA))
			textB += " " * (len(maxB) - len(textB))
			QListWidgetItem("%s  %s%s" % (textA, textB, textC),
					self.libElemList,
					number)

	def __addSystemBlockTable(self, prefix, table):
		entries = []
		for blockCls in sorted(dictValues(table), key=lambda c: c.name[0]):
			if blockCls.broken:
				continue
			number, name, desc = blockCls.name
			conf = self.project.getCpuConf()
			if number < 0 and not conf.extInsnsEn:
				continue

			absName = "%s %d" % (prefix, number)
			symName = '"%s"' % name
			if desc:
				desc = "  (%s)" % desc
			else:
				desc = ""
			entries.append((number + self.BLOCK_OFFSET,
					absName, symName, desc))
		self.__addLibElemList(entries)

	def __addLibraryTable(self, lib):
		entries = []
		for i, libCls in enumerate(sorted(lib.entries(),
						  key=lambda c: c.staticIndex)):
			if libCls.broken:
				continue
			absName = "%s %d" % ("FC" if libCls._isFC else "FB",\
					     libCls.staticIndex)
			symName = '"%s"' % libCls.symbolName
			if libCls.description:
				desc = "  (%s)" % libCls.description
			else:
				desc = ""
			entries.append((i + self.BLOCK_OFFSET,
					absName, symName, desc))
			self.__nr2entry[i + self.BLOCK_OFFSET] = libCls
		self.__addLibElemList(entries)

	def __hideAllActionWidgets(self):
		self.currentActionWidget = None
		self.sysAction.hide()
		self.libAction.hide()

	def __libItemChanged(self, item, prevItem):
		self.__hideAllActionWidgets()
		self.libElemList.clear()
		if item.type() == self.ITEM_SFC:
			self.__addSystemBlockTable("SFC", SFC_table)
		elif item.type() == self.ITEM_SFB:
			self.__addSystemBlockTable("SFB", SFB_table)
		elif item.type() >= self.ITEM_LIB_BASE:
			lib = self.__nr2lib[item.type()]
			self.__addLibraryTable(lib)
		else:
			assert(0)

	def __libElemItemChanged(self, item, prevItem):
		if not item:
			self.__hideAllActionWidgets()
			return
		libType = self.libList.currentItem().type()
		if libType in (self.ITEM_SFC, self.ITEM_SFB):
			blockNum = item.type() - self.BLOCK_OFFSET
			self.sysAction.show()
			self.currentActionWidget = self.sysAction
			if libType == self.ITEM_SFC:
				self.sysAction.updateData("SFC", SFC_table[blockNum])
			else:
				self.sysAction.updateData("SFB", SFB_table[blockNum])
		elif libType >= self.ITEM_LIB_BASE:
			entryCls = self.__nr2entry[item.type()]
			self.libAction.show()
			self.currentActionWidget = self.libAction
			self.libAction.updateData(entryCls)
		else:
			assert(0)

	def __libElemDoubleClicked(self, item):
		if self.currentActionWidget:
			self.currentActionWidget.defaultPaste()

	def __actionPaste(self, text):
		assert(self.pasteText is None)
		self.pasteText = text

	def __actionAddSym(self, symbolName, address, dataType, comment):
		assert(self.pasteSymbol is None)
		self.pasteSymbol = (symbolName, address, dataType, comment)

	def __actionAddLib(self, libSelection):
		assert(self.pasteLibSel is None)
		self.pasteLibSel = libSelection
bues.ch cgit interface