-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
lnetd_link.py
387 lines (326 loc) · 12.1 KB
/
lnetd_link.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
import math
from bisect import *
from PyQt5 import QtCore, QtGui, QtWidgets
from support import generate_path_config
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
import configparser
file_path = generate_path_config()
config = configparser.ConfigParser()
config.read(file_path)
blue_threshold = config.get("threshold", "blue_threshold")
green_threshold = config.get("threshold", "green_threshold")
yellow_threshold = config.get("threshold", "yellow_threshold")
orange_threshold = config.get("threshold", "orange_threshold")
class Link(QtWidgets.QGraphicsLineItem):
def __init__(self, source_node, link):
super(Link, self).__init__(parent=None)
self.source_node = source_node
self.link = link
self.show_latency = False
self.link_width = 2.5
# fix me here , set. self threshold for blue and then in paint create the dict
self.blue_threshold = int(blue_threshold)
self.green_threshold = int(green_threshold)
self.yellow_threshold = int(yellow_threshold)
self.orange_threshold = int(orange_threshold)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges)
self.setAcceptHoverEvents(True)
# self.font_family: str = "Times New Roman"
self.font_family = "Roboto"
self.font_size: int = 18
self.show_context = True
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(Link, 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(Link, self).paint(painter, option, widget)
util = self.link.utilization()
orangeColor = QColor(255, 165, 0)
colour_values = {
self.blue_threshold: QtCore.Qt.blue,
self.green_threshold: QtCore.Qt.green,
self.yellow_threshold: QtCore.Qt.yellow,
self.orange_threshold: orangeColor,
}
colour_values_list = sorted(colour_values)
link_color_index = bisect_left(colour_values_list, util)
if self.link._failed:
link_color = QtCore.Qt.red
elif util == 0:
link_color = QtCore.Qt.black
elif util > int(orange_threshold):
link_color = QtCore.Qt.magenta
else:
try:
link_color_key = colour_values_list[link_color_index]
link_color = colour_values[link_color_key]
# print(util, link_color, link_color_index, link_color_key)
except:
# guard agains <100% thresholds value
link_color = QtCore.Qt.magenta
painter.save()
painter.setFont(QFont(self.font_family, self.font_size / 3))
if self.link.link_num % 2 == 0:
targetDistance = self.link.link_num * 5
else:
targetDistance = (-self.link.link_num + 1) * 5
# hours of calculation and still can't figure out where it's wrong
n1_p = Vector(*self.source_node.get_position())
n2_p = Vector(*self.link.target.get_position())
x1_x0 = n2_p[0] - n1_p[0]
y1_y0 = n2_p[1] - n1_p[1]
if y1_y0 == 0:
x2_x0 = 0
y2_y0 = targetDistance
else:
angle = math.atan((x1_x0) / (y1_y0))
x2_x0 = -targetDistance * math.cos(angle)
y2_y0 = targetDistance * math.sin(angle)
d0x = n1_p[0] + (1 * x2_x0)
d0y = n1_p[1] + (1 * y2_y0)
d1x = n2_p[0] + (1 * x2_x0)
d1y = n2_p[1] + (1 * y2_y0)
dx = (d1x - d0x,)
dy = (d1y - d0y,)
dr = math.sqrt(dx[0] * dx[0] + dy[0] * dy[0])
endX = (d1x + d0x) / 2
endY = (d1y + d0y) / 2
len1 = dr - ((dr / 2) * math.sqrt(3))
endX = endX + (len1 / dr)
endY = endY + (len1 / dr)
n1_p = Vector(d0x, d0y)
n2_p = Vector(endX, endY)
uv = (n2_p - n1_p).unit()
d = distance(n1_p, n2_p)
r = self.link.target.get_radius()
arrow_head_pos = n2_p
d = distance(n1_p, arrow_head_pos)
uv_arrow = (arrow_head_pos - n1_p).unit()
arrow_base_pos = n1_p + uv_arrow * (d - 2 * 2)
nv_arrow = uv_arrow.rotated(math.pi / 2)
painter.setRenderHints(
QtGui.QPainter.Antialiasing
| QtGui.QPainter.TextAntialiasing
| QtGui.QPainter.SmoothPixmapTransform
| QtGui.QPainter.HighQualityAntialiasing,
True,
)
if self.link.highlight:
painter.setPen(QPen(link_color, 3, 3))
elif option.state & QtWidgets.QStyle.State_Selected:
painter.setPen(QPen(link_color, 2, 4))
else:
painter.setPen(QPen(link_color, self.link_width ,Qt.SolidLine))
painter.setBrush(QBrush(link_color, Qt.SolidPattern))
painter.drawPolygon(
QPointF(*arrow_head_pos),
QPointF(*(arrow_base_pos + nv_arrow * 2)),
QPointF(*(arrow_base_pos - nv_arrow * 2)),
)
painter.drawLine(QPointF(d0x, d0y), QPointF(endX, endY))
painter.setPen(QtGui.QPen(link_color, 1))
# text
if endX - d0x > 0:
link_paint = QLineF(QPointF(d0x, d0y), QPointF(endX, endY))
else:
link_paint = QLineF(QPointF(endX, endY), QPointF(d0x, d0y))
mid = (arrow_base_pos + n1_p) / 2
if self.show_latency:
w_len = (
len(str(self.link.metric) + str(self.link.latency) + "------") / 3 * r
+ r / 3
)
else:
w_len = len(str(self.link.metric)) / 3 * r + r / 3
weight_v = Vector(w_len, 2)
weight_rectangle = QRectF(*(mid - weight_v), *(2 * weight_v))
painter.save()
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])
if self.link._failed:
painter.setBrush(QBrush(Qt.red, Qt.SolidPattern))
elif option.state & QtWidgets.QStyle.State_Selected:
pass
painter.setBrush(QBrush(Qt.black, Qt.SolidPattern))
painter.setPen(QtGui.QPen(Qt.black, Qt.SolidLine))
painter.drawRect(QRect(rx, ry, weight_v[0], 2 * weight_v[1]))
painter.setFont(QFont(self.font_family, self.font_size / 3.3))
painter.setPen(QPen(Qt.white, Qt.SolidLine))
if self.show_latency:
painter.drawText(
new_rec,
Qt.AlignCenter,
str(self.link.metric) + " -- " + str(self.link.latency) + "/ms",
)
else:
painter.drawText(new_rec, Qt.AlignCenter, str(self.link.metric))
painter.restore()
painter.restore()
def mousePressEvent(self, event):
self.update()
super(Link, self).mousePressEvent(event)
def mouseReleaseEvent(self, event):
self.update()
super(Link, self).mouseReleaseEvent(event)
def hoverEnterEvent(self, event):
util = str(self.link.utilization())
self.setToolTip("Load:" + util + "%")
def hoverLeaveEvent(self, event):
pass
def contextMenuEvent(self, event):
if not self.show_context:
return
scene = self.scene()
cmenu = QMenu()
if not self.link._failed:
fail_interface = cmenu.addAction("Interface Down")
else:
unfail_interface = cmenu.addAction("Interface Up")
change_metric = cmenu.addAction("Edit Interface ")
delete_interface = cmenu.addAction("Delete Interface ")
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"
)
elif action == change_metric:
if scene is not None:
scene.handleInterfaceActionChange(
self, "this is a message from Node Down GraphicsItem "
)
elif action == delete_interface:
if scene is not None:
scene.handleInterfaceActionDelete(
self, "this is a message from Node Down GraphicsItem "
)
self.update()
super(Link, self).contextMenuEvent(event)
def _generate_bounding_rect(self):
if self.link.link_num % 2 == 0:
targetDistance = self.link.link_num * 5
else:
targetDistance = (-self.link.link_num + 1) * 5
# hours of calculation and still can't figure out where it's wrong
n1_p = Vector(*self.source_node.get_position())
n2_p = Vector(*self.link.target.get_position())
x1_x0 = n2_p[0] - n1_p[0]
y1_y0 = n2_p[1] - n1_p[1]
if y1_y0 == 0:
x2_x0 = 0
y2_y0 = targetDistance
else:
angle = math.atan((x1_x0) / (y1_y0))
x2_x0 = -targetDistance * math.cos(angle)
y2_y0 = targetDistance * math.sin(angle)
d0x = n1_p[0] + (1 * x2_x0)
d0y = n1_p[1] + (1 * y2_y0)
d1x = n2_p[0] + (1 * x2_x0)
d1y = n2_p[1] + (1 * y2_y0)
dx = (d1x - d0x,)
dy = (d1y - d0y,)
dr = math.sqrt(dx[0] * dx[0] + dy[0] * dy[0])
endX = (d1x + d0x) / 2
endY = (d1y + d0y) / 2
len1 = dr - ((dr / 2) * math.sqrt(3))
endX = endX + (len1 / dr)
endY = endY + (len1 / dr)
n1_p = Vector(d0x, d0y)
n2_p = Vector(endX, endY)
uv = (n2_p - n1_p).unit()
d = distance(n1_p, n2_p)
r = self.link.target.get_radius()
arrow_head_pos = n2_p
d = distance(n1_p, arrow_head_pos)
uv_arrow = (arrow_head_pos - n1_p).unit()
arrow_base_pos = n1_p + uv_arrow * (d - 2 * 2)
nv_arrow = uv_arrow.rotated(math.pi / 2)
# text
if endX - d0x > 0:
link_paint = QLineF(QPointF(d0x, d0y), QPointF(endX, endY))
else:
link_paint = QLineF(QPointF(endX, endY), QPointF(d0x, d0y))
mid = (arrow_base_pos + n1_p) / 2
w_len = len(str(self.link.metric)) / 3 * r + r / 3
weight_v = Vector(w_len, 2)
weight_rectangle = QRectF(*(mid - weight_v), *(2 * weight_v))
center_of_rec_x = weight_rectangle.center().x()
center_of_rec_y = weight_rectangle.center().y()
new_rec = QRectF(
center_of_rec_x - 10, center_of_rec_y - 10, 20, 20
).normalized()
return new_rec