示例#1
0
    def filter_documents(self):
        dialog = Qt.QDialog()
        layout = Qt.QVBoxLayout()
        dialog.setLayout(layout)
        table_data = TableDataBrowser(
            self.project, self, self.project.session.get_shown_tags(), False,
            True, link_viewer=False)
        layout.addWidget(table_data)
        hlay = Qt.QHBoxLayout()
        layout.addLayout(hlay)
        ok = Qt.QPushButton('Display')
        hlay.addWidget(ok)
        ok.clicked.connect(dialog.accept)
        ok.setDefault(True)
        cancel = Qt.QPushButton('Cancel')
        hlay.addWidget(cancel)
        cancel.clicked.connect(dialog.reject)
        hlay.addStretch(1)

        # Reducing the list of scans to selection
        all_scans = table_data.scans_to_visualize
        table_data.scans_to_visualize = self.documents
        table_data.scans_to_search = self.documents
        table_data.update_visualized_rows(all_scans)

        res = dialog.exec_()
        if res == Qt.QDialog.Accepted:
            points = table_data.selectedIndexes()
            result_names = []
            for point in points:
                row = point.row()
                # We get the FileName of the scan from the first row
                scan_name = table_data.item(row, 0).text()
                value = self.project.session.get_value(COLLECTION_CURRENT,
                                                       scan_name, TAG_FILENAME)
                value = os.path.abspath(os.path.join(self.project.folder,
                                                     value))
                result_names.append(value)
            self.display_files(result_names)
    def __init__(self, project, node_name, node, main_window):
        """
        Initialization of the Filter Widget

        :param project: current project in the software
        :param node_name: name of the current node
        :param node: instance of the corresponding Input_Filter node
        :param main_window: parent main window
        """

        super(FilterWidget, self).__init__(None)

        self.setWindowTitle("Filter - " + node_name)
        self.project = project
        self.visible_tags = self.project.session.get_shown_tags()
        self.node = node
        self.process = node.process
        self.main_window = main_window

        scan_list = []
        # The scan list to filter corresponds to the input of the Input Filter
        if self.process.input and self.process is not Undefined:
            for scan in self.process.input:
                path, file_name = os.path.split(scan)
                path, second_folder = os.path.split(path)
                first_folder = os.path.basename(path)
                database_file = os.path.join(first_folder, second_folder,
                                             file_name)
                scan_list.append(database_file)

        self.scan_list = scan_list

        # Graphical components
        self.table_data = TableDataBrowser(self.project, self,
                                           self.visible_tags, False, False)

        # Reducing the list of scans to selection
        all_scans = self.table_data.scans_to_visualize

        self.table_data.scans_to_visualize = self.scan_list
        self.table_data.scans_to_search = self.scan_list
        self.table_data.update_visualized_rows(all_scans)

        # Filter information
        filter_to_apply = node.process.filter

        # Search
        self.rapid_search = RapidSearch(self)
        if filter_to_apply.search_bar:
            self.rapid_search.setText(filter_to_apply.search_bar)
        self.rapid_search.textChanged.connect(partial(self.search_str))

        self.advanced_search = AdvancedSearch(self.project,
                                              self,
                                              self.scan_list,
                                              self.visible_tags,
                                              from_pipeline=True)
        self.advanced_search.show_search()
        self.advanced_search.apply_filter(filter_to_apply)

        # Initialize Qt objects
        self.button_cross = QToolButton()
        self.push_button_tag_filter = QPushButton(TAG_FILENAME)

        self.layout_view()
