104 lines
3 KiB
Python
104 lines
3 KiB
Python
#!/usr/bin/python3
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
from PyQt6.QtWidgets import QMessageBox
|
|
from file import File
|
|
|
|
|
|
class Utils:
|
|
def __init__(self, app):
|
|
self.app = app
|
|
|
|
self.home_path = str(Path.home())
|
|
|
|
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"
|
|
|
|
def bstring_to_oz(self, data): # convert binary data to a string of ones and zeros (oz)
|
|
oz_bytes = []
|
|
|
|
for byte in data:
|
|
oz_bytes.append(format(byte, "08b"))
|
|
|
|
oz_string = " ".join(oz_bytes)
|
|
|
|
return oz_string
|
|
|
|
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
|
|
|
|
def on_close(self, event):
|
|
changes = False
|
|
|
|
for file_path in self.app.open_files:
|
|
file = self.app.open_files[file_path]
|
|
|
|
if file.bit_editor.not_saved:
|
|
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()
|
|
self.close()
|
|
|
|
case "cancel":
|
|
event.ignore()
|
|
|
|
case "discard":
|
|
self.close()
|
|
|
|
else:
|
|
self.close()
|
|
|
|
def close(self):
|
|
print("Bye!")
|
|
|
|
self.app.settings.save()
|
|
|
|
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
|
|
|
|
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 = File(self.app, file_path, file_path.split("/")[-1])
|
|
self.app.open_files[file_path] = file
|