aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/gui/fup/fup_elemawl.py
blob: 6404660d26b97cad638db81ac002b33bf27778f1 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - FUP - Inline AWL element classes
#
# 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.fup.fup_base import *
from awlsim.gui.fup.fup_elem import *
from awlsim.gui.fup.fup_elemoperand import *

from awlsim.gui.editwidget import *


class FupElem_AWL_factory(FupElem_factory):
	def parser_open(self, tag):
		assert(tag)
		x = tag.getAttrInt("x")
		y = tag.getAttrInt("y")
		content = tag.getAttr("content", "")
		uuid = tag.getAttr("uuid", None)
		enabled = tag.getAttrBool("enabled", True)
		self.elem = FupElem_AWL(x=x, y=y,
					contentText=content,
					uuid=uuid,
					enabled=enabled)
		self.elem.grid = self.grid
		XmlFactory.parser_open(self, tag)

	def parser_beginTag(self, tag):
		XmlFactory.parser_beginTag(self, tag)

	def parser_endTag(self, tag):
		if tag.name == "element":
			# Insert the element into the grid.
			if not self.grid.placeElem(self.elem):
				raise self.Error("<element> caused "
					"a grid collision.")
			self.parser_finish()
			return
		XmlFactory.parser_endTag(self, tag)

	def composer_getTags(self):
		elem = self.elem

		return [
			self.Tag(name="element",
				attrs={
					"type" : "awl",
					"x" : str(elem.x),
					"y" : str(elem.y),
					"content" : elem.contentText,
					"uuid" : str(elem.uuid),
					"enabled" : "0" if not elem.enabled else "",
				}
			)
		]

class FupElem_AWL(FupElem):
	"""Inline-AWL FUP/FBD element"""

	factory			= FupElem_AWL_factory

	def __init__(self, x, y, contentText="", **kwargs):
		FupElem.__init__(self, x, y, **kwargs)
		self.contentText = contentText

		self._lightTextPen = QPen(QColor("#707070"))
		self._lightTextPen.setWidth(0)

	# Overridden method. For documentation see base class.
	def getAreaViaPixCoord(self, pixelX, pixelY):
		return self.AREA_BODY, 0

	# Overridden method. For documentation see base class.
	@property
	def height(self):
		return 3

	# Overridden method. For documentation see base class.
	@property
	def width(self):
		return 2

	# Overridden method. For documentation see base class.
	def draw(self, painter):
		grid = self.grid
		if not grid:
			return
		cellWidth = grid.cellPixWidth
		cellHeight = grid.cellPixHeight
		xpad, ypad = self._xpadding, self._ypadding
		elemHeight = cellHeight * self.height
		elemWidth = cellWidth * self.width
		selected = self.selected
		expanded = self.expanded and not selected

		# Draw body
		painter.setPen(self._outlineSelPen if selected
			       else self._outlinePen)
		painter.setBrush(self._bgSelBrush if selected
				 else self._bgBrush)
		(tlX, tlY), (trX, trY), (blX, blY), (brX, brY) = self._calcBodyBox()
		painter.drawRoundedRect(tlX, tlY,
					trX - tlX, blY - tlY,
					self.BODY_CORNER_RADIUS,
					self.BODY_CORNER_RADIUS)

		# Draw content text
		text = self.contentText
		if text:
			bodyRect = QRect(2 * xpad, 2 * ypad,
					 elemWidth - 4 * xpad, elemHeight - 4 * ypad)
			textRect = bodyRect
			textFlags = Qt.AlignLeft | Qt.AlignTop
			if expanded:
				textMaxRect = bodyRect.translated(0, 0)
				textMaxRect.setHeight(grid.height * cellHeight)
				textMaxRect.setWidth(grid.width * cellWidth)
				textRect = painter.boundingRect(textMaxRect, textFlags, text)
				if textRect.width() < bodyRect.width():
					textRect.setWidth(bodyRect.width())

				painter.setBrush(self._bgSelBrush if selected
						 else self._bgBrush)
				painter.setPen(self._noPen)
				painter.drawRect(textRect)
			painter.setPen(self._textPen if expanded
				       else self._lightTextPen)
			painter.setFont(self.getFont(8))
			painter.drawText(textRect, textFlags, text)

		# Draw symbol text
		if not expanded:
			painter.setPen(self._outlineSelPen if selected
				       else self._outlinePen)
			painter.setBrush(self._bgSelBrush if selected
					 else self._bgBrush)
			painter.setFont(self.getFont(16, bold=True))
			painter.drawText(0, 0,
					 elemWidth, elemHeight,
					 Qt.AlignVCenter | Qt.AlignHCenter,
					 "AWL")

		# Draw disable-marker
		self._drawDisableMarker(painter)

	# Overridden method. For documentation see base class.
	def edit(self, parentWidget):
		dlg = EditDialog(parentWidget,
				 readOnly=False, withHeader=False, withCpuStats=False,
				 okButton=True, cancelButton=True)
		dlg.setWindowTitle("Inline AWL code")
		source = AwlSource(sourceBytes=self.contentText.encode(
					AwlSource.ENCODING,
					"ignore"))
		dlg.edit.setSource(source)
		if dlg.exec_() == QDialog.Accepted:
			source = dlg.edit.getSource()
			self.contentText = source.sourceText
			return True
		return False

	# Overridden method. For documentation see base class.
	def expand(self, expand=True, area=None):
		if expand != self.expanded:
			self.expanded = expand
			return True
		return False

	# Overridden method. For documentation see base class.
	def prepareContextMenu(self, menu, area=None, conn=None):
		menu.enableEdit(True)
bues.ch cgit interface