source: trunk/src/mouse_up.m @ 700

Last change on this file since 700 was 699, checked in by sommeria, 10 years ago

mouse indication improved

File size: 19.7 KB
Line 
1%'mouse_up': function  activated when the mouse button is released
2%------------------------------------------------------------------------
3% function mouse_up(hObject,eventdata,handles)
4% activated by the command:
5% set(hObject,'WindowButtonUpFcn',{'mouse_up'}),
6% where hObject is the handle of the figure
7
8%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
9%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
10%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
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%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
23
24function mouse_up(hObject,eventdata,handles)
25
26test_ruler=0;%default
27hcurrentaxes=get(hObject,'CurrentAxes');
28if isempty(hcurrentaxes)
29    return % no axes in the current figure
30end
31AxeData=get(hcurrentaxes,'UserData');
32if isfield(AxeData,'ParentAxes')% case of a zoom plot as current axis
33    hcurrentaxes=AxeData.ParentAxes;
34    AxeData=get(hcurrentaxes,'UserData');
35    hcurrentfig=get(hcurrentaxes,'parent');%handles of the GUI parent of the zoom plot
36    testsubplot=1;% mouse selection is on a zoom subplot
37else
38    hcurrentfig=hObject;
39    testsubplot=0;
40end
41%set(get(hcurrentfig,'CurrentObject'),'Selected','off')
42
43CurrentOrigin=[];
44if isfield(AxeData,'CurrentOrigin')
45    CurrentOrigin=AxeData.CurrentOrigin;
46end
47FigTag=get(hcurrentfig,'tag');
48hhcurrentfig=guidata(hcurrentfig);%the current figure is a GUI (uvmat or view_field)
49CheckZoom=get(hhcurrentfig.CheckZoom,'Value');
50CheckZoomFig=get(hhcurrentfig.CheckZoomFig,'Value');%exclusive to CheckZoom
51huvmat=findobj(allchild(0),'tag','uvmat');%find the uvmat interface handle
52if ~isempty(huvmat)
53    hhuvmat=guidata(huvmat);
54    UvData=get(huvmat,'UserData');
55   test_ruler=~CheckZoom && isequal(get(hhuvmat.MenuRuler,'checked'),'on');%test for ruler  action, second priority
56end
57test_drawing=0;%default, =1 to allow drawing by further mouse action
58if ~(isfield(AxeData,'Enable')&& strcmp(AxeData.Enable,'on'))
59    return
60end
61
62xy=get(hcurrentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
63
64
65%% proceed with the creation or editing (translation/deformation) of an object
66if ~isempty(huvmat) && isfield(AxeData,'Drawing') && ~isequal(AxeData.Drawing,'off') && isfield(AxeData,'CurrentObject')...
67        && ~isempty(AxeData.CurrentObject) && ishandle(AxeData.CurrentObject)
68    set(AxeData.CurrentObject,'Selected','off')
69    PlotData=get(AxeData.CurrentObject,'UserData');%get data attached to the current projection object
70    IndexObj=PlotData.IndexObj;
71    ObjectData=UvData.ProjObject{IndexObj};
72    check_multiple=0;
73   
74    h_set_object=findobj(allchild(0),'Tag','set_object');
75    hh_set_object=guidata(h_set_object);
76    ObjectData.Coord=get(hh_set_object.Coord,'Data');
77   
78    % ending translation
79    if isequal(AxeData.Drawing,'translate')
80        XYData=AxeData.CurrentOrigin;
81        DX=xy(1,1)-XYData(1);%translation from initial position
82        DY=xy(1,2)-XYData(2);
83        ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
84        ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
85        set(hh_set_object.Coord,'Data',ObjectData.Coord);
86        %ending object deformation
87    elseif isequal(AxeData.Drawing,'deform')
88        ind_move=AxeData.CurrentIndex;
89        ObjectData.Coord(ind_move,1)=xy(1,1);
90        ObjectData.Coord(ind_move,2)=xy(1,2);
91        set(hh_set_object.Coord,'Data',ObjectData.Coord);
92        %creating object
93    else
94        switch ObjectData.Type
95            case {'line'}
96                if size(ObjectData.Coord,1)==1 % this is the mouse up for the first point, continue until next click
97                    check_multiple=1;
98                else
99                    %ObjectData.Coord=[ObjectData.Coord ;CurrentOrigin];% append the second point of the line (the last pointed position during mouse down)
100                end
101            case {'rectangle','ellipse','volume'}
102                %                  if size(ObjectData.Coord,1)==1 % this is the mouse up for the first point, continue until next click
103                %                     check_multiple=1;
104                %                  else
105                ObjectData.Coord=(CurrentOrigin+xy(1,1:2))/2;% keep only the first point coordinate
106                ObjectData.RangeX=abs(ObjectData.Coord(1,1)-xy(1,1));%rectangle width
107                ObjectData.RangeY=abs(ObjectData.Coord(1,2)-xy(1,2));%rectangle height
108                if isequal(ObjectData.RangeX,0)||isequal(ObjectData.RangeY,0)
109                    check_multiple=1;% pass to next mous up if width of height=0
110                end
111                %                 ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
112                %                 ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
113                %                 ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
114                %                 ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
115                %                  end
116            case 'plane' %case of 'plane'
117                DX=(xy(1,1)-ObjectData.Coord(1,1));
118                DY=(xy(1,2)-ObjectData.Coord(1,2));
119                ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle width
120                if isfield(ObjectData,'RangeX')
121                    XMax=sqrt(DX*DX+DY*DY);
122                    if XMax>max(ObjectData.RangeX)
123                        ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
124                    end
125                end
126            otherwise
127                check_multiple=1;
128        end
129    end
130   
131    %show object coordinates in the GUI set_object
132    %     h_set_object=findobj(allchild(0),'Tag','set_object');
133    %     hh_set_object=guidata(h_set_object);
134    %     set(hh_set_object.Coord,'Data',ObjectData.Coord);
135    if strcmp(ObjectData.Type,'rectangle')||strcmp(ObjectData.Type,'ellipse')
136        set(hh_set_object.num_RangeX_2,'String',num2str(ObjectData.RangeX,4));
137        set(hh_set_object.num_RangeY_2,'String',num2str(ObjectData.RangeY,4));
138    end
139   
140    %% stop drawing and plot the projected field if the object manipulation is finished
141    if check_multiple==0  || isequal(get(hcurrentfig,'SelectionType'),'alt')
142        AxeData.CurrentOrigin=[]; %suppress the current origin
143        hobject=UvData.ProjObject{IndexObj}.DisplayHandle.(FigTag);
144        if ~isempty(hObject)
145            ProjObject=UvData.ProjObject{get(hhuvmat.ListObject_1,'Value')};
146            AxeData.CurrentObject=plot_object(ObjectData,ProjObject,hobject,'m');%draw the object and its handle becomes AxeData.CurrentObject
147        end
148        %%
149        if  ~isempty(ObjectData)
150            % plot the field projected on the object
151            ProjData= proj_field(UvData.Field,ObjectData);%project the current interface field on ObjectData
152            if ~isempty(ProjData)
153                if strcmp(FigTag,'uvmat')% uvmat plot selected, projection plot seen in view_field
154                    hview_field=findobj(allchild(0),'tag','view_field');
155                    if isempty(hview_field)
156                        hview_field=view_field(ProjData); %open the view_field GUI for plot
157                    else
158                        hhview_field=guidata(hview_field);
159                        [PlotType,PlotParam]=plot_field(ProjData,hhview_field.PlotAxes,read_GUI(hview_field));%update an existing  plot in view_field
160                        errormsg=fill_GUI(PlotParam,hview_field);
161                    end
162                    ViewFieldData=get(hview_field,'UserData');
163                    haxes=findobj(hview_field,'tag','axes3');
164                    if strcmp(get(haxes,'Visible'),'off')%sempty(PlotParam.Coordinates)% case of no plot display (pure text table)
165                        h_TableDisplay=findobj(hview_field,'tag','TableDisplay');
166                        pos_table=get(h_TableDisplay,'Position');
167                        pos=get(hview_field,'Position');
168                        set(hview_field,'Position',[pos(1)+pos(3)-pos_table(3) pos(2)+pos(4)-pos_table(4) pos_table(3) pos_table(4)])
169                        drawnow
170                        set(hview_field,'UserData',ViewFieldData);% restore the previously stored GUI position after GUI resizing
171                    elseif isfield(ViewFieldData,'GUISize')
172                        set(hview_field,'Position',ViewFieldData.GUISize)
173                    end
174                else
175                    UvData.PlotAxes=ProjData;
176                    [PlotType,PlotParam]=plot_field(ProjData,hhuvmat.PlotAxes,read_GUI(huvmat));%update an existing field plot
177                    errormsg=fill_GUI(PlotParam,huvmat);
178                end
179            end
180            set(hhuvmat.CheckViewField,'Value',1);%
181            set(hhuvmat.CheckEditObject,'Value',1);%           
182            set(hhuvmat.CheckEditObject,'Enable','on');%
183            set(get(h_set_object,'children'),'Enable','on')
184        end
185        UvData.ProjObject{IndexObj}=ObjectData;
186        if isfield(UvData.ProjObject{IndexObj},'CreateMode')
187            UvData.ProjObject{IndexObj}=rmfield(UvData.ProjObject{IndexObj},'CreateMode');%remove createMode to mark the object as finished
188        end
189    else
190        test_drawing=1;%allow continuation of drawing object
191        AxeData.CurrentOrigin=[xy(1,1) xy(1,2)]; %the current point becomes the next current origin
192    end
193    %     UvData.ProjObject{IndexObj}=ObjectData;
194    hother=findobj('Tag','deformpoint');%find all the deformpoints
195    set(hother,'Color','b');%reset all the deformpoints in 'blue'
196end
197
198%% creation or update of a  zoom sub-plot
199if CheckZoomFig && isequal(get(hcurrentfig,'SelectionType'),'normal')&&...%if left button has been pressed
200     ~isempty(CurrentOrigin) && ~isequal(CurrentOrigin(1),xy(1,1)) && ~isequal(CurrentOrigin(2),xy(1,2))%if mouse moved in x and y since presed down
201    hparentfig=hcurrentfig;
202    %open or update a new zoom figure if a rectangle has been drawn
203    if ishandle(hcurrentaxes);
204        if isfield(AxeData,'CurrentRectZoom') && ~isempty(AxeData.CurrentRectZoom) && ishandle(AxeData.CurrentRectZoom)
205            %PosRect=get(AxeData.CurrentRectZoom,'Position');
206            if isfield(AxeData,'CurrentVec') && ~isempty(AxeData.CurrentVec) && ishandle(AxeData.CurrentVec)
207                delete(AxeData.CurrentVec)
208            end
209            if ~testsubplot% if we are not already on a zoom plot
210                hfig2=findobj(allchild(0),'Tag','zoom_fig');
211                if isempty(hfig2)% create zoom sub plot if absent
212                    hfig2=figure('name',['zoom_' FigTag],'tag','zoom_fig');%create new figure (unit='pixels' by default)
213                    set(0,'Unit','pixels')
214                    FigPos=get(hfig2,'Position');%get the standard width and height of the fig
215                    ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
216                    Left=ScreenSize(3)- FigPos(3)-40; %right edge close to the right, with margin=40
217                    Bottom=ScreenSize(4)-FigPos(4)-40; %put fig at top right
218                    FigPos(1:2)=[Left Bottom];
219                    set(hfig2,'Position',FigPos);% put the zoom fig close to the upper right of the screen
220                    map=colormap(hcurrentaxes);
221                    colormap(map);%transmit the current colormap to the zoom fig
222                    set(hfig2,'KeyPressFcn',{@keyboard_callback,handles})%set keyboard action function
223                    set(hfig2,'WindowButtonMotionFcn',{@mouse_motion,handles})%set mouse action function
224                    set(hfig2,'WindowButtonDownFcn',{@mouse_down})%set mouse click action function
225                    set(hfig2,'WindowButtonUpFcn',{@mouse_up,handles})
226                else
227                    zoom_axes=findobj(hfig2,'Type','axes');%delete existing axes
228                    axes(zoom_axes);%make the zoom axes apparent
229                    delete(zoom_axes)
230                end
231                set(hfig2,'DeleteFcn',{@close_fig,AxeData.CurrentRectZoom})
232                set(hfig2,'UserData',AxeData.CurrentRectZoom)% record the parent object (zoom rectangle) in the new fig   
233                AxeData.ZoomAxes=copyobj(hcurrentaxes,hfig2); %copy the current graph axes to the zoom figure
234                hrect_zoom=findobj(AxeData.ZoomAxes,'Tag','rect_zoom');%find and delete the copy of the rect_zoom rectangle
235                delete(hrect_zoom)
236                hcol=findobj(hparentfig,'Tag','Colorbar'); %look for colorbar axes
237                if ~isempty(hcol)
238                    hcol_new=colorbar;
239                    YTick=get(hcol,'YTick');
240                    YTicklabel=get(hcol,'Yticklabel');
241                    colbarlim=get(hcol,'YLim');
242                    newcolbarlim=get(hcol_new,'YLim');
243                    scale_bar=(newcolbarlim(2)-newcolbarlim(1))/(colbarlim(2)-colbarlim(1));
244                    YTick_rescaled=newcolbarlim(1)+scale_bar*(YTick-colbarlim(1));
245                    set(hcol_new,'YTick',YTick_rescaled);
246                    set(hcol_new,'Yticklabel',YTicklabel);
247                end
248                ChildAxeData=get(AxeData.ZoomAxes,'UserData');
249            end 
250            ChildAxeData.CurrentOrigin=[];% forget the current origin
251            ChildAxeData.CurrentRectZoom=[]; % no rect zoom in the new window
252            ChildAxeData.Drawing='off';
253            ChildAxeData.ParentAxes=hcurrentaxes;
254            ChildAxeData.ParentRect=AxeData.CurrentRectZoom;%set the rectangle drawing as a 'parent' associated to the new axe
255            if xy(1,1)>CurrentOrigin(1)
256            set(AxeData.ZoomAxes,'Xlim',[CurrentOrigin(1) xy(1,1)])
257            else
258                set(AxeData.ZoomAxes,'Xlim',[xy(1,1) CurrentOrigin(1)])
259            end
260            if xy(1,2)>CurrentOrigin(2)
261            set(AxeData.ZoomAxes,'Ylim',[CurrentOrigin(2) xy(1,2)])
262            else
263                set(AxeData.ZoomAxes,'Ylim',[xy(1,2) CurrentOrigin(2)])
264            end
265            set(AxeData.ZoomAxes,'UserData',ChildAxeData);%update the AxeData of the new axes
266        end
267    end
268end
269
270%% zoom in or out by a factor 2 if no new figure is created
271if CheckZoom
272    if testsubplot
273        haxes=gca;% zoom on a zoom sub-plot
274    else
275        haxes=hcurrentaxes;% zoom on the main plot
276    end
277   % xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
278    xlim=get(haxes,'XLim');
279    ylim=get(haxes,'YLim');
280    % if left mouse button has been pressed, zoom in by a factor of 2
281    if  isequal(get(gcf,'SelectionType'),'normal');%if left button has been pressed, zoom in by a factor of 2
282        PlotBoxAspectRatio=get(haxes,'PlotBoxAspectRatio');
283        yoverx=PlotBoxAspectRatio(2)/PlotBoxAspectRatio(1);
284        if yoverx <2
285            xlim(1)=0.5*xy(1,1)+0.5*xlim(1);
286            xlim(2)=0.5*xy(1,1)+0.5*xlim(2);%double the field whith the middle at the selected points
287            set(haxes,'XLim',xlim)
288        end
289        if yoverx >0.5
290            ylim(2)=0.5*xy(1,2)+0.5*ylim(2);
291            ylim(1)=0.5*xy(1,2)+0.5*ylim(1);
292            set(haxes,'YLim',ylim)
293        end
294       
295        % if right mouse button has been pressed, zoom out by a factor of 2
296    else
297        xlim(1)=2*xlim(1)-xy(1,1);% reverse of the zoom on action
298        xlim(2)=2*xlim(2)-xy(1,1);
299        ylim(1)=2*ylim(1)-xy(1,2);
300        ylim(2)=2*ylim(2)-xy(1,2);
301        % adjust the zoom out to the available field
302        if ~testsubplot && isfield(AxeData,'RangeX')&& isfield(AxeData,'RangeY')
303            xlim(1)=max(AxeData.RangeX(1),xlim(1));
304            xlim(2)=min(AxeData.RangeX(2),xlim(2));
305            ylim(1)=max(AxeData.RangeY(1),ylim(1));
306            ylim(2)=min(AxeData.RangeY(2),ylim(2));
307            if ylim(1)>=ylim(2)|| xlim(1)>=xlim(2)
308                xlim=AxeData.RangeX;
309                ylim=AxeData.RangeY;
310            end
311            % desactivate the zoom if the full field is visible within the axes
312            if isequal(xlim,AxeData.RangeX) && isequal(ylim,AxeData.RangeY)
313                set(hhuvmat.CheckZoom,'Value',0)
314               % set(hhuvmat.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
315                set(hhuvmat.CheckFixLimits,'Value',0)
316              %  set(hhuvmat.CheckFixLimits,'BackgroundColor',[0.7 0.7 0.7])
317            end
318        end
319        set(haxes,'XLim',xlim)
320        set(haxes,'YLim',ylim)
321        %test whther zoom out is operating (to inactivate AxedAta
322        if ~isfield(AxeData,'CurrentXLim')|| ~isequal(xlim,AxeData.CurrentXLim)
323            AxeData.CurrentXLim=xlim;%
324        end
325    end
326    %if isfield(AxeData,'LimEditBox')&& AxeData.LimEditBox% update display of the GUI containing the axis (uvmat or view_field)
327    if testsubplot
328        set(AxeData.CurrentRectZoom,'Position',[xlim(1) ylim(1) xlim(2)-xlim(1) ylim(2)-ylim(1)])
329    else
330        set(hhcurrentfig.num_MinX,'String',num2str(xlim(1)))
331        set(hhcurrentfig.num_MaxX,'String',num2str(xlim(2)))
332        set(hhcurrentfig.num_MinY,'String',num2str(ylim(1)))
333        set(hhcurrentfig.num_MaxY,'String',num2str(ylim(2)))
334    end
335end
336
337%% editing calibration point
338if ~CheckZoom && isfield(AxeData,'Drawing') && isequal(AxeData.Drawing,'calibration')
339    h_geometry_calib=findobj(allchild(0),'tag','geometry_calib'); %find the geomterty_calib GUI
340    if ~isempty(h_geometry_calib)
341        hh_geometry_calib=guidata(h_geometry_calib);
342        edit_test=get(hh_geometry_calib.CheckEnableMouse,'Value');
343        hh=findobj(hcurrentaxes,'tag','calib_points');%look for handle of calibration points
344        if ~isempty(hh) && edit_test
345            index_point=get(hh,'UserData');
346            set(hh,'UserData',[])%remove edit mode
347            h_ListCoord=hh_geometry_calib.ListCoord; %handles of the coordinate list
348            Coord=get(h_ListCoord,'Data');
349            Coord(index_point,4)=xy(1,1);
350            Coord(index_point,5)=xy(1,2);
351            set(h_ListCoord,'Data',Coord)
352            set(hh,'XData',Coord(:,4))
353            set(hh,'YData',Coord(:,5))
354        end
355    end
356end
357
358%% finalising ruler
359if test_ruler && ~isempty(xy)
360    set(hhuvmat.MenuRuler,'checked','off')%desable the ruler option in uvmat
361    xy=get(hcurrentaxes,'CurrentPoint');% get the current mouse coordinates
362    RulerCoord=[AxeData.RulerCoord ;xy(1,1:2)];% append the recorded ruler origin to the current mouse coordinates
363    RulerCoord=diff(RulerCoord,1);% coordiante difference between segment end and beginning
364    RulerCoord=RulerCoord(1)+i*RulerCoord(2);
365    distance=abs(RulerCoord);
366    azimuth=(180/pi)*angle(RulerCoord);
367    msgbox_uvmat('RULER','',['length: ' num2str(distance,3) ',  angle(degrees): ' num2str(azimuth,3)])
368    delete(AxeData.RulerHandle)%delete the ruler graphic object
369    AxeData=rmfield(AxeData,'RulerHandle');%remove the ruler handle in AxeData
370    AxeData.Drawing='off';%exit the ruler drawing mode
371end
372
373
374%% update
375if test_drawing==0
376        AxeData.Drawing='off';%stop current drawing action
377end
378set(hcurrentaxes,'UserData',AxeData);
379if ~isempty(huvmat)
380    set(huvmat,'UserData',UvData);
381end
382
383%------------------------------------------------------------------------   
384% --- 'close_fig': function  activated when a zoom figure is closed
385%------------------------------------------------------------------------
386function close_fig(ggg,eventdata,hparent)
387
388hfig=get(get(hparent,'parent'),'parent');
389hbutton=findobj(hfig,'Tag','CheckZoomFig');
390if ~isempty(hbutton)
391    set(hbutton,'Value',0)% desactivate the zoom fig option
392end
393delete(hparent)  % delete the rectangle showing the zoom graph in the parent fig
394
Note: See TracBrowser for help on using the repository browser.