4848from .universal_gui_part import ChannelComboBox , CustomDoubleSpinBox , CustomSpinBox
4949
5050
51- def update (d , u ):
51+ def recursive_update (d , u ):
5252 if not isinstance (d , typing .MutableMapping ):
5353 d = {}
5454 for k , v in u .items ():
55- d [k ] = update (d .get (k , {}), v ) if isinstance (v , collections .abc .Mapping ) else v
55+ d [k ] = recursive_update (d .get (k , {}), v ) if isinstance (v , collections .abc .Mapping ) else v
5656 return d
5757
5858
@@ -70,13 +70,20 @@ def __init__(self):
7070 super ().__init__ ()
7171 self ._settings = None
7272
73- def add_settings (self , settings : BaseSettings ):
73+ def _update_choices (self ):
74+ if hasattr (self ._settings , "roi_profiles" ):
75+ self .clear ()
76+ self .addItems (list (self ._settings .roi_profiles .keys ()))
77+
78+ def set_settings (self , settings : BaseSettings ):
7479 self ._settings = settings
75- if hasattr (settings , "roi_profiles" ):
76- self .addItems (list (settings .roi_profiles .keys ()))
80+ if hasattr (self ._settings , "roi_profiles" ):
81+ self ._settings .roi_profiles .setted .connect (self ._update_choices )
82+ self ._settings .roi_profiles .deleted .connect (self ._update_choices )
83+ self ._update_choices ()
7784
7885 def get_value (self ):
79- if self ._settings is not None and hasattr (self ._settings , "roi_profiles" ):
86+ if self ._settings is not None and hasattr (self ._settings , "roi_profiles" ) and self . currentText () != "" :
8087 return self ._settings .roi_profiles [self .currentText ()]
8188 return None
8289
@@ -386,6 +393,7 @@ def _add_to_layout(
386393 if ap .name in start_values :
387394 ap .get_field ().set_starting (start_values [ap .name ])
388395 ap .change_fun .connect (_any_arguments (self .value_changed .emit ))
396+ self .channels_chose .append (ap .get_field ())
389397 return
390398 if isinstance (ap .get_field (), Widget ):
391399 layout .addRow (label , ap .get_field ().native )
@@ -402,7 +410,7 @@ def _add_to_layout(
402410 self .channels_chose .append (ap .get_field ())
403411 if issubclass (ap .value_type , ROIExtractionProfile ):
404412 # noinspection PyTypeChecker
405- ap .get_field ().add_settings (settings )
413+ ap .get_field ().set_settings (settings )
406414 if ap .name in start_values :
407415 with suppress (KeyError , ValueError , TypeError ):
408416 ap .set_value (start_values [ap .name ])
@@ -562,20 +570,20 @@ class BaseAlgorithmSettingsWidget(QScrollArea):
562570 values_changed = Signal ()
563571 algorithm_thread : SegmentationThread
564572
565- def __init__ (self , settings : BaseSettings , name , algorithm : typing .Type [ROIExtractionAlgorithm ]):
573+ def __init__ (self , settings : BaseSettings , algorithm : typing .Type [ROIExtractionAlgorithm ]):
566574 """
567575 For algorithm which works on one channel
568576 """
569577 super ().__init__ ()
570578 self .settings = settings
571579 self .widget_list = []
572- self .name = name
573580 self .algorithm = algorithm
574581 main_layout = QVBoxLayout ()
575582 self .info_label = QLabel ()
576583 self .info_label .setHidden (True )
584+ # FIXME verify inflo_label usage
577585 main_layout .addWidget (self .info_label )
578- start_values = settings .get (f"algorithm_widget_state.{ name } " , {})
586+ start_values = settings .get (f"algorithm_widget_state.{ self . name } " , {})
579587 self .form_widget = self ._form_widget (algorithm , start_values = start_values )
580588 self .form_widget .value_changed .connect (self .values_changed .emit )
581589 self .setWidget (self .form_widget )
@@ -585,6 +593,10 @@ def __init__(self, settings: BaseSettings, name, algorithm: typing.Type[ROIExtra
585593 self .algorithm_thread .info_signal .connect (self .show_info )
586594 self .algorithm_thread .exception_occurred .connect (self .exception_occurred )
587595
596+ @property
597+ def name (self ):
598+ return self .algorithm .get_name ()
599+
588600 @staticmethod
589601 def _form_widget (algorithm , start_values ) -> FormWidget :
590602 return FormWidget (
@@ -615,7 +627,7 @@ def exception_occurred(exc: Exception):
615627
616628 def show_info (self , text ):
617629 self .info_label .setText (text )
618- self .info_label .setVisible (True )
630+ self .info_label .setVisible (text != "" )
619631
620632 def image_changed (self , image : Image ):
621633 self .form_widget .image_changed (image )
@@ -633,9 +645,6 @@ def set_values(self, values_dict):
633645 def get_values (self ):
634646 return self .form_widget .get_values ()
635647
636- def channel_num (self ):
637- return self .channels_chose .currentIndex ()
638-
639648 def execute (self , exclude_mask = None ):
640649 values = self .get_values ()
641650 self .settings .set (f"algorithms.{ self .name } " , deepcopy (values ))
@@ -659,29 +668,18 @@ def recursive_get_values(self):
659668 return self .form_widget .recursive_get_values ()
660669
661670
662- class AlgorithmSettingsWidget (BaseAlgorithmSettingsWidget ):
663- def execute (self , exclude_mask = None ):
664- self .algorithm_thread .algorithm .set_image (self .settings .image )
665- super ().execute (exclude_mask )
666-
667-
668671class InteractiveAlgorithmSettingsWidget (BaseAlgorithmSettingsWidget ):
669672 algorithm_thread : SegmentationThread
670673
671- def __init__ (self , settings , name , algorithm : typing .Type [ROIExtractionAlgorithm ], selector : typing .List [QWidget ]):
672- super ().__init__ (settings , name , algorithm )
673- self .selector = selector
674+ def __init__ (self , settings , algorithm : typing .Type [ROIExtractionAlgorithm ], selector : typing .List [QWidget ]):
675+ super ().__init__ (settings , algorithm )
676+ self .selector = selector [:]
674677 self .algorithm_thread .finished .connect (self .enable_selector )
675678 self .algorithm_thread .started .connect (self .disable_selector )
676679 # noinspection PyUnresolvedReferences
677680 if hasattr (settings , "mask_changed" ):
678681 settings .mask_changed .connect (self .change_mask )
679682
680- def value_updated (self ):
681- if not self .parent ().interactive :
682- return
683- self .execute ()
684-
685683 def change_mask (self ):
686684 if not self .isVisible ():
687685 return
@@ -726,15 +724,15 @@ def __init__(self, settings: BaseSettings, algorithms: typing.Type[AlgorithmSele
726724 self .setLayout (layout )
727725
728726 @staticmethod
729- def _algorithm_widget (settings , name , val ) -> InteractiveAlgorithmSettingsWidget :
730- return InteractiveAlgorithmSettingsWidget (settings , name , val , [])
727+ def _algorithm_widget (settings , val ) -> InteractiveAlgorithmSettingsWidget :
728+ return InteractiveAlgorithmSettingsWidget (settings , val , [])
731729
732730 def add_widgets_to_algorithm (self ):
733731 self .algorithm_choose .blockSignals (True )
734732 self .algorithm_choose .clear ()
735733 for name , val in self .algorithms .__register__ .items ():
736734 self .algorithm_choose .addItem (name )
737- widget = self ._algorithm_widget (self .settings , name , val )
735+ widget = self ._algorithm_widget (self .settings , val )
738736 self .algorithm_dict [name ] = widget
739737 widget .algorithm_thread .execution_done .connect (self .result .emit )
740738 widget .algorithm_thread .finished .connect (self .finished .emit )
@@ -770,7 +768,9 @@ def updated_algorithm(self):
770768 def recursive_get_values (self ):
771769 result = {key : widget .recursive_get_values () for key , widget in self .algorithm_dict .items ()}
772770
773- self .settings .set ("algorithm_widget_state" , update (self .settings .get ("algorithm_widget_state" , {}), result ))
771+ self .settings .set (
772+ "algorithm_widget_state" , recursive_update (self .settings .get ("algorithm_widget_state" , {}), result )
773+ )
774774 return result
775775
776776 def change_algorithm (self , name , values : dict = None ):
0 commit comments