Newer
Older
# -*- coding: utf-8 -*-
"""
Some sub-classes from pyqtgraph.
"""
from pyqtgraph import ROI
from PyQt5.QtCore import QSettings, pyqtSlot
from PyQt5.QtWidgets import QFileDialog
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
class CustomRectangularROI(ROI):
"""
Rectangular ROI subclass with a single scale handle at the top-right corner.
============== =============================================================
**Arguments**
pos (length-2 sequence) The position of the ROI origin.
See ROI().
size (length-2 sequence) The size of the ROI. See ROI().
centered (bool) If True, scale handles affect the ROI relative to its
center, rather than its origin.
sideScalers (bool) If True, extra scale handles are added at the top and
right edges.
**args All extra keyword arguments are passed to ROI()
============== =============================================================
"""
def __init__(self, pos, size = [8, 8], centered=False, sideScalers=False):
#ROI.__init__(self, pos, size, **args)
super().__init__(pos, size, centered, sideScalers)
## handles scaling horizontally around center
self.addScaleHandle([1, 0.5], [0.5, 0.5])
self.addScaleHandle([0, 0.5], [0.5, 0.5])
## handles scaling vertically from opposite edge
self.addScaleHandle([0.5, 0], [0.5, 1])
self.addScaleHandle([0.5, 1], [0.5, 0])
## handles scaling both vertically and horizontally
self.addScaleHandle([1, 1], [0, 0])
self.addScaleHandle([0, 0], [1, 1])
def correct_scaling(self):
x, y = self.pos()[0], self.pos()[1]
if x%8 != 0 or y%8 != 0:
self.setPos((x//8)*8, (y//8)*8)
sx, sy = self.size()[0], self.size()[1]
if sx%8 != 0 or sy%8 != 0:
self.setSize((sx//8)*8, (sy//8)*8)
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
@pyqtSlot()
def save(self, current_binning='', location = 'config.ini'):
settings = QSettings(str(location), QSettings.IniFormat)
settings.beginGroup('Roi')
settings.setValue('Binning', current_binning)
settings.setValue('PositionX', self.pos()[0])
settings.setValue('PositionY', self.pos()[1])
settings.setValue('SizeX', self.size()[0])
settings.setValue('SizeY', self.size()[1])
settings.endGroup()
print('roi saved')
@pyqtSlot()
def load(self, current_binning='', location = 'config.ini'):
settings = QSettings(str(location), QSettings.IniFormat)
settings.beginGroup('Roi')
saved_binning = settings.value('Binning')
posX = settings.value('PositionX')
posY = settings.value('PositionY')
sizeX = settings.value('SizeX')
sizeY =settings.value('SizeY')
settings.endGroup()
self.setPos([posX, posY])
self.setSize([sizeX, sizeY])
self.repositionRoi(saved_binning, current_binning)
def repositionRoi(self, old_binning, new_binning):
aux_dict = {'1x1': 2048, '2x2': 1024, '4x4': 512, '8x8': 256}
old_size = aux_dict[old_binning]
new_size = aux_dict[new_binning]
x, y, sx, sy = self.pos()[0], self.pos()[1], self.size()[0], self.size()[1]
new_x, new_y = new_size/old_size * x, new_size/old_size * y
new_sx, new_sy = new_size/old_size * sx, new_size/old_size * sy
self.setPos([new_x, new_y])
self.setSize([new_sx, new_sy])