forked from manoharan-lab/camera-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QtConvenience.py
202 lines (172 loc) · 6.13 KB
/
QtConvenience.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
from PySide import QtGui, QtCore
QtAlignmentLookup = {}
QtAlignmentLookup['top'] = QtCore.Qt.AlignTop
QtAlignmentLookup['bottom'] = QtCore.Qt.AlignBottom
QtAlignmentLookup['middle'] = QtCore.Qt.AlignCenter
def make_qListWidget(multi_selection = True, height = None, width = None):
qlist = QtGui.QListWidget()
if multi_selection:
qlist.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
if height:
qlist.setFixedHeight(height)
if width:
qlist.setFixedHeight(width)
return qlist
def make_label(text, wordwrap=True, bold=False, height=None, width=None,
align=None):
label = QtGui.QLabel()
label.setText(text)
if wordwrap:
label.setWordWrap(True)
if bold:
label.setStyleSheet('font-weight:bold')
if height:
label.setFixedHeight(height)
if width:
label.setFixedWidth(width)
if align:
if not isinstance(align, QtCore.Qt.AlignmentFlag):
align = QtAlignmentLookup[align]
label.setAlignment(align)
return label
def make_VBox(items, parent=None):
return fill_layout(QtGui.QVBoxLayout(parent), items)
def make_HBox(items, parent=None):
return fill_layout(QtGui.QHBoxLayout(parent), items)
def fill_layout(layout, items):
for item in items:
if isinstance(item, int):
layout.addStretch(item)
elif isinstance(item, QtGui.QBoxLayout):
layout.addLayout(item)
else:
if isinstance(item, basestring):
item = make_label(item)
layout.addWidget(item)
return layout
def make_button(label, callback = None, parent=None, shortcut=None, height=50, width=100,
tooltip=None):
"""
Handle the common boilerplate for creating buttons
Parameters
----------
label : string
The label to display on the button
callback : function
The function to call when the button is clicked
parent : QtGui.QWidget
the parent widget
height, width : int
The dimensions of the button
tooltip : string
A tooltip for the button
"""
button = QtGui.QPushButton(label, parent)
if height:
button.setFixedHeight(height)
if width:
button.setFixedWidth(width)
if shortcut is not None:
button.setShortcut(shortcut)
if tooltip is not None:
button.setToolTip(tooltip)
if callback:
button.clicked.connect(callback)
return button
def make_checkbox(label, start_checked=False, callback=None):
checkbox = QtGui.QCheckBox()
checkbox.setText(label)
if start_checked:
checkbox.toggle()
if callback:
checkbox.stateChanged.connect(callback)
return checkbox
def make_combobox(items, callback=None, width=None, default=None, editable=False):
box = QtGui.QComboBox(editable=editable)
for item in items:
box.addItem(item)
if default is not None:
box.setCurrentIndex(default)
if width is not None:
box.setFixedWidth(150)
if callback is not None:
box.activated[str].connect(callback)
return box
def make_control_group(parent, buttons, exclusive=True, default=None):
controlgroup = QtGui.QButtonGroup(QtGui.QWidget(parent))
for button in buttons:
controlgroup.addButton(button)
button.setCheckable(True)
if default is not None:
default.toggle()
controlgroup.setExclusive(exclusive)
return controlgroup
def make_LineEdit(starting_text=None, callback=None, width=None, align=None):
line = QtGui.QLineEdit()
if starting_text:
line.setText(starting_text)
if width:
line.setFixedWidth(width)
if callback:
line.textChanged.connect(callback)
if align:
if not isinstance(align, QtCore.Qt.AlignmentFlag):
align = QtAlignmentLookup[align]
line.setAlignment(align)
return line
class CheckboxGatedValue(QtGui.QHBoxLayout):
"""A checkbox and value together.
The value can either be generated by a function, or be the value
of a text box. This widget acts like a checkbox and a (optionally)
a line edit together. If the checkbox is not checked, it will
return an empty string as the text
"""
def __init__(self, text, value, callback=None,
default_checked=False):
super(CheckboxGatedValue, self).__init__()
self.checkbox = make_checkbox(text, default_checked, callback=callback)
self.addWidget(self.checkbox)
self._value = value
if isinstance(self._value, QtGui.QLineEdit):
self.addWidget(self._value)
if callback:
self._value.textChanged.connect(callback)
def setCheckState(self, value):
self.checkbox.setCheckState(value)
def text(self):
if not self.checkbox.isChecked():
return ""
if isinstance(self._value, QtGui.QWidget):
return str(self._value.text())
else:
return self._value()
def setText(self, value):
self._value.setText(value)
def isChecked(self):
return self.checkbox.isChecked()
def setChecked(self, value):
self.checkbox.setChecked(value)
def increment_textbox(textbox):
old = textbox.text()
digits = len(old)
textbox.setText(str(int(old)+1).zfill(digits))
def zero_textbox(textbox):
textbox.setText(str(0).zfill(len(textbox.text())))
def textbox_int(textbox):
return int(str(textbox.text()))
def textbox_float(textbox):
return float(str(textbox.text()))
def make_tabs(tabs):
"""Make Qt tabbed panel
Parameters
----------
tabs : [(string, [QtWidget])]
List of pairs of tab names with the contents for the tab. Tab contents
should be a list of QtWidgets, and will be placed in a vbox in the tab
"""
tab_widget = QtGui.QTabWidget()
for name, content in tabs:
tab = QtGui.QWidget()
make_VBox(content, tab)
tab_widget.addTab(tab, name)
return tab_widget