Skip to content

Commit

Permalink
added hover color change to ToggleSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
GuyTeichman committed Sep 10, 2024
1 parent 177410e commit 4714a5d
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions rnalysis/gui/gui_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,16 +541,26 @@ class ToggleSwitchCore(QtWidgets.QPushButton):
https://stackoverflow.com/questions/56806987/switch-button-in-pyqt
"""
stateChanged = QtCore.pyqtSignal(bool)
RADIUS = 11
RADIUS = 12
WIDTH = 42
BORDER = 2
__slots__ = {}
__slots__ = {'_hover': 'indicates if the mouse is hovering over the widget'}

def __init__(self, parent=None):
super().__init__(parent)
self._hover = False
self.setCheckable(True)
self.clicked.connect(self.state_changed)

def enterEvent(self, event):
self._hover = True
self.update()
super().enterEvent(event)

def leaveEvent(self, event):
self._hover = False
self.update()
super().leaveEvent(event)
def state_changed(self):
self.stateChanged.emit(self.isChecked())

Expand All @@ -560,7 +570,10 @@ def setChecked(self, is_checked):

def paintEvent(self, event):
label = " True" if self.isChecked() else "False"
bg_color = QtGui.QColor('#72e5bf') if self.isChecked() else QtGui.QColor('#e96e3a')
if self.isChecked():
bg_color = QtGui.QColor('#72e5bf') if not self._hover else QtGui.QColor('#52c59f')
else:
bg_color = QtGui.QColor('#e96e3a') if not self._hover else QtGui.QColor('#c94e1a')

radius = int(self.RADIUS * (self.font().pointSize() / 10))
width = int(self.WIDTH * (self.font().pointSize() / 10))
Expand Down Expand Up @@ -1805,7 +1818,8 @@ def append_text(self, text: str):
self.carriage = False
diff = self.document().characterCount() - self.prev_coord
cursor = self.textCursor()
cursor.movePosition(QtGui.QTextCursor.MoveOperation.PreviousCharacter, QtGui.QTextCursor.MoveMode.MoveAnchor, n=diff)
cursor.movePosition(QtGui.QTextCursor.MoveOperation.PreviousCharacter,
QtGui.QTextCursor.MoveMode.MoveAnchor, n=diff)
cursor.movePosition(QtGui.QTextCursor.MoveOperation.End, QtGui.QTextCursor.MoveMode.KeepAnchor)
cursor.removeSelectedText()

Expand Down Expand Up @@ -2166,5 +2180,3 @@ def clear_layout(layout, exceptions: set = frozenset()):
child = layout.takeAt(0)
if child.widget() and child.widget() not in exceptions:
child.widget().deleteLater()


0 comments on commit 4714a5d

Please sign in to comment.