default_gui.py 8.01 KB
Newer Older
Karol Actun's avatar
Karol Actun committed
1
2
3
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QVBoxLayout, QPushButton, QColorDialog, QRadioButton, QLabel, QWidget, QTabWidget, \
                            QSlider, QHBoxLayout
Karol Actun's avatar
Karol Actun committed
4
5
6
from PyQt5.QtCore import Qt


Karol Actun's avatar
Karol Actun committed
7
8
9
10
11
12
13
14
15
16
17
18
def rgba_tuple_to_qcolor(color):
    return QColor(int(color[0]*255),
                  int(color[1]*255),
                  int(color[2]*255),
                  int(color[3]*255))


def qcolor_to_rgba_tuple(qcolor):
    return qcolor.red8() / 255.0, qcolor.green8() / 255.0, qcolor.blue8() / 255.0, qcolor.alpha8() / 255.0


def define_gui(world):
Karol Actun's avatar
Karol Actun committed
19
20

    tabbar = QTabWidget()
Karol Actun's avatar
Karol Actun committed
21
22
23
    tabbar.setMinimumWidth(100)
    tabbar.addTab(sim_tab(world), "Simulation")
    tabbar.addTab(vis_tab(world), "Visualization")
Karol Actun's avatar
Karol Actun committed
24
25
26
27

    return tabbar


Karol Actun's avatar
Karol Actun committed
28
def sim_tab(world):
Karol Actun's avatar
Karol Actun committed
29
30
    tab = QWidget()
    layout = QVBoxLayout()
Karol Actun's avatar
Karol Actun committed
31
32
33
34
    layout.addLayout(get_rps_slider(world))
    layout.addStretch(0)
    # start stop button
    start_stop_button = QPushButton("start Simulation")
Karol Actun's avatar
Karol Actun committed
35

Karol Actun's avatar
Karol Actun committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    def start_stop_sim():
        world.vis.running = not world.vis.running
        if world.vis.running:
            start_stop_button.setText("stop Simulation")
        else:
            start_stop_button.setText("start Simulation")

    start_stop_button.clicked.connect(start_stop_sim)
    layout.addWidget(start_stop_button)
    tab.setLayout(layout)

    return tab

def get_rps_slider(world):
    hbox = QVBoxLayout()
    desc = QLabel("rounds per second (%d) : " % world.vis.rounds_per_second)
Karol Actun's avatar
Karol Actun committed
52
53
54
55
56
57
    hbox.addWidget(desc)
    rps_slider = QSlider(Qt.Horizontal)
    rps_slider.setTickInterval(10)
    rps_slider.setTickPosition(2)
    rps_slider.setMaximum(60)
    rps_slider.setMinimum(1)
Karol Actun's avatar
Karol Actun committed
58
    rps_slider.setSliderPosition(world.vis.rounds_per_second)
Karol Actun's avatar
Karol Actun committed
59
60

    def set_rps():
Karol Actun's avatar
Karol Actun committed
61
62
63
        world.vis.rounds_per_second = rps_slider.value()
        desc.setText("rounds per second (%d) : " % world.vis.rounds_per_second)

Karol Actun's avatar
Karol Actun committed
64
65
66
    rps_slider.valueChanged.connect(set_rps)

    hbox.addWidget(rps_slider)
Karol Actun's avatar
Karol Actun committed
67
    return hbox
Karol Actun's avatar
Karol Actun committed
68
69


Karol Actun's avatar
Karol Actun committed
70
71
72
73
74
75
76
77
78
79
def vis_tab(world):
    tab = QWidget()
    layout = QVBoxLayout()
    #layout.addLayout(get_grid_alpha_slider(world))
    layout.addLayout(get_projection_switch(world))
    layout.addLayout(get_fov_slider(world))
    layout.addLayout(get_drag_sens_slider(world))
    layout.addLayout(get_zoom_sens_slider(world))
    layout.addLayout(get_rota_sens_slider(world))
    layout.addLayout(get_color_picker(world))
Karol Actun's avatar
Karol Actun committed
80

