Esempio n. 1
0
    def select(self, selection, flags):
        if isinstance(selection, QModelIndex):
            selection = QItemSelection(selection, selection)

        model = self.model()
        indexes = selection.indexes()
        sel_inds = {ind.row() for ind in indexes} | \
                   {ind.column() for ind in indexes}
        if flags == QItemSelectionModel.ClearAndSelect:
            selected = set()
        else:
            selected = {ind.row() for ind in self.selectedIndexes()}
        if flags & QItemSelectionModel.Select:
            selected |= sel_inds
        elif flags & QItemSelectionModel.Deselect:
            selected -= sel_inds
        new_selection = QItemSelection()
        regions = list(ranges(sorted(selected)))
        for r_start, r_end in regions:
            for c_start, c_end in regions:
                top_left = model.index(r_start, c_start)
                bottom_right = model.index(r_end - 1, c_end - 1)
                new_selection.select(top_left, bottom_right)
        QItemSelectionModel.select(self, new_selection,
                                   QItemSelectionModel.ClearAndSelect)
Esempio n. 2
0
    def select(self, selection, flags):
        if isinstance(selection, QModelIndex):
            selection = QItemSelection(selection, selection)

        model = self.model()
        indexes = selection.indexes()
        sel_inds = {ind.row() for ind in indexes} | \
                   {ind.column() for ind in indexes}
        if flags == QItemSelectionModel.ClearAndSelect:
            selected = set()
        else:
            selected = {ind.row() for ind in self.selectedIndexes()}
        if flags & QItemSelectionModel.Select:
            selected |= sel_inds
        elif flags & QItemSelectionModel.Deselect:
            selected -= sel_inds
        new_selection = QItemSelection()
        regions = list(ranges(sorted(selected)))
        for r_start, r_end in regions:
            for c_start, c_end in regions:
                top_left = model.index(r_start, c_start)
                bottom_right = model.index(r_end - 1, c_end - 1)
                new_selection.select(top_left, bottom_right)
        QItemSelectionModel.select(self, new_selection,
                                   QItemSelectionModel.ClearAndSelect)
Esempio n. 3
0
 def select(self,
            uuids,
            lbox: QTreeView = None,
            scroll_to_show_single=True):
     lbox = self.current_set_listbox if lbox is None else lbox
     lbox.clearSelection()
     if not uuids:
         return
     # FUTURE: this is quick and dirty
     rowdict = dict(
         (u, i) for i, u in enumerate(self.doc.current_layer_uuid_order))
     items = QItemSelection()
     q = None
     for uuid in uuids:
         row = rowdict.get(uuid, None)
         if row is None:
             LOG.error(
                 'UUID {} cannot be selected in list view'.format(uuid))
             continue
         q = self.createIndex(row, 0)
         items.select(q, q)
         lbox.selectionModel().select(items, QItemSelectionModel.Select)
         # lbox.setCurrentIndex(q)
     if scroll_to_show_single and len(uuids) == 1 and q is not None:
         lbox.scrollTo(q)
