forked from fschill/mavue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_elements.py
397 lines (322 loc) · 15 KB
/
gui_elements.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
250
251
252
253
254
255
256
257
258
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
316
317
318
319
320
321
322
323
324
325
326
327
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
'''
MAVUE v0.1 (beta)
Graphical inspector for MAVLink enabled embedded systems.
Copyright (c) 2009-2014, Felix Schill
All rights reserved.
Refer to the file LICENSE.TXT which should be included in all distributions of this project.
'''
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
from threading import Thread
from multiprocessing import Queue
class HorizontalBar(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__( self, parent=parent)
self.items=[]
self.layout=QtGui.QHBoxLayout()
self.setLayout(self.layout)
def add(self, widget, signal, action):
self.layout.addWidget(widget)
self.items.append(widget)
self.connect(widget, QtCore.SIGNAL(signal), action)
class PlainComboField(QtGui.QComboBox):
def __init__(self, parent=None, label="", value=None, choices=None, onOpenCallback=None):
QtGui.QComboBox.__init__( self, parent=parent)
self.choices = choices
self.onOpenCallback = onOpenCallback
if not value in choices:
self.choices.append(value)
for t in choices:
self.addItem(QString(str(t)))
if value!=None:
self.setCurrentIndex(self.choices.index(value))
self.combo=self
def updateValue(self, value):
if value!=None:
self.combo.setCurrentIndex(self.choices.index(value))
def showPopup(self):
if self.onOpenCallback!=None:
self.onOpenCallback()
QtGui.QComboBox.showPopup(self)
def updateChoices(self, choices):
changed=False
for mc,nc in zip(self.choices, choices):
if mc != nc:
changed= True
if not changed:
return
self.clear()
self.choices = choices
for t in choices:
self.addItem(QString(t))
class LabeledComboField(QtGui.QWidget):
def __init__(self, parent=None, label="", value=None, choices=None):
QtGui.QWidget.__init__( self, parent=parent)
self.layout = QtGui.QHBoxLayout()
self.setLayout(self.layout)
self.label=QtGui.QLabel(label)
self.layout.addWidget(self.label)
self.combo=QtGui.QComboBox(parent=self)
for t in choices:
self.combo.addItem(QString(t))
if value!=None:
self.combo.setCurrentIndex(choices.index(value))
self.layout.addWidget(self.combo)
def updateValue(self, value):
if value!=None:
self.combo.setCurrentIndex(choices.index(value))
class LabeledTextField(QtGui.QWidget):
def __init__(self, parent=None, editable=True, label="", value=None, formatString="{:s}"):
QtGui.QWidget.__init__( self, parent=parent)
self.layout = QtGui.QHBoxLayout()
self.setLayout(self.layout)
self.formatString=formatString
self.editable=editable
self.label=QtGui.QLabel(label)
self.layout.addWidget(self.label)
self.text=QtGui.QLineEdit(parent=self)
self.text.setReadOnly(not self.editable)
if value!=None:
self.text.setText(formatString.format(value))
self.layout.addWidget(self.text)
def updateValue(self, value):
if value!=None:
# check if value is a multi-value object:
if isinstance(value, (list, frozenset, tuple, set, bytearray)):
self.text.setText(''.join(self.formatString.format(x) for x in value))
else:
self.text.setText(self.formatString.format(value))
class LabeledProgressField(QtGui.QWidget):
def __init__(self, parent=None, label="", value=None, min=None, max=None, step=1.0):
QtGui.QWidget.__init__( self, parent=parent)
self.layout = QtGui.QHBoxLayout()
self.setLayout(self.layout)
self.label=QtGui.QLabel(label)
self.layout.addWidget(self.label)
self.progress=QtGui.QProgressBar(parent=self)
self.updateValue(value=value, min=min, max=max)
self.layout.addWidget(self.progress)
def updateValue(self, value, min, max):
self.progress.setMinimum(min)
self.progress.setMaximum(max)
self.progress.setValue(value)
class LabeledFileField(QtGui.QWidget):
def __init__(self, parent=None, editable=True, label="", value=None, fileSelectionPattern="All files (*.*)"):
QtGui.QWidget.__init__( self, parent=parent)
self.layout = QtGui.QHBoxLayout()
self.setLayout(self.layout)
self.fileSelectionPattern=fileSelectionPattern
self.editable=editable
self.label=QtGui.QLabel(label)
self.layout.addWidget(self.label)
self.text=QtGui.QLineEdit(parent=self)
self.text.setReadOnly(not self.editable)
if value!=None:
self.text.setText(formatString.format(value))
self.layout.addWidget(self.text)
self.fileDialogButton=QtGui.QPushButton("Select...")
self.connect(self.fileDialogButton, SIGNAL("clicked()"), self.showDialog)
self.layout.addWidget(self.fileDialogButton)
def updateValue(self, value):
if value!=None:
self.text.setText(value)
def showDialog(self):
filename=QtGui.QFileDialog.getOpenFileName(self, 'Open file', '', self.fileSelectionPattern)
if filename!=None:
self.updateValue(filename)
class LabeledNumberField(QtGui.QWidget):
def __init__(self, parent=None, label="", min=None, max=None, value=0, step=1.0):
QtGui.QWidget.__init__( self, parent=parent)
self.layout = QtGui.QHBoxLayout()
self.setLayout(self.layout)
self.label=QtGui.QLabel(label)
self.layout.addWidget(self.label)
self.number=QtGui.QDoubleSpinBox(parent=self)
if min!=None:
self.number.setMinimum(min)
else:
self.number.setMinimum(-10000000)
if max!=None:
self.number.setMaximum(max)
else:
self.number.setMaximum(10000000)
self.number.setSingleStep(step);
self.number.setValue(value)
self.layout.addWidget(self.number)
def updateValue(self, value):
self.number.setValue(value)
class ToolPropertyWidget(QtGui.QWidget):
def updateParameter(self, object=None, newValue=None):
object.updateValue(newValue)
def __init__(self, parent, tool):
QtGui.QWidget.__init__( self, parent=parent)
self.layout = QtGui.QVBoxLayout()
self.setLayout(self.layout)
self.parameters=dict()
# get editable parameters
for object in tool.parameters:
p=object
if object.__class__.__name__=="TextParameter":
w=LabeledTextField(parent=self, label=object.name, editable=object.editable, formatString=object.formatString)
self.parameters[p]=w
w.updateValue(object.value)
self.layout.addWidget(w)
if object.editable:
self.connect(w.text, SIGNAL("textChanged(QString)"), object.updateValue)
if object.__class__.__name__=="FileParameter":
w=LabeledFileField(parent=self, label=object.name, editable=object.editable, fileSelectionPattern=object.fileSelectionPattern)
self.parameters[p]=w
w.updateValue(object.value)
self.layout.addWidget(w)
if object.editable:
self.connect(w.text, SIGNAL("textChanged(QString)"), object.updateValue)
if object.__class__.__name__=="NumericalParameter":
w=LabeledNumberField(parent=self, label=object.name, min=object.min, max=object.max, value=object.getValue(), step=object.step)
self.parameters[p]=w
self.layout.addWidget(w)
if object.editable:
self.connect(w.number, SIGNAL("valueChanged(double)"), object.updateValue)
if object.__class__.__name__=="ProgressParameter":
w=LabeledProgressField(parent=self, label=object.name, min=object.min, max=object.max, value=object.getValue())
self.parameters[p]=w
self.layout.addWidget(w)
if object.__class__.__name__=="ChoiceParameter":
w=LabeledComboField(parent=self, label=object.name, value=object.getChoiceStrings()[object.choices.index(object.getValue())], choices=object.getChoiceStrings())
self.parameters[p]=w
self.layout.addWidget(w)
if object.editable:
self.connect(w.combo, SIGNAL("currentIndexChanged(QString)"), object.updateValueByString)
if object.__class__.__name__=="ActionParameter":
w=QtGui.QPushButton(object.name)
self.parameters[p]=w
self.layout.addWidget(w)
self.connect(w, SIGNAL("clicked()"), object.callback)
self.layout.addStretch()
def update(self):
# get editable parameters
for object in self.parameters.keys():
w=self.parameters[object]
if object.__class__.__name__=="TextParameter":
w.updateValue(object.value)
if object.__class__.__name__=="FileParameter":
w.updateValue(object.value)
if object.__class__.__name__=="ProgressParameter":
w.updateValue(value=object.value, min=object.min, max=object.max)
if object.__class__.__name__=="NumericalParameter":
w.updateValue(object.value)
if object.__class__.__name__=="ChoiceParameter":
w.updateValue(object.value)
if object.__class__.__name__=="ActionParameter":
None
class ItemListModel(QAbstractListModel):
def __init__(self, itemlist, parent=None, *args):
QAbstractListModel.__init__(self, parent, *args)
self.listdata = itemlist
def rowCount(self, parent=QModelIndex()):
return len(self.listdata)
def data(self, index, role):
if not (index.isValid() and index.row()<len(self.listdata)):
return None
if role == Qt.DisplayRole:
return self.listdata[index.row()].name.value
if role == Qt.CheckStateRole:
if len(self.listdata)>0 and self.listdata[index.row()].selected:
return Qt.Checked
else:
return Qt.Unchecked
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
if role == Qt.CheckStateRole:
if index.row()<len(self.listdata):
self.listdata[index.row()].selected=(not self.listdata[index.row()].selected)
self.dataChanged.emit(index, index)
return True
return False
def addItem(self, newItem):
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()+1)
self.listdata.append(newItem)
self.endInsertRows()
return self.index(self.rowCount()-1)
def removeRows(self, row, count, parent):
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()+1)
for i in reversed(range(row, row+count)):
del self.listdata[i]
self.endInsertRows()
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable | Qt.ItemIsUserCheckable
class ListWidget(QtGui.QWidget):
def __init__(self, parent=None, title="", itemlist=[], itemclass=None, on_select_cb=None, **creationArgs):
QtGui.QWidget.__init__( self, parent=parent)
self.creationArgs=creationArgs
self.on_select_cb=on_select_cb
## Create a grid layout to manage the widgets size and position
self.layout = QtGui.QGridLayout()
self.setLayout(self.layout)
self.listmodel=ItemListModel(itemlist)
self.itemclass=itemclass
self.listw = QtGui.QListView()
self.listw.setModel(self.listmodel)
self.listw.clicked.connect(self.respondToSelect)
buttonwidget=QtGui.QWidget()
buttonLayout=QtGui.QHBoxLayout()
buttonwidget.setLayout(buttonLayout)
self.addBtn = QtGui.QPushButton('+')
self.removeBtn = QtGui.QPushButton('-')
buttonLayout.addWidget(self.addBtn)
buttonLayout.addWidget(self.removeBtn)
self.layout.addWidget(QtGui.QLabel(title), 0, 0) # button goes in upper-left
self.layout.addWidget(self.listw, 1, 0) # list widget goes in bottom-left
self.layout.addWidget(buttonwidget, 2, 0) # button goes in upper-left
self.connect(self.addBtn, SIGNAL("clicked()"), self.addItem)
self.connect(self.removeBtn, SIGNAL("clicked()"), self.removeItem)
if len(itemlist)>0:
self.selectedTool=itemlist[0]
else:
self.selectedTool=None
if self.selectedTool!=None:
self.propertyWidget=ToolPropertyWidget(parent=self, tool=self.selectedTool)
self.layout.addWidget(self.propertyWidget, 0, 1, 3, 1)
def respondToSelect(self, index):
self.selectedTool=self.listmodel.listdata[index.row()]
if self.selectedTool!=None:
self.propertyWidget=ToolPropertyWidget(parent=self, tool=self.selectedTool)
self.layout.addWidget(self.propertyWidget, 0, 1, 3, 1)
if self.on_select_cb!=None:
self.on_select_cb(self.selectedTool)
def addItem(self, addExistingItems=True, **creationArgs):
if len(creationArgs)==0:
newItem=self.itemclass(**self.creationArgs)
else:
newItem=self.itemclass(**creationArgs)
newName=newItem.name.value
nameExists=False
foundItem=None
for item in self.listmodel.listdata:
if item.name.value==newName:
nameExists=True
foundItem=item
break
if not nameExists or (nameExists and addExistingItems):
counter=1
while newName in [i.name.value for i in self.listmodel.listdata]:
newName="%s - %i"%(newItem.name.value, counter)
counter+=1
newItem.name.updateValue(newName)
# add to list model
addedItem=self.listmodel.addItem(newItem)
self.listw.setCurrentIndex(addedItem)
self.respondToSelect(addedItem)
return newItem
else:
return foundItem
def findItem(self, name):
for item in self.listmodel.listdata:
if name==item.name.value:
return item
return None
def removeItem(self):
itemindex=self.listw.selectedIndexes()
if len(itemindex)==0:
return
self.listmodel.removeRows(itemindex[0].row(), 1, itemindex[0])