source: trunk/src/mouse_up.m @ 29

Last change on this file since 29 was 18, checked in by sommeria, 14 years ago

moved all the scripts from private down to root

File size: 15.9 KB
Line 
1%'mouse_up': function  activated when the mouse button is released
2%----------------------------------------------------------------
3% function mouse_up(ggg,eventdata,handles)
4% activated by the command:
5% set(hObject,'WindowButtonUpFcn',{'mouse_up'}),
6% where hObject is the handle of the figure
7%
8%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
9%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
10%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
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%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
23
24function mouse_up(ggg,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
39if isequal(MouseAction,'calib') && ~zoomstate
40    return
41end
42currentfig=gcbo;
43AxeData=get(gca,'UserData');
44currentaxes=gca; %store the current axes handle
45test_drawing=0;%default
46
47%finalize the fabrication or the translation/deformation of an object and plot the corresponding projected field
48if ~isempty(huvmat) & isfield(AxeData,'Drawing') & ~isequal(AxeData.Drawing,'off') & isfield(AxeData,'CurrentObject')...
49           & ishandle(AxeData.CurrentObject)
50    xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
51    PlotData=get(AxeData.CurrentObject,'UserData');%get data attached to the current projection object 
52    IndexObj=PlotData.IndexObj;
53    ObjectData=UvData.Object{IndexObj};   
54    if isequal(AxeData.Drawing,'translate')
55        XYData=AxeData.CurrentOrigin;
56        DX=xy(1,1)-XYData(1);%translation from initial position
57        DY=xy(1,2)-XYData(2);
58        ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
59        ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
60    elseif isequal(AxeData.Drawing,'deform')
61        ind_move=AxeData.CurrentIndex;
62        ObjectData.Coord(ind_move,1)=xy(1,1);
63        ObjectData.Coord(ind_move,2)=xy(1,2);
64    else   %creating object
65        if isequal(ObjectData.Style,'line')||isequal(ObjectData.Style,'polyline')||...
66                isequal(ObjectData.Style,'polygon')||isequal(ObjectData.Style,'points')
67            if isfield(AxeData,'ObjectCoord') && size(AxeData.ObjectCoord,2)==3
68              xy(1,3)=AxeData.ObjectCoord(1,3); % z coordinate of the mouse: to generalise ...
69            else
70                 xy(1,3)=0; % z coordinate set to 0 by default
71            end
72            if ~isequal(ObjectData.Coord,xy(1,:))
73                ObjectData.Coord=[ObjectData.Coord ;xy(1,:)];% append the coordiantes marked by the mouse to the eobject
74            end
75        elseif isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')||isequal(ObjectData.Style,'volume')
76            XYData=AxeData.CurrentOrigin;
77            ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
78            ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
79            ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
80            ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
81        elseif isequal(ObjectData.Style,'plane') %case of 'plane'
82            DX=(xy(1,1)-ObjectData.Coord(1,1));
83            DY=(xy(1,2)-ObjectData.Coord(1,2));
84            ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
85            if isfield(ObjectData,'RangeX')
86                XMax=sqrt(DX*DX+DY*DY);
87                if XMax>max(ObjectData.RangeX)
88                    ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
89                end
90            end
91        end
92    end
93    %set(AxeData.CurrentObject,'UserData',ObjectData); %update the object properties
94    if isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')
95        NbDefPoint=1; 
96    elseif isequal(ObjectData.Style,'line')|| isequal(ObjectData.Style,'plane');
97        NbDefPoint=2;
98    else
99         NbDefPoint=3;
100    end
101   
102    %show object coordinates in the GUI set_object
103    h_set_object=findobj(allchild(0),'Name','set_object');
104    h_XObject=findobj(h_set_object,'Tag','XObject');
105    h_YObject=findobj(h_set_object,'Tag','YObject');
106    h_ZObject=findobj(h_set_object,'Tag','ZObject');
107    set(h_XObject,'String',num2str(ObjectData.Coord(:,1),4));
108    set(h_YObject,'String',num2str(ObjectData.Coord(:,2),4));
109    set(h_ZObject,'String',num2str(ObjectData.Coord(:,3),4));
110    if NbDefPoint<=2 || isequal(get(currentfig,'SelectionType'),'alt') ||...
111              isequal(AxeData.Drawing,'translate') || isequal(AxeData.Drawing,'deform');%stop drawing
112        AxeData.CurrentOrigin=[]; %suppress the current origin
113       if isequal(ObjectData.Style,'line') && size(ObjectData.Coord,1)<=1
114           AxeData.Drawing='off';
115           set(currentaxes,'UserData',AxeData);
116            return % line needs at leqst two points
117       end
118       if  ~isempty(ObjectData)
119             testmask=0;
120             hmask=findobj(huvmat,'Tag','makemask');
121             if ~isempty(hmask)
122                testmask=get(hmask,'Value');
123             end
124             if testmask
125                 PlotHandles=[];%do not project data on the object during mask creation
126             else
127                 PlotHandles=get_plot_handles(handles);%get the handles of the graphic objects setting the plotting parameters
128             end
129            AxeData.hset_object=set_object(ObjectData,PlotHandles);% call the set_object interface ,*
130            UvData.Object{IndexObj}=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
131            %ObjectData=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
132            if  isfield(UvData.Object{IndexObj},'PlotParam')
133                write_plot_param(PlotHandles,UvData.Object{IndexObj}.PlotParam); %update the display of plotting parameters for the current object
134            end             
135            set(hhuvmat.create,'Value',0);% set to 'off' the button for object creation
136            set(hhuvmat.create,'BackgroundColor',[0 1 0]);% paint the creation button in green
137            set(hhuvmat.edit,'BackgroundColor',[1 1 0]);% paint the edit text in yellow
138            set(hhuvmat.edit,'Value',1);%
139            set(hhuvmat.edit,'Enable','on');%
140            set(hhuvmat.MenuEdit,'Enable','on');%
141            set(hhuvmat.MenuEdit,'Enable','on');%
142            set(hhuvmat.MenuObject,'Enable','on');%
143            UvData.MouseAction='edit_object'; % set the edit button to 'on'
144        end
145    else
146       AxeData.CurrentOrigin=[xy(1,1) xy(1,2)]; %the current point becomes the new current origin
147       test_drawing=1;%allow continuation of drawing object
148       UvData.Object{IndexObj}=ObjectData;
149    end
150    hother=findobj('Tag','deformpoint');%find all the deformpoints
151    set(hother,'Color','b');%reset all the deformpoints in 'blue'
152else
153    test_drawing=0;
154end
155
156%creation of a new zoom plot
157test_replot=0;
158if isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
159%         FigData=get(currentfig,'UserData');
160        hparentfig=currentfig;
161        %open or update a new zoom figure if a rectangle has been drawn
162        if ishandle(currentaxes);
163            if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
164                PosRect=get(AxeData.CurrentRectZoom,'Position');
165                if isfield(AxeData,'CurrentVec') & ishandle(AxeData.CurrentVec)
166                    delete(AxeData.CurrentVec)
167                end
168                %update the axes UvData.Plane2 if it exists, else create it
169%                 if isfield (UvData,'Plane2') & ishandle(UvData.Plane2.Fig)%if the second plan plotting axis already exists
170%                     hfig2=UvData.Plane2.Fig;
171%                     if isequal(gcf,hfig2)%if we are already on the secondary figure
172%                         test_replot=1;
173%                     else
174%                         figure(hfig2)%set hfig2 as the current figure
175%                         clf; %erase axes
176%                     end
177%                 else
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%                     UvData.Plane2.Fig=hfig2;
183                    map=colormap(currentaxes);
184                    colormap(map);%transmit the current colormap to the zoom fig
185                    get(handles.RootFile,'String')
186%                 end
187                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
188                if test_replot==0
189                    set(hfig2,'Unit','normalized')
190                    set(hfig2,'KeyPressFcn',{@keyboard_callback,handles})%set keyboard action function
191                    set(hfig2,'WindowButtonMotionFcn',{@mouse_motion,handles})%set mouse action function
192                    set(hfig2,'WindowButtonDownFcn',{@mouse_down})%set mouse click action function
193                    set(hfig2,'WindowButtonUpFcn',{@mouse_up,handles}) 
194                    set(hfig2,'DeleteFcn',{@close_fig,AxeData.CurrentRectZoom,'zoom'})
195                    set(hfig2,'UserData',AxeData.CurrentRectZoom)% record the parent object (zoom rectangle) in the new fig
196                    AxeData.ZoomAxes=copyobj(currentaxes,hfig2); %copy the current graph axes to the zoom figure
197                    figure(hfig2)
198                    set(AxeData.ZoomAxes,'Position',[0.1300    0.1100    0.7750    0.8150])% standard axes position on a figure
199                    hcol=findobj(hparentfig,'Tag','Colorbar'); %look for colorbar axes
200                    if ~isempty(hcol)             
201                        hcol_new=colorbar;
202                        YTick=get(hcol,'YTick');
203                        YTicklabel=get(hcol,'Yticklabel'); 
204                        colbarlim=get(hcol,'YLim');
205                        newcolbarlim=get(hcol_new,'YLim');
206                        scale_bar=(newcolbarlim(2)-newcolbarlim(1))/(colbarlim(2)-colbarlim(1));               
207                        YTick_rescaled=newcolbarlim(1)+scale_bar*(YTick-colbarlim(1));
208                        set(hcol_new,'YTick',YTick_rescaled);
209                        set(hcol_new,'Yticklabel',YTicklabel);
210                    end
211                end
212                if ishandle(AxeData.ZoomAxes)
213                    hnew_rect=findobj(AxeData.ZoomAxes,'Tag','rect_zoom');
214                    if ~isempty(hnew_rect)
215                        delete(hnew_rect);
216                        ChildAxeData=get(AxeData.ZoomAxes,'UserData');
217                        ChildAxeData.CurrentRectZoom=[]; % no rect zoom in the new window
218                        ChildAxeData.Drawing='off';
219                        ChildAxeData.ParentRect=AxeData.CurrentRectZoom;%set the rectangle as a 'parent' associated to the new axes
220                        set(AxeData.ZoomAxes,'UserData',ChildAxeData);%update the AxeData of the new axes
221                       % UvData.TopFig=hfig2;%put the new fig to the top of the stack for uvmat
222        %                 set(huvmat,'UserData',UvData)
223                        set(AxeData.ZoomAxes,'Xlim',[PosRect(1) PosRect(1)+PosRect(3)])
224                        set(AxeData.ZoomAxes,'Ylim',[PosRect(2) PosRect(2)+PosRect(4)])
225                    end
226                end
227            end
228        end
229end
230%zoom in if no new figure is created
231if zoomstate
232     if  isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
233        %zoom(2)% zoom in by a factor of 2
234        alpha=0.5; %zoom factor (zoom in by a factor 2)
235        xlim=get(currentaxes,'XLim');
236        xlim_new(1)=(1+alpha)*xlim(1)/2+(1-alpha)*xlim(2)/2;
237        xlim_new(2)=(1-alpha)*xlim(1)/2+(1+alpha)*xlim(2)/2;
238        set(currentaxes,'XLim',xlim_new)
239        ylim=get(currentaxes,'YLim');
240        ylim_new(1)=(1+alpha)*ylim(1)/2+(1-alpha)*ylim(2)/2;
241        ylim_new(2)=(1-alpha)*ylim(1)/2+(1+alpha)*ylim(2)/2;
242        set(currentaxes,'YLim',ylim_new)
243        if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
244            hparentrect=AxeData.ParentRect;
245            xlim=get(currentaxes,'XLim');
246            ylim=get(currentaxes,'YLim');
247            rect([1 2])=[xlim(1) ylim(1)];
248            rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
249            set(hparentrect,'Position',rect)
250        end
251
252     elseif isequal(get(currentfig,'SelectionType'),'alt'); %if right button has been pressed
253            %zoom(0.5)% zoom out by a factor of 2
254            alpha=2; %zoom factor (zoom out by a factor 2)
255            xlim=get(currentaxes,'XLim');
256            xlim_new(1)=(1+alpha)*xlim(1)/2+(1-alpha)*xlim(2)/2;
257            xlim_new(2)=(1-alpha)*xlim(1)/2+(1+alpha)*xlim(2)/2;
258            ylim=get(currentaxes,'YLim');
259            ylim_new(1)=(1+alpha)*ylim(1)/2+(1-alpha)*ylim(2)/2;
260            ylim_new(2)=(1-alpha)*ylim(1)/2+(1+alpha)*ylim(2)/2;
261            set(currentaxes,'XLim',xlim_new)
262            set(currentaxes,'YLim',ylim_new)
263            %test whther zoom out is operating (to inactivate AxedAta
264            if ~isfield(AxeData,'CurrentXLim')| ~isequal(xlim,AxeData.CurrentXLim)
265                AxeData.CurrentXLim=xlim;%
266            end
267            if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
268                hparentrect=AxeData.ParentRect;
269                xlim=get(currentaxes,'XLim');
270                ylim=get(currentaxes,'YLim');
271                rect([1 2])=[xlim(1) ylim(1)];
272                rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
273                set(hparentrect,'Position',rect)
274            end
275      end
276end
277
278%display the data of the current object selected with the mouse right click
279if isequal(get(currentfig,'SelectionType'),'alt') && ~zoomstate && (~isfield(AxeData,'Drawing')||~isequal(AxeData.Drawing,'create'))
280    hother=findobj('Tag','proj_object');%find all the proj objects
281    nbselect=0;
282    %test the existence of selected objects:
283    for iproj=1:length(hother);
284        iselect=isequal(get(hother(iproj),'Selected'),'on');%reset all the proj objects in 'blue' by default
285        nbselect=nbselect+iselect;
286    end
287    hother=findobj('Tag','proj_object','Type','line');%find all the proj objects
288    set(hother,'Color','b');%reset all the proj objects in 'blue' by default
289    set(hother,'Selected','off')
290    hother=findobj('Tag','proj_object','Type','rectangle');
291    set(hother,'EdgeColor','b');
292    set(hother,'Selected','off')
293    hother=findobj('Tag','proj_object','Type','patch');
294    set(hother,'FaceColor','b');   
295    if isequal(get(gco,'Type'),'image')
296        currentobj=get(gco,'parent');%parent axes of the image
297    else
298        currentobj=gco;%default
299    end
300    if ((nbselect==0) && isequal(get(currentobj,'Type'),'axes')) || isequal(currentobj,huvmat)
301        global CurData
302        CurData=get(currentobj,'UserData');
303        %plot_text(CurData)
304        %get_field([],CurData);
305        evalin('base','global CurData')%make CurData global in the workspace
306        objtype=get(currentobj,'Type');
307        display(['UserData of ' objtype ':'])
308        evalin('base','CurData') %display CurData in the workspace
309        commandwindow
310    end
311end
312if test_drawing==0
313        AxeData.Drawing='off';%stop current drawing action
314end
315set(currentaxes,'UserData',AxeData);
316if ~isempty(huvmat)
317    set(huvmat,'UserData',UvData);
318end
319
320   
321
Note: See TracBrowser for help on using the repository browser.