source: trunk/src/mouse_up.m @ 644

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

a few bugs repaired, display of the local mouse indication in a separate window

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