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

    classdef BoundSelector < mbcgui.widget.BasicContainer
    %cgoptimgui.BoundSelector class
    %   cgoptimgui.BoundSelector extends mbcgui.widget.BasicContainer.
    %
    %    cgoptimgui.BoundSelector 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)
    %       ItemSelected - Property is of type 'int32'
    %       Constant - Property is of type 'double'
    %       InternalItemString - Property is of type 'ustring'
    %       pInternalItem - Property is of type 'MATLAB array'
    %       pPROJ - Property is of type 'MATLAB array'
    %       ExpressionSelector - Property is of type 'MATLAB array' (read only)
    %
    %    cgoptimgui.BoundSelector methods:
    %       getExpression - Return the selected expression
    %       getExpressionEvalMode - Return the expression evaluation mode
    %       setExpression - Set the current expression
    %       setExpressionEvalMode - Set the expression evaluation mode
    
    %   Copyright 2006-2015 The MathWorks, Inc.
    
    properties (AbortSet, SetObservable)
        %ITEMSELECTED Property is of type 'int32'
        ItemSelected = 0;
        %CONSTANT Property is of type 'double'
        Constant = 0;
        %INTERNALITEMSTRING Property is of type 'ustring'
        InternalItemString = '';
        %PINTERNALITEM Property is of type 'MATLAB array'
        pInternalItem = mbcpointer( 1 );
        %PPROJ Property is of type 'MATLAB array'
        pPROJ = mbcpointer( 1 );
    end
    
    properties (Access=protected, AbortSet, SetObservable)
        %RADIOITEMTYPE Property is of type 'MATLAB array'
        RadioItemType = '';
        %EDITCONSTANT Property is of type 'MATLAB array'
        EditConstant = '';
    end
    
    properties (SetAccess=protected, AbortSet, SetObservable)
        %EXPRESSIONSELECTOR Property is of type 'MATLAB array' (read only)
        ExpressionSelector = mbcpointer( 1, 0 );
    end
    
    events
        ItemSelectedChanged
        ConstantChanged
        ExpressionChanged
        ExpressionEvalModeChanged
    end  % events
    
    methods  % constructor block
        function obj = BoundSelector(varargin)
        %BOUNDSELECTOR Constructor for cgoptimgui.BoundSelector object
        %   OBJ = BOUNDSELECTOR(PROP, VAL, ...) creates a new BoundSelector object.
        %   This object is used to allow the user to select a bound from a choice
        %   of a constant, internal pointer or CAGE item.
        
        % Call parent class constructor
        obj@mbcgui.widget.BasicContainer(varargin{ : }); % converted super class constructor call
        
        % Construct the object
        obj.RadioItemType = xregGui.rbgroup('parent', obj.Parent, ...
            'visible', obj.Visible, ...
            'nx', 1, ...
            'Callback', @n_editItemType);
        obj.pPostSetInternalItem;
        obj.EditConstant = mbcgui.widget.Spinner('Parent', obj.Parent, ...
            'Callback', @n_editConstant);
        obj.ExpressionSelector = cgoptimgui.ExpressionSelector(...
            'pPROJ', obj.pPROJ, 'Parent', obj.Parent);
        
        if isnull(obj.pInternalItem)
            Ldim = [3 4];
            rowsizes = [20 20 -1];
            offset = 0;
            elements = {obj.RadioItemType, [], [], ...
                [], [], obj.ExpressionSelector,...
                obj.EditConstant};
        else
            Ldim = [4 4];
            rowsizes = [20 20 20 -1];
            offset = 1;
            elements = {obj.RadioItemType, [], [], [], ...
                [], [], [], obj.ExpressionSelector, ...
                obj.EditConstant};
        end
        
        L = xreggridbaglayout(obj.Parent, ...
            'dimension', Ldim, ...
            'rowsizes', rowsizes, ...
            'colsizes', [18 65 70 -1], ...
            'mergeblock', {[1 2 + offset], [1 2]}, ...
            'mergeblock', {[3 3] + offset, [2 4]}, ...
            'elements', elements);
        
        % Create layout and assign to parent
        obj.ContentHandle = L;
        
        % Update the GUI
        obj.pUpdate;
        
        % Create listeners
        obj.addPropertyListeners( ...
            { ...
            'Constant', ...
            'ItemSelected', ...
            'pPROJ', ...
            'InternalItemString', ...
            'pInternalItem', ...
            }, ...
            { ...
            @n_setConstant, ...
            @n_setItemSelected, ...
            @n_setProject, ...
            @n_setInternalItemString, ...
            @n_setInternalItem, ...
            } ...
            );
        obj.addListeners( ...
            [event.listener(obj.ExpressionSelector, 'ExpressionChanged', ...
            @n_expressionChanged);...
            event.listener(obj.ExpressionSelector, 'ExpressionEvalModeChanged', ...
            @n_expressionEvalModeChanged)]...
            );
        
            function n_setConstant(~, ~)
            
            % Set the new constant in the GUI
            obj.pUpdate('constant');
            
            end
        
            function n_setItemSelected(~, ~)
            
            % Set the new item selected in the GUI
            obj.pUpdate;
            
            end
        
            function n_setProject(~, ~)
            
            % Set the project in the Expression Selector
            obj.ExpressionSelector.pPROJ = obj.pPROJ;
            
            % Perform an update of the whole GUI
            obj.pUpdate;
            
            end
        
            function n_setInternalItemString(~, ~)
            
            % Only set the internal item string if there is an internal item
            if ~isnull(obj.pInternalItem)
                RadStr = get(obj.pGetRadioItemType, 'String');
                RadStr{2} =  obj.InternalItemString;
                set(obj.pGetRadioItemType, 'String', RadStr);
            end
            
            end
        
            function n_setInternalItem(~, ~)
            obj.pPostSetInternalItem;
            end
        
            function n_editConstant(hEdit, ~)
            
            % Disable the listeners
            obj.disableListeners;
            
            % Set the new constant and send the constant changed event
            obj.Constant = hEdit.Value;
            notify(obj, 'ConstantChanged');
            
            % Disable the listeners
            obj.enableListeners;
            
            end
        
            function n_editItemType(hRadio, ~)
            
            % Disable the listeners
            obj.disableListeners;
            
            % Set the new selected item and send the item selected changed event
            ThisRadVal = find(get(hRadio, 'Value'));
            ThisRadStr = get(hRadio, 'String');
            ThisRadStr = ThisRadStr{ThisRadVal};
            BaseTypes = obj.pGetBaseTypes;
            if strcmp(ThisRadStr, BaseTypes{2})
                ThisRadVal = 3;
                if obj.getExpression == obj.pGetInternalExpression
                    % If moving from internal selection to CAGE item, then set
                    % the expression to null
                    obj.setExpression(mbcpointer(1));
                    notify(obj, 'ExpressionChanged');
                end
            end
            if ThisRadVal == 2
                obj.setExpression(obj.pGetInternalExpression);
                notify(obj, 'ExpressionChanged');
            end
            obj.ItemSelected = ThisRadVal - 1;
            notify(obj, 'ItemSelectedChanged');
            
            % Set the correct enable status on the widget controls
            obj.pDoEnable;
            
            % Reenable the listeners
            obj.enableListeners;
            
            end
        
            function n_expressionChanged(~, ~)
            
            % Send the expression changed event
            notify(obj, 'ExpressionChanged');
            
            end
        
            function n_expressionEvalModeChanged(~, ~)
            
            % Send the expression eval mode changed event
            notify(obj, 'ExpressionEvalModeChanged');
            
            end
        
        end
        
    end  % constructor block
    
    methods
        function set.ItemSelected(obj,value)
        obj.ItemSelected = i_setItemSelected(obj,value);
        end
        
        function set.Constant(obj,value)
        obj.Constant = i_setConstant(obj,value);
        end
        
        function set.pInternalItem(obj,value)
        obj.pInternalItem = i_setInternalItem(obj,value);
        end
        
        function set.pPROJ(obj,value)
        obj.pPROJ = i_setProject(obj,value);
        end
        
    end   % set and get functions
    
    methods  % public methods
        %----------------------------------------
        function pExpr = getExpression(obj)
        %GETEXPRESSION Return the selected expression
        %   PEXPR = GETEXPRESSION(OBJ) returns the expression selected in
        %   OBJ.EXPRESSIONSELECTOR.
        %
        %   See also CGOPTIMGUI.BOUNDSELECTOR/GETEXPRESSIONEVALMODE
        
        
        pExpr = obj.ExpressionSelector.Expression;
        
        end  % getExpression
        
        %----------------------------------------
        function EvalMode = getExpressionEvalMode(obj)
        %GETEXPRESSIONEVALMODE Return the expression evaluation mode
        %   EVALMODE = GETEXPRESSIONEVALMODE(OBJ) returns the expression evaluation
        %   mode selected for the expression in OBJ.EXPRESSIONSELECTOR.
        %
        %   See also CGOPTIMGUI.BOUNDSELECTOR/GETEXPRESSION
        
        EvalMode = obj.ExpressionSelector.ExpressionEvalMode;
        end  % getExpressionEvalMode
        
        %----------------------------------------
        function setExpression(obj, pExpr)
        %SETEXPRESSION Set the current expression
        %
        %   SETEXPRESSION(OBJ, PEXPR) sets the current expression to be PEXPR
        
        
        obj.ExpressionSelector.Expression = pExpr;
        end  % setExpression
        
        %----------------------------------------
        function setExpressionEvalMode(obj, idx)
        %SETEXPRESSIONEVALMODE Set the expression evaluation mode
        %
        %   OUT = SETEXPRESSIONEVALMODE(OBJ, IDX) sets the expression evaluation
        %   mode to be 1 (normal evaluation) or 2 (PEV evaluation). Note that this
        %   method does not check whether the selected evaluation method will work.
        
        
        obj.ExpressionSelector.ExpressionEvalMode = idx;
        end  % setExpressionEvalMode
        
    end  % public methods
    
    methods(Access=protected)
               %----------------------------------------
        function pDoEnable(obj)
        %PDOENABLE Set the enable status for the widget controls
        %   PDOENABLE(OBJ) sets the correct enable status for the widget controls.
        
        
        % Enable/disable the correct controls for the comparison value ------------
        if obj.ItemSelected == 0
            obj.EditConstant.Enable  = 'on';
            obj.ExpressionSelector.Enable  = 'off';
        elseif obj.ItemSelected == 1
            obj.EditConstant.Enable  = 'off';
            obj.ExpressionSelector.Enable  = 'off';
        else
            obj.EditConstant.Enable  = 'off';
            obj.ExpressionSelector.Enable  = 'on';
        end
        
        end  % pDoEnable
        
        %----------------------------------------
        function BaseTypes = pGetBaseTypes(obj) %#ok<MANU>
        %PGETBASETYPES Return the base types
        %   BASETYPES = PGETBASETYPES(OBJ) returns the base options that OBJ will
        %   always support for the bound.
        
        BaseTypes = {'Constant:'; 'CAGE item:'};
        
        end  % pGetBaseTypes
        
        %----------------------------------------
        function pInt = pGetInternalExpression(obj)
        %PGETINTERNALEXPRESSION Return the internal expression
        %   PINT = PGETINTERNALEXPRESSION(OBJ) returns the internal expression held
        %   in the optimization.
        
        pInt = obj.pInternalItem;
        end  % pGetInternalExpression
        
        %----------------------------------------
        function hRad = pGetRadioItemType(obj)
        %PGETRADIOITEMTYPE Return the RadioItemType control
        %   HRAD = PGETRADIOITEMTYPE(OBJ) returns OBJ.RADIOITEMTYPE
        
        hRad = obj.RadioItemType;
        end  % pGetRadioItemType
        
        %----------------------------------------
        function pPostSetInternalItem(obj)
        %PPOSTSETINTERNALITEM Update the GUI following internal item being set
        %   OUT = PPOSTSETINTERNALITEM(OBJ) updates the GUI following the setting
        %   of OBJ.PINTERNALITEM.
        
        BaseTypes = obj.pGetBaseTypes;
        if isvalid(obj.pInternalItem)
            RadStr = {BaseTypes{1}; obj.InternalItemString; BaseTypes{2}};
        else
            RadStr = BaseTypes;
        end
        NY = length(RadStr);
        set(obj.RadioItemType, 'ny', NY, 'String', RadStr);
        
        end  % pPostSetInternalItem
        
        %----------------------------------------
        function pUpdate(obj, varargin)
        %PUPDATE Update the GUI
        %
        %   PUPDATE(OBJ) updates all the graphical components from the settings in
        %   OBJ.
        %
        %   PUPDATE(OBJ, 'CONSTANT') updates the constant control in OBJ.
        
        
        % Find what we need to update
        if nargin < 2
            updateAction = 'all';
        else
            updateAction = varargin{1};
        end
        
        % Choose the correct item
        if strcmp(updateAction, 'all')
            if isvalid(obj.pInternalItem)
                radioValue = obj.ItemSelected + 1;
            elseif obj.ItemSelected < 2
                radioValue = obj.ItemSelected + 1;
            else
                radioValue = obj.ItemSelected;
            end
            obj.RadioItemType.Selected = radioValue;
            
            % Set the correct value in the constant field
            set(obj.EditConstant, 'Value', obj.Constant);
            
            % Set the correct status on all the widget controls
            obj.pDoEnable;
        else
            
            % Only set the correct value in the constant field
            set(obj.EditConstant, 'Value', obj.Constant);
            
        end
        
        end  % pUpdate
 
    end
    
