Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes undo stack and clears stack on project creation #37

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions rascal2/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import logging
import pathlib
import platform
import sys
from os import PathLike

from rascal2.core import Settings
from rascal2.core import Settings, get_global_settings

SOURCE_PATH = pathlib.Path(__file__).parent
STATIC_PATH = SOURCE_PATH / "static"
Expand Down Expand Up @@ -72,12 +73,26 @@ def setup_logging(log_path: str | PathLike, level: int = logging.INFO) -> loggin

"""
path = pathlib.Path(log_path)
logger = logging.getLogger(path.stem)
logger = logging.getLogger("rascal_log")
logger.setLevel(level)
logger.handlers.clear()

# TODO add console print handler when console is added
# https://github.com/RascalSoftware/RasCAL-2/issues/5
log_filehandler = logging.FileHandler(path)
logger.addHandler(log_filehandler)

return logger


def log_uncaught_exceptions(exc_type, exc_value, exc_traceback):
"""Qt slots swallows exceptions but this ensures exceptions are logged"""
logger = logging.getLogger("rascal_log")
if not logger.handlers:
# Backup in case the crash happens before the local logger setup
path = pathlib.Path(get_global_settings().fileName()).parent
path.mkdir(parents=True, exist_ok=True)
logger.addHandler(logging.FileHandler(path / "crash.log"))
logger.critical("An unhandled exception occurred!", exc_info=(exc_type, exc_value, exc_traceback))
logging.shutdown()
sys.exit(1)
4 changes: 2 additions & 2 deletions rascal2/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from rascal2.core.settings import Settings
from rascal2.core.settings import Settings, get_global_settings

__all__ = ["Settings"]
__all__ = ["Settings", "get_global_settings"]
4 changes: 2 additions & 2 deletions rascal2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from PyQt6 import QtGui, QtWidgets

from rascal2.config import handle_scaling, path_for
from rascal2.config import handle_scaling, log_uncaught_exceptions, path_for
from rascal2.ui.view import MainWindowView


Expand All @@ -28,7 +28,7 @@ def ui_execute():

def main():
multiprocessing.freeze_support()

sys.excepthook = log_uncaught_exceptions
exit_code = ui_execute()
sys.exit(exit_code)

Expand Down
2 changes: 1 addition & 1 deletion rascal2/ui/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def __init__(self, view):
self.view = view
self.model = MainWindowModel()
self.title = self.view.windowTitle()
self.undo_stack = self.view.undo_stack

def create_project(self, name: str, save_path: str):
"""Creates a new RAT project and controls object then initialise UI.
Expand All @@ -39,6 +38,7 @@ def create_project(self, name: str, save_path: str):
# https://github.com/RascalSoftware/RasCAL-2/issues/15
self.view.init_settings_and_log(save_path)
self.view.setup_mdi()
self.view.undo_stack.clear()

def edit_controls(self, setting: str, value: Any):
"""Edit a setting in the Controls object.
Expand Down
21 changes: 13 additions & 8 deletions rascal2/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,20 @@ def create_actions(self):
self.save_project_action.setIcon(QtGui.QIcon(path_for("save-project.png")))
self.save_project_action.setShortcut(QtGui.QKeySequence.StandardKey.Save)

self.undo_action = QtGui.QAction("&Undo", self)
self.undo_action.setStatusTip("Undo")
self.undo_action = self.undo_stack.createUndoAction(self, "&Undo")
self.undo_action.setStatusTip("Undo the last action")
self.undo_action.setIcon(QtGui.QIcon(path_for("undo.png")))
self.undo_action.setShortcut(QtGui.QKeySequence.StandardKey.Undo)

self.redo_action = QtGui.QAction("&Redo", self)
self.redo_action.setStatusTip("Redo")
self.redo_action = self.undo_stack.createRedoAction(self, "&Redo")
self.redo_action.setStatusTip("Redo the last undone action")
self.redo_action.setIcon(QtGui.QIcon(path_for("redo.png")))
self.redo_action.setShortcut(QtGui.QKeySequence.StandardKey.Redo)

self.undo_view_action = QtGui.QAction("Undo &History", self)
self.undo_view_action.setStatusTip("View undo history")
self.undo_view_action.triggered.connect(self.undo_view.show)

self.export_plots_action = QtGui.QAction("Export", self)
self.export_plots_action.setStatusTip("Export Plots")
self.export_plots_action.setIcon(QtGui.QIcon(path_for("export-plots.png")))
Expand Down Expand Up @@ -128,11 +132,12 @@ def create_menus(self):
file_menu.addSeparator()
file_menu.addAction(self.exit_action)

# edit_menu = main_menu.addMenu("&Edit")
edit_menu = main_menu.addMenu("&Edit")
edit_menu.addAction(self.undo_action)
edit_menu.addAction(self.redo_action)
edit_menu.addAction(self.undo_view_action)

tools_menu = main_menu.addMenu("&Tools")
tools_menu.addAction(self.undo_action)
tools_menu.addAction(self.redo_action)
# tools_menu = main_menu.addMenu("&Tools")

windows_menu = main_menu.addMenu("&Windows")
windows_menu.addAction(self.tile_windows_action)
Expand Down
Loading