Implemented size check for files opened via command line, simplified some code and added some comments.

This commit is contained in:
The Wobbler 2024-12-09 16:24:15 +01:00
parent 7235736309
commit 46f49804a8
4 changed files with 25 additions and 25 deletions

18
file.py
View file

@ -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):

View file

@ -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())

1
ui.py
View file

@ -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

View file

@ -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)