Change how the switch is animated
This commit is contained in:
@@ -23,12 +23,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from PyQt6.QtCore import QPropertyAnimation, Qt, pyqtProperty # pyright: ignore
|
from PyQt6.QtCore import QPropertyAnimation, Qt, pyqtProperty, pyqtSlot # pyright: ignore
|
||||||
from PyQt6.QtGui import QEnterEvent, QMouseEvent, QPainter, QPaintEvent, QResizeEvent
|
from PyQt6.QtGui import QEnterEvent, QPainter, QPaintEvent, QResizeEvent
|
||||||
from PyQt6.QtWidgets import QAbstractButton, QWidget
|
from PyQt6.QtWidgets import QAbstractButton, QWidget
|
||||||
|
|
||||||
from novelwriter import SHARED
|
from novelwriter import SHARED
|
||||||
from novelwriter.types import QtMouseLeft, QtNoPen, QtPaintAntiAlias, QtSizeFixed
|
from novelwriter.types import QtNoPen, QtPaintAntiAlias, QtSizeFixed
|
||||||
|
|
||||||
|
|
||||||
class NSwitch(QAbstractButton):
|
class NSwitch(QAbstractButton):
|
||||||
@@ -50,6 +50,8 @@ class NSwitch(QAbstractButton):
|
|||||||
self.setFixedHeight(self._xH)
|
self.setFixedHeight(self._xH)
|
||||||
self._offset = self._xR
|
self._offset = self._xR
|
||||||
|
|
||||||
|
self.clicked.connect(self._onClick)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
##
|
##
|
||||||
@@ -106,19 +108,18 @@ class NSwitch(QAbstractButton):
|
|||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
|
|
||||||
"""Animate the switch on mouse release."""
|
|
||||||
super().mouseReleaseEvent(event)
|
|
||||||
if event.button() == QtMouseLeft:
|
|
||||||
anim = QPropertyAnimation(self, b"offset", self)
|
|
||||||
anim.setDuration(120)
|
|
||||||
anim.setStartValue(self._offset)
|
|
||||||
anim.setEndValue((self._xW - self._xR) if self.isChecked() else self._xR)
|
|
||||||
anim.start()
|
|
||||||
return
|
|
||||||
|
|
||||||
def enterEvent(self, event: QEnterEvent) -> None:
|
def enterEvent(self, event: QEnterEvent) -> None:
|
||||||
"""Change the cursor when hovering the button."""
|
"""Change the cursor when hovering the button."""
|
||||||
self.setCursor(Qt.CursorShape.PointingHandCursor)
|
self.setCursor(Qt.CursorShape.PointingHandCursor)
|
||||||
super().enterEvent(event)
|
super().enterEvent(event)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@pyqtSlot(bool)
|
||||||
|
def _onClick(self, checked: bool) -> None:
|
||||||
|
"""Animate the toggle action."""
|
||||||
|
anim = QPropertyAnimation(self, b"offset", self)
|
||||||
|
anim.setDuration(120)
|
||||||
|
anim.setStartValue(self._offset)
|
||||||
|
anim.setEndValue((self._xW - self._xR) if checked else self._xR)
|
||||||
|
anim.start()
|
||||||
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user