source: trunk/src/mouse_down.m @ 302

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

improve the display of projection objects

File size: 21.1 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'),'ListObject')
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                IndexObj_old=get(hhuvmat.ListObject,'Value');
239                if IndexObj>IndexObj_old(1)
240                    IndexObj=[IndexObj_old(1) IndexObj];
241                else
242                    IndexObj=[1 IndexObj];
243                end
244                set(hhuvmat.ListObject,'Value',IndexObj);
245                set(hhuvmat.ListObject,'UserData',IndexObj);
246%                 list_str=get(hhuvmat.list_object_2,'String');
247%                 UvData.Object{IndexObj(2)}.Name=list_str{IndexObj};
248            else
249                set(hhuvmat.ListObject,'Value',IndexObj);
250                list_str=get(hhuvmat.ListObject,'String');
251                UvData.Object{IndexObj}.Name=list_str{IndexObj};
252            end
253            h_set_object=findobj(allchild(0),'Tag','set_object');
254            if ~isempty(h_set_object)
255                delete(h_set_object)
256            end
257            set_object(UvData.Object{IndexObj})
258            axes(hchild);%set back the current axes haxes
259            testdeform=0;
260            set(gcbo,'Pointer','circle');
261            AxeData.Drawing='deform';
262            if isequal(tag_obj,'DeformPoint')       
263               if isfield(ObjectData,'DeformPoint')
264                   set(hcurrentobject,'Selected','on')
265                   for ipt=1:length(ObjectData.DeformPoint)
266                       if isequal(ObjectData.DeformPoint(ipt),hcurrentobject)
267                            AxeData.CurrentIndex=ipt;
268                            testdeform=1;
269                       end
270                   end
271               end
272            end
273            if testdeform==0
274                AxeData.Drawing='translate';
275                set(AxeData.CurrentObject,'Selected','on')
276                set(gcbo,'Pointer','fleur');
277            end
278        end
279    end
280end
281
282%%  create new projection  object
283if  test_create && ~isempty(xy) && ~(isfield(AxeData,'Drawing')&& isequal(AxeData.Drawing,'create'))
284        hset_object=findobj(allchild(0),'tag','set_object');
285        if ~isempty(hset_object)
286            sethandles=guidata(hset_object);
287            ObjectData=read_set_object(sethandles); %read object features in the GUI set_object
288            ObjectData.Coord=[]; %reset previous object coordinates
289            ObjectData.Coord(1,1)=xy(1,1);
290            ObjectData.Coord(1,2)=xy(1,2);
291            ObjectData.Coord(1,3)=0;
292            if isfield(AxeData,'ObjectCoord') & size(AxeData.ObjectCoord,2)==3
293                 ObjectData.Coord(1,3)=AxeData.ObjectCoord(1,3); %generaliser au cas avec angle
294            end
295            AxeData.CurrentObject=plot_object(ObjectData,[],haxes,'m');%draw the object and its handle becomes AxeData.CurrentObject
296            if isfield(UvData,'Object')
297                IndexObj=length(UvData.Object)+1;% add the object as index IndexObj on the list of the interface
298            else
299                IndexObj=2;
300            end 
301            UvData.Object{IndexObj}=ObjectData;       
302            list_str=get(hhuvmat.ListObject,'String');
303            IndexObj_old=get(hhuvmat.ListObject,'Value');
304            set(hhuvmat.ListObject,'Value',[IndexObj_old(1) IndexObj] );
305            UvData.Object{IndexObj}.DisplayHandle_uvmat=AxeData.CurrentObject;
306            object_name=get(sethandles.TITLE,'String');
307            if isempty(object_name)|| strcmp(object_name,'')
308                list_str{IndexObj}=[num2str(IndexObj) '-' ObjectData.Style];
309                set(sethandles.TITLE,'String',list_str{IndexObj})
310            else
311               list_str{IndexObj}=object_name;
312            end
313            set(hhuvmat.ListObject,'String',list_str)
314            UvData.Object{IndexObj}.DisplayHandle_view_field=AxeData.CurrentObject;
315            %list_str{end+1}='...';
316%             set(hhuvmat.list_object_2,'String',list_str)
317%             if strcmp(fig_tag,'view_field')%we are in view_field plot
318%                   set(hhuvmat.list_object_1,'Value',IndexObj)% the projection field will be plotted in uvmat frame
319%                   UvData.Object{IndexObj}.DisplayHandle_uvmat=[];
320%                   UvData.Object{IndexObj}.DisplayHandle_view_field=AxeData.CurrentObject;       
321%             else%we are in uvmat plot
322%                 set(hhuvmat.list_object_2,'Value',IndexObj)
323%                 UvData.Object{IndexObj}.DisplayHandle_uvmat=AxeData.CurrentObject;
324%                 UvData.Object{IndexObj}.DisplayHandle_view_field=[];
325%             end
326            set(huvmat,'UserData',UvData)
327            PlotData=get(AxeData.CurrentObject,'UserData');
328            PlotData.IndexObj=IndexObj;
329            set(AxeData.CurrentObject,'UserData',PlotData); %record the object index in the graph
330            AxeData.Drawing='create';
331        end
332end
333
334% create calibration points if the GUI geometry_calib is opened, if the main axes axes3 of uvmat has ben selected
335if ~test_zoom && test_cal && ~isempty(haxes) && strcmp(get(haxes,'tag'),'axes3')
336    h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
337    hh_geometry_calib=guidata(h_geometry_calib);
338    h_edit_append=hh_geometry_calib.edit_append;%findobj(h_geometry_calib,'Tag','edit_append');
339    if isequal(get(h_edit_append,'Value'),1) && ~isempty(haxes)
340        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
341        coord_value=get(hhuvmat.transform_fct,'Value');% set uvmat to pixel coordinates, run it again if not
342        if ~(isequal(coord_value,1)||isequal(coord_value,3)); %active only with no transform or px (no phys)
343            set(hhuvmat.transform_fct,'Value',1)
344            uvmat('transform_fct_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat
345            set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off'
346            set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
347            return
348        end
349        Coord=get(h_ListCoord,'String');
350        data=read_geometry_calib(Coord);%transform char cell to numbers
351        xlim=get(haxes,'XLim');
352        ind_range_x=abs((xlim(2)-xlim(1))/50);
353        ylim=get(haxes,'YLim');
354        ind_range_y=abs((ylim(2)-ylim(1))/50);
355        ind_range=sqrt(ind_range_x*ind_range_y);
356        test_newpoint=1;
357        if size(data.Coord,2)>=5 %if calibration points already exist
358            XCoord=(data.Coord(:,4));
359            YCoord=(data.Coord(:,5));
360            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
361                          (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
362            test_newpoint=isempty(index_point);%test for no existing calibration point near the mouse position
363        end
364        val=get(h_ListCoord,'Value');
365        %create a new calib point if we are not close to an existing one
366        if test_newpoint                 
367             strline=[ '    |    '  '    |    '  '    |    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)];
368           
369             if length(Coord)>=val
370                 Coord(val+1:length(Coord)+1)=Coord(val:length(Coord));% push the list forward beyond the current point
371             end
372             Coord{val}=strline;
373             set(h_ListCoord,'String',Coord)
374             data=read_geometry_calib(Coord);%transform char cell to numbers
375             XCoord=data.Coord(:,4);
376             YCoord=data.Coord(:,5);
377        end
378        hh=findobj('Tag','calib_points');%look for handle of calibration points           
379        if isempty(hh)
380            hh=line(XCoord,YCoord,'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
381        else
382            set(hh,'XData',XCoord)
383            set(hh,'YData',YCoord)
384        end
385        set(hh,'UserData',val)% flag the points to edit mode
386        hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
387        if ~isempty(hhh)
388            set(hhh,'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range])
389        else
390            rectangle('Curvature',[1 1],...
391                  'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range],'EdgeColor','m',...
392                  'LineStyle','-','Tag','calib_marker');
393           % line([xy(1,1) xy(1,1)],[xy(1,2) xy(1,2)],'Color','m','Tag','calib_marker','LineStyle','.','Marker','o','MarkerSize',ind_range);
394        end
395        AxeData.Drawing='calibration';
396    end
397end
398
399% edit vectors
400if test_edit_vect && ~isempty(ivec)
401    %create the error flag FF if it does not exist
402    if ~isfield(Field,'FF')
403        Field.ListVarName=[Field.ListVarName 'FF'];
404        Field.VarDimName=[Field.VarDimName Field.VarDimName{VarType{icell}.coord_x}];
405        nbvar=length(Field.ListVarName);
406        Field.VarAttribute{nbvar}.Role='errorflag';
407        Field.FF=zeros(size(Field.X));
408    end
409    if isequal(Field.FF(ivec),0)
410        Field.FF(ivec)=100; %mark vector #ivec as false
411    else
412        Field.FF(ivec)=0;
413    end
414    PlotParam=read_GUI(hcurrentfig);
415    plot_field(Field,haxes,PlotParam);
416    eval(['FigData.' tagaxes '=Field;'])%record the modified field in FigData
417    set(hcurrentfig,'UserData',FigData);
418end   
419set(haxes,'UserData',AxeData);
420
Note: See TracBrowser for help on using the repository browser.