Re: widget [message #77814 is a reply to message #22367] |
Fri, 07 October 2011 12:47   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 10/7/11 1:11 PM, David Fanning wrote:
> Marco Otto writes:
>
>> I am new to widget programming in IDL :-(
>>
>> maybe someone can tell me how to realise the second step:
>>
>> 1. read a .sav file
>> 2. if there are more than one variable stored then give me a list of
>> variables (here comes the widget - hence the hard part for me) where I
>> can interactively select the variable I want to be read into IDL
>> 3. rename the variable
>>
>> That's all! But somehow I don't get it - seems a bit over complicated
>> in IDL - but the problem is definitely sitting in front of the
>> screen ;-)
>>
>> Here is what I have so far
>
> Oh, dear! I'm off to Australia in a few hours, so I
> won't be able to help. But, I will tell you that
> nearly everything about this program is wrong. :-)
>
> Ben Tupper, or someone else, may take pity on you. Or,
> I'll give you some help when I get back. But, just
> briefly, I think you want to use the IDL_Savefile
> object to query your save file and get the variable
> names, and I *think* (can't really tell for sure)
> you want to build a pop-up dialog widget:
>
> http://www.idlcoyote.com/widget_tips/popup.html
Try the program listed below; it's not hard to write, but the
explanation would be (MUCH) longer than the program. For a full
explanation, I would see one of the fine books available to learn IDL
(http://www.ittvis.com/language/en-US/Support/IDLBooks.aspx). To try it, do:
IDL> a = 1
IDL> b = 2
IDL> c = 3
IDL> save, a, b, c, filename='test.sav'
IDL> mg_sav_selection, 'test.sav'
The user interface is pretty simple: change the test in the text box at
the bottom to the name you want to call the variable and then select the
variable you want to import from the list.
pro mg_sav_selection_event, event
compile_opt strictarr
on_error, 2
widget_control, event.top, get_uvalue=pstate
uname = widget_info(event.id, /uname)
case uname of
'var_list': begin
varname_text = widget_info(event.top, find_by_uname='varname_text')
widget_control, varname_text, get_value=varname
(*pstate).s->restore, ((*pstate).varnames)[event.index]
(scope_varfetch(varname, /enter, level=1)) $
= scope_varfetch(((*pstate).varnames)[event.index])
end
'varname_text': ; no need to do anything
else: message, 'unknown widget event'
endcase
end
pro mg_sav_selection_cleanup, tlb
compile_opt strictarr
widget_control, tlb, get_uvalue=pstate
obj_destroy, (*pstate).s
ptr_free, pstate
end
pro mg_sav_selection, sav_filename
compile_opt strictarr
s = obj_new('IDL_Savefile', sav_filename)
varnames = s->names(count=nvars)
if (nvars eq 0L) then return
tlb = widget_base(/column, title='Select variables from ' + sav_filename)
var_list = widget_list(tlb, value=varnames, xsize=20, ysize=10, $
uname='var_list')
varname_text = widget_text(tlb, value='varname', xsize=20, /editable, $
uname='varname_text')
widget_control, tlb, /realize
state = { s: s, varnames: varnames }
pstate = ptr_new(state, /no_copy)
widget_control, tlb, set_uvalue=pstate
xmanager, 'mg_sav_selection', tlb, /no_block, $
event_handler='mg_sav_selection_event', $
cleanup='mg_sav_selection_cleanup'
end
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL, A Guide to Learning IDL: http://modernidl.idldev.com
Research Mathematician
Tech-X Corporation
|
|
|