-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_window.py
2905 lines (2597 loc) · 108 KB
/
main_window.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
995
996
997
998
999
1000
from pathlib import Path
import shutil
import copy
import os
import json
import time
import numpy as np
import platform
from PyQt5.QtWidgets import (
QAction, QApplication, QAbstractItemView,
QCheckBox, QComboBox,
QDialog, QDialogButtonBox,
QFileDialog, QFrame,
QGridLayout, QGroupBox,
QHBoxLayout,
QLabel, QLineEdit,
QMainWindow, QMenuBar, QMessageBox,
QPlainTextEdit, QPushButton,
QSizePolicy,
QSpacerItem, QSpinBox, QDoubleSpinBox,
QStatusBar, QStyle, QStyledItemDelegate,
QTableView, QTabWidget, QTextEdit, QToolBar,
QVBoxLayout,
QWidget,
)
from PyQt5.QtCore import (
QAbstractTableModel, QCoreApplication, QObject,
QThread, QSize, QTimer, Qt, qVersion, QSettings,
pyqtSignal
)
from PyQt5.QtGui import QPainter, QPalette, QColor, QCursor, QIcon, QPixmap, QImage
from PyQt5.QtSvg import QSvgRenderer
from PyQt5.QtXml import QDomDocument
from tiff_loader import TiffLoader
from zarr_loader import ZarrLoader
from data_window import DataWindow, SurfaceWindow
from project import Project, ProjectView
from fragment import Fragment, FragmentsModel, FragmentView
from trgl_fragment import TrglFragment, TrglFragmentView
from base_fragment import BaseFragment, BaseFragmentView
from volume import (
Volume, VolumesModel,
DirectionSelectorDelegate,
ColorSelectorDelegate)
from volume_zarr import CachedZarrVolume
from ppm import Ppm
from utils import Utils
class ColorBlock(QLabel):
def __init__(self, color, text=""):
super(ColorBlock, self).__init__()
self.setAutoFillBackground(True)
self.setText(text)
self.setAlignment(Qt.AlignCenter)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
class RoundnessSetter(QGroupBox):
def __init__(self, main_window, parent=None):
super(RoundnessSetter, self).__init__("Skinny border triangles", parent)
self.main_window = main_window
self.min_roundness = 0.
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
# gb = QGroupBox("Skinny border triangles", self)
vlayout = QVBoxLayout()
# gb.setLayout(vlayout)
self.setLayout(vlayout)
self.cb = QCheckBox("Hide skinny triangles")
self.cb.clicked.connect(self.onClicked)
vlayout.addWidget(self.cb)
hlayout = QHBoxLayout()
label = QLabel("Min roundness:")
hlayout.addWidget(label)
self.edit = QLineEdit()
fm = self.edit.fontMetrics()
w = 7*fm.width('0')
self.edit.setFixedWidth(w)
self.edit.editingFinished.connect(self.onEditingFinished)
self.edit.textEdited.connect(self.onTextEdited)
hlayout.addWidget(self.edit)
hlayout.addStretch()
vlayout.addLayout(hlayout)
vlayout.addStretch()
def onClicked(self, s):
# s is bool
# self.main_window.setVolBoxesVisible(s==Qt.Checked)
# print("hide skinny trgls", s)
self.setHideSkinnyTriangles(s)
def setHideSkinnyTriangles(self, state):
self.cb.setChecked(state)
self.main_window.setHideSkinnyTriangles(state)
def getHideSkinnyTriangles(self):
return self.cb.checked()
def setMinRoundness(self, value):
self.min_roundness = value
txt = "%.2f"%value
self.edit.setText(txt)
self.edit.setStyleSheet("")
self.main_window.setMinRoundness(value)
def getMinRoundness(self):
return self.min_roundness
def parseText(self, txt):
valid = True
f = 0
try:
f = float(txt)
except:
valid = False
if f < 0 or f > 1:
valid = False
# f = min(f, 1.)
# f = max(f, 0.)
return valid, f
def onTextEdited(self, txt):
valid, f = self.parseText(txt)
# print("ote", valid)
if valid:
self.edit.setStyleSheet("")
else:
self.edit.setStyleSheet("QLineEdit { color: red }")
def onEditingFinished(self):
txt = self.edit.text()
valid, value = self.parseText(txt)
if not valid:
self.setMinRoundness(self.min_roundness)
return
self.setMinRoundness(value)
# if value != self.min_roundness:
# # print("oef", valid, value)
# self.min_roundness = value
# self.main_window.setMinRoundness(value)
class InfillDialog(QDialog):
def __init__(self, main_window, needs_infill, parent=None):
instructions = "In order to fit the curved fragment surface,\ninfill points are added to the exported mesh.\nThe distance between points is given in voxels.\n16 is a good default.\n0 means do not infill."
super(InfillDialog, self).__init__(parent)
project = main_window.project_view.project
self.ppms = project.ppms
needs_ppm = main_window.canUsePpm()
self.needs_ppm = needs_ppm
self.needs_infill = needs_infill
vlayout = QVBoxLayout()
self.setLayout(vlayout)
if self.needs_infill:
hlayout = QHBoxLayout()
vlayout.addLayout(hlayout)
hlayout.addWidget(QLabel("Infill spacing"))
self.edit = QLineEdit("16")
fm = self.edit.fontMetrics()
w = 7*fm.width('0')
self.edit.setFixedWidth(w)
self.edit.editingFinished.connect(self.onEditingFinished)
self.edit.textEdited.connect(self.onTextEdited)
hlayout.addWidget(self.edit)
hlayout.addStretch()
# self.wlabel = QLabel("")
# vlayout.addWidget(self.wlabel)
self.ilabel = QLabel(instructions)
vlayout.addWidget(self.ilabel)
if self.needs_ppm:
self.apply_ppm_cb = QCheckBox("Export in scroll coordinates")
self.apply_ppm_cb.setChecked(False)
self.apply_ppm_cb.clicked.connect(self.onClicked)
vlayout.addWidget(self.apply_ppm_cb)
hlayout = QHBoxLayout()
vlayout.addLayout(hlayout)
self.ppm_label = QLabel("PPM:")
hlayout.addWidget(self.ppm_label)
self.ppm_label.setEnabled(False)
self.ppm_cb = QComboBox()
for ppm in self.ppms:
self.ppm_cb.addItem(ppm.name)
self.ppm_cb.setCurrentIndex(0)
self.ppm_cb.setEnabled(False)
hlayout.addWidget(self.ppm_cb)
hlayout.addStretch()
bbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
bbox.accepted.connect(self.accepted)
bbox.rejected.connect(self.rejected)
vlayout.addWidget(bbox)
self.is_accepted = False
# print("InfillDialog created")
def onClicked(self, s):
self.ppm_label.setEnabled(s)
self.ppm_cb.setEnabled(s)
def getValue(self):
if not self.needs_infill:
return -1
txt = self.edit.text()
valid, i = self.parseText(txt)
if not valid:
i = -1
# print("getValue", i)
return i
def getPpm(self):
if not self.needs_ppm:
return None
if not self.apply_ppm_cb.isChecked():
return None
return self.ppms[self.ppm_cb.currentIndex()]
def accepted(self):
self.is_accepted = True
value = self.getValue()
if self.needs_infill and value < 0:
self.edit.setText("")
self.wlabel.setText('Please enter a valid value (an integer >= 0)\nor press "Cancel"')
self.wlabel.setStyleSheet("QLabel { color: red; font-weight: bold }")
else:
self.close()
def rejected(self):
self.is_accepted = False
self.close()
def onEditingFinished(self):
txt = self.edit.text()
valid, size = self.parseText(txt)
def parseText(self, txt):
valid = True
i = -1
try:
i = int(txt)
except:
valid = False
if i < 0:
valid = False
return valid, i
def onTextEdited(self, txt):
valid, i = self.parseText(txt)
if valid:
self.edit.setStyleSheet("")
else:
self.edit.setStyleSheet("QLineEdit { color: red }")
class PositionSetter(QWidget):
def __init__(self, main_window, parent=None):
super(PositionSetter, self).__init__(parent)
self.main_window = main_window
hlayout = QHBoxLayout()
self.setLayout(hlayout)
label = QLabel("Set Position:")
hlayout.addWidget(label)
zsetter = QSpinBox()
zsetter.setRange(0, 1000000)
zsetter.setMinimumWidth(80)
ysetter = QSpinBox()
ysetter.setRange(0, 1000000)
ysetter.setMinimumWidth(80)
xsetter = QSpinBox()
xsetter.setRange(0, 1000000)
xsetter.setMinimumWidth(80)
hlayout.addWidget(QLabel("Z:"))
hlayout.addWidget(zsetter)
hlayout.addWidget(QLabel("Y:"))
hlayout.addWidget(ysetter)
hlayout.addWidget(QLabel("X:"))
hlayout.addWidget(xsetter)
button = QPushButton()
button.setText("Move to position")
button.clicked.connect(self.onClicked)
hlayout.addWidget(button)
hlayout.addStretch()
self.zsetter = zsetter
self.ysetter = ysetter
self.xsetter = xsetter
def onClicked(self):
z = self.zsetter.value()
y = self.ysetter.value()
x = self.xsetter.value()
self.main_window.recenterCurrentVolume(np.array([x, y, z]))
class ZInterpolationSetter(QWidget):
def __init__(self, main_window, parent=None):
super(ZInterpolationSetter, self).__init__(parent)
self.main_window = main_window
hlayout = QHBoxLayout()
self.setLayout(hlayout)
label = QLabel("Z interpolation:")
hlayout.addWidget(label)
cb = QComboBox()
cb.addItem("Linear")
cb.addItem("Nearest")
cb.setCurrentIndex(0)
cb.activated.connect(self.onActivated)
hlayout.addWidget(cb)
hlayout.addStretch()
def onActivated(self, index):
self.main_window.setZInterpolation(index)
class CreateFragmentButton(QPushButton):
def __init__(self, main_window, parent=None):
super(CreateFragmentButton, self).__init__("Start New Fragment", parent)
self.main_window = main_window
self.setToolTip("Once the new fragment is created use\nshift plus left mouse button to create new nodes")
self.clicked.connect(self.onButtonClicked)
def onButtonClicked(self, s):
self.main_window.createFragment()
class CopyActiveFragmentButton(QPushButton):
def __init__(self, main_window, parent=None):
# super(CopyActiveFragmentButton, self).__init__("Copy Active Fragment", parent)
super(CopyActiveFragmentButton, self).__init__("Copy", parent)
self.main_window = main_window
self.setStyleSheet("QPushButton { %s; padding: 5; }"%self.main_window.highlightedBackgroundStyle())
self.setEnabled(False)
self.setToolTip("Create a new fragment that is a copy\nof the currently active fragment")
self.clicked.connect(self.onButtonClicked)
def onButtonClicked(self, s):
self.main_window.copyActiveFragment()
class MoveActiveFragmentAlongZButton(QPushButton):
def __init__(self, main_window, text, step, parent=None):
super(MoveActiveFragmentAlongZButton, self).__init__(text, parent)
self.main_window = main_window
self.step = step
self.setStyleSheet("QPushButton { %s; padding: 5; }"%self.main_window.highlightedBackgroundStyle())
self.setEnabled(False)
# up and down are opposite signs to what you might expect
if step > 0:
self.setToolTip("Move active fragment %d pixel(s) down"%step)
else:
self.setToolTip("Move active fragment %d pixel(s) up"%(-step))
self.clicked.connect(self.onButtonClicked)
def onButtonClicked(self, s):
self.main_window.moveActiveFragmentAlongZ(self.step)
class MoveActiveFragmentAlongNormalsButton(QPushButton):
def __init__(self, main_window, text, step, parent=None):
super(MoveActiveFragmentAlongNormalsButton, self).__init__(text, parent)
self.main_window = main_window
self.step = step
self.setStyleSheet("QPushButton { %s; padding: 5; }"%self.main_window.highlightedBackgroundStyle())
self.setEnabled(False)
# up and down are opposite signs to what you might expect
if step > 0:
self.setToolTip("Move active fragment %d pixel(s) downwards along normals"%step)
else:
self.setToolTip("Move active fragment %d pixel(s) upwards along normals"%(-step))
self.clicked.connect(self.onButtonClicked)
def onButtonClicked(self, s):
self.main_window.moveActiveFragmentAlongNormals(self.step)
class LiveZsurfUpdateButton(QPushButton):
def __init__(self, main_window, parent=None):
super(LiveZsurfUpdateButton, self).__init__("", parent)
self.main_window = main_window
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
self.setStyleSheet("QPushButton {padding: 5}")
# self.setCheckable(True)
self.checked = False
self.setText("LU")
self.clicked.connect(self.onButtonClicked)
self.setChecked(self.main_window.live_zsurf_update)
def onButtonClicked(self, s):
self.setChecked(not self.checked)
'''
self.checked = not self.checked
self.main_window.setLiveZsurfUpdate(self.checked)
if self.checked:
self.setStyleSheet("QPushButton {padding: 5}")
else:
self.setStyleSheet("QPushButton { background-color: red ; padding: 5 }")
self.doSetToolTip()
'''
def doSetToolTip(self):
main_str = "Live Update mode:\nSets whether data slices will be updated in real time\nas nodes are modified"
add_strs = [
"\n(Currently not in live-update mode)",
"\n(Currently in live-update mode)",
]
self.setToolTip(main_str+add_strs[int(self.checked)])
def setChecked(self, flag):
# super(LiveZsurfUpdateButton, self).setChecked(flag)
self.checked = flag
self.main_window.setLiveZsurfUpdate(self.checked)
if self.checked:
self.setStyleSheet("QPushButton {padding: 5}")
else:
self.setStyleSheet("QPushButton { background-color: red ; padding: 5 }")
self.doSetToolTip()
class CursorModeButton(QPushButton):
def __init__(self, main_window, parent=None):
super(CursorModeButton, self).__init__("", parent)
self.main_window = main_window
self.setCheckable(True)
args = QCoreApplication.arguments()
path = os.path.dirname(os.path.realpath(args[0]))
# print("path is", path, args[0])
# crosshair.svg is from https://iconduck.com/icons/14824/crosshair
self.cross = QIcon(path+"/icons/crosshair.svg")
self.setIcon(self.cross)
self.setChecked(self.main_window.add_node_mode)
# self.doSetToolTip()
self.clicked.connect(self.onButtonClicked)
# print("cursor mode button")
def onButtonClicked(self, s):
# self.add_node_mode = not self.add_node_mode
self.main_window.add_node_mode = self.isChecked()
self.doSetToolTip()
# print("add node mode:", self.main_window.add_node_mode)
# if self.add_node_mode:
# self.setIcon(self.arrow)
# else:
# self.setIcon(self.cross)
def doSetToolTip(self):
main_str = "Sets whether cursor is in add-node mode or panning mode"
add_strs = [
"\n(Currently in panning mode)",
"\n(Currently in add-node mode)" ]
self.setToolTip(main_str+add_strs[self.main_window.add_node_mode])
def setChecked(self, flag):
super(CursorModeButton, self).setChecked(flag)
self.doSetToolTip()
class VolBoxesVisibleCheckBox(QCheckBox):
def __init__(self, main_window, parent=None):
super(VolBoxesVisibleCheckBox, self).__init__("Volume Boxes Visible", parent)
self.main_window = main_window
# at the time this widget is created, state is unknown,
# so no use to check it
# self.setChecked(state)
self.stateChanged.connect(self.onStateChanged)
def onStateChanged(self, s):
self.main_window.setVolBoxesVisible(s==Qt.Checked)
class TrackingCursorsVisibleCheckBox(QCheckBox):
def __init__(self, main_window, parent=None):
super(TrackingCursorsVisibleCheckBox, self).__init__("Show tracking cursors", parent)
self.main_window = main_window
# at the time this widget is created, state is unknown,
# so no use to check it
# self.setChecked(state)
self.stateChanged.connect(self.onStateChanged)
self.setting = "tracking_cursors"
self.param = "show"
self.setChecked(main_window.draw_settings[self.setting][self.param])
main_window.draw_settings_widgets[self.setting][self.param] = self
def onStateChanged(self, s):
# self.main_window.setVolBoxesVisible(s==Qt.Checked)
self.main_window.setTrackingCursorsVisible(s==Qt.Checked)
def updateValue(self, value):
self.setChecked(value)
class VoxelSizeEditor(QWidget):
def __init__(self, main_window, parent=None):
super(VoxelSizeEditor, self).__init__(parent)
self.main_window = main_window
# print("widget margins", self.contentsMargins())
layout = QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
# print("layout margins", layout.contentsMargins())
# print("layout spacing", layout.spacing())
# layout.setSpacing(0)
self.setLayout(layout)
self.edit = QLineEdit()
fm = self.edit.fontMetrics()
w = 7*fm.width('0')
self.edit.setFixedWidth(w)
self.edit.editingFinished.connect(self.onEditingFinished)
self.edit.textEdited.connect(self.onTextEdited)
self.setToVoxelSize()
layout.addWidget(self.edit)
label = QLabel("Voxel size in μm")
layout.addWidget(label)
layout.addStretch()
def setToVoxelSize(self):
voxel_size_um = self.main_window.getVoxelSizeUm()
txt = self.floatToText(voxel_size_um)
self.edit.setText(txt)
self.onTextEdited(txt)
def floatToText(self, value):
return "%.3f"%value
def onEditingFinished(self):
txt = self.edit.text()
valid, size = self.parseText(txt)
# print("oef", valid, size)
if valid:
self.main_window.setVoxelSizeUm(size)
def parseText(self, txt):
valid = True
f = 0
try:
f = float(txt)
except:
valid = False
if f <= 0:
valid = False
return valid, f
def onTextEdited(self, txt):
valid, f = self.parseText(txt)
# print("ote", valid)
if valid:
self.edit.setStyleSheet("")
else:
self.edit.setStyleSheet("QLineEdit { color: red }")
class ZarrMaxWindowWidthEditor(QWidget):
def __init__(self, main_window, parent=None):
super(ZarrMaxWindowWidthEditor, self).__init__(parent)
self.main_window = main_window
layout = QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
self.setLayout(layout)
self.edit = QLineEdit()
fm = self.edit.fontMetrics()
w = 7*fm.width('0')
self.edit.setFixedWidth(w)
self.edit.editingFinished.connect(self.onEditingFinished)
self.edit.textEdited.connect(self.onTextEdited)
layout.addWidget(self.edit)
label = QLabel("Zarr max window width")
layout.addWidget(label)
layout.addStretch()
self.setting = "zarr"
self.param = "max_window_width"
self.setToMaxWidth()
main_window.draw_settings_widgets[self.setting][self.param] = self
def setToMaxWidth(self):
zarr_max_window_width = self.main_window.draw_settings[self.setting][self.param]
txt = self.intToText(zarr_max_window_width)
self.edit.setText(txt)
self.onTextEdited(txt)
def intToText(self, value):
return "%d"%value
def onEditingFinished(self):
txt = self.edit.text()
valid, width = self.parseText(txt)
# print("oef", valid, size)
if valid:
self.main_window.setDrawSettingsValue(self.setting, self.param, width)
def parseText(self, txt):
valid = True
i = 0
try:
i = int(txt)
except:
valid = False
if i < 2:
valid = False
return valid, i
def onTextEdited(self, txt):
valid, f = self.parseText(txt)
# print("ote", valid)
if valid:
self.edit.setStyleSheet("")
else:
self.edit.setStyleSheet("QLineEdit { color: red }")
class ZarrMaxCacheGb(QWidget):
def __init__(self, main_window, parent=None):
super(ZarrMaxCacheGb, self).__init__(parent)
self.main_window = main_window
layout = QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
self.setLayout(layout)
self.edit = QLineEdit()
fm = self.edit.fontMetrics()
w = 7*fm.width('0')
self.edit.setFixedWidth(w)
self.edit.editingFinished.connect(self.onEditingFinished)
self.edit.textEdited.connect(self.onTextEdited)
layout.addWidget(self.edit)
label = QLabel("Zarr cache size (Gb)")
layout.addWidget(label)
layout.addStretch()
self.setting = "zarr"
self.param = "max_cache_size_gb"
self.setToMaxCacheSize()
self.warned = False
main_window.draw_settings_widgets[self.setting][self.param] = self
def setToMaxCacheSize(self):
zarr_max_cache_size = self.main_window.draw_settings[self.setting][self.param]
txt = self.floatToText(zarr_max_cache_size)
self.edit.setText(txt)
self.onTextEdited(txt)
def floatToText(self, value):
return "%.1f"%value
def onEditingFinished(self):
txt = self.edit.text()
valid, mem_gb = self.parseText(txt)
print("oef", valid, mem_gb, self.warned)
if valid:
warned = self.warned
if not warned:
self.warned = True
self.main_window.setZarrMaxCacheSize(mem_gb, not warned)
def parseText(self, txt):
valid = True
f = 0
try:
f = float(txt)
except:
valid = False
if f < 2:
valid = False
return valid, f
def onTextEdited(self, txt):
valid, f = self.parseText(txt)
# print("ote", valid)
if valid:
self.edit.setStyleSheet("")
else:
self.edit.setStyleSheet("QLineEdit { color: red }")
class ShiftClicksSpinBox(QSpinBox):
def __init__(self, main_window, parent=None):
super(ShiftClicksSpinBox, self).__init__(parent)
self.main_window = main_window
self.setting = "shift_clicks"
self.param = "count"
self.setMinimum(0)
self.setMaximum(2)
self.setValue(main_window.draw_settings[self.setting][self.param])
self.valueChanged.connect(self.onValueChanged, Qt.QueuedConnection)
main_window.draw_settings_widgets[self.setting][self.param] = self
def onValueChanged(self, value):
self.main_window.setShiftClicksCount(value)
self.lineEdit().deselect()
def updateValue(self, value):
self.setValue(value)
class WidthSpinBox(QSpinBox):
def __init__(self, main_window, name, parent=None):
super(WidthSpinBox, self).__init__(parent)
self.main_window = main_window
self.setting = name
self.param = "width"
self.setMinimum(0)
self.setMaximum(10)
self.setValue(main_window.draw_settings[self.setting][self.param])
self.valueChanged.connect(self.onValueChanged, Qt.QueuedConnection)
main_window.draw_settings_widgets[self.setting][self.param] = self
def onValueChanged(self, value):
self.main_window.setDrawSettingsValue(self.setting, self.param, value)
self.lineEdit().deselect()
def updateValue(self, value):
self.setValue(value)
class OpacitySpinBox(QDoubleSpinBox):
def __init__(self, main_window, name, parent=None):
super(OpacitySpinBox, self).__init__(parent)
self.main_window = main_window
self.setting = name
self.param = "opacity"
self.setMinimum(0.0)
self.setMaximum(1.0)
self.setDecimals(1)
self.setSingleStep(0.1)
self.setValue(main_window.draw_settings[self.setting][self.param])
self.valueChanged.connect(self.onValueChanged, Qt.QueuedConnection)
main_window.draw_settings_widgets[self.setting][self.param] = self
def onValueChanged(self, value):
rvalue = round(value*10)/10.
if rvalue != value:
# print("rvalue",rvalue,"!=","value",value)
self.setValue(rvalue)
self.main_window.setDrawSettingsValue(self.setting, self.param, rvalue)
self.lineEdit().deselect()
def updateValue(self, value):
self.setValue(value)
class ApplyOpacityCheckBox(QCheckBox):
def __init__(self, main_window, name, enabled, parent=None):
super(ApplyOpacityCheckBox, self).__init__(parent)
self.main_window = main_window
self.setting = name
self.param = "apply_opacity"
self.setChecked(main_window.draw_settings[self.setting][self.param])
self.setEnabled(enabled)
self.stateChanged.connect(self.onStateChanged)
main_window.draw_settings_widgets[self.setting][self.param] = self
def onStateChanged(self, s):
self.main_window.setDrawSettingsValue(self.setting, self.param, s==Qt.Checked)
def updateValue(self, value):
self.setChecked(value)
'''
# class function
def widthSpinBoxFactory(parent):
sb = QSpinBox(parent)
sb.minimum = 0
sb.maximum = 10
return sb
# class function
def opacitySpinBoxFactory(parent):
sb = QDoubleSpinBox(parent)
sb.minimum = 0.
sb.maximum = 1.0
sb.decimals = 1
sb.singleStep = 0.1
return sb
'''
class MainWindow(QMainWindow):
appname = "χάρτης"
draw_settings_defaults = {
"node": {
"width": 5,
"opacity": 1.0,
"apply_opacity": True,
},
"free_node": {
"width": 6,
"opacity": 1.0,
"apply_opacity": True,
},
"line": {
"width": 2,
"opacity": 1.0,
"apply_opacity": True,
},
"mesh": {
"width": 1,
"opacity": 1.0,
"apply_opacity": True,
},
"axes": {
"width": 2,
"opacity": 1.0,
"apply_opacity": True,
},
"borders": {
"width": 5,
"opacity": 1.0,
"apply_opacity": True,
},
"labels": {
"width": 1,
"opacity": 1.0,
"apply_opacity": False,
},
"overlay": {
"opacity": 1.0,
"apply_opacity": True,
},
"tracking_cursors": {
"show": False,
},
"shift_clicks": {
"count": 1,
},
"zarr": {
"max_cache_size_gb": 8,
"max_window_width": 480,
},
}
zarr_signal = pyqtSignal(str)
def __init__(self, appname, app):
super(MainWindow, self).__init__()
self.app = app
self.platform = platform.system()
print("Running on", self.platform)
self.is_macos = (self.platform == "Darwin")
self.settings = QSettings(QSettings.IniFormat, QSettings.UserScope, 'khartes.org', 'khartes')
print("Loaded settings from", self.settings.fileName())
qv = [int(x) for x in qVersion().split('.')]
# print("Qt version", qv)
if qv[0] > 5 or qv[0] < 5 or qv[1] < 12:
print("Need to use Qt version 5, subversion 12 or above")
# 5.12 or above is needed for QImage::Format_RGBX64
exit()
self.live_zsurf_update = True
self.setWindowTitle(MainWindow.appname)
self.setMinimumSize(QSize(750,600))
# print("has size:",self.settingsHasSize())
self.settingsApplySizePos()
self.draw_settings = copy.deepcopy(MainWindow.draw_settings_defaults)
self.settingsLoadDrawSettings()
self.draw_settings_widgets = copy.deepcopy(MainWindow.draw_settings_defaults)
# if False, shift lock only requires a single click
# self.shift_lock_double_click = True
grid = QGridLayout()
self.project_view = None
self.cursor_tijk = None
self.cursor_window = None
args = QCoreApplication.arguments()
path = os.path.dirname(os.path.realpath(args[0]))
# https://iconduck.com/icons/163625/openhand
px = QPixmap(path+"/icons/openhand.svg")
# print("px size",px.size())
self.cursor_center = (16, 8)
self.openhand = QCursor(px, *self.cursor_center)
px = QPixmap(path+"/icons/openhand transparent.svg")
# print("px size",px.size())
self.openhand_transparent = QCursor(px, *self.cursor_center)
self.openhand_transparents = self.transparentSvgs(path+"/icons/openhand transparent.svg", 11)
self.openhand_transparent = self.openhand_transparents[0]
# x slice or y slice in data
self.depth = DataWindow(self, 2)
# z slice in data
self.inline = DataWindow(self, 0)
# y slice or x slice in data
self.xline = DataWindow(self, 1)
# slice of data from interpreted surface
self.surface = SurfaceWindow(self)
# GUI panel
self.tab_panel = QTabWidget()
self.tab_panel.setMinimumSize(QSize(200,200))
grid.addWidget(self.xline, 0, 0, 2, 2)
grid.addWidget(self.inline, 2, 0, 2, 2)
grid.addWidget(self.depth, 4, 0, 2, 2)
grid.addWidget(self.surface, 0, 2, 4, 3)
grid.addWidget(self.tab_panel, 4, 2, 2, 3)
# self.edit = QPlainTextEdit(self)
self.edit = QTextEdit(self)
self.edit.setReadOnly(True)
# self.edit.setMaximumSize(QSize(200,300))
grid.addWidget(self.edit, 0, 2, 4, 3)
self.addVolumesPanel()
self.addFragmentsPanel()
self.addSettingsPanel()
self.addDevToolsPanel()
self.addVolumeAnnotationPanel()
widget = QWidget()
widget.setLayout(grid)
self.setCentralWidget(widget)
self.load_hardwired_project_action = QAction("Load hardwired project", self)
self.load_hardwired_project_action.triggered.connect(self.onLoadHardwiredProjectButtonClick)
self.new_project_action = QAction("New project...", self)
self.new_project_action.triggered.connect(self.onNewProjectButtonClick)
self.open_project_action = QAction("Open project...", self)
self.open_project_action.triggered.connect(self.onOpenProjectButtonClick)
self.save_project_action = QAction("Save project", self)
self.save_project_action.triggered.connect(self.onSaveProjectButtonClick)
self.save_project_action.setEnabled(False)
self.save_project_as_action = QAction("Save project as...", self)
self.save_project_as_action.triggered.connect(self.onSaveProjectAsButtonClick)
self.save_project_as_action.setEnabled(False)
self.import_obj_action = QAction("Import OBJ files...", self)
self.import_obj_action.triggered.connect(self.onImportObjButtonClick)
self.import_obj_action.setEnabled(False)
self.import_nrrd_action = QAction("Import NRRD files...", self)
self.import_nrrd_action.triggered.connect(self.onImportNRRDButtonClick)
self.import_nrrd_action.setEnabled(False)
self.import_ppm_action = QAction("Import PPM files...", self)
self.import_ppm_action.triggered.connect(self.onImportPPMButtonClick)
self.import_ppm_action.setEnabled(False)
self.import_tiffs_action = QAction("Create volume from TIFF files...", self)
self.import_tiffs_action.triggered.connect(self.onImportTiffsButtonClick)
self.import_tiffs_action.setEnabled(False)
self.attach_zarr_action = QAction("Attach Zarr/OME/TIFF data store...", self)
self.attach_zarr_action.triggered.connect(self.onAttachZarrButtonClick)
self.attach_zarr_action.setEnabled(False)
self.export_mesh_action = QAction("Export fragment as mesh...", self)
self.export_mesh_action.triggered.connect(self.onExportAsMeshButtonClick)
self.export_mesh_action.setEnabled(False)
self.exit_action = QAction("Exit", self)
self.exit_action.triggered.connect(self.onExitButtonClick)
# Qt trickery to put menu bar and tool bar on same line
self.menu_toolbar = self.addToolBar("Menu")
self.menu_toolbar.setFloatable(False)
self.menu_toolbar.setMovable(False)
# self.menu = self.menuBar()
self.menu = QMenuBar()
self.menu.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
self.menu_toolbar.addWidget(self.menu)
self.file_menu = self.menu.addMenu("&File")
self.file_menu.addAction(self.open_project_action)
self.file_menu.addAction(self.new_project_action)
self.file_menu.addAction(self.save_project_action)
self.file_menu.addAction(self.save_project_as_action)
self.file_menu.addAction(self.import_obj_action)
self.file_menu.addAction(self.import_nrrd_action)
self.file_menu.addAction(self.import_ppm_action)
self.file_menu.addAction(self.import_tiffs_action)
self.file_menu.addAction(self.attach_zarr_action)
self.file_menu.addAction(self.export_mesh_action)
# self.file_menu.addAction(self.load_hardwired_project_action)
self.file_menu.addAction(self.exit_action)
# put space between end of menu bar and start of tool bar
sep = QAction(" ", self)
self.menu.addAction(sep)
sep.setDisabled(True)
self.toolbar = self.addToolBar("Tools")
self.add_node_mode = False
self.add_node_mode_button = CursorModeButton(self)
self.toolbar.addWidget(self.add_node_mode_button)
self.last_shift_time = 0
self.live_zsurf_update_button = LiveZsurfUpdateButton(self)
self.toolbar.addWidget(self.live_zsurf_update_button)
self.toggle_direction_action = QAction("Toggle direction", self)
self.toggle_direction_action.triggered.connect(self.onToggleDirectionButtonClick)
self.next_volume_action = QAction("Next volume", self)
self.next_volume_action.triggered.connect(self.onNextVolumeButtonClick)
self.status_bar = QStatusBar(self)
self.setStatusBar(self.status_bar)
# is this needed?
self.volumes_model = VolumesModel(None, self)
self.tiff_loader = TiffLoader(self)
self.zarr_loader = ZarrLoader(self)