source: trunk/src/mouse_up.m @ 622

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

mouse system for uvmat imrpoved

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