-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlnetd_l1circuit.py
235 lines (179 loc) · 7.27 KB
/
lnetd_l1circuit.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
import math
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QSize, QTimer, QPointF, QRectF, QMetaObject, QRect , QCoreApplication, QPoint, QLineF , pyqtSlot
from PyQt5.QtGui import QPainter, QBrush, QPen, QFont, QIcon, QTransform, QPalette, QColor
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QFrame,
QCheckBox,
QHBoxLayout,
QLineEdit,
QPushButton,
QMessageBox,
QFileDialog,
QSizePolicy,
QMenu,
QSlider,
QDialog,
QComboBox,
QLabel,
QSpacerItem,
QMainWindow,
QMenuBar,
QOpenGLWidget
)
from graph import Graph
from node import Node
from interface import Interface
from utilities import Vector,distance
class L1CircuitItem(QtWidgets.QGraphicsLineItem):
def __init__(self, source_node, link):
super(L1CircuitItem, self).__init__(parent=None)
self.source_node = source_node
self.link = link
self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges)
self.setCursor(Qt.ArrowCursor)
self.setAcceptHoverEvents(True)
#self.font_family: str = "Times New Roman"
self.font_family = "Roboto"
self.font_size: int = 18
self.arrow_separation = math.pi / 18
self.weight_rectangle_size = 2
def updatePosition(self):
self.prepareGeometryChange()
self.update()
def shape(self):
"""this is the selection area and colision detection"""
path = QtGui.QPainterPath()
path.addRect(self._generate_bounding_rect())
return path
def boundingRect(self):
return self.shape().boundingRect()
def itemChange(self, change, value):
result = super(L1CircuitItem, self).itemChange(change, value)
if isinstance(result, QtWidgets.QGraphicsLineItem):
result = sip.cast(result, QtWidgets.QGraphicsLineItem)
self.prepareGeometryChange()
return result
def paint(self, painter, option, widget=None):
super(L1CircuitItem, self).paint(painter, option, widget)
painter.save()
n1_p = Vector(*self.source_node.get_position())
n2_p = Vector(*self.link.target.get_position())
uv = (n2_p - n1_p).unit()
start = n1_p + uv * self.source_node.get_radius()
end = n2_p - uv * self.link.target.get_radius()
#if multiple links ( always )
if self.link.link_num % 2 == 0:
targetDistance = self.link.link_num * 2
else:
targetDistance = (-self.link.link_num +1 ) * 2
start = start.rotated_new(self.arrow_separation * targetDistance, n1_p)
end = end.rotated_new(-self.arrow_separation * targetDistance, n2_p)
if self.link._failed:
link_color = Qt.red
else:
link_color = Qt.green
painter.setPen(QPen(link_color, Qt.SolidLine))
painter.drawLine(QPointF(*start), QPointF(*end))
painter.setPen(QPen(Qt.black, Qt.SolidLine))
painter.setBrush(
QBrush(
Qt.black,
Qt.SolidPattern,
)
)
#rect calculation
r = self.weight_rectangle_size
mid = (start + end) / 2
w_len = len(str(self.link.label)) * 4
weight_v = Vector(r if w_len <= r else w_len, r)
weight_rectangle = QRectF(*(mid - weight_v), *(2 * weight_v))
if end.unit()[0] - start.unit()[0] > 0:
link_paint = QLineF(QPointF(*start), QPointF(*end))
else:
link_paint = QLineF(QPointF(*end), QPointF(*start))
center_of_rec_x = weight_rectangle.center().x()
center_of_rec_y = weight_rectangle.center().y()
painter.translate(center_of_rec_x, center_of_rec_y)
rx = -(weight_v[0] * 0.5)
ry = -(weight_v[1] )
painter.rotate(- link_paint.angle());
new_rec = QRect(rx , ry, weight_v[0], 2 * weight_v[1])
painter.drawRect(QRect(rx , ry, weight_v[0] , 2 * weight_v[1] ))
painter.setFont(QFont(self.font_family, self.font_size / 4))
painter.setPen(QPen(Qt.white, Qt.SolidLine))
painter.drawText(new_rec, Qt.AlignCenter, str(self.link.label))
painter.restore()
painter.setPen(QPen(Qt.black, Qt.SolidLine))
painter.setFont(QFont(self.font_family, self.font_size / 3))
#painter.restore()
def mousePressEvent(self, event):
self.update()
super(L1CircuitItem, self).mousePressEvent(event)
def mouseReleaseEvent(self, event):
self.update()
super(L1CircuitItem, self).mouseReleaseEvent(event)
def hoverEnterEvent(self,event):
links = self.link.interfaces
message = "<p>L3 Interfaces:</p>"
for entry in links:
message += "<li>" + str(entry) +"</li>"
self.setToolTip( message )
def hoverLeaveEvent(self,event):
pass
def contextMenuEvent(self,event):
scene = self.scene()
cmenu = QMenu()
if not self.link._failed:
fail_interface = cmenu.addAction("Circuit Down")
else:
unfail_interface = cmenu.addAction("Circuit Up")
action = cmenu.exec_(event.screenPos())
if not self.link._failed and action == fail_interface:
self.link._failed = True
if scene is not None:
scene.handleInterfaceActionDown(self, 'this is a message from Node Down GraphicsItem ')
elif self.link._failed and action == unfail_interface :
self.link._failed = False
if scene is not None:
scene.handleInterfaceActionUp(self,'this is a message from Node Up GraphicsItem')
self.update()
super(L1CircuitItem, self).contextMenuEvent(event)
def _generate_bounding_rect(self):
n1_p = Vector(*self.source_node.get_position())
n2_p = Vector(*self.link.target.get_position())
uv = (n2_p - n1_p).unit()
start = n1_p + uv * self.source_node.get_radius()
end = n2_p - uv * self.link.target.get_radius()
#if multiple links ( always )
if self.link.link_num % 2 == 0:
targetDistance = self.link.link_num * 2
else:
targetDistance = (-self.link.link_num +1 ) * 2
start = start.rotated_new(self.arrow_separation * targetDistance, n1_p)
end = end.rotated_new(-self.arrow_separation * targetDistance, n2_p)
if self.link._failed:
link_color = Qt.red
else:
link_color = Qt.green
#rect calculation
r = self.weight_rectangle_size
mid = (start + end) / 2
w_len = len(str(self.link.label)) * 4
weight_v = Vector(r if w_len <= r else w_len, r)
weight_rectangle = QRectF(*(mid - weight_v), *(2 * weight_v))
if end.unit()[0] - start.unit()[0] > 0:
link_paint = QLineF(QPointF(*start), QPointF(*end))
else:
link_paint = QLineF(QPointF(*end), QPointF(*start))
center_of_rec_x = weight_rectangle.center().x()
center_of_rec_y = weight_rectangle.center().y()
rx = -(weight_v[0] * 0.5)
ry = -(weight_v[1] )
#new_rec = QRectF(rx , ry, weight_v[0], 2 * weight_v[1])
new_rec = QRectF(center_of_rec_x - 10 ,center_of_rec_y - 10 ,15,15).normalized();
return new_rec