Skip to content

Commit

Permalink
Merge pull request #182 from dlippok/photometry-export
Browse files Browse the repository at this point in the history
Photometry export
  • Loading branch information
dlippok authored Nov 17, 2024
2 parents c5e70e4 + 26aef77 commit 0c2d4b5
Show file tree
Hide file tree
Showing 10 changed files with 2,699 additions and 2,480 deletions.
1,670 changes: 854 additions & 816 deletions data/translations/de/LC_MESSAGES/io.github.dlippok.photometric-viewer.po

Large diffs are not rendered by default.

1,574 changes: 806 additions & 768 deletions data/translations/io.github.dlippok.photometric-viewer.pot

Large diffs are not rendered by default.

1,660 changes: 849 additions & 811 deletions data/translations/pl/LC_MESSAGES/io.github.dlippok.photometric-viewer.po

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion photometric_viewer/config/accelerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class Accelerators:
Accelerators(accelerators=["<alt>Left"], action="win.nav.back"),
Accelerators(accelerators=["<alt>Up"], action="win.nav.top"),
Accelerators(accelerators=["<alt>Home"], action="win.nav.home"),
Accelerators(accelerators=["<control>N"], action="app.new_window")
Accelerators(accelerators=["<control>n"], action="app.new_window"),
Accelerators(accelerators=["<control>e"], action="win.export_photometry")
]
22 changes: 0 additions & 22 deletions photometric_viewer/gui/dialogs/file_chooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,6 @@ def for_ldc(**kwargs):

return chooser

@staticmethod
def for_ldt(**kwargs):
chooser = ExportFileChooser(**kwargs)

file_filter = FileFilter(name=_("EULUMDAT (*.ldt)"))
file_filter.add_pattern("*.ldt")
chooser.add_filter(file_filter)
chooser._add_all_files_filter()

return chooser

@staticmethod
def for_ies(**kwargs):
chooser = ExportFileChooser(**kwargs)

file_filter = FileFilter(name=_("IESNA (*.ies)"))
file_filter.add_pattern("*.ies")
chooser.add_filter(file_filter)
chooser._add_all_files_filter()

return chooser


class FileChooser(FileChooserNative):
def __init__(self, **kwargs):
Expand Down
138 changes: 138 additions & 0 deletions photometric_viewer/gui/pages/photometry_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import io
from datetime import datetime

from gi.repository import Adw, Gio, Gtk
from gi.repository.Gtk import Box, Orientation, Label, PolicyType, ScrolledWindow, FileFilter, FileChooserDialog, \
Button, FileChooserNative
from gi.repository.Pango import WrapMode

from photometric_viewer.config.appearance import CLAMP_MAX_WIDTH
from photometric_viewer.formats import svg, png, ldt, ies
from photometric_viewer.gui.dialogs.file_chooser import ExportFileChooser
from photometric_viewer.gui.pages.base import BasePage
from photometric_viewer.gui.widgets.photomery_export.photometry_export_list import PhotometryExportList, \
LdtExportProperties
from photometric_viewer.model.luminaire import Luminaire
from photometric_viewer.utils.gi.gio import write_bytes, write_string
from photometric_viewer.utils.project import PROJECT


class PhotometryExportPage(BasePage):
def __init__(self, on_exported, transient_for: Gtk.Window, **kwargs):
super().__init__(_("Export Photometric File"), **kwargs)
self.current_name = _("untitled")
self.luminaire = None
self.on_exported = on_exported
self.transient_for = transient_for

box = Box(
orientation=Orientation.VERTICAL,
spacing=16,
margin_top=16,
margin_bottom=16,
margin_start=16,
margin_end=16,
)

self.export_list = PhotometryExportList()
box.append(self.export_list)

clamp = Adw.Clamp(maximum_size=CLAMP_MAX_WIDTH)
clamp.set_child(box)

scrolled_window = ScrolledWindow()
scrolled_window.set_child(clamp)
scrolled_window.set_vexpand(True)
scrolled_window.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC)

export_box = Box(
orientation=Orientation.VERTICAL
)
export_box.append(scrolled_window)

export_button = Button(
label=_("Export"),
css_classes=["pill", "suggested-action"],
halign=Gtk.Align.CENTER,
margin_top=24,
margin_bottom=24
)
export_button.connect("clicked", self.on_export_clicked)

export_box.append(
export_button
)

self.set_content(export_box)

def set_current_name(self, current_name):
self.current_name = current_name

def set_photometry(self, luminaire: Luminaire):
self.luminaire = luminaire

def on_export_clicked(self, dialog: FileChooserDialog):
self.show_file_chooser()

def show_file_chooser(self):
self.file_chooser = FileChooserNative(
transient_for=self.transient_for,
action=Gtk.FileChooserAction.SAVE,
select_multiple=False,
modal=True,
)
self.file_chooser.connect("response", self.on_export_response)

