Changeset 1617:a67c39dc9477 in orange-bioinformatics for widgets/OWPIPAx.py


Ignore:
Timestamp:
05/09/12 15:10:00 (2 years ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Copied SelectionByKey class from OWPIPA into OWPIPAx (problems with pickling
due to Orange Canvas widget discovery).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • widgets/OWPIPAx.py

    r1616 r1617  
    1818 
    1919from OWPIPA import (MyTreeWidgetItem, ListItemDelegate, 
    20                     SelectionSetsWidget, SortedListWidget, 
    21                     SelectionByKey) 
     20                    SelectionSetsWidget, SortedListWidget) 
    2221 
    2322try: 
     
    6261 
    6362bufferfile = os.path.join(bufferpath, "database.sq3") 
     63 
     64 
     65class SelectionByKey(object): 
     66    """An object stores item selection by unique key values 
     67    (works only for row selections in list and table models) 
     68    Example:: 
     69 
     70        ## Save selection by unique tuple pairs (DisplayRole of column 1 and 2) 
     71        selection = SelectionsByKey(itemView.selectionModel().selection(), 
     72                                    key = (1,2)) 
     73        ## restore selection (Possibly omitting rows not present in the model) 
     74        selection.select(itemView.selectionModel()) 
     75 
     76    """ 
     77 
     78    def __init__(self, itemSelection, name="", key=(0,)): 
     79        self._key = key 
     80        self.name = name 
     81        self._selected_keys = [] 
     82        if itemSelection: 
     83            self.setSelection(itemSelection) 
     84 
     85    def _row_key(self, model, row): 
     86        def key(row, col): 
     87            return str(model.data(model.index(row, col), 
     88                                  Qt.DisplayRole).toString()) 
     89 
     90        return tuple(key(row, col) for col in self._key) 
     91 
     92    def setSelection(self, itemSelection): 
     93        self._selected_keys = [self._row_key(ind.model(), ind.row()) \ 
     94                               for ind in itemSelection.indexes() \ 
     95                               if ind.column() == 0] 
     96 
     97    def select(self, selectionModel): 
     98        model = selectionModel.model() 
     99        selectionModel.clear() 
     100        for i in range(model.rowCount()): 
     101            if self._row_key(model, i) in self._selected_keys: 
     102                selectionModel.select(model.index(i, 0), 
     103                    QItemSelectionModel.Select | QItemSelectionModel.Rows) 
     104 
     105    def __len__(self): 
     106        return len(self._selected_keys) 
     107 
    64108 
    65109# Mapping from PIPAx.results_list annotation keys to Header names. 
Note: See TracChangeset for help on using the changeset viewer.