OrsayDev / OrsayLaser

Instrumentation for Laser / Blanker / Gain usage in Nionswift
0 stars 0 forks source link

Changed Event in [gain_inst.py] #6

Closed yvesauad closed 4 years ago

yvesauad commented 4 years ago

In gain_inst.py there is a self.property_changed_event that updates my widgets that were all initiazed in the init inside gainView in [gain_panel.py].

This is not estrictly an issue but rather a question. How does this work?

yvesauad commented 4 years ago

Hint: If my decorator is, lets say, @property for a start_wav_f(self) and a @start_wav_f.setter for a start_wav_f(self, value: float) then self.property_changed_event.fire("start_wav_f") works while self.property_changed_event.fire("start_wav") doesn't.

Of course thus this .fire is related to my def instead of my declared variables.

kociak commented 4 years ago

Sounds like there are two questions. In Swift, with the Declarative interface, you don't need to use explicitly the property_changed_event. when in the View you are making a binding (for example ui.create_line_edit(text="@binding(instrument.start_wav)"), the binding mechanism will take care to reflect any change in the start_wav property of your instrument into the UI, and vice versa. However, from time to time (and quite extensively when discussing with an instrument), the correspondance is not direct between an UI element and the corresponding property. For example, if you change a the start_line lineedit of the gainView, maybe it will take time for the instrument object to send the information to the real world instrument, and then get notified by the hardware that the startwavelength has been really changed. Or maybe the real value is not as round as submitted. So in those case, the instrument object needs to more explicitly notify the UI of the change (the final value of the initial wavelength, as measured/set by the real life instrument). It does this by self.property_changed_event.fire("name_of_the_property"). So, if you don't deal with hardware or network connection, most of the time you just need to declare the self.property_changed, but the binding does the job for you. Second question: the event fire method expect the name of a property if it is used in the framework of a Binding. So this explains that... Now, you can use swift event for any sort of notification, which are not restricted at all to binding. You can listen to any event (by adding a listener to the given event). This can be the self.property_changed event (the one implicitly used in the Binding process), but it can also be another event you declare yourself. in that case, of course, you need to explicitly attach one or several listeners to it if you want something to happen. The Binding mechanism attaches automatically listeners that takes care to synchronize the interface and properties.

yvesauad commented 4 years ago

thanks @kociak i appreciate your complete answer. I got it :+1: :+1: