Files
krowlog/src/plugins/domain/raction.py
2023-01-30 18:58:01 +01:00

113 lines
3.7 KiB
Python

from typing import Callable
from PySide6.QtGui import QAction
from PySide6.QtWidgets import QMenu, QPushButton, QWidget
from src.ui.icon import Icon
class RAction():
def __init__(self,
label: str,
action: Callable[[], None] = None,
after_action: Callable[[], None] = None,
shortcut: str = None,
icon_from_theme: str = None,
icon_file: str = None,
checkable: bool = False,
checked: bool = False
):
"""
:param label: the label
:param action: the callback to be executed when clicked. Note: use the setter when creating a checkable menu item
:param shortcut: the shortcut, e.g. 'Ctrl+X'
:param icon_from_theme: environment specific name of an icon. On Linux: /usr/share/icons
:param icon_file: path to an icon
:param checkable: if this menu item behaves like a checkbox
:param checked: if it is checked
"""
super(RAction, self).__init__()
self.label = label
self.action = action
self.after_action = after_action
self.shortcut = shortcut
self.icon_from_theme = icon_from_theme
self.icon_file = icon_file
self.checkable = checkable
self.checked = checked
self._action: QAction = None
def set_action(self, action):
self.action = lambda *args: self.decorated_action(action)
def decorated_action(self, action):
if self.checkable:
self.checked = not self.checked
self._update_check_state()
action()
def set_icon_from_theme(self, icon_from_theme: str):
self.icon_from_theme = icon_from_theme
def set_icon_file(self, icon_file: str):
self.icon_file = icon_file
def set_shortcut(self, shortcut: str):
self.shortcut = shortcut
def set_checkable(self, checkable: bool):
self.checkable = checkable
def set_checked(self, checked: bool):
self.checked = checked
self._update_check_state()
def _update_check_state(self):
if self._action:
if self.checked:
self._action.setIcon(Icon("icons/myicons/rect-checked.svg"))
else:
self._action.setIcon(Icon("icons/myicons/rect.svg"))
def set_label(self, label: str):
if self._action:
self._action.setText(label)
def to_qaction(self, qmenu: QMenu) -> QAction:
action = QAction(qmenu)
action.setText(self.label)
self._action = action
if self.icon_from_theme:
action.setIcon(Icon.fromTheme(self.icon_from_theme, self.icon_file))
elif self.icon_file:
action.setIcon(Icon(self.icon_file))
if self.shortcut:
action.setShortcut(self.shortcut)
if self.action:
action.triggered.connect(self.action)
action.triggered.connect(self.after_action)
if self.checkable:
self._update_check_state()
return action
def to_qpushbutton(self, parent: QWidget) -> QPushButton:
button = QPushButton(parent)
if self.label:
button.setText(self.label)
if self.icon_from_theme:
button.setIcon(Icon.fromTheme(self.icon_from_theme))
if self.icon_file:
button.setIcon(Icon(self.icon_file))
if self.shortcut:
button.setShortcut(self.shortcut)
if self.action:
button.pressed.connect(self.action)
button.pressed.connect(self.after_action)
if self.checkable:
button.setChecked(self.checked)
button.setCheckable(self.checkable)
return button