OOPed everything a little more and added some comments.

This commit is contained in:
The Wobbler 2024-11-18 18:28:26 +01:00
parent d86c9acc58
commit 30c1b55419
4 changed files with 55 additions and 36 deletions

View file

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

45
file.py Normal file
View file

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

32
main.py
View file

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

View file

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