default_gui.py 11.3 KB
Newer Older
Karol Actun's avatar
Karol Actun committed
1
2
3
from PyQt5.QtGui import QColor, QIntValidator
from PyQt5.QtWidgets import (QVBoxLayout, QPushButton, QColorDialog, QRadioButton, QLabel, QTabWidget,
                             QSlider, QHBoxLayout, QCheckBox, QTabBar, QTextEdit, QLineEdit, QStyle)
Karol Actun's avatar
Karol Actun committed
4
from PyQt5.QtCore import Qt
Karol Actun's avatar
Karol Actun committed
5
6
7
from OpenGL.GL import glGetFloatv, GL_LINE_WIDTH_RANGE
from lib.vis3d import Visualization
from lib.visualization.utils import eprint
Karol Actun's avatar
Karol Actun committed
8
9


Karol Actun's avatar
Karol Actun committed
10
11
12
13
14
15
16
17
18
19
20
21
22
def create_slider(tick_interval: int, tick_position: int, max_position: int, min_position: int,
                  slider_position: int, callback, orientation=Qt.Horizontal):
    """
    helper function for creating a slider
    """
    slider = QSlider(orientation)
    slider.setTickInterval(tick_interval)
    slider.setTickPosition(tick_position)
    slider.setMaximum(max_position)
    slider.setMinimum(min_position)
    slider.setSliderPosition(slider_position)
    slider.valueChanged.connect(callback)
    return slider
Karol Actun's avatar
Karol Actun committed
23
24


Karol Actun's avatar
Karol Actun committed
25
def create_gui(world, vis: Visualization):
Karol Actun's avatar
Karol Actun committed
26
27

    tabbar = QTabWidget()
Karol Actun's avatar
Karol Actun committed
28
29
30
    tabbar.setMinimumWidth(200)
    tabbar.addTab(sim_tab(vis, world), "Simulation")
    tabbar.addTab(vis_tab(vis), "Visualization")
Karol Actun's avatar
Karol Actun committed
31
32
33
34

    return tabbar


Karol Actun's avatar
Karol Actun committed
35
36
37
38
39
40
41
def key_handler(key, world, vis):
    if key == Qt.Key_Space:
        vis.start_stop()


def sim_tab(vis, world):
    tab = QTabBar()
Karol Actun's avatar
Karol Actun committed
42
    layout = QVBoxLayout()
Karol Actun's avatar
Karol Actun committed
43
44
    layout.addLayout(get_rps_slider(vis))

Karol Actun's avatar
Karol Actun committed
45
    # start stop button
Karol Actun's avatar
Karol Actun committed
46
    start_stop_button = QPushButton("start/stop Simulation")
Karol Actun's avatar
Karol Actun committed
47

Karol Actun's avatar
Karol Actun committed
48
    def start_stop_sim():
Karol Actun's avatar
Karol Actun committed
49
        vis.start_stop()
Karol Actun's avatar
Karol Actun committed
50
    start_stop_button.clicked.connect(start_stop_sim)