Karol Actun's avatar
Karol Actun committed
81
    layout.addStretch(0)
Karol Actun's avatar
Karol Actun committed
82

Karol Actun's avatar
Karol Actun committed
83
    reset_position_button = QPushButton("reset position")
Karol Actun's avatar
Karol Actun committed
84

Karol Actun's avatar
Karol Actun committed
85
86
87
88
89
90
91
    def reset_pos():
        world.vis.viewer.x_offset = 0
        world.vis.viewer.y_offset = 0
        world.vis.viewer.radius = 10
        world.vis.viewer.phi = 0
        world.vis.viewer.theta = 0
        world.vis.viewer.update_view()
Karol Actun's avatar
Karol Actun committed
92

Karol Actun's avatar
Karol Actun committed
93
94
    reset_position_button.clicked.connect(reset_pos)
    layout.addWidget(reset_position_button)
Karol Actun's avatar
Karol Actun committed
95

Karol Actun's avatar
Karol Actun committed
96
97
    tab.setLayout(layout)
    return tab
Karol Actun's avatar
Karol Actun committed
98
99


Karol Actun's avatar
Karol Actun committed
100
101
102
103
def get_fov_slider(world):
    hbox = QVBoxLayout()
    desc = QLabel("(only for perspective projection)\nfield of view (%d°) : " % world.vis.viewer.fov)
    hbox.addWidget(desc)
Karol Actun's avatar
Karol Actun committed
104
105
106
    fov_slider = QSlider(Qt.Horizontal)
    fov_slider.setTickInterval(10)
    fov_slider.setTickPosition(2)
Karol Actun's avatar
Karol Actun committed
107
108
109
    fov_slider.setMaximum(120)
    fov_slider.setMinimum(10)
    fov_slider.setSliderPosition(world.vis.viewer.fov)
Karol Actun's avatar
Karol Actun committed
110
111

    def set_fov():
Karol Actun's avatar
Karol Actun committed
112
113
114
        world.vis.viewer.fov = fov_slider.value()
        desc.setText("(only for perspective projection)\nfield of view (%d°) : " % world.vis.viewer.fov)
        world.vis.viewer.update_view()
Karol Actun's avatar
Karol Actun committed
115
116
117

    fov_slider.valueChanged.connect(set_fov)

Karol Actun's avatar
Karol Actun committed
118
119
    hbox.addWidget(fov_slider)
    return hbox
Karol Actun's avatar
Karol Actun committed
120
121


Karol Actun's avatar
Karol Actun committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def get_drag_sens_slider(world):
    hbox = QVBoxLayout()
    desc = QLabel("drag sensitivity:")
    hbox.addWidget(desc)
    slider = QSlider(Qt.Horizontal)
    slider.setTickInterval(500)
    slider.setTickPosition(2)
    slider.setMaximum(5000)
    slider.setMinimum(100)
    slider.setSliderPosition(5100-world.vis.viewer.drag_sensitivity)

    def set_fov():
        world.vis.viewer.drag_sensitivity = 5100-slider.value()
        desc.setText("drag sensitivity:")
        world.vis.viewer.update_view()
Karol Actun's avatar
Karol Actun committed
137

Karol Actun's avatar
Karol Actun committed
138
    slider.valueChanged.connect(set_fov)
Karol Actun's avatar
Karol Actun committed
139

Karol Actun's avatar
Karol Actun committed
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
    hbox.addWidget(slider)
    return hbox


def get_zoom_sens_slider(world):
    hbox = QVBoxLayout()
    desc = QLabel("zoom sensitivity:")
    hbox.addWidget(desc)
    slider = QSlider(Qt.Horizontal)
    slider.setTickInterval(100)
    slider.setTickPosition(2)
    slider.setMaximum(1000)
    slider.setMinimum(1)
    slider.setSliderPosition(1001-world.vis.viewer.zoom_sensitivity)

    def set_fov():
        world.vis.viewer.zoom_sensitivity = 1001-slider.value()
        desc.setText("zoom sensitivity:")
        world.vis.viewer.update_view()

    slider.valueChanged.connect(set_fov)

    hbox.addWidget(slider)
    return hbox


