-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruction.py
More file actions
257 lines (204 loc) · 9.75 KB
/
instruction.py
File metadata and controls
257 lines (204 loc) · 9.75 KB
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
# Copyright 2018 <Quenos Blockchain R&D KFT>
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
from enum import Enum
from PyQt5 import QtCore, QtWidgets, QtGui
from abc import ABC, abstractmethod
class Location(Enum):
BOTTOM = 0
RIGHT = 1
TOP = 2
LEFT = 3
class Instruction(ABC):
_cp_width = 0
_cp_height = 0
MARGIN = 10
MIN_RECT_WIDTH = 155
def __init__(self, pen=None):
if pen:
self._pen = pen
else:
self._pen = None
def paint(self, widget, painter):
if self._pen:
painter.setPen(self._pen)
@abstractmethod
def reset(self):
# set all variables etc to their initial state
Instruction._cp_width = 0
Instruction._cp_height = 0
Instruction.MARGIN = 10
Instruction.MIN_RECT_WIDTH = 155
class LineInstruction(Instruction):
def __init__(self, line, pen=None):
super().__init__(pen)
self._line = line
def paint(self, widget, painter):
super().paint(widget, painter)
painter.drawLine(self._line)
def reset(self):
super().reset()
class RectInstruction(Instruction):
def __init__(self, rect, pen=None):
super().__init__(pen)
self._rect = rect
def paint(self, widget, painter):
super().paint(widget, painter)
painter.drawRect(self._rect)
def reset(self):
super().reset()
class NodeInstruction(Instruction):
_offset_x = Instruction.MARGIN
_offset_y = Instruction.MARGIN
_location = Location.BOTTOM
def __init__(self, node, pen=None, chan_id=0):
super().__init__(pen)
self._window_position = None
self.chan_id = chan_id
self._node = node
def _set_window_position(self, pos_x, pos_y, width, height):
self._window_position = QtCore.QRect(pos_x, pos_y, width, height)
def get_window_position(self):
return self._window_position
def paint(self, widget, painter):
super().paint(widget, painter)
# if the index of the node is 1 (center node == 0)
# the offsets need to be reset so that the node is drawn at the bottom left corner of the window
if self._node.get_node_index() == 1:
NodeInstruction._offset_x = 10 * Instruction.MARGIN
NodeInstruction._offset_y = Instruction.MARGIN
NodeInstruction._location = Location.BOTTOM
# calculate the width and height of the node rectangle based on the length and height of the node name
font_metrics = QtGui.QFontMetrics(widget.font())
text_width = font_metrics.width(self._node.text) + 5
text_height = font_metrics.height()
rect_width = text_width + Instruction.MARGIN
rect_height = text_height + Instruction.MARGIN / 2
# decide whether the node can still be drawn at the current side of the window
# if not then adjust the offsets to the next empty part of the window
if NodeInstruction._location == Location.BOTTOM:
if NodeInstruction._offset_x > widget.width() - rect_width - Instruction.MARGIN:
# the node would be drawn outside the window
NodeInstruction._location = Location.RIGHT
NodeInstruction._offset_x = widget.width() - Instruction.MIN_RECT_WIDTH - Instruction.MARGIN
NodeInstruction._offset_y = 2 * rect_height
elif NodeInstruction._location == Location.RIGHT:
if NodeInstruction._offset_y > widget.height() - 3 * rect_height:
# the node would be drawn outside the window
NodeInstruction._location = Location.LEFT
NodeInstruction._offset_x = Instruction.MARGIN
NodeInstruction._offset_y = 2 * rect_height
elif NodeInstruction._location == Location.LEFT:
if NodeInstruction._offset_y > widget.height() - 4 * rect_height:
# the node would be drawn outside the window
NodeInstruction._location = Location.TOP
NodeInstruction._offset_x = 10 * Instruction.MARGIN
NodeInstruction._offset_y = widget.height() - 2 * rect_height
# set the left upper corner of the node rectangle
rect_lu_x = NodeInstruction._offset_x
rect_lu_y = widget.height() - NodeInstruction._offset_y - rect_height - Instruction.MARGIN
# Safe and draw the node rectangle and write the name of the node in it
self._set_window_position(rect_lu_x, rect_lu_y, rect_width, rect_height)
painter.drawRect(rect_lu_x, rect_lu_y, rect_width, rect_height)
painter.drawText(rect_lu_x + 8, rect_lu_y + text_height, self._node.text)
# calculate the beginning and end point of the edge from the node to center node
# this depends on the side of the window where the node is drawn
# the bottom nodes connect from the top of the node to bottom of the center node
# the right nodes connect from the left side of the node to the right side of the center node
# the top nodes connect from the bottom of the node to the top of the center node
# and the left nodes connect from the right of the node to the left of the center node
if NodeInstruction._location == Location.BOTTOM:
line_start_x = rect_lu_x + rect_width / 2
line_start_y = rect_lu_y
line_end_x = widget.width() / 2
line_end_y = widget.height() / 2 + Instruction._cp_height / 2
elif NodeInstruction._location == Location.RIGHT:
line_start_x = rect_lu_x
line_start_y = rect_lu_y + rect_height / 2
line_end_x = widget.width() / 2 + Instruction._cp_width / 2
line_end_y = widget.height() / 2
elif NodeInstruction._location == Location.LEFT:
line_start_x = rect_lu_x + rect_width
line_start_y = rect_lu_y + rect_height / 2
line_end_x = widget.width() / 2 - Instruction._cp_width / 2
line_end_y = widget.height() / 2
elif NodeInstruction._location == Location.TOP:
line_start_x = rect_lu_x + rect_width / 2
line_start_y = rect_lu_y + rect_height
line_end_x = widget.width() / 2
line_end_y = widget.height() / 2 - Instruction._cp_height / 2
else:
raise ValueError
# draw the edge
painter.drawLine(line_start_x, line_start_y, line_end_x, line_end_y)
# calculate the starting offsets of the next node
if NodeInstruction._location == Location.BOTTOM or NodeInstruction._location == Location.TOP:
NodeInstruction._offset_x = rect_lu_x + rect_width + Instruction.MARGIN
elif NodeInstruction._location == Location.RIGHT or NodeInstruction._location == Location.LEFT:
NodeInstruction._offset_y += rect_height + Instruction.MARGIN
else:
raise ValueError
def reset(self):
super().reset()
NodeInstruction._offset_x = Instruction.MARGIN
NodeInstruction._offset_y = Instruction.MARGIN
NodeInstruction._location = Location.BOTTOM
class CenterNodeInstruction(Instruction):
def __init__(self, center_node, pen=None):
super().__init__(pen)
self._center_node = center_node
def paint(self, widget, painter):
super().paint(widget, painter)
font_metrics = QtGui.QFontMetrics(widget.font())
text_width = font_metrics.width(self._center_node.text)
text_height = font_metrics.height()
rect_width = text_width + Instruction.MARGIN
rect_height = text_height + Instruction.MARGIN
Instruction._cp_width = rect_width
Instruction._cp_height = rect_height
rect_lu_x = widget.width() / 2 - rect_width / 2
rect_lu_y = widget.height() / 2 - rect_height / 2
painter.drawRect(rect_lu_x, rect_lu_y, rect_width, rect_height)
painter.drawText(rect_lu_x + 10, rect_lu_y + text_height, self._center_node.text)
def reset(self):
super().reset()
class TextInstruction(Instruction):
def __init__(self, text, pen=None):
super().__init__(pen)
self._text = text
def paint(self, widget, painter):
super().paint(widget, painter)
painter.drawText(self._text)
def reset(self):
super().reset()
class ChannelGraphPicture(object):
instructions = []
@staticmethod
def reset():
for instruction in ChannelGraphPicture.instructions:
instruction.reset()
Node.reset()
ChannelGraphPicture.instructions = []
class Node(object):
_index = 0
def __init__(self, text):
self.text = text
self._index = Node._index
Node._index += 1
def get_node_index(self):
return self._index
@staticmethod
def reset():
Node._index = 0
class CenterNode(Node):
def __init__(self, text):
super().__init__(text)