www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/+cgoptimgui/weightsrangeselector.m

    classdef weightsrangeselector < mbcgui.widget.BasicContainer
    %cgoptimgui.weightsrangeselector class
    %   cgoptimgui.weightsrangeselector extends mbcgui.widget.BasicContainer.
    %
    %    cgoptimgui.weightsrangeselector properties:
    %       Parent - Property is of type 'MATLAB array'
    %       Position - Property is of type 'rect'
    %       Enable - Property is of type 'on/off'
    %       Visible - Property is of type 'on/off'
    %       UserData - Property is of type 'MATLAB array'
    %       Tag - Property is of type 'string'
    %       Display - Property is of type 'MATLAB array' (read only)
    %       optimItem - Property is of type 'MATLAB array'
    
    %  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (AbortSet, SetObservable)
        %OPTIMITEM Property is of type 'MATLAB array'
        optimItem = [  ];
    end
    
    properties (Access=protected, AbortSet, SetObservable)
        %MINCLICKEDIT Property is of type 'MATLAB array'
        minClickEdit = [  ];
        %MAXCLICKEDIT Property is of type 'MATLAB array'
        maxClickEdit = [  ];
        %LISTENERS Property is of type 'handle vector'
        listeners = [];
    end
    
    events
        weightsRangeChanged
    end  % events
    
    methods  % constructor block
        function obj = weightsrangeselector(varargin)
        %WEIGHTSRANGESELECTOR Constructor for weightsrangeselector object
        %  OBJ = WEIGHTSRANGESELECTOR(OPTIMITEM) constructs an WEIGHTSRANGESELECTOR
        %  object. This component allows a user to select the weights range for a
        %  CGOPTIMITEM object that supports GET/SETWEIGHTSRANGE.
        %
        %  OBJ = WEIGHTSRANGESELECTOR(OPTIMITEM, PROP, VAL, ...) constructs
        %  an WEIGHTSRANGESELECTOR object where graphical properties can be set
        %  via PROP, VAL pairs.
        
        % Allow the construction of an empty object
        
        % Call the inherited constructor
        obj@mbcgui.widget.BasicContainer(varargin{ 2:end }); % converted super class constructor call
        
        if nargin>0
            % Store the cgoptimitem object - check to see if the object supports
            % get and setWeightsRange. If not, error (the error checking is handled
            % in the schema)
            obj.optimItem = varargin{1};
            
            % Create the display
            obj.pCreateDisplay;
            
            % Update the object to reflect the new optimitem
            obj.pPostSetOptimItem;
            
            % Create listeners
            obj.listeners = event.proplistener(obj, findprop(obj, 'optimItem'), ...
                'PostSet', @(src, evt) pPostSetOptimItem(obj));
            
        end
        
        
        end  % weightsrangeselector
        
    end  % constructor block
    
    methods
        function set.optimItem(obj,value)
        obj.optimItem = i_checkoptimItem(obj,value);
        end
        
    end   % set and get functions
    
    methods (Access=protected) 
        %----------------------------------------
        function OK = pCheckOptimItem(obj, optimItem)
        %PCHECKOPTIMITEM Check the contained cgoptimitem
        %  OK = PCHECKOPTIMITEM(OBJ, OPTIMITEM) checks OPTIMITEM to see if supports
        %  GETWEIGHTSRANGE and SETWEIGHTSRANGE. If so, the OPTIMITEM can be used by
        %  OBJ.
        
        % Assume it will be OK
        OK = true;
        
        % Test the required interfaces
        if ~isempty(obj)
            try
                rng = getWeightsRange(optimItem);
                optimItem = setWeightsRange(optimItem, rng);
            catch
                OK = false;
            end
        end
        end  % pCheckOptimItem

        %----------------------------------------
        function pCreateDisplay(obj)
        %PCREATEDISPLAY Private GUI component creation method
        
        if isempty(obj.Parent)
            f = figure;
            obj.Parent = f;
        end
        
        hLabel1 = mbcgui.widget.Label('Parent',obj.Parent,...
            'Visible', obj.Visible, ...
            'String','Weights range:',...
            'HorizontalAlignment','left');
        
        hLabel2 = mbcgui.widget.Label('Parent',obj.Parent,...
            'Visible', obj.Visible, ...
            'String','Min:',...
            'HorizontalAlignment','left');
        
        hLabel3 = mbcgui.widget.Label('Parent',obj.Parent,...
            'Visible', obj.Visible, ...
            'String','Max:',...
            'Horizontalalignment','left');
        
        obj.minClickEdit = mbcgui.widget.Spinner('Parent', obj.Parent, ...
            'Visible', obj.Visible, ...
            'Tag', 'min', ...
            'UserData', 0, ...
            'Enable', 'off', ...
            'Value', 0, ...
            'Callback', @i_editweightsrange);
        obj.maxClickEdit = mbcgui.widget.Spinner('Parent', obj.Parent, ...
            'Visible', obj.Visible, ...
            'Tag', 'max', ...
            'Value', 1, ...
            'Enable', 'off', ...
            'UserData', 1, ...
            'Callback', @i_editweightsrange);
        
        lyt = xreggridbaglayout(obj.Parent,...
            'colsizes', [30 65 30 30 65], ...
            'rowsizes', [15 7 3 15 2], ...
            'dimension',[5 5],...
            'mergeblock', {[1 1], [1 5]},...
            'mergeblock', {[3 5], [2 2]}, ...
            'mergeblock', {[3 5], [5 5]}, ...
            'elements',{hLabel1, [], [], hLabel2, [], ...
            [], [], obj.minClickEdit, [], [], ...
            [], [], [], [], [], ...
            [], [], [], hLabel3, [], ...
            [], [], obj.maxClickEdit, [], []});
        
        obj.ContentHandle = lyt;
        
            function i_editweightsrange(src, ~)
            
            obj.pDisableListeners;
            valtype = get(src, 'Tag');
            newvalue = get(src, 'Value');
            rng = getWeightsRange(obj.optimItem);
            
            % Assume user types in sensible weight range
            OK = true;
            
            switch valtype
                case 'min'
                    if newvalue > rng(2)
                        OK = false;
                    else
                        ind = 1;
                    end
                case 'max'
                    if newvalue < rng(1)
                        OK = false;
                    else
                        ind = 2;
                    end
            end
            
            if OK
                set(src, 'UserData', newvalue);
                rng(ind) = newvalue;
                obj.optimItem = setWeightsRange(obj.optimItem, rng);
                notify(obj, 'weightsRangeChanged');
                
            else
                set(src, 'Value', get(src, 'UserData'));
            end
            obj.pEnableListeners;
            
            end
        end
        
        %----------------------------------------
        function pDisableListeners(obj)
        %PDISABLELISTENERS Private method to disable listeners
        
        [obj.listeners.Enabled] = deal(false);
        
        end  % pDisableListeners
        
        %----------------------------------------
        function pEnableListeners(obj)
        %PENABLELISTENERS Private method to enable listeners
        
        [obj.listeners.Enabled] = deal(true);
        
        end  % pEnableListeners

        
        %----------------------------------------
        function pPostSetOptimItem(obj)
        %PPOSTSETOPTIMITEM Update the object after a new optimItem has been set
        
        % Did the previous optimItem contain no Expression (i.e. click edits
        % disabled)
        HAVEPREVEXPR = strcmpi(obj.minClickEdit.Enable, 'on');
        
        % Get the weights range from the cgoptimitem if the previous optimItem did
        % not contain an expression, otherwise update the weights from the GUI
        if ~HAVEPREVEXPR
            wtsrng = getWeightsRange(obj.optimItem);
            
            % Update the click edits
            obj.minClickEdit.Value = wtsrng(1);
            obj.maxClickEdit.Value = wtsrng(2);
        else
            wtsrng = [obj.minClickEdit.Value, obj.maxClickEdit.Value];
            obj.optimItem = setWeightsRange(obj.optimItem, wtsrng);
        end
        
        % Set the enable status. Note, if the expression is set externally to the
        % object, then the object must be updated by the user.
        pExpr = getExpression(obj.optimItem);
        if isnull(pExpr)
            obj.minClickEdit.Enable = 'off';
            obj.maxClickEdit.Enable = 'off';
        else
            obj.minClickEdit.Enable = 'on';
            obj.maxClickEdit.Enable = 'on';
        end
        
        end  % pPostSetOptimItem
        
    end  
    
end  % classdef

function val = i_checkoptimItem(obj, val)
%--------------------------------------------------------------------------

OK = obj.pCheckOptimItem(val);
if ~OK
    error(message('mbc:weightsrangeselector:InvalidPropertyValue'));
end
end  % i_checkoptimItem