Added binary file format

This commit is contained in:
2026-07-20 15:39:34 +02:00
parent 9ed8e371ef
commit 42bf09610b
13 changed files with 106 additions and 461 deletions

View File

@@ -40,7 +40,7 @@ from bedit.core.model import (
)
from bedit.core.simulation import Simulation
from bedit.core.port_types import PortTypeRegistry
from bedit.core.serializer import JsonDocumentSerializer
from bedit.core.serializer import DocumentSerializer
class DocumentController(QObject):
@@ -108,7 +108,7 @@ class DocumentController(QObject):
self.filePathChanged.emit(None)
def load(self, path: Path) -> None:
self.document = JsonDocumentSerializer.load(path)
self.document = DocumentSerializer.load(path)
self.active_component_id = next(iter(self.document.roots), None)
self.file_path = path
self.undo_stack.clear()
@@ -124,7 +124,7 @@ class DocumentController(QObject):
target = path or self.file_path
if target is None:
raise ValueError("No file path has been selected")
JsonDocumentSerializer.save(self.document, target)
DocumentSerializer.save(self.document, target)
self.file_path = target
self.undo_stack.setClean()
self.filePathChanged.emit(target)

View File

@@ -3,7 +3,7 @@ from pathlib import Path
from PySide6.QtCore import QSettings, Signal
from PySide6.QtWidgets import QDialog, QFileDialog
from bedit.core.libraries import default_library_paths
from bedit.core.libraries import bundled_library_path, default_library_paths
from bedit.gui.preferences import application_settings
from bedit.gui.generated.ui_settings_dialog import Ui_SettingsDialog
@@ -56,8 +56,22 @@ class SettingsDialog(QDialog):
settings = settings if settings is not None else application_settings()
value = settings.value("libraries/paths", default_library_paths())
if isinstance(value, str):
return [value]
return [str(path) for path in value]
paths = [value]
else:
paths = [str(path) for path in value]
bundled = bundled_library_path()
migrated = [
str(bundled)
if not Path(path).exists()
and Path(path).parent == bundled.parent
and Path(path).suffix.lower() == ".json"
else path
for path in paths
]
if migrated != paths:
settings.setValue("libraries/paths", migrated)
settings.sync()
return migrated
@staticmethod
def graph_grid_size(settings: QSettings | None = None) -> int:
@@ -79,7 +93,7 @@ class SettingsDialog(QDialog):
self,
"Add library",
"",
"BEdit libraries (*.json);;All files (*)",
"BEdit libraries (*.beb *.bedit.json *.json);;All files (*)",
)
if path:
self._append_unique_path(path)

View File

@@ -176,7 +176,7 @@ class Ui_SettingsDialog(object):
self.iconGridLabel.setText(QCoreApplication.translate("SettingsDialog", u"Icon grid size:", None))
self.iconGridSpinBox.setSuffix(QCoreApplication.translate("SettingsDialog", u" units", None))
self.settingsTabs.setTabText(self.settingsTabs.indexOf(self.generalTab), QCoreApplication.translate("SettingsDialog", u"General", None))
self.libraryPathsLabel.setText(QCoreApplication.translate("SettingsDialog", u"Load library JSON files from these files or folders at startup:", None))
self.libraryPathsLabel.setText(QCoreApplication.translate("SettingsDialog", u"Load library JSON or BEdit Binary files from these files or folders at startup:", None))
self.addLibraryFileButton.setText(QCoreApplication.translate("SettingsDialog", u"Add File\u2026", None))
self.addLibraryFolderButton.setText(QCoreApplication.translate("SettingsDialog", u"Add Folder\u2026", None))
self.removeLibraryPathButton.setText(QCoreApplication.translate("SettingsDialog", u"Remove", None))

View File

@@ -14,7 +14,7 @@ from PySide6.QtWidgets import (
from bedit.core.model import Component, Parameter
from bedit.core.application_log import get_logger
from bedit.core.serializer import JsonDocumentSerializer
from bedit.core.serializer import DocumentSerializer
from bedit.core.simulation import Simulation
from bedit.gui.controllers.document import DocumentController
from bedit.gui.dialogs.component_options import ComponentOptionsDialog
@@ -407,7 +407,11 @@ class MainWindow(QMainWindow):
if not self._resolve_source_edits() or not self._maybe_save():
return
filename, _ = QFileDialog.getOpenFileName(
self, "Open graph", "", "BEdit graphs (*.bedit.json *.json);;All files (*)"
self,
"Open graph",
"",
"BEdit documents (*.bedit.json *.json *.beb);;"
"BEdit JSON (*.bedit.json *.json);;BEdit Binary (*.beb);;All files (*)",
)
if not filename:
return
@@ -437,16 +441,21 @@ class MainWindow(QMainWindow):
def save_document_as(self) -> bool:
if self.document_controller.document is None:
return False
filename, _ = QFileDialog.getSaveFileName(
filename, selected_filter = QFileDialog.getSaveFileName(
self,
"Save graph",
"untitled.bedit.json",
"BEdit graphs (*.bedit.json);;JSON files (*.json);;All files (*)",
"untitled.beb",
"BEdit Binary (*.beb);;BEdit JSON (*.bedit.json *.json);;All files (*)",
)
if not filename:
return False
path = Path(filename)
if path.suffix.lower() not in {".json", ".beb"}:
path = path.with_suffix(
".beb" if "Binary" in selected_filter else ".bedit.json"
)
try:
path = self.document_controller.save(Path(filename))
path = self.document_controller.save(path)
self.log.info("Saved document %s", path)
except OSError as error:
self.log.error("Could not save document %s: %s", filename, error)
@@ -557,7 +566,7 @@ class MainWindow(QMainWindow):
try:
if library is not None:
library.document.validate()
JsonDocumentSerializer.save(library.document, Path(library.source_path))
DocumentSerializer.save(library.document, Path(library.source_path))
except (OSError, ValueError) as error:
component.inputs, component.outputs = old_inputs, old_outputs
self.log.error("Could not change library ports: %s", error)

View File

@@ -1,4 +1,3 @@
import json
from pathlib import Path
from PySide6.QtCore import QObject, Signal
@@ -23,7 +22,7 @@ class LibraryRepository(QObject):
for candidate in library_candidates(path):
try:
libraries.append(load_library_file(candidate))
except (OSError, ValueError, KeyError, TypeError, json.JSONDecodeError) as error:
except (OSError, ValueError, KeyError, TypeError) as error:
warnings.append(f"{candidate}: {error}")
self.libraries = libraries
self.load_warnings = warnings