-
Notifications
You must be signed in to change notification settings - Fork 0
/
Myscene.py
82 lines (62 loc) · 2.5 KB
/
Myscene.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
import math
from PyQt5.QtWidgets import QGraphicsScene
from PyQt5.QtGui import QColor, QPen
from PyQt5.QtCore import QLine
class GraphicScene(QGraphicsScene):
def __init__(self, parent=None):
super().__init__(parent)
# settings
self.grid_size = 20
self.grid_squares = 5
self._color_background = QColor('#393939')
self._color_light = QColor('#2f2f2f')
self._color_dark = QColor('#292929')
self._pen_light = QPen(self._color_light)
self._pen_light.setWidth(1)
self._pen_dark = QPen(self._color_dark)
self._pen_dark.setWidth(2)
self.setBackgroundBrush(self._color_background)
#self.setSceneRect(0, 0, 500, 500)
self.nodes = []
self.edges = []
def add_node(self, node):
self.nodes.append(node)
self.addItem(node)
def remove_node(self, node):
self.nodes.remove(node)
for edge in self.edges:
if edge.edge_wrap.start_item is node or edge.edge_wrap.end_item is node:
self.remove_edge(edge)
self.removeItem(node)
def add_edge(self, edge):
self.edges.append(edge)
self.addItem(edge)
def remove_edge(self, edge):
self.edges.remove(edge)
self.removeItem(edge)
def drawBackground(self, painter, rect):
super().drawBackground(painter, rect)
left = int(math.floor(rect.left()))
right = int(math.ceil(rect.right()))
top = int(math.floor(rect.top()))
bottom = int(math.ceil(rect.bottom()))
first_left = left - (left % self.grid_size)
first_top = top - (top % self.grid_size)
lines_light, lines_dark = [], []
for x in range(first_left, right, self.grid_size):
if x % (self.grid_size * self.grid_squares) != 0:
lines_light.append(QLine(x, top, x, bottom))
else:
lines_dark.append(QLine(x, top, x, bottom))
for y in range(first_top, bottom, self.grid_size):
if y % (self.grid_size * self.grid_squares) != 0:
lines_light.append(QLine(left, y, right, y))
else:
lines_dark.append(QLine(left, y, right, y))
# draw the lines
painter.setPen(self._pen_light)
if lines_light:
painter.drawLines(*lines_light)
painter.setPen(self._pen_dark)
if lines_dark:
painter.drawLines(*lines_dark)