Added file saving.
This commit is contained in:
parent
fd5b46d2e6
commit
362ef5aef4
4 changed files with 37 additions and 2 deletions
|
@ -2,3 +2,4 @@
|
||||||
|
|
||||||
def connect_gui(app):
|
def connect_gui(app):
|
||||||
app.main_window.openFile.triggered.connect(app.file_actions.open_files)
|
app.main_window.openFile.triggered.connect(app.file_actions.open_files)
|
||||||
|
app.main_window.saveFile.triggered.connect(app.file_actions.save_current_file)
|
||||||
|
|
17
file.py
17
file.py
|
@ -16,10 +16,23 @@ class FileActions:
|
||||||
|
|
||||||
if dialog.exec():
|
if dialog.exec():
|
||||||
for file_path in dialog.selectedFiles():
|
for file_path in dialog.selectedFiles():
|
||||||
self.app.open_files.append(File(self.app, file_path, file_path.split("/")[-1]))
|
|
||||||
if not file_path in self.app.open_files: # dont open file twice
|
if not file_path in self.app.open_files: # dont open file twice
|
||||||
self.app.open_files[file_path] = File(self.app, file_path, file_path.split("/")[-1])
|
self.app.open_files[file_path] = File(self.app, file_path, file_path.split("/")[-1])
|
||||||
|
|
||||||
|
def save_file(self, path):
|
||||||
|
oz_string = self.app.open_files[path].content_binary_input.toPlainText()
|
||||||
|
data = self.app.utils.oz_string_to_bstring(oz_string)
|
||||||
|
|
||||||
|
file = open(path, "wb")
|
||||||
|
file.write(data)
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
def save_current_file(self):
|
||||||
|
current_tab = self.app.main_window.openFileTabs.currentWidget()
|
||||||
|
current_file_path = current_tab.objectName()
|
||||||
|
|
||||||
|
self.save_file(current_file_path)
|
||||||
|
|
||||||
|
|
||||||
class File:
|
class File:
|
||||||
def __init__(self, app, path, name):
|
def __init__(self, app, path, name):
|
||||||
|
@ -35,7 +48,7 @@ class File:
|
||||||
self.content = file_content
|
self.content = file_content
|
||||||
|
|
||||||
# the widget that contains all the file content
|
# the widget that contains all the file content
|
||||||
self.content_widget = QWidget(self.app.main_window.openFileTabs)
|
self.content_widget = QWidget(self.app.main_window.openFileTabs, objectName=path)
|
||||||
self.content_widget_layout = QVBoxLayout()
|
self.content_widget_layout = QVBoxLayout()
|
||||||
|
|
||||||
self.content_binary_input = QPlainTextEdit(self.content_widget)
|
self.content_binary_input = QPlainTextEdit(self.content_widget)
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
<string>File</string>
|
<string>File</string>
|
||||||
</property>
|
</property>
|
||||||
<addaction name="openFile"/>
|
<addaction name="openFile"/>
|
||||||
|
<addaction name="saveFile"/>
|
||||||
</widget>
|
</widget>
|
||||||
<addaction name="fileMenu"/>
|
<addaction name="fileMenu"/>
|
||||||
</widget>
|
</widget>
|
||||||
|
@ -59,6 +60,14 @@
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
|
<action name="saveFile">
|
||||||
|
<property name="text">
|
||||||
|
<string>Save</string>
|
||||||
|
</property>
|
||||||
|
<property name="shortcut">
|
||||||
|
<string>Ctrl+S</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
</widget>
|
</widget>
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
|
|
12
utils.py
12
utils.py
|
@ -18,3 +18,15 @@ class Utils:
|
||||||
oz_string = " ".join(oz_bytes)
|
oz_string = " ".join(oz_bytes)
|
||||||
|
|
||||||
return oz_string
|
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
|
||||||
|
|
Loading…
Reference in a new issue