cleanup
This commit is contained in:
@@ -30,15 +30,6 @@ def flat_map(array: List[List]) -> List:
|
|||||||
return reduce(list.__add__, array)
|
return reduce(list.__add__, array)
|
||||||
|
|
||||||
|
|
||||||
def _action_about():
|
|
||||||
about_action = RAction(
|
|
||||||
"&About",
|
|
||||||
action=lambda: AboutDialog().exec(),
|
|
||||||
icon_file=constants.raven_icon
|
|
||||||
)
|
|
||||||
return about_action
|
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(QMainWindow):
|
class MainWindow(QMainWindow):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(MainWindow, self).__init__(*args, **kwargs)
|
super(MainWindow, self).__init__(*args, **kwargs)
|
||||||
@@ -67,11 +58,9 @@ class MainWindow(QMainWindow):
|
|||||||
menu_bar = QMenuBar()
|
menu_bar = QMenuBar()
|
||||||
|
|
||||||
menu_contributions: [MenuContribution] = flat_map(PluginRegistry.execute("get_menu_contributions"))
|
menu_contributions: [MenuContribution] = flat_map(PluginRegistry.execute("get_menu_contributions"))
|
||||||
menu_contributions.append(MenuContribution("file", action=self._action_close(), action_id="close app"))
|
|
||||||
menu_contributions.append(MenuContribution("settings", action=self._action_highlighter()))
|
menu_contributions.append(MenuContribution("settings", action=self._action_highlighter()))
|
||||||
menu_contributions.append(MenuContribution("settings", action=self._action_highlight_search_terms()))
|
menu_contributions.append(MenuContribution("settings", action=self._action_highlight_search_terms()))
|
||||||
menu_contributions.append(MenuContribution("settings", action=self._action_new_tab()))
|
menu_contributions.append(MenuContribution("settings", action=self._action_new_tab()))
|
||||||
menu_contributions.append(MenuContribution("help", action=_action_about()))
|
|
||||||
|
|
||||||
known_menus = [
|
known_menus = [
|
||||||
("file", self.tr("&File")),
|
("file", self.tr("&File")),
|
||||||
@@ -85,7 +74,6 @@ class MainWindow(QMainWindow):
|
|||||||
if len(mcs) == 0:
|
if len(mcs) == 0:
|
||||||
continue
|
continue
|
||||||
for menu_contribution in mcs:
|
for menu_contribution in mcs:
|
||||||
print("%s %s" % (menu_id, menu_contribution.action_id))
|
|
||||||
if menu_contribution.action:
|
if menu_contribution.action:
|
||||||
action = self._raction_to_qaction(menu_contribution.action, menu)
|
action = self._raction_to_qaction(menu_contribution.action, menu)
|
||||||
menu.addAction(action)
|
menu.addAction(action)
|
||||||
@@ -121,39 +109,6 @@ class MainWindow(QMainWindow):
|
|||||||
action.setChecked(raction.checked)
|
action.setChecked(raction.checked)
|
||||||
return action
|
return action
|
||||||
|
|
||||||
def _action_open_file(self) -> RAction:
|
|
||||||
open_file = RAction("&Open...", action=self._open_file_dialog, shortcut='Ctrl+O',
|
|
||||||
icon_from_theme="document-open")
|
|
||||||
return open_file
|
|
||||||
|
|
||||||
def _action_close(self) -> RAction:
|
|
||||||
# Linux: QIcon.fromTheme("exit")
|
|
||||||
icon = "close" if sys.platform == 'win32' or sys.platform == 'cygwin' else "exit"
|
|
||||||
close_action = RAction("E&xit", action=self.destruct, shortcut='Ctrl+X', icon_from_theme=icon)
|
|
||||||
return close_action
|
|
||||||
|
|
||||||
def create_menu_bar(self) -> QMenuBar:
|
|
||||||
menu_bar = QMenuBar()
|
|
||||||
|
|
||||||
menu_bar.addMenu(self.file_menu())
|
|
||||||
menu_bar.addMenu(self.settings_menu())
|
|
||||||
menu_bar.addMenu(self.help_menu())
|
|
||||||
|
|
||||||
return menu_bar
|
|
||||||
|
|
||||||
def file_menu(self) -> QMenu:
|
|
||||||
file_menu = QMenu(self.tr("&File", "name of the file menu"), self)
|
|
||||||
|
|
||||||
open_file = self._action_open_file()
|
|
||||||
close_action = self._action_close()
|
|
||||||
|
|
||||||
self._update_recent_files_menu()
|
|
||||||
|
|
||||||
file_menu.addAction(open_file)
|
|
||||||
file_menu.addMenu(self._menu_recent_files)
|
|
||||||
file_menu.addAction(close_action)
|
|
||||||
return file_menu
|
|
||||||
|
|
||||||
def _action_highlighter(self):
|
def _action_highlighter(self):
|
||||||
manage = RAction(
|
manage = RAction(
|
||||||
"&Highlighter",
|
"&Highlighter",
|
||||||
@@ -180,67 +135,6 @@ class MainWindow(QMainWindow):
|
|||||||
lambda checked: self.settings.set_session("general", "open_tab_on_save_as_file", str(checked)))
|
lambda checked: self.settings.set_session("general", "open_tab_on_save_as_file", str(checked)))
|
||||||
return new_tab
|
return new_tab
|
||||||
|
|
||||||
def settings_menu(self) -> QMenu:
|
|
||||||
result = QMenu(self.tr("&Settings"), self)
|
|
||||||
result.addAction(self._action_highlighter())
|
|
||||||
result.addAction(self._action_highlight_search_terms())
|
|
||||||
result.addAction(self._action_new_tab())
|
|
||||||
return result
|
|
||||||
|
|
||||||
def help_menu(self) -> QMenu:
|
|
||||||
help_menu = QMenu(self.tr("&Help", "name of the help menu"), self)
|
|
||||||
help_menu.addAction(_action_about())
|
|
||||||
return help_menu
|
|
||||||
|
|
||||||
def _update_recent_files_menu(self):
|
|
||||||
self._menu_recent_files.clear()
|
|
||||||
files = self._get_recent_files()
|
|
||||||
for file in files:
|
|
||||||
action = QAction(os.path.basename(file), self)
|
|
||||||
action.triggered.connect(lambda x, f=file: self.open_file(f))
|
|
||||||
self._menu_recent_files.addAction(action)
|
|
||||||
|
|
||||||
def _open_file_dialog(self) -> None:
|
|
||||||
current_file = self.tabs.current_file()
|
|
||||||
directory = os.path.dirname(current_file) if current_file else ''
|
|
||||||
|
|
||||||
dialog = QFileDialog(self)
|
|
||||||
(selected_file, _filter) = dialog.getOpenFileName(
|
|
||||||
caption=self.tr("Open File"),
|
|
||||||
directory=directory
|
|
||||||
)
|
|
||||||
if selected_file:
|
|
||||||
self.tabs.create_tab(selected_file)
|
|
||||||
self._remember_recent_file(selected_file)
|
|
||||||
|
|
||||||
def current_file(self) -> Optional[str]:
|
|
||||||
return self.tabs.current_file()
|
|
||||||
|
|
||||||
def open_file(self, file: str) -> None:
|
|
||||||
|
|
||||||
self.tabs.create_tab(file)
|
|
||||||
PluginRegistry.execute("after_open_file", file)
|
|
||||||
# self._remember_recent_file(file)
|
|
||||||
# can_open_plugins = PluginRegistry.execute("can_open_file", file)
|
|
||||||
# if len(can_open_plugins) == 1:
|
|
||||||
# else:
|
|
||||||
|
|
||||||
def _get_recent_files(self) -> [str]:
|
|
||||||
recent_files = self.settings.session.get('general', 'recent_files', fallback='')
|
|
||||||
files = recent_files.split(os.pathsep)
|
|
||||||
if "" in files:
|
|
||||||
files.remove("")
|
|
||||||
return files
|
|
||||||
|
|
||||||
# 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 dragEnterEvent(self, e: QDragEnterEvent):
|
def dragEnterEvent(self, e: QDragEnterEvent):
|
||||||
if url_is_file(e.mimeData().text()):
|
if url_is_file(e.mimeData().text()):
|
||||||
e.accept()
|
e.accept()
|
||||||
@@ -249,7 +143,7 @@ class MainWindow(QMainWindow):
|
|||||||
|
|
||||||
def dropEvent(self, e):
|
def dropEvent(self, e):
|
||||||
file = urlutils.url_to_path(e.mimeData().text())
|
file = urlutils.url_to_path(e.mimeData().text())
|
||||||
self.open_file(file)
|
PluginRegistry.executeSingle("create_tab", file)
|
||||||
|
|
||||||
def _restore_window(self):
|
def _restore_window(self):
|
||||||
qsettings = CuteSettings()
|
qsettings = CuteSettings()
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
|
import sys
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
import constants
|
||||||
|
from aboutdialog import AboutDialog
|
||||||
from raven.mainwindow import MainWindow
|
from raven.mainwindow import MainWindow
|
||||||
from raven.pluginbase import PluginBase
|
from raven.pluginbase import PluginBase
|
||||||
|
from raven.pluginregistry import PluginRegistry
|
||||||
|
from raven.plugins.domain.menucontribution import MenuContribution
|
||||||
|
from raven.plugins.domain.raction import RAction
|
||||||
|
|
||||||
|
|
||||||
class RavenLogPlugin(PluginBase):
|
class RavenLogPlugin(PluginBase):
|
||||||
@@ -14,8 +20,29 @@ class RavenLogPlugin(PluginBase):
|
|||||||
self.main_window = MainWindow()
|
self.main_window = MainWindow()
|
||||||
return self.main_window
|
return self.main_window
|
||||||
|
|
||||||
|
def get_menu_contributions(self) -> [MenuContribution]:
|
||||||
|
return [
|
||||||
|
MenuContribution("file", action=self._action_close(), action_id="close application"),
|
||||||
|
MenuContribution("help", action=self._action_about(), action_id="open about dialog"),
|
||||||
|
]
|
||||||
|
|
||||||
def current_file(self) -> Optional[str]:
|
def current_file(self) -> Optional[str]:
|
||||||
return self.main_window.current_file()
|
return self.main_window.tabs.current_file()
|
||||||
|
|
||||||
def create_tab(self, file: str):
|
def create_tab(self, file: str):
|
||||||
self.main_window.tabs.create_tab(file)
|
self.main_window.tabs.create_tab(file)
|
||||||
|
PluginRegistry.execute("after_open_file", file)
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user