source: trunk/src/mouse_down.m @ 243

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

various bug corrections

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