Skip to content
pyqtgraph_subclasses.py 3.47 KiB
Newer Older
# -*- coding: utf-8 -*-
"""
Some sub-classes from pyqtgraph. 
"""

from pyqtgraph import ROI
Martin Drechsler's avatar
Martin Drechsler committed
from PyQt5.QtCore import QSettings, pyqtSlot
from PyQt5.QtWidgets import QFileDialog


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])

Martin Drechsler's avatar
Martin Drechsler committed
        self.current_binning = ''

    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)
Martin Drechsler's avatar
Martin Drechsler committed
    
    @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])