source: trunk/src/mouse_up.m @ 100

Last change on this file since 100 was 89, checked in by sommeria, 14 years ago

many bug corrections and cleaning. Activation of the BW option in uvmat. Improvement of the interaction of get_field with uvmat.

File size: 18.1 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)
25MouseAction='none'; %default
26zoomstate=0;%default
27if ~exist('handles','var')
28   handles=get(gcbo,'UserData');
29end
30huvmat=findobj(allchild(0),'Name','uvmat');%find the uvmat interface handle
31if ~isempty(huvmat)
32    hhuvmat=guidata(huvmat);
33    UvData=get(huvmat,'UserData');
34    if isfield(UvData,'MouseAction')
35        MouseAction=UvData.MouseAction;% set the mouse action (edit, create objects...)
36    end
37    zoomstate=get(hhuvmat.zoom,'Value');
38end
39currentfig=hObject;
40currentaxes=gca; %store the current axes handle
41AxeData=get(currentaxes,'UserData');
42
43test_drawing=0;%default
44
45%finalize the fabrication or the translation/deformation of an object and plot the corresponding projected field
46if ~isempty(huvmat) & isfield(AxeData,'Drawing') & ~isequal(AxeData.Drawing,'off') & isfield(AxeData,'CurrentObject')...
47           & ishandle(AxeData.CurrentObject)
48    xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
49    PlotData=get(AxeData.CurrentObject,'UserData');%get data attached to the current projection object 
50    IndexObj=PlotData.IndexObj;
51    ObjectData=UvData.Object{IndexObj};   
52    ObjectData.enable_plot=1;
53    if isequal(AxeData.Drawing,'translate')
54        XYData=AxeData.CurrentOrigin;
55        DX=xy(1,1)-XYData(1);%translation from initial position
56        DY=xy(1,2)-XYData(2);
57        ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
58        ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
59    elseif isequal(AxeData.Drawing,'deform')
60        ind_move=AxeData.CurrentIndex;
61        ObjectData.Coord(ind_move,1)=xy(1,1);
62        ObjectData.Coord(ind_move,2)=xy(1,2);
63    else   %creating object
64        if isequal(ObjectData.Style,'line')||isequal(ObjectData.Style,'polyline')||...
65                isequal(ObjectData.Style,'polygon')||isequal(ObjectData.Style,'points')
66            if isfield(AxeData,'ObjectCoord') && size(AxeData.ObjectCoord,2)==3
67              xy(1,3)=AxeData.ObjectCoord(1,3); % z coordinate of the mouse: to generalise ...
68            else
69                 xy(1,3)=0; % z coordinate set to 0 by default
70            end
71            if ~isequal(ObjectData.Coord,xy(1,:))
72                ObjectData.Coord=[ObjectData.Coord ;xy(1,:)];% append the coordiantes marked by the mouse to the eobject
73            end
74        elseif isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')||isequal(ObjectData.Style,'volume')
75            XYData=AxeData.CurrentOrigin;
76            ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
77            ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
78            ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
79            ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
80        elseif isequal(ObjectData.Style,'plane') %case of 'plane'
81            DX=(xy(1,1)-ObjectData.Coord(1,1));
82            DY=(xy(1,2)-ObjectData.Coord(1,2));
83            ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
84            if isfield(ObjectData,'RangeX')
85                XMax=sqrt(DX*DX+DY*DY);
86                if XMax>max(ObjectData.RangeX)
87                    ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
88                end
89            end
90        end
91    end
92    %set(AxeData.CurrentObject,'UserData',ObjectData); %update the object properties
93    if isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')
94        NbDefPoint=1; 
95    elseif isequal(ObjectData.Style,'line')|| isequal(ObjectData.Style,'plane');
96        NbDefPoint=2;
97    else
98         NbDefPoint=3;
99    end
100   
101    %show object coordinates in the GUI set_object
102    h_set_object=findobj(allchild(0),'Name','set_object');
103    h_XObject=findobj(h_set_object,'Tag','XObject');
104    h_YObject=findobj(h_set_object,'Tag','YObject');
105    h_ZObject=findobj(h_set_object,'Tag','ZObject');
106    set(h_XObject,'String',num2str(ObjectData.Coord(:,1),4));
107    set(h_YObject,'String',num2str(ObjectData.Coord(:,2),4));
108    set(h_ZObject,'String',num2str(ObjectData.Coord(:,3),4));
109    if NbDefPoint<=2 || isequal(get(currentfig,'SelectionType'),'alt') ||...
110              isequal(AxeData.Drawing,'translate') || isequal(AxeData.Drawing,'deform');%stop drawing
111        AxeData.CurrentOrigin=[]; %suppress the current origin
112       if isequal(ObjectData.Style,'line') && size(ObjectData.Coord,1)<=1
113           AxeData.Drawing='off';
114           set(currentaxes,'UserData',AxeData);
115            return % line needs at leqst two points
116       end
117       if  ~isempty(ObjectData)
118             testmask=0;
119             hmask=findobj(huvmat,'Tag','makemask');
120             if ~isempty(hmask)
121                testmask=get(hmask,'Value');
122             end
123             if testmask
124                 PlotHandles=[];%do not project data on the object during mask creation
125             else
126                 PlotHandles=get_plot_handles(handles);%get the handles of the graphic objects setting the plotting parameters
127             end
128            AxeData.hset_object=set_object(ObjectData,PlotHandles);% call the set_object interface ,*
129            UvData.Object{IndexObj}=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
130            %ObjectData=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
131            if  isfield(UvData.Object{IndexObj},'PlotParam')
132                write_plot_param(PlotHandles,UvData.Object{IndexObj}.PlotParam); %update the display of plotting parameters for the current object
133            end             
134%             set(hhuvmat.create,'Value',0);% set to 'off' the button for object creation
135%             set(hhuvmat.create,'BackgroundColor',[0 1 0]);% paint the creation button in green
136            set(hhuvmat.edit,'BackgroundColor',[1 1 0]);% paint the edit text in yellow
137            set(hhuvmat.edit,'Value',1);%
138            set(hhuvmat.edit,'Enable','on');%
139            set(hhuvmat.MenuEditObject,'Enable','on');%
140            set(hhuvmat.MenuEdit,'Enable','on');%
141%             set(hhuvmat.MenuObject,'Enable','on');%
142            UvData.MouseAction='edit_object'; % set the edit button to 'on'
143        end
144    else
145       AxeData.CurrentOrigin=[xy(1,1) xy(1,2)]; %the current point becomes the new current origin
146       test_drawing=1;%allow continuation of drawing object
147       UvData.Object{IndexObj}=ObjectData;
148    end
149    hother=findobj('Tag','deformpoint');%find all the deformpoints
150    set(hother,'Color','b');%reset all the deformpoints in 'blue'
151else
152    test_drawing=0;
153end
154
155%creation of a new zoom plot
156test_replot=0;
157if isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
158%         FigData=get(currentfig,'UserData');
159        hparentfig=currentfig;
160        %open or update a new zoom figure if a rectangle has been drawn
161        if ishandle(currentaxes);
162            if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
163                PosRect=get(AxeData.CurrentRectZoom,'Position');
164                if isfield(AxeData,'CurrentVec') & ishandle(AxeData.CurrentVec)
165                    delete(AxeData.CurrentVec)
166                end
167                hfig2=figure;%create new figure
168                set(hfig2,'name','zoom')
169                set(hfig2,'Units','normalized')
170                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
171                map=colormap(currentaxes);
172                colormap(map);%transmit the current colormap to the zoom fig
173                get(handles.RootFile,'String')
174                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
175                if test_replot==0
176                    set(hfig2,'Unit','normalized')
177                    set(hfig2,'KeyPressFcn',{@keyboard_callback,handles})%set keyboard action function
178                    set(hfig2,'WindowButtonMotionFcn',{@mouse_motion,handles})%set mouse action function
179                    set(hfig2,'WindowButtonDownFcn',{@mouse_down})%set mouse click action function
180                    set(hfig2,'WindowButtonUpFcn',{@mouse_up,handles}) 
181                    set(hfig2,'DeleteFcn',{@close_fig,AxeData.CurrentRectZoom,'zoom'})
182                    set(hfig2,'UserData',AxeData.CurrentRectZoom)% record the parent object (zoom rectangle) in the new fig
183                    AxeData.ZoomAxes=copyobj(currentaxes,hfig2); %copy the current graph axes to the zoom figure
184                    figure(hfig2)
185                    set(AxeData.ZoomAxes,'Position',[0.1300    0.1100    0.7750    0.8150])% standard axes position on a figure
186                    hcol=findobj(hparentfig,'Tag','Colorbar'); %look for colorbar axes
187                    if ~isempty(hcol)             
188                        hcol_new=colorbar;
189                        YTick=get(hcol,'YTick');
190                        YTicklabel=get(hcol,'Yticklabel'); 
191                        colbarlim=get(hcol,'YLim');
192                        newcolbarlim=get(hcol_new,'YLim');
193                        scale_bar=(newcolbarlim(2)-newcolbarlim(1))/(colbarlim(2)-colbarlim(1));               
194                        YTick_rescaled=newcolbarlim(1)+scale_bar*(YTick-colbarlim(1));
195                        set(hcol_new,'YTick',YTick_rescaled);
196                        set(hcol_new,'Yticklabel',YTicklabel);
197                    end
198                end
199                if ishandle(AxeData.ZoomAxes)
200                    hnew_rect=findobj(AxeData.ZoomAxes,'Tag','rect_zoom');
201                    if ~isempty(hnew_rect)
202                        delete(hnew_rect);
203                        ChildAxeData=get(AxeData.ZoomAxes,'UserData');
204                        ChildAxeData.CurrentRectZoom=[]; % no rect zoom in the new window
205                        ChildAxeData.Drawing='off';
206                        ChildAxeData.ParentRect=AxeData.CurrentRectZoom;%set the rectangle as a 'parent' associated to the new axes
207                        set(AxeData.ZoomAxes,'UserData',ChildAxeData);%update the AxeData of the new axes
208                        set(AxeData.ZoomAxes,'Xlim',[PosRect(1) PosRect(1)+PosRect(3)])
209                        set(AxeData.ZoomAxes,'Ylim',[PosRect(2) PosRect(2)+PosRect(4)])
210                    end
211                end
212            end
213        end
214end
215%zoom in by a factor 2 if no new figure is created
216if zoomstate
217    xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
218     if  isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
219        xlim=get(currentaxes,'XLim');
220        xlim_new(2)=0.5*xy(1,1)+0.5*xlim(2);
221        xlim_new(1)=0.5*xy(1,1)+0.5*xlim(1);
222        set(currentaxes,'XLim',xlim_new)
223        ylim=get(currentaxes,'YLim');
224        ylim_new(2)=0.5*xy(1,2)+0.5*ylim(2);
225        ylim_new(1)=0.5*xy(1,2)+0.5*ylim(1);
226        set(currentaxes,'YLim',ylim_new)
227        if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
228            hparentrect=AxeData.ParentRect;
229            xlim=get(currentaxes,'XLim');
230            ylim=get(currentaxes,'YLim');
231            rect([1 2])=[xlim(1) ylim(1)];
232            rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
233            set(hparentrect,'Position',rect)
234        end
235     %zoom out by a factor of 2 out when the right mouse button has been used
236     elseif isequal(get(currentfig,'SelectionType'),'alt'); %if right button has been pressed
237            alpha=2; %zoom factor (zoom out by a factor 2)
238            xlim=get(currentaxes,'XLim');
239            xlim_new(1)=2*xlim(1)-xy(1,1);
240            xlim_new(2)=2*xlim(2)-xy(1,1);
241%             xlim_new(1)=(1+alpha)*xlim(1)/2+(1-alpha)*xlim(2)/2;
242%             xlim_new(2)=(1-alpha)*xlim(1)/2+(1+alpha)*xlim(2)/2;
243            ylim=get(currentaxes,'YLim');
244            ylim_new(1)=2*ylim(1)-xy(1,2);
245            ylim_new(2)=2*ylim(2)-xy(1,2);
246%             ylim_new(1)=(1+alpha)*ylim(1)/2+(1-alpha)*ylim(2)/2;
247%             ylim_new(2)=(1-alpha)*ylim(1)/2+(1+alpha)*ylim(2)/2;
248            if isfield(AxeData,'RangeX') && isfield(AxeData,'RangeY')
249                xlim_new(1)=max(AxeData.RangeX(1),xlim_new(1));
250                xlim_new(2)=min(AxeData.RangeX(2),xlim_new(2));
251                ylim_new(1)=max(AxeData.RangeY(1),ylim_new(1));
252                ylim_new(2)=min(AxeData.RangeY(2),ylim_new(2));
253                if isequal(xlim_new,AxeData.RangeX) && isequal(ylim_new,AxeData.RangeY)
254                    set(hhuvmat.zoom,'Value',0)
255                    set(hhuvmat.zoom,'BackgroundColor',[0.7 0.7 0.7])
256                    set(hhuvmat.FixedLimits,'Value',0)
257                    set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
258                end
259            end
260            set(currentaxes,'XLim',xlim_new)
261            set(currentaxes,'YLim',ylim_new)
262            %test whther zoom out is operating (to inactivate AxedAta
263            if ~isfield(AxeData,'CurrentXLim')|| ~isequal(xlim,AxeData.CurrentXLim)
264                AxeData.CurrentXLim=xlim;%
265            end
266            if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
267                hparentrect=AxeData.ParentRect;
268                xlim=get(currentaxes,'XLim');
269                ylim=get(currentaxes,'YLim');
270                rect([1 2])=[xlim(1) ylim(1)];
271                rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
272                set(hparentrect,'Position',rect)
273            end
274      end
275end
276
277% editing calibration point
278if ~zoomstate && strcmp(MouseAction,'calib')
279    h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
280    hh_geometry_calib=guidata(h_geometry_calib);
281    edit_test=get(hh_geometry_calib.edit_append,'Value');
282    hh=findobj(currentaxes,'Tag','calib_points');%look for handle of calibration points           
283    if ~isempty(hh) && edit_test
284        index_point=get(hh,'UserData');
285        set(hh,'UserData',[])%remove edit mode
286        h_ListCoord=hh_geometry_calib.ListCoord; %handles of the coordinate list
287        Coord=get(h_ListCoord,'String');
288%         val=get(h_ListCoord,'Value');
289        coord_str=Coord{index_point}; %current line (string)
290        k=findstr('|',coord_str);%find separator indices on the string
291        xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
292        if numel(k)>=3
293            coord_str=[coord_str(1:k(3)-1) '|    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)]; %update the pixel information while preserving phys coord
294        else
295            coord_str=[ '    |    '  '    |    '  '    |    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)];
296        end
297        Coord{index_point}=coord_str;       
298        set(h_ListCoord,'String',Coord)
299        data=read_geometry_calib(Coord);%transform char cell to numbers
300        set(hh,'XData',data.Coord(:,4))
301        set(hh,'YData',data.Coord(:,5))
302    end
303end
304
305% finalising ruler
306if strcmp(MouseAction,'ruler')
307    UvData.MouseAction='none';
308    UvData=rmfield(UvData,'RulerHandle');
309     xy=get(currentaxes,'CurrentPoint');
310    RulerCoord=[UvData.RulerCoord ;xy(1,1:2)];
311    set(huvmat,'UserData',UvData)
312    RulerCoord=diff(RulerCoord,1);
313    RulerCoord=RulerCoord(1)+i*RulerCoord(2);
314    distance=abs(RulerCoord);
315    azimuth=(180/pi)*angle(RulerCoord);
316    msgbox_uvmat('RULER','',['length: ' num2str(distance,3) ',  angle(degrees): ' num2str(azimuth,3)])
317    hruler=findobj(currentaxes,'Tag','ruler');
318    delete(hruler)
319    AxeData.Drawing='off';%stop current drawing a
320end
321
322
323%display the data of the current object selected with the mouse right click
324if isequal(get(currentfig,'SelectionType'),'alt') && ~zoomstate && (~isfield(AxeData,'Drawing')||~isequal(AxeData.Drawing,'create'))
325    hother=findobj('Tag','proj_object');%find all the proj objects
326    nbselect=0;
327    %test the existence of selected objects:
328    for iproj=1:length(hother);
329        iselect=isequal(get(hother(iproj),'Selected'),'on');%reset all the proj objects in 'blue' by default
330        nbselect=nbselect+iselect;
331    end
332    hother=findobj('Tag','proj_object','Type','line');%find all the proj objects
333    set(hother,'Color','b');%reset all the proj objects in 'blue' by default
334    set(hother,'Selected','off')
335    hother=findobj('Tag','proj_object','Type','rectangle');
336    set(hother,'EdgeColor','b');
337    set(hother,'Selected','off')
338    hother=findobj('Tag','proj_object','Type','patch');
339    set(hother,'FaceColor','b');   
340    if isequal(get(gco,'Type'),'image')
341        currentobj=get(gco,'parent');%parent axes of the image
342    else
343        currentobj=gco;%default
344    end
345    if ((nbselect==0) && isequal(get(currentobj,'Type'),'axes')) || isequal(currentobj,huvmat)
346        global Data_uvmat
347        Data_uvmat=get(currentobj,'UserData');
348        %plot_text(CurData)
349        %get_field([],CurData);
350        evalin('base','global Data_uvmat')%make CurData global in the workspace
351        objtype=get(currentobj,'Type');
352        display(['UserData of ' objtype ':'])
353        evalin('base','Data_uvmat') %display CurData in the workspace
354        commandwindow %brings the Matlab command window to the front
355    end
356end
357if test_drawing==0
358        AxeData.Drawing='off';%stop current drawing action
359end
360set(currentaxes,'UserData',AxeData);
361if ~isempty(huvmat)
362    set(huvmat,'UserData',UvData);
363end
364
365   
366
Note: See TracBrowser for help on using the repository browser.