measureModes.py 5.6 KB
Newer Older
1
from PyQt5 import QtCore, QtWidgets
2
from gui.filterView import FilterView, MeasureBoxGraphItem
3
from geometricMethods import BoxSelectionSubsamplingMethod, CrossBoxSelector, SpiralSelector
4 5 6 7 8


class MeasureMode(QtCore.QObject):
    def __init__(self, relatedFilterView: FilterView):
        super(MeasureMode, self).__init__()
9
        self.filterView: FilterView = relatedFilterView
10
        self.uiControls: QtWidgets.QGroupBox = QtWidgets.QGroupBox()
11
        self.boxGenerator: BoxSelectionSubsamplingMethod = None
12 13 14 15 16 17 18

    def get_control_groupBox(self) -> QtWidgets.QGroupBox:
        return self.uiControls

    def update_measure_viewItems(self) -> None:
        raise NotImplementedError

19 20 21 22 23
    def _send_measuredParticles_to_filterview(self) -> None:
        if self.boxGenerator.particleContainer is not None:
            subParticles = self.boxGenerator.apply_subsampling_method()
            self.filterView.update_measured_particles(subParticles)

24 25 26 27 28

class CrossBoxMode(MeasureMode):
    def __init__(self, *args):
        super(CrossBoxMode, self).__init__(*args)
        self.uiControls = CrossBoxesControls(self)
29
        self.boxGenerator: CrossBoxSelector = CrossBoxSelector(None)
30 31 32
        self.update_measure_viewItems()

    def update_measure_viewItems(self) -> None:
33 34
        self.boxGenerator.filterDiameter = self.filterView.filter.diameter
        self.boxGenerator.numBoxesAcross = int(self.uiControls.numBoxesSelector.currentText())
35 36

        desiredCoverage: int = self.uiControls.coverageSpinbox.value()
37
        maxCoverage: int = int(self.boxGenerator.get_maximum_achievable_fraction() * 100)
38 39 40 41
        self.uiControls.set_to_max_possible_coverage(maxCoverage)
        if desiredCoverage > maxCoverage:
            desiredCoverage = maxCoverage

42
        self.boxGenerator.fraction = desiredCoverage / 100
43

44 45
        topLefts: list = self.boxGenerator.get_topLeft_of_boxes()
        boxSize = self.boxGenerator.boxSize
46
        self.filterView.update_measure_boxes(topLefts, boxSize)
47
        self._send_measuredParticles_to_filterview()
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


class CrossBoxesControls(QtWidgets.QGroupBox):
    """
    Gives a groupbox with the controls for setting up the cross boxes.
    """

    def __init__(self, measureModeParent: MeasureMode):
        super(CrossBoxesControls, self).__init__()
        self.setTitle('Cross Box Controls')
        self.measureModeParent = measureModeParent
        layout = QtWidgets.QHBoxLayout()
        self.setLayout(layout)

        layout.addWidget(QtWidgets.QLabel('Number of Boxes across:'))
        self.numBoxesSelector = QtWidgets.QComboBox()
        self.numBoxesSelector.addItems([str(3), str(5)])
        self.numBoxesSelector.currentTextChanged.connect(self._config_changed)
        layout.addWidget(self.numBoxesSelector)

        layout.addStretch()

        layout.addWidget(QtWidgets.QLabel('Desired Coverage (%)'))
        self.coverageSpinbox = QtWidgets.QSpinBox()
        self.coverageSpinbox.setFixedWidth(50)
        self.coverageSpinbox.setMinimum(0)
        self.coverageSpinbox.setMaximum(100)
        self.coverageSpinbox.setValue(10)
        self.coverageSpinbox.valueChanged.connect(self._config_changed)
        layout.addWidget(self.coverageSpinbox)

    def _config_changed(self):
        self.measureModeParent.update_measure_viewItems()

Josef Brandt's avatar
Josef Brandt committed
82
    def set_to_max_possible_coverage(self, maxCoverage: int):
83 84 85 86
        self.coverageSpinbox.setMaximum(maxCoverage)
        if maxCoverage < self.coverageSpinbox.value():
            self.coverageSpinbox.valueChanged.disconnect()
            self.coverageSpinbox.setValue(maxCoverage)
87 88 89 90 91 92
            self.coverageSpinbox.valueChanged.connect(self._config_changed)


class SpiralBoxMode(MeasureMode):
    def __init__(self, *args):
        super(SpiralBoxMode, self).__init__(*args)
Josef Brandt's avatar
Josef Brandt committed
93
        self.uiControls: SpiralBoxControls = SpiralBoxControls(self)
94
        self.boxGenerator: SpiralSelector = SpiralSelector(None)
95 96 97
        self.update_measure_viewItems()

    def update_measure_viewItems(self) -> None:
98
        self.boxGenerator.filterDiameter = self.filterView.filter.diameter
99

100 101
        self.boxGenerator.numBoxes = self.uiControls.numBoxesSpinbox.value()
        self.boxGenerator.fraction = self.uiControls.coverageSpinbox.value() / 100
102

103 104
        topLefts: list = self.boxGenerator.get_topLeft_of_boxes()
        boxSize = self.boxGenerator.boxSize
105
        self.filterView.update_measure_boxes(topLefts, boxSize)
106
        self._send_measuredParticles_to_filterview()
Josef Brandt's avatar
Josef Brandt committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126


class SpiralBoxControls(QtWidgets.QGroupBox):
    """
    Gives a groupbox with the controls for setting up the cross boxes.
    """

    def __init__(self, measureModeParent: MeasureMode):
        super(SpiralBoxControls, self).__init__()
        self.setTitle('Spiral Box Controls')
        self.measureModeParent = measureModeParent
        layout = QtWidgets.QHBoxLayout()
        self.setLayout(layout)

        layout.addWidget(QtWidgets.QLabel('Num Boxes:'))
        self.numBoxesSpinbox = QtWidgets.QSpinBox()
        self.numBoxesSpinbox.setValue(10)
        self.numBoxesSpinbox.valueChanged.connect(self._config_changed)
        layout.addWidget(self.numBoxesSpinbox)

Josef Brandt's avatar
Josef Brandt committed
127 128 129 130 131 132 133 134 135 136 137
        layout.addStretch()

        layout.addWidget(QtWidgets.QLabel('Desired Coverage (%)'))
        self.coverageSpinbox = QtWidgets.QSpinBox()
        self.coverageSpinbox.setFixedWidth(50)
        self.coverageSpinbox.setMinimum(0)
        self.coverageSpinbox.setMaximum(100)
        self.coverageSpinbox.setValue(10)
        self.coverageSpinbox.valueChanged.connect(self._config_changed)
        layout.addWidget(self.coverageSpinbox)

Josef Brandt's avatar
Josef Brandt committed
138
    def _config_changed(self):
Josef Brandt's avatar
Josef Brandt committed
139 140
        if self.numBoxesSpinbox.value() > 0:
            self.measureModeParent.update_measure_viewItems()