rename ravenlog to krowlog
There is a database named RavenDB. KrowLog starts with a K, which is a) distinctive and b) has an association to KDE.
This commit is contained in:
99
src/plugins/openfileplugin.py
Normal file
99
src/plugins/openfileplugin.py
Normal file
@@ -0,0 +1,99 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtWidgets import QFileDialog
|
||||
|
||||
from src.pluginbase import PluginBase
|
||||
from src.pluginregistry import PluginRegistry
|
||||
from src.plugins.domain.menucontribution import MenuContribution
|
||||
from src.plugins.domain.raction import RAction
|
||||
from src.plugins.domain.rmenu import RMenu
|
||||
from src.settings.settings import Settings
|
||||
|
||||
from src.i18n import _
|
||||
|
||||
|
||||
class OpenFilePlugin(PluginBase):
|
||||
def __init__(self):
|
||||
super(OpenFilePlugin, self).__init__()
|
||||
self.settings = None
|
||||
|
||||
def set_settings(self, settings: Settings):
|
||||
self.settings = settings
|
||||
|
||||
def _action_open_file(self) -> RAction:
|
||||
open_file = RAction(_("&Open..."), self._open_file_dialog, shortcut='Ctrl+O',
|
||||
icon_from_theme="document-open")
|
||||
return open_file
|
||||
|
||||
def _sub_menu_recent_files(self) -> RMenu:
|
||||
self._menu_recent_files = RMenu(_("Open &Recent"), icon_from_theme="document-open-recent")
|
||||
self._update_recent_files_menu()
|
||||
return self._menu_recent_files
|
||||
|
||||
def get_menu_contributions(self) -> [MenuContribution]:
|
||||
return [
|
||||
MenuContribution("file", action=self._action_open_file(), action_id="open file"),
|
||||
MenuContribution("file", menu=self._sub_menu_recent_files(), action_id="recent files menu"),
|
||||
]
|
||||
|
||||
def _open_file_dialog(self) -> None:
|
||||
current_file = PluginRegistry.execute_single("current_file")
|
||||
directory = os.path.dirname(current_file) if current_file else os.path.join(Path.home())
|
||||
|
||||
dialog = QFileDialog()
|
||||
(selected_file, _filter) = dialog.getOpenFileName(
|
||||
caption=_("Open File"),
|
||||
dir=directory
|
||||
)
|
||||
# directory=directory
|
||||
if selected_file:
|
||||
self.open_file(selected_file)
|
||||
|
||||
def open_file(self, selected_file: str):
|
||||
tab = PluginRegistry.execute_single("create_tab", selected_file)
|
||||
if tab:
|
||||
PluginRegistry.execute_single("add_tab", tab)
|
||||
PluginRegistry.execute("after_open_file", selected_file)
|
||||
|
||||
def _get_recent_files(self) -> [str]:
|
||||
recent_files = self.settings.session.get('general', 'recent_files', fallback='')
|
||||
# print(recent_files)
|
||||
files = recent_files.split(os.pathsep)
|
||||
if "" in files:
|
||||
files.remove("")
|
||||
return files
|
||||
|
||||
def _update_recent_files_menu(self):
|
||||
self._menu_recent_files.clear()
|
||||
files = self._get_recent_files()
|
||||
for file in files:
|
||||
action = RAction(os.path.basename(file))
|
||||
action.set_action(lambda _="", f=file: self.open_file(f))
|
||||
self._menu_recent_files.add_action(action)
|
||||
|
||||
def _remember_recent_file(self, file: str):
|
||||
files = self._get_recent_files()
|
||||
if file in files:
|
||||
files.remove(file)
|
||||
files.insert(0, file)
|
||||
recent_files = os.pathsep.join(files[:10])
|
||||
self.settings.set_session('general', 'recent_files', recent_files)
|
||||
self._update_recent_files_menu()
|
||||
|
||||
def after_open_file(self, file: str):
|
||||
self._remember_recent_file(file)
|
||||
|
||||
def after_start(self):
|
||||
open_files_as_string = self.settings.get_session('general', 'open_files', fallback='')
|
||||
files = open_files_as_string.split(os.pathsep)
|
||||
if "" in files:
|
||||
files.remove("")
|
||||
for file in files:
|
||||
self.open_file(file)
|
||||
|
||||
def before_shutdown(self):
|
||||
open_files = PluginRegistry.execute_single("get_open_files")
|
||||
if open_files:
|
||||
open_files_as_string = os.pathsep.join(open_files)
|
||||
self.settings.set_session('general', 'open_files', open_files_as_string)
|
||||
Reference in New Issue
Block a user