class FilterWidget(QWidget):
    """
    Filter widget used on a Input_Filter process

    The widget displays a browser with the selected files of the database,
    a rapid search and an advanced search to filter these files. Once the
    filtering is done, the filter is saved in the process.

    Attributes:
        - advanced_search: advanced search widget to build a complex filter
        - main_window: parent main window
        - process: process instance of the selected node
        - project: current project in the software
        - node: instance of the corresponding Input_Filter node
        - node_controller: parent node controller
        - rapid_search: rapid search widget to build a simple filter
        - scan_list: list of database files to filter
        - table_data: browser that displays the database files to filter
        - visible_tags: tags that are visible in the current project

    Methods:
        - update_tag_to_filter: update the tag to Filter
        - update_tags: update the list of visualized tags
        - reset_search_bar: reset the search bar of the rapid search
        - search_str: update the files to display in the browser
        - ok_clicked: sets the filter to the process and closes the widget
        - set_plug_value: sets the output of the filter to the output of the
           node
    """
    def __init__(self, project, node_name, node, main_window):
        """
        Initialization of the Filter Widget

        :param project: current project in the software
        :param node_name: name of the current node
        :param node: instance of the corresponding Input_Filter node
        :param main_window: parent main window
        """

        super(FilterWidget, self).__init__(None)

        self.setWindowTitle("Filter - " + node_name)
        self.project = project
        self.visible_tags = self.project.session.get_shown_tags()
        self.node = node
        self.process = node.process
        self.main_window = main_window

        scan_list = []
        # The scan list to filter corresponds to the input of the Input Filter
        if self.process.input and self.process is not Undefined:
            for scan in self.process.input:
                path, file_name = os.path.split(scan)
                path, second_folder = os.path.split(path)
                first_folder = os.path.basename(path)
                database_file = os.path.join(first_folder, second_folder,
                                             file_name)
                scan_list.append(database_file)

        self.scan_list = scan_list

        # Graphical components
        self.table_data = TableDataBrowser(self.project, self,
                                           self.visible_tags, False, False)

        # Reducing the list of scans to selection
        all_scans = self.table_data.scans_to_visualize

        self.table_data.scans_to_visualize = self.scan_list
        self.table_data.scans_to_search = self.scan_list
        self.table_data.update_visualized_rows(all_scans)

        # Filter information
        filter_to_apply = node.process.filter

        # Search
        self.rapid_search = RapidSearch(self)
        if filter_to_apply.search_bar:
            self.rapid_search.setText(filter_to_apply.search_bar)
        self.rapid_search.textChanged.connect(partial(self.search_str))

        self.advanced_search = AdvancedSearch(self.project,
                                              self,
                                              self.scan_list,
                                              self.visible_tags,
                                              from_pipeline=True)
        self.advanced_search.show_search()
        self.advanced_search.apply_filter(filter_to_apply)

        # Initialize Qt objects
        self.button_cross = QToolButton()
        self.push_button_tag_filter = QPushButton(TAG_FILENAME)

        self.layout_view()

    def layout_view(self):

        sources_images_dir = Config().getSourceImageDir()

        self.button_cross.setStyleSheet('background-color:rgb(255, 255, 255);')
        self.button_cross.setIcon(
            QIcon(os.path.join(sources_images_dir, 'gray_cross.png')))
        self.button_cross.clicked.connect(self.reset_search_bar)

        search_bar_layout = QHBoxLayout()
        search_bar_layout.addWidget(self.rapid_search)
        search_bar_layout.addWidget(self.button_cross)

        push_button_tags = QPushButton("Visualized tags")
        push_button_tags.clicked.connect(self.update_tags)

        self.push_button_tag_filter.clicked.connect(self.update_tag_to_filter)

        push_button_ok = QPushButton("OK")
        push_button_ok.clicked.connect(self.ok_clicked)

        push_button_cancel = QPushButton("Cancel")
        push_button_cancel.clicked.connect(self.close)

        # Layout
        buttons_layout = QHBoxLayout()
        buttons_layout.addWidget(push_button_tags)
        buttons_layout.addWidget(self.push_button_tag_filter)
        buttons_layout.addStretch(1)
        buttons_layout.addWidget(push_button_ok)
        buttons_layout.addWidget(push_button_cancel)

        main_layout = QVBoxLayout()
        main_layout.addLayout(search_bar_layout)
        main_layout.addWidget(self.advanced_search)
        main_layout.addWidget(self.table_data)
        main_layout.addLayout(buttons_layout)

        self.setLayout(main_layout)

        screen_resolution = QApplication.instance().desktop().screenGeometry()
        width, height = screen_resolution.width(), screen_resolution.height()
        self.setMinimumWidth(0.6 * width)
        self.setMinimumHeight(0.8 * height)

    def ok_clicked(self):
        """
        Set the filter to the process and closes the widget

        """
        if isinstance(self.process, ProcessMIA):
            (fields, conditions, values, links,
             nots) = self.advanced_search.get_filters(False)
            filt = Filter(None, nots, values, fields, links, conditions,
                          self.rapid_search.text())
            self.process.filter = filt

        self.set_output_value()
        self.close()

    def reset_search_bar(self):
        """
        Reset the search bar of the rapid search

        """
        self.rapid_search.setText("")
        self.advanced_search.rows = []
        self.advanced_search.show_search()

        # All rows reput
        old_scan_list = self.table_data.scans_to_visualize
        self.table_data.scans_to_visualize = self.scan_list
        self.table_data.scans_to_search = self.scan_list
        self.table_data.update_visualized_rows(old_scan_list)

    def search_str(self, str_search):
        """
        Update the files to display in the browser

        :param str_search: string typed in the rapid search
        """

        old_scan_list = self.table_data.scans_to_visualize

        # Every scan taken if empty search
        if str_search == "":
            return_list = self.table_data.scans_to_search
        else:
            # Scans with at least a not defined value
            if str_search == not_defined_value:
                filter = self.prepare_not_defined_filter(
                    self.project.session.get_shown_tags())
            # Scans matching the search
            else:
                filter = self.rapid_search.prepare_filter(
                    str_search, self.project.session.get_shown_tags(),
                    old_scan_list)

            generator = self.project.session.filter_documents(
                COLLECTION_CURRENT, filter)

            # Creating the list of scans
            return_list = [getattr(scan, TAG_FILENAME) for scan in generator]

        self.table_data.scans_to_visualize = return_list
        self.advanced_search.scans_list = return_list

        # Rows updated
        self.table_data.update_visualized_rows(old_scan_list)

    def set_output_value(self):
        """
        Set the output of the filter to the output of the node

        """

        result_names = []
        filter = self.table_data.get_current_filter()
        for i in range(len(filter)):
            scan_name = filter[i]
            tag_name = self.push_button_tag_filter.text()
            value = self.project.session.get_value(COLLECTION_CURRENT,
                                                   scan_name, tag_name)
            if tag_name == TAG_FILENAME:
                value = os.path.abspath(
                    os.path.join(self.project.folder, value))
            result_names.append(value)

        result_files = []
        for result_name in result_names:
            full_path = os.path.abspath(
                os.path.join(self.project.folder, result_name))
            result_files.append(full_path)

        self.node.set_plug_value("output", result_files)

    def update_tag_to_filter(self):
        """
        Update the tag to Filter

        """

        pop_up = PopUpSelectTagCountTable(self.project, self.visible_tags,
                                          self.push_button_tag_filter.text())
        if pop_up.exec_():
            self.push_button_tag_filter.setText(pop_up.selected_tag)

    def update_tags(self):
        """
        Update the list of visualized tags

        """

        dialog = QDialog()
        visualized_tags = PopUpVisualizedTags(self.project, self.visible_tags)
        layout = QVBoxLayout()
        layout.addWidget(visualized_tags)
        buttons_layout = QHBoxLayout()
        buttons = QDialogButtonBox(QDialogButtonBox.Ok
                                   | QDialogButtonBox.Cancel)
        buttons.accepted.connect(dialog.accept)
        buttons.rejected.connect(dialog.reject)
        buttons_layout.addWidget(buttons)
        layout.addLayout(buttons_layout)
        dialog.setLayout(layout)
        dialog.show()
        dialog.setMinimumHeight(600)
        dialog.setMinimumWidth(600)
        if dialog.exec():
            new_visibilities = []
            for x in range(visualized_tags.list_widget_selected_tags.count()):
                visible_tag = visualized_tags.list_widget_selected_tags.item(
                    x).text()
                new_visibilities.append(visible_tag)
            new_visibilities.append(TAG_FILENAME)
            self.table_data.update_visualized_columns(self.visible_tags,
                                                      new_visibilities)
            self.node_controller.visibles_tags = new_visibilities
            for row in self.advanced_search.rows:
                fields = row[2]
                fields.clear()
                for visible_tag in new_visibilities:
                    fields.addItem(visible_tag)
                fields.model().sort(0)
                fields.addItem("All visualized tags")
    def __init__(self, project, scans_list, process, node_name, plug_name,
                 node_controller, main_window):
        """
        Initialization of the PlugFilter widget

        :param project: current project in the software
        :param scans_list: list of database files to filter
        :param process: process instance of the selected node
        :param node_name: name of the current node
        :param plug_name: name of the selected node plug
        :param node_controller: parent node controller
        :param main_window: parent main window
        """

        super(PlugFilter, self).__init__(None)

        from populse_mia.user_interface.data_browser.rapid_search import \
            RapidSearch
        from populse_mia.data_manager.project import \
            COLLECTION_CURRENT

        self.project = project
        self.node_controller = node_controller
        self.main_window = main_window
        self.process = process
        self.plug_name = plug_name

        # If the filter is saved in the node plug (not the case now)
        # if hasattr(self.process, 'filters'):
        #    if self.plug_name in self.process.filters.keys():
        #         print("Already a filter for {0} plug of {1} process".format(
        #                 self.plug_name, self.process.name))
        #         # TODO: fill the advanced search with the corresponding
        #          filter:orphan:

        # Verifying that the scan names begin not with a "/" or a "\"
        if scans_list:
            scans_list_copy = []
            for scan in scans_list:
                scan_no_pfolder = scan.replace(self.project.folder, "")
                if scan_no_pfolder[0] in ["\\", "/"]:
                    scan_no_pfolder = scan_no_pfolder[1:]
                scans_list_copy.append(scan_no_pfolder)

            self.scans_list = scans_list_copy

        # If there is no element in scans_list, this means that all the scans
        # of the database needs to be taken into account
        else:
            self.scans_list = self.project.session.get_documents_names(
                COLLECTION_CURRENT)

        self.setWindowTitle("Filter - " + node_name + " - " + plug_name)

        # Graphical components
        self.table_data = TableDataBrowser(self.project,
                                           self,
                                           self.node_controller.visibles_tags,
                                           False,
                                           True,
                                           link_viewer=False)

        # Reducing the list of scans to selection
        all_scans = self.table_data.scans_to_visualize
        self.table_data.scans_to_visualize = self.scans_list
        self.table_data.scans_to_search = self.scans_list
        self.table_data.update_visualized_rows(all_scans)

        search_bar_layout = QHBoxLayout()

        self.rapid_search = RapidSearch(self)
        self.rapid_search.textChanged.connect(partial(self.search_str))

        sources_images_dir = Config().getSourceImageDir()
        self.button_cross = QToolButton()
        self.button_cross.setStyleSheet('background-color:rgb(255, 255, 255);')
        self.button_cross.setIcon(
            QIcon(os.path.join(sources_images_dir, 'gray_cross.png')))
        self.button_cross.clicked.connect(self.reset_search_bar)

        search_bar_layout.addWidget(self.rapid_search)
        search_bar_layout.addWidget(self.button_cross)

        self.advanced_search = AdvancedSearch(
            self.project,
            self,
            self.scans_list,
            self.node_controller.visibles_tags,
            from_pipeline=True)
        self.advanced_search.show_search()

        push_button_tags = QPushButton("Visualized tags")
        push_button_tags.clicked.connect(self.update_tags)

        self.push_button_tag_filter = QPushButton(TAG_FILENAME)
        self.push_button_tag_filter.clicked.connect(self.update_tag_to_filter)

        push_button_ok = QPushButton("OK")
        push_button_ok.clicked.connect(self.ok_clicked)

        push_button_cancel = QPushButton("Cancel")
        push_button_cancel.clicked.connect(self.close)

        # Layout
        buttons_layout = QHBoxLayout()
        buttons_layout.addWidget(push_button_tags)
        buttons_layout.addWidget(self.push_button_tag_filter)
        buttons_layout.addStretch(1)
        buttons_layout.addWidget(push_button_ok)
        buttons_layout.addWidget(push_button_cancel)

        main_layout = QVBoxLayout()
        main_layout.addLayout(search_bar_layout)
        main_layout.addWidget(self.advanced_search)
        main_layout.addWidget(self.table_data)
        main_layout.addLayout(buttons_layout)

        self.setLayout(main_layout)

        screen_resolution = QApplication.instance().desktop().screenGeometry()
        width, height = screen_resolution.width(), screen_resolution.height()
        self.setMinimumWidth(0.6 * width)
        self.setMinimumHeight(0.8 * height)
