forked from Cryostat-GUI/Cryostat-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqlistmodel.py
404 lines (300 loc) · 12.2 KB
/
qlistmodel.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
398
399
400
401
402
403
404
# from PyQt5 import QtWidgets, QtCore, uic
import sys
from copy import deepcopy
from pickle import dumps, load, loads
from PyQt5.QtCore import QTimer
# import math
class PyMimeData(QtCore.QMimeData):
""" The PyMimeData wraps a Python instance as MIME data.
"""
# The MIME type for instances.
MIME_TYPE = 'application/x-ets-qt4-instance'
def __init__(self, data=None, **kwargs):
""" Initialise the instance.
"""
super(PyMimeData, self).__init__(**kwargs)
# Keep a local reference to be returned if possible.
self._local_instance = data
if data is not None:
# We may not be able to pickle the data.
try:
pdata = dumps(data)
except:
return
# This format (as opposed to using a single sequence) allows the
# type to be extracted without unpickling the data itself.
self.setData(self.MIME_TYPE, dumps(data.__class__) + pdata)
@classmethod
def coerce(cls, md):
""" Coerce a QMimeData instance to a PyMimeData instance if possible.
"""
# See if the data is already of the right type. If it is then we know
# we are in the same process.
if isinstance(md, cls):
return md
# See if the data type is supported.
if not md.hasFormat(cls.MIME_TYPE):
return None
nmd = cls()
nmd.setData(cls.MIME_TYPE, md.data())
return nmd
def instance(self):
""" Return the instance.
"""
if self._local_instance is not None:
return self._local_instance
io = StringIO(str(self.data(self.MIME_TYPE)))
try:
# Skip the type.
load(io)
# Recreate the instance.
return load(io)
except:
pass
return None
def instanceType(self):
""" Return the type of the instance.
"""
if self._local_instance is not None:
return self._local_instance.__class__
try:
return loads(str(self.data(self.MIME_TYPE)))
except:
pass
return None
# class Node(object):
# """docstring for Node"""
# def __init__(self, data):
# super(Node, self).__init__()
# self.data = data
class SequenceListModel(QtCore.QAbstractListModel):
sig_send = QtCore.pyqtSignal(list)
def __init__(self, sequence = [], parent = None):
QtCore.QAbstractListModel.__init__(self, parent)
self.__sequence = sequence
# self.countinserted = 0
# self.root = Node(dict(DisplayText='specialnode', arbdata='weha'))
# self.debug_running()
def debug_running(self):
try:
print(self.__sequence)
finally:
QTimer.singleShot(2*1e3,self.debug_running)
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return "Sequence"
else:
return '{}'.format(section+1)
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.__sequence)
# def columnCount(self, parent):
# return 1
def data(self, index, role):
row = index.row()
if role == QtCore.Qt.EditRole:
return self.__sequence[row]['DisplayText']
# if role == QtCore.Qt.ToolTipRole:
# return "Hex code: " + self.__sequence[index.row()].name()
if role == QtCore.Qt.DisplayRole:
value = self.__sequence[row]['DisplayText']
return value
# def setData(self, index, value, role = QtCore.Qt.EditRole):
# if role == QtCore.Qt.EditRole:
# row = index.row()
# self.__sequence[row]['DisplayText'] = value
# self.dataChanged.emit(index, index)
# return True
# return False
def flags(self, index):
if not index.isValid():
return QtCore.Qt.ItemIsEnabled
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable#| \
# QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled
#=====================================================#
#INSERTING & REMOVING
#=====================================================#
def addItem(self, item):
parent=QtCore.QModelIndex()
# print(item, self.rowCount(), self.__sequence)
self.beginInsertRows(parent, self.rowCount(), self.rowCount())
self.__sequence.append(item)
self.endInsertRows()
# print(item, self.rowCount(), self.__sequence)
self.dataChanged.emit(parent, parent)
def clear_all(self):
parent=QtCore.QModelIndex()
self.beginRemoveRows(parent, self.rowCount(), self.rowCount())
self.__sequence = []
self.endRemoveRows()
self.dataChanged.emit(parent, parent)
# ------------------- passing to Gui and writing to file --------------
def pass_data(self):
# print(self.__sequence)
self.sig_send.emit(deepcopy(self.__sequence)) # important for sequence!
return deepcopy(self.__sequence)
# ------------------------ drag'n'drop
# def insertRows(self, position, rows, parent = QtCore.QModelIndex()):
# self.beginInsertRows(parent, position, position + rows - 1)
# for i in range(rows):
# self.__sequence.insert(position, dict(DisplayText='inserted {}'.format(self.countinserted)))
# self.countinserted += 1
# self.endInsertRows()
# self.dataChanged.emit(parent, parent)
# return True
# def removeRows(self, position, rows, parent = QtCore.QModelIndex()):
# self.beginRemoveRows(parent, position, position + rows - 1)
# for i in range(rows):
# value = self.__sequence[position]
# self.__sequence.remove(value)
# self.endRemoveRows()
# return True
# def insertRow(self, row, parent):
# return self.insertRows(row, 1, parent)
# def removeRow(self, row, parentIndex):
# return self.removeRows(row, 1, parentIndex)
# def mimeTypes(self):
# return ['text/xml']
# def mimeData(self, indexes):
# mimedata = QtCore.QMimeData()
# mimedata.setData('text/xml', 'mimeData')
# return mimedata
# def dropMimeData(self, data, action, row, column, parent):
# print ('dropMimeData {} {} {} {}'.format(data.data('text/xml'), action, row, parent) )
# return True
# def mimeTypes(self):
# types = []
# types.append('application/x-ets-qt4-instance')
# return types
# def mimeData(self, index):
# node = self.__sequence[index[0].row()]
# mimeData = PyMimeData(node)
# return mimeData
# def dropMimeData(self, mimedata, action, row, column, parentIndex):
# if action == QtCore.Qt.IgnoreAction:
# return True
# dragNode = mimedata.instance()
# # parentNode = self.nodeFromIndex(parentIndex)
# # make an copy of the node being moved
# newNode = deepcopy(dragNode)
# # newNode.setParent(parentNode)
# self.insertRow(row, parentIndex)
# self.dataChanged.emit(parentIndex, parentIndex)
# return True
# def index(self, row, column, parent):
# node = self.nodeFromIndex(parent)
# return self.createIndex(row, column)
# def nodeFromIndex(self, index):
# return index.internalPointer() if index.isValid() else self.root
class ScanListModel(QtCore.QAbstractListModel):
sig_send = QtCore.pyqtSignal(list)
sig_stepsize = QtCore.pyqtSignal(float)
sig_Nsteps = QtCore.pyqtSignal(int)
def __init__(self, signalreceiver, start=None, end=None, Nsteps=None, SizeSteps=None, **kwargs):
super(ScanListModel, self).__init__(**kwargs)
self.signalreceiver = signalreceiver
self.__sequence = []
self.dic = dict(start=start, end=end, Nsteps=Nsteps, SizeSteps=SizeSteps)
self.updateData(self.dic)
self.signalreceiver.sig_updateScanListModel.connect(self.updateData)
self.dataChanged.emit(QtCore.QModelIndex(), QtCore.QModelIndex())
# self.countinserted = 0
# self.root = Node(dict(DisplayText='specialnode', arbdata='weha'))
# self.debug_running()
def updateData(self, dic):
if dic['SizeSteps']:
self.__sequence = self.Build_Scan_Size(dic['start'], dic['end'], dic['SizeSteps'])
elif dic['Nsteps']:
self.__sequence = self.Build_Scan_N(dic['start'], dic['end'], dic['Nsteps'])
self.debug_running()
def Build_Scan_N(self, start, end, N):
N += 1
stepsize = abs(end-start)/(N-1)
stepsize = abs(stepsize) if start < end else -abs(stepsize)
seq = []
for __ in range(int(N)):
seq.append(start)
start += stepsize
# self.sig_Nsteps.emit(N-1)
self.sig_stepsize.emit(deepcopy(stepsize))
self.dataChanged.emit(QtCore.QModelIndex(), QtCore.QModelIndex())
return seq
def Build_Scan_Size(self, start, end, parameter):
stepsize = abs(parameter) if start < end else -abs(parameter)
seq = []
if start < end:
while start < end:
seq.append(start)
start += stepsize
else:
while start > end:
seq.append(start)
start += stepsize
N = len(seq)
self.sig_Nsteps.emit(deepcopy(N))
# self.sig_stepsize.emit(stepsize)
self.dataChanged.emit(QtCore.QModelIndex(), QtCore.QModelIndex())
return seq
def pass_data(self):
# print(self.__sequence)
self.sig_send.emit(deepcopy(self.__sequence)) # important for sequence!
return deepcopy(self.__sequence)
def debug_running(self):
try:
print(self.__sequence)
finally:
pass
# QTimer.singleShot(2*1e3,self.debug_running)
def data(self, index, role):
row = index.row()
if role == QtCore.Qt.EditRole:
return self.__sequence[row]
if role == QtCore.Qt.ToolTipRole:
return row
if role == QtCore.Qt.DisplayRole:
value = self.__sequence[row]
return value
def setData(self, index, value, role = QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
self.__sequence[row] = value
self.dataChanged.emit(index, index)
return True
return False
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.__sequence)
def flags(self, index):
if not index.isValid():
return QtCore.Qt.ItemIsEnabled
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable# | \
# QtCore.Qt.ItemIsEditable
# QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setStyle("plastique")
#ALL OF OUR VIEWS
listView = QtWidgets.QListView()
listView.show()
# listView.setAcceptDrops(True)
listView.setFrameShape(QtWidgets.QFrame.StyledPanel)
listView.setFrameShadow(QtWidgets.QFrame.Sunken)
# listView.setDragEnabled(True)
# listView.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
# listView.setDefaultDropAction(QtCore.Qt.MoveAction)
listView.setAlternatingRowColors(True)
# listView.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
# listView.setTextElideMode(QtCore.Qt.ElideRight)
# listView.setMovement(QtWidgets.QListView.Free)
# listView.setFlow(QtWidgets.QListView.TopToBottom)
# listView.showDropIndicator()
first = dict(DisplayText='first', arbdata = 'arvb')
second = dict(DisplayText='second', arbdata = 'arvb')
third = dict(DisplayText='third', arbdata = 'arvb')
fourth = dict(DisplayText='fourth', arbdata = 'arvb')
five = dict(DisplayText='five', arbdata = 'arvb')
model = SequenceListModel([first, second, third])
model.addItem(fourth)
# model.addItem(dict(DisplayText='wuha', arbdata='noha'))
listView.setModel(model)
model.pass_data()
sys.exit(app.exec_())