2024-11-17 18:54:51 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
from pathlib import Path
|
2024-11-20 13:05:24 +01:00
|
|
|
from PyQt6.QtWidgets import QMessageBox
|
2024-11-21 18:08:34 +01:00
|
|
|
from PyQt6.QtGui import QFont
|
2024-11-17 18:54:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Utils:
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
self.home_path = str(Path.home())
|
2024-11-19 17:52:08 +01:00
|
|
|
|
2024-11-20 13:05:24 +01:00
|
|
|
def popup_init(self):
|
|
|
|
self.usc_popup = QMessageBox() # create a popup window that notifies the user that we have unsaved changes
|
|
|
|
self.usc_popup.setWindowTitle("Unsaved Changes!") # usc means unsaved changes
|
|
|
|
self.usc_popup.setText("The file you are trying to close has unsaved changes.")
|
|
|
|
self.usc_popup.setStandardButtons(
|
|
|
|
QMessageBox.StandardButton.Save |
|
|
|
|
QMessageBox.StandardButton.Discard |
|
|
|
|
QMessageBox.StandardButton.Cancel
|
|
|
|
)
|
|
|
|
|
|
|
|
def unsaved_changes_popup(self):
|
|
|
|
button = self.usc_popup.exec()
|
|
|
|
|
|
|
|
match button:
|
|
|
|
case QMessageBox.StandardButton.Save:
|
|
|
|
return "save"
|
|
|
|
case QMessageBox.StandardButton.Discard:
|
|
|
|
return "discard"
|
|
|
|
case QMessageBox.StandardButton.Cancel:
|
|
|
|
return "cancel"
|
|
|
|
|
2024-11-19 17:52:08 +01:00
|
|
|
def bstring_to_oz(self, data): # convert binary data to a string of ones and zeros (oz)
|
2024-11-19 18:04:01 +01:00
|
|
|
oz_bytes = []
|
|
|
|
|
|
|
|
for byte in data:
|
|
|
|
oz_bytes.append(format(byte, "08b"))
|
|
|
|
|
|
|
|
oz_string = " ".join(oz_bytes)
|
2024-11-19 17:52:08 +01:00
|
|
|
|
|
|
|
return oz_string
|
2024-11-19 18:32:52 +01:00
|
|
|
|
|
|
|
def oz_string_to_bstring(self, oz_string): # convert a string of zeroes and ones to a binary string
|
|
|
|
oz_bytes = oz_string.split()
|
|
|
|
|
|
|
|
bytes_int = []
|
|
|
|
|
|
|
|
for byte in oz_bytes:
|
|
|
|
bytes_int.append(int(byte, 2))
|
|
|
|
|
|
|
|
binary_string = bytes(bytes_int)
|
|
|
|
|
|
|
|
return binary_string
|
2024-11-20 13:45:49 +01:00
|
|
|
|
|
|
|
def on_close(self, event):
|
|
|
|
changes = False
|
|
|
|
|
|
|
|
for file_path in self.app.open_files:
|
|
|
|
file = self.app.open_files[file_path]
|
|
|
|
|
2024-11-21 17:56:11 +01:00
|
|
|
if file.bit_editor.not_saved:
|
2024-11-20 13:45:49 +01:00
|
|
|
changes = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if changes:
|
|
|
|
save_or_not = self.unsaved_changes_popup()
|
|
|
|
|
|
|
|
match save_or_not:
|
|
|
|
case "save":
|
|
|
|
self.app.file_actions.save_all_files()
|
|
|
|
|
|
|
|
case "cancel":
|
|
|
|
event.ignore()
|
|
|
|
|
|
|
|
print("Bye!")
|
2024-11-21 18:08:34 +01:00
|
|
|
|
|
|
|
def update_font_in_all_bit_editors(self):
|
|
|
|
for file_path in self.app.open_files:
|
|
|
|
editor = self.app.open_files[file_path].bit_editor
|
|
|
|
|
|
|
|
square = self.app.gui.main_window.bitsAreSquaresSetting.isChecked()
|
|
|
|
|
|
|
|
spacing = 200 if square else 100 # add spacing when setting is checked
|
|
|
|
|
|
|
|
editor.font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, spacing)
|
|
|
|
editor.input.setFont(editor.font)
|