class PlugFilter(QWidget):
    """Filter widget used on a node plug

    The widget displays a browser with the selected files of the database,
    a rapid search and an advanced search to filter these files. Once the
    filtering is done, the result (as a list of files) is set to the plug.

    Methods:
        - ok_clicked: set the new value to the node plug and closes the widget
        - update_tag_to_filter: update the tag to Filter
        - update_tags: update the list of visualized tags
        - reset_search_bar: reset the search bar of the rapid search
        - search_str: update the files to display in the browser
        - set_plug_value: emit a signal to set the file names to the node plug
    """

    plug_value_changed = pyqtSignal(list)

    def __init__(self, project, scans_list, process, node_name, plug_name,
                 node_controller, main_window):
        """
        Initialization of the PlugFilter widget

        :param project: current project in the software
        :param scans_list: list of database files to filter
        :param process: process instance of the selected node
        :param node_name: name of the current node
        :param plug_name: name of the selected node plug
        :param node_controller: parent node controller
        :param main_window: parent main window
        """

        super(PlugFilter, self).__init__(None)

        from populse_mia.user_interface.data_browser.rapid_search import \
            RapidSearch
        from populse_mia.data_manager.project import \
            COLLECTION_CURRENT

        self.project = project
        self.node_controller = node_controller
        self.main_window = main_window
        self.process = process
        self.plug_name = plug_name

        # If the filter is saved in the node plug (not the case now)
        # if hasattr(self.process, 'filters'):
        #    if self.plug_name in self.process.filters.keys():
        #         print("Already a filter for {0} plug of {1} process".format(
        #                 self.plug_name, self.process.name))
        #         # TODO: fill the advanced search with the corresponding
        #          filter:orphan:

        # Verifying that the scan names begin not with a "/" or a "\"
        if scans_list:
            scans_list_copy = []
            for scan in scans_list:
                scan_no_pfolder = scan.replace(self.project.folder, "")
                if scan_no_pfolder[0] in ["\\", "/"]:
                    scan_no_pfolder = scan_no_pfolder[1:]
                scans_list_copy.append(scan_no_pfolder)

            self.scans_list = scans_list_copy

        # If there is no element in scans_list, this means that all the scans
        # of the database needs to be taken into account
        else:
            self.scans_list = self.project.session.get_documents_names(
                COLLECTION_CURRENT)

        self.setWindowTitle("Filter - " + node_name + " - " + plug_name)

        # Graphical components
        self.table_data = TableDataBrowser(self.project,
                                           self,
                                           self.node_controller.visibles_tags,
                                           False,
                                           True,
                                           link_viewer=False)

        # Reducing the list of scans to selection
        all_scans = self.table_data.scans_to_visualize
        self.table_data.scans_to_visualize = self.scans_list
        self.table_data.scans_to_search = self.scans_list
        self.table_data.update_visualized_rows(all_scans)

        search_bar_layout = QHBoxLayout()

        self.rapid_search = RapidSearch(self)
        self.rapid_search.textChanged.connect(partial(self.search_str))

        sources_images_dir = Config().getSourceImageDir()
        self.button_cross = QToolButton()
        self.button_cross.setStyleSheet('background-color:rgb(255, 255, 255);')
        self.button_cross.setIcon(
            QIcon(os.path.join(sources_images_dir, 'gray_cross.png')))
        self.button_cross.clicked.connect(self.reset_search_bar)

        search_bar_layout.addWidget(self.rapid_search)
        search_bar_layout.addWidget(self.button_cross)

        self.advanced_search = AdvancedSearch(
            self.project,
            self,
            self.scans_list,
            self.node_controller.visibles_tags,
            from_pipeline=True)
        self.advanced_search.show_search()

        push_button_tags = QPushButton("Visualized tags")
        push_button_tags.clicked.connect(self.update_tags)

        self.push_button_tag_filter = QPushButton(TAG_FILENAME)
        self.push_button_tag_filter.clicked.connect(self.update_tag_to_filter)

        push_button_ok = QPushButton("OK")
        push_button_ok.clicked.connect(self.ok_clicked)

        push_button_cancel = QPushButton("Cancel")
        push_button_cancel.clicked.connect(self.close)

        # Layout
        buttons_layout = QHBoxLayout()
        buttons_layout.addWidget(push_button_tags)
        buttons_layout.addWidget(self.push_button_tag_filter)
        buttons_layout.addStretch(1)
        buttons_layout.addWidget(push_button_ok)
        buttons_layout.addWidget(push_button_cancel)

        main_layout = QVBoxLayout()
        main_layout.addLayout(search_bar_layout)
        main_layout.addWidget(self.advanced_search)
        main_layout.addWidget(self.table_data)
        main_layout.addLayout(buttons_layout)

        self.setLayout(main_layout)

        screen_resolution = QApplication.instance().desktop().screenGeometry()
        width, height = screen_resolution.width(), screen_resolution.height()
        self.setMinimumWidth(0.6 * width)
        self.setMinimumHeight(0.8 * height)

    def ok_clicked(self):
        """
        Set the new value to the node plug and closes the widget

        """

        # To use if the filters are set on plugs, which is not the case
        # if isinstance(self.process, ProcessMIA):
        #     (fields, conditions, values, links, nots) =
        #     self.advanced_search.get_filters(False)
        #
        #
        #     plug_filter = Filter(None, nots, values, fields,
        #     links, conditions, "")
        #     self.process.filters[self.plug_name] = plug_filter

        self.set_plug_value()
        self.close()

    def reset_search_bar(self):
        """
        Reset the search bar of the rapid search

        """
        self.rapid_search.setText("")
        self.advanced_search.rows = []
        self.advanced_search.show_search()

        # All rows reput
        old_scan_list = self.table_data.scans_to_visualize
        self.table_data.scans_to_visualize = self.scans_list
        self.table_data.scans_to_search = self.scans_list
        self.table_data.update_visualized_rows(old_scan_list)

    def search_str(self, str_search):
        """
        Update the files to display in the browser
        :param str_search: string typed in the rapid search
        """

        from project.project import COLLECTION_CURRENT, TAG_FILENAME
        from data_browser.data_browser import not_defined_value

        old_scan_list = self.table_data.scans_to_visualize

        # Every scan taken if empty search
        if str_search == "":
            return_list = self.table_data.scans_to_search
        else:
            # Scans with at least a not defined value
            if str_search == not_defined_value:
                filter = self.prepare_not_defined_filter(
                    self.project.session.get_shown_tags())

            # Scans matching the search
            else:
                filter = self.rapid_search.prepare_filter(
                    str_search, self.project.session.get_shown_tags(),
                    self.table_data.scans_to_search)

            generator = self.project.session.filter_documents(
                COLLECTION_CURRENT, filter)

            # Creating the list of scans
            return_list = [getattr(scan, TAG_FILENAME) for scan in generator]

        self.table_data.scans_to_visualize = return_list
        self.advanced_search.scans_list = return_list

        # Rows updated
        self.table_data.update_visualized_rows(old_scan_list)

    def set_plug_value(self):
        """
        Emit a signal to set the file names to the node plug

        """

        result_names = []
        points = self.table_data.selectedIndexes()

        # If the use has selected some items
        if points:
            for point in points:
                row = point.row()
                tag_name = self.push_button_tag_filter.text()
                # We get the FileName of the scan from the first row
                scan_name = self.table_data.item(row, 0).text()
                value = self.project.session.get_value(COLLECTION_CURRENT,
                                                       scan_name, tag_name)
                if tag_name == TAG_FILENAME:
                    value = os.path.abspath(
                        os.path.join(self.project.folder, value))
                result_names.append(value)
        else:
            filter = self.table_data.get_current_filter()
            for i in range(len(filter)):
                scan_name = filter[i]
                tag_name = self.push_button_tag_filter.text()
                value = self.project.session.get_value(COLLECTION_CURRENT,
                                                       scan_name, tag_name)
                if tag_name == TAG_FILENAME:
                    value = os.path.abspath(
                        os.path.join(self.project.folder, value))
                result_names.append(value)

        self.plug_value_changed.emit(result_names)

    def update_tag_to_filter(self):
        """
        Update the tag to Filter

        """

        popUp = PopUpSelectTagCountTable(self.project,
                                         self.node_controller.visibles_tags,
                                         self.push_button_tag_filter.text())
        if popUp.exec_():
            self.push_button_tag_filter.setText(popUp.selected_tag)

    def update_tags(self):
        """
        Update the list of visualized tags

        """

        dialog = QDialog()
        visualized_tags = PopUpVisualizedTags(
            self.project, self.node_controller.visibles_tags)
        layout = QVBoxLayout()
        layout.addWidget(visualized_tags)
        buttons_layout = QHBoxLayout()
        buttons = QDialogButtonBox(QDialogButtonBox.Ok
                                   | QDialogButtonBox.Cancel)
        buttons.accepted.connect(dialog.accept)
        buttons.rejected.connect(dialog.reject)
        buttons_layout.addWidget(buttons)
        layout.addLayout(buttons_layout)
        dialog.setLayout(layout)
        dialog.show()
        dialog.setMinimumHeight(600)
        dialog.setMinimumWidth(600)
        if dialog.exec():
            new_visibilities = []
            for x in range(visualized_tags.list_widget_selected_tags.count()):
                visible_tag = visualized_tags.list_widget_selected_tags.item(
                    x).text()
                new_visibilities.append(visible_tag)
            new_visibilities.append(TAG_FILENAME)
            self.table_data.update_visualized_columns(
                self.node_controller.visibles_tags, new_visibilities)
            self.node_controller.visibles_tags = new_visibilities
            for row in self.advanced_search.rows:
                fields = row[2]
                fields.clear()
                for visible_tag in new_visibilities:
                    fields.addItem(visible_tag)
                fields.model().sort(0)
                fields.addItem("All visualized tags")
