Skip to content

DataTableView

Base classes

Name Children Inherits
TableView
prettyqt.widgets.tableview
Default model/view implementation of a table view.

â‹” Inheritance diagram

graph TD
  1965600599["dataframeviewer.DataTableView"]
  1288268085["widgets.TableView"]
  2313507980["widgets.TableViewMixin"]
  3836649406["widgets.AbstractItemViewMixin"]
  1259044718["widgets.AbstractScrollAreaMixin"]
  1689631995["widgets.FrameMixin"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  2105279592["QtWidgets.QTableView"]
  2555450592["QtWidgets.QAbstractItemView"]
  2048140712["QtWidgets.QAbstractScrollArea"]
  1267405807["QtWidgets.QFrame"]
  1348938779["QtWidgets.QWidget"]
  13951691["QtCore.QObject"]
  395147830["sip.wrapper"]
  46922758["sip.simplewrapper"]
  2007623938["QtGui.QPaintDevice"]
  1288268085 --> 1965600599
  2313507980 --> 1288268085
  3836649406 --> 2313507980
  1259044718 --> 3836649406
  1689631995 --> 1259044718
  829357528 --> 1689631995
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2105279592 --> 1288268085
  2555450592 --> 2105279592
  2048140712 --> 2555450592
  1267405807 --> 2048140712
  1348938779 --> 1267405807
  13951691 --> 1348938779
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758
  2007623938 --> 1348938779
  46922758 --> 2007623938

🛈 DocStrings

Bases: TableView

Source code in src/cutepandas/pandaswidgets/dataframeviewer.py
class DataTableView(widgets.TableView):
    def __init__(self, df: Any = None, **kwargs):
        super().__init__(**kwargs)

        self.h_header.hide()
        self.v_header.hide()
        self.setAlternatingRowColors(True)
        self.set_scroll_mode("pixel")
        self.set_margin(0)
        self.set_scrollbar_policy("always_off")
        self.setWordWrap(False)  # single-line cells (row height assumes it)
        self.setItemDelegate(_viewer_style.DataCellDelegate(self))
        self.set_size_policy("expanding", "expanding")
        self.set_model(pandasmodels.DataFrameModel(df, parent=self))

    def set_df(self, df: Any):
        self.get_model(skip_proxies=True).holder.df = df

    def copy(self):
        """Copy the bounding box of the selected cells as tab-separated text."""
        indexes = self.selectionModel().selection().indexes()
        if not indexes or self.get_model(skip_proxies=True).source is None:
            return
        rows = [ix.row() for ix in indexes]
        cols = range(
            min(ix.column() for ix in indexes), max(ix.column() for ix in indexes) + 1
        )
        model = self.get_model(skip_proxies=True)
        chunks = model.fetch_window(min(rows), max(rows) + 1, cols)
        lines = [
            "\t".join(
                "" if chunk.values[i] is None else str(chunk.values[i])
                for chunk in chunks
            )
            for i in range(max(rows) + 1 - min(rows))
        ]
        gui.GuiApplication.clipboard().setText("\n".join(lines))

    def paste(self):
        """Paste tab-separated clipboard text, starting at the current cell."""
        text = gui.GuiApplication.clipboard().text()
        model = self.model()
        if not text or model is None:
            return
        current = self.currentIndex()
        top, left = (current.row(), current.column()) if current.isValid() else (0, 0)
        for i, line in enumerate(text.splitlines()):
            for j, cell in enumerate(line.split("\t")):
                index = model.index(top + i, left + j)
                if index.isValid():  # clip to the frame
                    model.setData(index, helpers.infer_type(cell), constants.EDIT_ROLE)

    def keyPressEvent(self, event: gui.QKeyEvent):
        # Handled here: the base class would otherwise copy only the current cell.
        if event.matches(gui.KeySequence.StandardKey.Copy):
            self.copy()
        elif event.matches(gui.KeySequence.StandardKey.Paste):
            self.paste()
        else:
            super().keyPressEvent(event)

    def sizeHint(self):
        if self.get_model(skip_proxies=True).df is None:
            return core.Size(0, 0)
        width = 2 * self.frameWidth()  # Account for border & padding
        # width += self.v_scrollbar.width()
        max_cols = min(100, self.model().columnCount())
        for i in range(max_cols):
            width += self.columnWidth(i)

        # Height
        height = 2 * self.frameWidth()  # Account for border & padding
        # height += self.h_scrollbar.height()
        max_rows = min(100, self.model().rowCount())
        for i in range(max_rows):
            height += self.rowHeight(i)

        return core.Size(width, height)

copy

copy()

Copy the bounding box of the selected cells as tab-separated text.

Source code in src/cutepandas/pandaswidgets/dataframeviewer.py
def copy(self):
    """Copy the bounding box of the selected cells as tab-separated text."""
    indexes = self.selectionModel().selection().indexes()
    if not indexes or self.get_model(skip_proxies=True).source is None:
        return
    rows = [ix.row() for ix in indexes]
    cols = range(
        min(ix.column() for ix in indexes), max(ix.column() for ix in indexes) + 1
    )
    model = self.get_model(skip_proxies=True)
    chunks = model.fetch_window(min(rows), max(rows) + 1, cols)
    lines = [
        "\t".join(
            "" if chunk.values[i] is None else str(chunk.values[i])
            for chunk in chunks
        )
        for i in range(max(rows) + 1 - min(rows))
    ]
    gui.GuiApplication.clipboard().setText("\n".join(lines))

paste

paste()

Paste tab-separated clipboard text, starting at the current cell.

Source code in src/cutepandas/pandaswidgets/dataframeviewer.py
def paste(self):
    """Paste tab-separated clipboard text, starting at the current cell."""
    text = gui.GuiApplication.clipboard().text()
    model = self.model()
    if not text or model is None:
        return
    current = self.currentIndex()
    top, left = (current.row(), current.column()) if current.isValid() else (0, 0)
    for i, line in enumerate(text.splitlines()):
        for j, cell in enumerate(line.split("\t")):
            index = model.index(top + i, left + j)
            if index.isValid():  # clip to the frame
                model.setData(index, helpers.infer_type(cell), constants.EDIT_ROLE)

:fa-brands-github: Show source on GitHub