ability to change language

This commit is contained in:
2022-02-06 15:37:26 +01:00
parent 95634355c2
commit 6573580f3c
8 changed files with 158 additions and 82 deletions

View File

@@ -1,14 +1,30 @@
from typing import Callable
from PySide6.QtGui import QAction, QIcon
from PySide6.QtWidgets import QMenu
class RAction():
def __init__(self,
label: str,
action=None,
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
@@ -17,9 +33,16 @@ class RAction():
self.icon_file = icon_file
self.checkable = checkable
self.checked = checked
self._action: QAction = None
def set_action(self, action):
self.action = 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
@@ -35,3 +58,31 @@ class RAction():
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(QIcon("icons/ionicons/checkbox-outline.svg"))
else:
self._action.setIcon(QIcon("icons/ionicons/square-outline.svg"))
def set_label(self, label: str):
if self._action:
self._action.setText(label)
def to_qaction(self, qmenu: QMenu) -> QAction:
action = QAction(self.label, qmenu)
self._action = action
if self.icon_from_theme:
action.setIcon(QIcon.fromTheme(self.icon_from_theme))
if self.icon_file:
action.setIcon(QIcon(self.icon_file))
if self.shortcut:
action.setShortcut(self.shortcut)
if self.action:
action.triggered.connect(self.action)
if self.checkable:
self._update_check_state()
return action

View File

@@ -1,8 +1,8 @@
import sys
from typing import Optional, Callable
from typing import Optional, Callable, Dict
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QDockWidget
from PySide6.QtWidgets import QDockWidget, QMessageBox
import constants
from aboutdialog import AboutDialog
@@ -12,7 +12,7 @@ from raven.plugins.domain.menucontribution import MenuContribution
from raven.plugins.domain.raction import RAction
from raven.plugins.domain.rmenu import RMenu
from raven.plugins.ravenlog.Tab import Tab
from raven.i18n import _
from raven.i18n import _, locale
from settings import Settings
@@ -20,6 +20,9 @@ class RavenLogPlugin(PluginBase):
def __init__(self):
super(RavenLogPlugin, self).__init__()
self.main_window = None
self._locale = locale
self._locale_actions = {}
self.settings = None
def set_settings(self, settings: Settings):
self.settings = settings
@@ -38,16 +41,36 @@ class RavenLogPlugin(PluginBase):
def _sub_menu_languages(self) -> RMenu:
menu = RMenu(_("&Languages"))
menu.add_action(RAction(_("&Default"), lambda: self._set_lang('')))
menu.add_action(RAction(_("&English"), lambda: self._set_lang('en')))
menu.add_action(RAction(_("&German"), lambda: self._set_lang('de')))
self._locale_actions[''] = RAction(_("&Default"), lambda: self._change_locale(''), checkable=True)
self._locale_actions['en'] = RAction(_("&English"), lambda: self._change_locale('en'), checkable=True)
self._locale_actions['de'] = RAction(_("&German"), lambda: self._change_locale('de'), checkable=True)
for (key, action) in self._locale_actions.items():
action.checked = self._locale == key
menu.add_action(action)
if not self._locale in self._locale_actions.keys():
self._locale_actions[''].checked = True
return menu
def _set_lang(self, lang: str):
if (lang == ''):
self.settings.session.remove_option('general', 'lang')
else:
self.settings.session.set('general', 'lang', lang)
def _change_locale(self, locale: str):
if self._locale != locale:
if self._locale in self._locale_actions:
self._locale_actions[self._locale].set_checked(False)
self._locale_actions[locale].set_checked(True)
self._locale = locale
if locale == '':
self.settings.session.remove_option('general', 'lang')
else:
self.settings.session.set('general', 'lang', locale)
info = QMessageBox(
QMessageBox.Icon.Information,
_("Language Changed"),
_("The language for this application has been changed. The change will take effect the next time the application is started."))
info.setStandardButtons(QMessageBox.Ok)
info.exec()
def current_file(self) -> Optional[str]:
return self.main_window.tabs.current_file()