www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/+mbcgui/+widget/IntervalPatch1D.m
classdef IntervalPatch1D < matlab.mixin.SetGet & matlab.mixin.Copyable %mbcgui.widget.IntervalPatch1D class % xregGui.intervalPatch1D properties: % OpenIntervals - Property is of type 'MATLAB array' % Orientation - Property is of type 'xregGui.horz/vert enumeration: {'horizontal','vertical'}' % DisplayName - String used as display name for patch % Parent - Parent object of patch % Visible - Property indicating if patch is visible ('on'/'off') % HitTest - Property indicating if patch can be clicked ('on'/'off') % FaceColor - Vector of 3 elements representing the color of patch face % EdgeColor - Vector of 3 elements representing the color of patch edge % Tag - String tag used to find patch % % mbcgui.widget.IntervalPatch1D methods: % convertConstraintVector - Create intervals from constraint information. % setConstraintVector - Set intervals from constraint information. % Copyright 2015-2016 The MathWorks, Inc. and Ford Global Technologies, Inc. properties (AbortSet) %ORIENTATION Property is of type 'xregGui.horz/vert enumeration: {'horizontal','vertical'}' Orientation = 'horizontal'; end properties %OPENINTERVALS Property is of type 'MATLAB array' OpenIntervals = []; end properties (Dependent) %PARENT Parent object of patch Parent; %VISIBLE Property indicating if patch is visible ('on'/'off') Visible; %HITTEST Property indicating if patch can be clicked ('on'/'off') HitTest; %FACECOLOR Vector of 3 elements representing the color of patch face FaceColor; %EDGECOLOR Vector of 3 elements representing the color of patch edge EdgeColor; %TAG String tag used to find patch Tag; %Clipping clip patches Clipping %DISPLAYNAME String used as display name for patch DisplayName = ''; end properties (Access=protected, AbortSet) %IPLISTENERS Property is of type 'MATLAB array' IPListeners = []; %HPATCH Property is of type 'MATLAB array' hPatch = []; end methods % constructor block function obj = IntervalPatch1D(varargin) %INTERVALPATCH1D Constructor for IntervalPatch1D object % % OBJ = INTERVALPATCH1D(PROP, VAL, ...) obj.hPatch = patch('Parent', [], ... 'XLimInclude', 'off', ... 'YLimInclude', 'off', ... 'ZLimInclude', 'off', ... 'Visible', 'on', ... 'HitTest', 'off', ... 'FaceColor', mbcbdrycolor, ... 'EdgeColor', [0,0,0], ... 'Vertices', [], ... 'Tag', '', ... 'Faces', []); if nargin set(obj, varargin{:}); end if isempty(obj.Parent) obj.Parent = gca; end obj.pDoIntervals end % IntervalPatch1D end % constructor block methods % dependent properties function set.Parent(obj,value) % DataType = 'on/off' obj.hPatch.Parent = value; obj.pSetupIP; end function value = get.Parent(obj) % DataType = 'on/off' value = obj.hPatch.Parent; end function set.Visible(obj,value) % DataType = 'on/off' obj.hPatch.Visible = value; end function value = get.Visible(obj) % DataType = 'on/off' value = obj.hPatch.Visible; end function set.HitTest(obj,value) % DataType = 'on/off' obj.hPatch.HitTest = value; end function value = get.HitTest(obj) % DataType = 'on/off' value = obj.hPatch.HitTest; end function set.FaceColor(obj,value) % DataType = 'color' obj.hPatch.FaceColor = value; end function value = get.FaceColor(obj) % DataType = 'on/off' value = obj.hPatch.FaceColor; end function set.EdgeColor(obj,value) % DataType = 'color' obj.hPatch.EdgeColor = value; end function value = get.EdgeColor(obj) % DataType = 'color' value = obj.hPatch.EdgeColor; end function set.Tag(obj,value) % DataType = 'string' obj.hPatch.Tag = value; end function value = get.Tag(obj) % DataType = 'string' value = obj.hPatch.Tag; end % non-dependent properties function set.OpenIntervals(obj,value) obj.OpenIntervals = i_CheckVector(value); pDoIntervals(obj); end function set.Orientation(obj,value) % Enumerated DataType = 'xregGui.horz/vert enumeration: {'horizontal','vertical'}' value = validatestring(value,{'horizontal','vertical'},'','Orientation'); obj.Orientation = value; pDoIntervals(obj); end function set.DisplayName(obj,value) % DataType = 'string' obj.hPatch.DisplayName = value; end function value = get.DisplayName(obj) value = obj.hPatch.DisplayName; end function set.Clipping(obj,value) obj.hPatch.Clipping = value; end function value = get.Clipping(obj) value = obj.hPatch.Clipping; end end % set and get functions methods % public methods %---------------------------------------- function setConstraintVector(obj, xdata, condata) %SETCONSTRAINTVECTOR Set intervals from constraint information. % % SETCONSTRAINTVECTOR(OBJ, X, CONDATA) updates the open intervals in OBJ % from the values in X and the associated logical constraint information, % CONDATA. int = xregGui.intervalPatch1D.convertConstraintVector(xdata, condata); obj.OpenIntervals = int; end % setConstraintVector function delete(obj) %delete when deleting the IntervalPatch1D delete the patch if ~mbcgui.util.isBeingDestroyed(obj.hPatch) delete(obj.hPatch); end end end % public methods methods (Access=private) %---------------------------------------- function pDoIntervals(obj) %PDOINTERVALS Private method to draw the object % % PDOINTERVALS(OBJ) if isempty(obj.OpenIntervals) || (isscalar(obj.OpenIntervals) && isnan(obj.OpenIntervals)) set(obj.hPatch, 'Vertices', [], 'Faces', []); else hAx = ancestor(obj.hPatch, 'axes'); xL = mbcmakelimits(get(hAx, 'XLim'), 'loose'); yL = mbcmakelimits(get(hAx, 'YLim'), 'loose'); if strcmp(obj.Orientation(1), 'v') % Swap X and Y limits for vertical intervals tmp = xL; xL = yL; yL = tmp; end redout = [xL(1), obj.OpenIntervals, xL(2); ... xL(1), obj.OpenIntervals, xL(2)]; Ndisallowed = size(redout,2)*0.5; verts = zeros(Ndisallowed*4,3); verts(:,3) = -1; if strcmp(obj.Orientation(1), 'h') verts(:,2) = repmat([yL(1); yL(2); yL(2); yL(1)],Ndisallowed,1); verts(:,1) = redout(:); else verts(:,1) = repmat([yL(1); yL(2); yL(2); yL(1)],Ndisallowed,1); verts(:,2) = redout(:); end faces = reshape(1:size(verts,1),4, Ndisallowed)'; set(obj.hPatch, 'Vertices', verts, 'Faces', faces); end end % pDoIntervals %---------------------------------------- function pSetupIP(obj) %PSETUPIP Private method to setup listeners % % PSETUPIP(OBJ) hAx = ancestor(obj.Parent, 'axes'); obj.IPListeners = { ... mbcgui.hgclassesutil.proplistener(hAx, 'XLim', 'PostSet', mbcutils.callback(@i_setlims, obj)); ... mbcgui.hgclassesutil.proplistener(hAx, 'YLim', 'PostSet', mbcutils.callback(@i_setlims, obj)); ... mbcgui.hgclassesutil.listener(obj.hPatch, 'ObjectBeingDestroyed', mbcutils.callback(@i_delself, obj)); ... }; end % pSetupIP end methods (Static) % static methods %---------------------------------------- function int = convertConstraintVector(xdata, condata) %CONVERTCONSTRAINTVECTOR Create intervals from constraint information. % % INT = CONVERTCONSTRAINTVECTOR(X, CONDATA) is a static method that % converts logical or distance constraint values into open intervals. if isempty(xdata) int = NaN; else % Convert constraint values into logical where true implies outside the % constraint, not inside. if islogical(condata) condata = ~condata; else condata = condata>1e-6; end % Data is padded at each end: it is assumed that outside the range is % outside constraint. condatadiff = diff([1, condata(:)', 1]); xdata = xdata(:)'; xdatamid = [xdata(1), xdata(1:end-1)+diff(xdata)/2 xdata(end);]; pos = [find(condatadiff>0), find(condatadiff<0)]; int = xdatamid(sort(pos)); end end % convertConstraintVector end % static methods end % classdef function out = i_CheckVector(in) len = length(in); if len==1 && isnan(in) % A NaN value is a flag that entire region is open out = in; else if len ~= numel(in) || (len*0.5)~=floor(len*0.5) error(message('mbc:intervalPatch1D:InvalidPropertyValue')); else out = in(:)'; end end end % i_CheckVector function i_delself(~, ~, obj) %i_delself When the patch is destroyed delete the object if ~mbcgui.util.isBeingDestroyed(obj) delete(obj); end end % i_delself function i_setlims(~, ~, obj) obj.pDoIntervals; end % i_setlims