Skip to content

Commit

Permalink
small close-icon on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
bkloeckl committed Nov 27, 2024
1 parent 46dd18f commit 1676780
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 42 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
1 change: 1 addition & 0 deletions src/mnelab/icons/dark/actions/close-data.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/mnelab/icons/light/actions/close-data.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 1 addition & 17 deletions src/mnelab/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
12 changes: 5 additions & 7 deletions src/mnelab/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 10 additions & 16 deletions src/mnelab/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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)
Expand All @@ -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())
Expand Down Expand Up @@ -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)
"""

0 comments on commit 1676780

Please sign in to comment.