Esempio n. 4
0
 def _set_selection(self):
     selection = QItemSelection()
     index = self.tableview.model().index
     for row, col in self.selection:
         sel = index(row + 2, col + 2)
         selection.select(sel, sel)
     self.tableview.selectionModel().select(
         selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 5
0
 def _set_selection(self):
     selection = QItemSelection()
     index = self.tableview.model().index
     for row, col in self.selection:
         sel = index(row + 2, col + 2)
         selection.select(sel, sel)
     self.tableview.selectionModel().select(
         selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 6
0
    def select_correct(self):
        selection = QItemSelection()
        n = self.tablemodel.rowCount()
        for i in range(n):
            index = self.tablemodel.index(i, i)
            selection.select(index, index)

        self.tableview.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 7
0
 def on_actionSelect_odd_images_triggered(self):
     widget = self.activ_selection
     model = widget.model()
     selection = widget.selectionModel()
     sel = QItemSelection()
     for i in  range(0,len(model),2):
         sel.select(model.index(i,0), model.index(i,1))
     selection.select(sel, QItemSelectionModel.ClearAndSelect)
     widget.viewport().update()
Esempio n. 8
0
 def select_correct(self):
     """Select the diagonal elements of the matrix"""
     selection = QItemSelection()
     n = self.tablemodel.rowCount()
     for i in range(2, n):
         index = self.tablemodel.index(i, i)
         selection.select(index, index)
     self.tableview.selectionModel().select(
         selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 9
0
 def select_correct(self):
     """Select the diagonal elements of the matrix"""
     selection = QItemSelection()
     n = self.tablemodel.rowCount()
     for i in range(2, n):
         index = self.tablemodel.index(i, i)
         selection.select(index, index)
     self.tableview.selectionModel().select(
         selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 10
0
    def select_correct(self):
        selection = QItemSelection()
        n = self.tablemodel.rowCount()
        for i in range(2, n):
            index = self.tablemodel.index(i, i)
            selection.select(index, index)

        self.tableview.selectionModel().select(
            selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 11
0
 def _restoreSelection(self):
     newSelection = QItemSelection()
     for index in self.model.selected_indexes:
         newSelection.select(self.createIndex(index, 0), self.createIndex(index, 0))
     self.view.selectionModel().select(newSelection, QItemSelectionModel.ClearAndSelect)
     if len(newSelection.indexes()):
         currentIndex = newSelection.indexes()[0]
         self.view.selectionModel().setCurrentIndex(currentIndex, QItemSelectionModel.Current)
         self.view.scrollTo(currentIndex)
Esempio n. 12
0
 def _learner_changed(self):
     # The selected learner has changed
     indices = self.tableview.selectedIndexes()
     self._update()
     selection = QItemSelection()
     for sel in indices:
         selection.select(sel, sel)
     self.tableview.selectionModel().select(
         selection, QItemSelectionModel.ClearAndSelect)
     self.commit()
Esempio n. 13
0
 def _learner_changed(self):
     # The selected learner has changed
     indices = self.tableview.selectedIndexes()
     self._update()
     selection = QItemSelection()
     for sel in indices:
         selection.select(sel, sel)
     self.tableview.selectionModel().select(
         selection, QItemSelectionModel.ClearAndSelect
     )
     self.commit()
Esempio n. 14
0
 def _updateViewSelection(self):
     # Takes the selection on the model's side and update the view with it.
     newSelection = QItemSelection()
     columnCount = self.columnCount(QModelIndex())
     for index in self.model.selected_indexes:
         newSelection.select(self.createIndex(index, 0), self.createIndex(index, columnCount-1))
     self.view.selectionModel().select(newSelection, QItemSelectionModel.ClearAndSelect)
     if len(newSelection.indexes()):
         currentIndex = newSelection.indexes()[0]
         self.view.selectionModel().setCurrentIndex(currentIndex, QItemSelectionModel.Current)
         self.view.scrollTo(currentIndex)
Esempio n. 15
0
 def _updateViewSelection(self):
     # Takes the selection on the model's side and update the view with it.
     newSelection = QItemSelection()
     columnCount = self.columnCount(QModelIndex())
     for index in self.model.selected_indexes:
         newSelection.select(self.createIndex(index, 0), self.createIndex(index, columnCount-1))
     self.view.selectionModel().select(newSelection, QItemSelectionModel.ClearAndSelect)
     if len(newSelection.indexes()):
         currentIndex = newSelection.indexes()[0]
         self.view.selectionModel().setCurrentIndex(currentIndex, QItemSelectionModel.Current)
         self.view.scrollTo(currentIndex)
Esempio n. 16
0
 def set_selection(self):
     if len(self.selected_rows) and len(self.selected_cols):
         view = self.tabs.currentWidget()
         selection = QItemSelection()
         temp_selection = QItemSelection()
         for row in self.selected_rows:
             for col in self.selected_cols:
                 index = view.model().index(row, col)
                 temp_selection.select(index, index)
                 selection.merge(temp_selection, QItemSelectionModel.Select)
         view.selectionModel().select(selection,
                                      QItemSelectionModel.ClearAndSelect)
Esempio n. 17
0
    def select_wrong(self):
        selection = QItemSelection()
        n = self.tablemodel.rowCount()

        for i in range(n):
            for j in range(i + 1, n):
                index = self.tablemodel.index(i, j)
                selection.select(index, index)
                index = self.tablemodel.index(j, i)
                selection.select(index, index)

        self.tableview.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 18
0
        def select(model, selection_model, selected_items):
            all_items = list(model)
            try:
                indices = [all_items.index(item) for item in selected_items]
            except:
                indices = []
            selection = QItemSelection()
            for ind in indices:
                index = model.index(ind)
                selection.select(index, index)

            selection_model.select(selection, QItemSelectionModel.Select)
Esempio n. 19
0
 def set_selection(self):
     if len(self.selected_rows) and len(self.selected_cols):
         view = self.tabs.currentWidget()
         selection = QItemSelection()
         temp_selection = QItemSelection()
         for row in self.selected_rows:
             for col in self.selected_cols:
                 index = view.model().index(row, col)
                 temp_selection.select(index, index)
                 selection.merge(temp_selection, QItemSelectionModel.Select)
         view.selectionModel().select(selection,
                                      QItemSelectionModel.ClearAndSelect)
Esempio n. 20
0
        def select(model, selection_model, selected_items):
            all_items = list(model)
            try:
                indices = [all_items.index(item) for item in selected_items]
            except:
                indices = []
            selection = QItemSelection()
            for ind in indices:
                index = model.index(ind)
                selection.select(index, index)

            selection_model.select(selection, QItemSelectionModel.Select)
Esempio n. 21
0
 def select_wrong(self):
     """Select the off-diagonal elements of the matrix"""
     selection = QItemSelection()
     n = self.tablemodel.rowCount()
     for i in range(2, n):
         for j in range(i + 1, n):
             index = self.tablemodel.index(i, j)
             selection.select(index, index)
             index = self.tablemodel.index(j, i)
             selection.select(index, index)
     self.tableview.selectionModel().select(
         selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 22
0
 def select_wrong(self):
     """Select the off-diagonal elements of the matrix"""
     selection = QItemSelection()
     n = self.tablemodel.rowCount()
     for i in range(2, n):
         for j in range(i + 1, n):
             index = self.tablemodel.index(i, j)
             selection.select(index, index)
             index = self.tablemodel.index(j, i)
             selection.select(index, index)
     self.tableview.selectionModel().select(
         selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 23
0
    def select_wrong(self):
        selection = QItemSelection()
        n = self.tablemodel.rowCount()

        for i in range(2, n):
            for j in range(i + 1, n):
                index = self.tablemodel.index(i, j)
                selection.select(index, index)
                index = self.tablemodel.index(j, i)
                selection.select(index, index)

        self.tableview.selectionModel().select(
            selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 24
0
    def selectionChanged(self):
        X = self.X
        mapping = self.onehot_mapping
        instances = set()
        where = np.where

        def whole_subtree(node):
            yield node
            for i in range(node.childCount()):
                yield from whole_subtree(node.child(i))

        def itemset(node):
            while node:
                yield node.data(0, self.ITEM_DATA_ROLE)
                node = node.parent()

        def selection_ranges(node):
            n_children = node.childCount()
            if n_children:
                yield (self.tree.indexFromItem(node.child(0)),
                       self.tree.indexFromItem(node.child(n_children - 1)))
            for i in range(n_children):
                yield from selection_ranges(node.child(i))

        nSelectedItemsets = 0
        item_selection = QItemSelection()
        for node in self.tree.selectedItems():
            nodes = (node, ) if node.isExpanded() else whole_subtree(node)
            if not node.isExpanded():
                for srange in selection_ranges(node):
                    item_selection.select(*srange)
            for node in nodes:
                nSelectedItemsets += 1
                cols, vals = zip(*(mapping[i] for i in itemset(node)))
                if issparse(X):
                    rows = (len(cols) == np.bincount(
                        (X[:, cols] != 0).indices,
                        minlength=X.shape[0])).nonzero()[0]
                else:
                    rows = where((X[:, cols] == vals).all(axis=1))[0]
                instances.update(rows)
        self.tree.itemSelectionChanged.disconnect(self.selectionChanged)
        self.tree.selectionModel().select(
            item_selection,
            QItemSelectionModel.Select | QItemSelectionModel.Rows)
        self.tree.itemSelectionChanged.connect(self.selectionChanged)

        self.nSelectedExamples = len(instances)
        self.nSelectedItemsets = nSelectedItemsets
        self.output = self.data[sorted(instances)] or None
        self.commit()
Esempio n. 25
0
    def selectFiltered(self):
        if not self.data:
            return
        itemSelection = QItemSelection()

        index = self.treeWidget.model().sourceModel().index
        mapFromSource = self.treeWidget.model().mapFromSource
        for i, row in enumerate(self.cells):
            if not self.rowFiltered(i):
                itemSelection.select(mapFromSource(index(i, 0)),
                                     mapFromSource(index(i, 0)))
        self.treeWidget.selectionModel().select(
            itemSelection,
            QItemSelectionModel.Select | QItemSelectionModel.Rows)
Esempio n. 26
0
    def selectLargePathSegments(self):
        selection = QItemSelection()
        for path in self.largePathSegmentsModel.selectedGroundings():

            for i in range(self.pathSegmentsModel.rowCount()):
                entry = self.pathSegmentsModel.get(i)
                if path.contains(entry.grounding.range):
                    idx = self.pathSegmentsModel.index(i, 0)
                    selection.select(idx, idx)
                    #self.pathSegmentsTable.selectRow(i)
                    #break
        print 'adding path', selection
        self.pathSegmentsTable.selectionModel().select(selection,
                                                       QItemSelectionModel.Rows | QItemSelectionModel.SelectCurrent)
Esempio n. 27
0
    def selectFiltered(self):
        if not self.data:
            return
        itemSelection = QItemSelection()

        index = self.treeWidget.model().sourceModel().index
        mapFromSource = self.treeWidget.model().mapFromSource
        for i, row in enumerate(self.cells):
            if not self.rowFiltered(i):
                itemSelection.select(mapFromSource(index(i, 0)),
                                     mapFromSource(index(i, 0)))
        self.treeWidget.selectionModel().select(
            itemSelection,
            QItemSelectionModel.Select | QItemSelectionModel.Rows)
Esempio n. 28
0
def select_rows(view, row_indices, command=QItemSelectionModel.ClearAndSelect):
    """
    Select rows in view.

    :param PyQt4.QtGui.QAbstractItemView view:
    :param row_indices: Integer indices of rows to select.
    :param command: QItemSelectionModel.SelectionFlags
    """
    selmodel = view.selectionModel()
    model = view.model()
    selection = QItemSelection()
    for row in row_indices:
        index = model.index(row, 0)
        selection.select(index, index)
    selmodel.select(selection, command | QItemSelectionModel.Rows)
Esempio n. 29
0
def select_rows(view, row_indices, command=QItemSelectionModel.ClearAndSelect):
    """
    Select rows in view.

    :param PyQt4.QtGui.QAbstractItemView view:
    :param row_indices: Integer indices of rows to select.
    :param command: QItemSelectionModel.SelectionFlags
    """
    selmodel = view.selectionModel()
    model = view.model()
    selection = QItemSelection()
    for row in row_indices:
        index = model.index(row, 0)
        selection.select(index, index)
    selmodel.select(selection, command | QItemSelectionModel.Rows)
Esempio n. 30
0
    def selectionChanged(self):
        X = self.X
        mapping = self.onehot_mapping
        instances = set()
        where = np.where

        def whole_subtree(node):
            yield node
            for i in range(node.childCount()):
                yield from whole_subtree(node.child(i))

        def itemset(node):
            while node:
                yield node.data(0, self.ITEM_DATA_ROLE)
                node = node.parent()

        def selection_ranges(node):
            n_children = node.childCount()
            if n_children:
                yield (self.tree.indexFromItem(node.child(0)), self.tree.indexFromItem(node.child(n_children - 1)))
            for i in range(n_children):
                yield from selection_ranges(node.child(i))

        nSelectedItemsets = 0
        item_selection = QItemSelection()
        for node in self.tree.selectedItems():
            nodes = (node,) if node.isExpanded() else whole_subtree(node)
            if not node.isExpanded():
                for srange in selection_ranges(node):
                    item_selection.select(*srange)
            for node in nodes:
                nSelectedItemsets += 1
                cols, vals = zip(*(mapping[i] for i in itemset(node)))
                if issparse(X):
                    rows = (len(cols) == np.bincount((X[:, cols] != 0).indices, minlength=X.shape[0])).nonzero()[0]
                else:
                    rows = where((X[:, cols] == vals).all(axis=1))[0]
                instances.update(rows)
        self.tree.itemSelectionChanged.disconnect(self.selectionChanged)
        self.tree.selectionModel().select(item_selection, QItemSelectionModel.Select | QItemSelectionModel.Rows)
        self.tree.itemSelectionChanged.connect(self.selectionChanged)

        self.nSelectedExamples = len(instances)
        self.nSelectedItemsets = nSelectedItemsets
        self.output = self.data[sorted(instances)] or None
        self.commit()
Esempio n. 31
0
 def word_clicked(self, word):
     """Called from JavaScript"""
     if not word:
         self.selected_words.clear()
         return ''
     selection = QItemSelection()
     for i, row in enumerate(self.tablemodel):
         for j, val in enumerate(row):
             if val == word:
                 index = self.tablemodel.index(i, j)
                 selection.select(index, index)
     if word not in self.selected_words:
         self.selected_words.add(word)
         self.tableview.selectionModel().select(
             selection, QItemSelectionModel.Select | QItemSelectionModel.Rows)
         return 'selected'
     else:
         self.selected_words.remove(word)
         self.tableview.selectionModel().select(
             selection, QItemSelectionModel.Deselect | QItemSelectionModel.Rows)
         return ''
Esempio n. 32
0
 def word_clicked(self, word):
     """Called from JavaScript"""
     if not word:
         self.selected_words.clear()
         return ''
     selection = QItemSelection()
     for i, row in enumerate(self.tablemodel):
         for j, val in enumerate(row):
             if val == word:
                 index = self.tablemodel.index(i, j)
                 selection.select(index, index)
     if word not in self.selected_words:
         self.selected_words.add(word)
         self.tableview.selectionModel().select(
             selection,
             QItemSelectionModel.Select | QItemSelectionModel.Rows)
         return 'selected'
     else:
         self.selected_words.remove(word)
         self.tableview.selectionModel().select(
             selection,
             QItemSelectionModel.Deselect | QItemSelectionModel.Rows)
         return ''
Esempio n. 33
0
 def sort_by_column(self, index):
     table = self.tabs.currentWidget()
     if index == table.oldSortingIndex and index != -1:
         order = (table.oldSortingOrder == QtCore.Qt.AscendingOrder and
                  QtCore.Qt.DescendingOrder or QtCore.Qt.AscendingOrder)
     else:
         order = QtCore.Qt.AscendingOrder
     oldsel = self.get_current_selection()
     model = table.model()
     if index == -1:
         table.horizontalHeader().setSortIndicatorShown(False)
         model.reset_sort()
     else:
         table.horizontalHeader().setSortIndicatorShown(1)
         table.sortByColumn(index, order)
     newsort = sorted(enumerate(model.sorted_map), key=lambda x: x[1])
     newsel = [ newsort[a][0] for a in oldsel ]
     itemsel = QItemSelection()
     for a in newsel:
         itemsel.select(model.index(a, 0), model.index(a, 0))
     table.selectionModel().select(itemsel, QItemSelectionModel.Rows | \
         QItemSelectionModel.Select | QItemSelectionModel.Clear)
     table.oldSortingIndex = index
     table.oldSortingOrder = order
Esempio n. 34
0
 def sort_by_column(self, index):
     table = self.tabs.currentWidget()
     if index == table.oldSortingIndex and index != -1:
         order = (table.oldSortingOrder == QtCore.Qt.AscendingOrder
                  and QtCore.Qt.DescendingOrder or QtCore.Qt.AscendingOrder)
     else:
         order = QtCore.Qt.AscendingOrder
     oldsel = self.get_current_selection()
     model = table.model()
     if index == -1:
         table.horizontalHeader().setSortIndicatorShown(False)
         model.reset_sort()
     else:
         table.horizontalHeader().setSortIndicatorShown(1)
         table.sortByColumn(index, order)
     newsort = sorted(enumerate(model.sorted_map), key=lambda x: x[1])
     newsel = [newsort[a][0] for a in oldsel]
     itemsel = QItemSelection()
     for a in newsel:
         itemsel.select(model.index(a, 0), model.index(a, 0))
     table.selectionModel().select(itemsel, QItemSelectionModel.Rows | \
         QItemSelectionModel.Select | QItemSelectionModel.Clear)
     table.oldSortingIndex = index
     table.oldSortingOrder = order
Esempio n. 35
0
 def set_selected_items(self, inds):
     index = self.model().index
     selection = QItemSelection()
     for i in inds:
         selection.select(index(i, i), index(i, i))
     self.select(selection, QItemSelectionModel.ClearAndSelect)
Esempio n. 36
0
 def set_selected_items(self, inds):
     index = self.model().index
     selection = QItemSelection()
     for i in inds:
         selection.select(index(i, i), index(i, i))
     self.select(selection, QItemSelectionModel.ClearAndSelect)