<s> from ginga import GingaPlugin <EOL> from ginga import colors <EOL> from ginga . gw import Widgets <EOL> from ginga . misc import ParamSet , Bunch <EOL> from ginga . util import dp <EOL> draw_colors = colors . get_colors ( ) <EOL> default_drawtype = '<STR_LIT>' <EOL> default_drawcolor = '<STR_LIT>' <EOL> fillkinds = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Drawing ( GingaPlugin . LocalPlugin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fv , fitsimage ) : <EOL> super ( Drawing , self ) . __init__ ( fv , fitsimage ) <EOL> self . layertag = '<STR_LIT>' <EOL> self . dc = fv . getDrawClasses ( ) <EOL> canvas = self . dc . DrawingCanvas ( ) <EOL> canvas . enable_draw ( True ) <EOL> canvas . enable_edit ( True ) <EOL> canvas . set_drawtype ( '<STR_LIT>' , color = '<STR_LIT>' ) <EOL> canvas . set_callback ( '<STR_LIT>' , self . draw_cb ) <EOL> canvas . set_callback ( '<STR_LIT>' , self . edit_cb ) <EOL> canvas . set_callback ( '<STR_LIT>' , self . edit_select_cb ) <EOL> canvas . setSurface ( self . fitsimage ) <EOL> canvas . register_for_cursor_drawing ( self . fitsimage ) <EOL> self . canvas = canvas <EOL> self . drawtypes = list ( canvas . get_drawtypes ( ) ) <EOL> self . drawcolors = draw_colors <EOL> self . linestyles = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . coordtypes = [ '<STR_LIT:data>' , '<STR_LIT>' ] <EOL> self . draw_args = [ ] <EOL> self . draw_kwdargs = { } <EOL> self . drawparams_cache = { } <EOL> self . edit_obj = None <EOL> self . _drawn_tags = [ ] <EOL> self . _mask_prefix = '<STR_LIT>' <EOL> def build_gui ( self , container ) : <EOL> top = Widgets . VBox ( ) <EOL> top . set_border_width ( <NUM_LIT:4> ) <EOL> vbox , sw , orientation = Widgets . get_oriented_box ( container ) <EOL> self . orientation = orientation <EOL> vbox . set_border_width ( <NUM_LIT:4> ) <EOL> vbox . set_spacing ( <NUM_LIT:2> ) <EOL> msgFont = self . fv . getFont ( "<STR_LIT>" , <NUM_LIT:12> ) <EOL> tw = Widgets . TextArea ( wrap = True , editable = False ) <EOL> tw . set_font ( msgFont ) <EOL> self . tw = tw <EOL> fr = Widgets . Expander ( "<STR_LIT>" ) <EOL> fr . set_widget ( tw ) <EOL> vbox . add_widget ( fr , stretch = <NUM_LIT:0> ) <EOL> fr = Widgets . Frame ( "<STR_LIT>" ) <EOL> captions = ( ( "<STR_LIT>" , '<STR_LIT:label>' , "<STR_LIT>" , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , '<STR_LIT:label>' , "<STR_LIT>" , '<STR_LIT>' ) , <EOL> ) <EOL> w , b = Widgets . build_info ( captions ) <EOL> self . w . update ( b ) <EOL> combobox = b . draw_type <EOL> for name in self . drawtypes : <EOL> combobox . append_text ( name ) <EOL> index = self . drawtypes . index ( default_drawtype ) <EOL> combobox . set_index ( index ) <EOL> combobox . add_callback ( '<STR_LIT>' , lambda w , idx : self . set_drawparams_cb ( ) ) <EOL> combobox = b . coord_type <EOL> for name in self . coordtypes : <EOL> combobox . append_text ( name ) <EOL> index = <NUM_LIT:0> <EOL> combobox . set_index ( index ) <EOL> combobox . add_callback ( '<STR_LIT>' , lambda w , idx : self . set_drawparams_cb ( ) ) <EOL> fr . set_widget ( w ) <EOL> vbox . add_widget ( fr , stretch = <NUM_LIT:0> ) <EOL> mode = self . canvas . get_draw_mode ( ) <EOL> hbox = Widgets . HBox ( ) <EOL> btn1 = Widgets . RadioButton ( "<STR_LIT>" ) <EOL> btn1 . set_state ( mode == '<STR_LIT>' ) <EOL> btn1 . add_callback ( '<STR_LIT>' , lambda w , val : self . set_mode_cb ( '<STR_LIT>' , val ) ) <EOL> btn1 . set_tooltip ( "<STR_LIT>" ) <EOL> self . w . btn_draw = btn1 <EOL> hbox . add_widget ( btn1 ) <EOL> btn2 = Widgets . RadioButton ( "<STR_LIT>" , group = btn1 ) <EOL> btn2 . set_state ( mode == '<STR_LIT>' ) <EOL> btn2 . add_callback ( '<STR_LIT>' , lambda w , val : self . set_mode_cb ( '<STR_LIT>' , val ) ) <EOL> btn2 . set_tooltip ( "<STR_LIT>" ) <EOL> self . w . btn_edit = btn2 <EOL> hbox . add_widget ( btn2 ) <EOL> hbox . add_widget ( Widgets . Label ( '<STR_LIT>' ) , stretch = <NUM_LIT:1> ) <EOL> vbox . add_widget ( hbox , stretch = <NUM_LIT:0> ) <EOL> fr = Widgets . Frame ( "<STR_LIT>" ) <EOL> vbox2 = Widgets . VBox ( ) <EOL> self . w . attrlbl = Widgets . Label ( ) <EOL> vbox2 . add_widget ( self . w . attrlbl , stretch = <NUM_LIT:0> ) <EOL> self . w . drawvbox = Widgets . VBox ( ) <EOL> vbox2 . add_widget ( self . w . drawvbox , stretch = <NUM_LIT:1> ) <EOL> fr . set_widget ( vbox2 ) <EOL> vbox . add_widget ( fr , stretch = <NUM_LIT:0> ) <EOL> captions = ( ( "<STR_LIT>" , '<STR_LIT:label>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> "<STR_LIT>" , '<STR_LIT:label>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" , '<STR_LIT>' , <EOL> "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" , '<STR_LIT>' ) , <EOL> ) <EOL> w , b = Widgets . build_info ( captions ) <EOL> self . w . update ( b ) <EOL> b . delete_obj . add_callback ( '<STR_LIT>' , lambda w : self . delete_object ( ) ) <EOL> b . delete_obj . set_tooltip ( "<STR_LIT>" ) <EOL> b . delete_obj . set_enabled ( False ) <EOL> b . scale_by . add_callback ( '<STR_LIT>' , self . scale_object ) <EOL> b . scale_by . set_text ( '<STR_LIT>' ) <EOL> b . scale_by . set_tooltip ( "<STR_LIT>" ) <EOL> b . scale_by . set_enabled ( False ) <EOL> b . rotate_by . add_callback ( '<STR_LIT>' , self . rotate_object ) <EOL> b . rotate_by . set_text ( '<STR_LIT>' ) <EOL> b . rotate_by . set_tooltip ( "<STR_LIT>" ) <EOL> b . rotate_by . set_enabled ( False ) <EOL> b . create_mask . add_callback ( '<STR_LIT>' , lambda w : self . create_mask ( ) ) <EOL> b . create_mask . set_tooltip ( "<STR_LIT>" ) <EOL> b . clear_canvas . add_callback ( '<STR_LIT>' , lambda w : self . clear_canvas ( ) ) <EOL> b . clear_canvas . set_tooltip ( "<STR_LIT>" ) <EOL> vbox . add_widget ( w , stretch = <NUM_LIT:0> ) <EOL> spacer = Widgets . Label ( '<STR_LIT>' ) <EOL> vbox . add_widget ( spacer , stretch = <NUM_LIT:1> ) <EOL> top . add_widget ( sw , stretch = <NUM_LIT:1> ) <EOL> btns = Widgets . HBox ( ) <EOL> btns . set_spacing ( <NUM_LIT:4> ) <EOL> btn = Widgets . Button ( "<STR_LIT>" ) <EOL> btn . add_callback ( '<STR_LIT>' , lambda w : self . close ( ) ) <EOL> btns . add_widget ( btn , stretch = <NUM_LIT:0> ) <EOL> btns . add_widget ( Widgets . Label ( '<STR_LIT>' ) , stretch = <NUM_LIT:1> ) <EOL> top . add_widget ( btns , stretch = <NUM_LIT:0> ) <EOL> container . add_widget ( top , stretch = <NUM_LIT:1> ) <EOL> self . toggle_create_button ( ) <EOL> def close ( self ) : <EOL> self . fv . stop_local_plugin ( self . chname , str ( self ) ) <EOL> def instructions ( self ) : <EOL> self . tw . set_text ( <EOL> """<STR_LIT>""" ) <EOL> def start ( self ) : <EOL> self . instructions ( ) <EOL> self . set_drawparams_cb ( ) <EOL> p_canvas = self . fitsimage . get_canvas ( ) <EOL> try : <EOL> obj = p_canvas . getObjectByTag ( self . layertag ) <EOL> except KeyError : <EOL> p_canvas . add ( self . canvas , tag = self . layertag ) <EOL> self . resume ( ) <EOL> def pause ( self ) : <EOL> self . canvas . ui_setActive ( False ) <EOL> def resume ( self ) : <EOL> self . canvas . ui_setActive ( True ) <EOL> self . fv . showStatus ( "<STR_LIT>" ) <EOL> def stop ( self ) : <EOL> p_canvas = self . fitsimage . get_canvas ( ) <EOL> try : <EOL> p_canvas . deleteObjectByTag ( self . layertag ) <EOL> except : <EOL> pass <EOL> self . canvas . set_draw_mode ( '<STR_LIT>' ) <EOL> self . canvas . ui_setActive ( False ) <EOL> self . fv . showStatus ( "<STR_LIT>" ) <EOL> def redo ( self ) : <EOL> pass <EOL> def draw_cb ( self , canvas , tag ) : <EOL> obj = canvas . getObjectByTag ( tag ) <EOL> self . _drawn_tags . append ( tag ) <EOL> self . toggle_create_button ( ) <EOL> self . logger . info ( "<STR_LIT>" % ( obj . kind ) ) <EOL> return True <EOL> def set_drawparams_cb ( self ) : <EOL> if self . canvas . get_draw_mode ( ) != '<STR_LIT>' : <EOL> return <EOL> index = self . w . draw_type . get_index ( ) <EOL> kind = self . drawtypes [ index ] <EOL> index = self . w . coord_type . get_index ( ) <EOL> coord = self . coordtypes [ index ] <EOL> self . w . drawvbox . remove_all ( ) <EOL> drawClass = self . canvas . getDrawClass ( kind ) <EOL> self . w . attrlbl . set_text ( "<STR_LIT>" % ( kind ) ) <EOL> paramlst = drawClass . get_params_metadata ( ) <EOL> params = self . drawparams_cache . setdefault ( kind , Bunch . Bunch ( ) ) <EOL> self . draw_params = ParamSet . ParamSet ( self . logger , params ) <EOL> w = self . draw_params . build_params ( paramlst , <EOL> orientation = self . orientation ) <EOL> self . draw_params . add_callback ( '<STR_LIT>' , self . draw_params_changed_cb ) <EOL> self . w . drawvbox . add_widget ( w , stretch = <NUM_LIT:1> ) <EOL> self . w . delete_obj . set_enabled ( False ) <EOL> self . w . scale_by . set_enabled ( False ) <EOL> self . w . rotate_by . set_enabled ( False ) <EOL> args , kwdargs = self . draw_params . get_params ( ) <EOL> if kind != '<STR_LIT>' : <EOL> kwdargs [ '<STR_LIT>' ] = coord <EOL> self . canvas . set_drawtype ( kind , ** kwdargs ) <EOL> def draw_params_changed_cb ( self , paramObj , params ) : <EOL> index = self . w . draw_type . get_index ( ) <EOL> kind = self . drawtypes [ index ] <EOL> args , kwdargs = self . draw_params . get_params ( ) <EOL> self . canvas . set_drawtype ( kind , ** kwdargs ) <EOL> def edit_cb ( self , fitsimage , obj ) : <EOL> if obj != self . edit_obj : <EOL> self . edit_select_cb ( fitsimage , obj ) <EOL> else : <EOL> self . draw_params . params_to_widgets ( ) <EOL> def edit_params_changed_cb ( self , paramObj , obj ) : <EOL> self . draw_params . widgets_to_params ( ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> tomap = self . fitsimage . get_coordmap ( obj . coord ) <EOL> if obj . crdmap != tomap : <EOL> obj . convert_mapper ( tomap ) <EOL> paramObj . params_to_widgets ( ) <EOL> obj . sync_state ( ) <EOL> whence = <NUM_LIT:2> <EOL> self . canvas . redraw ( whence = whence ) <EOL> def edit_initialize ( self , fitsimage , obj ) : <EOL> self . w . drawvbox . remove_all ( ) <EOL> self . edit_obj = obj <EOL> if ( obj is not None ) and self . canvas . is_selected ( obj ) : <EOL> self . w . attrlbl . set_text ( "<STR_LIT>" % ( obj . kind ) ) <EOL> drawClass = obj . __class__ <EOL> paramlst = drawClass . get_params_metadata ( ) <EOL> self . draw_params = ParamSet . ParamSet ( self . logger , obj ) <EOL> w = self . draw_params . build_params ( paramlst , <EOL> orientation = self . orientation ) <EOL> self . draw_params . add_callback ( '<STR_LIT>' , self . edit_params_changed_cb ) <EOL> self . w . drawvbox . add_widget ( w , stretch = <NUM_LIT:1> ) <EOL> self . w . delete_obj . set_enabled ( True ) <EOL> self . w . scale_by . set_enabled ( True ) <EOL> self . w . rotate_by . set_enabled ( True ) <EOL> else : <EOL> self . w . attrlbl . set_text ( "<STR_LIT>" ) <EOL> self . w . delete_obj . set_enabled ( False ) <EOL> self . w . scale_by . set_enabled ( False ) <EOL> self . w . rotate_by . set_enabled ( False ) <EOL> def edit_select_cb ( self , fitsimage , obj ) : <EOL> self . logger . debug ( "<STR_LIT>" % str ( obj ) ) <EOL> self . edit_initialize ( fitsimage , obj ) <EOL> def set_mode_cb ( self , mode , tf ) : <EOL> if tf : <EOL> self . canvas . set_draw_mode ( mode ) <EOL> if mode == '<STR_LIT>' : <EOL> self . edit_initialize ( self . fitsimage , None ) <EOL> elif mode == '<STR_LIT>' : <EOL> self . set_drawparams_cb ( ) <EOL> return True <EOL> def toggle_create_button ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . _drawn_tags ) > <NUM_LIT:0> : <EOL> self . w . create_mask . set_enabled ( True ) <EOL> else : <EOL> self . w . create_mask . set_enabled ( False ) <EOL> def create_mask ( self ) : <EOL> """<STR_LIT>""" <EOL> ntags = len ( self . _drawn_tags ) <EOL> if ntags == <NUM_LIT:0> : <EOL> return <EOL> old_image = self . fitsimage . get_image ( ) <EOL> if old_image is None : <EOL> return <EOL> mask = None <EOL> obj_kinds = set ( ) <EOL> for tag in self . _drawn_tags : <EOL> obj = self . canvas . get_object_by_tag ( tag ) <EOL> try : <EOL> cur_mask = old_image . get_shape_mask ( obj ) <EOL> except Exception as e : <EOL> self . logger . error ( '<STR_LIT>' . format ( str ( e ) ) ) <EOL> continue <EOL> if mask is not None : <EOL> mask |= cur_mask <EOL> else : <EOL> mask = cur_mask <EOL> obj_kinds . add ( obj . kind ) <EOL> image = dp . make_image ( mask . astype ( '<STR_LIT>' ) , old_image , { } , <EOL> pfx = self . _mask_prefix ) <EOL> imname = image . get ( '<STR_LIT:name>' ) <EOL> self . fv . gui_call ( self . fv . add_image , imname , image , chname = self . chname ) <EOL> image . make_callback ( '<STR_LIT>' ) <EOL> s = '<STR_LIT>' . format ( <EOL> ntags , '<STR_LIT:U+002C>' . join ( sorted ( obj_kinds ) ) ) <EOL> iminfo = self . chinfo . get_image_info ( imname ) <EOL> iminfo . reason_modified = s <EOL> self . logger . info ( s ) <EOL> def clear_canvas ( self ) : <EOL> self . canvas . clear_selected ( ) <EOL> self . canvas . deleteAllObjects ( ) <EOL> self . _drawn_tags = [ ] <EOL> self . toggle_create_button ( ) <EOL> def delete_object ( self ) : <EOL> tag = self . canvas . lookup_object_tag ( self . canvas . _edit_obj ) <EOL> self . _drawn_tags . remove ( tag ) <EOL> self . toggle_create_button ( ) <EOL> self . canvas . edit_delete ( ) <EOL> self . canvas . redraw ( whence = <NUM_LIT:2> ) <EOL> def rotate_object ( self , w ) : <EOL> delta = float ( w . get_text ( ) ) <EOL> self . canvas . edit_rotate ( delta , self . fitsimage ) <EOL> def scale_object ( self , w ) : <EOL> delta = float ( w . get_text ( ) ) <EOL> self . canvas . edit_scale ( delta , delta , self . fitsimage ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' </s>
