source: trunk/src/mouse_down.m @ 402

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

bugs corrected and improved procedure for object projection

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