mainView.py 4.15 KB
Newer Older
1
from PyQt5 import QtWidgets
2 3 4 5
import sys
sys.path.append("C://Users//xbrjos//Desktop//Python")
import gepard
from gepard import dataset
6 7
from gui.filterView import FilterView
from gui.measureModes import MeasureMode, CrossBoxMode, CrossBoxesControls, SpiralBoxMode
8
import helpers
9 10 11 12 13 14 15 16 17 18 19


class MainView(QtWidgets.QWidget):
    def __init__(self):
        super(MainView, self).__init__()

        self.setWindowTitle('Subsampling Selector')
        self.layout: QtWidgets.QVBoxLayout = QtWidgets.QVBoxLayout()
        self.setLayout(self.layout)

        self.modeSelector: QtWidgets.QComboBox = QtWidgets.QComboBox()
20
        self.modeSelector.currentTextChanged.connect(self._switch_to_mode)
21
        self.measureModes: dict = {}
22
        self.activeMode: MeasureMode = None
23 24
        self.activeModeControl: QtWidgets.QGroupBox = QtWidgets.QGroupBox()

25 26 27
        loadDsetBtn = QtWidgets.QPushButton('Load Dataset')
        loadDsetBtn.released.connect(self._load_dataset)

28 29 30 31
        self.controlGroup = QtWidgets.QGroupBox()
        self.controlGroupLayout = QtWidgets.QHBoxLayout()
        self.controlGroup.setLayout(self.controlGroupLayout)

32
        self.controlGroupLayout.addWidget(loadDsetBtn)
33 34 35 36 37 38 39 40 41 42 43
        self.controlGroupLayout.addWidget(QtWidgets.QLabel('Select Subsampling Mode:'))
        self.controlGroupLayout.addWidget(self.modeSelector)
        self.controlGroupLayout.addWidget(self.activeModeControl)

        self.layout.addWidget(self.controlGroup)
        self.filterView = FilterView()
        self.layout.addWidget(self.filterView)
        self._add_measure_modes()
        self._switch_to_default_mode()

    def _add_measure_modes(self) -> None:
44
        self.measureModes['spiralSelection'] = SpiralBoxMode(self.filterView)
45
        self.measureModes['crossSelection'] = CrossBoxMode(self.filterView)
46 47
        self.modeSelector.addItem('spiralSelection')
        self.modeSelector.addItem('crossSelection')
48 49 50 51

    def _switch_to_default_mode(self) -> None:
        modes: list = list(self.measureModes.keys())
        self._activate_mode(modes[0])
52 53 54 55 56
    
    def _switch_to_mode(self) -> None:
        newMode: str = self.modeSelector.currentText()
        self._activate_mode(newMode)
    
57
    def _activate_mode(self, modeName: str) -> None:
58 59 60 61 62 63 64 65
        requestedMode: MeasureMode = self.measureModes[modeName]
        if requestedMode is not self._activate_mode:
            self.activeModeControl.setParent(None)
            self.controlGroupLayout.removeWidget(self.activeModeControl)

            self.activeMode = requestedMode
            self.activeModeControl = self.activeMode.get_control_groupBox()
            self.controlGroupLayout.insertWidget(2, self.activeModeControl)
66

67
            self.activeMode.update_measure_viewItems()
68

69 70 71
    def _load_dataset(self) -> None:
        fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Select .pkl file', filter='pkl file (*.pkl)')
        if fname[0] != '':
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
            try:
                dset: dataset.DataSet = dataset.loadData(fname[0])
            except IndexError:
                dset = None
                QtWidgets.QMessageBox.critical(self, 'Index Error', 'Unable to load dataset..')

            if dset is not None:
                offset, diameter, widthHeight = helpers.get_filterDimensions_from_dataset(dset)
                offsetx = helpers.convert_length_to_pixels(dset, offset[0])
                offsety = helpers.convert_length_to_pixels(dset, offset[1])
                diameter = helpers.convert_length_to_pixels(dset, diameter)
                width = helpers.convert_length_to_pixels(dset, widthHeight[0])
                height = helpers.convert_length_to_pixels(dset, widthHeight[1])
                self.filterView.filter.update_filterSize(width, height, diameter, (offsetx, offsety))

                for mode in self.measureModes.values():
                    mode.boxGenerator.particleContainer = dset.particleContainer
                    mode.boxGenerator.offset = (offsetx, offsety)

                self.filterView.update_from_dataset(dset)
                self.activeMode.update_measure_viewItems()
93

94 95 96 97 98 99 100

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    subsampling = MainView()
    subsampling.show()
    ret = app.exec_()