-
Notifications
You must be signed in to change notification settings - Fork 5
/
camviewer_ui_impl.py
3079 lines (2785 loc) · 113 KB
/
camviewer_ui_impl.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
# NOTES:
# OK, New regime: all of the rotation is handled by the false coloring processor for the image PV.
# So, now this processor is given an orientation, and everything is automatically rotated with
# x going right and y going down, (0,0) in the upper left corner. The Rect and Point classes in
# the param module automatically deal with absolute image coordinates as well as rotated ones.
#
# So, we only have to deal with true screen coordinates and how the oriented image is mapped to
# this.
#
from __future__ import annotations
from camviewer_ui import Ui_MainWindow
from psp.Pv import Pv
from dialogs import advdialog
from dialogs import markerdialog
from dialogs import specificdialog
from dialogs import forcedialog
import sys
import os
from pycaqtimage import pycaqtimage
import pyca
import math
import re
import time
import functools
import numpy as np
import numpy.typing as npt
import tempfile
import shutil
import typing
import contextlib
import subprocess
from PyQt5.QtWidgets import (
QSizePolicy,
QLabel,
QMainWindow,
QSpacerItem,
QFileDialog,
QMessageBox,
QAction,
QDialogButtonBox,
QApplication,
)
from PyQt5.QtGui import (
QClipboard,
QPixmap,
QDrag,
QImageWriter,
)
from PyQt5.QtCore import (
QTimer,
QPoint,
QSize,
QObject,
QEvent,
Qt,
QMimeData,
QSettings,
pyqtSignal,
)
import param
#
# Utility functions to put/get Pv values.
#
def caput(pvname, value, timeout=1.0):
try:
pv = Pv(pvname, initialize=True)
pv.wait_ready(timeout)
pv.put(value, timeout)
pv.disconnect()
except pyca.pyexc as e:
print("pyca exception: %s" % (e))
except pyca.caexc as e:
print("channel access exception: %s" % (e))
def caget(pvname, timeout=1.0):
try:
pv = Pv(pvname, initialize=True)
pv.wait_ready(timeout)
v = pv.value
pv.disconnect()
return v
except pyca.pyexc as e:
print("pyca exception: %s" % (e))
return None
except pyca.caexc as e:
print("channel access exception: %s" % (e))
return None
#
# A configuration object class.
#
class cfginfo:
def __init__(self):
self.dict = {}
def read(self, name):
try:
cfg = open(name).readlines()
for line in cfg:
line = line.lstrip()
token = line.split()
if len(token) == 2:
self.dict[token[0]] = token[1]
else:
self.dict[token[0]] = token[1:]
return True
except Exception:
return False
def add(self, attr, val):
self.dict[attr] = val
def __getattr__(self, name):
if name in self.dict.keys():
return self.dict[name]
else:
raise AttributeError
class FilterObject(QObject):
def __init__(self, app: QApplication, main: GraphicUserInterface):
QObject.__init__(self, main)
self.app = app
self.clip = app.clipboard()
self.main = main
self.app.installEventFilter(self)
sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
self.renderlabel = QLabel()
self.renderlabel.setSizePolicy(sizePolicy)
self.renderlabel.setMinimumSize(QSize(0, 20))
self.renderlabel.setMaximumSize(QSize(16777215, 100))
self.last = QPoint(0, 0)
def eventFilter(self, obj, event):
if event.type() in [QEvent.MouseButtonPress, QEvent.KeyPress]:
self.main.refresh_rate_timer()
if event.type() == QEvent.MouseButtonPress and event.button() == Qt.MidButton:
p = event.globalPos()
w = self.app.widgetAt(p)
if w == obj and self.last != p:
self.last = p
try:
t = w.writepvname
except Exception:
t = None
if t is None:
try:
t = w.readpvname
except Exception:
t = None
if t is None:
return False
self.clip.setText(t, QClipboard.Selection)
mimeData = QMimeData()
mimeData.setText(t)
self.renderlabel.setText(t)
self.renderlabel.adjustSize()
pixmap = QPixmap(self.renderlabel.size())
self.renderlabel.render(pixmap)
drag = QDrag(self.main)
drag.setMimeData(mimeData)
drag.setPixmap(pixmap)
drag.exec_(Qt.CopyAction)
return False
SINGLE_FRAME = 0
LOCAL_AVERAGE = 2
class GraphicUserInterface(QMainWindow):
# Define our signals.
imageUpdate = pyqtSignal()
miscUpdate = pyqtSignal()
sizeUpdate = pyqtSignal()
cross1Update = pyqtSignal()
cross2Update = pyqtSignal()
cross3Update = pyqtSignal()
cross4Update = pyqtSignal()
retry_save_image = pyqtSignal()
def __init__(
self,
app,
cwd,
instrument,
camera,
cameraPv,
cameraListFilename,
cfgdir,
activedir,
rate,
idle,
min_timeout,
max_timeout,
options,
):
QMainWindow.__init__(self)
self.app = app
self.cwd = cwd
self.rcnt = 0
self.resizing = False
self.cfgdir = cfgdir
self.cfg = None
self.activedir = activedir
self.instrument = instrument
self.description = "%s:%d" % (os.uname()[1], os.getpid())
self.min_timeout = min_timeout
self.max_timeout = max_timeout
self.options = options
if self.options.pos is not None:
try:
p = self.options.pos.split(",")
p = QPoint(int(p[0]), int(p[1]))
self.move(p)
except Exception:
pass
# View parameters
self.viewwidth = 640 # Size of our viewing area.
self.viewheight = 640 # Size of our viewing area.
self.projsize = 300 # Size of our projection window.
self.minwidth = 450
self.minheight = 450
self.minproj = 250
# Default to VGA!
param.setImageSize(640, 480)
self.isColor = False
self.bits = 10
self.maxcolor = 1023
self.lastUpdateTime = time.time()
self.dispUpdates = 0
self.lastDispUpdates = 0
self.average = 1
param.orientation = param.ORIENT0
self.connected = False
self.cameraBase = ""
self.camera = None
self.notify = None
self.haveNewImage = False
self.lastGetDone = True
self.wantNewImage = True
self.lensPv = None
self.putlensPv = None
self.nordPv = None
self.nelmPv = None
self.count = None
self.maxcount = None
self.rowPv = None
self.colPv = None
self.launch_gui_pv = None
self.launch_edm_pv = None
self.launch_gui_script = ""
self.launch_edm_script = ""
self.scale = 1
self.fLensPrevValue = -1
self.fLensValue = 0
self.avgState = SINGLE_FRAME
self.index = -1
self.averageCur = 0
self.iRangeMin = 0
self.iRangeMax = 1023
self.camactions = []
self.lastwidth = 0
self.useglobmarks = False
self.useglobmarks2 = False
self.globmarkpvs = []
self.globmarkpvs2 = []
self.lastimagetime = [0, 0]
self.dispspec = 0
self.otherpvs = []
self.markhash = []
for i in range(131072):
self.markhash.append(8 * [0])
self.itime = 10 * [0.0]
self.idispUpdates = 10 * [0]
self.idataUpdates = 10 * [0]
self.rfshTimer = QTimer()
self.acquire_image_timer = QTimer()
self.rate_limit_timer = QTimer()
self.refresh_timeout_display_timer = QTimer()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.projH.set_x()
self.ui.projV.set_y()
self.RPSpacer = QSpacerItem(20, 0, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.ui.RightPanel.addItem(self.RPSpacer)
self.ui.xmark = [
self.ui.Disp_Xmark1,
self.ui.Disp_Xmark2,
self.ui.Disp_Xmark3,
self.ui.Disp_Xmark4,
]
self.ui.ymark = [
self.ui.Disp_Ymark1,
self.ui.Disp_Ymark2,
self.ui.Disp_Ymark3,
self.ui.Disp_Ymark4,
]
self.ui.pBM = [
self.ui.pushButtonMarker1,
self.ui.pushButtonMarker2,
self.ui.pushButtonMarker3,
self.ui.pushButtonMarker4,
self.ui.pushButtonRoiSet,
]
self.ui.actM = [
self.ui.actionM1,
self.ui.actionM2,
self.ui.actionM3,
self.ui.actionM4,
self.ui.actionROI,
]
self.advdialog = advdialog(self)
self.advdialog.hide()
self.markerdialog = markerdialog(self)
self.markerdialog.xmark = [
self.markerdialog.ui.Disp_Xmark1,
self.markerdialog.ui.Disp_Xmark2,
self.markerdialog.ui.Disp_Xmark3,
self.markerdialog.ui.Disp_Xmark4,
]
self.markerdialog.ymark = [
self.markerdialog.ui.Disp_Ymark1,
self.markerdialog.ui.Disp_Ymark2,
self.markerdialog.ui.Disp_Ymark3,
self.markerdialog.ui.Disp_Ymark4,
]
self.markerdialog.pBM = [
self.markerdialog.ui.pushButtonMarker1,
self.markerdialog.ui.pushButtonMarker2,
self.markerdialog.ui.pushButtonMarker3,
self.markerdialog.ui.pushButtonMarker4,
None,
]
self.markerdialog.hide()
self.specificdialog = specificdialog(self)
self.specificdialog.hide()
self.forcedialog = None
self.haveforce = False
self.lastforceid = ""
# Not sure how to do this in designer, so we put it in the main window.
# Move it to the status bar!
self.ui.statusbar.addWidget(self.ui.labelMarkerInfo)
# This is our popup menu, which we just put into the menubar for convenience.
# Take it out!
self.ui.menuBar.removeAction(self.ui.menuPopup.menuAction())
# Turn off the stuff we don't care about!
self.ui.labelLens.setVisible(False)
self.ui.horizontalSliderLens.setVisible(False)
self.ui.lineEditLens.setVisible(False)
self.ui.groupBoxFits.setVisible(False)
# Resize the main window!
self.ui.display_image.setImageSize(False)
self.iScaleIndex = 0
self.ui.comboBoxScale.currentIndexChanged.connect(
self.onComboBoxScaleIndexChanged
)
self.cameraListFilename = cameraListFilename
if param.orientation & 2:
self.px = np.zeros((param.y), dtype=np.float64)
self.py = np.zeros((param.x), dtype=np.float64)
self.image = np.zeros((param.x, param.y), dtype=np.uint32)
else:
self.px = np.zeros((param.x), dtype=np.float64)
self.py = np.zeros((param.y), dtype=np.float64)
self.image = np.zeros((param.y, param.x), dtype=np.uint32)
self.imageBuffer = pycaqtimage.pyCreateImageBuffer(
self.ui.display_image.image,
self.px,
self.py,
self.image,
param.x,
param.y,
param.orientation,
)
self.updateRoiText()
self.updateMarkerText(True, True, 0, 15)
sizeProjX = QSize(self.viewwidth, self.projsize)
self.ui.projH.doResize(sizeProjX)
sizeProjY = QSize(self.projsize, self.viewheight)
self.ui.projV.doResize(sizeProjY)
self.ui.display_image.doResize(QSize(self.viewwidth, self.viewheight))
self.camconn_pvs: list[Pv] = []
self.update_cam_rate_label(0)
self.updateCameraCombo()
self.ui.checkBoxProjAutoRange.stateChanged.connect(self.onCheckProjUpdate)
self.ui.horizontalSliderRangeMin.valueChanged.connect(
self.onSliderRangeMinChanged
)
self.ui.horizontalSliderRangeMax.valueChanged.connect(
self.onSliderRangeMaxChanged
)
self.ui.lineEditRangeMin.returnPressed.connect(self.onRangeMinTextEnter)
self.ui.lineEditRangeMax.returnPressed.connect(self.onRangeMaxTextEnter)
self.ui.horizontalSliderLens.sliderReleased.connect(self.onSliderLensReleased)
self.ui.horizontalSliderLens.valueChanged.connect(self.onSliderLensChanged)
self.ui.lineEditLens.returnPressed.connect(self.onLensEnter)
self.ui.singleframe.toggled.connect(self.onCheckDisplayUpdate)
self.ui.grayScale.stateChanged.connect(self.onCheckGrayUpdate)
self.ui.local_avg.toggled.connect(self.onCheckDisplayUpdate)
self.ui.comboBoxColor.currentIndexChanged.connect(
self.onComboBoxColorIndexChanged
)
self.hot() # default option
for i in range(4):
self.ui.xmark[i].returnPressed.connect(
functools.partial(self.onMarkerTextEnter, i)
)
self.ui.ymark[i].returnPressed.connect(
functools.partial(self.onMarkerTextEnter, i)
)
self.markerdialog.xmark[i].returnPressed.connect(
functools.partial(self.onMarkerDialogEnter, i)
)
self.markerdialog.ymark[i].returnPressed.connect(
functools.partial(self.onMarkerDialogEnter, i)
)
self.ui.Disp_RoiX.returnPressed.connect(self.onRoiTextEnter)
self.ui.Disp_RoiY.returnPressed.connect(self.onRoiTextEnter)
self.ui.Disp_RoiW.returnPressed.connect(self.onRoiTextEnter)
self.ui.Disp_RoiH.returnPressed.connect(self.onRoiTextEnter)
#
# Special Mouse Mode:
# 1-4: Marker 1-4, 5: ROI
self.iSpecialMouseMode = 0
for i in range(4):
self.ui.pBM[i].clicked.connect(functools.partial(self.onMarkerSet, i))
self.markerdialog.pBM[i].clicked.connect(
functools.partial(self.onMarkerDialogSet, i)
)
self.ui.actM[i].triggered.connect(functools.partial(self.onMarkerTrig, i))
self.ui.pushButtonRoiSet.clicked.connect(self.onRoiSet)
self.ui.pushButtonRoiReset.clicked.connect(self.onRoiReset)
self.ui.actionMS.triggered.connect(self.onMarkerSettingsTrig)
self.ui.actionROI.triggered.connect(self.onRoiTrig)
self.ui.actionResetROI.triggered.connect(self.onRoiReset)
self.ui.actionResetMarkers.triggered.connect(self.onMarkerReset)
self.ui.pushButtonZoomRoi.clicked.connect(self.onZoomRoi)
self.ui.pushButtonZoomIn.clicked.connect(self.onZoomIn)
self.ui.pushButtonZoomOut.clicked.connect(self.onZoomOut)
self.ui.pushButtonZoomReset.clicked.connect(self.onZoomReset)
self.ui.actionZoomROI.triggered.connect(self.onZoomRoi)
self.ui.actionZoomIn.triggered.connect(self.onZoomIn)
self.ui.actionZoomOut.triggered.connect(self.onZoomOut)
self.ui.actionZoomReset.triggered.connect(self.onZoomReset)
self.ui.actionReconnect.triggered.connect(self.on_reconnect)
self.ui.actionForce.triggered.connect(self.on_force_disconnect)
self.rfshTimer.timeout.connect(self.UpdateRate)
self.rfshTimer.start(1000)
self.acquire_image_timer.timeout.connect(self.wantImage)
rate = max(int(rate), 1)
self.user_set_max_image_rate(rate)
self.ui.spinbox_set_max_rate.setValue(rate)
self.ui.spinbox_set_max_rate.valueChanged.connect(self.user_set_max_image_rate)
self.rate_limit_timer.timeout.connect(self.apply_rate_limit)
self.refresh_timeout_display_timer.timeout.connect(self.update_timeout_display)
self.refresh_timeout_display_timer.setInterval(1000 * 20)
self.refresh_timeout_display_timer.start()
self.ui.average.returnPressed.connect(self.onAverageSet)
self.ui.comboBoxOrientation.currentIndexChanged.connect(
self.onOrientationSelect
)
self.ui.orient0.triggered.connect(lambda: self.setOrientation(param.ORIENT0))
self.ui.orient90.triggered.connect(lambda: self.setOrientation(param.ORIENT90))
self.ui.orient180.triggered.connect(
lambda: self.setOrientation(param.ORIENT180)
)
self.ui.orient270.triggered.connect(
lambda: self.setOrientation(param.ORIENT270)
)
self.ui.orient0F.triggered.connect(lambda: self.setOrientation(param.ORIENT0F))
self.ui.orient90F.triggered.connect(
lambda: self.setOrientation(param.ORIENT90F)
)
self.ui.orient180F.triggered.connect(
lambda: self.setOrientation(param.ORIENT180F)
)
self.ui.orient270F.triggered.connect(
lambda: self.setOrientation(param.ORIENT270F)
)
self.setOrientation(param.ORIENT0) # default to use unrotated
self.ui.FileSave.triggered.connect(self.onfileSave)
self.retry_save_image.connect(self.onfileSave)
self.imageUpdate.connect(self.onImageUpdate)
self.miscUpdate.connect(self.onMiscUpdate)
self.sizeUpdate.connect(self.onSizeUpdate)
self.cross1Update.connect(lambda: self.onCrossUpdate(0))
self.cross2Update.connect(lambda: self.onCrossUpdate(1))
self.cross3Update.connect(lambda: self.onCrossUpdate(2))
self.cross4Update.connect(lambda: self.onCrossUpdate(3))
self.ui.showconf.triggered.connect(self.doShowConf)
self.ui.showproj.triggered.connect(self.doShowProj)
self.ui.showmarker.triggered.connect(self.doShowMarker)
self.ui.showexpert.triggered.connect(self.onExpertMode)
self.ui.showspecific.triggered.connect(self.doShowSpecific)
self.ui.actionGlobalMarkers.triggered.connect(self.onGlobMarks)
self.advdialog.ui.showexpert.clicked.connect(self.on_open_expert)
self.onExpertMode()
self.ui.checkBoxProjRoi.stateChanged.connect(self.onGenericConfigChange)
self.ui.checkBoxM1Lineout.stateChanged.connect(self.onGenericConfigChange)
self.ui.checkBoxM2Lineout.stateChanged.connect(self.onGenericConfigChange)
self.ui.checkBoxM3Lineout.stateChanged.connect(self.onGenericConfigChange)
self.ui.checkBoxM4Lineout.stateChanged.connect(self.onGenericConfigChange)
self.ui.radioGaussian.toggled.connect(self.onGenericConfigChange)
self.ui.radioSG4.toggled.connect(self.onGenericConfigChange)
self.ui.radioSG6.toggled.connect(self.onGenericConfigChange)
self.ui.checkBoxFits.stateChanged.connect(self.onCheckFitsUpdate)
self.ui.lineEditCalib.returnPressed.connect(self.onCalibTextEnter)
self.calib = 1.0
self.calibPVName = ""
self.calibPV = None
self.displayFormat = "%12.8g"
self.advdialog.ui.buttonBox.clicked.connect(self.onAdvanced)
self.specificdialog.ui.buttonBox.clicked.connect(self.onSpecific)
self.specificdialog.ui.cameramodeG.currentIndexChanged.connect(
lambda n: self.comboWriteCallback(self.specificdialog.ui.cameramodeG, n)
)
self.specificdialog.ui.gainG.returnPressed.connect(
lambda: self.lineFloatWriteCallback(self.specificdialog.ui.gainG)
)
self.specificdialog.ui.timeG.returnPressed.connect(
lambda: self.lineFloatWriteCallback(self.specificdialog.ui.timeG)
)
self.specificdialog.ui.periodG.returnPressed.connect(
lambda: self.lineFloatWriteCallback(self.specificdialog.ui.periodG)
)
self.specificdialog.ui.runButtonG.clicked.connect(
lambda: self.buttonWriteCallback(self.specificdialog.ui.runButtonG)
)
# set camera pv and start display
self.ui.menuCameras.triggered.connect(self.onCameraMenuSelect)
self.ui.comboBoxCamera.activated.connect(self.onCameraSelect)
# Sigh, we might change this if taking a one-liner!
camera = options.camera
if camera is not None:
try:
cameraIndex = int(camera)
except Exception:
# OK, I suppose it's a name! Default to 0, then look for it!
cameraIndex = 0
for i in range(len(self.lCameraDesc)):
if self.lCameraDesc[i].find(camera) >= 0:
cameraIndex = i
break
if cameraPv is not None:
try:
idx = self.lCameraList.index(cameraPv)
print("Camera PV %s --> index %d" % (cameraPv, idx))
cameraIndex = idx
except Exception:
# Can't find an exact match. Is this a prefix?
p = re.compile(cameraPv + ".*$")
idx = -1
for i in range(len(self.lCameraList)):
m = p.search(self.lCameraList[i])
if m is not None:
idx = i
break
if idx >= 0:
print("Camera PV %s --> index %d" % (cameraPv, idx))
cameraIndex = idx
else:
# OK, not a prefix. Try stripping off the end and look for
# the same base.
m = re.search("(.*):([^:]*)$", cameraPv)
if m is None:
print("Cannot find camera PV %s!" % cameraPv)
else:
try:
pvname = m.group(1)
pvnamelen = len(pvname)
idx = -1
for i in range(len(self.lCameraList)):
if self.lCameraList[i][:pvnamelen] == pvname:
idx = i
break
if idx <= -1:
raise Exception("No match")
print("Camera PV %s --> index %d" % (cameraPv, idx))
cameraIndex = idx
except Exception:
print("Cannot find camera PV %s!" % cameraPv)
try:
self.ui.comboBoxCamera.setCurrentIndex(-1)
if cameraIndex < 0 or cameraIndex >= len(self.lCameraList):
print("Invalid camera index %d" % cameraIndex)
cameraIndex = 0
self.onCameraSelect(int(cameraIndex))
except Exception:
pass
self.efilter = FilterObject(self.app, self)
def closeEvent(self, event):
self.end_monitors()
if self.cameraBase != "":
self.activeClear()
if self.haveforce and self.forcedialog is not None:
self.forcedialog.close()
self.advdialog.close()
self.markerdialog.close()
self.specificdialog.close()
if self.cfg is None:
self.dumpConfig()
QMainWindow.closeEvent(self, event)
def end_monitors(self):
all_mons = []
all_mons.extend(self.camconn_pvs)
all_mons.extend(self.globmarkpvs)
all_mons.extend(self.globmarkpvs2)
all_mons.extend(self.otherpvs)
all_mons.append(self.notify)
all_mons.append(self.rowPv)
all_mons.append(self.colPv)
all_mons.append(self.launch_gui_pv)
all_mons.append(self.launch_edm_pv)
all_mons.append(self.lensPv)
all_mons.append(self.calibPV)
for pv in all_mons:
if pv is not None:
pv.monitor_stop()
pyca.flush_io()
def setImageSize(self, newx, newy, reset=True):
if newx == 0 or newy == 0:
return
param.setImageSize(newx, newy)
self.ui.display_image.setImageSize(reset)
if param.orientation & 2:
self.px = np.zeros((param.y), dtype=np.float64)
self.py = np.zeros((param.x), dtype=np.float64)
self.image = np.zeros((param.x, param.y), dtype=np.uint32)
else:
self.px = np.zeros((param.x), dtype=np.float64)
self.py = np.zeros((param.y), dtype=np.float64)
self.image = np.zeros((param.y, param.x), dtype=np.uint32)
self.imageBuffer = pycaqtimage.pyCreateImageBuffer(
self.ui.display_image.image,
self.px,
self.py,
self.image,
param.x,
param.y,
param.orientation,
)
if self.camera is not None:
if self.isColor:
self.camera.processor = pycaqtimage.pyCreateColorImagePvCallbackFunc(
self.imageBuffer
)
# self.ui.grayScale.setVisible(True)
else:
self.camera.processor = pycaqtimage.pyCreateImagePvCallbackFunc(
self.imageBuffer
)
# self.ui.grayScale.setVisible(False)
pycaqtimage.pySetImageBufferGray(
self.imageBuffer, self.ui.grayScale.isChecked()
)
def doShowProj(self):
v = self.ui.showproj.isChecked()
self.ui.projH.setVisible(v)
self.ui.projV.setVisible(v)
self.ui.projectionFrame.setVisible(v)
self.ui.groupBoxFits.setVisible(v and self.ui.checkBoxFits.isChecked())
if self.cfg is None:
# print("done doShowProj")
self.dumpConfig()
def doShowMarker(self):
v = self.ui.showmarker.isChecked()
self.ui.groupBoxMarker.setVisible(v)
self.ui.RightPanel.invalidate()
if self.cfg is None:
# print("done doShowMarker")
self.dumpConfig()
def doShowConf(self):
v = self.ui.showconf.isChecked()
self.ui.groupBoxAverage.setVisible(v)
self.ui.groupBoxCamera.setVisible(v)
self.ui.groupBoxColor.setVisible(v)
self.ui.groupBoxZoom.setVisible(v)
self.ui.groupBoxROI.setVisible(v)
self.ui.RightPanel.invalidate()
if self.cfg is None:
# print("done doShowConf")
self.dumpConfig()
def onDropRoiSet(self):
if self.ui.ROI1.isChecked():
self.onSetROI1()
if self.ui.ROI2.isChecked():
self.onSetROI2()
pass
def onDropRoiFetch(self):
if self.ui.ROI1.isChecked():
self.onFetchROI1()
if self.ui.ROI2.isChecked():
self.onFetchROI2()
pass
def onFetchROI1(self):
x = caget(self.cameraBase + ":ROI_X1")
y = caget(self.cameraBase + ":ROI_Y1")
w = caget(self.cameraBase + ":ROI_WIDTH1")
h = caget(self.cameraBase + ":ROI_HEIGHT1")
x -= w / 2
y -= h / 2
self.ui.display_image.roiSet(x, y, w, h)
def onFetchROI2(self):
x = caget(self.cameraBase + ":ROI_X2")
y = caget(self.cameraBase + ":ROI_Y2")
w = caget(self.cameraBase + ":ROI_WIDTH2")
h = caget(self.cameraBase + ":ROI_HEIGHT2")
x -= w / 2
y -= h / 2
self.ui.display_image.roiSet(x, y, w, h)
def getROI(self):
roi = self.ui.display_image.rectRoi.abs()
x = roi.left()
y = roi.top()
w = roi.width()
h = roi.height()
return (int(x + w / 2 + 0.5), int(y + h / 2 + 0.5), int(w), int(h))
def onSetROI1(self):
box = self.getROI()
caput(self.cameraBase + ":ROI_X1", box[0])
caput(self.cameraBase + ":ROI_Y1", box[1])
caput(self.cameraBase + ":ROI_WIDTH1", box[2])
caput(self.cameraBase + ":ROI_HEIGHT1", box[3])
def onSetROI2(self):
box = self.getROI()
caput(self.cameraBase + ":ROI_X2", box[0])
caput(self.cameraBase + ":ROI_Y2", box[1])
caput(self.cameraBase + ":ROI_WIDTH2", box[2])
caput(self.cameraBase + ":ROI_HEIGHT2", box[3])
def onGlobMarks(self):
self.setUseGlobalMarkers(self.ui.actionGlobalMarkers.isChecked())
def setUseGlobalMarkers(self, ugm):
if ugm != self.useglobmarks: # If something has changed...
if ugm:
self.useglobmarks = self.connectMarkerPVs()
if self.useglobmarks:
self.onCrossUpdate(0)
self.onCrossUpdate(1)
else:
self.useglobmarks = self.disconnectMarkerPVs()
if self.cfg is None:
self.dumpConfig()
def setUseGlobalMarkers2(self, ugm):
if ugm != self.useglobmarks2: # If something has changed...
if ugm:
self.useglobmarks2 = self.connectMarkerPVs2()
if self.useglobmarks2:
self.onCrossUpdate(2)
self.onCrossUpdate(3)
else:
self.useglobmarks2 = self.disconnectMarkerPVs2()
if self.cfg is None:
self.dumpConfig()
def onMarkerTextEnter(self, n):
self.ui.display_image.lMarker[n].setRel(
float(self.ui.xmark[n].text()), float(self.ui.ymark[n].text())
)
if n <= 1:
self.updateMarkerText(False, True, 1 << n, 1 << n)
else:
self.updateMarkerText(False, True, 0, 1 << n)
self.updateMarkerValue()
self.updateall()
if self.cfg is None:
self.dumpConfig()
def onMarkerDialogEnter(self, n):
self.ui.display_image.lMarker[n].setRel(
float(self.markerdialog.xmark[n].text()),
float(self.markerdialog.ymark[n].text()),
)
if n <= 1:
self.updateMarkerText(False, True, 1 << n, 1 << n)
else:
self.updateMarkerText(False, True, 0, 1 << n)
self.updateMarkerValue()
self.updateall()
if self.cfg is None:
self.dumpConfig()
def updateMarkerText(self, do_main=True, do_dialog=True, pvmask=0, change=15):
if do_main:
for i in range(4):
if change & (1 << i):
pt = self.ui.display_image.lMarker[i].oriented()
self.ui.xmark[i].setText("%.0f" % pt.x())
self.ui.ymark[i].setText("%.0f" % pt.y())
if do_dialog:
for i in range(4):
if change & (1 << i):
pt = self.ui.display_image.lMarker[i].oriented()
self.markerdialog.xmark[i].setText("%.0f" % pt.x())
self.markerdialog.ymark[i].setText("%.0f" % pt.y())
if self.useglobmarks:
for i in range(2):
if pvmask & (1 << i):
pt = self.ui.display_image.lMarker[i].abs()
newx = int(pt.x())
newy = int(pt.y())
self.globmarkpvs[2 * i + 0].put(newx)
self.globmarkpvs[2 * i + 1].put(newy)
self.updateMarkerValue()
def updateMarkerValue(self):
lValue = pycaqtimage.pyGetPixelValue(
self.imageBuffer,
self.ui.display_image.cursorPos.oriented(),
self.ui.display_image.lMarker[0].oriented(),
self.ui.display_image.lMarker[1].oriented(),
self.ui.display_image.lMarker[2].oriented(),
self.ui.display_image.lMarker[3].oriented(),
)
self.averageCur = lValue[5]
sMarkerInfoText = ""
if lValue[0] >= 0:
pt = self.ui.display_image.cursorPos.oriented()
sMarkerInfoText += "(%d,%d): %-4d " % (pt.x(), pt.y(), lValue[0])
for iMarker in range(4):
if lValue[iMarker + 1] >= 0:
pt = self.ui.display_image.lMarker[iMarker].oriented()
sMarkerInfoText += "%d:(%d,%d): %-4d " % (
1 + iMarker,
pt.x(),
pt.y(),
lValue[iMarker + 1],
)
# Sigh. This is the longest label... if it is too long, the window will resize.
# This would be bad, because the display_image minimum size is small... so we
# need to protect it a bit until things stabilize.
self.ui.labelMarkerInfo.setText(sMarkerInfoText)
def updateall(self):
self.updateProj()
self.updateMiscInfo()
self.ui.display_image.update()
def onCheckProjUpdate(self):
self.updateall()
if self.cfg is None:
self.dumpConfig()
def onCheckGrayUpdate(self, newval):
pycaqtimage.pySetImageBufferGray(self.imageBuffer, newval)
if self.cfg is None:
self.dumpConfig()
def onCheckDisplayUpdate(self, newval):
if not newval:
return # Only do this for the checked one!
if self.ui.singleframe.isChecked():
self.avgState = SINGLE_FRAME
if self.isColor:
pycaqtimage.pySetImageBufferGray(
self.imageBuffer, self.ui.grayScale.isChecked()
)
elif self.ui.local_avg.isChecked():
self.avgState = LOCAL_AVERAGE
self.onAverageSet()
def onCheckFitsUpdate(self):
self.ui.groupBoxFits.setVisible(self.ui.checkBoxFits.isChecked())
if self.cfg is None:
self.dumpConfig()
def onGenericConfigChange(self):
if self.cfg is None:
self.dumpConfig()
def clearSpecialMouseMode(self, keepMode, bNewCheckedState):
for i in range(1, 6):
if keepMode != i:
self.ui.pBM[i - 1].setChecked(False)
if self.markerdialog.pBM[i - 1] is not None:
self.markerdialog.pBM[i - 1].setChecked(False)
self.ui.actM[i - 1].setChecked(False)
if bNewCheckedState:
self.iSpecialMouseMode = keepMode
else:
self.iSpecialMouseMode = 0
if self.iSpecialMouseMode == 0:
self.ui.display_image.setCursor(Qt.ArrowCursor)
else:
self.ui.display_image.setCursor(Qt.CrossCursor)
def onMarkerSet(self, n, bChecked):
self.clearSpecialMouseMode(n + 1, bChecked)
self.ui.actM[n].setChecked(bChecked)
self.markerdialog.pBM[n].setChecked(bChecked)
self.ui.display_image.update()
def onMarkerDialogSet(self, n, bChecked):
self.clearSpecialMouseMode(n + 1, bChecked)
self.ui.actM[n].setChecked(bChecked)
self.ui.pBM[n].setChecked(bChecked)
self.ui.display_image.update()
def onRoiSet(self, bChecked):
self.clearSpecialMouseMode(5, bChecked)
self.ui.actionROI.setChecked(bChecked)
self.ui.display_image.update()
def onMarkerSettingsTrig(self):
self.markerdialog.show()
def onMarkerTrig(self, n):
bChecked = self.ui.actM[n].isChecked()
self.clearSpecialMouseMode(n + 1, bChecked)
self.ui.pBM[n].setChecked(bChecked)
self.markerdialog.pBM[n].setChecked(bChecked)
self.ui.display_image.update()
def onMarkerReset(self):
self.ui.display_image.lMarker = [
param.Point(-100, -100),
param.Point(param.x + 100, -100),
param.Point(param.x + 100, param.y + 100),
param.Point(-100, param.y + 100),
]
self.updateMarkerText(True, True, 3, 15)