Exemple #1
0
class DataModel(QAbstractTableModel):
    """Data manager for the browser table.

    _items -- The card or note ids currently hold and corresponding to the
              table's rows.
    _rows -- The cached data objects to render items to rows.
    columns -- The data objects of all available columns, used to define the display
               of active columns and list all toggleable columns to the user.
    _block_updates -- If True, serve stale content to avoid hitting the DB.
    _stale_cutoff -- A threshold to decide whether a cached row has gone stale.
    """
    def __init__(
        self,
        parent: QObject,
        col: Collection,
        state: ItemState,
        row_state_will_change_callback: Callable,
        row_state_changed_callback: Callable,
    ) -> None:
        super().__init__(parent)
        self.col: Collection = col
        self.columns: dict[str, Column] = {
            c.key: c
            for c in self.col.all_browser_columns()
        }
        gui_hooks.browser_did_fetch_columns(self.columns)
        self._state: ItemState = state
        self._items: Sequence[ItemId] = []
        self._rows: dict[int, CellRow] = {}
        self._block_updates = False
        self._stale_cutoff = 0.0
        self._on_row_state_will_change = row_state_will_change_callback
        self._on_row_state_changed = row_state_changed_callback
        self._want_tooltips = aqt.mw.pm.show_browser_table_tooltips()

    # Row Object Interface
    ######################################################################

    # Get Rows

    def get_cell(self, index: QModelIndex) -> Cell:
        return self.get_row(index).cells[index.column()]

    def get_row(self, index: QModelIndex) -> CellRow:
        item = self.get_item(index)
        if row := self._rows.get(item):
            if not self._block_updates and row.is_stale(self._stale_cutoff):
                # need to refresh
                return self._fetch_row_and_update_cache(index, item, row)
            # return row, even if it's stale
            return row
        if self._block_updates:
            # blank row until we unblock
            return CellRow.placeholder(self.len_columns())
        # missing row, need to build
        return self._fetch_row_and_update_cache(index, item, None)
Exemple #2
0
class DataModel(QAbstractTableModel):
    """Data manager for the browser table.

    _items -- The card or note ids currently hold and corresponding to the
              table's rows.
    _rows -- The cached data objects to render items to rows.
    columns -- The data objects of all available columns, used to define the display
               of active columns and list all toggleable columns to the user.
    _block_updates -- If True, serve stale content to avoid hitting the DB.
    _stale_cutoff -- A threshold to decide whether a cached row has gone stale.
    """

    def __init__(self, col: Collection, state: ItemState) -> None:
        QAbstractTableModel.__init__(self)
        self.col: Collection = col
        self.columns: Dict[str, Column] = dict(
            ((c.key, c) for c in self.col.all_browser_columns())
        )
        gui_hooks.browser_did_fetch_columns(self.columns)
        self._state: ItemState = state
        self._items: Sequence[ItemId] = []
        self._rows: Dict[int, CellRow] = {}
        self._block_updates = False
        self._stale_cutoff = 0.0

    # Row Object Interface
    ######################################################################

    # Get Rows

    def get_cell(self, index: QModelIndex) -> Cell:
        return self.get_row(index).cells[index.column()]

    def get_row(self, index: QModelIndex) -> CellRow:
        item = self.get_item(index)
        if row := self._rows.get(item):
            if not self._block_updates and row.is_stale(self._stale_cutoff):
                # need to refresh
                self._rows[item] = self._fetch_row_from_backend(item)
                return self._rows[item]
            # return row, even if it's stale
            return row
        if self._block_updates:
            # blank row until we unblock
            return CellRow.placeholder(self.len_columns())
        # missing row, need to build
        self._rows[item] = self._fetch_row_from_backend(item)
        return self._rows[item]