From 30c1b55419c9ac6c39f67c6cb023589941544e6a Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Mon, 18 Nov 2024 18:28:26 +0100 Subject: [PATCH] OOPed everything a little more and added some comments. --- connect_gui.py | 2 +- file.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ main.py | 32 ++++++++------------------------ utils.py | 12 +----------- 4 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 file.py diff --git a/connect_gui.py b/connect_gui.py index f21b89e..8c10c69 100644 --- a/connect_gui.py +++ b/connect_gui.py @@ -1,4 +1,4 @@ #!/usr/bin/python3 def connect_gui(app): - app.main_window.openFile.triggered.connect(app.utils.open_file) + app.main_window.openFile.triggered.connect(app.file_actions.open_files) diff --git a/file.py b/file.py new file mode 100644 index 0000000..3d05e39 --- /dev/null +++ b/file.py @@ -0,0 +1,45 @@ +#!/usr/bin/python3 + +from PyQt6.QtWidgets import QWidget, QLabel, QFileDialog + + +class FileActions: + def __init__(self, app): + self.app = app + + def open_files(self): + dialog = QFileDialog(self.app.QTMainWindow) + dialog.setDirectory(self.app.utils.home_path) + dialog.setFileMode(QFileDialog.FileMode.ExistingFiles) + dialog.setNameFilters(["Binary (*.bin)", "Any (*)"]) + dialog.setViewMode(QFileDialog.ViewMode.List) + + if dialog.exec(): + for file_path in dialog.selectedFiles(): + self.app.open_files.append(File(self.app, file_path, file_path.split("/")[-1])) + + +class File: + def __init__(self, app, path, name): + self.app = app + + self.path = path + self.name = name + + file = open(path, "r") + file_content = file.read() + file.close() + + self.content = file_content + + # the widget that contains all the file content + self.content_widget = QWidget(self.app.main_window.openFileTabs) + self.content_label = QLabel(self.content_widget) # the label for the text in the file + self.content_label.setText(file_content) + + self.app.main_window.openFileTabs.addTab( # add a tab for the file in the top files list + self.content_widget, + self.name + ) + + diff --git a/main.py b/main.py index 9068796..c56a1d8 100644 --- a/main.py +++ b/main.py @@ -1,45 +1,29 @@ #!/usr/bin/python3 import sys -from PyQt6 import QtWidgets -from gui.main_window import Ui_MainWindow +from PyQt6.QtWidgets import QApplication, QMainWindow from utils import Utils +from file import FileActions from connect_gui import connect_gui +from gui.main_window import Ui_MainWindow class BreadEditor: def __init__(self): self.utils = Utils(self) - self.open_files = {} + self.file_actions = FileActions(self) - self.qt_app = QtWidgets.QApplication(sys.argv) + self.open_files = [] - self.QTMainWindow = QtWidgets.QMainWindow() + self.qt_app = QApplication(sys.argv) + + self.QTMainWindow = QMainWindow() self.main_window = Ui_MainWindow() self.main_window.setupUi(self.QTMainWindow) connect_gui(self) - def open_file(self, file_path): - file = open(file_path, "r") - file_content = file.read() - file.close() - - self.open_files[file_path] = { - "name": file_path.split("/")[-1], # set name that shows in the tab list - "content": file_content - } - - content_widget = QtWidgets.QWidget(self.main_window.openFileTabs) - content_label = QtWidgets.QLabel(content_widget) - content_label.setText(file_content) - - self.main_window.openFileTabs.addTab( - content_widget, - self.open_files[file_path]["name"] - ) - def run(self): self.QTMainWindow.show() sys.exit(self.qt_app.exec()) diff --git a/utils.py b/utils.py index fe46e30..ee7ea9b 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,7 @@ from pathlib import Path from PyQt6.QtWidgets import QFileDialog +from file import File class Utils: @@ -9,14 +10,3 @@ class Utils: self.app = app self.home_path = str(Path.home()) - - def open_file(self): - dialog = QFileDialog(self.app.QTMainWindow) - dialog.setDirectory(self.home_path) - dialog.setFileMode(QFileDialog.FileMode.ExistingFiles) - dialog.setNameFilters(["Binary (*.bin)", "Any (*)"]) - dialog.setViewMode(QFileDialog.ViewMode.List) - - if dialog.exec(): - for file_path in dialog.selectedFiles(): - self.app.open_file(file_path)