source: trunk/src/mouse_down.m @ 198

Last change on this file since 198 was 187, checked in by sommeria, 14 years ago

various bug repairs

File size: 18.0 KB
Line 
1%'mouse_down': function activated when the mouse button is pressed on a figure (callback for 'WindowButtonDownFcn'
2%--------------------------------------------------------------
3% xy=mouse_down(hObject,eventdata)
4% activated by the command:
5% set(hObject,'WindowButtonDownFcn',{'mouse_down'}),
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 xy=mouse_down(hObject,eventdata)
25
26huvmat=findobj(allchild(0),'tag','uvmat');%find the uvmat interface handle which controls theoption of  mouse action
27if isempty(huvmat)
28    return
29end
30hhuvmat=guidata(huvmat);%handles of elements in uvmat
31UvData=get(huvmat,'UserData');
32FigData=get(hObject,'UserData');
33if ishandle(FigData)% case of a zoom plot, the handle of the parent rectangle is stored in UserData, its parent is the plotting axes of the rectangle
34    hcurrentfig=get(get(FigData,'parent'),'parent');
35else
36    hcurrentfig=hObject;%usual plot
37end
38hhcurrentfig=guidata(hcurrentfig);
39test_zoom=get(hhcurrentfig.zoom,'Value');%test for zoom action, first priority
40test_ruler=isequal(get(hhuvmat.MenuRuler,'checked'),'on');%test for ruler  action, second priority;
41test_edit=get(hhuvmat.edit_object,'Value');%test for object editing, third priority
42test_edit_vect=get(hhuvmat.edit_vect,'Value');%test for vector editing,  priority 4
43test_create=isequal(get(hhuvmat.MenuObject,'checked'),'on');% test for object creation,  priority 5
44if test_create
45    hset_object=findobj(allchild(0),'tag','set_object');
46    test_create=~isempty(hset_object)&&~test_edit;
47end
48test_cal=isequal(get(hhuvmat.MenuCalib,'checked'),'on');% test for calibration
49if test_cal% test for calibration popints,  priority 6
50    h_calib=findobj(allchild(0),'tag','geometry_calib');
51    if isempty(h_calib)
52        test_cal=0;
53        set(hhuvmat.MenuCalib,'checked','off');% test for calibration off
54    else
55        hh_calib=guidata(h_calib);
56        test_cal=get(hh_calib.edit_append,'Value');
57    end
58end
59xdisplay=[];%default
60ydisplay=[];%default
61AxeData=[];%default
62
63%% determine the currently selected items
64hcurrentobject=gco;% current object handle (selected by the mouse)
65%hcurrentfig=hObject;% current figure handle
66fig_tag=get(hcurrentfig,'Tag');
67tag_obj=get(gco,'Tag');%tag of the currently selected object
68xy=[];%default
69xy_fig=get(hObject,'CurrentPoint');% current point of the current figure (gcbo)
70hchild=get(hObject,'Children');%handles of all objects in the current figure
71haxes=[];
72
73%% loop on all the objects in the current figure (selected by the last mouse click)
74for ichild=1:length(hchild)
75    obj_pos=get(hchild(ichild),'Position');%position of the object
76    if xy_fig(1) >=obj_pos(1) & xy_fig(2) >= obj_pos(2)& xy_fig(1) <=obj_pos(1)+obj_pos(3) & xy_fig(2) <= obj_pos(2)+obj_pos(4);
77        htype=get(hchild(ichild),'Type');%type of object child of the current figure
78        %if the mouse is over an axis, look at the data
79        if isequal(htype,'axes')
80            y_lim=get(hchild(ichild),'YLim');
81            x_lim=get(hchild(ichild),'XLim');
82            haxes=hchild(ichild);
83            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
84            if xy(1,1)>x_lim(1) && xy(1,1)<x_lim(2) && xy(1,2)>y_lim(1) && xy(1,2)<y_lim(2)
85                AxeData=get(haxes,'UserData');% data attached to the axis
86                AxeData.CurrentOrigin=[xy(1,1) xy(1,2)];% The current point set by the mouse becomes the current origin
87                if test_edit_vect && ~isequal(tag_obj,'proj_object') & ~test_create
88                    ivec=[];
89                    FigData=get(hcurrentfig,'UserData');
90                    tagaxes=get(haxes,'tag');
91                    if isfield(FigData,tagaxes)
92                        eval(['Field=FigData.' tagaxes ';'])
93                        [CellVarIndex,NbDim,VarType]=find_field_indices(Field);%analyse the physical fields contained in Field
94                        for icell=1:numel(CellVarIndex)%look for all physical fields
95                            if NbDim(icell)==2 % select 2D field
96                                if  isfield(Field,'Mesh') && ~isempty(Field.Mesh)&& ~isempty(VarType{icell}.coord_x) && ~isempty(VarType{icell}.coord_y)%case of unstructured data
97                                    eval(['X=Field.' Field.ListVarName{VarType{icell}.coord_x} ';'])
98                                    eval(['Y=Field.' Field.ListVarName{VarType{icell}.coord_y} ';'])
99                                    flag_vec=(X<(xy(1,1)+Field.Mesh/4) & X>(xy(1,1)-Field.Mesh/4)) & ...%flagx=1 for the vectors with x position selected by the mouse
100                                        (Y<(xy(1,2)+Field.Mesh/4) & Y>(xy(1,2)-Field.Mesh/4));%f
101                                    ivec=find(flag_vec,1);% search the (first) selected vector index ivec
102                                end
103                            end
104                        end
105                    end
106                end
107            else
108                haxes=[];%mouse out of axes
109            end
110        end
111    end
112end
113
114%% desable  object creation and vector editing if NbDim different from 2
115if ~(isfield(AxeData,'NbDim') && isequal(AxeData.NbDim,2))
116    test_create=0;
117    test_edit_vect=0;   
118end
119
120%% delete the current zoom rectangle
121if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
122    delete(AxeData.CurrentRectZoom)
123    AxeData.CurrentRectZoom=[];
124end   
125
126%% zoom has first priority
127if test_zoom %&& ~test_create && ~test_edit && ~test_edit_vect && exist('xy','var')
128     AxeData.Drawing='zoom'; %initiate drawing mode
129     AxeData.CurrentObject=[];%unselect objects
130     set(haxes,'UserData',AxeData);
131     return
132end
133if isempty(huvmat)%further options require the uvmat GUI
134    return
135end
136
137%% ruler has second priority
138if test_ruler
139    AxeData.RulerCoord(1,1)=xy(1,1);
140    AxeData.RulerCoord(1,2)=xy(1,2);
141    AxeData.RulerHandle=line([xy(1,1) xy(1,1)],[xy(1,2) xy(1,2)],'Color','m','Tag','ruler');
142    AxeData.Drawing='ruler';
143    set(haxes,'UserData',AxeData);
144    return
145end
146
147%% selection of an existing projection object (third priority)
148if  test_edit && (isequal(tag_obj,'proj_object')||isequal(tag_obj,'DeformPoint'))
149    if ~(isfield(AxeData,'Drawing') && isequal(AxeData.Drawing,'create'))
150        userdata=get(hcurrentobject,'UserData');
151        if ishandle(userdata)%the selected line depends on a parent line
152            AxeData.CurrentObject=userdata;% the parent object becomes the current one
153        else
154            AxeData.CurrentObject=hcurrentobject;% the selected object becomes the current one
155        end
156        ObjectData=get(AxeData.CurrentObject,'UserData');
157        if test_edit && isfield(ObjectData,'IndexObj')
158            hother=findobj('Tag','proj_object','Type','line');%find all the proj objects
159            set(hother,'Color','b');%reset all the proj objects in 'blue' by default
160            set(hother,'Selected','off')
161            hother=findobj('Tag','proj_object','Type','rectangle');
162            set(hother,'EdgeColor','b');
163            set(hother,'Selected','off');
164            hother=findobj('Tag','proj_object','Type','image');
165            for iobj=1:length(hother)
166                   Acolor=get(hother(iobj),'CData');
167                   Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
168                   set(hother(iobj),'CData',Acolor);
169            end
170            hother=findobj('Tag','DeformPoint');
171            set(hother,'Color','b');
172            set(hother,'Selected','off')   
173            if isequal(get(AxeData.CurrentObject,'Type'),'line')
174                set(AxeData.CurrentObject,'Color','m'); %set the selected object to magenta color
175            elseif isequal(get(AxeData.CurrentObject,'Type'),'rectangle')
176                 set(AxeData.CurrentObject,'EdgeColor','m'); %set the selected object to magenta color
177            end
178            if isfield(ObjectData,'SubObject')& ishandle(ObjectData.SubObject)
179                for iobj=1:length(ObjectData.SubObject)
180                    hsub=ObjectData.SubObject(iobj);
181                    if isequal(get(hsub,'Type'),'rectangle')
182                        set(hsub,'EdgeColor','m'); %set the selected object to magenta color
183                    elseif isequal(get(hsub,'Type'),'image')
184                       Acolor=get(hsub,'CData');
185                       Acolor(:,:,1)=Acolor(:,:,3);
186                       set(hsub,'CData',Acolor);
187                    else
188                        set(hsub,'Color','m')
189                    end
190                end
191            end
192            if isequal(tag_obj,'DeformPoint')
193                 set(hcurrentobject,'Color','m'); %set the selected DeformPoint to magenta color
194            end
195            IndexObj=ObjectData.IndexObj;
196                    %indicate on the list of the GUI uvmat which object has been selected
197            if strcmp(get(hcurrentfig,'tag'),'uvmat') %if the uvmat graph has been selected, object projection is on the other frame view_field
198                set(hhuvmat.list_object_2,'Value',IndexObj);
199                list_str=get(hhuvmat.list_object_2,'String');
200                UvData.Object{IndexObj}.Name=list_str{IndexObj};
201            else
202                set(hhuvmat.list_object_1,'Value',IndexObj);
203                list_str=get(hhuvmat.list_object_1,'String');
204                UvData.Object{IndexObj}.Name=list_str{IndexObj};
205            end
206            h_set_object=findobj(allchild(0),'Tag','set_object');
207            if ~isempty(h_set_object)
208                delete(h_set_object)
209            end
210            set_object(UvData.Object{IndexObj})
211            axes(haxes);%set back the current axes haxes
212            testdeform=0;
213            set(gcbo,'Pointer','circle');
214            AxeData.Drawing='deform';
215            if isequal(tag_obj,'DeformPoint')       
216               if isfield(ObjectData,'DeformPoint')
217                   set(hcurrentobject,'Selected','on')
218                   for ipt=1:length(ObjectData.DeformPoint)
219                       if isequal(ObjectData.DeformPoint(ipt),hcurrentobject)
220                            AxeData.CurrentIndex=ipt;
221                            testdeform=1;
222                       end
223                   end
224               end
225            end
226            if testdeform==0
227                AxeData.Drawing='translate';
228                set(AxeData.CurrentObject,'Selected','on')
229                set(gcbo,'Pointer','fleur');
230            end
231        end
232    end
233end
234
235%%  create new projection  object
236if  test_create && ~isempty(xy) && ~(isfield(AxeData,'Drawing')&& isequal(AxeData.Drawing,'create'))
237        hset_object=findobj(allchild(0),'tag','set_object');
238        if ~isempty(hset_object)
239            sethandles=guidata(hset_object);
240            ObjectData=read_set_object(sethandles);
241            ObjectData.Coord=[]; %reset previous object coordinates
242            ObjectData.Coord(1,1)=xy(1,1);
243            ObjectData.Coord(1,2)=xy(1,2);
244            ObjectData.Coord(1,3)=0;
245            if isfield(AxeData,'ObjectCoord') & size(AxeData.ObjectCoord,2)==3
246                 ObjectData.Coord(1,3)=AxeData.ObjectCoord(1,3); %generaliser au cas avec angle
247            end
248            AxeData.CurrentObject=plot_object(ObjectData,[],haxes,'m');%draw the object and its handle becomes AxeData.CurrentObject
249            if isfield(UvData,'Object')
250                IndexObj=length(UvData.Object)+1;% add the object as index IndexObj on the list of the interface
251            else
252                IndexObj=2;
253            end 
254            UvData.Object{IndexObj}=ObjectData;       
255            list_str=get(hhuvmat.list_object_1,'String');
256            object_name=get(sethandles.TITLE,'String');
257            if isempty(object_name)|| strcmp(object_name,'')
258                list_str{IndexObj}=[num2str(IndexObj) '-' ObjectData.Style];
259                set(sethandles.TITLE,'String',list_str{IndexObj})
260            else
261               list_str{IndexObj}=object_name;
262            end
263            set(hhuvmat.list_object_1,'String',list_str)
264            list_str{end+1}='...';
265            set(hhuvmat.list_object_2,'String',list_str)
266            if strcmp(fig_tag,'view_field')%we are in view_field plot
267                  set(hhuvmat.list_object_1,'Value',IndexObj)% the projection field will be plotted in uvmat frame
268                  UvData.Object{IndexObj}.DisplayHandle_uvmat=[];
269                  UvData.Object{IndexObj}.DisplayHandle_view_field=AxeData.CurrentObject;       
270            else%we are in uvmat plot
271                set(hhuvmat.list_object_2,'Value',IndexObj)
272                UvData.Object{IndexObj}.DisplayHandle_uvmat=AxeData.CurrentObject;
273                UvData.Object{IndexObj}.DisplayHandle_view_field=[];
274            end
275            set(huvmat,'UserData',UvData)
276            PlotData=get(AxeData.CurrentObject,'UserData');
277            PlotData.IndexObj=IndexObj;
278            set(AxeData.CurrentObject,'UserData',PlotData); %record the object index in the graph
279            AxeData.Drawing='create';
280        end
281end
282
283% create calibration points if the GUI geometry_calib is opened, if the main axes axes3 of uvmat has ben selected
284if ~test_zoom && test_cal && ~isempty(haxes) && strcmp(get(haxes,'tag'),'axes3')
285    h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
286    hh_geometry_calib=guidata(h_geometry_calib);
287    h_edit_append=hh_geometry_calib.edit_append;%findobj(h_geometry_calib,'Tag','edit_append');
288    if isequal(get(h_edit_append,'Value'),1) && ~isempty(haxes)
289        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
290        coord_value=get(hhuvmat.transform_fct,'Value');% set uvmat to pixel coordinates, run it again if not
291        if ~(isequal(coord_value,1)||isequal(coord_value,3)); %active only with no transform or px (no phys)
292            set(hhuvmat.transform_fct,'Value',1)
293            uvmat('transform_fct_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat
294            set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off'
295            set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
296            return
297        end
298        Coord=get(h_ListCoord,'String');
299        data=read_geometry_calib(Coord);%transform char cell to numbers
300        xlim=get(haxes,'XLim');
301        ind_range_x=abs((xlim(2)-xlim(1))/50);
302        ylim=get(haxes,'YLim');
303        ind_range_y=abs((ylim(2)-ylim(1))/50);
304        ind_range=sqrt(ind_range_x*ind_range_y);
305        test_newpoint=1;
306        if size(data.Coord,2)>=5 %if calibration points already exist
307            XCoord=(data.Coord(:,4));
308            YCoord=(data.Coord(:,5));
309            index_point=find((XCoord<xy(1,1)+ind_range) & (XCoord>xy(1,1)-ind_range) & ...%flagx=1 for the vectors with x position selected by the mouse
310                          (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
311            test_newpoint=isempty(index_point);%test for no existing calibration point near the mouse position
312        end
313        val=get(h_ListCoord,'Value');
314        %create a new calib point if we are not close to an existing one
315        if test_newpoint                 
316             strline=[ '    |    '  '    |    '  '    |    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)];
317           
318             if length(Coord)>=val
319                 Coord(val+1:length(Coord)+1)=Coord(val:length(Coord));% push the list forward beyond the current point
320             end
321             Coord{val}=strline;
322             set(h_ListCoord,'String',Coord)
323             data=read_geometry_calib(Coord);%transform char cell to numbers
324             XCoord=data.Coord(:,4);
325             YCoord=data.Coord(:,5);
326        end
327        hh=findobj('Tag','calib_points');%look for handle of calibration points           
328        if isempty(hh)
329            hh=line(XCoord,YCoord,'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
330        else
331            set(hh,'XData',XCoord)
332            set(hh,'YData',YCoord)
333        end
334        set(hh,'UserData',val)% flag the points to edit mode
335        hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
336        if ~isempty(hhh)
337            set(hhh,'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range])
338        else
339            rectangle('Curvature',[1 1],...
340                  'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range],'EdgeColor','m',...
341                  'LineStyle','-','Tag','calib_marker');
342           % line([xy(1,1) xy(1,1)],[xy(1,2) xy(1,2)],'Color','m','Tag','calib_marker','LineStyle','.','Marker','o','MarkerSize',ind_range);
343        end
344        AxeData.Drawing='calibration';
345    end
346end
347
348% edit vectors
349if test_edit_vect && ~isempty(ivec)
350    %create the error flag FF if it does not exist
351    if ~isfield(Field,'FF')
352        Field.ListVarName=[Field.ListVarName 'FF'];
353        Field.VarDimName=[Field.VarDimName Field.VarDimName{VarType{icell}.coord_x}];
354        nbvar=length(Field.ListVarName);
355        Field.VarAttribute{nbvar}.Role='errorflag';
356        Field.FF=zeros(size(Field.X));
357    end
358    if isequal(Field.FF(ivec),0)
359        Field.FF(ivec)=100; %mark vector #ivec as false
360    else
361        Field.FF(ivec)=0;
362    end
363    PlotParam=read_plot_param(hhcurrentfig);
364    plot_field(Field,haxes,PlotParam);
365    eval(['FigData.' tagaxes '=Field;'])%record the modified field in FigData
366    set(hcurrentfig,'UserData',FigData);
367end   
368set(haxes,'UserData',AxeData);
369
Note: See TracBrowser for help on using the repository browser.