-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
226 lines (171 loc) · 5.33 KB
/
main.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
# UI Generation From Code
# By: Hyuri Pimentel
#
# For now, you have to prefix with "UI_" the name of the variables of which you want widgets to be generated for.
# See the example code "code.py" for reference.
#-------------------------------------------------------------------------------------------
import sys
from PySide2.QtWidgets import QSizePolicy,\
QApplication,\
QLabel,\
QSpinBox,\
QDoubleSpinBox,\
QLineEdit,\
QCheckBox,\
QComboBox,\
QPushButton,\
QVBoxLayout,\
QHBoxLayout,\
QGridLayout,\
QGroupBox,\
QScrollArea,\
QFrame,\
QLayout,\
QWidget,\
QShortcut
from PySide2.QtCore import Slot,\
Qt,\
Signal,\
QThread,\
QObject
from PySide2.QtGui import QKeySequence, QFont
#-------------------------------------------------------------------------------------------
from constants import *
import assets
#-------------------------------------------------------------------------------------------
# Sample Code
import code
#-------------------------------------------------------------------------------------------
def get_widget_by_type(obj):
if type(obj) == int:
widget = QSpinBox()
widget.setRange(INT_RANGE_MIN, INT_RANGE_MAX)
widget.setSingleStep(INT_RANGE_STEP)
widget.setValue(obj)
widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
elif type(obj) == float:
widget = QDoubleSpinBox()
widget.setRange(FLOAT_RANGE_MIN, FLOAT_RANGE_MAX)
widget.setSingleStep(FLOAT_RANGE_STEP)
widget.setValue(obj)
widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
elif type(obj) == str:
widget = QLineEdit()
widget.setText(obj)
elif type(obj) == bool:
widget = QCheckBox()
widget.setChecked(obj)
elif type(obj) == list:
widget = QComboBox()
widget.addItems(obj)
widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
elif type(obj) == tuple:
widget = QFrame()
if len(obj) <= 3:
box = QHBoxLayout()
else:
box = QVBoxLayout()
box.setMargin(0)
for item in obj:
value_widget = QLabel(f"{item}")
value_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
value_widget.setObjectName("tuple")
box.addWidget(value_widget)
widget.setLayout(box)
elif type(obj) == dict:
widget = QFrame()
# If less than 3 items, lay it out horizontally else vertically
# if len(obj) <= 3:
# box = QHBoxLayout()
# else:
# box = QVBoxLayout()
# box.setMargin(0)
grid = QGridLayout()
grid.setMargin(0)
row = 0
for key in obj:
label = QLabel(f"{key.capitalize()}:")
grid.addWidget(label, row, 0)
value_widget = get_widget_by_type(obj[key])
value_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
grid.addWidget(value_widget, row, 1)
try:
value_widget.setRange(INT_RANGE_MIN, INT_RANGE_MAX)
value_widget.setSingleStep(INT_RANGE_STEP)
value_widget.setValue(obj[key])
except:
pass
row += 1
widget.setLayout(grid)
# TODO: Lists inside of lists. Should probably use QTreeView
# elif type(obj) == list:
# widget = []
# for l in obj:
# widget.append(QComboBox())
# widget[-1].addItems(obj)
# widget[-1].setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
return widget
#-------------------------------------------------------------------------------------------
#
def grid_layout_build(variables_list):
grid = QGridLayout()
grid.setSpacing(16)
grid.setMargin(8)
# grid.setAlignment(Qt.AlignTop)
row = 0
for name, value in variables_list.items():
if name.startswith("UI_"):
name = name.replace("UI_", "").replace("_", " ").capitalize()
grid.addWidget(QLabel(f"{name}:"), row, 0, alignment = Qt.AlignTop)
grid.addWidget(get_widget_by_type(value), row, 1, alignment = Qt.AlignTop)
row += 1
return grid
#-------------------------------------------------------------------------------------------
#
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
#
class Panel(QWidget):
header_font = QFont()
header_font.setBold(True)
header_font.setPointSize(10)
def __init__(self, name, script):
QWidget.__init__(self)
self.name = name
self.script = script
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignTop)
self.layout.setMargin(0)
self.header = QLabel(self.name)
self.header.setFont(self.header_font)
self.header.setAlignment(Qt.AlignLeft)
self.header.setObjectName("header")
self.layout.addWidget(self.header)
self.grid = grid_layout_build(vars(self.script))
self.layout.addLayout(self.grid)
self.setLayout(self.layout)
#-------------------------------------------------------------------------------------------
# Main
if __name__ == "__main__":
app = QApplication(sys.argv)
# Main Window
main_window = MainWindow()
# main_window.resize(300, 300)
main_window.setWindowTitle("UI From Variables")
# Vertical Box
layout = QVBoxLayout()
layout.setAlignment(Qt.AlignTop)
layout.setMargin(0)
# Panels
for i in range(1):
panel = Panel(f"Panel {i + 1}", code)
layout.addWidget(panel)
main_window.setLayout(layout)
# Stylesheet
with open(assets.stylesheets["default"], "r") as stylesheet:
main_window.setStyleSheet(stylesheet.read())
main_window.show()
# Shortcuts
QShortcut(QKeySequence("Ctrl+Q"), main_window, main_window.close)
sys.exit(app.exec_())