source: trunk/src/mouse_up.m @ 954

Last change on this file since 954 was 954, checked in by sommeria, 8 years ago

update calib modfied + various updates

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