-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimpleQt.py
More file actions
712 lines (520 loc) · 17.6 KB
/
simpleQt.py
File metadata and controls
712 lines (520 loc) · 17.6 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
'''
<svg width="120" height="100">
<rect x="20" y="0" width="100" height="100" stroke=#ffa02f stroke-width="2" fill=#207700 />
<text x="20" y="70" font-size="80" fill=#000000>Qt</text>
<text x="0" y="70" font-size="80" fill=#207700 stroke=#ffa02f>S</text>
<text x="40" y="100" font-size="25" fill=#207700 stroke=#ffa02f>imple</text>
</svg>
simpleQt: simple panel making for ScopePy:
widgets:
label
button
entry
textarea
graph
combobox
MiniConsole
table?
positioners:
frame
SimpleBase
'''
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import ScopePy_graphs as spg
import ScopePy_panels as panel
from ScopePy_graphs import GraphWidget
from ScopePy_channel import ScopePyChannel,plotLineStyle
## [label]
class label:
def __init__(self,frame,name):
self.widget = QLabel(name)
#frame.grid.addWidget(self.widget,row,col)
## [frame]
class frame(QFrame):
def __init__(self,panel):
super(frame, self).__init__()
self.grid = QGridLayout(self)
self.setLayout(self.grid)
self.panel = panel
self.setFrameStyle(QFrame.Panel | QFrame.Raised)
self.setLineWidth(3)
def position(self,lol):
'''
lol: List of lists of widgets
'''
c = 0
for i in lol:
nc = 0
for ni in i:
try:
self.grid.addWidget(ni.widget,c,nc)
except AttributeError:
self.grid.addWidget(ni,c,nc)
nc += 1
c += 1
#panel.PanelBase.initialise
class SimpleBase(panel.PanelBase):
def initialise(self):
self.grid = QGridLayout(self)
self.setLayout(self.grid)
self.init()
def init(self):
pass
def position(self,lol):
'''
lol: List of lists of widgets or frames
'''
c = 0
for i in lol:
nc = 0
for ni in i:
try:
self.grid.addWidget(ni.widget,c,nc)
except AttributeError:
self.grid.addWidget(ni,c,nc)
nc += 1
c += 1
def empty():
"""
Inserts an empty space.
Example:
what you want:
label button
button
button
to make that you would use:
[[label,button],[empty(),button],[empty(),button]]
"""
return QWidget()
## [button]
class button:
"""
the simple button Widget!
args:
frame = frame (self)
row: row on grid layout
col: column on grid layout
name: text on button
Bindings:
bindClicked(function) > runs function when button pressed
This is part of ScopePy
"""
def __init__(self,frame,name):
self.widget = QPushButton(name)
self.button = self.widget
#frame.grid.addWidget(self.button,row,col)
self.panel = frame
def bindClicked(self,function):
self.button.clicked.connect(function)
def disable(self):
self.button.setDisabled(True)
def enable(self):
self.button.setEnabled(True)
def setShortcut(self,cut):
self.widget.setShortcut(QKeySequence(cut))
## [entry]
class entry:
"""
the one line entry Widget!
args:
frame = frame (self)
row: row on grid layout
col: column on grid layout
Bindings:
bindReturn(function) > runs when return pressed > gives your function the arg text.
bindChanged(function) > runs when text changed > gives your function the arg text.
"""
def __init__(self,frame,onlyInt=False,onlyFloat=False):
self.line = QLineEdit()
self.widget = self.line
float_regex = QRegExp(r"[\.0-9\-\+e\*\/]+")
float_validator = QRegExpValidator(float_regex)
int_regex = QRegExp(r"[0-9]+")
int_validator = QRegExpValidator(int_regex)
#frame.grid.addWidget(self.line,row,col)
if onlyFloat:
self.line.setValidator(float_validator)
if onlyInt:
self.line.setValidator(int_validator)
self.panel = frame
def bindReturn(self,function):
self.panel.connect(self.line,SIGNAL('returnPressed()'),self.returnfunc)
self.rf = function
def returnfunc(self):
self.rf(self.line.text())
def bindChanged(self,function):
self.panel.connect(self.line,SIGNAL('textChanged(str)'),function)
def setText(self,text):
self.line.setText(text)
@property
def text(self):
return self.line.text()
## [textarea]
import numpy as np
def lists_to_array(x_data,y_data,x_label,y_label):
nData = len(x_data)
data_array = np.zeros(nData,dtype=[(x_label,float),(y_label,float)])
data_array[x_label] = np.array(x_data)
data_array[y_label] = np.array(y_data)
return data_array
class Textarea:
"""
the multi line Text Area Widget!
Args:
frame = frame (self)
row: row on grid layout
col: column on grid layout
Bindings:
bindChanged(function) > runs when text changed > gives your function the arg text.
"""
def __init__(self,frame):
self.line = QTextEdit()
self.widget = self.line
#frame.grid.addWidget(self.line,row,col)
self.panel = frame
def bindChanged(self,function):
self.panel.connect(self.line,SIGNAL('textChanged(str)'),function)
def setText(self,text):
self.line.setText(text)
@property
def text(self):
return self.line.toPlainText()
@property
def html(self):
return self.line.toHtml()
## [Graph]
class graph:
def __init__(self,frame,linecolor='#ff00ff',fillcolor='#00ff00',marker='',channelname='Firstchannel'):
try:
self.graph = spg.GraphWidget(frame.panel.preferences)
except:
self.graph = spg.GraphWidget(frame.preferences)
self.widget = self.graph
#frame.grid.addWidget(self.graph,row,col)
self.frame = frame
self.channel = {}
self.addChannel(channelname,linecolor,fillcolor,marker)
def addChannel(self,name,linecolor='#ff00ff',fillcolor='#00ff00',marker=''):
linestyle = plotLineStyle(lineColour=linecolor,marker=marker,markerFillColour=fillcolor)
self.channel[name] = ScopePyChannel("channel",linestyle)
self.graph.addChannel(self.channel[name],chunkMode='latest')
self.widget.update()
def addData(self,name: str,datainarray: np.array):
self.channel[name].addData2Channel(datainarray)
self.widget.update()
## combo box
class combobox:
def __init__(self,frame):
self.widget = QComboBox(frame)
self.frame = frame
def addItem(self,item):
self.widget.addItem(item)
def addItems(self,items):
self.widget.addItems(items)
def clear(self):
self.widget.clear()
def insertSpace(self,index):
self.widget.insertSeparator(index)
@property
def currentIndex(self):
return self.widget.currentIndex()
@currentIndex.setter
def currentIndex(self,index):
self.widget.setCurrentIndex(index)
@property
def currentText(self):
#print(self.widget.currentText())
return self.widget.currentText()
@currentText.setter
def currentText(self,value):
items = self.items
if value in items:
self.currentIndex = items.index(value)
def removeItem(self,index):
self.widget.removeItem(index)
@property
def count(self):
return self.widget.count()
@property
def items(self):
"""
Return list of the text of all the items in the Combo box
"""
return [self.widget.itemText(ind) for ind in range(self.count)]
def bindChanged(self,command):
self.frame.connect(self.widget,SIGNAL('currentIndexChanged(int)'),command)
def bindTextChanged(self,command):
self.frame.connect(self.widget,SIGNAL('currentIndexChanged(str)'),command)
## Mini Console
class MiniConsole(QWidget):
def __init__(self,commands={}):
QWidget.__init__(self)
self.commands = commands
self.commands['echo'] = self.echo
#.args = args
self.completer = QCompleter(list(self.commands.keys()))
self.edit = QLineEdit()
self.edit.setCompleter(self.completer)
l = QVBoxLayout(self)
self.label = QTextEdit()
self.label.setReadOnly(True)
l.addWidget(self.edit)
l.addWidget(self.label)
self.setLayout(l)
self.edit.keyPressEvent = self.press
self.history = []
self.hnum = 0
self._locals={}
self.setSizePolicy(QSizePolicy(QSizePolicy.Minimum,
QSizePolicy.Maximum))
def echo(self,item):
return str(item)
def _check(self,txt):
self.history.append(txt)
if txt.startswith('$'):
a,b = txt.split('=')
if b.startswith('%'):
b = b.replace('%','')
b=float(b)
self._locals[a]=b
self.edit.clear()
return
try:
command, args = txt.split('(')
try:
self.commands[command]
except:
self.label.setText('<font color=#ff0000>No Function named %s</font>' % command)
args=args.replace(')','')
argl = args.split(',')
rrgs = []
for i in argl:
if i.startswith('$'):
rrgs.append(self._locals[i])
elif i.startswith('%'):
i = i.replace('%','')
rrgs.append(float(i))
else:
rrgs.append(i)
try:
c = self.commands[command]
try:
msg = c(*rrgs)
if type(msg) == tuple:
rmsg = str(msg[0])
self._locals[msg[1]]=msg[2]
else:
rmsg = str(msg)
self.label.setText('%s' % rmsg)
except Exception as ec:
self.label.setText('<font color=#ff0000>Error: %s</font>' % ec)
except:
self.label.setText('<font color=#ff0000>No Function named %s</font>' % command)
except:
self.label.setText('<font color=#ff0000>Inproper syntax</font>')
self.edit.clear()
def press(self,event):
if event.key() in (Qt.Key_Enter, Qt.Key_Return):
self._check(self.edit.text())
self.hnum = 0
return
if event.key() == Qt.Key_Up:
self.setHistory()
return
QLineEdit.keyPressEvent(self.edit,event)
def setHistory(self):
try:
self.edit.setText(self.history[self.hnum])
self.hnum += 1
except Exception as ec:
print(ec)
self.hnum = 0
def sizeHint(self):
"""
Set the minimum size
"""
return QSize(7*70,14*8)
## Table Viewer
class TableBase(object):
'''
A base helper-class for TabelArray
'''
def __init__(self,lol=None):
self.rows = []
self.cols = []
if lol != None:
self._data = lol
self.setData(self._data)
else:
self._data = [[]]
self.rows = []
self.cols = []
def setData(self,lol):
self._data = lol
self.rows = []
self.cols = []
for e, i in enumerate(self._data):
self.rows.append(i)
for e,i in enumerate(self.rows[0]):
c = []
for i in self.rows:
c.append(i[e])
self.cols.append(c)
#self.cols.append(col)
def addCol(self,items=0):
self.cols.append(items)
for e, i in enumerate(self.rows):
i.append(items[e])
def addRow(self,items):
self.rows.append(items)
for e, i in enumerate(self.cols):
i.append(items[e])
def getCell(self,row,col):
return self.rows[row][col]
def setCell(self,row,col,value):
self.rows[row].insert(col,value)
self.cols[row].insert(row,value)
import data_sources.data_source_base_library as DS
class TableArray(DS.BaseDatasetTableWrapper):
"""
Wrapper for HDF5/numpy array datasets when sending to the TableEditor
Makes standard functions for accessing rows and columns, inserting,
deleting
"""
def __init__(self,lol):
"""
Create wrapper with the HDF5/numpy array
Inputs
---------
array : HDF5/numpy array
e.g. Meas['Data/Measurement/dataset1']
"""
# Store array
self.array = TableBase(lol)
# Read only flag - for completness with other wrappers
# - prevents insertions and deletions
self.readOnly = False
def rowCount(self):
return len(self.array.rows)
def columnCount(self):
return len(self.array.cols)
def cell(self,row,col):
"""
Return contents of array element given row and column
"""
# TODO : filter infs and NaNs
return self.array.getCell(row,col)
def setCell(self,row,col,value):
"""
Set contents of array element given row and column
"""
self.array.setCell(row,col,value)
def insertRows(self,position,rows=1):
"""
Insert a set of new rows, set to zero
Inputs
---------
position : int
row index where new rows will be inserted
rows : int
Number of rows to be inserted
"""
self.array.addRow()
def removeRows(self,position,rows=1):
"""
Remove a set of rows
Inputs
---------
position : int
row index where rows will be removed
rows : int
Number of rows to be deleted
"""
print('Not Implimented Yet')
#self.array = np.delete(self.array,rows2delete)
def columnHeaders(self):
"""
Return the names of the columns
For recarrays this is the dtype.names field for normal arrays this
returns a list of numbers converted to strings
"""
return list(range(len(self.array.cols)))
def getColumn(self,column_index):
"""
Return a whole column as one numpy array
"""
return np.array(self.array.cols[column_index])
def columnFormat(self,col):
return '%s'
import widgets.table_editor_lib as te
class Table:
def __init__(self,frame,tbarray):
self.frame = frame
self.widget = te.TableEditorWidget()
self.widget.setModel(te.TableEditorModel(tbarray))
def setData(self,tbarray):
self.widget.setModel(te.TableEditorModel(tbarray))
def getSelection(self):
'''
Returns a TableBase of the selection
'''
l = self.widget.getStrSelection()
nl = []
for i in l:
nl.append([i[0],i[1]])
return TableBase(nl)
## Toggle Button
OFF = 'color: #00b599;'
ON = 'color: red;'
class ToggleButton(object):
def __init__(self,parent,txt,state=False):
self.widget = QPushButton(txt,parent)
self.widget.setCheckable(True)
self.panel = parent
self.txt = txt
self.state = state
self.command = None
self.txtdict = {True:None,
False:None}
self.setState(state)
self.widget.toggled.connect(self.onClick)
def setButtonText(self,txt):
self.txt = txt
self.widget.setText(self.txt)
def setButtonTextForState(self,txt,state):
'''
change text on button when state becomes argument "state"
if txt is None, the text won't change when the state is changed.
'''
self.txtdict[state] = txt
def setState(self,state):
'''
state: bool
True is down, False is up
'''
if state:
s = ON
self.widget.setDown(True)
else:
s = OFF
self.widget.setDown(False)
self.widget.setStyleSheet(s)
self.state = state
if self.txtdict[state] != None:
self.widget.setText(self.txtdict[state])
if self.command:
self.command(self.state)
def onClick(self,pressed):
if pressed:
#print('%i was pressed!' % pin)
#print('Is it an out: %s' % str(t))
self.setState(True)
else:
self.setState(False)
def bindStateChanged(self,command):
'''
command: function or method
must take 1 argument: state (True=down, False=up)
'''
self.command = command