/
render_area.py
133 lines (107 loc) · 4.81 KB
/
render_area.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
# ~*~ coding:utf-8 ~*~
from PyQt5.QtCore import QRect, QSize, QPoint, Qt
from PyQt5.QtGui import QPainter, QPalette, QPen, QPolygon, QColor
from PyQt5.QtWidgets import QWidget, QColorDialog
from sphere import Sphere
class RenderArea(QWidget):
"""
Класс области рисования
"""
def __init__(self, parent=None):
super(RenderArea, self).__init__(parent)
self.sphere = Sphere(self)
self.pen = QPen(QColor(0, 0, 0), 0)
self.faces_color = QColor(0, 255, 0)
self.is_light = False
self.is_clipping = False
self.setBackgroundRole(QPalette.Base)
self.setAutoFillBackground(True)
def minimumSizeHint(self):
return QSize(200, 200)
def sizeHint(self):
return QSize(400, 400)
def set_pen_width(self, width):
self.pen = QPen(self.pen.color(), width)
self.update()
def set_pen_color(self, label):
color = QColorDialog.getColor()
if color.isValid():
self.pen = QPen(color, self.pen.width())
label_palette = QPalette()
label_palette.setColor(QPalette.WindowText, color)
label.setPalette(label_palette)
label.setText("Цвет линии " + color.name())
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(self.pen)
# Пересчитываем сферу
self.sphere.recalculate()
# Рисуем
for face in self.sphere.geom.faces:
self.draw_item(face, painter)
# Окантовка виджета
painter.setPen(self.palette().dark().color())
painter.setBrush(Qt.NoBrush)
painter.drawRect(QRect(0, 0, self.width() - 1, self.height() - 1))
def draw_item(self, face, painter):
is_draw = True
if self.is_clipping:
is_draw = self.sphere.is_face_visible(face)
if is_draw:
polygon = QPolygon()
for index, point_index in enumerate(face):
p1_x = int(self.sphere.geom.points[face[index-1]][0])
p1_y = int(self.sphere.geom.points[face[index-1]][1])
p1_z = int(self.sphere.geom.points[face[index-1]][2])
p2_x = int(self.sphere.geom.points[point_index][0])
p2_y = int(self.sphere.geom.points[point_index][1])
p2_z = int(self.sphere.geom.points[point_index][2])
if self.sphere.projection_name == "front":
# Фронтальная проекция (вид спереди) -> z = 0
real_p1 = QPoint(p1_x, p1_y)
real_p2 = QPoint(p2_x, p2_y)
elif self.sphere.projection_name == "horizontal":
# Горизонтальная проекция (вид сверху) -> y = 0
real_p1 = QPoint(p1_x, p1_z)
real_p2 = QPoint(p2_x, p2_z)
elif self.sphere.projection_name == "profile":
# Профильная проекция (вид сбоку) -> x = 0
real_p1 = QPoint(p1_y, p1_z)
real_p2 = QPoint(p2_y, p2_z)
else:
real_p1 = QPoint(p1_x, p1_y)
real_p2 = QPoint(p2_x, p2_y)
# Точки для проволочного рисования
real_p1.setX(self.width()/2 + real_p1.x())
real_p1.setY(self.height()/2 - real_p1.y())
real_p2.setX(self.width()/2 + real_p2.x())
real_p2.setY(self.height()/2 - real_p2.y())
# Полигоны для рисования с цветом
polygon.append(real_p1)
polygon.append(real_p2)
if not self.is_light:
painter.drawLine(real_p1, real_p2)
if self.is_light:
painter.setBrush(self.sphere.get_face_light(face, self.faces_color))
painter.drawPolygon(polygon)
def set_projection(self, button):
self.sphere.projection_name = button.objectName()
self.update()
def set_clipping(self, state):
self.is_clipping = True if state == Qt.Checked else False
self.update()
def set_faces_color(self, label):
color = QColorDialog.getColor()
if color.isValid():
self.faces_color = color
label_palette = QPalette()
label_palette.setColor(QPalette.WindowText, color)
label.setPalette(label_palette)
label.setText("Цвет объекта " + color.name())
self.update()
def set_light(self, is_light, clipping_checkbox):
self.is_light = is_light
clipping_checkbox.setChecked(self.is_light)
clipping_checkbox.setDisabled(self.is_light)
self.update()