measureModes.py 4.65 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 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


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:
        self.crossBoxGenerator.filterHeight = self.filterView.filter.height
        self.crossBoxGenerator.filterWidth = self.filterView.filter.width
        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

        viewItems = []
        topLefts: list = self.crossBoxGenerator.get_topLeft_of_boxes()
        boxSize = self.crossBoxGenerator.boxSize
        for (x, y) in topLefts:
            newBox: MeasureBoxGraphItem = MeasureBoxGraphItem(x, y, boxSize, boxSize)
            viewItems.append(newBox)

        self.filterView.update_measure_boxes(viewItems)


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

    def set_to_max_possible_coverage(self, maxCoverage:int):
        self.coverageSpinbox.setMaximum(maxCoverage)
        if maxCoverage < self.coverageSpinbox.value():
            self.coverageSpinbox.valueChanged.disconnect()
            self.coverageSpinbox.setValue(maxCoverage)
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
            self.coverageSpinbox.valueChanged.connect(self._config_changed)


class SpiralBoxMode(MeasureMode):
    def __init__(self, *args):
        super(SpiralBoxMode, self).__init__(*args)
        self.uiControls = QtWidgets.QGroupBox('Spiral Box Controls')
        self.spiralBoxGenerator: SpiralSelector = SpiralSelector(None)
        self.update_measure_viewItems()

    def update_measure_viewItems(self) -> None:
        self.spiralBoxGenerator.filterHeight = self.filterView.filter.height
        self.spiralBoxGenerator.filterWidth = self.filterView.filter.width

        # 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

        viewItems = []
        topLefts: list = self.spiralBoxGenerator.get_topLeft_of_boxes()
        boxSize = self.spiralBoxGenerator.boxSize
        for (x, y) in topLefts:
            newBox: MeasureBoxGraphItem = MeasureBoxGraphItem(x, y, boxSize, boxSize)
            viewItems.append(newBox)

        self.filterView.update_measure_boxes(viewItems)