def get_rota_sens_slider(world):
    hbox = QVBoxLayout()
    desc = QLabel("(only for 3D)\nrotation sensitivity:")
    hbox.addWidget(desc)
    slider = QSlider(Qt.Horizontal)
    slider.setTickInterval(1)
    slider.setTickPosition(2)
    slider.setMaximum(10)
    slider.setMinimum(1)
    slider.setSliderPosition(11 - world.vis.viewer.rotate_sensitivity)

    def set_fov():
        world.vis.viewer.rotate_sensitivity = 11 - slider.value()
        desc.setText("(only for 3D)\nrotation sensitivity:")
        world.vis.viewer.update_view()

    slider.valueChanged.connect(set_fov)

    hbox.addWidget(slider)
    return hbox


def get_projection_switch(world):
    vbox = QVBoxLayout()
    desc = QLabel("projection type:")
    vbox.addWidget(desc)

    o = QRadioButton("orthographic")

    def orthotoggle():
        if o.isChecked():
            world.vis.viewer.projection = "ortho"
            world.vis.viewer.update_view()
    o.toggled.connect(orthotoggle)

    p = QRadioButton("perspective")
    p.setChecked(True)

    def perstoggle():
        if p.isChecked():
            world.vis.viewer.projection = "perspective"
            world.vis.viewer.update_view()
    p.toggled.connect(perstoggle)
    hbox = QHBoxLayout()
    hbox.addWidget(o)
    hbox.addWidget(p)
    vbox.addLayout(hbox)
    return vbox

def get_color_picker(world):
    pc_button = QPushButton("particles")
    def pc():
        current = rgba_tuple_to_qcolor(world.vis.viewer.matter_program.particle_color)
        world.vis.viewer.matter_program.particle_color = \
            qcolor_to_rgba_tuple(QColorDialog.getColor(current).rgba64())
        world.vis.viewer.update_view()

    pc_button.clicked.connect(pc)

    tc_button = QPushButton("tiles")

    def tc():
        current = rgba_tuple_to_qcolor(world.vis.viewer.matter_program.tile_color)
        world.vis.viewer.matter_program.tile_color = \
            qcolor_to_rgba_tuple(QColorDialog.getColor(current).rgba64())
        world.vis.viewer.update_view()

    tc_button.clicked.connect(tc)

    mc_button = QPushButton("markers")

    def mc():
        current = rgba_tuple_to_qcolor(world.vis.viewer.matter_program.marker_color)
        world.vis.viewer.matter_program.marker_color = \
            qcolor_to_rgba_tuple(QColorDialog.getColor(current).rgba64())
        world.vis.viewer.update_view()

    mc_button.clicked.connect(mc)

    gr_button = QPushButton("grid")

    def gr():
        current = rgba_tuple_to_qcolor(world.vis.viewer.grid_program.color)
        new = qcolor_to_rgba_tuple(QColorDialog.getColor(current, options=QColorDialog.ShowAlphaChannel).rgba64())
        world.vis.viewer.grid_program.set_color(new)
        world.vis.viewer.update_view()

    gr_button.clicked.connect(gr)

    bg_button = QPushButton("background")

    def bg():
        current = rgba_tuple_to_qcolor(world.vis.viewer.background)
        new = qcolor_to_rgba_tuple(QColorDialog.getColor(current).rgba64())
        world.vis.viewer.set_background(new)
        world.vis.viewer.update_view()

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

Karol Actun's avatar
Karol Actun committed
265
266
267
268
269
270
271
272
273
274
275
276
277
    vbox = QVBoxLayout()
    desc = QLabel("change color of:")
    vbox.addWidget(desc)
    hbox1 = QHBoxLayout()
    hbox1.addWidget(pc_button)
    hbox1.addWidget(tc_button)
    hbox1.addWidget(mc_button)
    hbox2 = QHBoxLayout()
    hbox2.addWidget(gr_button)
    hbox2.addWidget(bg_button)
    vbox.addLayout(hbox1)
    vbox.addLayout(hbox2)
    return vbox