Skip to content

Commit

Permalink
Merge pull request #93 from DeepLabCut/clickable_label
Browse files Browse the repository at this point in the history
Make keypoint labels clickable in the color reference widget
  • Loading branch information
jeylau authored Aug 30, 2023
2 parents 66da173 + 2fa2492 commit 4d34cd2
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/napari_deeplabcut/_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from napari.utils.events import Event
from napari.utils.history import get_save_history, update_save_history
from qtpy.QtCore import Qt, QTimer, Signal, QSize, QPoint, QSettings
from qtpy.QtGui import QPainter, QIcon, QAction
from qtpy.QtGui import QPainter, QIcon, QAction, QCursor
from qtpy.QtWidgets import (
QButtonGroup,
QCheckBox,
Expand Down Expand Up @@ -989,6 +989,26 @@ def dropEvent(self, event):
self.sig_dropped.emit(event)


class ClickableLabel(QLabel):
clicked = Signal(str)

def __init__(self, text="", color="turquoise", parent=None):
super().__init__(text, parent)
self._default_style = self.styleSheet()
self.color = color

def mousePressEvent(self, event):
self.clicked.emit(self.text())

def enterEvent(self, event):
self.setCursor(QCursor(Qt.PointingHandCursor))
self.setStyleSheet(f"color: {self.color}")

def leaveEvent(self, event):
self.unsetCursor()
self.setStyleSheet(self._default_style)


class LabelPair(QWidget):
def __init__(self, color: str, name: str, parent: QWidget):
super().__init__(parent)
Expand All @@ -997,7 +1017,7 @@ def __init__(self, color: str, name: str, parent: QWidget):
self._part_name = name

self.color_label = QLabel("", parent=self)
self.part_label = QLabel(name, parent=self)
self.part_label = ClickableLabel(name, color=color, parent=self)

self.color_label.setToolTip(name)
self.part_label.setToolTip(name)
Expand Down Expand Up @@ -1057,6 +1077,15 @@ def __init__(self, parent):

self._build()

@property
def labels(self):
labels = []
for i in range(self._layout.count()):
item = self._layout.itemAt(i)
if w := item.widget():
labels.append(w)
return labels

def _build(self):
self._container.setSizePolicy(
QSizePolicy.Fixed, QSizePolicy.Maximum
Expand Down

0 comments on commit 4d34cd2

Please sign in to comment.