/
edge.py
462 lines (374 loc) · 17.7 KB
/
edge.py
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Widget to display simulation data of a CSDF graph.
author: Sander Giesselink
"""
from PyQt5.QtWidgets import QGraphicsItem, QInputDialog, QMessageBox
from PyQt5.QtCore import Qt, QRectF, QPointF
from PyQt5.QtGui import QColor, QPen, QBrush, QPainterPath, QFont
from log import Log
class Edge(QGraphicsItem):
def __init__(self, beginPoint, endPoint, beginSide, endSide, edgeSelfLoops, pRates, cRates, color):
super().__init__()
self.edgeSelfLoops = edgeSelfLoops
self.penWidth = 4
self.beginSide = beginSide
self.endSide = endSide
self.beginPoint = beginPoint
self.endPoint = endPoint
self.calculateCurvePoints(beginPoint, endPoint)
self.cRates = cRates
self.pRates = pRates
self.updatePCRects()
self.calculateEdgeColors(color)
#self.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable)
self.setAcceptHoverEvents(True)
self.hover = False
self.debugOn = False
def boundingRect(self):
#Used for collision detection and repaint
path = self.getEdgePath()
path.setFillRule(Qt.WindingFill)
path.addRect(self.cRect)
path.addRect(self.pRect)
path.addPath(self.getLargerEdgePath())
return path.boundingRect()
def shape(self):
#Determines the collision area
path = self.getEdgePath()
path.setFillRule(Qt.WindingFill)
path.addRect(self.cRect)
path.addRect(self.pRect)
path.addPath(self.getLargerEdgePath())
return path
def paint(self, painter, option, widget):
lod = option.levelOfDetailFromTransform(painter.worldTransform())
if lod > 0.05:
self.paintEdge(painter, lod)
if lod > 0.15 and self.debugOn:
self.debug(painter) #Uncomment to turn on debug mode
def paintEdge(self, painter, lod):
pen = QPen(Qt.black)
pen.setWidth(1)
pen.setCapStyle(Qt.RoundCap)
brush = QBrush(self.edgeColor)
if self.hover:
#pen.setColor(self.edgeColorHover)
brush.setColor(self.edgeColorHover)
if QGraphicsItem.isSelected(self):
#pen.setColor(self.edgeColorSelected)
brush.setColor(self.edgeColorSelected)
painter.setPen(pen)
painter.setBrush(brush)
edgePath = self.getEdgePath()
edgePath = edgePath.simplified()
painter.drawPath(edgePath)
if lod > 0.4:
self.drawPCRates(painter)
def drawPCRates(self, painter):
#Draw production and consumption rates above begin and end of edge
pen = QPen(QColor(0, 0, 100))
painter.setPen(pen)
painter.setFont(QFont("Arial", 8))
#Only display rates if larger than 1 or more than 1, and max 1 rate at the time
#Build string for production rate
pPhase = self.tokenCluster.getFireCount('src') % len(self.pRates)
if len(self.pRates) == 1:
if self.pRates == [1]:
pRateStr = ''
else:
pRateStr = str(self.pRates[0])
elif pPhase == 0:
pRateStr = str(self.pRates[pPhase]) + ',..'
elif pPhase == len(self.pRates) - 1:
pRateStr = '..,' + str(self.pRates[pPhase])
else:
pRateStr = '..,' + str(self.pRates[pPhase]) + ',..'
#Same for consumption string
cPhase = self.tokenCluster.getFireCount('dst') % len(self.cRates)
if len(self.cRates) == 1:
if self.cRates == [1]:
cRateStr = ''
else:
cRateStr = str(self.cRates[0])
elif cPhase == 0:
cRateStr = str(self.cRates[cPhase]) + ',..'
elif cPhase == len(self.cRates) - 1:
cRateStr = '..,' + str(self.cRates[cPhase])
else:
cRateStr = '..,' + str(self.cRates[cPhase]) + ',..'
if len(self.pRates) > 1 or self.pRates[0] > 1:
painter.drawText(self.pRect, Qt.AlignCenter, pRateStr)
if len(self.cRates) > 1 or self.cRates[0] > 1:
painter.drawText(self.cRect, Qt.AlignCenter, cRateStr)
def getEdgePath(self):
yTranslation = 2
#Curve 1
beginPoint = QPointF(self.beginPoint.x(), self.beginPoint.y() + yTranslation)
curvePoint1 = QPointF(self.curvePoint1.x(), self.curvePoint1.y() + yTranslation)
curvePoint2 = QPointF(self.curvePoint2.x(), self.curvePoint2.y() + yTranslation)
endPoint = QPointF(self.endPoint.x(), self.endPoint.y() + yTranslation)
path = QPainterPath(beginPoint)
point1 = QPointF(curvePoint1.x(), curvePoint1.y())
point2 = QPointF(curvePoint2.x(), curvePoint2.y())
path.cubicTo(point1, point2, endPoint)
#Arrow
arrowBeginPoint = QPointF(self.endPoint.x(), self.endPoint.y() + 4)
path.lineTo(arrowBeginPoint)
if self.endSide == 'right':
path.lineTo(QPointF(self.endPoint.x() - 10, self.endPoint.y()))
else:
path.lineTo(QPointF(self.endPoint.x() + 10, self.endPoint.y()))
path.lineTo(QPointF(self.endPoint.x(), self.endPoint.y() - 4))
path.lineTo(QPointF(self.endPoint.x(), self.endPoint.y() - 2))
#Curve 2 (back)
endPoint = QPointF(self.beginPoint.x(), self.beginPoint.y() - yTranslation)
curvePoint2 = QPointF(self.curvePoint1.x(), self.curvePoint1.y() - yTranslation)
curvePoint1 = QPointF(self.curvePoint2.x(), self.curvePoint2.y() - yTranslation)
beginPoint = QPointF(self.endPoint.x(), self.endPoint.y() - yTranslation)
point1 = QPointF(curvePoint1.x(), curvePoint1.y())
point2 = QPointF(curvePoint2.x(), curvePoint2.y())
path.cubicTo(point1, point2, endPoint)
if self.beginSide == 'right':
path.lineTo(QPointF(self.beginPoint.x() - 10, self.beginPoint.y() - 2))
path.lineTo(QPointF(self.beginPoint.x() - 10, self.beginPoint.y() + 2))
else:
path.lineTo(QPointF(self.beginPoint.x() + 10, self.beginPoint.y() - 2))
path.lineTo(QPointF(self.beginPoint.x() + 10, self.beginPoint.y() + 2))
path.lineTo(QPointF(self.beginPoint.x(), self.beginPoint.y() + 2))
return path
def getLargerEdgePath(self):
#Used to fill in the small areas on the edge
#This makes it easier to select the edge
yTranslation = 2
#Curve 1
beginPoint = QPointF(self.beginPoint.x(), self.beginPoint.y() + yTranslation)
curvePoint1 = QPointF(self.curvePoint1.x()+4, self.curvePoint1.y() + yTranslation)
curvePoint2 = QPointF(self.curvePoint2.x()+4, self.curvePoint2.y() + yTranslation)
endPoint = QPointF(self.endPoint.x(), self.endPoint.y() + yTranslation)
path = QPainterPath(beginPoint)
point1 = QPointF(curvePoint1.x(), curvePoint1.y())
point2 = QPointF(curvePoint2.x(), curvePoint2.y())
path.cubicTo(point1, point2, endPoint)
#Arrow
arrowBeginPoint = QPointF(self.endPoint.x(), self.endPoint.y() + 4)
path.lineTo(arrowBeginPoint)
if self.endSide == 'right':
path.lineTo(QPointF(self.endPoint.x() - 10, self.endPoint.y()))
else:
path.lineTo(QPointF(self.endPoint.x() + 10, self.endPoint.y()))
path.lineTo(QPointF(self.endPoint.x(), self.endPoint.y() - 4))
path.lineTo(QPointF(self.endPoint.x(), self.endPoint.y() - 2))
#Curve 2 (back)
endPoint = QPointF(self.beginPoint.x(), self.beginPoint.y() - yTranslation)
curvePoint2 = QPointF(self.curvePoint1.x(), self.curvePoint1.y() - yTranslation)
curvePoint1 = QPointF(self.curvePoint2.x(), self.curvePoint2.y() - yTranslation)
beginPoint = QPointF(self.endPoint.x(), self.endPoint.y() - yTranslation)
point1 = QPointF(curvePoint1.x(), curvePoint1.y())
point2 = QPointF(curvePoint2.x(), curvePoint2.y())
path.cubicTo(point1, point2, endPoint)
if self.beginSide == 'right':
path.lineTo(QPointF(self.beginPoint.x() - 10, self.beginPoint.y() - 2))
path.lineTo(QPointF(self.beginPoint.x() - 10, self.beginPoint.y() + 2))
else:
path.lineTo(QPointF(self.beginPoint.x() + 10, self.beginPoint.y() - 2))
path.lineTo(QPointF(self.beginPoint.x() + 10, self.beginPoint.y() + 2))
path.lineTo(QPointF(self.beginPoint.x(), self.beginPoint.y() + 2))
return path
def debug(self, painter):
#Paint path
painter.setBrush(QBrush(QColor(0, 0, 0, 25)))
pen = QPen(QColor(255, 0, 0, 100))
pen.setWidth(1)
painter.setPen(pen)
#Curve area
path = QPainterPath()
path.addPath(self.shape())
painter.drawPath(path)
#Curve controll points
painter.drawEllipse(self.curvePoint1, 2, 2)
painter.drawEllipse(self.curvePoint2, 2, 2)
#Draw area
painter.setPen(QPen(QColor(0, 255, 0, 100)))
painter.setBrush(QBrush(QColor(0, 0, 0, 15)))
path2 = QPainterPath()
rect = self.boundingRect()
path2.addRect(rect)
painter.drawPath(path2)
#Middel point
painter.setPen(QPen(QColor(0, 0, 255, 100)))
painter.drawEllipse(self.midPoint, 2, 2)
def calculateCurvePoints(self, beginPoint, endPoint):
x = (beginPoint.x() + endPoint.x()) / 2
y = (beginPoint.y() + endPoint.y()) / 2
#Calculate the point in the middle of beginPoint and endPoint
self.midPoint = QPointF(x, y)
xPoint1 = self.midPoint.x()
xPoint2 = self.midPoint.x()
#If beginPoint and endPoint are the same, move the endPoint by 0.01
if self.beginPoint == self.endPoint:
if self.beginSide == 'left':
self.endPoint = QPointF(self.endPoint.x() + 0.01, self.endPoint.y())
else:
self.endPoint = QPointF(self.endPoint.x() - 0.01, self.endPoint.y())
self.calculateCurvePoints(self.beginPoint, self.endPoint)
#Calculate curvePoints based on the position of the nodes
self.xDiff = abs(self.beginPoint.x() - self.endPoint.x())
if self.xDiff < 400:
self.xDiff = 400
self.xDiff = self.xDiff / 4
#Adjust curve to the different combinations of the node locations
if self.beginSide == 'right':
if self.endSide == 'left':
if abs(self.beginPoint.y() - self.endPoint.y()) < 25:
#When the nodes are too close
xPoint1 = self.beginPoint.x()
xPoint2 = self.endPoint.x()
else:
xPoint1 = self.beginPoint.x() + self.xDiff
xPoint2 = self.endPoint.x() - self.xDiff
else:
xPoint1 = self.beginPoint.x() + self.xDiff
xPoint2 = self.endPoint.x() + self.xDiff
else:
if self.endSide == 'right':
if abs(self.beginPoint.y() - self.endPoint.y()) < 25:
#When the nodes are too close
xPoint1 = self.beginPoint.x()
xPoint2 = self.endPoint.x()
else:
xPoint1 = self.beginPoint.x() - self.xDiff
xPoint2 = self.endPoint.x() + self.xDiff
else:
xPoint1 = self.beginPoint.x() - self.xDiff
xPoint2 = self.endPoint.x() - self.xDiff
#Add a y translation to the curve points when the edge loops to the same node
#or otherwise crosses straight over itself
self.yTranslation = 0
if self.beginSide == self.endSide:
#The edge always crosses nodes on the same y level when the IO are on opposite sides
if abs(self.beginPoint.y() - self.endPoint.y()) < 35:
self.yTranslation = -45
else:
#When the IO is on different sides but the nodes are switched around
if abs(self.beginPoint.y() - self.endPoint.y()) < 55:
if self.beginSide == 'right':
if self.beginPoint.x() > self.endPoint.x():
self.yTranslation = -45
xPoint1 = self.beginPoint.x() + 100
xPoint2 = self.endPoint.x() - 100
else:
if self.beginPoint.x() < self.endPoint.x():
self.yTranslation = -45
xPoint1 = self.beginPoint.x() - 100
xPoint2 = self.endPoint.x() + 100
#Adjust for selflooping
if self.edgeSelfLoops:
if self.beginSide != self.endSide:
self.yTranslation = -45
if self.beginSide == 'right':
xPoint1 = self.beginPoint.x() + 100
xPoint2 = self.endPoint.x() - 100
else:
xPoint1 = self.beginPoint.x() - 100
xPoint2 = self.endPoint.x() + 100
#Add curvePoints
self.curvePoint1 = QPointF(xPoint1, self.beginPoint.y() + self.yTranslation)
self.curvePoint2 = QPointF(xPoint2, self.endPoint.y() + self.yTranslation)
def hoverEnterEvent(self, event):
self.hover = True
self.setCursor(Qt.PointingHandCursor)
super().hoverEnterEvent(event)
self.update()
def hoverLeaveEvent(self, event):
self.hover = False
self.setCursor(Qt.ArrowCursor)
super().hoverLeaveEvent(event)
self.update()
def mousePressEvent(self, event):
if event.button() == Qt.RightButton:
self.tokenCluster.contextMenu(event.scenePos())
super().mousePressEvent(event)
self.update()
def moveEdge(self, delta, edgeSide):
if edgeSide == 'begin':
self.beginPoint += delta
else:
self.endPoint += delta
#Update curve
self.calculateCurvePoints(self.beginPoint, self.endPoint)
#Update tokens on the edge
self.tokenCluster.updateTokens()
#Update P & C rate rects
self.updatePCRects()
#Prepare the painter for a geometry change, so it repaints correctly
self.prepareGeometryChange()
self.update()
def setZValueEdge(self, zValue):
self.setZValue(zValue)
self.tokenCluster.setZValueTokenCluster(zValue + 1)
def getPointOnEdge(self, t):
edgePath = QPainterPath(self.beginPoint)
edgePath.cubicTo(self.curvePoint1, self.curvePoint2, self.endPoint)
return QPointF(edgePath.pointAtPercent(t))
def getPointCloseToCenter(self, distance):
edgePath = QPainterPath(self.beginPoint)
edgePath.cubicTo(self.curvePoint1, self.curvePoint2, self.endPoint)
if edgePath.length() > 0:
percent = (edgePath.length() / 2 + distance) / edgePath.length()
else:
percent = 0
#Snap to begin/end point when the edge is too small
if percent < 0:
percent = 0
elif percent > 1:
percent = 1
return self.getPointOnEdge(percent)
def setTokenCluster(self, tokenCluster):
self.tokenCluster = tokenCluster
def updatePCRects(self):
if self.beginSide == 'left':
self.pRect = QRectF(self.beginPoint.x() - 20, self.beginPoint.y() - 15, 20, 10)
else:
self.pRect = QRectF(self.beginPoint.x(), self.beginPoint.y() - 15, 20, 10)
if self.endSide == 'left':
self.cRect = QRectF(self.endPoint.x() - 20, self.endPoint.y() - 15, 20, 10)
else:
self.cRect = QRectF(self.endPoint.x(), self.endPoint.y() - 15, 20, 10)
def setPRatesActiontriggered(self):
pRatesStr = str(self.pRates)
newPRatesStr, ok = QInputDialog.getText(self.tokenCluster.widget, 'Edit production rates', 'Production rate:', text = pRatesStr)
if ok:
try:
newPRates = eval(newPRatesStr)
self.pRates = newPRates
self.tokenCluster.widget.editPRates(self.tokenCluster.src, self.tokenCluster.dst, newPRates)
except:
Log.addLogMessage(Log.ERROR, 'Production rate not valid.')
QMessageBox.critical(self.tokenCluster.widget, 'Error', 'Production rate not valid.')
def setCRatesActiontriggered(self):
cRatesStr = str(self.cRates)
newCRatesStr, ok = QInputDialog.getText(self.tokenCluster.widget, 'Edit consumption rates', 'Consumption rate:', text = cRatesStr)
if ok:
try:
newCRates = eval(newCRatesStr)
self.cRates = newCRates
self.tokenCluster.widget.editCRates(self.tokenCluster.src, self.tokenCluster.dst, newCRates)
except:
Log.addLogMessage(Log.ERROR, 'Consumption rate not valid.')
QMessageBox.critical(self.tokenCluster.widget, 'Error', 'Consumption rate not valid.')
def calculateEdgeColors(self, color):
#Calculate all edge colors based on a given color
r = color.red()
g = color.green()
b = color.blue()
if r < 80:
r = 80
if g < 80:
g = 80
if b < 80:
b = 80
self.edgeColor = QColor(r, g, b)
self.edgeColorSelected = Qt.black
self.edgeColorHover = QColor(r - 80, g - 80, b - 80)