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
RevLine 
[11]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)
[67]25
[71]26huvmat=findobj(allchild(0),'tag','uvmat');%find the uvmat interface handle which controls theoption of  mouse action
[60]27if isempty(huvmat)
28    return
[11]29end
[60]30hhuvmat=guidata(huvmat);%handles of elements in uvmat
31UvData=get(huvmat,'UserData');
[187]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);
[156]39test_zoom=get(hhcurrentfig.zoom,'Value');%test for zoom action, first priority
[177]40test_ruler=isequal(get(hhuvmat.MenuRuler,'checked'),'on');%test for ruler  action, second priority;
[156]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
[11]59xdisplay=[];%default
60ydisplay=[];%default
61AxeData=[];%default
62
[177]63%% determine the currently selected items
[11]64hcurrentobject=gco;% current object handle (selected by the mouse)
[187]65%hcurrentfig=hObject;% current figure handle
[62]66fig_tag=get(hcurrentfig,'Tag');
[177]67tag_obj=get(gco,'Tag');%tag of the currently selected object
[11]68xy=[];%default
[187]69xy_fig=get(hObject,'CurrentPoint');% current point of the current figure (gcbo)
70hchild=get(hObject,'Children');%handles of all objects in the current figure
[67]71haxes=[];
[156]72
73%% loop on all the objects in the current figure (selected by the last mouse click)
[11]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')
[78]80            y_lim=get(hchild(ichild),'YLim');
81            x_lim=get(hchild(ichild),'XLim');
[11]82            haxes=hchild(ichild);
83            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
[78]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
[156]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
[78]105                    end
[11]106                end
[78]107            else
108                haxes=[];%mouse out of axes
[11]109            end
110        end
111    end
112end
[187]113
114%% desable  object creation and vector editing if NbDim different from 2
115if ~(isfield(AxeData,'NbDim') && isequal(AxeData.NbDim,2))
[11]116    test_create=0;
[187]117    test_edit_vect=0;   
[11]118end
[187]119
120%% delete the current zoom rectangle
[11]121if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
122    delete(AxeData.CurrentRectZoom)
123    AxeData.CurrentRectZoom=[];
124end   
125
[156]126%% zoom has first priority
[150]127if test_zoom %&& ~test_create && ~test_edit && ~test_edit_vect && exist('xy','var')
[11]128     AxeData.Drawing='zoom'; %initiate drawing mode
129     AxeData.CurrentObject=[];%unselect objects
[71]130     set(haxes,'UserData',AxeData);
[67]131     return
132end
[156]133if isempty(huvmat)%further options require the uvmat GUI
134    return
[67]135end
136
[156]137%% ruler has second priority
[71]138if test_ruler
[156]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');
[71]142    AxeData.Drawing='ruler';
143    set(haxes,'UserData',AxeData);
144    return
145end
146
[156]147%% selection of an existing projection object (third priority)
[67]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');
[102]157        if test_edit && isfield(ObjectData,'IndexObj')
[67]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);
[11]169            end
[67]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')
[11]189                    end
190                end
[67]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;
[71]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);
[102]199                list_str=get(hhuvmat.list_object_2,'String');
200                UvData.Object{IndexObj}.Name=list_str{IndexObj};
[71]201            else
202                set(hhuvmat.list_object_1,'Value',IndexObj);
[102]203                list_str=get(hhuvmat.list_object_1,'String');
204                UvData.Object{IndexObj}.Name=list_str{IndexObj};
[71]205            end
[102]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
[67]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;
[11]222                       end
223                   end
[67]224               end
[11]225            end
[67]226            if testdeform==0
227                AxeData.Drawing='translate';
228                set(AxeData.CurrentObject,'Selected','on')
229                set(gcbo,'Pointer','fleur');
230            end
[11]231        end
232    end
[67]233end
[156]234
235%%  create new projection  object
[67]236if  test_create && ~isempty(xy) && ~(isfield(AxeData,'Drawing')&& isequal(AxeData.Drawing,'create'))
[156]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';
[11]280        end
[67]281end
[11]282
[67]283% create calibration points if the GUI geometry_calib is opened, if the main axes axes3 of uvmat has ben selected
[150]284if ~test_zoom && test_cal && ~isempty(haxes) && strcmp(get(haxes,'tag'),'axes3')
[67]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');
[78]288    if isequal(get(h_edit_append,'Value'),1) && ~isempty(haxes)
[156]289        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
[67]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)
[71]293            uvmat('transform_fct_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat
[67]294            set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off'
295            set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
296            return
[11]297        end
[67]298        Coord=get(h_ListCoord,'String');
[71]299        data=read_geometry_calib(Coord);%transform char cell to numbers
[78]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);
[67]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
[78]313        val=get(h_ListCoord,'Value');
[67]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)];
[78]317           
[67]318             if length(Coord)>=val
[71]319                 Coord(val+1:length(Coord)+1)=Coord(val:length(Coord));% push the list forward beyond the current point
[67]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','+');
[11]330        else
[67]331            set(hh,'XData',XCoord)
332            set(hh,'YData',YCoord)
[11]333        end
[78]334        set(hh,'UserData',val)% flag the points to edit mode
[67]335        hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
336        if ~isempty(hhh)
[71]337            set(hhh,'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range])
[67]338        else
[71]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);
[67]343        end
[71]344        AxeData.Drawing='calibration';
[60]345    end
[11]346end
[67]347
348% edit vectors
[156]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));
[67]357    end
[156]358    if isequal(Field.FF(ivec),0)
[177]359        Field.FF(ivec)=100; %mark vector #ivec as false
[67]360    else
[156]361        Field.FF(ivec)=0;
[67]362    end
[156]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);
[67]367end   
[11]368set(haxes,'UserData',AxeData);
369
Note: See TracBrowser for help on using the repository browser.