source: trunk/src/mouse_down.m @ 323

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

many bugs corrected, cleaning of civ.m, resize of the GUI civ

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