Skip to content

CornerLevelModel

Base classes

Name Children Inherits
DataFrameHolderMixin
cutepandas.pandasmodels.dataframeholder
Model mixin: keeps `self.df` in a (possibly shared) DataFrameHolder.
AbstractTableModel
prettyqt.core.abstracttablemodel
Abstract model that can be subclassed to create table models.

⋔ Inheritance diagram

graph TD
  2070077174["gridviewer.CornerLevelModel"]
  103874424["dataframeholder.DataFrameHolderMixin"]
  2751680898["builtins.object"]
  656210031["core.AbstractTableModel"]
  4092674192["core.AbstractTableModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  1115338939["QtCore.QAbstractTableModel"]
  930985301["QtCore.QAbstractItemModel"]
  13951691["QtCore.QObject"]
  395147830["sip.wrapper"]
  46922758["sip.simplewrapper"]
  103874424 --> 2070077174
  2751680898 --> 103874424
  656210031 --> 2070077174
  4092674192 --> 656210031
  3882849576 --> 4092674192
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  1115338939 --> 656210031
  930985301 --> 1115338939
  13951691 --> 930985301
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758

🛈 DocStrings

Bases: DataFrameHolderMixin, AbstractTableModel

Top-left corner model showing pandas index & column level names.

Source code in src/cutepandas/pandaswidgets/gridviewer.py
class CornerLevelModel(DataFrameHolderMixin, core.AbstractTableModel):
    """Top-left corner model showing pandas index & column level names."""

    def __init__(self, df: Any = None, **kwargs):
        super().__init__(**kwargs)
        self._init_holder(df)

    def _col_names(self) -> list[str]:
        axis = _pd_axis(self.df, horizontal=True)
        if axis is None:
            return [""]
        return ["" if n is None else str(n) for n in axis.names]

    def _idx_names(self) -> list[str]:
        axis = _pd_axis(self.df, horizontal=False)
        if axis is None:
            return [""]
        return ["" if n is None else str(n) for n in axis.names]

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        if self.df is None or (parent is not None and parent.isValid()):
            return 0
        return len(self._col_names())

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        if self.df is None or (parent is not None and parent.isValid()):
            return 0
        return len(self._idx_names())

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if self.df is None or not index.isValid():
            return None
        col_names = self._col_names()
        idx_names = self._idx_names()
        last_row = len(col_names) - 1
        last_col = len(idx_names) - 1
        r, c = index.row(), index.column()
        match role:
            case constants.DISPLAY_ROLE | constants.TOOLTIP_ROLE:
                if r == last_row and c == last_col:
                    left = idx_names[c]
                    right = col_names[r]
                    if left and right:
                        return f"{left} / {right}"
                    return left or right
                if r == last_row:
                    return idx_names[c]
                if c == last_col:
                    return col_names[r]
                return ""
            case constants.ALIGNMENT_ROLE:
                if r == last_row and c < last_col:
                    return constants.ALIGN_LEFT | constants.ALIGN_V_CENTER
                if c == last_col and r < last_row:
                    return constants.ALIGN_RIGHT | constants.ALIGN_V_CENTER
                return constants.ALIGN_CENTER
        return None

:fa-brands-github: Show source on GitHub