end  % classdef

function val = i_setItemSelected(src, val)

% Bound selector has an internal expression => ItemSelected can be 0, 1 or
% 2
if (~isempty(src.pGetInternalExpression) && ~ismember(val, 0:2))
    error(message('mbc:ExpressionSelector:InvalidArgument'));
end

% Bound selector does not have an internal expression => ItemSelected can
% be 0 or 2
if (isempty(src.pGetInternalExpression) && ~ismember(val, [0 2]))
    error(message('mbc:ExpressionSelector:InvalidArgument1'));
end
end  % i_setItemSelected

function val = i_setConstant(~, val)

if ~isnumeric(val) || ~isscalar(val) || ~isreal(val)
    error(message('mbc:BoundSelector:InvalidArgument'));
end
end  % i_setConstant

function val = i_setInternalItem(~, val)

if ~isa(val, 'xregpointer') || ...
        ~( (isvalid(val) && issource(val.info)) || ~isvalid(val))
    errstr = ['pInternalItem must be a null pointer or a pointer', ...
        'to a source expression'];
    error('mbc:BoundSelector:InvalidArgument1', errstr);
end
end  % i_setInternalItem

function val = i_setProject(~, val)

if ~isa(val, 'xregpointer') || ...
        ~( (isvalid(val) && isproject(val.info)) || ~isvalid(val))
    error(message('mbc:BoundSelector:InvalidArgument2'));
end
end  % i_setProject