;+ ; NAME: ; cw_tpmgr ; PURPOSE: (one line) ; Photometry template manager for itool. ; DESCRIPTION: ; ; CATEGORY: ; Compound Widgets ; CALLING SEQUENCE: ; result = cw_tpmgr( parent ) ; INPUTS: ; parent = ID of parent base. ; OPTIONAL INPUT PARAMETERS: ; ; KEYWORD PARAMETERS: ; TMPLFILE = Name of file containing templates. ; UVALUE = Optional user value. ; OUTPUTS: ; ; COMMON BLOCKS: ; SIDE EFFECTS: ; ; RESTRICTIONS: ; ; PROCEDURE: ; ; MODIFICATION HISTORY: ; Written by Doug Loucks, Lowell Observatory, July, 1994. This version ; is completely new. ; 97/12/12, MWB, Changed behavior to sort the template list by name. ;- ; ------------------------------------------------------------------------------ ; Procedure cw_tpmgr_add ; ------------------------------------------------------------------------------ pro cw_tpmgr_add,state,name,objnam,x,y widget_control,state.tplistid,get_uvalue=tp_list if state.nplates eq 0 then begin p = 0 state.nplates = 1 tp_list = [ {name:name,selected:0B,tlbid:0L,tpid:0L} ] endif else begin tp_list = [tp_list,tp_list[0]] p = n_elements(tp_list) - 1 tp_list[p].name = name idx=sort(strupcase(tp_list[*].name)) if state.selected ge 0 then oldselected = tp_list[state.selected].name tp_list=tp_list[idx] p = where(name eq tp_list[*].name) p = p[0] if state.selected ge 0 then begin z = where(oldselected eq tp_list[*].name) state.selected = z[0] endif state.nplates = state.nplates + 1 endelse tp_list[p].tlbid = $ widget_base(title=tp_list[p].name,event_func='cw_tpmgr_eve', $ group_leader=state.topbase) if objnam[0] eq '' then begin numobj=0 new = [1B] t_objnam = [''] endif else begin numobj=n_elements(objnam) new = bytarr(numobj) t_objnam = objnam endelse tplate = {numobj:numobj, $ mode:0B, $ modified:0B, $ new:new, $ objnam:t_objnam, $ x:x, $ y:y } tp_list[p].tpid = cw_tplat(tp_list[p].tlbid,value=tplate) ;Refresh the template list widget. widget_control,state.tplistid,set_value=tp_list[*].name, $ set_list_select=state.selected widget_control,tp_list[p].tlbid,map=0 widget_control,state.tplistid,set_uvalue=tp_list end ; ------------------------------------------------------------------------------ ; Procedure cw_tpmgr_load ; Loads a set of templates from a template file. ; ------------------------------------------------------------------------------ pro cw_tpmgr_load,state bel = string(7B) if state.tmplfile eq '' then begin message,'No file.'+bel,/info return endif if not exists( state.tmplfile ) then begin message,'Template file '+state.tmplfile+' does not exist.'+bel,/info return endif if state.nplates gt 0 then begin answer = qannounc('Warning. Existing templates will be purged.', $ truelabel='Purge templates and continue', $ falselabel='Cancel Load Request') if answer eq 0 then return widget_control,state.tplistid,get_uvalue=tp_list for j=0,state.nplates-1 do begin widget_control,tp_list[j].tlbid,/destroy endfor tp_list = tp_list[0] tp_list[0].name = '' tp_list[0].selected = 0B tp_list[0].tlbid = 0L tp_list[0].tpid = 0L state.nplates = 0 widget_control,state.tplistid,set_uvalue=tp_list endif get_lun,lu err = 0 openr,lu,state.tmplfile,error=err if err ne 0 then begin ; Problem with file. message,!error_state.msg +bel,/info free_lun,lu return endif on_ioerror,rderr version = '' readf,lu,version if version ne state.version then begin message,'Old file version '+version+'. Not loaded.',/info close,lu free_lun,lu return endif count = 0 while not eof(lu) do begin ; Read in the templates. name = '' readf,lu,name ; Template name. numobj = 0 readf,lu,numobj ; # objects. aix = 0 aiy = 0 ax = 0.0 ay = 0.0 aobjnam = '' readf,lu,ax,ay,aobjnam ; First (anchor) position. ;Initialize a temporary structure vector to hold the incoming objects ;(the first element will hold the anchor position). objnam = [strtrim(aobjnam,2)] x = [ax] y = [ay] ; Get the remaining coordinate pairs. tx = 0.0 ty = 0.0 tobjnam = '' for j=1,numobj-1 do begin readf,lu,tx,ty,tobjnam x = [x,tx] y = [y,ty] objnam = [objnam,strtrim(tobjnam,2)] endfor cw_tpmgr_add,state,name,objnam,x,y count = count + 1 endwhile free_lun,lu widget_control,state.tplistid,get_uvalue=tp_list,/no_copy widget_control,state.tplistid,set_value=tp_list[*].name widget_control,state.tplistid,set_uvalue=tp_list,/no_copy message,strn(count)+' templates loaded from file '+state.tmplfile,/info return rderr: free_lun,lu message,!error_state.msg+bel,/info end ; ------------------------------------------------------------------------------ ; Function cw_tpmgr_gvl ; Get_value function. ; ------------------------------------------------------------------------------ ;This function returns the 'value' of the Template Manager, which is defined ;to be the template list array. It has the following structure: ; {name:'', selected:0B, tlbid:0L, tpid:0L} ; ;Tag tlbid is the Top Level Base of the template. ;Tag tpid is the id of the template widget. function cw_tpmgr_gvl,id stash = widget_info(id,/child) widget_control,stash,get_uvalue=state,/no_copy widget_control,state.tplistid,get_uvalue=tp_list widget_control,stash,set_uvalue=state,/no_copy return,tp_list end ; ------------------------------------------------------------------------------ ; Function cw_tpmgr_eve ; Main event handler. ; ------------------------------------------------------------------------------ function cw_tpmgr_eve,event widget_control,event.id,/hourglass stash = widget_info(event.handler,/CHILD ) widget_control,stash,get_uvalue=state,/no_copy bel = string(7B) out_event = 0 CASE event.id OF state.delid : begin if state.selected GE 0 then begin widget_control,state.tplistid,get_uvalue=tp_list con = qannounc(TITLE='Delete Template Confirmation', $ 'Delete template '+tp_list[state.selected].name+'?', $ TRUELABEL='Yes, continue', $ FALSELABEL='No, cancel request' ) if not con then begin ;Don't delete the template. widget_control,stash,set_uvalue=state,/no_copy return,0 endif ;Destroy the template. widget_control,tp_list[state.selected].tlbid,/destroy ;Clear some tags. tp_list[state.selected].name = '' tp_list[state.selected].selected = 0B tp_list[state.selected].tlbid = 0L tp_list[state.selected].tpid = 0L ;Extract all but the deleted template and re-build the list. w = where(tp_list[*].name ne '',count) if count ne 0 then begin tp_list = tp_list[w] state.nplates = count endif else begin tp_list = tp_list[state.selected] state.nplates = 0 endelse widget_control,state.tplistid,set_value=tp_list[*].name widget_control,state.tpnameid,set_value='' widget_control,state.tplistid,set_uvalue=tp_list state.selected = -1 endif end state.dismissid : begin ;Access the template list. widget_control,state.tplistid,get_uvalue=tp_list,/no_copy if state.selected ge 0 then begin ;A template is selected. Un-map it, reset the list and name edit ;widgets. widget_control,tp_list[state.selected].tlbid,map=0 widget_control,state.tplistid,set_value=tp_list[*].name widget_control,state.tpnameid,set_value='' endif widget_control,event.top,map=0 widget_control,event.top,get_uvalue=status,/no_copy status.active = 0B widget_control,event.top,set_uvalue=status,/no_copy widget_control,state.tplistid,set_uvalue=tp_list,/no_copy end state.fnselid : begin f = dialog_pickfile(title='Select a template file') fn = strtrim(f,2) if fn ne '' then begin widget_control,state.fntxtid,set_value=fn state.tmplfile = fn cw_tpmgr_load,state endif end state.fntxtid : begin widget_control,event.id,get_value=value state.tmplfile = strtrim(value[0],2) message,'Filename set to '+state.tmplfile,/info end state.loadid : begin cw_tpmgr_load,state end state.newid : begin newname = qinput(prompt='Please enter new template name:') if newname ne '' then begin cw_tpmgr_add,state,newname,[''],[0.0],[0.0] endif end state.purgid : begin if state.nplates gt 0 then begin con = qannounc('Purge all templates?', $ TITLE='Template Purge Confirmation', TRUE='Yes, Continue', $ FALSE='No, Cancel Request', XSIZE=40 ) if con then begin widget_control, state.tplistid, get_uvalue=tp_list for j=0, state.nplates-1 do begin if tp_list[j].tlbid NE 0L then begin if tp_list[j].tlbid NE 0L then begin widget_control, tp_list[j].tlbid, /DESTROY endif tp_list[j].name = '' tp_list[j].selected = 0B tp_list[j].tlbid = 0L tp_list[j].tpid = 0L endif endfor tp_list = [ tp_list[0] ] widget_control, state.tplistid, set_uvalue=tp_list widget_control, state.tplistid, set_value='' widget_control, state.tpnameid, set_value='' state.nplates = 0 endif endif end ; The template file has the following format for each template entry: ; First line : File version code (text). ; Second line : Template name (text). ; Third line : n ; Number of objects (numeric). ; Fourth line : x y ; First object absolute position (numeric). ; Next n-1 lines : x y ; Object positions relative to first. ; etc... state.saveid : begin if state.tmplfile eq '' then begin message, 'Error. Template file not defined.' + bel, /INFO widget_control, stash, set_uvalue=state, /no_copy return, 0 endif if state.nplates eq 0 then begin message, 'Error. Empty template list.' + bel, /INFO widget_control, stash, set_uvalue=state, /no_copy return, 0 endif if exists( state.tmplfile ) then begin t = [' File ' + state.tmplfile + ' exists.', $ ' It will be overwritten, unless you choose to cancel and', $ 'change the name of the file.' ] con = qannounc( t, TITLE='File Overwrite Confirmation', $ FALSE='Cancel', TRUE='Overwrite File' ) if not con then begin widget_control, stash, set_uvalue=state, /no_copy return, 0 endif endif widget_control, state.tplistid, get_uvalue=tp_list, /no_copy count = 0 get_lun, lu openw, lu, state.tmplfile, ERROR=err if err eq 0 then begin printf, lu, state.version for j = 0, state.nplates-1 do begin widget_control, tp_list[j].tpid, GET_VALUE=tplate, /no_copy if tplate.numobj gt 0 then begin ; Write the template to the file. printf, lu, tp_list[j].name printf, lu, tplate.numobj for k = 0, tplate.numobj-1 do begin printf, lu, tplate.x[k], tplate.y[k], ' ', $ tplate.objnam[k], FORMAT='(2F10.3,A,A)' endfor tplate.modified = 0B count = count + 1 endif else begin message, 'Warning. Null template - not saved.', /INFO endelse widget_control, tp_list[j].tpid, set_value=tplate, /no_copy endfor close, lu free_lun, lu if count gt 0 then begin sc = strcompress( string( count ) ) message, sc + ' templates saved to file ' + state.tmplfile, /INFO endif endif else begin message, !error_state.msg + string(7B), /INFO endelse widget_control, state.tplistid, set_uvalue=tp_list, /no_copy end ;Open access to the list of templates. state.tplistid : begin widget_control, state.tplistid, get_uvalue=tp_list, /no_copy ;See if a template is selected. if state.selected GE 0 then begin ;Clear the selected status of the template. widget_control, tp_list[state.selected].tlbid, MAP=0 tp_list[state.selected].selected = 0B state.selected = 0 endif ;Map the newly selected template. widget_control, tp_list[event.index].tlbid, /REALIZE, MAP=1 ;Set the edit name text widget to the new name. widget_control, state.tpnameid, set_value=tp_list[event.index].name state.selected = event.index ; Here is where you build an event to pass up the chain. For now, ; only one thing is passed up, just refresh the display. So, the ; content doesn't matter. To make things easy the incoming event is ; sent back out. If something else needs to be done as well then this ; even needs to contain more information. out_event=event tp_list[event.index].selected = 1B widget_control, state.tplistid, set_uvalue=tp_list, /no_copy end state.tpnameid : begin ;New template name. widget_control, state.tplistid, get_uvalue=tp_list, /no_copy if state.selected GE 0 then begin widget_control, event.id, GET_VALUE=text name = STRTRIM( text[0], 2 ) tp_list[state.selected].name = name widget_control, state.tplistid, set_value=tp_list[*].name, $ SET_LIST_SELECT=state.selected widget_control, tp_list[state.selected].tlbid, TLB_SET_TITLE=name endif else begin message, 'Error. ' + $ 'Empty template list, or template not selected.'+bel, /INFO widget_control, event.id, set_value='' endelse widget_control, state.tplistid, set_uvalue=tp_list, /no_copy end else : begin print,'cw_tpmgr_eve: Unrecognized event.' help,event,/st end endcase widget_control, stash, set_uvalue=state, /no_copy return, out_event end ; ------------------------------------------------------------------------------ ; Function cw_tpmgr ; Photometry Template Manager. ; ------------------------------------------------------------------------------ function cw_tpmgr, parent, TMPLFILE=tmplfile, UVALUE=uvalue ;Define the state structure. state = { $ debugid:0L, $ delid:0L, $ dismissid:0L, $ fnselid:0L, $ fntxtid:0L, $ loadid:0L, $ newid:0L, $ nplates:0, $ purgid:0L, $ saveid:0L, $ selected:-1, $ tmplfile:'', $ topbase:parent, $ tplistid:0L, $ tpnameid:0L, $ version:'phottemp_v02' $ } if KEYWORD_SET( tmplfile ) then state.tmplfile=tmplfile ;Define the main base. if KEYWORD_SET( uvalue ) then begin mainbase = widget_base( parent, COLUMN=1, EVENT_FUNC='cw_tpmgr_eve', $ FUNC_GET_VALUE='cw_tpmgr_gvl', pro_set_value='cw_tpmgr_svl', $ UVALUE=uvalue ) endif else begin mainbase = widget_base( parent, COLUMN=1, EVENT_FUNC='cw_tpmgr_eve', $ FUNC_GET_VALUE='cw_tpmgr_gvl', pro_set_value='cw_tpmgr_svl' ) endelse ;Define the button widgets. wb = widget_base( mainbase, ROW=1 ) state.dismissid = widget_button( wb, VALUE='Dismiss' ) state.loadid = widget_button( wb, VALUE='Load' ) state.saveid = widget_button( wb, VALUE='Save' ) state.newid = widget_button( wb, VALUE='Add Template' ) state.delid = widget_button( wb, VALUE='Delete Template' ) state.purgid = widget_button( wb, VALUE='Purge' ) ;Debug. ;state.debugid = widget_button( wb, VALUE='Debug' ) ; Filename widget. wb = widget_base( mainbase, /ROW ) w1 = widget_label( wb, VALUE='Filename:' ) state.fnselid = widget_button( wb, VALUE='Select' ) state.fntxtid = WIDGET_TEXT( wb, VALUE=state.tmplfile, XSIZE=26, /EDITABLE ) ; Templates label. w1 = widget_label(mainbase, VALUE='Template Name List (Click-left to select):') ;Template list widget. tp_list = [{name:'', selected:0B, tlbid:0L, tpid:0L}] state.tplistid = widget_list( mainbase, YSIZE=5, UVALUE=tp_list ) ;Template name edit. state.tpnameid = cw_field( mainbase, $ TITLE='Change name of selected template:', /RETURN_EVENTS ) ;Load photometry templates from a file? if state.tmplfile NE '' then cw_tpmgr_load, state stash = widget_info( mainbase, /CHILD ) widget_control, stash, set_uvalue=state, /no_copy return, mainbase end