From 16767809f2d25c02d5c1a04763e7e428936d9b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20Kl=C3=B6ckl?= Date: Wed, 27 Nov 2024 16:53:59 +0100 Subject: [PATCH] small close-icon on hover --- CHANGELOG.md | 4 +-- src/mnelab/icons/dark/actions/close-data.svg | 1 + src/mnelab/icons/light/actions/close-data.svg | 1 + src/mnelab/mainwindow.py | 18 +------------ src/mnelab/model.py | 12 ++++----- src/mnelab/sidebar.py | 26 +++++++------------ 6 files changed, 20 insertions(+), 42 deletions(-) create mode 100644 src/mnelab/icons/dark/actions/close-data.svg create mode 100644 src/mnelab/icons/light/actions/close-data.svg diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c8979de..0c704a3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## [UNRELEASED] - YYYY-MM-DD -### -- Add close-button icon to sidebar table ([#454]) https://github.com/cbrnr/mnelab/pull/454 by [Benedikt Kloeckl](https://github.com/bkloeckl)) +### Added +- Add close-button icon to sidebar table on hover ([#454]) https://github.com/cbrnr/mnelab/pull/454 by [Benedikt Kloeckl](https://github.com/bkloeckl)) ### Fixed - Fix a bug where appending data would not be correctly displayed in the history ([#446]) https://github.com/cbrnr/mnelab/pull/446 by [Benedikt Kloeckl](https://github.com/bkloeckl)) diff --git a/src/mnelab/icons/dark/actions/close-data.svg b/src/mnelab/icons/dark/actions/close-data.svg new file mode 100644 index 00000000..aa1c4b72 --- /dev/null +++ b/src/mnelab/icons/dark/actions/close-data.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/mnelab/icons/light/actions/close-data.svg b/src/mnelab/icons/light/actions/close-data.svg new file mode 100644 index 00000000..de47033b --- /dev/null +++ b/src/mnelab/icons/light/actions/close-data.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/mnelab/mainwindow.py b/src/mnelab/mainwindow.py index 4c8b20c8..9b3c3e5e 100644 --- a/src/mnelab/mainwindow.py +++ b/src/mnelab/mainwindow.py @@ -465,23 +465,6 @@ def data_changed(self): item.setFlags(item.flags() | Qt.ItemIsEditable) self.sidebar.setItem(row_index, 0, item) - # add delete button - delete_button = QPushButton(self) - icon_path = os.path.join( - os.path.dirname(__file__), - "icons", - "close-data.svg", - ) - - delete_button.setIcon(QIcon(icon_path)) - delete_button.setStyleSheet( - "background: transparent; border: none; margin: auto;" - ) - delete_button.clicked.connect( - lambda _, index=row_index: self.model.remove_data_at(index) - ) - self.sidebar.setCellWidget(row_index, 1, delete_button) - self.sidebar.styleRows() self.sidebar.selectRow(self.model.index) @@ -1385,6 +1368,7 @@ def _update_data(self, row, column): if row != self.model.index: self.model.index = row self.data_changed() + self.sidebar.showCloseButton(row) self.model.history.append(f"data = datasets[{self.model.index}]") @Slot() diff --git a/src/mnelab/model.py b/src/mnelab/model.py index da055b6d..fe9c3b10 100644 --- a/src/mnelab/model.py +++ b/src/mnelab/model.py @@ -70,15 +70,13 @@ def update_data(self, dataset): """Update/overwrite data set at current index.""" self.current = dataset - def remove_data_at(self, index): - """Remove data set at certain index.""" - self.index = index - self.remove_data() - @data_changed - def remove_data(self): + def remove_data(self, index=-1): """Remove data set at current index.""" - self.data.pop(self.index) + if index == -1: + index = self.index + + self.data.pop(index) if self.index >= len(self.data): # if last entry was removed self.index = len(self.data) - 1 # reset index to last entry diff --git a/src/mnelab/sidebar.py b/src/mnelab/sidebar.py index 74685606..e5741ce5 100644 --- a/src/mnelab/sidebar.py +++ b/src/mnelab/sidebar.py @@ -2,12 +2,13 @@ # # License: BSD (3-clause) -from PySide6.QtCore import Qt, Signal -from PySide6.QtGui import QColor, QPainter, QPen +from PySide6.QtCore import QEvent, Qt, Signal +from PySide6.QtGui import QColor, QIcon, QPainter, QPen from PySide6.QtWidgets import ( QAbstractItemView, QFrame, QHeaderView, + QPushButton, QTableWidget, ) @@ -17,6 +18,7 @@ class SidebarTableWidget(QTableWidget): def __init__(self, parent): super().__init__(parent) + self.parent = parent self.setDragEnabled(True) self.setAcceptDrops(True) self.setDragDropMode(QAbstractItemView.InternalMove) @@ -37,8 +39,8 @@ def __init__(self, parent): self.horizontalHeader().setSectionResizeMode(1, QHeaderView.Fixed) self.setColumnWidth(1, 20) - # self.setMouseTracking(True) - # self.viewport().installEventFilter(self) + self.setMouseTracking(True) + self.viewport().installEventFilter(self) def mousePressEvent(self, event): item = self.itemAt(event.pos()) @@ -111,31 +113,23 @@ def update_vertical_header(self): row_count = self.rowCount() self.setVerticalHeaderLabels([str(i) for i in range(row_count)]) - """ - # SHOW ON HOVER: def eventFilter(self, source, event): if source == self.viewport() and event.type() == QEvent.MouseMove: index = self.indexAt(event.pos()) if index.isValid(): self.showCloseButton(index.row()) - return super().eventFilter(source, event) def showCloseButton(self, row_index): for i in range(self.rowCount()): if i == row_index: delete_button = QPushButton(self) - icon_path = os.path.join( - os.path.dirname(__file__), - "icons", - "close-data.svg", - ) - - delete_button.setIcon(QIcon(icon_path)) + delete_button.setIcon(QIcon.fromTheme("close-data")) delete_button.setStyleSheet( "background: transparent; border: none; margin: auto;" ) - # TODO: connect to model.remove_data() + delete_button.clicked.connect( + lambda _, index=row_index: self.parent.model.remove_data(index) + ) self.setCellWidget(row_index, 1, delete_button) else: self.removeCellWidget(i, 1) - """