Skip to content

Commit

Permalink
add reset button for the column filter
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Sep 26, 2023
1 parent 9847e5b commit 724e8b2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
6 changes: 6 additions & 0 deletions tabulous/_qt/_icons/restore_columns.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 28 additions & 1 deletion tabulous/_qt/_proxy_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
FilterInfo,
ComposableFilter,
ComposableSorter,
ColumnFilter,
)
from superqt import QEnumComboBox

Expand All @@ -21,6 +22,7 @@
if TYPE_CHECKING:
from typing_extensions import Self
from tabulous._qt._table import QBaseTable
from tabulous._qt._table._base._header_view import QDataFrameHeaderView
import pandas as pd

logger = logging.getLogger("tabulous")
Expand All @@ -42,7 +44,7 @@ def on_uninstalled(self, table: QBaseTable, index: int):
"""Callback when this widget is uninstalled"""


class _QHeaderSectionButton(QColoredToolButton, HeaderAnchorMixin):
class _QTransparentToolButton(QColoredToolButton):
def __init__(self, parent: QtW.QWidget = None):
super().__init__(parent)
self.setFixedSize(16, 16)
Expand All @@ -68,6 +70,10 @@ def updateColorByBackground(self, bg: QtGui.QColor):
self.updateColor("#CCCCCC")


class _QHeaderSectionButton(_QTransparentToolButton, HeaderAnchorMixin):
pass


class QHeaderSortButton(_QHeaderSectionButton):
sortSignal = Signal(bool)
resetSignal = Signal()
Expand Down Expand Up @@ -347,3 +353,24 @@ def value(self) -> list:

def itemWidget(self, item: QtW.QListWidgetItem) -> QtW.QCheckBox:
...


class QColumnFilterButton(_QTransparentToolButton):
def __init__(self, parent: QDataFrameHeaderView):
super().__init__(parent)
self.setPopupMode(QtW.QToolButton.ToolButtonPopupMode.InstantPopup)
self.setIcon(ICON_DIR / "restore_columns.svg")
self.setToolTip("Clear column filter")
self.clicked.connect(self._on_clicked)
_viewer = parent.parentWidget().parentViewer()
self.updateColorByBackground(_viewer.backgroundColor())
self.setFixedHeight(parent.height() - 2)

if TYPE_CHECKING:

def parentWidget(self) -> QDataFrameHeaderView:
...

def _on_clicked(self):
qtable = self.parentWidget().parentWidget().parentTable()
qtable.setColumnFilter(ColumnFilter.identity())
19 changes: 18 additions & 1 deletion tabulous/_qt/_table/_base/_header_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

from tabulous._qt._action_registry import QActionRegistry
from tabulous._qt._proxy_button import HeaderAnchorMixin
from tabulous._qt._proxy_button import HeaderAnchorMixin, QColumnFilterButton

if TYPE_CHECKING:
from ._enhanced_table import _QTableViewEnhanced
Expand Down Expand Up @@ -211,6 +211,10 @@ def reindexSectionWidgets(self, indexer: list[int] | None):
class QHorizontalHeaderView(QDataFrameHeaderView):
_Orientation = Qt.Orientation.Horizontal

def __init__(self, parent: QtW.QWidget | None = None) -> None:
super().__init__(parent)
self._column_filter_btn: QColumnFilterButton | None = None

def visualRectAtIndex(self, index: int) -> QtCore.QRect:
x = self.sectionViewportPosition(index)
y = self.rect().top()
Expand Down Expand Up @@ -244,6 +248,19 @@ def _iter_selections(self):
def _index_for_selection_model(self, logicalIndex):
return -1, logicalIndex

def show_column_filter_button(self):
if self._column_filter_btn:
return
self._column_filter_btn = QColumnFilterButton(self)
self._column_filter_btn.show()

def hide_column_filter_button(self):
if btn := self._column_filter_btn:
btn.setParent(None)
btn.close()
btn.deleteLater()
self._column_filter_btn = None


class QVerticalHeaderView(QDataFrameHeaderView):
_Orientation = Qt.Orientation.Vertical
Expand Down
5 changes: 5 additions & 0 deletions tabulous/_qt/_table/_base/_table_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ def _set_column_filter(self, cfil: ColumnFilter):
hheader = self._qtable_view.horizontalHeader()
hheader.reindexSectionWidgets(indexer)

if self._column_filter.is_identity():
hheader.hide_column_filter_button()
else:
hheader.show_column_filter_button()

@_set_column_filter.server
def _set_column_filter(self, cfil: ColumnFilter):
return arguments(self._column_filter)
Expand Down
2 changes: 1 addition & 1 deletion tabulous/_qt/_table_stack/_tabwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def openColumnFilterDialog(self, index: int | None = None):
_widget = QColumnFilterWidget(ol)

ol.addWidget(_widget)
ol.setTitle("Filter")
ol.setTitle("Column Filter")
_widget._cbox.setFocus()
return _widget

Expand Down
2 changes: 1 addition & 1 deletion tabulous/commands/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def sort_by_columns(viewer: TableViewerBase) -> None:


def filter_by_columns(viewer: TableViewerBase) -> None:
"""Filter by a column"""
"""Filter by selected columns"""
table = _utils.get_table(viewer)
indices = _utils.get_selected_columns(viewer)
by = [table.columns[index] for index in indices]
Expand Down

0 comments on commit 724e8b2

Please sign in to comment.