source: trunk/Documentation/perldoc_idl/SelectMap__define.pro @ 61

Last change on this file since 61 was 61, checked in by pinsard, 18 years ago

upgrade of Documentation/perldoc_idl according to cerbere.lodyc.jussieu.fr: /usr/home/smasson/SAXO_RD/

  • Property svn:executable set to *
File size: 17.0 KB
Line 
1;+---------------------------------------------------------------------------
2; CLASSNAME: SelectMap
3;
4; AUTHOR:
5;       Antonio Santiago Perez <santiago@grahi.upc.edu>
6;                              (http://asantiago.gentelibre.org)
7;
8; DESCRIPTION:
9;       Show a background image and make line selection. The
10;       coordenates of initial/final points of the line seleccion are
11;       returned normalized respect widget and image size.
12;
13; METHODS:
14;       
15;
16; HISTORY:
17;
18;       Mon Nov 15 17:44:54 2004, Antonio Santiago
19;       <santiago@grahi.upc.edu>
20;
21;----------------------------------------------------------------------------
22PRO SelectMap__define
23   
24    struct = { SelectMap, $
25      INHERITS EventAction, $
26      parent_id: 0L, $          ;Parent widget
27      base_id: 0L, $            ;WIDGET_BASE of the object.
28      widget_id: 0L, $          ;WIDGET_DRAW inside widget_base.
29     
30      oWindow: OBJ_NEW(), $     ;The window
31      oView: OBJ_NEW(), $       ;The view
32      oModel: OBJ_NEW(), $      ;The model
33     
34      oImage: OBJ_NEW(), $      ;Background image
35      oPalette: OBJ_NEW(), $    ;Palette for the image
36      oSelectLine: OBJ_NEW(), $ ;Selection line
37
38      size: [0., 0.], $         ;Widget draw size
39      map_size: [0., 0.], $     ;Image size
40     
41      ;;Line selection
42      line_sel: [[0.,0.], [0.,0.]], $ ;Initial/final poinst of the
43                                ;selected line with coordenates respect
44                                ;widget_draw size.
45      line_map_sel: [[0.,0.], [0.,0.]], $ ;Initial/final points of the
46                                ;selected line with coordenates respect
47                                ;image size.
48      ;;Square selection
49      square_sel: [[0.,0.], [0.,0.], $ ;Initial/final poinst of the
50                   [0.,0.], [0.,0.]], $ ;selected square with
51                                ;coordenates respect
52                                ;widget_draw size.
53      square_map_sel: [[0.,0.], [0.,0.], $ ;Initial/final points of the
54                       [0.,0.], [0.,0.]], $ ;selected square with
55                                ;coordenates respect
56                                ;image size.
57
58      selectionType: '', $      ;Specifies the selection type: LINE/SQUARE
59      bSelecting: 0B, $         ;Are we making a selection?
60      bHideSelection: 0B, $     ;Is visible the selection line?
61      bDynamicSelection: 0B $   ;Emit SELECTION signals when we are
62                                ;making the selection?
63      }
64END
65
66
67;+---------------------------------------------------------------------------
68; NAME: SelectMap::Init
69;
70; DESCRIPTION:
71;       Creates an instance of 'SelectMap' class.
72;
73; PARAMETERS:
74;       parent - Parents widget ID.
75;
76; EXTRA PARAMETERS:
77;       extra - Extra KEYWORD with options to be passed to the
78;               widget_draw.
79;
80;----------------------------------------------------------------------------
81FUNCTION SelectMap::Init, parent, _EXTRA=extra
82
83    ;;Initilize superclass
84    result = self->EventAction::Init()
85
86    IF NOT KEYWORD_SET(parent) THEN RETURN, 0
87
88    ;;Create widget and object hieriarchy.
89    wBase = WIDGET_BASE(parent)
90    wDraw = WIDGET_DRAW(wBase, GRAPHICS_LEVEL=2, /EXPOSE_EVENTS, $
91                        /BUTTON_EVENTS, /MOTION_EVENTS, _EXTRA=extra, $
92                        UVALUE={class_name: 'SelectMap', object: self, $
93                                handler: 'EventHandler'})
94   
95    self.parent_id = parent
96    self.base_id = wBase
97    self.widget_id = wDraw
98
99    ;;Register the 'event' of the class
100    self->AddEvent, 'SELECTION'
101    self->AddEvent, 'MOTION'
102   
103    self->Initialize
104   
105    RETURN, 1
106END
107
108
109;+---------------------------------------------------------------------------
110; NAME: SelectMap::Initialize
111;
112; DESCRIPTION:
113;       Initialize the state of the object. If it is executed when the
114;       object is running the object's state goes back to the same as
115;       the creation state. All possible used memory are free.
116;
117;----------------------------------------------------------------------------
118PRO SelectMap::Initialize
119
120    ;;Free memory usage
121    self->Clear
122
123    ;;Realize the WIDGET_BASE and get the IDLgrWindow object from,
124    ;;WIDGET_DRAW
125    WIDGET_CONTROL, self.base_id , /REALIZE
126    WIDGET_CONTROL, self.widget_id, GET_VALUE=oWindow   
127    oWindow->SetProperty, QUALITY=0
128   
129    oView = OBJ_NEW('IDLgrView', ZCLIP=[0.2,-0.2], $
130      VIEWPLANE_RECT=[0,0,1,1], COLOR=[50,50,70])               
131    oModel = OBJ_NEW('IDLgrModel')     
132    oLight = OBJ_NEW('IDLgrLight', TYPE=1, LOCATION=[0,0,12], $
133      INTENSITY=0.95)
134
135    ;;Initialize attributes
136    geom = WIDGET_INFO(self.widget_id, /GEOMETRY)
137    self.size = [geom.xsize, geom.ysize]
138    self.map_size = [0., 0.]
139    self.line_sel = [[0.,0.], [0.,0.]]
140    self.line_map_sel = [[0.,0.], [0.,0.]]
141    self.selectionType = 'SQUARE'
142    self.bHideSelection = 0     ;Hide selection
143    self.bDynamicSelection = 0  ;No dynamic selection
144    self.bSelecting = 0
145
146    ;;Save references to the objects
147    oView->Add, oModel 
148    oModel->Add, oLight
149
150    self.oWindow = oWindow
151    self.oView = oView
152    self.oModel = oModel
153   
154END     
155
156
157;+---------------------------------------------------------------------------
158; NAME: SelectMap::Cleanup
159;
160; DESCRIPTION:
161;       Free memory used by the object.
162;
163;----------------------------------------------------------------------------
164PRO SelectMap::Cleanup
165
166    OBJ_DESTROY, [self.oWindow]
167   
168    self->Clear
169
170    self->EventAction::Cleanup
171END
172
173
174;+---------------------------------------------------------------------------
175; NAME: SelectMap::Clear
176;
177; DESCRIPTION:
178;       Free memory used by the object.
179;
180;----------------------------------------------------------------------------
181PRO SelectMap::Clear
182    ;;When destroy de IDLgrView the IDLgrModel are destroyed with all
183    ;;its contents, included the image and seleccion line.
184    OBJ_DESTROY, [self.oView, self.oPalette, self.oSelectLine]
185END     
186
187
188;+---------------------------------------------------------------------------
189; NAME: SelectMap::SetPalette
190;
191; DESCRIPTION:
192;       Sets a color palette used to show bachground image. Only takes
193;       efect if the background image is a X,Y array.
194;
195; PARAMETERS:
196;       colors - 256x3 array values (RGB).
197;
198;----------------------------------------------------------------------------
199PRO SelectMap::SetPalette, colors
200   
201    ;;Destroy the last palette (if exists)
202    OBJ_DESTROY, self.oPalette
203   
204    oPalette = OBJ_NEW('IDLgrPalette', colors[*,0], colors[*,1], colors[*,2])
205   
206    ;;Save reference to the palette.
207    self.oPalette = oPalette
208END
209
210
211;+---------------------------------------------------------------------------
212; NAME: SelectMap::SetMap
213;
214; DESCRIPTION:
215;       Sets a background image. The image can be a (x,y) array that
216;       will be colored with the specified palette or may be a
217;       (3,x,y), (x,3,y) or (x,y,3) array (like an 'jpeg' image).
218;
219; PARAMETERS:
220;       data_map - Array 2D or 3D with the image to show.
221;
222;----------------------------------------------------------------------------
223PRO SelectMap::SetMap, data_map
224   
225    ;;Destroy previeus objects.
226    OBJ_DESTROY, [self.oImage, self.oSelectLine]
227   
228    ;;Get dimension of the image and calcula the normalization factors
229    ;;depending on the type of the image.
230    sizes = SIZE(data_map, /DIMENSIONS)
231
232    IF N_ELEMENTS(sizes) EQ 3 THEN BEGIN
233        ;;Image 3,X,Y
234        IF sizes[0] LE 4 THEN BEGIN
235            xsize = sizes[1]
236            ysize = sizes[2]
237            xconv = NORMALIZE([0, xsize-1], Position=[0,1])
238            yconv = NORMALIZE([0, ysize-1], Position=[0,1])
239        ENDIF
240        ;;Image X,3,Y
241        IF sizes[1] LE 4 THEN BEGIN
242            xsize = sizes[0]
243            ysize = sizes[2]
244            xconv = NORMALIZE([0, xsize-1], Position=[0,1])
245            yconv = NORMALIZE([0, ysize-1], Position=[0,1])
246        ENDIF
247        ;;Image X,Y,3
248        IF sizes[2] LE 4 THEN BEGIN
249            xsize = sizes[0]
250            ysize = sizes[1]
251            xconv = NORMALIZE([0, xsize-1], Position=[0,1])
252            yconv = NORMALIZE([0, ysize-1], Position=[0,1])
253        ENDIF
254    ENDIF ELSE BEGIN
255        ;;Image X,Y
256        xsize = sizes[0]
257        ysize = sizes[1]
258        xconv = NORMALIZE([0, xsize-1], Position=[0,1])
259        yconv = NORMALIZE([0, ysize-1], Position=[0,1])
260    ENDELSE
261
262    ;;Save the dimensions of the image.
263    self.map_size = [xsize, ysize]
264
265    ;;Create the image as a texture of polygon.
266    oImage = OBJ_NEW('IDLgrImage', data_map, PALETTE=self.oPalette, $
267      XCOORD_CONV=xconv, YCOORD_CONV=yconv)
268
269    ;;Create the selection line object
270    IF self.selectionType EQ 'LINE' THEN BEGIN
271        connect_line = [2, 0, 1]
272        oSelectLine = OBJ_NEW('IDLgrPolyline', self.line_map_sel, $
273                              POLYLINES=connect_line, $
274                              COLOR=[150,150,150], THICK=2, $
275                              HIDE=self.bHideSelection, $
276                              XCOORD_CONV=xconv, YCOORD_CONV=yconv)       
277    ENDIF ELSE BEGIN
278        connect_line = [4, 0, 1, 2, 3]
279        oSelectLine = OBJ_NEW('IDLgrPolyline', self.square_map_sel, $
280                              POLYLINES=connect_line, $
281                              COLOR=[150,150,150], THICK=2, $
282                              HIDE=self.bHideSelection, $
283                              XCOORD_CONV=xconv, YCOORD_CONV=yconv)       
284    ENDELSE
285
286    ;;Save object references
287    self.oModel->Add, oImage
288    self.oModel->Add, oSelectLine
289    self.oImage = oImage
290    self.oSelectLine = oSelectLine
291
292END
293
294
295;+---------------------------------------------------------------------------
296; NAME: SelectMap::Draw
297;
298; DESCRIPTION:
299;         Draws the object.
300;
301;----------------------------------------------------------------------------
302PRO SelectMap::Draw
303    self.oWindow->Draw, self.oView
304END
305
306
307;+---------------------------------------------------------------------------
308; NAME: SelectMap::GetProperty
309;
310; DESCRIPTION:
311;       Get the the properties of the object.
312;
313; KEYWORD PARAMETERS:
314;      id - ID of the WIDGET_BASE over the object are created.
315;
316;----------------------------------------------------------------------------
317 PRO SelectMap::GetProperty, ID=id
318
319     id = self.base_id
320 END   
321
322
323;+---------------------------------------------------------------------------
324; NAME: SelectMap::SetProperty
325;
326; DESCRIPTION:
327;       Sets the properties of the object.
328;
329; KEYWORD PARAMETERS:
330;       hide_selection - Show/hide the selection line.
331;       dynamic_selection - If true, SELECTION events are emited while a
332;                           selecction is doing.
333;
334;----------------------------------------------------------------------------
335PRO SelectMap::SetProperty, $
336  SELECTION_TYPE=selection_type, $
337  DYNAMIC_SELECTION=dynamic_selection, $
338  HIDE_SELECTION=hide_selection
339       
340    IF selection_type EQ 'LINE' OR selection_type EQ 'SQUARE' THEN BEGIN
341        self.selectionType = selection_type
342    ENDIF
343
344    IF N_ELEMENTS(dynamic_selection) THEN BEGIN
345        self.bDynamicSelection = dynamic_selection
346    ENDIF
347
348    IF N_ELEMENTS(hide_selection) THEN BEGIN
349        self.bHideSelection = hide_selection
350        self.oSelectLine->SetProperty, HIDE=hide_selection
351    ENDIF
352   
353 END   
354
355
356;+---------------------------------------------------------------------------
357; NAME: SelectMap_EventHandler
358;
359; DESCRIPTION:
360;       Object's event handler.
361;       
362;----------------------------------------------------------------------------
363PRO SelectMap::EventHandler, event
364
365    ;;Expose Event
366    IF event.type EQ 4 THEN BEGIN
367        self->Draw
368    ENDIF
369   
370    ;;Mouse down button
371    IF (event.type EQ 0) AND (event.press EQ 1) THEN BEGIN
372        IF NOT self.bHideSelection THEN BEGIN
373            ;;Set that a selection is doing
374            self.bSelecting = 1
375
376            ;;Update the line coordenates
377            self->UpdateLineSelection, event, /ALL
378            self->UpdateSquareSelection, event, /ALL
379            IF self.selectionType EQ 'LINE' THEN BEGIN
380                connect_line = [2, 0, 1]
381                self.oSelectLine->SetProperty, DATA=self.line_map_sel, $
382                  COLOR=[255,255,0], POLYLINE=connect_line
383            ENDIF ELSE BEGIN
384                connect_line = [4, 0, 1, 2, 3]
385                self.oSelectLine->SetProperty, DATA=self.square_map_sel, $
386                  COLOR=[255,255,0], POLYLINE=connect_line           
387            ENDELSE
388        ENDIF     
389           
390    ENDIF
391   
392    ;;Mouse up button
393    IF (event.type EQ 1)  AND (event.release EQ 1)THEN BEGIN
394
395        ;;Set that the selection are finished
396        self.bSelecting = 0
397
398        ;;Update the line corrdenates.
399        self->UpdateLineSelection, event
400        self->UpdateSquareSelection, event
401            IF self.selectionType EQ 'LINE' THEN BEGIN
402                self.oSelectLine->SetProperty, DATA=self.line_map_sel, $
403                  COLOR=[255,255,255]
404            ENDIF ELSE BEGIN
405                self.oSelectLine->SetProperty, DATA=self.square_map_sel, $
406                  COLOR=[255,255,255] 
407            ENDELSE
408
409        ;;Generate a 'SELECTION' event.
410        self->Draw
411        self->EmitEvent, 'SELECTION', $
412          EVENT_STRUCT={line_sel: self.line_sel, $
413                        line_map_sel: self.line_map_sel}
414    ENDIF
415   
416    ;;Mouse move
417    IF (event.type EQ 2)  THEN BEGIN
418        ;;If a selection is doing actualize the coordenates.
419        IF self.bSelecting THEN BEGIN         
420            self->UpdateLineSelection, event           
421            self->UpdateSquareSelection, event
422            IF self.selectionType EQ 'LINE' THEN BEGIN
423                self.oSelectLine->SetProperty, DATA=self.line_map_sel
424            ENDIF ELSE BEGIN
425                self.oSelectLine->SetProperty, DATA=self.square_map_sel   
426            ENDELSE
427           
428            self->Draw
429        ENDIF
430
431        ;;Generate a 'MOTION' event.
432        values = [event.x, event.y]
433        convert = self->ConvertValuesW2M(values)
434        self->EmitEvent, 'MOTION', $
435          EVENT_STRUCT={ point_sel: values, $
436                         point_map_sel: convert }
437    ENDIF
438   
439END
440
441
442;+---------------------------------------------------------------------------
443; NAME: SelectMap::UpdateLineSelection
444;
445; DESCRIPTION:
446;       Get the actual coordenate points and convert respect the image
447;       size.
448;       
449; KEYWORD PARAMETERS:
450;       all - If set specify that the two points (origin, final) of
451;             the selection line must to be initialize. If not set,
452;             only the final point is converted.
453;
454; NOTE: The 0 and 2 points of the square are the initial and final
455;       points respectively.
456;
457;----------------------------------------------------------------------------
458PRO SelectMap::UpdateLineSelection, event, ALL=all
459
460    xpoint = (event.x > 0) < (self.size[0]-1)
461    ypoint = (event.y > 0) < (self.size[1]-1)
462   
463    IF KEYWORD_SET(all) THEN BEGIN
464        self.line_sel[*,0] = [xpoint, ypoint]   
465        self.line_map_sel[*,0] = self->ConvertValuesW2M(self.line_sel[*,0])
466    ENDIF
467
468    self.line_sel[*,1] = [xpoint, ypoint]
469    self.line_map_sel[*,1] = self->ConvertValuesW2M(self.line_sel[*,1])
470END
471
472
473;+---------------------------------------------------------------------------
474; NAME: SelectMap::UpdateSquareSelection
475;
476; DESCRIPTION:
477;       Get the actual coordenate points and convert respect the image
478;       size.
479;       
480; KEYWORD PARAMETERS:
481;       all - If set specify that the two points (origin, final) of
482;             the selection line must to be initialize. If not set,
483;             only the final point is converted.
484;
485; NOTE: The 0 and 2 points of the square are the initial and final
486;       points respectively.
487;
488;----------------------------------------------------------------------------
489PRO SelectMap::UpdateSquareSelection, event, ALL=all
490
491    xpoint = (event.x > 0) < (self.size[0]-1)
492    ypoint = (event.y > 0) < (self.size[1]-1)
493   
494    IF KEYWORD_SET(all) THEN BEGIN
495        ;;Initial point
496        self.square_sel[*,0] = [xpoint, ypoint]
497        self.square_map_sel[*,0] = self->ConvertValuesW2M([xpoint, ypoint])
498    ENDIF
499
500    ;;Final point
501    self.square_sel[*,2] = [xpoint, ypoint]
502    self.square_map_sel[*,2] = self->ConvertValuesW2M([xpoint, ypoint])
503
504    ;;Other points
505    xinit = self.square_sel[0,0]
506    yinit = self.square_sel[1,0]
507    width = xinit - xpoint
508    height = yinit - ypoint
509
510    self.square_sel[*,1] = [(xinit + width), ypoint]
511    self.square_map_sel[*,1] = self->ConvertValuesW2M([xpoint, ypoint])
512   
513    self.square_sel[*,3] = [xpoint, (ypoint + height)]
514    self.square_map_sel[*,3] = self->ConvertValuesW2M([xpoint, ypoint])
515
516
517END
518
519
520;+---------------------------------------------------------------------------
521; NAME: SelectMap::ConvertValuesW2M
522;
523; DESCRIPTION:
524;       Convert a pair of values from widget size relative coordenatos
525;       to the image relativa size coordenates.
526;       
527;----------------------------------------------------------------------------
528FUNCTION SelectMap::ConvertValuesW2M, values
529
530    values[0] = (values[0] > 0) < (self.size[0]-1)
531    values[1] = (values[1] > 0) < (self.size[1]-1)
532
533    convert = values
534    convert[0] = values[0] * (self.map_size[0]-1) / (self.size[0]-1)
535    convert[1] = values[1] * (self.map_size[1]-1) / (self.size[1]-1)
536   
537    RETURN, convert
538END
539
540
541
Note: See TracBrowser for help on using the repository browser.