-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathquick.py
994 lines (807 loc) · 29 KB
/
quick.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
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
from typing import Optional
import signal
import logging
import sys
from functools import partial
import math
from copy import copy
import click
from qtpy import QtGui
from qtpy import QtWidgets
from qtpy import QtCore
try:
import qdarkstyle
_has_qdarkstyle = True
except ModuleNotFoundError:
_has_qdarkstyle = False
_GTypeRole = QtCore.Qt.ItemDataRole.UserRole
_missing = object()
signal.signal(
signal.SIGINT, signal.SIG_DFL
) # make CTRL+C exit the program successfully
class GStyle(object):
_base_style = """
._OptionLabel {
font-size: 16px;
font: bold;
font-family: monospace;
}
._HelpLabel {
font-family: serif;
font-size: 14px;
}
._InputComboBox{
font-size: 16px;
}
._InputLineEdit{
font-size: 16px;
}
._InputCheckBox{
font-size: 16px;
}
._InputSpinBox{
font-size: 16px;
}
._InputTabWidget{
font: bold;
font-size: 16px;
}
.GListView{
font-size: 16px;
}
QToolTip{
font-family: serif;
}
"""
def __init__(self, style=""):
if not GStyle.check_style(style):
self.text_color = "black"
self.placehoder_color = "#898b8d"
self.stylesheet = (
GStyle._base_style
+ """
._Spliter{
border: 1px inset gray;
}
"""
)
elif style == "qdarkstyle":
self.text_color = "#eff0f1"
self.placehoder_color = "#898b8d"
self.stylesheet = (
qdarkstyle.load_stylesheet_pyqt5()
+ GStyle._base_style
+ """
.GListView{
padding: 5px;
}
._Spliter{
border: 5px solid gray;
}
"""
)
@staticmethod
def check_style(style):
if style == "qdarkstyle":
return _has_qdarkstyle
return False
_gstyle = GStyle()
class GListView(QtWidgets.QListView):
def __init__(self, opt):
super(GListView, self).__init__()
self.nargs = opt.nargs
self._model = GItemModel(
opt.nargs, parent=self, opt_type=opt.type, default=opt.default
)
self.setModel(self._model)
self.delegate = GEditDelegate(self)
self.setItemDelegate(self.delegate)
self.setSelectionMode(
QtWidgets.QAbstractItemView.SelectionMode.ExtendedSelection
)
if self.nargs == -1:
self.keyPressEvent = self.key_press
self.setToolTip(
"'a': add a new item blow the selected one\n"
"'d': delete the selected item"
)
def key_press(self, e):
if self.nargs == -1:
if e.key() == QtCore.Qt.Key.Key_A:
if len(self.selectedIndexes()) == 0:
self._model.insertRow(0)
else:
for i in self.selectedIndexes():
self._model.insertRow(i.row() + 1)
if e.key() == QtCore.Qt.Key.Key_D:
si = self.selectedIndexes()
for i in si:
self._model.removeRow(i.row())
super(GListView, self).keyPressEvent(e)
class GItemModel(QtGui.QStandardItemModel):
def __init__(self, n, parent=None, opt_type=click.STRING, default=None):
super(QtGui.QStandardItemModel, self).__init__(0, 1, parent) # type: ignore
self.type = opt_type
for row in range(n):
if hasattr(default, "__len__"):
assert default is not None
self.insertRow(row, default[row])
else:
self.insertRow(row, default)
def insertRow(self, idx, val=""):
super(GItemModel, self).insertRow(idx)
index = self.index(idx, 0, QtCore.QModelIndex())
if val is None or val == "":
self.setData(
index,
QtGui.QBrush(QtGui.QColor(_gstyle.placehoder_color)),
role=QtCore.Qt.ItemDataRole.ForegroundRole,
)
else:
self.setData(index, val)
def data(self, index, role=QtCore.Qt.ItemDataRole.DisplayRole):
if role == QtCore.Qt.ItemDataRole.DisplayRole:
dstr = QtGui.QStandardItemModel.data(self, index, role)
if dstr == "" or dstr is None:
if isinstance(self.type, click.types.Tuple):
row = index.row()
if 0 <= row < len(self.type.types):
tp = self.type.types[row]
dstr = tp.name
else:
dstr = self.type.name
return dstr
if role == _GTypeRole:
tp = click.STRING
if isinstance(self.type, click.types.Tuple):
row = index.row()
if 0 <= row < len(self.type.types):
tp = self.type.types[row]
elif isinstance(self.type, click.types.ParamType):
tp = self.type
return tp
return QtGui.QStandardItemModel.data(self, index, role)
class GEditDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
tp = index.data(role=_GTypeRole)
if isinstance(tp, click.Path):
led = GLineEdit_path.from_option(tp, parent)
else:
led = QtWidgets.QLineEdit(parent)
led.setPlaceholderText(tp.name)
validator = select_type_validator(tp)
assert validator is not None
led.setValidator(validator)
return led
def setEditorData(self, editor, index):
item_var = index.data(role=QtCore.Qt.ItemDataRole.EditRole)
if item_var is not None:
editor.setText(str(item_var))
def setModelData(self, editor, model, index):
data_str = editor.text()
if data_str == "" or data_str is None:
model.setData(
index,
QtGui.QBrush(QtGui.QColor(_gstyle.placehoder_color)),
role=QtCore.Qt.ItemDataRole.ForegroundRole,
)
else:
model.setData(
index,
QtGui.QBrush(QtGui.QColor(_gstyle.text_color)),
role=QtCore.Qt.ItemDataRole.ForegroundRole,
)
QtWidgets.QStyledItemDelegate.setModelData(self, editor, model, index)
def generate_label(opt):
show_name = getattr(opt, "show_name", _missing)
show_name = opt.name if show_name is _missing else show_name
param = _OptionLabel(show_name)
param.setToolTip(str(getattr(opt, "help", None)))
return param
class GStringLineEditor(click.types.StringParamType):
def to_widget(self, opt, validator=None):
value = _InputLineEdit()
value.setPlaceholderText(self.name)
if opt.default:
value.setText(str(opt.default))
if getattr(opt, "hide_input", False):
value.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
value.setValidator(validator)
def to_command():
return [opt.opts[0], value.text()]
return [value], to_command
class GIntLineEditor(GStringLineEditor):
def to_widget(self, opt):
return GStringLineEditor.to_widget(self, opt, validator=QtGui.QIntValidator())
class GFloatLineEditor(GStringLineEditor):
def to_widget(self, opt):
return GStringLineEditor.to_widget(
self, opt, validator=QtGui.QDoubleValidator()
)
class GFileDialog(QtWidgets.QFileDialog):
def __init__(self, *args, exists=False, file_okay=True, dir_okay=True, **kwargs):
super(GFileDialog, self).__init__(*args, **kwargs)
self.setOption(QtWidgets.QFileDialog.Option.DontUseNativeDialog, True)
self.setLabelText(QtWidgets.QFileDialog.DialogLabel.Accept, "Select")
if (exists, file_okay, dir_okay) == (True, True, False):
self.setFileMode(QtWidgets.QFileDialog.FileMode.ExistingFile)
elif (exists, file_okay, dir_okay) == (False, True, False):
self.setFileMode(QtWidgets.QFileDialog.FileMode.AnyFile)
elif (exists, file_okay, dir_okay) == (True, False, True):
self.setFileMode(QtWidgets.QFileDialog.FileMode.Directory)
elif (exists, file_okay, dir_okay) == (False, False, True):
self.setFileMode(QtWidgets.QFileDialog.FileMode.Directory)
elif exists is True:
self.setFileMode(QtWidgets.QFileDialog.FileMode.ExistingFile)
self.accept = self.accept_all
elif exists is False:
self.setFileMode(QtWidgets.QFileDialog.FileMode.AnyFile)
self.accept = self.accept_all
def accept_all(self):
super(GFileDialog, self).done(QtWidgets.QFileDialog.DialogCode.Accepted)
class GLineEdit_path(QtWidgets.QLineEdit):
def __init__(self, parent=None, exists=False, file_okay=True, dir_okay=True):
super(GLineEdit_path, self).__init__(parent)
self.action = self.addAction(
self.style().standardIcon(QtWidgets.QStyle.StandardPixmap.SP_DirIcon),
QtWidgets.QLineEdit.ActionPosition.TrailingPosition,
)
self.fdlg = lambda: GFileDialog(
self,
"Select File Dialog",
"./",
"*",
exists=exists,
file_okay=file_okay,
dir_okay=dir_okay,
)
self.action.triggered.connect(self.run_dialog)
def run_dialog(self):
dlg = self.fdlg()
if dlg.exec() == QtWidgets.QFileDialog.DialogCode.Accepted:
self.setText(dlg.selectedFiles()[0])
@staticmethod
def from_option(opt, parent=None):
return GLineEdit_path(
parent=parent,
exists=opt.exists,
file_okay=opt.file_okay,
dir_okay=opt.dir_okay,
)
class GPathGLineEdit_path(click.types.Path):
def to_widget(self, opt):
value = GLineEdit_path(
exists=self.exists, file_okay=self.file_okay, dir_okay=self.dir_okay
)
value.setPlaceholderText(self.name)
if opt.default:
value.setText(str(opt.default))
def to_command():
return [opt.opts[0], value.text()]
return [value], to_command
class _GLabeledSlider(QtWidgets.QSlider):
def __init__(self, min, max, val):
super(_GLabeledSlider, self).__init__(QtCore.Qt.Orientation.Horizontal)
self.min, self.max = min, max
self.setMinimum(min)
self.setMaximum(max)
self.setValue(val)
self.label = self.__init_label()
def __init_label(self):
length = max(
[
math.ceil(math.log10(abs(x))) if x != 0 else 1
for x in [self.min, self.max]
]
)
length += 1
return QtWidgets.QLabel("0" * length)
def argument_command(to_command):
def tc():
a = to_command()
return a[1:]
return tc
class GSlider(QtWidgets.QHBoxLayout):
def __init__(self, min=0, max=10, default=None, *args, **kwargs):
super(QtWidgets.QHBoxLayout, self).__init__() # type: ignore
self.min, self.max, self.default = min, max, default
self.label = self.__init_label()
self.slider = self.__init_slider()
self.label.setText(str(self.default))
self.addWidget(self.slider)
self.addWidget(self.label)
def value(self):
return self.slider.value()
def __init_slider(self):
slider = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
slider.setMinimum(self.min)
slider.setMaximum(self.max)
default_val = (self.min + self.max) // 2
if isinstance(self.default, int):
if self.min <= self.default <= self.max:
default_val = self.default
self.default = default_val
slider.setValue(default_val)
slider.valueChanged.connect(lambda x: self.label.setText(str(x)))
return slider
def __init_label(self):
length = max(
[
math.ceil(math.log10(abs(x))) if x != 0 else 1
for x in [self.min, self.max]
]
)
length += 1
return QtWidgets.QLabel("0" * length)
class GIntRangeGSlider(click.types.IntRange):
def to_widget(self, opt):
value = GSlider(min=self.min, max=self.max, default=opt.default)
def to_command():
return [opt.opts[0], str(value.value())]
return [value], to_command
class GIntRangeSlider(click.types.IntRange):
def to_widget(self, opt):
value = QtWidgets.QSlider(QtCore.Qt.Orientation.Horizontal)
value.setMinimum(self.min)
value.setMaximum(self.max)
default_val = (self.min + self.max) // 2
if isinstance(opt.default, int):
if self.min <= opt.default <= self.max:
default_val = opt.default
value.setValue(default_val)
def to_command():
return [opt.opts[0], str(value.value())]
return [value], to_command
class GIntRangeLineEditor(click.types.IntRange):
def to_widget(self, opt):
value = QtWidgets.QLineEdit()
# TODO: set validator
def to_command():
return [opt.opts[0], value.text()]
return [value], to_command
def bool_flag_option(opt):
checkbox = _InputCheckBox(opt.name)
if opt.default:
checkbox.setCheckState(QtCore.Qt.CheckState.Checked)
# set tip
checkbox.setToolTip(opt.help)
def to_command():
if checkbox.checkState():
return [opt.opts[0]]
else:
return opt.secondary_opts
return [checkbox], to_command
class GChoiceComboBox(click.types.Choice):
def to_widget(self, opt):
cb = _InputComboBox()
cb.addItems(self.choices)
def to_command():
return [opt.opts[0], cb.currentText()]
return [cb], to_command
def count_option(opt):
sb = _InputSpinBox()
def to_command():
return [opt.opts[0]] * int(sb.text())
return [sb], to_command
class GTupleGListView(click.Tuple):
def to_widget(self, opt):
view = GListView(opt)
def to_command():
_ = [opt.opts[0]]
for idx in range(view.model.rowCount()):
_.append(view.model.item(idx).text())
return _
return [view], to_command
def multi_text_argument(opt):
value = GListView(opt)
def to_command():
_ = []
for idx in range(value.model.rowCount()):
_.append(value.model.item(idx).text())
return _
# return [QtWidgets.QLabel(opt.name), value], to_command
return [_OptionLabel(opt.name), value], to_command
def select_type_validator(tp: click.types.ParamType) -> Optional[QtGui.QValidator]:
"""select the right validator for `tp`"""
if isinstance(tp, click.types.IntParamType):
return QtGui.QIntValidator()
elif isinstance(tp, click.types.FloatParamType):
return QtGui.QDoubleValidator()
return None
def select_opt_validator(opt):
"""select the right validator for `opt`"""
return select_type_validator(opt.type)
_TO_WIDGET = {
click.types.Choice: GChoiceComboBox,
click.types.Path: GPathGLineEdit_path,
click.types.IntRange: GIntRangeGSlider,
click.types.IntParamType: GIntLineEditor,
click.types.FloatParamType: GFloatLineEditor,
}
def opt_to_widget(opt):
def add_label(ans):
widgets, to_command = ans
widgets.insert(0, generate_label(opt))
return ans
if opt.nargs > 1:
ans = add_label(GTupleGListView.to_widget(opt.type, opt))
elif getattr(opt, "is_bool_flag", False):
ans = bool_flag_option(opt)
elif getattr(opt, "count", False):
ans = add_label(count_option(opt))
else:
for t, w_class in _TO_WIDGET.items():
if isinstance(opt.type, t):
break
else:
w_class = GStringLineEditor
if opt.multiple:
s = GMultiple(w_class, opt)
ans = add_label([[s], s.to_command])
else:
ans = add_label(w_class.to_widget(opt.type, opt))
return ans
class GMultiple(QtWidgets.QGridLayout):
def __init__(self, cl, opt):
super().__init__()
self._class = cl
self._opt = opt
self._to_command = []
self.init_add()
def init_add(self):
try:
iterable = enumerate(self._opt.default)
except:
self.add()
else:
for i, default in iterable:
opt = copy(self._opt)
opt.default = default
self._add(opt, i)
self._opt.default = []
def add(self, button=None):
i = 0 if button is None else button.i + 1
if i < len(self._to_command):
for row_id in range(len(self._to_command), i, -1):
for j in range(3):
w = self.itemAtPosition(row_id - 1, j).widget()
self.addWidget(w, row_id, j, 1, 1)
w.i += 1
self._add(self._opt, i)
def _add(self, opt, i):
w, c = self._class.to_widget(opt.type, opt)
add_button = QtWidgets.QPushButton("+")
add_button.clicked.connect(lambda: self.add(add_button))
remove_button = QtWidgets.QPushButton("-")
remove_button.clicked.connect(lambda: self.remove(remove_button))
for j, w in enumerate([w[0], add_button, remove_button]):
w.i = i
self.addWidget(w, i, j, 1, 1)
self._to_command.insert(i, c)
def remove(self, button):
i = button.i
if i == 0 and len(self._to_command) == 1:
return
was = []
for row_id in range(i, len(self._to_command)):
rws = []
for j in range(3):
w = self.itemAtPosition(row_id, j).widget()
w.i -= 1
rws.append(w)
self.removeWidget(w)
was.append(rws)
for w in was[0]:
w.hide()
del w
was = was[1:]
self._to_command.pop(i)
for row_id, rws in enumerate(was, i):
for j, w in enumerate(rws):
self.addWidget(w, row_id, j, 1, 1)
def to_command(self):
ans = []
for c in self._to_command:
ans.extend(c())
return ans
def _to_widget(opt):
# customed widget
if isinstance(opt.type, click.types.FuncParamType):
if hasattr(opt.type.func, "to_widget"):
return opt.type.func.to_widget()
elif hasattr(opt.type, "to_widget"):
return opt.type.to_widget()
if isinstance(opt, click.core.Argument):
if opt.nargs == 1:
w, tc = opt_to_widget(opt)
return w, argument_command(tc)
elif opt.nargs > 1 or opt.nargs == -1:
return multi_text_argument(opt)
else:
return opt_to_widget(opt)
def layout_append_opts(layout, opts):
params_func = []
widgets = []
i = 0
for i, para in enumerate(opts):
result = _to_widget(para)
assert result is not None
widget, value_func = result
widgets.append(widget)
params_func.append(value_func)
for idx, w in enumerate(widget):
if isinstance(w, QtWidgets.QLayout):
layout.addLayout(w, i, idx)
else:
layout.addWidget(w, i, idx)
return layout, params_func, widgets
def generate_sysargv(cmd_list):
argv_list = []
for name, func_list in cmd_list:
argv_list.append(name)
for value_func in func_list:
argv_list += value_func()
return argv_list
class _Spliter(QtWidgets.QFrame):
def __init__(self, parent=None):
super(_Spliter, self).__init__(parent=parent)
self.setFrameShape(QtWidgets.QFrame.Shape.HLine)
class _InputComboBox(QtWidgets.QComboBox):
pass
class _InputTabWidget(QtWidgets.QTabWidget):
pass
class _HelpLabel(QtWidgets.QLabel):
pass
class _OptionLabel(QtWidgets.QLabel):
pass
class _InputLineEdit(QtWidgets.QLineEdit):
pass
class _InputCheckBox(QtWidgets.QCheckBox):
pass
class _InputSpinBox(QtWidgets.QSpinBox):
pass
class CommandLayout(QtWidgets.QGridLayout):
def __init__(self, func, run_exit, parent_layout=None):
super(CommandLayout, self).__init__()
self.parent_layout = parent_layout
self.func = func
self.run_exit = run_exit
if func.help:
label = _HelpLabel(func.help)
label.setWordWrap(True)
self.addWidget(label, 0, 0, 1, 2)
frame = _Spliter()
self.addWidget(frame, 1, 0, 1, 2)
self.params_func, self.widgets = self.append_opts(self.func.params)
def add_sysargv(self):
if hasattr(self.parent_layout, "add_sysargv"):
assert self.parent_layout is not None
self.parent_layout.add_sysargv()
sys.argv += generate_sysargv([(self.func.name, self.params_func)])
def append_opts(self, opts):
params_func = []
widgets = []
for i, para in enumerate(opts, self.rowCount()):
result = _to_widget(para)
assert result is not None
widget, value_func = result
widgets.append(widget)
params_func.append(value_func)
for idx, w in enumerate(widget):
if isinstance(w, QtWidgets.QLayout):
self.addLayout(w, i, idx)
else:
self.addWidget(w, i, idx)
return params_func, widgets
def generate_cmd_button(self, label, cmd_slot, tooltip=""):
button = QtWidgets.QPushButton(label)
button.setToolTip(tooltip)
button.clicked.connect(self.clean_sysargv)
button.clicked.connect(self.add_sysargv)
button.clicked.connect(cmd_slot)
return button
def add_cmd_button(self, label, cmd_slot, pos=None):
run_button = self.generate_cmd_button(label, cmd_slot)
if pos is None:
pos = self.rowCount() + 1, 0
self.addWidget(run_button, pos[0], pos[1])
def add_cmd_buttons(self, args):
row = self.rowCount() + 1
cmd_layout = QtWidgets.QGridLayout()
cmd_layout.setHorizontalSpacing(20)
cmd_layout.addItem(
QtWidgets.QSpacerItem(
0,
0,
QtWidgets.QSizePolicy.Policy.Minimum,
QtWidgets.QSizePolicy.Policy.Expanding,
),
0,
0,
1,
2,
)
for col, arg in enumerate(args):
button = self.generate_cmd_button(**arg)
cmd_layout.addWidget(button, 1, col)
self.addLayout(cmd_layout, row, 0, 1, 2)
@QtCore.Slot()
def clean_sysargv(self):
sys.argv = []
class RunCommand(QtCore.QRunnable):
def __init__(self, func, run_exit):
super(RunCommand, self).__init__()
self.func = func
self.run_exit = run_exit
@QtCore.Slot()
def run(self):
cmd_str = " ".join(sys.argv)
logging.info(
f"Running: {cmd_str}",
)
try:
self.func(standalone_mode=self.run_exit)
logging.info(f"Successfully executed: {cmd_str}")
except click.exceptions.BadParameter as bpe:
# warning message
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Icon.Warning)
msg.setText(bpe.format_message())
msg.exec()
except Exception as bpe:
logging.error(bpe)
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Icon.Warning)
msg.setText(repr(bpe))
msg.exec()
class GCommand(click.Command):
def __init__(self, new_thread=True, *arg, **args):
super(GCommand, self).__init__(*arg, **args)
self.new_thread = new_thread
class GOption(click.Option):
def __init__(self, *arg, show_name=_missing, **args):
super(GOption, self).__init__(*arg, **args)
self.show_name = show_name
class GuiStream(QtCore.QObject):
textWritten = QtCore.Signal(str)
def flush(self):
pass
def write(self, text):
self.textWritten.emit(str(text))
class OutputEdit(QtWidgets.QTextEdit):
def print(self, text):
cursor = self.textCursor()
cursor.movePosition(QtGui.QTextCursor.MoveOperation.End)
cursor.insertText(text)
self.setTextCursor(cursor)
self.ensureCursorVisible()
class App(QtWidgets.QWidget):
def __init__(
self,
func,
run_exit,
new_thread,
output="gui",
left=10,
top=10,
width=400,
height=140,
):
"""
Parameters
----------
output : str
'gui': [default] redirect screen output to the gui
'term': do nothing
"""
super().__init__()
self.new_thread = new_thread
self.title = func.name
self.func = func
self.initUI(run_exit, QtCore.QRect(left, top, width, height))
self.threadpool = QtCore.QThreadPool()
self.outputEdit = self.initOutput(output)
def initOutput(self, output):
if output == "gui":
sys.stdout = GuiStream()
sys.stderr = sys.stdout
text = OutputEdit()
text.setReadOnly(True)
sys.stdout.textWritten.connect(text.print)
sys.stdout.textWritten.connect(text.show)
return text
else:
return None
def closeEvent(self, event):
app = QtWidgets.QApplication.instance()
app.quit()
def initCommandUI(self, func, run_exit, parent_layout=None):
opt_set = CommandLayout(func, run_exit, parent_layout=parent_layout)
if isinstance(func, click.MultiCommand):
tabs = _InputTabWidget()
for cmd, f in func.commands.items():
sub_opt_set = self.initCommandUI(f, run_exit, parent_layout=opt_set)
tab = QtWidgets.QWidget()
tab.setLayout(sub_opt_set)
tabs.addTab(tab, cmd)
opt_set.addWidget(tabs, opt_set.rowCount(), 0, 1, 2)
# return opt_set
elif isinstance(func, click.Command):
new_thread = getattr(func, "new_thread", self.new_thread)
opt_set.add_cmd_buttons(
args=[
{
"label": "&Run",
"cmd_slot": partial(self.run_cmd, new_thread=new_thread),
"tooltip": "run command",
},
{
"label": "&Copy",
"cmd_slot": self.copy_cmd,
"tooltip": "copy command to clipboard",
},
]
)
return opt_set
def initUI(self, run_exit, geometry):
self.run_exit = run_exit
self.setWindowTitle(self.title)
# self.setGeometry(self.left, self.top, self.width, self.height)
self.setGeometry(geometry)
self.opt_set = self.initCommandUI(
self.func,
run_exit,
)
self.setLayout(self.opt_set)
self.show()
@QtCore.Slot()
def copy_cmd(self):
cb = QtWidgets.QApplication.clipboard()
cb.clear(mode=cb.Mode.Clipboard)
cmd_text = " ".join(sys.argv)
cb.setText(cmd_text, mode=cb.Mode.Clipboard)
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Icon.Information)
msg.setText(f"copy '{cmd_text}' to clipboard")
msg.exec()
def run_cmd(self, new_thread):
runcmd = RunCommand(self.func, self.run_exit)
if new_thread:
self.threadpool.start(runcmd)
else:
runcmd.run()
def gui_it(click_func, style="qdarkstyle", **kargs) -> None:
"""
Parameters
----------
click_func
`new_thread` is used for qt-based func, like matplotlib
"""
global _gstyle
_gstyle = GStyle(style)
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(_gstyle.stylesheet)
# set the default value for argvs
kargs["run_exit"] = kargs.get("run_exit", False)
kargs["new_thread"] = kargs.get("new_thread", False)
ex = App(click_func, **kargs)
sys.exit(app.exec())
def gui_option(**kargs) -> click.core.BaseCommand:
"""decorator for adding '--gui' option to command"""
# TODO: add run_exit, new_thread
def actual_decorator(f: click.core.BaseCommand):
def run_gui_it(ctx, param, value):
if not value or ctx.resilient_parsing:
return
f.params = [p for p in f.params if not p.name == "gui"]
gui_it(f, **kargs)
ctx.exit()
return click.option(
"--gui",
is_flag=True,
callback=run_gui_it,
help="run with gui",
expose_value=False,
is_eager=False,
)(f)
return actual_decorator