113 lines
4.2 KiB
Python
113 lines
4.2 KiB
Python
import sys
|
|
from typing import Optional
|
|
|
|
from PySide6.QtCore import Qt
|
|
from PySide6.QtWidgets import QDockWidget, QMessageBox
|
|
|
|
import constants
|
|
from raven.plugins.ravenlog.aboutdialog import AboutDialog
|
|
from raven.mainwindow import MainWindow
|
|
from raven.pluginbase import PluginBase
|
|
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 _, locale
|
|
from raven.settings.settings import Settings
|
|
|
|
|
|
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
|
|
|
|
def create_main_window(self):
|
|
if not self.main_window:
|
|
self.main_window = MainWindow()
|
|
return self.main_window
|
|
|
|
def get_menu_contributions(self) -> [MenuContribution]:
|
|
return [
|
|
MenuContribution("file", action=self._action_close(), action_id="close application", after="<last>"),
|
|
MenuContribution("help", action=self._action_about(), action_id="open about dialog", after="<last>"),
|
|
MenuContribution("settings", menu=self._sub_menu_languages(), action_id="recent files menu"),
|
|
]
|
|
|
|
def _sub_menu_languages(self) -> RMenu:
|
|
menu = RMenu(_("&Languages"))
|
|
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 _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()
|
|
|
|
def update_window_title(self, title: str):
|
|
if len(title) > 0:
|
|
self.main_window.setWindowTitle(_("{0} - RavenLog").format(title))
|
|
else:
|
|
self.main_window.setWindowTitle(_("RavenLog"))
|
|
|
|
def update_status_bar(self, text: str):
|
|
if not self.main_window:
|
|
return
|
|
self.main_window.status_bar.showMessage(text)
|
|
|
|
def update_ui(self):
|
|
self.main_window.update()
|
|
|
|
def add_tab(self, tab: Tab):
|
|
self.main_window.tabs.add_tab(tab)
|
|
|
|
def add_dock(self, area: Qt.DockWidgetArea, widget: Tab):
|
|
dock_widget = QDockWidget(widget.title, self.main_window)
|
|
dock_widget.setWidget(widget)
|
|
self.main_window.addDockWidget(area, dock_widget)
|
|
|
|
def _action_about(self) -> RAction:
|
|
about_action = RAction(
|
|
_("&About"),
|
|
action=lambda: AboutDialog().exec(),
|
|
icon_file=constants.raven_icon
|
|
)
|
|
return about_action
|
|
|
|
def _action_close(self) -> RAction:
|
|
icon = "close" if sys.platform == 'win32' or sys.platform == 'cygwin' else "exit"
|
|
close_action = RAction(_("E&xit"), action=lambda: self.main_window.destruct(), shortcut='Ctrl+X',
|
|
icon_from_theme=icon)
|
|
return close_action
|