From f0152cc2cee275f79fa22e9bf655883cd8323f46 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Thu, 21 Nov 2024 19:47:15 +0100 Subject: [PATCH] Implemented reopening last opened file. --- editor.py | 4 ++++ file.py | 2 ++ main.py | 16 +++++++++++----- settings.py | 4 +++- ui.py | 8 +++----- utils.py | 11 +++++++++++ 6 files changed, 34 insertions(+), 11 deletions(-) diff --git a/editor.py b/editor.py index a3f06f0..5f04804 100644 --- a/editor.py +++ b/editor.py @@ -16,6 +16,8 @@ class BitEditor: self.app = app self.file = file + self.not_saved = False + self.setup_gui() def setup_gui(self): @@ -26,7 +28,9 @@ class BitEditor: self.input = BinaryTextEdit() self.input.setOverwriteMode(True) self.widget_layout.addWidget(self.input) + self.input.setPlainText(self.app.utils.bstring_to_oz(self.file.content)) + self.input.textChanged.connect(self.on_edit) self.input.setFont(self.font) diff --git a/file.py b/file.py index 7a8eaac..72f5266 100644 --- a/file.py +++ b/file.py @@ -61,6 +61,8 @@ class File: self.bit_editor = BitEditor(self.app, self) + self.app.settings.last_opened_file = self.path + def close(self): self.app.gui.main_window.openFileTabs.removeTab(self.bit_editor.tab_index) del self.app.open_files[self.path] diff --git a/main.py b/main.py index 1b5919a..2b38f13 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import sys from wobbl_tools.data_file import save_dataclass_json, load_dataclass_json from utils import Utils -from file import FileActions, File, BitEditor +import file from ui import GUI from settings import Settings @@ -12,11 +12,14 @@ class BreadEditor: def __init__(self): self.settings = load_dataclass_json(Settings, "settings.json") setattr(self.settings, "save", lambda: save_dataclass_json(self.settings, "settings.json")) - self.utils = Utils(self) - self.file_actions = FileActions(self) - self.gui = GUI(self) - self.BitEditor = BitEditor + # save the file module to a variable because we need to use it in other classes but we change some file class -- + # variables that also need to be accesible from the other classes. + self.file = file + File = file.File + self.utils = Utils(self) + self.file_actions = file.FileActions(self) + self.gui = GUI(self) self.gui.connect_gui(self) @@ -24,6 +27,9 @@ class BreadEditor: def run(self): self.gui.post_setup() + + self.utils.open_last_file() + self.utils.popup_init() self.gui.QTMainWindow.show() diff --git a/settings.py b/settings.py index 4db7b86..47bf7f9 100644 --- a/settings.py +++ b/settings.py @@ -1,8 +1,10 @@ #!/usr/bin/python3 +import os from dataclasses import dataclass @dataclass class Settings: - square_bits: bool = False + last_opened_file: str=f"{os.path.dirname(os.path.abspath(__file__))}/example.txt" + square_bits: bool=False diff --git a/ui.py b/ui.py index 81d91e8..1a9d7aa 100644 --- a/ui.py +++ b/ui.py @@ -1,6 +1,5 @@ #!/usr/bin/python3 -import sys from PyQt6.QtWidgets import QApplication, QMainWindow from PyQt6.QtGui import QFont from gui.main_window import Ui_MainWindow @@ -11,7 +10,7 @@ class GUI: def __init__(self, app): self.app = app - self.qt_app = QApplication(sys.argv) + self.qt_app = QApplication([]) self.QTMainWindow = QMainWindow() self.main_window = Ui_MainWindow() @@ -27,7 +26,6 @@ class GUI: def post_setup(self): self.main_window.bitsAreSquaresSetting.setChecked(self.app.settings.square_bits) - spacing = 200 if self.app.settings.square_bits else 100 # add spacing when setting is checked - print(self.app.settings.square_bits) + spacing = 200 if self.app.settings.square_bits else 100 - self.app.BitEditor.font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, spacing) + self.app.file.BitEditor.font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, spacing) diff --git a/utils.py b/utils.py index 12e3c31..0d959a8 100644 --- a/utils.py +++ b/utils.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 +import sys from pathlib import Path from PyQt6.QtWidgets import QMessageBox from PyQt6.QtGui import QFont @@ -78,6 +79,9 @@ class Utils: case "discard": self.close() + else: + self.close() + def close(self): print("Bye!") @@ -95,3 +99,10 @@ class Utils: editor.font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, spacing) editor.input.setFont(editor.font) + + def open_last_file(self): + if len(sys.argv) == 1: # if no parameters were passed to the editor + file_path = self.app.settings.last_opened_file + + file = self.app.file.File(self.app, file_path, file_path.split("/")[-1]) + self.app.open_files[file_path] = file