measureModes.py 5.51 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
            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
92
        self.uiControls: SpiralBoxControls = SpiralBoxControls(self)
93 94 95 96 97 98 99
        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

Josef Brandt's avatar
Josef Brandt committed
100 101
        self.spiralBoxGenerator.boxSize = self.uiControls.boxSizeSpinbox.value()
        self.spiralBoxGenerator.numBoxes = self.uiControls.numBoxesSpinbox.value()
102 103 104 105 106 107 108 109

        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)

Josef Brandt's avatar
Josef Brandt committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        self.filterView.update_measure_boxes(viewItems)


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)
        self.boxSizeSpinbox.setMaximum(1000)
        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()