www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/@mvgraph4d/linedragcb.m

    function linedragcb(gr,action,line)
%LINEDRAGCB Callback function
%
%  Callback function for graph4d object.  Handles dragging of the
%  range indicator objects on the colourbar.

%  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.


persistent th
switch lower(action)
    case 'buttondown'
        obj = gr.colorbar.([line 'range']);
        set(ancestor(gr.axes,'figure'),'Pointer','top');
        
        bm = xregGui.ButtonUpManager(ancestor(gr.axes,'figure'));
        ud.motionfcn = get(ancestor(gr.axes,'figure'),'WindowButtonMotionFcn');
        
        set(gr.colorbar.userange,'UserData',ud);

        set(obj,'FaceVertexCData',1-get(obj,'FaceVertexCData'));
        
        th=uicontrol('Parent',get(gr.axes,'Parent'),...
            'Style','text',...
            'Visible','off',...
            'BackgroundColor',[1 1 0.6]);

        % easy way to pop up text box immediately
        linedragcb(gr,'motion',line);
        set(th,'Visible','on');
        
        set(ancestor(gr.axes,'figure'),'WindowButtonMotionFcn',{@i_motionfcn,gr,line});
        bm.getNextEvent({@i_btnupfcn,gr,line}, true);
        
    case 'buttonup'
        obj=gr.colorbar.([line 'range']);
        ud=get(gr.colorbar.userange,'UserData');

        set(obj,'FaceVertexCData',1-get(obj,'FaceVertexCData'));
        set(ancestor(gr.axes,'figure'),'WindowButtonMotionFcn',ud.motionfcn);

        delete(th);
        set(ancestor(gr.axes,'figure'),'Pointer','arrow');

        % update axes
        pr_plot(gr);

    case 'motion'
        % get current cursor point from figure
        currPoint=get(ancestor(gr.axes,'figure'),'CurrentPoint');
        currPoint = mbcgui.util.convertLocation(currPoint, get(gr.axes, 'Parent'));
        
        % translate to a data point in the colourbar axes!
        cbpos=get(gr.colorbar.axes,'Position');
        ylim=get(gr.colorbar.axes,'YLim');
        switch lower(line)
            case 'min'
                % need to drag min to position of cursor. Special cases are:
                %    (1)  if min is dragged above max, it stops at max
                %    (2)  if min is dragged below axes, it stops at bottom of axes
                % Note that the midrange object needs to be moved too to keep it in the middle

                axcp=(ylim(2)-ylim(1))*(currPoint(2)-cbpos(2))/cbpos(4)+ylim(1);
                % do checks to limit axcp if necessary
                if axcp<0.5
                    axcp=0.5;
                end
                cmax = gr.DataPointer.info.rangepositions(3);
                if axcp>cmax
                    axcp=cmax;
                end
                strnum=axcp;
                % reset position of line
                % work out delta
                delta=2*(ylim(2)-ylim(1))/(cbpos(4));
                newvert=[0 axcp+delta/4; 0.5 axcp+2*delta; 1 axcp+delta/4;....
                    0 axcp-delta/4; 0.5 axcp-2*delta; 1 axcp-delta/4];
                set(gr.colorbar.minrange,'Vertices',newvert);
                gr.DataPointer.info.rangepositions(1) = axcp;
                % move midrange bar
                axcp=(cmax+axcp)/2;
                newvert=[0 axcp+delta/4; 0.5 axcp+2*delta; 1 axcp+delta/4;....
                    0 axcp-delta/4; 0.5 axcp-2*delta; 1 axcp-delta/4];
                set(gr.colorbar.midrange,'Vertices',newvert);
                gr.DataPointer.info.rangepositions(2) = axcp;
            case 'mid'
                midcp=(ylim(2)-ylim(1))*(currPoint(2)-cbpos(2))/cbpos(4)+ylim(1);
                % do checks to limit axcp if necessary
                rp = gr.DataPointer.info.rangepositions;
                oldmin = rp(1);
                oldmax = rp(3);

                mincp=midcp-(oldmax-oldmin)/2;
                maxcp=midcp+(oldmax-oldmin)/2;

                if mincp<ylim(1)
                    % push maxcp back up
                    maxcp=maxcp+ylim(1)-mincp;
                    mincp=ylim(1);
                end
                if maxcp>ylim(2)
                    % push mincp back down
                    mincp=mincp-maxcp+ylim(2);
                    maxcp=ylim(2);
                end

                % rework midcp according to max and min
                midcp=(mincp+maxcp)/2;
                strnum=midcp;
                delta=2*(ylim(2)-ylim(1))/(cbpos(4));
                newvert=[0 mincp+delta/4; 0.5 mincp+2*delta; 1 mincp+delta/4;....
                    0 mincp-delta/4; 0.5 mincp-2*delta; 1 mincp-delta/4];
                set(gr.colorbar.minrange,'Vertices',newvert);
                newvert=[0 midcp+delta/4; 0.5 midcp+2*delta; 1 midcp+delta/4;....
                    0 midcp-delta/4; 0.5 midcp-2*delta; 1 midcp-delta/4];
                set(gr.colorbar.midrange,'Vertices',newvert);
                newvert=[0 maxcp+delta/4; 0.5 maxcp+2*delta; 1 maxcp+delta/4;....
                    0 maxcp-delta/4; 0.5 maxcp-2*delta; 1 maxcp-delta/4];
                set(gr.colorbar.maxrange,'Vertices',newvert);
                gr.DataPointer.info.rangepositions = [mincp midcp maxcp];
            case 'max'
                axcp=(ylim(2)-ylim(1))*(currPoint(2)-cbpos(2))/cbpos(4)+ylim(1);
                % do checks to limit axcp if necessary
                if axcp>ylim(2)
                    axcp=ylim(2);
                end
                cmin = gr.DataPointer.info.rangepositions(1);
                if axcp<cmin
                    axcp=cmin;
                end
                strnum=axcp;
                % reset position of line
                % work out delta
                delta=2*(ylim(2)-ylim(1))/(cbpos(4));
                newvert=[0 axcp+delta/4; 0.5 axcp+2*delta; 1 axcp+delta/4;....
                    0 axcp-delta/4; 0.5 axcp-2*delta; 1 axcp-delta/4];
                set(gr.colorbar.maxrange,'Vertices',newvert);
                gr.DataPointer.info.rangepositions(3) = axcp;
                % move midrange bar
                axcp=(cmin+axcp)/2;
                newvert=[0 axcp+delta/4; 0.5 axcp+2*delta; 1 axcp+delta/4;....
                    0 axcp-delta/4; 0.5 axcp-2*delta; 1 axcp-delta/4];
                set(gr.colorbar.midrange,'Vertices',newvert);
                gr.DataPointer.info.rangepositions(2) = axcp;
        end

        % redraw text box
        if isgraphics(th)
            % need to scale number to actual values.
            clim=get(gr.axes,'CLim');
            strnum=clim(1)+(clim(2)-clim(1)).*strnum./(ylim(2)-ylim(1));
            tstr=sprintf('%5.3f',strnum);
            set(th,'String',tstr);
            % set position of tooltip
            tExtent = th.Extent;
            tWidth = tExtent(3);
            tHeight = tExtent(4);
            tpos(1)=currPoint(1)-(tWidth+5);
            tpos(2)=currPoint(2);
            tpos(3)=tWidth;
            tpos(4)=tHeight;
            set(th,'Position',tpos)
        end
end


function i_motionfcn(src,evt,gr,item)
linedragcb(gr,'motion',item);



function i_btnupfcn(src,evt,gr,item)
linedragcb(gr,'buttonup',item);