-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add persistence for unit values in SVG #1144
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -261,15 +261,18 @@ class UnitType(enum.Enum): | |
|
||
|
||
class ExportSizeModel(Observable.Observable): | ||
def __init__(self, display_item: DisplayItem.DisplayItem) -> None: | ||
def __init__(self, display_item: DisplayItem.DisplayItem, units_persistent_model: UserInterface.StringPersistentModel) -> None: | ||
super().__init__() | ||
self.__units_persistent_model = units_persistent_model | ||
display_size = self.__calculate_display_size_in_pixels(display_item) | ||
self.__width = display_size.width | ||
self.__height = display_size.height | ||
self.__aspect_ratio = self.__width / self.__height | ||
self.__units = UnitType.PIXELS | ||
self.__float_to_string_converter = Converter.FloatToStringConverter() | ||
self.__primary_field = 'width' # Primary field to determine which text is calculated | ||
self.__primary_field = 'width' # Primary field to determine which text is calculater | ||
# Load the persisted units value, default to PIXELS if not found | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This comment is outdated and can be removed. The default value is set by the caller. |
||
persisted_units = self.__units_persistent_model.value | ||
self.__units = UnitType[str(persisted_units)] | ||
Comment on lines
+274
to
+275
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple of notes here (minor points, depending on how you look at it):
A more difficult problem that is slightly trickier: we may want to expand the list of units in the future. If we do that and then try to load a file in this version, it may have persistent units that aren't recognized in this version and will throw an exception on line 275 above. The way I usually like to deal with this is by creating a separate map and reverse map for converting between the enum and the persistent value. Something along these lines (I didn't run this, but it's close):
Also, this avoids tying the file format to Python code (the enum.name). |
||
|
||
def __calculate_display_size_in_pixels(self, display_item: DisplayItem.DisplayItem) -> Geometry.IntSize: | ||
if display_item.display_data_shape and len(display_item.display_data_shape) == 2: | ||
|
@@ -327,6 +330,7 @@ def units(self, new_units: int) -> None: | |
new_enum = UnitType(new_units) | ||
if self.__units != new_enum: | ||
self.__units = new_enum | ||
self.__units_persistent_model.value = self.__units.name | ||
self.notify_property_changed("width") | ||
self.notify_property_changed("height") | ||
self.notify_property_changed("width_text") | ||
|
@@ -391,7 +395,12 @@ def __init__(self, document_controller: DocumentController.DocumentController, | |
super().__init__() | ||
self.__document_controller = document_controller | ||
self.__display_item = display_item | ||
self.__model = ExportSizeModel(display_item) | ||
self.__units_persistent_model = UserInterface.StringPersistentModel( | ||
ui=document_controller.ui, | ||
storage_key="export_units", | ||
value=UnitType.PIXELS.name # Default value if not found | ||
) | ||
self.__model = ExportSizeModel(display_item, self.__units_persistent_model) | ||
self.__handler = ExportSVGHandler(self.__model) | ||
self.__init_ui() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, you can just call it
units_model: Model.PropertyModel[str]
instead of the more specificunits_persistent_model: UserInterface.StringPersistentModel
. When receiving parameters, it's better to take the more general case instead of requiring the more specific. It makes testing easier. You could also consider adding a test here to avoid the bug that you'll discover when merging the latest changes onmaster
to this branch.