source: trunk/src/mouse_down.m @ 183

Last change on this file since 183 was 177, checked in by sommeria, 14 years ago

bug with mouse object editing resolved. Display feature 'satus' for PIV task advancement introduced. Various bug repair and cleaning

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