source: trunk/src/mouse_up.m @ 323

Last change on this file since 323 was 302, checked in by sommeria, 13 years ago

improve the display of projection objects

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