forked from mxcube/HardwareObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Qt4_GraphicsLib.py
1634 lines (1394 loc) · 65.9 KB
/
Qt4_GraphicsLib.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
#
# Project: MXCuBE
# https://github.com/mxcube.
#
# This file is part of MXCuBE software.
#
# MXCuBE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MXCuBE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MXCuBE. If not, see <http://www.gnu.org/licenses/>.
"""
Graphics item library:
- GraphicsItem : base class for all items
- GraphicsItemBeam : beam shape
- GraphicsItemPoint : centring point
- GraphicsItemLine : line between two centring points
- GraphicsItemGrid : 2D grid
- GraphicsItemScale : scale
- GraphicsItemOmegaReference : omega rotation line
- GraphicsItemCentringLine : centring lines for 3 click centring
- GraphicsItemMeasureDistance : line to measure distance
- GraphicsItemMeasureAngle : object to measure angle between two lines
- GraphicsItemMeasureArea : item to measure area
- GraphicsSelectTool : item selection tool
- GraphicsItemMoveBeamMark : item to move beam mark
- GraphicsCameraFrame : camera frame
- GraphicsScene : scene where all items are displayed
- GraphicsView : widget that contains GraphicsScene
"""
import copy
import math
import logging
from PyQt4 import QtGui
from PyQt4 import QtCore
from datetime import datetime
import queue_model_objects_v1 as queue_model_objects
SELECTED_COLOR = QtCore.Qt.green
NORMAL_COLOR = QtCore.Qt.yellow
SOLID_LINE_STYLE = QtCore.Qt.SolidLine
class GraphicsItem(QtGui.QGraphicsItem):
"""Base class for all graphics items.
"""
def __init__(self, parent=None, position_x=0, position_y=0):
"""
:param position_x: x coordinate in the scene
:type position_x: int
:param position_y: y coordinate in the scene
:type position_y: int
"""
QtGui.QGraphicsItem.__init__(self)
self.index = None
self.base_color = None
self.used_count = 0
self.pixels_per_mm = [0, 0]
self.start_coord = [0, 0]
self.end_coord = [0, 0]
self.beam_size_mm = [0, 0]
self.beam_size_pix = [0, 0]
self.beam_position = [0, 0]
self.beam_is_rectangle = False
self.rect = QtCore.QRectF(0, 0, 0, 0)
self.display_beam_shape = None
self.setPos(position_x, position_y)
self.setMatrix = QtGui.QMatrix()
self.custom_pen = QtGui.QPen(SOLID_LINE_STYLE)
self.custom_pen.setWidth(1)
self.custom_pen.setColor(QtCore.Qt.white)
self.custom_brush = QtGui.QBrush(SOLID_LINE_STYLE)
brush_color = QtGui.QColor(70, 70, 165)
brush_color.setAlpha(70)
self.custom_brush.setColor(brush_color)
self.custom_brush.setStyle(QtCore.Qt.SolidPattern)
self.setPos(position_x, position_y)
def boundingRect(self):
"""Returns adjusted rect
:returns: QRect
"""
return self.rect.adjusted(0, 0, 40, 40)
def set_size(self, width, height):
"""Sets fixed size
:param width: width
:type width: int
:param height: height
:type height: int
"""
self.rect.setWidth(width)
self.rect.setHeight(height)
def set_start_position(self, position_x, position_y):
"""Sets start position
"""
if (position_x is not None and
position_y is not None):
self.start_coord = [position_x, position_y]
self.scene().update()
def get_start_position(self):
"""Returns start coordinate of the shape
:return: list with two int
"""
return self.start_coord
def set_end_position(self, position_x, position_y):
"""Sets the end position of the item
:param position_x: x position in pix
:type position_x: int
:param position_y: y position in pix
:type position_y: int
"""
if (position_x is not None and
position_y is not None):
self.end_coord = [position_x, position_y]
self.scene().update()
def get_display_name(self):
"""Returns items display name
:returns: str
"""
return "Item %d" % self.index
def get_full_name(self):
"""Returns full name of the item
:returns: str
"""
return self.get_display_name()
def set_base_color(self, color):
"""Sets base color for lines
:param color: color
:type color: QColor
"""
self.base_color = color
def update_item(self):
"""Updates current item. Calls parent scene update method
"""
self.scene().update()
def mousePressEvent(self, event):
"""Emits scene itemClickedSignal to indicate selected item
"""
self.update()
self.scene().itemClickedSignal.emit(self, self.isSelected())
#self.scene().mouseClickedSignal.emit(self.start_coord[0],
# self.start_coord[1],
# True)
def toggle_selected(self):
"""Toggles item selection
"""
self.setSelected(not self.isSelected())
self.update()
def set_beam_info(self, beam_info):
"""Updates beam information
:param beam_info: dictionary with beam parameters
(size_x, size_y, shape)
:type beam_info: dict
"""
self.beam_is_rectangle = beam_info.get("shape") == "rectangular"
self.beam_size_mm[0] = beam_info.get("size_x", 0)
self.beam_size_mm[1] = beam_info.get("size_y", 0)
self.beam_size_pix[0] = int(self.beam_size_mm[0] * self.pixels_per_mm[0])
self.beam_size_pix[1] = int(self.beam_size_mm[1] * self.pixels_per_mm[1])
def set_beam_position(self, beam_position):
"""Sets beam position
"""
self.beam_position = beam_position
def set_pixels_per_mm(self, pixels_per_mm):
"""Sets pixels per mm and updates item
"""
self.pixels_per_mm = pixels_per_mm
self.beam_size_pix[0] = int(self.beam_size_mm[0] * self.pixels_per_mm[0])
self.beam_size_pix[1] = int(self.beam_size_mm[1] * self.pixels_per_mm[1])
self.update_item()
class GraphicsItemBeam(GraphicsItem):
"""Beam base class
"""
def __init__(self, parent, position_x=0, position_y=0):
"""Sets item flag ItemIsMovable
"""
GraphicsItem.__init__(self, parent, position_x=0, position_y=0)
self.beam_is_rectangle = True
self.setFlags(QtGui.QGraphicsItem.ItemIsMovable)
def paint(self, painter, option, widget):
"""Main beam painter method
Draws ellipse or rectangle with a cross in the middle
"""
self.custom_pen.setColor(QtCore.Qt.blue)
painter.setPen(self.custom_pen)
if self.beam_is_rectangle:
painter.drawRect(self.beam_position[0] * self.scene().image_scale - \
self.beam_size_pix[0] / 2 * self.scene().image_scale,
self.beam_position[1] * self.scene().image_scale - \
self.beam_size_pix[1] / 2 * self.scene().image_scale,
self.beam_size_pix[0] * self.scene().image_scale,
self.beam_size_pix[1] * self.scene().image_scale)
else:
painter.drawEllipse(self.beam_position[0] * self.scene().image_scale - \
self.beam_size_pix[0] / 2 * self.scene().image_scale,
self.beam_position[1] * self.scene().image_scale - \
self.beam_size_pix[1] / 2 * self.scene().image_scale,
self.beam_size_pix[0] * self.scene().image_scale,
self.beam_size_pix[1] * self.scene().image_scale)
self.custom_pen.setColor(QtCore.Qt.red)
painter.setPen(self.custom_pen)
painter.drawLine(self.beam_position[0] * self.scene().image_scale - 10,
self.beam_position[1] * self.scene().image_scale,
self.beam_position[0] * self.scene().image_scale + 10,
self.beam_position[1] * self.scene().image_scale)
painter.drawLine(self.beam_position[0] * self.scene().image_scale,
self.beam_position[1] * self.scene().image_scale - 10,
self.beam_position[0] * self.scene().image_scale,
self.beam_position[1] * self.scene().image_scale + 10)
class GraphicsItemInfo(GraphicsItem):
"""Message box for displaying information on the screen
"""
def __init__(self, parent, position_x=0, position_y=0):
"""
"""
GraphicsItem.__init__(self, parent, position_x=0, position_y=0)
self.beam_is_rectangle = True
self.start_coord = [position_x, position_y]
self.setFlags(QtGui.QGraphicsItem.ItemIsMovable)
self.__msg = None
self.__pos_x = None
self.__pos_y = None
self.__display_time = 5
self.__draw_rect = None
def paint(self, painter, option, widget):
"""Main painter class. Draws message box and after display time
hides the message box.
"""
self.custom_pen.setColor(QtCore.Qt.transparent)
painter.setPen(self.custom_pen)
self.custom_brush.setColor(QtGui.QColor(255, 255, 255, 140))
painter.setBrush(self.custom_brush)
font = painter.font()
font.setBold(True)
font.setItalic(True)
font.setPointSize(12)
painter.setFont(font)
if self.__msg:
painter.drawRoundedRect(self.__draw_rect, 10, 10)
self.custom_pen.setColor(QtCore.Qt.black)
painter.setPen(self.custom_pen)
if type(self.__msg) == str:
painter.drawText(self.__pos_x + 5,
self.__pos_y + 20,
self.__msg)
else:
for index, text_line in enumerate(self.__msg):
painter.drawText(self.__pos_x + 5,
self.__pos_y + 20 + 20 * index,
text_line)
time_delta = datetime.now() - self.__created_time
if self.__display_time:
if time_delta.seconds > self.__display_time:
self.hide()
def display_info(self, msg, pos_x, pos_y, hide_msg=True):
"""Shows message on pos_x, pos_y
"""
self.__msg = msg
self.__pos_x = pos_x
self.__pos_y = pos_y
if hide_msg:
self.__created_time = datetime.now()
else:
self.__created_time = None
width = 300
if type(msg) == str:
height = 25
else:
height = 20 * len(msg)
height += 10
self.__draw_rect = QtCore.QRectF(pos_x, pos_y, width, height)
self.show()
self.scene().update()
class GraphicsItemPoint(GraphicsItem):
"""Centred point class.
"""
def __init__(self, centred_position=None, full_centring=True,
position_x=0, position_y=0):
"""
:param: parent
:param centred position: motor positions
:type centred_position: dict with motors positions
:param full_centring: indicates centring method
:type full_centring : bool. True if 3click centring
"""
GraphicsItem.__init__(self, position_x, position_y)
self.__full_centring = full_centring
self.setFlags(QtGui.QGraphicsItem.ItemIsSelectable)
if centred_position is None:
self.__centred_position = queue_model_objects.CentredPosition()
self.__centred_position.centring_method = False
else:
self.__centred_position = centred_position
self.set_size(20, 20)
self.start_coord = [position_x, position_y]
self.setPos(position_x - 10, position_y - 10)
def get_display_name(self):
return "Point %d" % self.index
def get_full_name(self):
full_name = "Point %d" % self.index
try:
full_name += " (kappa: %0.2f phi: %0.2f)" % \
(self.__centred_position.kappa,
self.__centred_position.kappa_phi)
except:
pass
self.setToolTip(full_name)
return full_name
def get_centred_position(self):
return self.__centred_position
def set_centred_position(self, centred_position):
self.__centred_position = centred_position
def paint(self, painter, option, widget):
if option.state & QtGui.QStyle.State_Selected:
self.custom_pen.setColor(SELECTED_COLOR)
self.custom_pen.setWidth(2)
else:
self.custom_pen.setWidth(1)
if self.base_color:
self.custom_pen.setColor(self.base_color)
else:
self.custom_pen.setColor(NORMAL_COLOR)
painter.setPen(self.custom_pen)
painter.drawEllipse(self.rect.left(), self.rect.top(),
20, 20)
painter.drawLine(self.rect.left(), self.rect.top(),
self.rect.right(), self.rect.bottom())
painter.drawLine(self.rect.right(), self.rect.top(),
self.rect.left(), self.rect.bottom())
if self.index:
display_str = str(self.index)
else:
display_str = "#"
if self.isSelected():
display_str += " selected"
painter.drawText(self.rect.right() + 2, self.rect.top(), display_str)
if self.__centred_position.used_for_collection > 0:
painter.drawText(self.rect.right() + 2, self.rect.top() + 10,
"Used %d time(s)" % self.__centred_position.used_for_collection)
def set_start_position(self, position_x, position_y):
if (position_x is not None and
position_y is not None):
self.start_coord = [position_x, position_y]
self.setPos(position_x - 10, position_y - 10)
self.scene().update()
#def get_position(self):
# return self.start_coord[0], self.start_coord[1]
def mouseDoubleClickEvent(self, event):
position = QtCore.QPointF(event.pos())
self.scene().itemDoubleClickedSignal.emit(self)
self.update()
class GraphicsItemLine(GraphicsItem):
"""
Descrip. : Line class.
"""
def __init__(self, cp_start, cp_end):
GraphicsItem.__init__(self)
self.setFlags(QtGui.QGraphicsItem.ItemIsSelectable)
self.__cp_start = cp_start
self.__cp_end = cp_end
self.__num_images = 0
self.__display_overlay = False
self.__fill_alpha = 120
brush_color = QtGui.QColor(70, 70, 165)
brush_color.setAlpha(5)
self.custom_brush.setColor(brush_color)
def set_fill_alpha(self, value):
self.__fill_alpha = value
brush_color = QtGui.QColor(70, 70, 165, self.__fill_alpha)
self.custom_brush.setColor(brush_color)
def set_display_overlay(self, state):
self.__display_overlay = state
def get_display_name(self):
return "Line %d" % self.index
def get_full_name(self):
start_cpos = self.__cp_start.get_centred_position()
end_cpos = self.__cp_end.get_centred_position()
full_name = "Line (points: %d, %d)" % (self.__cp_start.index,
self.__cp_end.index)
try:
full_name += "kappa: %.2f phi: %.2f" % (start_cpos.kappa,
start_cpos.kappa_phi)
except:
pass
self.setToolTip(full_name)
return full_name
def get_graphics_points(self):
return [self.__cp_start, self.__cp_end]
def paint(self, painter, option, widget):
painter.setBrush(self.custom_brush)
(start_cp_x, start_cp_y) = self.__cp_start.get_start_position()
(end_cp_x, end_cp_y) = self.__cp_end.get_start_position()
mid_x = min(start_cp_x, end_cp_x) + abs((start_cp_x - end_cp_x) / 2.0)
mid_y = min(start_cp_y, end_cp_y) + abs((start_cp_y - end_cp_y) / 2.0)
if option.state & QtGui.QStyle.State_Selected and \
self.__num_images and self.__display_overlay:
painter.setPen(QtCore.Qt.NoPen)
for beam_index in range(self.__num_images):
coord_x = start_cp_x + (end_cp_x - start_cp_x) * \
beam_index / float(self.__num_images)
coord_y = start_cp_y + (end_cp_y - start_cp_y) * \
beam_index / float(self.__num_images)
painter.drawEllipse(coord_x - self.beam_size_pix[0] / 2,
coord_y - self.beam_size_pix[1] / 2,
self.beam_size_pix[0],
self.beam_size_pix[1])
info_txt = "Line %d (%d->%d)" % (self.index,
self.__cp_start.index, self.__cp_end.index)
if option.state & QtGui.QStyle.State_Selected:
self.custom_pen.setColor(SELECTED_COLOR)
info_txt += " selected"
painter.drawText(mid_x + 5, mid_y, info_txt)
if self.__num_images:
info_txt += " (%d images)" % self.__num_images
else:
self.custom_pen.setColor(NORMAL_COLOR)
self.custom_pen.setWidth(2)
painter.setPen(self.custom_pen)
painter.drawLine(start_cp_x, start_cp_y,
end_cp_x, end_cp_y)
painter.drawText(mid_x + 5, mid_y, info_txt)
def set_num_images(self, num_images):
self.__num_images = num_images
self.update_item()
def get_points_index(self):
return (self.__cp_start.index, self.__cp_end.index)
def get_graphical_points(self):
return (self.__cp_start, self.__cp_end)
def set_graphical_points(self, cp_start, cp_end):
self.__cp_start = cp_start
self.__cp_end = cp_end
self.update_item()
def get_centred_positions(self):
return (self.__cp_start.get_centred_position(), \
self.__cp_end.get_centred_position())
class GraphicsItemGrid(GraphicsItem):
"""
Descrip. : Grid representation is based on two grid states:
__draw_mode = True: user defines grid size
False: grid is defined
In draw mode during the draw grid size is esitmated and based
on the cell size and number of col and row actual grid
object is painted. After drawing corner_points are added. These
4 corner points are motor position dict. When one or several
motors are moved corner_cord are updated and grid is painted
in projection mode.
"""
def __init__(self, parent, beam_info, spacing_mm, pixels_per_mm):
GraphicsItem.__init__(self, parent)
self.setFlags(QtGui.QGraphicsItem.ItemIsSelectable)
self.pixels_per_mm = pixels_per_mm
self.beam_size_mm = [beam_info.get("size_x"),
beam_info.get("size_y")]
self.beam_is_rectangle = beam_info.get("shape") == "rectangular"
self.__spacing_mm = spacing_mm
self.__spacing_pix = [0, 0]
self.__cell_size_mm = [0, 0]
self.__cell_size_pix = [0, 0]
self.__corner_coord = (QtCore.QPoint(),
QtCore.QPoint(),
QtCore.QPoint(),
QtCore.QPoint())
self.__center_coord = QtCore.QPoint()
self.__num_cols = 0
self.__num_rows = 0
self.__num_lines = 0
self.__num_images_per_line = 0
self.__first_image_num = 1
self.__centred_point = None
self.__draw_mode = False
self.__draw_projection = False
self.__osc_start = None
self.__osc_range = 0.1
self.__motor_pos_corner = []
self.__centred_position = None
self.__snapshot = None
self.__grid_size_pix = [0, 0]
self.__grid_range_pix = {"fast": 0, "slow": 0}
self.__reversing_rotation = True
self.__score = None
self.__automatic = False
self.__fill_alpha = 120
self.__display_overlay = True
self.__osc_range = 0
#self.__overlay_pixmap = None
self.update_item()
@staticmethod
def set_grid_direction(grid_direction):
"""Sets grids direction.
"""
GraphicsItemGrid.grid_direction = grid_direction
def get_display_name(self):
if self.__automatic:
return "Automatic mesh"
else:
return "Mesh %d" % (self.index + 1)
def get_full_name(self):
return "Mesh %d (hor. spacing: %.1f, ver. spacing: %.1f, beam size: %d, %d)" % \
(self.index + 1, self.__spacing_mm[0], self.__spacing_mm[1],
self.beam_size_mm[0], self.beam_size_mm[1])
def get_col_row_num(self):
return self.__num_cols, self.__num_rows
def get_line_image_per_line_num(self):
return self.__num_lines, self.__num_images_per_line
def get_grid_range_mm(self):
return (float(self.__cell_size_mm[0] * (self.__num_cols - 1)), \
float(self.__cell_size_mm[1] * (self.__num_rows - 1)))
def get_grid_size_mm(self):
return (float(self.__cell_size_mm[0] * self.__num_cols), \
float(self.__cell_size_mm[1] * self.__num_rows))
def update_item(self):
self.__cell_size_mm = [self.beam_size_mm[0] + self.__spacing_mm[0] * 2,
self.beam_size_mm[1] + self.__spacing_mm[1] * 2]
self.__spacing_pix = [self.pixels_per_mm[0] * self.__spacing_mm[0],
self.pixels_per_mm[1] * self.__spacing_mm[1]]
self.__cell_size_pix = [self.pixels_per_mm[0] * self.__cell_size_mm[0],
self.pixels_per_mm[1] * self.__cell_size_mm[1]]
self.beam_size_pix[0] = int(self.beam_size_mm[0] * self.pixels_per_mm[0])
self.beam_size_pix[1] = int(self.beam_size_mm[1] * self.pixels_per_mm[1])
def set_osc_range(self, osc_range):
self.__osc_range = osc_range
def set_draw_start_position(self, pos_x, pos_y):
"""First click defines start position of the grid
"""
if self.__draw_mode:
self.__corner_coord[0].setX(pos_x)
self.__corner_coord[0].setY(pos_y)
self.__corner_coord[1].setY(pos_y)
self.__corner_coord[2].setX(pos_x)
self.scene().update()
def set_draw_end_position(self, pos_x, pos_y):
"""Actual drawing moment, when grid size is defined
"""
if self.__draw_mode:
self.__corner_coord[1].setX(pos_x)
self.__corner_coord[2].setY(pos_y)
self.__corner_coord[3].setX(pos_x)
self.__corner_coord[3].setY(pos_y)
#Number of columns and rows is calculated
num_cols = int(abs(self.__corner_coord[1].x() - \
self.__corner_coord[0].x()) / self.__cell_size_pix[0])
num_rows = int(abs((self.__corner_coord[3].y() - \
self.__corner_coord[1].y()) / self.__cell_size_pix[1]))
if num_rows * num_cols > pow(2, 16):
msg_text = "Unable to draw grid containing more than %d cells!" % pow(2, 16)
logging.getLogger("GUI").info(msg_text)
else:
self.__num_cols = num_cols
self.__num_rows = num_rows
#Based on the grid directions estimates number of lines and
#number of images per line
self.__num_lines = abs(self.grid_direction['fast'][1] * \
self.__num_cols) + abs(self.grid_direction['slow'][1] * \
self.__num_rows)
self.__num_images_per_line = abs(self.grid_direction['fast'][0] * \
self.__num_cols) + abs(self.grid_direction['slow'][0] * \
self.__num_rows)
self.update_grid_draw_parameters()
self.__center_coord.setX(min(self.__corner_coord[0].x(),
self.__corner_coord[1].x()) + self.__grid_size_pix[0] / 2.0)
self.__center_coord.setY(min(self.__corner_coord[0].y(),
self.__corner_coord[3].y()) + self.__grid_size_pix[1] / 2.0)
self.scene().update()
def update_grid_draw_parameters(self):
self.__grid_size_pix = [self.__num_cols * self.__cell_size_pix[0],
self.__num_rows * self.__cell_size_pix[1]]
#Also grid range is estimated
self.__grid_range_pix["fast"] = abs(self.grid_direction['fast'][0] * \
(self.__grid_size_pix[0] - self.__cell_size_pix[0])) + \
abs(self.grid_direction['fast'][1] * \
(self.__grid_size_pix[1] - self.__cell_size_pix[1]))
self.__grid_range_pix["slow"] = abs(self.grid_direction['slow'][0] * \
(self.__grid_size_pix[0] - self.__cell_size_pix[0])) + \
abs(self.grid_direction['slow'][1] * \
(self.__grid_size_pix[1] - self.__cell_size_pix[1]))
def set_corner_coord(self, corner_coord):
self.update_grid_draw_parameters()
for index, coord in enumerate(corner_coord):
self.__corner_coord[index].setX(coord[0])
self.__corner_coord[index].setY(coord[1])
self.__draw_projection = True
"""
if self.__overlay_pixmap:
self.__overlay_pixmap.setPos(self.__corner_coord[0].x(),
self.__corner_coord[0].y())
"""
def set_center_coord(self, center_coord):
self.__center_coord.setX(center_coord[0])
self.__center_coord.setY(center_coord[1])
"""
if self.__overlay_pixmap:
self.__overlay_pixmap.setPos(self.__corner_coord[0].x(),
self.__corner_coord[0].y())
"""
def set_spacing(self, spacing):
self.__spacing_mm = spacing
self.update_item()
self.update_grid_draw_parameters()
self.scene().update()
def set_draw_mode(self, draw_mode):
self.__draw_mode = draw_mode
def is_draw_mode(self):
return self.__draw_mode
def set_projection_mode(self, mode):
self.__draw_projection = mode
def get_properties(self):
(dx_mm, dy_mm) = self.get_grid_range_mm()
return {"name": "Mesh %d" % (self.index + 1),
"direction": self.grid_direction,
"reversing_rotation": self.__reversing_rotation,
"steps_x": self.__num_cols,
"steps_y": self.__num_rows,
"xOffset": self.__spacing_mm[0],
"yOffset": self.__spacing_mm[1],
"dx_mm": dx_mm,
"dy_mm": dy_mm,
"beam_x_mm": self.beam_size_mm[0],
"beam_y_mm": self.beam_size_mm[1],
"num_lines": self.__num_lines,
"num_images_per_line": self.__num_images_per_line,
"first_image_num": self.__first_image_num}
def update_auto_grid(self, grid_size):
"""
"""
self.__num_cols = int(grid_size[0] / self.beam_size_mm[0])
self.__num_rows = int(grid_size[1] / self.beam_size_mm[1])
self.set_center_coord(self.beam_position)
self.update_item()
self.__num_lines = abs(self.grid_direction['fast'][1] * \
self.__num_cols) + abs(self.grid_direction['slow'][1] * \
self.__num_rows)
self.__num_images_per_line = abs(self.grid_direction['fast'][0] * \
self.__num_cols) + abs(self.grid_direction['slow'][0] * \
self.__num_rows)
self.update_grid_draw_parameters()
self.__automatic = True
self.__draw_projection = False
self.__motor_pos_corner = []
self.__motor_pos_corner.append(\
self.get_motor_pos_from_col_row(0, self.__num_rows))
self.__motor_pos_corner.append(\
self.get_motor_pos_from_col_row(self.__num_cols, self.__num_rows))
self.__motor_pos_corner.append(\
self.get_motor_pos_from_col_row(0, 0))
self.__motor_pos_corner.append(\
self.get_motor_pos_from_col_row(self.__num_cols, 0))
def get_center_coord(self):
return self.__center_coord.x(), self.__center_coord.y()
def get_corner_coord(self):
return self.__corner_coord
def set_motor_pos_corner(self, motor_pos_corner):
self.__motor_pos_corner = motor_pos_corner
def get_motor_pos_corner(self):
return self.__motor_pos_corner
def set_centred_position(self, centred_position):
self.__centred_position = centred_position
self.__osc_start = self.__centred_position.phi
"""
self.__overlay_pixmap = QtGui.QGraphicsPixmapItem(self)
#self.scene().addItem(self.__overlay_pixmap)
qpixmap = QtGui.QPixmap("/tmp/smoth_result_smoth.png")
width, height = self.get_size_pix()
self.__overlay_pixmap.setPixmap(qpixmap.scaled(width, height))
self.__overlay_pixmap.setVisible(True)
self.__overlay_pixmap.setPos(self.__corner_coord[0].x(),
self.__corner_coord[0].y())
"""
def get_centred_position(self):
return self.__centred_position
def set_score(self, score):
self.__score = score
def get_snapshot(self):
return self.__snapshot
def set_snapshot(self, snapshot):
self.__snapshot = snapshot
def set_fill_alpha(self, value):
self.__fill_alpha = value
def set_display_overlay(self, state):
self.__display_overlay = state
def paint(self, painter, option, widget):
self.custom_pen.setColor(QtCore.Qt.darkGray)
self.custom_pen.setWidth(1)
if self.__draw_mode:
self.custom_pen.setStyle(QtCore.Qt.DashLine)
if self.__draw_mode or self.isSelected():
self.custom_pen.setColor(SELECTED_COLOR)
painter.setPen(self.custom_pen)
painter.setBrush(self.custom_brush)
if self.__draw_projection:
#In projection mode, just the frame is displayed
painter.drawLine(self.__corner_coord[0], self.__corner_coord[1])
painter.drawLine(self.__corner_coord[0], self.__corner_coord[2])
painter.drawLine(self.__corner_coord[3], self.__corner_coord[1])
painter.drawLine(self.__corner_coord[3], self.__corner_coord[2])
else:
draw_start_x = self.__center_coord.x() - self.__grid_size_pix[0] / 2.0
draw_start_y = self.__center_coord.y() - self.__grid_size_pix[1] / 2.0
# Horizontal and vertical grid lines
for i in range(0, self.__num_cols + 1):
offset = i * self.__cell_size_pix[0]
painter.drawLine(draw_start_x + offset, draw_start_y,
draw_start_x + offset, draw_start_y + \
self.__num_rows * self.__cell_size_pix[1])
for i in range(0, self.__num_rows + 1):
offset = i * self.__cell_size_pix[1]
painter.drawLine(draw_start_x, draw_start_y + offset,
draw_start_x + self.__num_cols * self.__cell_size_pix[0],
draw_start_y + offset)
#Draws beam shape and displays number of image if
#less than 1000 cells and size is greater than 20px
cell_index = 0
if self.__num_cols * self.__num_rows < 1000 and \
self.__cell_size_pix[1] > 20:
for col in range(self.__num_cols):
for row in range(self.__num_rows):
#Estimate area where frame number or score will be displayed
line, image = self.get_line_image_num(\
cell_index + self.__first_image_num)
pos_x, pos_y = self.get_coord_from_line_image(line, image)
paint_rect = QtCore.QRect(\
pos_x - self.__cell_size_pix[0] / 2,
pos_y - self.__cell_size_pix[1] / 2,
self.__cell_size_pix[0],
self.__cell_size_pix[1])
#If score exists overlay color may change
cell_score = None
if self.base_color:
brush_color = self.base_color
brush_color.setAlpha(self.__fill_alpha)
else:
brush_color = QtGui.QColor(70, 70, 165, self.__fill_alpha)
#if self.__display_overlay and not self.__overlay_pixmap:
if self.__display_overlay:
#painter.setBrush(QtGui.QColor(70, 70, 165))
if self.__score is not None:
cell_score = self.__score[cell_index]
if self.__score.max() > 0:
cell_score = float(cell_score) / \
self.__score.max()
brush_color.setHsv(0 + 60 * cell_score,
255, 255 * cell_score, self.__fill_alpha)
self.custom_brush.setColor(brush_color)
painter.setBrush(self.custom_brush)
else:
painter.setBrush(QtCore.Qt.transparent)
if self.beam_is_rectangle:
painter.drawRect(pos_x - self.beam_size_pix[0] / 2,
pos_y - self.beam_size_pix[1] / 2,
self.beam_size_pix[0],
self.beam_size_pix[1])
else:
painter.drawEllipse(pos_x - self.beam_size_pix[0] / 2,
pos_y - self.beam_size_pix[1] / 2,
self.beam_size_pix[0],
self.beam_size_pix[1])
painter.drawText(paint_rect, QtCore.Qt.AlignCenter, \
str(cell_index + self.__first_image_num))
cell_index += 1
"""
if self.__overlay_pixmap:
self.__overlay_pixmap.setOpacity(self.__fill_alpha / 255.0)
"""
#Draws x in the middle of the grid
painter.drawLine(self.__center_coord.x() - 5,
self.__center_coord.y() - 5,
self.__center_coord.x() + 5,
self.__center_coord.y() + 5)
painter.drawLine(self.__center_coord.x() + 5,
self.__center_coord.y() - 5,
self.__center_coord.x() - 5,
self.__center_coord.y() + 5)
if self.__automatic:
grid_info = "Auto mesh"
else:
grid_info = "Mesh %d" % (self.index + 1)
painter.drawText(self.__center_coord.x() + \
self.__grid_size_pix[0] / 2.0 + 3,
self.__center_coord.y() - \
self.__grid_size_pix[1] / 2.0 - 3,
grid_info)
painter.drawText(self.__center_coord.x() + \
self.__grid_size_pix[0] / 2.0 + 3,
self.__center_coord.y() - \
self.__grid_size_pix[1] / 2.0 + 12,
"%d x %d" % (self.__num_lines,
self.__num_images_per_line))
def move_by_pix(self, move_direction):
"""Moves grid by one pixel
"""
move_delta_x = 0
move_delta_y = 0
if move_direction == "left":
move_delta_x = - 1
elif move_direction == "right":
move_delta_x = 1
elif move_direction == "up":
move_delta_y = - 1
elif move_direction == "down":
move_delta_y = 1
for corner_coord in self.__corner_coord:
corner_coord.setX(corner_coord.x() + move_delta_x)
corner_coord.setY(corner_coord.y() + move_delta_y)
self.__center_coord.setX(self.__center_coord.x() + move_delta_x)
self.__center_coord.setY(self.__center_coord.y() + move_delta_y)
self.scene().update()
def get_size_pix(self):
width_pix = self.__cell_size_pix[0] * self.__num_cols
height_pix = self.__cell_size_pix[1] * self.__num_rows
return (width_pix, height_pix)
def get_line_image_num(self, image_number):
"""
Descript. : from serial frame (==image) number returns a number
of line == grid coord. along scan slow direction,
image == grid coord. along scan fast direction
"""
line = int((image_number - self.__first_image_num) / \
self.__num_images_per_line)
image = image_number - self.__first_image_num - \
line * self.__num_images_per_line
return line, image
def get_coord_from_line_image(self, line, image):
"""
Descript. : returns the screen coordinates x, y in pixel, of a middle
of the cell that correspoinds to
Args. : number an frame #image in line #line
"""
ref_fast, ref_slow = self.get_coord_ref_from_line_image(line, image)
coord_x = self.__center_coord.x() + self.__grid_range_pix['fast'] * \
self.grid_direction['fast'][0] * ref_fast + \
self.__grid_range_pix['slow'] * \
self.grid_direction['slow'][0] * ref_slow
coord_y = self.__center_coord.y() + self.__grid_range_pix['fast'] * \
self.grid_direction['fast'][1] * ref_fast + \
self.__grid_range_pix['slow'] * \
self.grid_direction['slow'][1] * ref_slow
return coord_x, coord_y
def get_coord_ref_from_line_image(self, line, image):
"""
Descript. : returns nameless constants used in conversion between
scan and screen coordinates.
"""
fast_ref = 0.5
if self.__num_images_per_line > 1:
fast_ref = 0.5 - float(image) / (self.__num_images_per_line - 1)