if isinstance(self.export_list.get_current_properties(), LdtExportProperties):
file_filter = FileFilter(name=_("EULUMDAT (*.ldt)"))
file_filter.add_pattern("*.ldt")
self.file_chooser.add_filter(file_filter)
self.file_chooser.set_current_name(f"{self.current_name}.ldt")
else:
file_filter = FileFilter(name=_("IESNA (*.ies)"))
file_filter.add_pattern("*.ies")
self.file_chooser.add_filter(file_filter)
self.file_chooser.set_current_name(f"{self.current_name}.ies")

all_files_filter = FileFilter(name=_("All files"))
all_files_filter.add_pattern("*")
self.file_chooser.add_filter(all_files_filter)
self.file_chooser.show()

def on_export_response(self, dialog, response):
if not self.luminaire:
return

if response != Gtk.ResponseType.ACCEPT:
return

export_properties = self.export_list.get_current_properties()

file: Gio.File = dialog.get_file()

if isinstance(export_properties, LdtExportProperties):
self.export_ldt(file, export_properties)
else:
self.export_ies(file, export_properties)

self.on_exported(file.get_basename())

def export_ldt(self, file: Gio.File, export_properties: LdtExportProperties):
with io.StringIO() as f:
ldt.export_to_file(f, self.luminaire)
write_string(file, f.getvalue())

def export_ies(self, file: Gio.File, export_properties: LdtExportProperties):
export_keywords = {
"_EXPORT_TOOL": PROJECT.name,
"_EXPORT_TOOL_VERSION": PROJECT.version,
"_EXPORT_TOOL_HOMEPAGE": PROJECT.urls.homepage,
"_EXPORT_TOOL_ISSUE_TRACKER": PROJECT.urls.bug_tracker,
"_EXPORT_TIMESTAMP": datetime.now().isoformat()
}

with io.StringIO() as f:
ies.export_to_file(f, self.luminaire, export_keywords)
write_string(file, f.getvalue())


8 changes: 2 additions & 6 deletions photometric_viewer/gui/widgets/app_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ class ApplicationMenuButton(Gtk.MenuButton):
<attribute name='label' translatable='yes'>Export</attribute>
<section>
<item>
<attribute name='label' translatable='yes'>As EULUMDAT</attribute>
<attribute name='action'>win.export_as_ldt</attribute>
</item>
<item>
<attribute name='label' translatable='yes'>As IESNA</attribute>
<attribute name='action'>win.export_as_ies</attribute>
<attribute name='label' translatable='yes'>Photometric File</attribute>
<attribute name='action'>win.export_photometry</attribute>
</item>
<item>
<attribute name='label' translatable='yes'>Light distribution curve</attribute>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import dataclasses

from gi.repository import Adw, Gtk
from gi.repository.Gtk import SelectionMode


@dataclasses.dataclass
class LdtExportProperties:
pass

@dataclasses.dataclass
class IesExportProperties:
pass

class PhotometryExportList(Gtk.ListBox):
def __init__(self):
super().__init__(
css_classes=["boxed-list"],
selection_mode=SelectionMode.NONE
)

self.format_selection_row = Adw.ComboRow(
title=_("Format"),
model = Gtk.StringList.new(
[
_("EULUMDAT"),
_("IES")
]
)
)
self.append(self.format_selection_row)

def get_current_properties(self):
if self.format_selection_row.get_selected() == 0:
return LdtExportProperties()
else:
return IesExportProperties()
67 changes: 11 additions & 56 deletions photometric_viewer/gui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from photometric_viewer.gui.pages.ldc_export import LdcExportPage
from photometric_viewer.gui.pages.ldc_zoom import LdcZoomPage
from photometric_viewer.gui.pages.photometry import PhotometryPage
from photometric_viewer.gui.pages.photometry_export import PhotometryExportPage
from photometric_viewer.gui.pages.source import SourceViewPage
from photometric_viewer.gui.pages.values import IntensityValuesPage
from photometric_viewer.gui.widgets.common.split_view import SplitView
Expand Down Expand Up @@ -57,7 +58,8 @@ def __init__(self, **kwargs):
self.luminaire_content_page = PhotometryContentPage()
self.source_view_page = SourceViewPage()
self.values_table_page = IntensityValuesPage()
self.ldc_export_page = LdcExportPage(on_exported=self.on_export_ldc_response, transient_for=self)
self.ldc_export_page = LdcExportPage(on_exported=self.on_export_response, transient_for=self)
self.photometry_export_page = PhotometryExportPage(on_exported=self.on_export_response, transient_for=self)
self.direct_ratios_page = DirectRatiosPage()
self.photometry_page = PhotometryPage()
self.geometry_page = GeometryPage()
Expand Down Expand Up @@ -90,12 +92,6 @@ def __init__(self, **kwargs):
self.csv_export_file_chooser = ExportFileChooser.for_csv(transient_for=self)
self.csv_export_file_chooser.connect("response", self.on_export_csv_response)

