-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpandableTabWidget.py
249 lines (205 loc) · 9.29 KB
/
expandableTabWidget.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
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
from collections import OrderedDict
class clickableQLabel(QtWidgets.QLabel):
clicked = QtCore.pyqtSignal()
def mousePressEvent(self, event):
if (event.buttons() & QtCore.Qt.LeftButton):
self.clicked.emit()
class scanningTab(QtWidgets.QWidget):
"""Widget that sets scanning parameters."""
textChanged = QtCore.pyqtSignal(int, str)
# scanToggled = QtCore.pyqtSignal(int, int)
def __init__(self, id, hiddenItems, parent=None):
super(scanningTab, self).__init__(parent)
self.id = id
self.layout = QtWidgets.QGridLayout()
self.setLayout(self.layout)
self.scanCheckbox = QtWidgets.QCheckBox()
self.scanSelectionLabel = clickableQLabel()
self.scanSelectionWidget = QtWidgets.QComboBox()
self.scanFromLabel = QtWidgets.QLabel('Scan From')
self.scanToLabel = QtWidgets.QLabel('Scan To')
self.scanStepLabel = QtWidgets.QLabel('Scan Step')
self.scanFromWidget = QtWidgets.QLineEdit()
self.scanFromWidget.setValidator(QtGui.QDoubleValidator())
self.scanToWidget = QtWidgets.QLineEdit()
self.scanToWidget.setValidator(QtGui.QDoubleValidator())
self.scanStepWidget = QtWidgets.QLineEdit()
self.scanStepWidget.setValidator(QtGui.QDoubleValidator())
self.scanSelectionWidget.setEnabled(False)
self.scanFromWidget.setEnabled(False)
self.scanToWidget.setEnabled(False)
self.scanStepWidget.setEnabled(False)
self.layout.addWidget(self.scanCheckbox,0,0,1,1)
self.layout.addWidget(self.scanSelectionLabel,0,1,1,1)
self.layout.addWidget(self.scanSelectionWidget,0,2,1,5)
self.layout.addWidget(self.scanFromLabel,1,0,1,2)
self.layout.addWidget(self.scanFromWidget,1,2,1,1)
self.layout.addWidget(self.scanToLabel,1,3,1,1)
self.layout.addWidget(self.scanToWidget,1,4,1,1)
self.layout.addWidget(self.scanStepLabel,1,5,1,1)
self.layout.addWidget(self.scanStepWidget,1,6,1,1)
self.scanCheckbox.stateChanged.connect(self.toggle_scan_parameters_state)
self.scanSelectionLabel.clicked.connect(self.toggle_scanCheckbox_state)
self.scanSelectionWidget.currentTextChanged.connect(self.emit_index_changed)
self._items = {}
self._hiddenItems = hiddenItems
def emit_index_changed(self, text):
self.textChanged.emit(self.id, text)
def addItems(self, dict):
self._items = dict
self.update_displayable_values()
def hideItem(self, id, item):
if not id == self.id:
self._hiddenItems[id] = item
self.update_displayable_values()
def update_displayable_values(self):
index = self.scanSelectionWidget.currentIndex()
text = self.scanSelectionWidget.currentText()
index = index if index >= 0 else 0
self.scanSelectionWidget.currentTextChanged.disconnect(self.emit_index_changed)
self.scanSelectionWidget.clear()
hiddenItems = [self._hiddenItems[k] for k in self._hiddenItems.keys() if k < self.id]
for (parameterDisplayStr, parameter) in self._items.items():
if not parameterDisplayStr in hiddenItems:
self.scanSelectionWidget.addItem(parameterDisplayStr, parameter)
self.scanSelectionWidget.currentTextChanged.connect(self.emit_index_changed)
newindex = self.scanSelectionWidget.findData(text, QtCore.Qt.MatchExactly)
if not newindex == -1:
self.scanSelectionWidget.setCurrentIndex(newindex)
else:
self.scanSelectionWidget.setCurrentIndex(index)
if not self.scanSelectionWidget.currentText() == text:
self.emit_index_changed(self.scanSelectionWidget.currentText())
def set_id(self, id):
self.id = id
self.scanCheckbox.setAccessibleName("scan:"+str(id)+":scan")
self.scanSelectionLabel.setText('Scan Parameter ' + str(id))
self.scanSelectionWidget.setAccessibleName("scan:"+str(id)+':parameter')
self.scanFromWidget.setAccessibleName("scan:"+str(id)+":scan_from_value")
self.scanToWidget.setAccessibleName("scan:"+str(id)+":scan_to_value")
self.scanStepWidget.setAccessibleName("scan:"+str(id)+":scan_step_size")
def toggle_scanCheckbox_state(self):
self.scanCheckbox.setChecked(not self.scanCheckbox.isChecked())
def toggle_scan_parameters_state(self):
if self.scanCheckbox.isChecked():
self.scanSelectionWidget.setEnabled(True)
self.scanFromWidget.setEnabled(True)
self.scanToWidget.setEnabled(True)
self.scanStepWidget.setEnabled(True)
# self.scanToggled.emit(self.id, 0)
else:
self.scanSelectionWidget.setEnabled(False)
self.scanFromWidget.setEnabled(False)
self.scanToWidget.setEnabled(False)
self.scanStepWidget.setEnabled(False)
# self.scanToggled.emit(self.id, 1)
class middleClickTabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(middleClickTabBar, self).__init__(parent)
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.MidButton:
self.tabCloseRequested.emit(self.tabAt(event.pos()))
super(QtWidgets.QTabBar, self).mouseReleaseEvent(event)
class expandableTabWidget(QtWidgets.QTabWidget):
"""Tab Widget that that can have new tabs easily added to it."""
scanTabAdded = QtCore.pyqtSignal(int)
scanTabRemoved = QtCore.pyqtSignal(object)
def __init__(self, parent=None):
super(expandableTabWidget, self).__init__(parent)
self.id = 1
self.tabs = OrderedDict()
self._hiddenItems = {}
# QtGui.QTabWidget.__init__(self, parent)
# Tab Bar
self.tab = middleClickTabBar()
self.setTabBar(self.tab)
# Properties
# self.setMovable(True)
# self.setTabsClosable(True)
self.buttonWidget = QtWidgets.QWidget()
self.buttonWidgetLayout = QtWidgets.QHBoxLayout()
self.buttonWidgetLayout.setSpacing(0)
self.buttonWidgetLayout.setContentsMargins(0,0,0,0)
self.buttonWidget.setLayout(self.buttonWidgetLayout)
# Add tab button
self.plusButton = QtWidgets.QToolButton(self)
self.plusButton.setText("+")
self.plusButton.setFixedSize(QtCore.QSize(20, 20))
self.plusButton.setToolTip('Add Scanning Dimension')
self.buttonWidgetLayout.addWidget(self.plusButton)
# Clear scanning checkboxes
self.clearButton = QtWidgets.QToolButton(self)
self.clearButton.setText("Clear")
self.clearButton.setFixedSize(QtCore.QSize(35, 20))
self.clearButton.setToolTip('Clear all Scan checkboxes')
self.buttonWidgetLayout.addWidget(self.clearButton)
# Set Corner widget
self.setCornerWidget(self.buttonWidget)
# Signals
self.plusButton.clicked.connect(self.addScanTab)
self.clearButton.clicked.connect(self.clearScans)
self.tab.tabCloseRequested.connect(self.removeScanTab)
def addScanTab(self):
tab = scanningTab(0, self._hiddenItems)
self.addTab(tab,'')
self.setCurrentWidget(tab)
self.relabel_tabs()
# tab.textChanged.connect(self.tabTextChanged)
# tab.scanToggled.connect(self.tab_scan_toggled)
self.scanTabAdded.emit(tab.id)
def removeScanTab(self, index):
id = self.widget(index).id
tab = self.widget(index)
self.removeTab(index)
tab.deleteLater()
self.relabel_tabs()
self.scanTabRemoved.emit(tab.layout)
if not self.tab.count() > 0:
self.addScanTab()
def relabel_tabs(self):
tabs = {}
for i in range(self.tab.count()):
v = self.widget(i)
if hasattr(v, 'set_id'):
v.set_id(i+1)
tabs[i+1] = v
self.setTabText(i, 'Scan '+str(i+1))
self.tabs = tabs
# def tab_scan_toggled(self, id, value):
# if value:
# print(id, False)
# self.tab.setTabTextColor(id, QtGui.QColor(255,0,0))
# else:
# print(id, True)
# self.tab.setTabTextColor(id, QtGui.QColor(0,0,0));
def tabTextChanged(self, id, text):
if text is not '':
self._hiddenItems[id] = text
print(id, text)
for i in range(int(id), self.tab.count()):
if not i+1 == int(id):
tab = self.widget(i)
tab.update_displayable_values()
def clearScans(self):
for id, tab in self.tabs.items():
tab.scanCheckbox.setCheckState(QtCore.Qt.Unchecked)
class TabExample(QtWidgets.QMainWindow):
def __init__(self):
super(TabExample, self).__init__()
self.setWindowTitle("Tab example")
# Create widgets
self.tab_widget = expandableTabWidget()
self.setCentralWidget(self.tab_widget)
# Label's to fill widget
self.label1 = QtWidgets.QLabel("Tab 1")
self.label2 = QtWidgets.QLabel("Tab 2")
# Adding tab's
self.tab_widget.addTab(self.label1, "Tab 1")
self.tab_widget.addTab(self.label2, "Tab 2")
self.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
gui = TabExample()
sys.exit(app.exec_())