source: trunk/src/mouse_up.m @ 61

Last change on this file since 61 was 61, checked in by sommeria, 14 years ago

gestion of projection objects improved. Implementation of view_field.fig
introduction of a circle to mark vectors with the mouse
various cleaning and debugging

File size: 15.6 KB
Line 
1%'mouse_up': function  activated when the mouse button is released
2%----------------------------------------------------------------
3% function mouse_up(ggg,eventdata,handles)
4% activated by the command:
5% set(hObject,'WindowButtonUpFcn',{'mouse_up'}),
6% where hObject is the handle of the figure
7%
8%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
9%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
10%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
11%     This file is part of the toolbox UVMAT.
12%
13%     UVMAT is free software; you can redistribute it and/or modify
14%     it under the terms of the GNU General Public License as published by
15%     the Free Software Foundation; either version 2 of the License, or
16%     (at your option) any later version.
17%
18%     UVMAT is distributed in the hope that it will be useful,
19%     but WITHOUT ANY WARRANTY; without even the implied warranty of
20%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
22%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
23
24function mouse_up(ggg,eventdata,handles)
25MouseAction='none'; %default
26zoomstate=0;%default
27if ~exist('handles','var')
28   handles=get(gcbo,'UserData');
29end
30huvmat=findobj(allchild(0),'Name','uvmat');%find the uvmat interface handle
31if ~isempty(huvmat)
32    hhuvmat=guidata(huvmat);
33    UvData=get(huvmat,'UserData');
34    if isfield(UvData,'MouseAction')
35        MouseAction=UvData.MouseAction;% set the mouse action (edit, create objects...)
36    end
37    zoomstate=get(hhuvmat.zoom,'Value');
38end
39if isequal(MouseAction,'calib') && ~zoomstate
40    return
41end
42currentfig=gcbo;
43AxeData=get(gca,'UserData');
44currentaxes=gca; %store the current axes handle
45test_drawing=0;%default
46
47%finalize the fabrication or the translation/deformation of an object and plot the corresponding projected field
48if ~isempty(huvmat) & isfield(AxeData,'Drawing') & ~isequal(AxeData.Drawing,'off') & isfield(AxeData,'CurrentObject')...
49           & ishandle(AxeData.CurrentObject)
50    xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
51    PlotData=get(AxeData.CurrentObject,'UserData');%get data attached to the current projection object 
52    IndexObj=PlotData.IndexObj;
53    ObjectData=UvData.Object{IndexObj};   
54    if isequal(AxeData.Drawing,'translate')
55        XYData=AxeData.CurrentOrigin;
56        DX=xy(1,1)-XYData(1);%translation from initial position
57        DY=xy(1,2)-XYData(2);
58        ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
59        ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
60    elseif isequal(AxeData.Drawing,'deform')
61        ind_move=AxeData.CurrentIndex;
62        ObjectData.Coord(ind_move,1)=xy(1,1);
63        ObjectData.Coord(ind_move,2)=xy(1,2);
64    else   %creating object
65        if isequal(ObjectData.Style,'line')||isequal(ObjectData.Style,'polyline')||...
66                isequal(ObjectData.Style,'polygon')||isequal(ObjectData.Style,'points')
67            if isfield(AxeData,'ObjectCoord') && size(AxeData.ObjectCoord,2)==3
68              xy(1,3)=AxeData.ObjectCoord(1,3); % z coordinate of the mouse: to generalise ...
69            else
70                 xy(1,3)=0; % z coordinate set to 0 by default
71            end
72            if ~isequal(ObjectData.Coord,xy(1,:))
73                ObjectData.Coord=[ObjectData.Coord ;xy(1,:)];% append the coordiantes marked by the mouse to the eobject
74            end
75        elseif isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')||isequal(ObjectData.Style,'volume')
76            XYData=AxeData.CurrentOrigin;
77            ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
78            ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
79            ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
80            ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
81        elseif isequal(ObjectData.Style,'plane') %case of 'plane'
82            DX=(xy(1,1)-ObjectData.Coord(1,1));
83            DY=(xy(1,2)-ObjectData.Coord(1,2));
84            ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
85            if isfield(ObjectData,'RangeX')
86                XMax=sqrt(DX*DX+DY*DY);
87                if XMax>max(ObjectData.RangeX)
88                    ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
89                end
90            end
91        end
92    end
93    %set(AxeData.CurrentObject,'UserData',ObjectData); %update the object properties
94    if isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')
95        NbDefPoint=1; 
96    elseif isequal(ObjectData.Style,'line')|| isequal(ObjectData.Style,'plane');
97        NbDefPoint=2;
98    else
99         NbDefPoint=3;
100    end
101   
102    %show object coordinates in the GUI set_object
103    h_set_object=findobj(allchild(0),'Name','set_object');
104    h_XObject=findobj(h_set_object,'Tag','XObject');
105    h_YObject=findobj(h_set_object,'Tag','YObject');
106    h_ZObject=findobj(h_set_object,'Tag','ZObject');
107    set(h_XObject,'String',num2str(ObjectData.Coord(:,1),4));
108    set(h_YObject,'String',num2str(ObjectData.Coord(:,2),4));
109    set(h_ZObject,'String',num2str(ObjectData.Coord(:,3),4));
110    if NbDefPoint<=2 || isequal(get(currentfig,'SelectionType'),'alt') ||...
111              isequal(AxeData.Drawing,'translate') || isequal(AxeData.Drawing,'deform');%stop drawing
112        AxeData.CurrentOrigin=[]; %suppress the current origin
113       if isequal(ObjectData.Style,'line') && size(ObjectData.Coord,1)<=1
114           AxeData.Drawing='off';
115           set(currentaxes,'UserData',AxeData);
116            return % line needs at leqst two points
117       end
118       if  ~isempty(ObjectData)
119             testmask=0;
120             hmask=findobj(huvmat,'Tag','makemask');
121             if ~isempty(hmask)
122                testmask=get(hmask,'Value');
123             end
124             if testmask
125                 PlotHandles=[];%do not project data on the object during mask creation
126             else
127                 PlotHandles=get_plot_handles(handles);%get the handles of the graphic objects setting the plotting parameters
128             end
129            AxeData.hset_object=set_object(ObjectData,PlotHandles);% call the set_object interface ,*
130            UvData.Object{IndexObj}=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
131            %ObjectData=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
132            if  isfield(UvData.Object{IndexObj},'PlotParam')
133                write_plot_param(PlotHandles,UvData.Object{IndexObj}.PlotParam); %update the display of plotting parameters for the current object
134            end             
135%             set(hhuvmat.create,'Value',0);% set to 'off' the button for object creation
136%             set(hhuvmat.create,'BackgroundColor',[0 1 0]);% paint the creation button in green
137            set(hhuvmat.edit,'BackgroundColor',[1 1 0]);% paint the edit text in yellow
138            set(hhuvmat.edit,'Value',1);%
139            set(hhuvmat.edit,'Enable','on');%
140            set(hhuvmat.MenuEdit,'Enable','on');%
141            set(hhuvmat.MenuEdit,'Enable','on');%
142            set(hhuvmat.MenuObject,'Enable','on');%
143            UvData.MouseAction='edit_object'; % set the edit button to 'on'
144        end
145    else
146       AxeData.CurrentOrigin=[xy(1,1) xy(1,2)]; %the current point becomes the new current origin
147       test_drawing=1;%allow continuation of drawing object
148       UvData.Object{IndexObj}=ObjectData;
149    end
150    hother=findobj('Tag','deformpoint');%find all the deformpoints
151    set(hother,'Color','b');%reset all the deformpoints in 'blue'
152else
153    test_drawing=0;
154end
155
156%creation of a new zoom plot
157test_replot=0;
158if isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
159%         FigData=get(currentfig,'UserData');
160        hparentfig=currentfig;
161        %open or update a new zoom figure if a rectangle has been drawn
162        if ishandle(currentaxes);
163            if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
164                PosRect=get(AxeData.CurrentRectZoom,'Position');
165                if isfield(AxeData,'CurrentVec') & ishandle(AxeData.CurrentVec)
166                    delete(AxeData.CurrentVec)
167                end
168                hfig2=figure;%create new figure
169                set(hfig2,'name','zoom')
170                set(hfig2,'Units','normalized')
171                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
172                map=colormap(currentaxes);
173                colormap(map);%transmit the current colormap to the zoom fig
174                get(handles.RootFile,'String')
175                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
176                if test_replot==0
177                    set(hfig2,'Unit','normalized')
178                    set(hfig2,'KeyPressFcn',{@keyboard_callback,handles})%set keyboard action function
179                    set(hfig2,'WindowButtonMotionFcn',{@mouse_motion,handles})%set mouse action function
180                    set(hfig2,'WindowButtonDownFcn',{@mouse_down})%set mouse click action function
181                    set(hfig2,'WindowButtonUpFcn',{@mouse_up,handles}) 
182                    set(hfig2,'DeleteFcn',{@close_fig,AxeData.CurrentRectZoom,'zoom'})
183                    set(hfig2,'UserData',AxeData.CurrentRectZoom)% record the parent object (zoom rectangle) in the new fig
184                    AxeData.ZoomAxes=copyobj(currentaxes,hfig2); %copy the current graph axes to the zoom figure
185                    figure(hfig2)
186                    set(AxeData.ZoomAxes,'Position',[0.1300    0.1100    0.7750    0.8150])% standard axes position on a figure
187                    hcol=findobj(hparentfig,'Tag','Colorbar'); %look for colorbar axes
188                    if ~isempty(hcol)             
189                        hcol_new=colorbar;
190                        YTick=get(hcol,'YTick');
191                        YTicklabel=get(hcol,'Yticklabel'); 
192                        colbarlim=get(hcol,'YLim');
193                        newcolbarlim=get(hcol_new,'YLim');
194                        scale_bar=(newcolbarlim(2)-newcolbarlim(1))/(colbarlim(2)-colbarlim(1));               
195                        YTick_rescaled=newcolbarlim(1)+scale_bar*(YTick-colbarlim(1));
196                        set(hcol_new,'YTick',YTick_rescaled);
197                        set(hcol_new,'Yticklabel',YTicklabel);
198                    end
199                end
200                if ishandle(AxeData.ZoomAxes)
201                    hnew_rect=findobj(AxeData.ZoomAxes,'Tag','rect_zoom');
202                    if ~isempty(hnew_rect)
203                        delete(hnew_rect);
204                        ChildAxeData=get(AxeData.ZoomAxes,'UserData');
205                        ChildAxeData.CurrentRectZoom=[]; % no rect zoom in the new window
206                        ChildAxeData.Drawing='off';
207                        ChildAxeData.ParentRect=AxeData.CurrentRectZoom;%set the rectangle as a 'parent' associated to the new axes
208                        set(AxeData.ZoomAxes,'UserData',ChildAxeData);%update the AxeData of the new axes
209                        set(AxeData.ZoomAxes,'Xlim',[PosRect(1) PosRect(1)+PosRect(3)])
210                        set(AxeData.ZoomAxes,'Ylim',[PosRect(2) PosRect(2)+PosRect(4)])
211                    end
212                end
213            end
214        end
215end
216%zoom in if no new figure is created
217if zoomstate
218     if  isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
219        %zoom(2)% zoom in by a factor of 2
220        alpha=0.5; %zoom factor (zoom in by a factor 2)
221        xlim=get(currentaxes,'XLim');
222        xlim_new(1)=(1+alpha)*xlim(1)/2+(1-alpha)*xlim(2)/2;
223        xlim_new(2)=(1-alpha)*xlim(1)/2+(1+alpha)*xlim(2)/2;
224        set(currentaxes,'XLim',xlim_new)
225        ylim=get(currentaxes,'YLim');
226        ylim_new(1)=(1+alpha)*ylim(1)/2+(1-alpha)*ylim(2)/2;
227        ylim_new(2)=(1-alpha)*ylim(1)/2+(1+alpha)*ylim(2)/2;
228        set(currentaxes,'YLim',ylim_new)
229        if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
230            hparentrect=AxeData.ParentRect;
231            xlim=get(currentaxes,'XLim');
232            ylim=get(currentaxes,'YLim');
233            rect([1 2])=[xlim(1) ylim(1)];
234            rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
235            set(hparentrect,'Position',rect)
236        end
237
238     elseif isequal(get(currentfig,'SelectionType'),'alt'); %if right button has been pressed
239            %zoom(0.5)% zoom out by a factor of 2
240            alpha=2; %zoom factor (zoom out by a factor 2)
241            xlim=get(currentaxes,'XLim');
242            xlim_new(1)=(1+alpha)*xlim(1)/2+(1-alpha)*xlim(2)/2;
243            xlim_new(2)=(1-alpha)*xlim(1)/2+(1+alpha)*xlim(2)/2;
244            ylim=get(currentaxes,'YLim');
245            ylim_new(1)=(1+alpha)*ylim(1)/2+(1-alpha)*ylim(2)/2;
246            ylim_new(2)=(1-alpha)*ylim(1)/2+(1+alpha)*ylim(2)/2;
247            set(currentaxes,'XLim',xlim_new)
248            set(currentaxes,'YLim',ylim_new)
249            %test whther zoom out is operating (to inactivate AxedAta
250            if ~isfield(AxeData,'CurrentXLim')| ~isequal(xlim,AxeData.CurrentXLim)
251                AxeData.CurrentXLim=xlim;%
252            end
253            if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
254                hparentrect=AxeData.ParentRect;
255                xlim=get(currentaxes,'XLim');
256                ylim=get(currentaxes,'YLim');
257                rect([1 2])=[xlim(1) ylim(1)];
258                rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
259                set(hparentrect,'Position',rect)
260            end
261      end
262end
263if isequal(MouseAction,'ruler')
264    UvData.MouseAction='none';
265    UvData=rmfield(UvData,'RulerHandle');
266     xy=get(currentaxes,'CurrentPoint');
267    RulerCoord=[UvData.RulerCoord ;xy(1,1:2)];
268    set(huvmat,'UserData',UvData)
269    RulerCoord=diff(RulerCoord,1);
270    RulerCoord=RulerCoord(1)+i*RulerCoord(2);
271    distance=abs(RulerCoord);
272    azimuth=(180/pi)*angle(RulerCoord);
273    msgbox_uvmat('RULER','',['length: ' num2str(distance,3) ',  angle(degrees): ' num2str(azimuth,3)])
274    hruler=findobj(currentaxes,'Tag','ruler');
275    delete(hruler)
276end
277
278
279%display the data of the current object selected with the mouse right click
280if isequal(get(currentfig,'SelectionType'),'alt') && ~zoomstate && (~isfield(AxeData,'Drawing')||~isequal(AxeData.Drawing,'create'))
281    hother=findobj('Tag','proj_object');%find all the proj objects
282    nbselect=0;
283    %test the existence of selected objects:
284    for iproj=1:length(hother);
285        iselect=isequal(get(hother(iproj),'Selected'),'on');%reset all the proj objects in 'blue' by default
286        nbselect=nbselect+iselect;
287    end
288    hother=findobj('Tag','proj_object','Type','line');%find all the proj objects
289    set(hother,'Color','b');%reset all the proj objects in 'blue' by default
290    set(hother,'Selected','off')
291    hother=findobj('Tag','proj_object','Type','rectangle');
292    set(hother,'EdgeColor','b');
293    set(hother,'Selected','off')
294    hother=findobj('Tag','proj_object','Type','patch');
295    set(hother,'FaceColor','b');   
296    if isequal(get(gco,'Type'),'image')
297        currentobj=get(gco,'parent');%parent axes of the image
298    else
299        currentobj=gco;%default
300    end
301    if ((nbselect==0) && isequal(get(currentobj,'Type'),'axes')) || isequal(currentobj,huvmat)
302        global CurData
303        CurData=get(currentobj,'UserData');
304        %plot_text(CurData)
305        %get_field([],CurData);
306        evalin('base','global CurData')%make CurData global in the workspace
307        objtype=get(currentobj,'Type');
308        display(['UserData of ' objtype ':'])
309        evalin('base','CurData') %display CurData in the workspace
310        commandwindow
311    end
312end
313if test_drawing==0
314        AxeData.Drawing='off';%stop current drawing action
315end
316set(currentaxes,'UserData',AxeData);
317if ~isempty(huvmat)
318    set(huvmat,'UserData',UvData);
319end
320
321   
322
Note: See TracBrowser for help on using the repository browser.