示例#6
0
class MiaViewer(Qt.QWidget, DataViewer):
    '''
    :class:`MIA data viewer <populse_mia.user_interface.data_viewer.data_viewer.DataViewer>`
    implementation based on `PyAnatomist <http://brainvisa.info/pyanatomist/sphinx/index.html>`_
    '''
    def __init__(self, init_global_handlers=None):

        super(MiaViewer, self).__init__()

        self.anaviewer = AnaSimpleViewer2(init_global_handlers)

        # count global number of viewers using anatomist, in order to close it
        # nicely
        if not hasattr(DataViewer, 'mia_viewers'):
            DataViewer.mia_viewers = 0
        DataViewer.mia_viewers += 1

        findChild = lambda x, y: Qt.QObject.findChild(x, Qt.QObject, y)

        awidget = self.anaviewer.awidget

        filter_action = findChild(awidget, 'filterAction')
        preferences_action = findChild(awidget, 'actionPreferences')

        filter_action.triggered.connect(self.filter_documents)
        preferences_action.triggered.connect(self.preferences)

        layout = Qt.QVBoxLayout()
        self.setLayout(layout)
        self.anaviewer.awidget.setSizePolicy(Qt.QSizePolicy.Expanding,
                                             Qt.QSizePolicy.Expanding)
        layout.addWidget(self.anaviewer.awidget)

        self.project = None
        self.documents = []
        self.displayed = []
        self.table_data = []

    def display_files(self, files):
        self.displayed += files
        self.anaviewer.loadObject(files)

    def displayed_files(self):
        return self.displayed

    def remove_files(self, files):
        self.anaviewer.deleteObjectsFromFiles(files)
        self.files = [doc for doc in self.displayed if doc not in files]

    def set_documents(self, project, documents):
        if self.project is not project:
            self.clear()
        self.project = project
        self.documents = documents

    def reset_search_bar(self):
        """Reset the rapid search bar."""
        self.search_bar.setText("")

    def search_str(self, str_search):
        """Search a string in the table and updates the vnot_defined_value = "*Not Defined*"isualized documents.

        :param str_search: string to search
        """

        old_scan_list = self.table_data.scans_to_visualize
        return_list = []

        # Every scan taken if empty search
        if str_search == "":
            return_list = self.table_data.scans_to_search
        else:
            # Scans with at least a not defined value
            if str_search == not_defined_value:
                filter = self.search_bar.prepare_not_defined_filter(
                    self.project.session.get_shown_tags())
            # Scans matching the search
            else:
                filter = self.search_bar.prepare_filter(
                    str_search, self.project.session.get_shown_tags(),
                    self.table_data.scans_to_search)

            generator = self.project.session.filter_documents(
                COLLECTION_CURRENT, filter)

            # Creating the list of scans
            return_list = [getattr(scan, TAG_FILENAME) for scan in generator]

        self.table_data.scans_to_visualize = return_list

        # Rows updated
        self.table_data.update_visualized_rows(old_scan_list)

        self.project.currentFilter.search_bar = str_search

    def filter_documents(self):
        '''Filter documents already loaded in the Databrowser
        '''
        dialog = Qt.QDialog()
        dialog.setWindowTitle('Filter documents')
        dialog.resize(1150, 500)
        layout = Qt.QVBoxLayout()
        dialog.setLayout(layout)

        #Some specific filtering
        #QLineEdit for research
        self.search_bar = RapidSearch(dialog)
        self.search_bar.textChanged.connect(self.search_str)
        #Cancel search button
        sources_images_dir = Config().getSourceImageDir()
        button_cross = QToolButton()
        button_cross.setStyleSheet('background-color:rgb(255, 255, 255);')
        button_cross.setIcon(
            QIcon(os.path.join(sources_images_dir, 'gray_cross.png')))
        button_cross.clicked.connect(self.reset_search_bar)

        title = Qt.QLabel()
        title.setText('Search by FileName: ')

        layout.addWidget(title)

        search_bar_layout = QHBoxLayout()
        search_bar_layout.addWidget(self.search_bar)
        search_bar_layout.addSpacing(3)
        search_bar_layout.addWidget(button_cross)
        #Add layout to dialogBox
        layout.addLayout(search_bar_layout)
        layout.addSpacing(8)

        self.table_data = TableDataBrowser(
            self.project,
            self,
            self.project.session.get_shown_tags(),
            False,
            True,
            link_viewer=False)
        layout.addWidget(self.table_data)
        hlay = Qt.QHBoxLayout()
        layout.addLayout(hlay)
        ok = Qt.QPushButton('Import')
        hlay.addWidget(ok)
        ok.clicked.connect(dialog.accept)
        ok.setDefault(True)
        cancel = Qt.QPushButton('Cancel')
        hlay.addWidget(cancel)
        cancel.clicked.connect(dialog.reject)
        hlay.addStretch(1)

        # Reducing the list of scans to selection
        all_scans = self.table_data.scans_to_visualize
        self.table_data.scans_to_visualize = self.documents
        self.table_data.scans_to_search = self.documents
        self.table_data.update_visualized_rows(all_scans)

        res = dialog.exec_()
        if res == Qt.QDialog.Accepted:
            points = self.table_data.selectedIndexes()
            result_names = []
            for point in points:
                row = point.row()
                # We get the FileName of the scan from the first row
                scan_name = self.table_data.item(row, 0).text()
                value = self.project.session.get_value(COLLECTION_CURRENT,
                                                       scan_name, TAG_FILENAME)
                value = os.path.abspath(
                    os.path.join(self.project.folder, value))
                result_names.append(value)
            self.display_files(result_names)

    def preferences(self):
        '''Preferences for the dataviewer
        '''
        #Get initial config:
        im_sec = Config().getViewerFramerate()
        config = Config().getViewerConfig()
        ref = Config().get_referential()

        dialog = Qt.QDialog()
        dialog.setWindowTitle('Preferences')
        dialog.resize(600, 400)
        layout = Qt.QVBoxLayout()
        layout.setContentsMargins(25, 25, 25, 25)
        dialog.setLayout(layout)

        #Change Neuro/Radio configuration
        config_layout = QHBoxLayout()
        title_config = Qt.QLabel()
        title_config.setText('Configuration: ')
        box = Qt.QComboBox()
        box.addItem('Neuro')
        box.addItem('Radio')
        config_layout.addWidget(title_config)
        config_layout.addWidget(box)
        if config == 'radio':
            box.setCurrentIndex(1)

        #set automatic time frame rate
        frame_rate_layout = QHBoxLayout()
        title = Qt.QLabel()
        title.setText('Automatic time image display:')
        slider = Qt.QSlider(Qt.Qt.Horizontal)
        slider.setRange(1, 100)
        slider.setValue(int(im_sec))
        size = QtCore.QSize(180, 15)
        slider.setMinimumSize(size)
        slow_label = Qt.QLabel()
        fast_label = Qt.QLabel()
        slow_label.setText('slow')
        fast_label.setText('fast')
        frame_rate_layout.addWidget(title)
        frame_rate_layout.addWidget(slow_label)
        frame_rate_layout.addWidget(slider)
        frame_rate_layout.addWidget(fast_label)
        frame_rate_layout.insertSpacing(1, 200)

        #Change referential
        ref_layout = QHBoxLayout()
        title_ref = Qt.QLabel()
        title_ref.setText('Referential: ')
        box2 = Qt.QComboBox()
        box2.addItem('World Coordinates')
        box2.addItem('Image referential')
        ref_layout.addWidget(title_ref)
        ref_layout.addWidget(box2)
        box2.setCurrentIndex(int(ref))

        #Set general vertical layout
        layout.addLayout(config_layout)
        layout.addLayout(frame_rate_layout)
        layout.addLayout(ref_layout)
        layout.addStretch(1)

        #Save and cancel buttons
        hlay = Qt.QHBoxLayout()
        layout.addLayout(hlay)
        ok = Qt.QPushButton('Save')
        hlay.addStretch(1)
        hlay.addWidget(ok)
        ok.clicked.connect(dialog.accept)
        ok.setDefault(True)
        cancel = Qt.QPushButton('Cancel')
        hlay.addWidget(cancel)
        cancel.clicked.connect(dialog.reject)
        hlay.addStretch(1)

        res = dialog.exec_()

        if res == Qt.QDialog.Accepted:
            new_config = box.currentText().lower()
            new_ref = box2.currentIndex()

            #Save Config parameters and reload images
            #when config and referential have changed
            Config().setViewerFramerate(slider.value())
            Config().setViewerConfig(new_config)
            Config().set_referential(new_ref)
            if new_config != config:
                self.anaviewer.changeConfig(new_config)
            if new_ref != ref:
                self.anaviewer.changeRef()

    def close(self):
        super(MiaViewer, self).close()
        close_ana = False
        DataViewer.mia_viewers -= 1  # dec count
        if DataViewer.mia_viewers == 0:
            close_ana = True
        self.anaviewer.closeAll(close_ana)
