www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/@xregcolorbar/xregcolorbar.m
function gr=xregcolorbar(varargin) %XREGCOLORBAR/XREGCOLORBAR XREGCOLORBAR constructor function % Constructor function for a XREGCOLORBAR object. % Usage: % GR=XREGCOLORBAR % GR=XREGCOLORBAR(FIG) % GR=XREGCOLORBAR('Property1',Value1,...) % GR=XREGCOLORBAR(FIG,'Property1',Value1,...) % Copyright 2000-2016 The MathWorks, Inc. and Ford Global Technologies, Inc. if nargin>0 && mbcgui.util.isComponentParent(varargin{1}) hParent=varargin{1}; varargin(1) = []; else hParent=gcf; end hFig = ancestor(hParent, 'figure'); % Store data in: % object - colorbar.axes % data - colorbar.frame1 % factors - colorbar.frame2 % visibility - userange % position - cfactor % ctext - various: axes clim, plot indices etc % callback,mouseID - patch % initial position pos=[30 30 100 300]; ud = []; ud.MousePtrStackID = []; ud.callback = []; ud.setcallback = 1; gr.patch=uipanel(... 'Parent', hParent, ... 'Units','pixels',... 'Position',pos,... 'Visible', 'off', ... 'BorderType', 'line', ... 'HighlightColor', 'k', ... 'BackgroundColor', 'w', ... 'Tag','xregcolorbar_obj', ... 'UserData', ud); cmap=get(hFig,'Colormap'); gr.colorbar.axes=axes(... 'Parent',gr.patch,... 'Units','pixels',... 'Position',[pos(1)+pos(3)-60 pos(2)+100 20 pos(4)-125],... 'Layer','top',... 'YLim',[0.5 size(cmap,1)+0.5],... 'XLim',[0 1],... 'XTick',[],... 'Box','on',... 'YAxisLocation','right'); clim=get(gr.colorbar.axes,'CLim'); gr.colorbar.bar=patch(... 'Parent',gr.colorbar.axes,... 'Vertices',[0 0 0;0 1 0;1 1 0;1 0 0],... 'FaceVertexCData',0,... 'Faces',[1 2 3 4],... 'EdgeColor','none',... 'FaceColor','flat',... 'Tag','cbar',... 'UserData',get(hFig,'Color')); % need to give patches a width , otherwise the buttondown fcn never fires. clen=size(cmap,1); delta=2*(clen)/(pos(4)-125); gr.colorbar.midrange=patch(... 'Parent',gr.colorbar.axes,... 'Visible','off',... 'Vertices',[0 (clen/2)+delta/4 1; 0.5 (clen/2)+2*delta 1; 1 (clen/2)+delta/4 1;... 0 (clen/2)-delta/4 1; 0.5 (clen/2)-2*delta 1; 1 (clen/2)-delta/4 1],... 'Faces',[1 2 5 4;2 3 6 5],... 'FaceVertexCData',[1 1 0.7;0.7 0 0;1 1 0.7;1 1 0.7;0.7 0 0;1 1 0.7],... 'FaceColor','interp',... 'EdgeColor','none',... 'UserData',clen/2,... 'Interruptible','off',... 'Tag','midbar'); gr.colorbar.minrange=patch(... 'Parent',gr.colorbar.axes,... 'Visible','off',... 'Vertices',[0 (3*clen/8)+delta/4; 0.5 (3*clen/8)+2*delta; 1 (3*clen/8)+delta/4;... 0 (3*clen/8)-delta/4; 0.5 (3*clen/8)-2*delta; 1 (3*clen/8)-delta/4],... 'Faces',[1 2 5 4;2 3 6 5],... 'FaceVertexCData',[1 1 0.7;0 0 0.7;1 1 0.7;1 1 0.7;0 0 0.7;1 1 0.7],... 'FaceColor','interp',... 'EdgeColor','none',... 'UserData',3*clen/8,... 'Interruptible','off',... 'Tag','minbar'); gr.colorbar.maxrange=patch(... 'Parent',gr.colorbar.axes,... 'Visible','off',... 'Vertices',[0 (5*clen/8)+delta/4; 0.5 (5*clen/8)+2*delta; 1 (5*clen/8)+delta/4;... 0 (5*clen/8)-delta/4; 0.5 (5*clen/8)-2*delta; 1 (5*clen/8)-delta/4],... 'Faces',[1 2 5 4;2 3 6 5],... 'FaceVertexCData',[1 1 0.7;0 0 0.7;1 1 0.7;1 1 0.7;0 0 0.7;1 1 0.7],... 'FaceColor','interp',... 'EdgeColor','none',... 'UserData',5*clen/8,... 'Interruptible','off',... 'Tag','maxbar'); gr.colorbar.frame1=uicontrol(... 'Style','frame',... 'Parent',gr.patch,... 'Position',[pos(1)+pos(3)-51 pos(2)+90 2 10],... 'Enable','inactive'); gr.colorbar.frame2=uicontrol(... 'Style','frame',... 'Parent',gr.patch,... 'Position',[pos(1)+pos(3)-86 pos(2)+89 72 1],... 'Enable','inactive'); gr.colorbar.userange=uicontrol(... 'Style','checkbox',... 'Parent',gr.patch,... 'Position',[pos(1)+pos(3)-86 pos(2)+65 72 24],... 'BackgroundColor','w',... 'UserData','on',... 'String','Limit range'); % set up labels according to color limits in axis labpoints=get(gr.colorbar.axes,'YTick'); actpoints=(labpoints-1)./(clen-1); set(gr.colorbar.axes,'YTickLabel',cellstr(num2str(actpoints','%3.2f'))); gr.cfactor=uicontrol(... 'Style','popupmenu',... 'Parent',gr.patch,... 'Position',[pos(1)+4*pos(3)/5-35 pos(2)+10 70 20],... 'String',' ',... 'BackgroundColor','w',... 'UserData',pos,... 'Tag','4'); ud = []; ud.clim = clim; ud.limitstyle = 'exclude'; ud.limitenable = 1; ud.cmax = []; ud.cmin = []; ud.coldata = []; ud.inds = []; gr.ctext=uicontrol(... 'Style','text',... 'Parent',gr.patch,... 'Position',[pos(1)+4*pos(3)/5-35 pos(2)+32 70 16],... 'String','Color by:',... 'BackgroundColor','w',... 'UserData',ud,... 'Enable','inactive'); gr.MouseMotion = []; % set up faces for colorbar pr_cbarfaces(gr.colorbar.bar,cmap,gr); gr=class(gr,'xregcolorbar'); % set up callbacks with object set(gr.colorbar.userange,'Callback',{@chboxcb,gr}); cb = linedragcb(gr); gr.colorbar.linebupcb = cb.ButtonUp; gr.colorbar.linemotioncb = cb.Motion; set(gr.colorbar.maxrange,'ButtonDownFcn',{cb.ButtonDown,gr,'max'}); set(gr.colorbar.minrange,'ButtonDownFcn',{cb.ButtonDown,gr,'min'}); set(gr.colorbar.midrange,'ButtonDownFcn',{cb.ButtonDown,gr,'mid'}); set(gr.colorbar.bar,'ButtonDownFcn',{@cmapcb,gr}); set(gr.cfactor,'Callback',{@i_plot,gr}); mcb = @i_setMousePtr; % Create some motion managers gr.MouseMotion = [... mbcgui.motion.MotionManager('MouseInFcn',{mcb,gr,1},... 'MouseOutFcn',{mcb,gr,0}),... mbcgui.motion.MotionManager('MouseInFcn',{mcb,gr,1},... 'MouseOutFcn',{mcb,gr,0}),... mbcgui.motion.MotionManager('MouseInFcn',{mcb,gr,1},... 'MouseOutFcn',{mcb,gr,0}),... mbcgui.motion.MotionManager('ExternalRef',gr.colorbar.axes,'UseExternalRef','on')]; mm=MotionManager(gr.patch); mm.RegisterManager(gr.MouseMotion(4)); for n=1:3 gr.MouseMotion(4).RegisterManager(gr.MouseMotion(n)); end % save an object handle in the axes for later use. builtin('set',gr.colorbar.axes,'userdata',gr); % set extra props if specified if ~isempty(varargin) % Set properties that are passed in gr=set(gr,'Visible','on',varargin{:}); else set(gr.patch,'Visible','on'); end %------------------------------------------- function chboxcb(src,event,gr) %------------------------------------------- %XREGCOLORBAR/CHBOXCB Callback function % set(gr,'userange',get(gr.colorbar.userange,'Value')); ud = get(gr.patch,'UserData'); cb = ud.callback; vis = get(gr.colorbar.userange,'UserData'); if ~isempty(cb) && strcmp(vis,'on') && ~ud.setcallback % Ensure callback fired once. xregcallback(cb); end %------------------------------------------- function cmapcb(src,event,gr) %------------------------------------------- %CMAPCB Callback function % callback function for interactively chnaging colormap in image % display of xregcolorbar object. Uses UISETCOLORMAP. figh = ancestor(gr.patch,'figure'); % look for a double click if strcmpi(get(figh,'SelectionType'),'open') PR = xregGui.PointerRepository; pointerID = PR.stackSetPointer(figh,'watch'); cmapnow=get(gr.colorbar.bar,'FaceVertexCData'); % pop up ui for colormap cmap=uisetcolormap(cmapnow,NaN); if cmap==0 % User pressed cancel set(figh,'Pointer','arrow'); return end % Apply colormap to object set(gr,'Colormap',cmap); PR = xregGui.PointerRepository; PR.stackRemovePointer(figh,pointerID); end return %------------------------------------------- function i_plot(src,event,gr) %------------------------------------------- pr_graphlim(gr); pr_plot(gr); %------------------------------------------- function i_setMousePtr(src,event,gr,region) %------------------------------------------- % SETMOUSEPTR Set the correct mouse pointer % % obj.setMousePtr(region) % where region =: % 0 - Mouse out % 1 - Up/Down fH=ancestor(gr.patch,'figure'); pr=xregGui.PointerRepository; ud = get(gr.patch,'UserData'); ID=ud.MousePtrStackID; switch region case 0 if ~isempty(ID) pr.stackRemovePointer(fH,ID); ud.MousePtrStackID=[]; end case 1 % switch to new pointer ud.MousePtrStackID= pr.stackSetPointer(fH,'uddrag'); % if we were in old region, remove this one form underneath new one i_rempointer(pr,fH,ID); end set(gr.patch,'UserData',ud); function i_rempointer(pr,fH,ID) if ~isempty(ID) pr.stackRemovePointer(fH,ID); end return