source: trunk/src/mouse_down.m @ 356

Last change on this file since 356 was 342, checked in by sommeria, 9 years ago

various bugs corrected

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