Пример #1
0
class ObjectTab(QWidget):
    """
    This class constructs the entire contents of the Object tab.
    """
    def __init__(self, inspector, dither, detector, object_id, *args):
        super().__init__(*args)

        self._inspector = inspector
        self._dither = dither  # remove
        self._detector = detector  # remove
        self._object_id = object_id
        self.contents = list()
        self.setMouseTracking(True)

        self.plot_selector = PlotSelector(self)

        detectors = self.determine_relevant_detectors(object_id)

        selector_area = MultiDitherDetectorSelector(detectors)
        selector_area.updated.connect(self.update_plot_button)

        self.mdi = QMdiArea(self)
        self.mdi.setContentsMargins(0, 0, 0, 0)
        self.mdi.setBackground(QBrush(QColor('#56595e')))

        h_layout = QGridLayout()
        h_layout.setSpacing(0)
        h_layout.setContentsMargins(0, 0, 0, 0)
        h_layout.addWidget(selector_area, 0, 0)
        h_layout.setAlignment(selector_area, Qt.AlignTop)
        h_layout.addWidget(self.mdi, 0, 1)

        h_layout.setColumnStretch(0, 0)
        h_layout.setColumnStretch(1, 10)

        self.detector_selector = selector_area

        v_layout = QVBoxLayout()
        v_layout.setSpacing(0)

        v_layout.addWidget(self.plot_selector)

        v_layout.addItem(h_layout)

        self.setLayout(v_layout)
        self.setContentsMargins(0, 0, 0, 0)
        self._spec_plots = None

        self._plot_descriptors = set()

        self._info_window = None

    @property
    def object_id(self):
        return self._object_id

    @property
    def dither(self):
        return self._dither

    @property
    def detector(self):
        return self._detector

    @property
    def inspector(self):
        return self._inspector

    def make_plots(self):
        self._spec_plots = SpecPlot(self._inspector, self.plot_selector, self.detector_selector)
        self._spec_plots.make_plots(self._object_id)
        for window in self._spec_plots.windows:
            if window.descriptor not in self._plot_descriptors:
                self.mdi.addSubWindow(window)
                self._plot_descriptors.add(window.descriptor)
                window.closing.connect(self.handle_closed_subwindow)
                window.activateWindow()
                window.show()

    def show_info(self):
        if self._inspector.location_tables is not None:
            info = self._inspector.location_tables.get_info(self._object_id)
            info_window = ObjectInfoWindow(info, self._inspector)
            self.mdi.addSubWindow(info_window)
            info_window.activateWindow()
            info_window.show()
            self._info_window = info_window
        else:
            m = QMessageBox(0, 'Missing Data',
                            'Location tables containing the requested information must be loaded before showing info.')
            m.exec()

    def determine_relevant_detectors(self, object_id):
        """
        Returns the detectors in which the spectra of the object can be found, in the format {dither: [detectors]}
        """
        detectors = {}

        for dither in self._inspector.spectra[object_id]:
            detectors[dither] = list(self._inspector.spectra[object_id][dither].keys())

        for d in (1, 2, 3, 4):
            if d not in detectors:
                detectors[d] = []

        return detectors

    def update_plot_button(self):
        selected = list(self.detector_selector.selected_detectors().values())

        if all(item is None for item in selected):
            self.plot_selector.plot_button.setDisabled(True)
            self.plot_selector.detector_button.setDisabled(True)
        else:
            self.plot_selector.detector_button.setEnabled(True)
            self.plot_selector.plot_button.setEnabled(True)

    def handle_closed_subwindow(self, descriptor):
        if descriptor in self._plot_descriptors:
            self._plot_descriptors.remove(descriptor)

        active_window = self.mdi.activeSubWindow()

        if active_window is not None and descriptor == active_window.widget().descriptor:
            self.mdi.closeActiveSubWindow()

    def open_detectors(self):

        selected_detectors = self.detector_selector.selected_detectors()

        inspector = self._inspector

        # make a list of all open detectors (detectors currently being viewed in tabs)

        open_detectors = []

        for tab_index in range(inspector.tabs.count()):
            tab = inspector.tabs.widget(tab_index)
            if isinstance(tab, ViewTab):
                open_detectors.append((tab.current_dither, tab.current_detector))

        # open new tabs, where necessary

        for dither in selected_detectors:
            if selected_detectors[dither] is not None:
                for detector in selected_detectors[dither]:
                    if (dither, detector) not in open_detectors:
                        inspector.new_view_tab(dither, detector)

            # pin the object in all tabs:

            for tab_index in range(inspector.tabs.count()):
                tab = inspector.tabs.widget(tab_index)
                if isinstance(tab, ViewTab):
                    tab.select_spectrum_by_id(self._object_id)