-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.py
280 lines (227 loc) · 11.9 KB
/
window.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
"""Create window and widgets, the starting point for executing Shapeshift."""
# Standard library imports
import random
import sys
# Third-party imports
from PySide6 import QtCore, QtGui, QtWidgets
from sympy import Rational
# Local imports
import figures
from graphics import ModernGLWidget
from polytope import Polyhedron
class AxisSlider(QtWidgets.QSlider):
"""Slider for rotation in a specific xyz dimension."""
def __init__(self):
super().__init__(QtCore.Qt.Horizontal)
self.setValue(random.randint(0, 100))
class FractionSlider(QtWidgets.QSlider):
"""Slider for determining at what fraction each edge is truncated."""
def __init__(self):
super().__init__(QtCore.Qt.Horizontal)
self.setValue(4)
self.setMinimum(1)
self.setMaximum(6)
def get_value(self):
"""Return value as a fraction."""
return Rational(self.value(), 6)
class OperationButton(QtWidgets.QPushButton):
"""Buttons for performing operations on polytopes."""
def __init__(self, name):
super().__init__(name)
self.setGeometry(200, 150, 100, 40)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Shapeshift")
self.setGeometry(100, 100, 830, 550)
self.polytope_models = [Model(figures.octahedron)]
# self.setStyleSheet("""
# background-color: #262626;
# color: #FFFFFF;
# """)
@property
def polytope(self):
return self.polytope_models[-1].polytope
def setupUI(self):
"""Set up layout of widgets."""
# Toolbar
menubar = QtWidgets.QMenuBar()
file_menu = menubar.addMenu("File")
file_menu.addAction(open_action := QtGui.QAction("Open", self))
file_menu.addAction(export_action := QtGui.QAction("Export", self))
edit_menu = menubar.addMenu("Edit")
edit_menu.addAction(undo_action := QtGui.QAction("Undo", self))
edit_menu.addAction(redo_action := QtGui.QAction("Redo", self))
view_menu = menubar.addMenu("View")
view_menu.addAction(tetrahedron_action := QtGui.QAction("Tetrahedron", self))
view_menu.addAction(cube_action := QtGui.QAction("Cube", self))
view_menu.addAction(octahedron_action := QtGui.QAction("Octahedron", self))
view_menu.addAction(dodecahedron_action := QtGui.QAction("Dodecahedron", self))
view_menu.addAction(icosahedron_action := QtGui.QAction("Icosahedron", self))
modify_menu = menubar.addMenu("Modify")
modify_menu.addAction(clear_polyhedra_action := QtGui.QAction("Clear polyhedra", self))
details_menu = menubar.addMenu("Details")
details_menu.addAction(element_values_action := QtGui.QAction("Element values", self))
details_menu.addAction(element_counts_action := QtGui.QAction("Element counts", self))
details_menu.addAction(face_types_action := QtGui.QAction("Face types", self))
self.setMenuBar(menubar)
# Operations tab
self.tab_1 = QtWidgets.QWidget()
self.tab_1_layout = QtWidgets.QGridLayout()
self.tab_1_layout.setAlignment(QtCore.Qt.AlignTop)
self.tab_1_layout.addWidget(uniform_truncate := OperationButton("Uniform"), 1, 1)
self.tab_1_layout.addWidget(rectify := OperationButton("Rectify"), 1, 2)
self.tab_1_layout.addWidget(dual := OperationButton("Dual"), 1, 3)
self.tab_1_layout.addWidget(truncate := OperationButton("Truncate"), 2, 1, 1, 3)
self.tab_1_layout.addWidget(truncate_fraction_slider := FractionSlider(), 3, 1, 1, 3)
self.tab_1_layout.addWidget(truncate_fraction_label := QtWidgets.QLabel(str(truncate_fraction_slider.get_value())), 2, 3)
self.tab_1_layout.addWidget(cap := OperationButton("Cap"), 4, 1)
self.tab_1_layout.addWidget(bridge := OperationButton("Bridge"), 4, 2)
self.tab_1_layout.addWidget(dual2 := OperationButton("Dual"), 4, 3)
self.tab_1_layout.addWidget(augment := OperationButton("Augment"), 5, 1, 1, 3)
self.tab_1_layout.addWidget(augment_fraction_slider := FractionSlider(), 6, 1, 1, 3)
self.tab_1_layout.addWidget(augment_fraction_label := QtWidgets.QLabel(str(augment_fraction_slider.get_value())), 5, 3)
#truncate_fraction_label.setAlignment(QtCore.Qt.AlignRight)
#self.tab_1_layout.addWidget(midpoint_radio := QtWidgets.QRadioButton())
self.tab_1.setLayout(self.tab_1_layout)
self.tab_2 = QtWidgets.QWidget()
self.tab_2_layout = QtWidgets.QGridLayout()
self.tab_2_layout.setAlignment(QtCore.Qt.AlignTop)
self.tab_2_layout.addWidget(decompose := OperationButton("Decompose"), 1, 1)
self.tab_2_layout.addWidget(uncouple := OperationButton("Uncouple"), 1, 2)
self.tab_2.setLayout(self.tab_2_layout)
self.tab_3 = QtWidgets.QWidget()
self.tab_3_layout = QtWidgets.QGridLayout()
self.tab_3_layout.setAlignment(QtCore.Qt.AlignTop)
self.tab_3_layout.addWidget(facet := OperationButton("Facet"), 1, 1)
self.tab_3_layout.addWidget(stellate := OperationButton("Stellate"), 1, 2)
self.tab_3_layout.addWidget(prismate := OperationButton("Prismate"), 1, 3)
self.tab_3.setLayout(self.tab_3_layout)
# Create tab widget
self.tab_widget = QtWidgets.QTabWidget()
self.tab_widget.addTab(self.tab_1, "Truncate")
self.tab_widget.addTab(self.tab_2, "Separate")
self.tab_widget.addTab(self.tab_3, "Other")
# Create descriptive text box
self.descriptive_textbox = QtWidgets.QLabel("")
self.descriptive_textbox.setWordWrap(True)
# Polyhedra rendering options
self.show_edges_only = QtWidgets.QCheckBox("Show edges only", self)
self.show_prior_polyhedra_checkbox = QtWidgets.QCheckBox("Show prior polyhedra", self)
self.midpoint_checkbox = QtWidgets.QCheckBox("Determine midpoint from midsphere", self)
self.midpoint_checkbox.setChecked(True)
# Create left vertical box
self.left_vbox = QtWidgets.QVBoxLayout()
self.left_vbox.addWidget(self.tab_widget)
self.left_vbox.addWidget(self.descriptive_textbox)
self.left_vbox.addWidget(self.show_edges_only)
self.left_vbox.addWidget(self.show_prior_polyhedra_checkbox)
self.left_vbox.addWidget(self.midpoint_checkbox)
self.left_widget = QtWidgets.QWidget()
self.left_widget.setLayout(self.left_vbox)
self.left_widget.setMaximumWidth(330)
# Create horizontal box layout
self.hbox = QtWidgets.QHBoxLayout()
self.hbox.addWidget(self.left_widget)
# Create vertical box for QOpenGLWidget and QSliders
self.vbox = QtWidgets.QVBoxLayout()
self.vbox.addWidget(moderngl_widget := ModernGLWidget(self))
self.x_slider = AxisSlider()
self.vbox.addWidget(self.x_slider)
self.y_slider = AxisSlider()
self.vbox.addWidget(self.y_slider)
self.z_slider = AxisSlider()
self.vbox.addWidget(self.z_slider)
self.moderngl_and_sliders = QtWidgets.QWidget()
self.moderngl_and_sliders.setLayout(self.vbox)
self.hbox.addWidget(self.moderngl_and_sliders)
# Add zoom slider
self.zoom_slider = QtWidgets.QSlider()
self.hbox.addWidget(self.zoom_slider)
self.zoom_slider.setValue(10)
# Create list of created polyhedra
self.right_vbox = QtWidgets.QVBoxLayout()
self.right_widget = QtWidgets.QWidget()
self.right_widget.setLayout(self.right_vbox)
self.hbox.addWidget(self.right_widget)
# Create central widget
self.central_widget = QtWidgets.QWidget()
self.central_widget.setLayout(self.hbox)
self.setCentralWidget(self.central_widget)
# Connect operations to buttons/actions
truncate.clicked.connect(lambda: self.operations(Polyhedron.truncate, fraction=truncate_fraction_slider.get_value(), method="by_midsphere" if self.midpoint_checkbox.isChecked() else "by_midpoint"))
rectify.clicked.connect(lambda: self.operations(Polyhedron.rectify))
facet.clicked.connect(lambda: self.operations(Polyhedron.facet))
dual.clicked.connect(lambda: self.operations(Polyhedron.reciprocate))
cap.clicked.connect(lambda: self.operations(Polyhedron.cap))
bridge.clicked.connect(lambda: self.operations(Polyhedron.bridge))
stellate.clicked.connect(lambda: self.operations(Polyhedron.stellate))
decompose.clicked.connect(lambda: self.operations(Polyhedron.decompose))
uncouple.clicked.connect(lambda: self.operations(Polyhedron.uncouple))
def truncation_fraction_changed(qlabel: QtWidgets.QLabel, qslider: FractionSlider, qbutton: OperationButton):
value = qslider.get_value()
qlabel.setText(str(value))
# if value == 1:
# qbutton.setText("Rectify")
# else:
# qbutton.setText("Truncate")
truncate_fraction_slider.valueChanged.connect(lambda: truncation_fraction_changed(truncate_fraction_label, truncate_fraction_slider, truncate))
augment_fraction_slider.valueChanged.connect(lambda: truncation_fraction_changed(augment_fraction_label, augment_fraction_slider, augment))
tetrahedron_action.triggered.connect(lambda: self.set_current_polyhedron(figures.tetrahedron))
cube_action.triggered.connect(lambda: self.set_current_polyhedron(figures.cube))
octahedron_action.triggered.connect(lambda: self.set_current_polyhedron(figures.octahedron))
dodecahedron_action.triggered.connect(lambda: self.set_current_polyhedron(figures.dodecahedron))
icosahedron_action.triggered.connect(lambda: self.set_current_polyhedron(figures.icosahedron))
undo_action.triggered.connect(self.undo)
redo_action.triggered.connect(self.redo)
clear_polyhedra_action.triggered.connect(self.clear_polyhedra)
element_values_action.triggered.connect(self.element_values)
element_counts_action.triggered.connect(self.element_count)
face_types_action.triggered.connect(self.face_types)
timer = QtCore.QTimer(self)
timer.timeout.connect(moderngl_widget.update)
timer.start(30)
#def add_list_item(self, text):
# if self.current_polyhedron:
# item = QtWidgets.QListWidgetItem(text)
# #self.listWidget.addItem(item)
def set_current_polyhedron(self, polyhedron):
"""Set current_polyhedron to a seed polyhedron."""
self.polytope_models.append(Model(polyhedron))
#self.current_polyhedron = polyhedron
#self.add_list_item(polyhedron.__class__.__name__.capitalize())
def operations(self, operation, **kwargs):
"""Perform a polyhedral operation on current_polyhedron."""
current_polyhedron = operation(self.polytope, **kwargs)
self.polytope_models.append(Model(current_polyhedron))
#self.add_list_item(operation.__name__.capitalize())
def clear_polyhedra(self):
self.polytope_models.clear()
def element_values(self):
self.polytope.full_stats()
def element_count(self):
self.polytope.stats()
def face_types(self):
self.polytope.face_types()
def undo(self):
if len(self.polytope_models) == 0:
print("No previous polyhedron")
else:
self.polytope_models.pop()
def redo(self):
print("No function yet")
class Model:
def __init__(self, polytope):
# Determine color of the polyhedron
self.polytope = polytope
self.color = [self.randomize_color() for face in self.polytope.faces]
def randomize_color(self):
"""Change RGB color of polyhedron."""
return (random.randint(2,10)/10,
random.randint(2,10)/10,
random.randint(2,10)/10)
app = QtWidgets.QApplication(sys.argv)
app_window = MainWindow()
app_window.setupUI()
app_window.show()
sys.exit(app.exec())