Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dynamic scrollbar for SpreadSheet #130

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tabulous/_qt/_table/_base/_table_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ def moveToItem(
"""Move current index."""
selection_model = self._qtable_view._selection_model
df_shape = self.model().df.shape
table_shape = self.model().rowCount(), self.model().columnCount()
table_shape = self.tableShape()

if row is None:
row = selection_model.current_index.row
Expand Down
77 changes: 74 additions & 3 deletions tabulous/_qt/_table/_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
_STRING_DTYPE = get_dtype("string")
_EMPTY = object()
_EXP_FLOAT = re.compile(r"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)")
_FETCH_SIZE = 20


class SpreadSheetModel(AbstractDataFrameModel):
Expand All @@ -39,7 +40,8 @@ def __init__(self, parent=None):

self._table_config = get_config().table
self._columns_dtype = self.parent()._columns_dtype
self._out_of_bound_color_cache = None
self._out_of_bound_color_cache: QtGui.QColor | None = None
self._nrows, self._ncols = _FETCH_SIZE * 10, _FETCH_SIZE * 3

@property
def _out_of_bound_color(self) -> QtGui.QColor:
Expand Down Expand Up @@ -67,10 +69,20 @@ def df(self, data: pd.DataFrame):
self._df = data

def rowCount(self, parent=None):
return self._table_config.max_row_count
return self._nrows

def columnCount(self, parent=None):
return self._table_config.max_column_count
return self._ncols

def _set_row_count(self, nrows: int):
_nrows = min(nrows, self._table_config.max_row_count)
self.setShape(_nrows, self._ncols)
self._nrows = _nrows

def _set_column_count(self, ncols: int):
_ncols = min(ncols, self._table_config.max_column_count)
self.setShape(self._nrows, _ncols)
self._ncols = _ncols

def _data_display(self, index: QtCore.QModelIndex):
"""Display role."""
Expand Down Expand Up @@ -198,6 +210,9 @@ def __init__(self, parent=None, data: pd.DataFrame | None = None):
self._qtable_view.verticalHeader().insertSection(0, nr, rspan)
self._qtable_view.horizontalHeader().insertSection(0, nc, cspan)

self._qtable_view.verticalScrollBar().valueChanged.connect(self._on_v_scroll)
self._qtable_view.horizontalScrollBar().valueChanged.connect(self._on_h_scroll)

if TYPE_CHECKING:

def model(self) -> SpreadSheetModel:
Expand Down Expand Up @@ -296,6 +311,38 @@ def setDataFrame(self, data: pd.DataFrame) -> None:
self.refreshTable()
return

def moveToItem(
self,
row: int | None = None,
column: int | None = None,
clear_selection: bool = True,
) -> None:
"""Move current index."""
model = self.model()
_need_reset_nrows = False
_need_reset_ncols = False
if row is None:
pass
elif row < 0:
_need_reset_nrows = True
model._set_row_count(model._table_config.max_row_count)
elif row > model.rowCount():
model._set_row_count(row + _FETCH_SIZE)
if column is None:
pass
elif column < 0:
_need_reset_ncols = True
model._set_column_count(model._table_config.max_column_count)
elif column > model.columnCount():
model._set_column_count(column + _FETCH_SIZE)
super().moveToItem(row, column, clear_selection)
idx = self._qtable_view._selection_model.current_index
if _need_reset_nrows:
model._set_row_count(idx.row + _FETCH_SIZE)
if _need_reset_ncols:
model._set_column_count(idx.column + _FETCH_SIZE)
return None

def updateValue(self, r, c, val):
index = self._data_raw.index[r]
columns = self._data_raw.columns[c]
Expand Down Expand Up @@ -379,6 +426,30 @@ def _assignColumns_fmt(self, serieses: dict[str, pd.Series]):
keys = list(serieses.keys())
return f"assign new data to {keys}"

def _on_v_scroll(self, value: int):
model = self.model()
nr = model.rowCount()
irow = self._qtable_view.rowAt(value + self._qtable_view.height())

if irow < 0:
irow = nr - 1
dr = nr - irow
if dr < _FETCH_SIZE or dr > _FETCH_SIZE * 2:
if irow > _FETCH_SIZE * 9:
model._set_row_count(irow + _FETCH_SIZE)

def _on_h_scroll(self, value: int):
model = self.model()
nc = model.columnCount()
icol = self._qtable_view.columnAt(value + self._qtable_view.width())

if icol < 0:
icol = nc - 1
dc = nc - icol
if dc < _FETCH_SIZE or dc > _FETCH_SIZE * 2:
if icol > _FETCH_SIZE * 2:
model._set_column_count(icol + _FETCH_SIZE)

def createModel(self) -> None:
"""Create spreadsheet model."""
model = SpreadSheetModel(self)
Expand Down