-
Notifications
You must be signed in to change notification settings - Fork 77
Open
Description
The MainWindow class (and therefore also mainwindow.py) keeps getting larger and larger, so maybe it would be a good idea to refactor the whole thing into separate modules. For example, each action could be refactored into a command (in its own module) instead of handling all the logic in MainWindow:
from abc import ABC, abstractmethod
class Command(ABC):
@abstractmethod
def execute(self, *args, **kwargs):
passfrom PySide6.QtWidgets import QFileDialog, QMessageBox
from pathlib import Path
from .base import Command
class OpenDataCommand(Command):
def __init__(self, model, parent):
self.model = model
self.parent = parent
def execute(self, fname=None):
if fname is None:
fnames, _ = QFileDialog.getOpenFileNames(self.parent, "Open raw")
else:
fnames = [fname]
for fname in fnames:
if not (Path(fname).is_file() or Path(fname).is_dir()):
QMessageBox.critical(
self.parent,
"File Error",
f"File {fname} does not exist anymore."
)
return
# determine file type (simplified example)
ext = "".join(Path(fname).suffixes).lower()
if ext in (".xdf", ".xdfz", ".xdf.gz"):
# you could further delegate to an XDF command here
self.model.load(fname) # placeholder
else:
try:
self.model.load(fname)
except (FileNotFoundError, ValueError) as e:
QMessageBox.critical(self.parent, "Error", str(e))from PySide6.QtWidgets import QMainWindow, QIcon, QKeySequence
from mnelab.commands.open_data import OpenDataCommand
# ... other imports ...
class MainWindow(QMainWindow):
def __init__(self, model):
super().__init__()
self.model = model
# ... initialization code ...
# Create command objects:
self.open_data_cmd = OpenDataCommand(model, self)
# setup File menu action:
file_menu = self.menuBar().addMenu("&File")
self.actions = {}
self.actions["open_file"] = file_menu.addAction(
QIcon.fromTheme("open-file"),
"&Open...",
lambda: self.open_data_cmd.execute(),
QKeySequence.Open
)
# ... add other menu items ...Metadata
Metadata
Assignees
Labels
No labels