source: trunk/src/mouse_up.m @ 453

Last change on this file since 453 was 444, checked in by sommeria, 12 years ago

masks and calibration updated to fit with the new conventions on file organisation.
bug corrected in mouse_up (object creation)
bug corrected in civ in mode TESTciv

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