From 46f49804a88da97221bad336b396314b98fb3e15 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Mon, 9 Dec 2024 16:24:15 +0100 Subject: [PATCH] Implemented size check for files opened via command line, simplified some code and added some comments. --- file.py | 18 +++++++++++------- main.py | 4 ++-- ui.py | 1 - utils.py | 27 ++++++++++++--------------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/file.py b/file.py index e2a881e..60c89e1 100644 --- a/file.py +++ b/file.py @@ -20,13 +20,7 @@ class FileActions: dialog.setViewMode(QFileDialog.ViewMode.List) if dialog.exec(): - for file_path in dialog.selectedFiles(): - if not file_path in self.app.open_files: # dont open file twice - if os.path.getsize(file_path) > MAX_FILE_SIZE: - self.app.utils.ftb_popup.exec() - return - - self.app.open_files[file_path] = File(self.app, file_path, file_path.split("/")[-1]) + self.open_multiple_files(dialog.selectedFiles()) def create_file(self, content: bin=b""): file_path, extension = QFileDialog.getSaveFileName( @@ -82,6 +76,16 @@ class FileActions: for file_path in self.app.open_files: self.app.open_files[file_path].save() + def open_multiple_files(self, file_paths): + for file_path in file_paths: + if not file_path in self.app.open_files and os.path.isfile(file_path): + if os.path.getsize(file_path) > MAX_FILE_SIZE: + self.app.utils.ftb_popup.exec() + return + + file = File(self.app, file_path, file_path.split("/")[-1]) + self.app.open_files[file_path] = file + class File: def __init__(self, app, path, name): diff --git a/main.py b/main.py index 52e3d64..c62dc3f 100755 --- a/main.py +++ b/main.py @@ -38,10 +38,10 @@ class BreadEditor: self.gui.post_setup() - self.utils.on_start() - self.utils.popup_init() + self.utils.on_start() + self.gui.QTMainWindow.show() sys.exit(self.gui.qt_app.exec()) diff --git a/ui.py b/ui.py index 8bda743..8dacb37 100644 --- a/ui.py +++ b/ui.py @@ -1,7 +1,6 @@ #!/usr/bin/python3 from PyQt6.QtWidgets import QApplication, QMainWindow -from PyQt6.QtGui import QFont from gui.main_window import Ui_MainWindow from connect_gui import connect_gui diff --git a/utils.py b/utils.py index 0e7c8bd..265d016 100644 --- a/utils.py +++ b/utils.py @@ -33,7 +33,7 @@ class Utils: "https://teapot.informationsanarchistik.de/Wobbl/Bread_Editor" ) - def unsaved_changes_popup(self): + def unsaved_changes_popup(self): # show popup and simplify return values button = self.usc_popup.exec() match button: @@ -69,14 +69,14 @@ class Utils: def on_close(self, event): changes = False - for file_path in self.app.open_files: + for file_path in self.app.open_files: # check for files that have unsaved changes file = self.app.open_files[file_path] if file.bit_editor.not_saved: changes = True break - if changes: + if changes: # show a popup that informs the user that there are unsaved changes and ask them what to do save_or_not = self.unsaved_changes_popup() match save_or_not: @@ -94,8 +94,7 @@ class Utils: self.close() def close(self): - print("Bye!") - + # get paths of open files and save them to the settings to reopen them automatically on next start file_keys = self.app.open_files.keys() open_files = [] @@ -106,7 +105,10 @@ class Utils: self.app.settings.save(f"{self.editor_path}/settings.json") + print("Bye!") + def update_style_in_all_bit_editors(self): + # update the highlighting and character spacing when a setting has changed self.app.settings.highlight_ones = self.app.gui.main_window.highlightOnesSetting.isChecked() self.app.settings.square_bits = self.app.gui.main_window.bitsAreSquaresSetting.isChecked() @@ -116,23 +118,18 @@ class Utils: editor.update_style() def on_start(self): + # either open the lastly opened files or open the files specified by the parameters if len(sys.argv) == 1: # if no parameters were passed to the editor - self.load_files(self.app.settings.last_opened_files) + file_paths = self.app.settings.last_opened_files else: file_paths = sys.argv[1:] - self.load_files(file_paths) - - def load_files(self, file_paths): - for file_path in file_paths: - if not file_path in self.app.open_files and os.path.isfile(file_path): - file = File(self.app, file_path, file_path.split("/")[-1]) - self.app.open_files[file_path] = file + self.app.file_actions.open_multiple_files(file_paths) def check_file_queue(self): - if not len(self.app.open_files_queue) == 0: + if not self.app.open_files_queue == []: # check for file paths in the queue that the ipc server put there filenames = self.app.open_files_queue self.app.open_files_queue = [] - self.load_files(filenames) + self.app.file_actions.open_multiple_files(filenames)