self.ldt_export_file_chooser = ExportFileChooser.for_ldt(transient_for=self)
self.ldt_export_file_chooser.connect("response", self.on_export_ldt_response)

self.ies_export_file_chooser = ExportFileChooser.for_ies(transient_for=self)
self.ies_export_file_chooser.connect("response", self.on_export_ies_response)

self.set_content(self.toast_overlay)

self.drop_target = DropTarget(
Expand Down Expand Up @@ -139,6 +135,7 @@ def display_photometry_content(self, luminaire: Luminaire):
self.photometry_page.set_photometry(luminaire)
self.geometry_page.set_photometry(luminaire)
self.ldc_zoom_page.set_photometry(luminaire)
self.photometry_export_page.set_photometry(luminaire)

self.opened_photometry = luminaire

Expand Down Expand Up @@ -223,46 +220,10 @@ def on_export_csv_response(self, dialog: FileChooserDialog, response):
write_string(file, data)
self.show_banner(_("Exported as {}").format(file.get_basename()))

def on_export_ldc_response(self, filename):
def on_export_response(self, filename):
self.show_start_page()
self.show_banner(_("Exported as {}").format(filename))

def on_export_ldt_response(self, dialog: FileChooserDialog, response):
if not self.opened_photometry:
return

if response != Gtk.ResponseType.ACCEPT:
return

file: Gio.File = dialog.get_file()

with io.StringIO() as f:
ldt.export_to_file(f, self.opened_photometry)
write_string(file, f.getvalue())
self.show_banner(_("Exported as {}").format(file.get_basename()))

def on_export_ies_response(self, dialog: FileChooserDialog, response):
if not self.opened_photometry:
return

if response != Gtk.ResponseType.ACCEPT:
return

file: Gio.File = dialog.get_file()

export_keywords = {
"_EXPORT_TOOL": PROJECT.name,
"_EXPORT_TOOL_VERSION": PROJECT.version,
"_EXPORT_TOOL_HOMEPAGE": PROJECT.urls.homepage,
"_EXPORT_TOOL_ISSUE_TRACKER": PROJECT.urls.bug_tracker,
"_EXPORT_TIMESTAMP": datetime.now().isoformat()
}

with io.StringIO() as f:
ies.export_to_file(f, self.opened_photometry, export_keywords)
write_string(file, f.getvalue())
self.show_banner(_("Exported as {}").format(file.get_basename()))

def on_title_visible_changed(self, *args):
title_visible = self.window_title.get_title_visible()
self.switcher_bar.set_reveal(title_visible)
Expand Down Expand Up @@ -296,8 +257,7 @@ def open_stream(self, f: IO):
("export_luminaire_as_json", self.show_json_export_file_chooser),
("export_intensities_as_csv", self.show_csv_export_file_chooser),
("export_ldc_as_image", self.show_ldc_export_page),
("export_as_ldt", self.show_ldt_export_file_chooser),
("export_as_ies", self.show_ies_export_file_chooser),
("export_photometry", self.show_photometry_export_page),
("save", self.on_save),
("autosave", self.on_autosave),
("save_as", self.on_save_as),
Expand All @@ -316,8 +276,7 @@ def update_file(self, file: Gio.File):
self.save_as_file_chooser.set_file(file)
self.json_export_file_chooser.set_current_name(f"{filename}.json")
self.csv_export_file_chooser.set_current_name(f"{filename}.csv")
self.ldt_export_file_chooser.set_current_name(f"{filename}_exported.ldt")
self.ies_export_file_chooser.set_current_name(f"{filename}_exported.ies")
self.photometry_export_page.set_current_name(f"{filename}_exported")

def open_file(self, file: Gio.File):
try:
Expand Down Expand Up @@ -389,16 +348,12 @@ def show_json_export_file_chooser(self, *args):
def show_csv_export_file_chooser(self, *args):
self.csv_export_file_chooser.show()

def show_ldc_export_file_chooser(self, *args):
self.ldc_export_file_chooser.show()

def show_ldt_export_file_chooser(self, *args):
self.ldt_export_file_chooser.show()

def show_ies_export_file_chooser(self, *args):
self.ies_export_file_chooser.show()
def show_photometry_export_page(self, *args):
self.show_start_page()
self.navigation_view.push(self.photometry_export_page)

def show_ldc_export_page(self, *args):
self.show_start_page()
self.navigation_view.push(self.ldc_export_page)

def on_update_source(self, buffer: Gtk.TextBuffer):
Expand Down

0 comments on commit 0c2d4b5

Please sign in to comment.