measureModes.py 5.31 KB
Newer Older
1
from PyQt5 import QtCore, QtWidgets
2
from gui.filterView import FilterView, MeasureBoxGraphItem
Josef Brandt's avatar
Josef Brandt committed
3
from geometricMethods import CrossBoxSelector, SpiralSelector
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26


class MeasureMode(QtCore.QObject):
    def __init__(self, relatedFilterView: FilterView):
        super(MeasureMode, self).__init__()
        self.filterView = relatedFilterView
        self.uiControls: QtWidgets.QGroupBox = QtWidgets.QGroupBox()

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

    def update_measure_viewItems(self) -> None:
        raise NotImplementedError


class CrossBoxMode(MeasureMode):
    def __init__(self, *args):
        super(CrossBoxMode, self).__init__(*args)
        self.uiControls = CrossBoxesControls(self)
        self.crossBoxGenerator: CrossBoxSelector = CrossBoxSelector(None)
        self.update_measure_viewItems()

    def update_measure_viewItems(self) -> None:
27
        self.crossBoxGenerator.filterDiameter = self.filterView.filter.diameter
28 29 30 31 32 33 34 35 36 37 38 39
        self.crossBoxGenerator.numBoxesAcross = int(self.uiControls.numBoxesSelector.currentText())

        desiredCoverage: int = self.uiControls.coverageSpinbox.value()
        maxCoverage: int = int(self.crossBoxGenerator.get_maximum_achievable_fraction() * 100)
        self.uiControls.set_to_max_possible_coverage(maxCoverage)
        if desiredCoverage > maxCoverage:
            desiredCoverage = maxCoverage

        self.crossBoxGenerator.fraction = desiredCoverage / 100

        topLefts: list = self.crossBoxGenerator.get_topLeft_of_boxes()
        boxSize = self.crossBoxGenerator.boxSize
40
        self.filterView.update_measure_boxes(topLefts, boxSize)
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


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
75
    def set_to_max_possible_coverage(self, maxCoverage: int):
76 77 78 79
        self.coverageSpinbox.setMaximum(maxCoverage)
        if maxCoverage < self.coverageSpinbox.value():
            self.coverageSpinbox.valueChanged.disconnect()
            self.coverageSpinbox.setValue(maxCoverage)
80 81 82 83 84 85
            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
86
        self.uiControls: SpiralBoxControls = SpiralBoxControls(self)
87 88 89 90
        self.spiralBoxGenerator: SpiralSelector = SpiralSelector(None)
        self.update_measure_viewItems()

    def update_measure_viewItems(self) -> None:
91
        self.spiralBoxGenerator.filterDiameter = self.filterView.filter.diameter
92

Josef Brandt's avatar
Josef Brandt committed
93
        self.spiralBoxGenerator.boxSize = self.uiControls.boxSizeSpinbox.value()
94 95 96 97 98
        # minBoxSize: float = self.spiralBoxGenerator.filterDiameter*0.1
        # if self.spiralBoxGenerator.boxSize < minBoxSize:
        #     self.spiralBoxGenerator.boxSize = minBoxSize
        #     self.uiControls.boxSizeSpinbox.setValue(int(round(minBoxSize)))

Josef Brandt's avatar
Josef Brandt committed
99
        self.spiralBoxGenerator.numBoxes = self.uiControls.numBoxesSpinbox.value()
100 101 102

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


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('Box Size:'))
        self.boxSizeSpinbox = QtWidgets.QSpinBox()
        self.boxSizeSpinbox.setValue(50)
121
        self.boxSizeSpinbox.setMaximum(10000)
Josef Brandt's avatar
Josef Brandt committed
122 123 124 125 126 127 128 129 130 131 132 133 134
        self.boxSizeSpinbox.valueChanged.connect(self._config_changed)
        layout.addWidget(self.boxSizeSpinbox)

        layout.addStretch()

        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)

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