Karol Actun's avatar
Karol Actun committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

    # screenshots button
    screenshot_button = QPushButton("take Screenshot")

    def take_screenshot():
        vis.take_screenshot()
    screenshot_button.clicked.connect(take_screenshot)

    # reset button
    reset_button = QPushButton("reset Simulation")

    def reset_sim():
        world.reset()
    reset_button.clicked.connect(reset_sim)

    layout.addWidget(screenshot_button, alignment=Qt.AlignBaseline)
    layout.addWidget(reset_button, alignment=Qt.AlignBaseline)
    layout.addStretch(0)
    layout.addWidget(start_stop_button, alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
70
71
72
73
    tab.setLayout(layout)

    return tab

Karol Actun's avatar
Karol Actun committed
74
75

def get_rps_slider(vis):
Karol Actun's avatar
Karol Actun committed
76
    hbox = QVBoxLayout()
Karol Actun's avatar
Karol Actun committed
77
78
79
80
81
82
83
84
    desc = QLabel("rounds per second (%d) : " % vis.get_rounds_per_second())
    hbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_rps(value):
        vis.set_rounds_per_second(value)
        desc.setText("rounds per second (%d) : " % vis.get_rounds_per_second())

    hbox.addWidget(create_slider(10, 2, 60, 1, vis.get_rounds_per_second(), set_rps), alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
85
    return hbox
Karol Actun's avatar
Karol Actun committed
86
87


Karol Actun's avatar
Karol Actun committed
88
89
def vis_tab(vis: Visualization):
    tab = QTabBar()
Karol Actun's avatar
Karol Actun committed
90
    layout = QVBoxLayout()
Karol Actun's avatar
Karol Actun committed
91
92
93
94
95
96
97
98
99
100
101
102
    layout.addLayout(get_projection_switch(vis))
    layout.addLayout(get_fov_slider(vis))
    layout.addLayout(get_render_distance_slider(vis))
    layout.addLayout(get_drag_sens_slider(vis))
    layout.addLayout(get_zoom_sens_slider(vis))
    layout.addLayout(get_rota_sens_slider(vis))
    layout.addLayout(get_grid_width_slider(vis))
    layout.addLayout(get_grid_lines_scale_slider(vis))
    layout.addLayout(get_grid_locations_scale_slider(vis))
    layout.addLayout(get_show_checkboxes(vis))
    layout.addLayout(recalculate_grid(vis))
    layout.addLayout(get_color_picker(vis))
Karol Actun's avatar
Karol Actun committed
103

Karol Actun's avatar
Karol Actun committed
104
    layout.addStretch(0)
Karol Actun's avatar
Karol Actun committed
105

Karol Actun's avatar
Karol Actun committed
106
    reset_position_button = QPushButton("reset position")
Karol Actun's avatar
Karol Actun committed
107
108
    reset_position_button.clicked.connect(vis.reset_camera_position)
    layout.addWidget(reset_position_button, alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
109

Karol Actun's avatar
Karol Actun committed
110
111
    tab.setLayout(layout)
    return tab
Karol Actun's avatar
Karol Actun committed
112
113


Karol Actun's avatar
Karol Actun committed
114
def get_fov_slider(vis: Visualization):
Karol Actun's avatar
Karol Actun committed
115
    hbox = QVBoxLayout()
Karol Actun's avatar
Karol Actun committed
116
117
118
119
120
121
122
123
    desc = QLabel("(only for perspective projection)\nfield of view (%d°) : " % vis.get_field_of_view())
    hbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_fov(value):
        vis.set_field_of_view(value)
        desc.setText("(only for perspective projection)\nfield of view (%d°) : " % vis.get_field_of_view())

    hbox.addWidget(create_slider(10, 2, 120, 10, vis.get_field_of_view(), set_fov), alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
124
    return hbox
Karol Actun's avatar
Karol Actun committed
125
126


Karol Actun's avatar
Karol Actun committed
127
def get_drag_sens_slider(vis):
Karol Actun's avatar
Karol Actun committed
128
129
    hbox = QVBoxLayout()
    desc = QLabel("drag sensitivity:")
Karol Actun's avatar
Karol Actun committed
130
131
132
133
134
135
136
    hbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_ds(value):
        vis.set_drag_sensitivity(5100-value)

    hbox.addWidget(create_slider(500, 2, 5000, 100, 5100-vis.get_drag_sensitivity(), set_ds),
                   alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
137
138
139
    return hbox


Karol Actun's avatar
Karol Actun committed
140
def get_zoom_sens_slider(vis):
Karol Actun's avatar
Karol Actun committed
141
142
    hbox = QVBoxLayout()
    desc = QLabel("zoom sensitivity:")
Karol Actun's avatar
Karol Actun committed
143
144
145
146
147
148
    hbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_zs(value):
        vis.set_zoom_sensitivity(1001-value)

    hbox.addWidget(create_slider(100, 2, 1000, 1, 1001-vis.get_zoom_sensitivity(), set_zs), alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
149
150
151
    return hbox


Karol Actun's avatar
Karol Actun committed
152
def get_rota_sens_slider(vis):
Karol Actun's avatar
Karol Actun committed
153
154
    hbox = QVBoxLayout()
    desc = QLabel("(only for 3D)\nrotation sensitivity:")
Karol Actun's avatar
Karol Actun committed
155
156
157
158
159
160
    hbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_rs(value):
        vis.set_rotation_sensitivity(11-value)

    hbox.addWidget(create_slider(1, 2, 10, 1, 11-vis.get_rotation_sensitivity(), set_rs), alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
161
162
163
    return hbox


Karol Actun's avatar
Karol Actun committed
164
def get_projection_switch(vis):
Karol Actun's avatar
Karol Actun committed
165
166
    vbox = QVBoxLayout()
    desc = QLabel("projection type:")
Karol Actun's avatar
Karol Actun committed
167
    vbox.addWidget(desc, alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
168
169
170

    o = QRadioButton("orthographic")

Karol Actun's avatar
Karol Actun committed
171
    def orth_toggle():
Karol Actun's avatar
Karol Actun committed
172
        if o.isChecked():
Karol Actun's avatar
Karol Actun committed
173
174
            vis.set_projection_type("ortho")
    o.toggled.connect(orth_toggle)
Karol Actun's avatar
Karol Actun committed
175
176
177

    p = QRadioButton("perspective")

Karol Actun's avatar
Karol Actun committed
178
179
180
181
182
183
    if vis.get_projection_type() == "ortho":
        o.setChecked(True)
    else:
        p.setChecked(True)

    def pers_toggle():
Karol Actun's avatar
Karol Actun committed
184
        if p.isChecked():
Karol Actun's avatar
Karol Actun committed
185
186
187
            vis.set_projection_type("perspective")
    p.toggled.connect(pers_toggle)

Karol Actun's avatar
Karol Actun committed
188
    hbox = QHBoxLayout()
Karol Actun's avatar
Karol Actun committed
189
190
    hbox.addWidget(o, alignment=Qt.AlignBaseline)
    hbox.addWidget(p, alignment=Qt.AlignBaseline)
Karol Actun's avatar
Karol Actun committed
191
192
193
194
    vbox.addLayout(hbox)
    return vbox


Karol Actun's avatar
Karol Actun committed
195
def get_color_picker(vis):
Karol Actun's avatar
Karol Actun committed
196

Karol Actun's avatar
Karol Actun committed
197
    bg_button = QPushButton("background")
Karol Actun's avatar
Karol Actun committed
198

Karol Actun's avatar
Karol Actun committed
199
200
201
202
203
204
    def bg():
        qcd = QColorDialog()
        qcd.setCurrentColor(QColor.fromRgbF(*vis.get_background_color()))
        qcd.exec()
        if qcd.result() == 1:
            vis.set_background_color((qcd.selectedColor().getRgbF()[:3]))
Karol Actun's avatar
Karol Actun committed
205

Karol Actun's avatar
Karol Actun committed
206
    bg_button.clicked.connect(bg)
Karol Actun's avatar
Karol Actun committed
207

Karol Actun's avatar
Karol Actun committed
208
    lines_button = QPushButton("grid lines")
Karol Actun's avatar
Karol Actun committed
209

Karol Actun's avatar
Karol Actun committed
210
211
212
213
214
215
216
    def lines():
        qcd = QColorDialog()
        qcd.setOption(QColorDialog.ShowAlphaChannel)
        qcd.setCurrentColor(QColor.fromRgbF(*vis.get_grid_line_color()))
        qcd.exec()
        if qcd.result() == 1:
            vis.set_grid_line_color((qcd.selectedColor().getRgbF()))
Karol Actun's avatar
Karol Actun committed
217

Karol Actun's avatar
Karol Actun committed
218
    lines_button.clicked.connect(lines)
Karol Actun's avatar
Karol Actun committed
219

Karol Actun's avatar
Karol Actun committed
220
    locs_button = QPushButton("grid locations")
Karol Actun's avatar
Karol Actun committed
221

Karol Actun's avatar
Karol Actun committed
222
223
224
225
226
227
228
    def locs():
        qcd = QColorDialog()
        qcd.setOption(QColorDialog.ShowAlphaChannel)
        qcd.setCurrentColor(QColor.fromRgbF(*vis.get_grid_location_color()))
        qcd.exec()
        if qcd.result() == 1:
            vis.set_grid_location_color((qcd.selectedColor().getRgbF()))
Karol Actun's avatar
Karol Actun committed
229

Karol Actun's avatar
Karol Actun committed
230
    locs_button.clicked.connect(locs)
Karol Actun's avatar
Karol Actun committed
231

Karol Actun's avatar
Karol Actun committed
232
233
234
235
236
237
238
239
240
    vbox = QVBoxLayout()
    desc = QLabel("change color of:")
    vbox.addWidget(desc, alignment=Qt.AlignBaseline)
    hbox = QHBoxLayout()
    hbox.addWidget(lines_button, alignment=Qt.AlignBaseline)
    hbox.addWidget(locs_button, alignment=Qt.AlignBaseline)
    hbox.addWidget(bg_button, alignment=Qt.AlignBaseline)
    vbox.addLayout(hbox)
    return vbox
Karol Actun's avatar
Karol Actun committed
241
242


Karol Actun's avatar
Karol Actun committed
243
244
245
246
247
248
249
250
def get_grid_width_slider(vis):
    hbox = QVBoxLayout()
    desc = QLabel("grid width (%d):" % vis.get_grid_line_width())
    hbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_gw(value):
        vis.set_grid_line_width(value)
        desc.setText("grid width (%d):" % value)
Karol Actun's avatar
Karol Actun committed
251

Karol Actun's avatar
Karol Actun committed
252
253
254
255
256
257
    hbox.addWidget(create_slider(1, 2, glGetFloatv(GL_LINE_WIDTH_RANGE)[1], 1, vis.get_grid_line_width(), set_gw),
                   alignment=Qt.AlignBaseline)
    return hbox


def get_render_distance_slider(vis):
Karol Actun's avatar
Karol Actun committed
258
    vbox = QVBoxLayout()
Karol Actun's avatar
Karol Actun committed
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
    desc = QLabel("render distance (%d):" % vis.get_render_distance())
    vbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_mrd(value):
        vis.set_render_distance(value)
        desc.setText("render distance (%d):" % vis.get_render_distance())

    vbox.addWidget(create_slider(10, 2, 5000, 5, vis.get_render_distance(), set_mrd), alignment=Qt.AlignBaseline)
    return vbox


def get_show_checkboxes(vis):

    lines_cb = QCheckBox()
    lines_cb.setText("show lines")
    lines_cb.setChecked(vis.get_show_lines())

    def lines_clicked():
        vis.set_show_lines(lines_cb.isChecked())
    lines_cb.clicked.connect(lines_clicked)

    locs_cb = QCheckBox()
    locs_cb.setText("show locations")
    locs_cb.setChecked(vis.get_show_locations())

    def locs_clicked():
        vis.set_show_locations(locs_cb.isChecked())

    locs_cb.clicked.connect(locs_clicked)

    center_cb = QCheckBox()
    center_cb.setText("show center")
    center_cb.setChecked(vis.get_show_center())

    def center_clicked():
        vis.set_show_center(center_cb.isChecked())

    center_cb.clicked.connect(center_clicked)

    focus_cb = QCheckBox()
    focus_cb.setText("show focus")
    focus_cb.setChecked(vis.get_show_focus())

    def focus_clicked():
        vis.set_show_focus(focus_cb.isChecked())

    focus_cb.clicked.connect(focus_clicked)

    rl_cb = QCheckBox()
    rl_cb.setText("rotate light")
    rl_cb.setChecked(vis.light_rotation)

    def rl_clicked():
        vis.light_rotation = rl_cb.isChecked()

    rl_cb.clicked.connect(rl_clicked)

Karol Actun's avatar
Karol Actun committed
316
    hbox1 = QHBoxLayout()
Karol Actun's avatar
Karol Actun committed
317
318
    hbox1.addWidget(lines_cb)
    hbox1.addWidget(locs_cb)
Karol Actun's avatar
Karol Actun committed
319
    hbox2 = QHBoxLayout()
Karol Actun's avatar
Karol Actun committed
320
321
322
323
324
325
    hbox2.addWidget(center_cb)
    hbox2.addWidget(focus_cb)
    hbox3 = QHBoxLayout()
    hbox3.addWidget(rl_cb)

    vbox = QVBoxLayout()
Karol Actun's avatar
Karol Actun committed
326
327
    vbox.addLayout(hbox1)
    vbox.addLayout(hbox2)
Karol Actun's avatar
Karol Actun committed
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
    vbox.addLayout(hbox3)
    return vbox


def get_grid_lines_scale_slider(vis):
    vbox = QVBoxLayout()
    desc = QLabel("grid lines scale (%f):" % vis.get_grid_line_scaling()[0])
    vbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_scale(value):
        vis.set_grid_line_scaling([value/50.0, value/50.0, value/50.0])
        desc.setText("grid lines scale (%f):" % float(value/50.0))

    vbox.addWidget(create_slider(10, 2, 50, 10, int(vis.get_grid_line_scaling()[0]*50), set_scale),
                   alignment=Qt.AlignBaseline)
    return vbox


def get_grid_locations_scale_slider(vis):
    vbox = QVBoxLayout()
    desc = QLabel("grid locations model scale (%f%%):" % vis.get_grid_location_scaling()[0])
    vbox.addWidget(desc, alignment=Qt.AlignBaseline)

    def set_scale(value):
        vis.set_grid_location_scaling([value/1000.0, value/1000.0, value/1000.0])
        desc.setText("grid locations model scale (%f%%):" % float(value/1000.0))

    vbox.addWidget(create_slider(10, 2, 200, 10, int(vis.get_grid_location_scaling()[0]*1000.0), set_scale),
                   alignment=Qt.AlignBaseline)
    return vbox


def recalculate_grid(vis):
    hbox = QHBoxLayout()
    rec_button = QPushButton("update grid with size:")

    size_edit = QLineEdit()
    size_edit.setValidator(QIntValidator())
    size_edit.setText(str(vis._world.grid.size))

    def on_click():
        if size_edit.text().isnumeric():
            vis.recalculate_grid(int(size_edit.text()))
        else:
            eprint("warning: grid size has to be a number")

    rec_button.clicked.connect(on_click)

    hbox.addWidget(rec_button, alignment=Qt.AlignBaseline)
    hbox.addWidget(size_edit, alignment=Qt.AlignBaseline)

    return hbox