source: trunk/src/mouse_down.m @ 296

Last change on this file since 296 was 296, checked in by sommeria, 12 years ago

various

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