From 6922e3dc9b0b3db910658886a52fcf2460051fe6 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Fri, 22 Nov 2024 16:20:43 +0100 Subject: [PATCH] Fixed 2 bugs and OOPed everything a little more. --- connect_gui.py | 4 ++-- editor.py | 20 ++++++++++++++++---- main.py | 11 ++--------- ui.py | 14 +++----------- utils.py | 19 ++++--------------- 5 files changed, 27 insertions(+), 41 deletions(-) diff --git a/connect_gui.py b/connect_gui.py index 1ef3183..ba32773 100644 --- a/connect_gui.py +++ b/connect_gui.py @@ -6,5 +6,5 @@ def connect_gui(app): app.gui.main_window.openFileTabs.tabCloseRequested.connect(app.file_actions.close_current_file) app.gui.QTMainWindow.closeEvent = app.utils.on_close app.gui.main_window.menuSettings.triggered.connect(app.gui.main_window.settingsDock.show) - app.gui.main_window.bitsAreSquaresSetting.stateChanged.connect(app.utils.update_font_in_all_bit_editors) - app.gui.main_window.highlightOnesSetting.stateChanged.connect(app.utils.update_font_in_all_bit_editors) + app.gui.main_window.bitsAreSquaresSetting.stateChanged.connect(app.utils.update_style_in_all_bit_editors) + app.gui.main_window.highlightOnesSetting.stateChanged.connect(app.utils.update_style_in_all_bit_editors) diff --git a/editor.py b/editor.py index 5f04804..62e56b7 100644 --- a/editor.py +++ b/editor.py @@ -8,7 +8,6 @@ from highlighting import Higlighter class BitEditor: font = QFont("Ubuntu Mono", 12) - font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, 200) one_format = QTextCharFormat() one_format.setBackground(QColor(200, 150, 100)) @@ -33,11 +32,10 @@ class BitEditor: self.input.textChanged.connect(self.on_edit) - self.input.setFont(self.font) - self.bit_highlighter = Higlighter() self.bit_highlighter.add_mapping("1", self.one_format) - self.bit_highlighter.setDocument(self.input.document()) + + self.update_style() self.widget.setLayout(self.widget_layout) @@ -48,3 +46,17 @@ class BitEditor: def on_edit(self): self.not_saved = True + + def update_style(self): + square = self.app.settings.square_bits + + spacing = 200 if square else 100 # add spacing when setting is checked + + self.font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, spacing) + self.input.setFont(self.font) + + highlight_ones = self.app.settings.highlight_ones + + highlighter_document = self.input.document() if highlight_ones else None + + self.bit_highlighter.setDocument(highlighter_document) diff --git a/main.py b/main.py index 36b9777..c4b7c1e 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 -import file +from file import File, FileActions from ui import GUI from settings import Settings @@ -12,19 +12,12 @@ 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")) - print(self.settings.highlight_ones) - - # 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.file_actions = FileActions(self) self.gui = GUI(self) self.gui.connect_gui(self) - self.open_files: dict[str, File] = {} def run(self): diff --git a/ui.py b/ui.py index b863a21..8bda743 100644 --- a/ui.py +++ b/ui.py @@ -24,18 +24,10 @@ class GUI: self.main_window.settingsDock.hide() def post_setup(self): + self.main_window.bitsAreSquaresSetting.blockSignals(True) self.main_window.bitsAreSquaresSetting.setChecked(self.app.settings.square_bits) + self.main_window.bitsAreSquaresSetting.blockSignals(False) - spacing = 200 if self.app.settings.square_bits else 100 - - self.app.file.BitEditor.font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, spacing) - - print(self.app.settings.highlight_ones) self.main_window.highlightOnesSetting.setChecked(self.app.settings.highlight_ones) - for file_path in self.app.open_files: - editor = self.app.open_files[file_path].bit_editor - - highlighter_document = editor.input.document() if self.app.settings.highlight_ones else None - - editor.bit_highlighter.setDocument(highlighter_document) + self.app.utils.update_style_in_all_bit_editors() diff --git a/utils.py b/utils.py index 0723f74..ff16230 100644 --- a/utils.py +++ b/utils.py @@ -3,7 +3,7 @@ import sys from pathlib import Path from PyQt6.QtWidgets import QMessageBox -from PyQt6.QtGui import QFont +from file import File class Utils: @@ -87,29 +87,18 @@ class Utils: self.app.settings.save() - def update_font_in_all_bit_editors(self): + def update_style_in_all_bit_editors(self): self.app.settings.highlight_ones = self.app.gui.main_window.highlightOnesSetting.isChecked() self.app.settings.square_bits = self.app.gui.main_window.bitsAreSquaresSetting.isChecked() for file_path in self.app.open_files: editor = self.app.open_files[file_path].bit_editor - highlight_ones = self.app.settings.highlight_ones - - highlighter_document = editor.input.document() if highlight_ones else None - - editor.bit_highlighter.setDocument(highlighter_document) - - square = self.app.settings.square_bits - - spacing = 200 if square else 100 # add spacing when setting is checked - - editor.font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, spacing) - editor.input.setFont(editor.font) + editor.update_style() 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]) + file = File(self.app, file_path, file_path.split("/")[-1]) self.app.open_files[file_path] = file