示例#7
0
    def filter_documents(self):
        '''Filter documents already loaded in the Databrowser
        '''
        dialog = Qt.QDialog()
        dialog.setWindowTitle('Filter documents')
        dialog.resize(1150, 500)
        layout = Qt.QVBoxLayout()
        dialog.setLayout(layout)

        #Some specific filtering
        #QLineEdit for research
        self.search_bar = RapidSearch(dialog)
        self.search_bar.textChanged.connect(self.search_str)
        #Cancel search button
        sources_images_dir = Config().getSourceImageDir()
        button_cross = QToolButton()
        button_cross.setStyleSheet('background-color:rgb(255, 255, 255);')
        button_cross.setIcon(
            QIcon(os.path.join(sources_images_dir, 'gray_cross.png')))
        button_cross.clicked.connect(self.reset_search_bar)

        title = Qt.QLabel()
        title.setText('Search by FileName: ')

        layout.addWidget(title)

        search_bar_layout = QHBoxLayout()
        search_bar_layout.addWidget(self.search_bar)
        search_bar_layout.addSpacing(3)
        search_bar_layout.addWidget(button_cross)
        #Add layout to dialogBox
        layout.addLayout(search_bar_layout)
        layout.addSpacing(8)

        self.table_data = TableDataBrowser(
            self.project,
            self,
            self.project.session.get_shown_tags(),
            False,
            True,
            link_viewer=False)
        layout.addWidget(self.table_data)
        hlay = Qt.QHBoxLayout()
        layout.addLayout(hlay)
        ok = Qt.QPushButton('Import')
        hlay.addWidget(ok)
        ok.clicked.connect(dialog.accept)
        ok.setDefault(True)
        cancel = Qt.QPushButton('Cancel')
        hlay.addWidget(cancel)
        cancel.clicked.connect(dialog.reject)
        hlay.addStretch(1)

        # Reducing the list of scans to selection
        all_scans = self.table_data.scans_to_visualize
        self.table_data.scans_to_visualize = self.documents
        self.table_data.scans_to_search = self.documents
        self.table_data.update_visualized_rows(all_scans)

        res = dialog.exec_()
        if res == Qt.QDialog.Accepted:
            points = self.table_data.selectedIndexes()
            result_names = []
            for point in points:
                row = point.row()
                # We get the FileName of the scan from the first row
                scan_name = self.table_data.item(row, 0).text()
                value = self.project.session.get_value(COLLECTION_CURRENT,
                                                       scan_name, TAG_FILENAME)
                value = os.path.abspath(
                    os.path.join(self.project.folder, value))
                result_names.append(value)
            self.display_files(result_names)