Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/pymodaq_plugins_utils/hardware/shaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,21 @@ def fill_shaper_settings(settings, name: str, width: int, height: int, pixel_siz
has_internal_calibration: bool = False) -> None:
"""Fill the groups of :func:`shaper_params` in the ``settings`` of a plugin (to be called in ``ini_stage``)

``use_internal_calibration`` is set to ``has_internal_calibration``, the user can change it afterwards.
``use_internal_calibration`` is the choice of the user: it is set to ``has_internal_calibration`` only the first
time (or if ``has_internal_calibration`` changed since the settings were saved, for instance with another
controller), otherwise the value restored from the preset is kept. It is never True without an internal calibration.
"""
geometry = settings.child(GEOMETRY_NAME)
for key, value in (('width', width), ('height', height), ('pixel_size_x', pixel_size_x),
('pixel_size_y', pixel_size_x if pixel_size_y is None else pixel_size_y)):
geometry.child(key).setValue(value)
properties = settings.child(PROPERTIES_NAME)
keep_choice = bool(properties['has_internal_calibration']) == has_internal_calibration
use_internal_calibration = has_internal_calibration and (bool(properties['use_internal_calibration'])
if keep_choice else True)
for key, value in (('name', name), ('modulator_type', ModulatorType(modulator_type).value),
('has_internal_calibration', has_internal_calibration),
('use_internal_calibration', has_internal_calibration)):
('use_internal_calibration', use_internal_calibration)):
properties.child(key).setValue(value)


Expand Down
20 changes: 20 additions & 0 deletions tests/test_shaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ def test_use_internal_calibration_is_the_choice_of_the_user():
assert shaper.has_internal_calibration and not shaper.use_internal_calibration


def test_choice_of_the_user_survives_a_new_initialization():
module = FakeModule()
fill(module, has_internal_calibration=True)
assert Shaper.from_module(module).use_internal_calibration # default: use it
module.plugin_settings.child('shaper_properties', 'use_internal_calibration').setValue(False)
fill(module, has_internal_calibration=True) # ini_stage again, settings restored from the preset
assert not Shaper.from_module(module).use_internal_calibration


def test_choice_reset_if_the_capability_changes():
module = FakeModule()
fill(module, has_internal_calibration=False)
assert not Shaper.from_module(module).use_internal_calibration
fill(module, has_internal_calibration=True)
assert Shaper.from_module(module).use_internal_calibration
module.plugin_settings.child('shaper_properties', 'use_internal_calibration').setValue(False)
fill(module, has_internal_calibration=False)
assert not Shaper.from_module(module).use_internal_calibration


def test_plugin_without_the_groups():
assert Shaper.from_module(FakeModule([])) is None
assert Shaper.from_module(object()) is None
Expand Down
Loading