示例#1
0
文件: model.py 项目: v-limc/anki
    def _fetch_row_from_backend(self, item: ItemId) -> CellRow:
        try:
            row = CellRow(*self.col.browser_row_for_id(item))
        except NotFoundError:
            return CellRow.deleted(self.len_columns())
        except Exception as e:
            return CellRow.generic(self.len_columns(), str(e))

        gui_hooks.browser_did_fetch_row(item, self._state.is_notes_mode(), row,
                                        self._state.active_columns)
        return row
示例#2
0
文件: model.py 项目: glutanimate/anki
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)
示例#3
0
文件: model.py 项目: rye761/anki
    def _fetch_row_from_backend(self, item: ItemId) -> CellRow:
        try:
            row = CellRow(*self.col.browser_row_for_id(item))
        except NotFoundError:
            return CellRow.deleted(self.len_columns())
        except Exception as e:
            return CellRow.generic(self.len_columns(), str(e))
        except BaseException as e:
            # fatal error like a panic in the backend - dump it to the
            # console so it gets picked up by the error handler
            import traceback

            traceback.print_exc()
            # and prevent Qt from firing a storm of follow-up errors
            self._block_updates = True
            return CellRow.generic(self.len_columns(), "error")

        gui_hooks.browser_did_fetch_row(item, self._state.is_notes_mode(), row,
                                        self._state.active_columns)
        return row
示例#4
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]