source: trunk/src/mouse_up.m @ 154

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

bug repair for object creation

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