Skip to content

Commit

Permalink
Refactor export svg to isolate actual file writing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeyer committed May 20, 2024
1 parent 83376a2 commit 1f1397c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
15 changes: 12 additions & 3 deletions nion/swift/DocumentController.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from nion.swift.model import Profile
from nion.swift.model import Project
from nion.swift.model import Symbolic
from nion.swift.model import UISettings
from nion.swift.model import Utility
from nion.swift.model import WorkspaceLayout
from nion.ui import CanvasItem
Expand Down Expand Up @@ -870,12 +871,12 @@ def export_file(self, display_item: DisplayItem.DisplayItem) -> None:
filter_line = writer_name + " files (" + writer_extensions + ")"
filter_lines.append(filter_line)
filter_line_to_writer_map[filter_line] = writer
filter = ";;".join(filter_lines)
filter += ";;All Files (*.*)"
filter_str = ";;".join(filter_lines)
filter_str += ";;All Files (*.*)"
export_dir = self.ui.get_persistent_string("export_directory", self.ui.get_document_location())
export_dir = os.path.join(export_dir, display_item.displayed_title)
selected_filter = self.ui.get_persistent_string("export_filter")
path, selected_filter, selected_directory = self.get_save_file_path(_("Export File"), export_dir, filter, selected_filter)
path, selected_filter, selected_directory = self.get_save_file_path(_("Export File"), export_dir, filter_str, selected_filter)
selected_writer = filter_line_to_writer_map.get(selected_filter)
if path and not os.path.splitext(path)[1]:
if selected_writer:
Expand All @@ -893,6 +894,14 @@ def export_files(self, display_items: typing.Sequence[DisplayItem.DisplayItem])
elif len(display_items) == 1:
self.export_file(display_items[0])

def export_svg_file(self, ui_settings: UISettings.UISettings, display_item: DisplayItem.DisplayItem, display_shape: Geometry.IntSize, path: pathlib.Path) -> None:
path = path.with_suffix(".svg")
drawing_context, shape = DisplayPanel.preview(ui_settings, display_item, display_shape.width, display_shape.height)
view_box = Geometry.IntRect(Geometry.IntPoint(), shape)
svg = drawing_context.to_svg(shape, view_box)
with Utility.AtomicFileWriter(path) as fp:
fp.write(svg)

def export_svg(self, display_item: DisplayItem.DisplayItem) -> None:
ExportDialog.ExportSVGDialog(self, display_item)

Expand Down
12 changes: 1 addition & 11 deletions nion/swift/ExportDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,8 @@ def ok_clicked() -> bool:
path = path + os.path.extsep + "svg"
if path:
ui.set_persistent_string("export_directory", selected_directory)

display_shape = Geometry.IntSize(height=height_px, width=width_px)

drawing_context, shape = DisplayPanel.preview(DisplayPanel.DisplayPanelUISettings(ui), display_item, display_shape.width, display_shape.height)

view_box = Geometry.IntRect(Geometry.IntPoint(), shape)

svg = drawing_context.to_svg(shape, view_box)

with Utility.AtomicFileWriter(pathlib.Path(path)) as fp:
fp.write(svg)

document_controller.export_svg_file(DisplayPanel.DisplayPanelUISettings(ui), display_item, display_shape, pathlib.Path(path))
return True

def cancel_clicked() -> bool:
Expand Down

0 comments on commit 1f1397c

Please sign in to comment.