source: trunk/src/mouse_down.m @ 379

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

several bugs corrected
set_object.fig rationalized so that read_set_object is replaced by the rgeneral fct read_GUI.

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        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%                 list_str=get(hhuvmat.list_object_2,'String');
256%                 UvData.Object{IndexObj(2)}.Name=list_str{IndexObj};
257            else
258                set(hhuvmat.ListObject,'Value',IndexObj);
259                list_str=get(hhuvmat.ListObject,'String');
260                UvData.Object{IndexObj}.Name=list_str{IndexObj};
261            end
262            h_set_object=findobj(allchild(0),'Tag','set_object');
263            if ~isempty(h_set_object)
264                delete(h_set_object)
265            end
266            set_object(UvData.Object{IndexObj})
267            axes(hchild);%set back the current axes haxes
268            testdeform=0;
269            set(gcbo,'Pointer','circle');
270            AxeData.Drawing='deform';
271            if isequal(tag_obj,'DeformPoint')       
272               if isfield(ObjectData,'DeformPoint')
273                   set(hcurrentobject,'Selected','on')
274                   for ipt=1:length(ObjectData.DeformPoint)
275                       if isequal(ObjectData.DeformPoint(ipt),hcurrentobject)
276                            AxeData.CurrentIndex=ipt;
277                            testdeform=1;
278                       end
279                   end
280               end
281            end
282            if testdeform==0
283                AxeData.Drawing='translate';
284                set(AxeData.CurrentObject,'Selected','on')
285                set(gcbo,'Pointer','fleur');
286            end
287        end
288    end
289end
290
291%%  create new projection  object
292if  test_create && ~isempty(xy) && ~(isfield(AxeData,'Drawing')&& isequal(AxeData.Drawing,'create'))
293        hset_object=findobj(allchild(0),'tag','set_object');
294        if ~isempty(hset_object)
295            %ObjectData=read_set_object(sethandles); %read object features in the GUI set_object
296            ObjectData=read_GUI(hset_object);
297            ObjectData.Coord=[]; %reset previous object coordinates
298            ObjectData.Coord(1,1)=xy(1,1);
299            ObjectData.Coord(1,2)=xy(1,2);
300%             ObjectData.Coord(1,3)=0;
301            if isfield(AxeData,'ObjectCoord') & size(AxeData.ObjectCoord,2)==3
302                 ObjectData.Coord(1,3)=AxeData.ObjectCoord(1,3); %generaliser au cas avec angle
303            end
304            AxeData.CurrentObject=plot_object(ObjectData,[],haxes,'m');%draw the object and its handle becomes AxeData.CurrentObject
305            if isfield(UvData,'Object')
306                IndexObj=length(UvData.Object)+1;% add the object as index IndexObj on the list of the interface
307            else
308                IndexObj=2;
309            end 
310            UvData.Object{IndexObj}=ObjectData;       
311            list_str=get(hhuvmat.ListObject,'String');
312            IndexObj_old=get(hhuvmat.ListObject,'Value');
313            set(hhuvmat.ListObject,'Value',[IndexObj_old(1) IndexObj] );
314            UvData.Object{IndexObj}.DisplayHandle_uvmat=AxeData.CurrentObject;
315            object_name=ObjectData.Name;
316            sethandles=guidata(hset_object);
317            if isempty(object_name)
318                list_str{IndexObj}=[num2str(IndexObj) '-' ObjectData.Type];
319                set(sethandles.Name,'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.