source: trunk/src/mouse_up.m

Last change on this file was 1180, checked in by sommeria, 6 weeks ago

various bugs repaired,Relabeling frames installed for multitif

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