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

    classdef optionsEditor < mbcgui.widget.BasicContainer
    %cgoptimgui.optionsEditor class
    %   cgoptimgui.optionsEditor extends mbcgui.widget.BasicContainer.
    %
    %    cgoptimgui.optionsEditor 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'
    %       OptionsObject - Property is of type 'MATLAB array'
    
    %  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (AbortSet, SetObservable)
        %OPTIONSOBJECT Property is of type 'MATLAB array'
        OptionsObject = [];
    end
    
    events
        ObjectAltered
    end  % events
    
    methods  % constructor block
        function obj = optionsEditor(varargin)
        %OPTIONSEDITOR Constructor for optionsEditor object
        %  OBJ = OPTIONSEDITOR(PROP, VALUE, ...) creates a simple editor panel for
        %  cgoptimoptions objects.  This allows you to change the numbers of the
        %  different objects in the object.
        
        obj@mbcgui.widget.BasicContainer(varargin{ : }); % converted super class constructor call
        
        % Create UI
        lbl = {'Number of free variables:', ...
            'Number of objectives:', ...
            'Number of constraints:', ...
            'Number of operating point sets:'};
        cb = {@i_changevar, @i_changeobj, @i_changecon, @i_changeops};
        hEdit = cell(1,4);
        for i = 1:length(lbl)
            hEdit{i} = i_createEdit(obj, lbl{i}, cb{i});
        end
        lyt = xreggridbaglayout(obj.Parent, ...
            'dimension', [4 1], ...
            'rowsizes',[20 20 20 20],...
            'gapy', 7,...
            'elements', hEdit);
        obj.ContentHandle = lyt;
        
        % Update gui
        i_setobject(obj, []);
        
        ObjectChangeList = event.proplistener(obj, obj.findprop('OptionsObject'), ...
            'PostSet', @i_setobject);
        obj.addListeners(ObjectChangeList);
        
        
            function i_setobject(~, ~)
            % Respond to changes in the options object property
            if isempty(obj.OptionsObject)
                set(obj.ContentHandle, 'Enable', 'off');
            elseif ~isa(obj.OptionsObject, 'cgoptimextoptions')
                warning(message('mbc:cgoptimgui:optionsEditor:InvalidPropertyValue'));
                set(obj.ContentHandle, 'Enable', 'off');
            else
                edt = get(obj.ContentHandle, 'elements');
                opts = obj.OptionsObject;
                [num, mn, mx] = numVariables(opts);
                i_setupedit(edt{1}, num, mn, mx);
                
                [num, mn, mx] = numObjectives(opts);
                i_setupedit(edt{2}, num, mn, mx);
                
                [num, mn, mx] = numConstraints(opts);
                i_setupedit(edt{3}, num, mn, mx);
                
                [num, mn, mx] = numOperatingPointSets(opts);
                i_setupedit(edt{4}, num, mn, mx);
            end
            end
        
        
            function i_changevar(src, ~)
            % Callback from free variables edit
            opts = obj.OptionsObject;
            newnum = src.Value;
            oldnum  = numVariables(opts);
            
            if newnum>oldnum
                for n = oldnum+1:newnum
                    opts = addFreeVariable(opts, ...
                        generateLabel(opts, 'FreeVariable'));
                end
            elseif newnum<oldnum
                lbls = getFreeVariables(opts);
                for n = oldnum:-1:newnum+1
                    opts = removeFreeVariable(opts, lbls{n});
                end
            else
                % Do not send event for no changes
                return
            end
            [ObjectChangeList.Enabled] = deal(false);
            obj.OptionsObject = opts;
            [ObjectChangeList.Enabled] = deal(true);
            obj.notify('ObjectAltered');
            end
        
            function i_changeobj(src, ~)
            % Callback from objectives edit
            opts = obj.OptionsObject;
            newnum = src.Value;
            oldnum  = numObjectives(opts);
            
            if newnum>oldnum
                for n = oldnum+1:newnum
                    opts = addObjective(opts, ...
                        generateLabel(opts, 'Objective'), ...
                        'min/max');
                end
            elseif newnum<oldnum
                lbls = getObjectiveLabels(opts);
                for n = oldnum:-1:newnum+1
                    opts = removeObjective(opts, lbls{n});
                end
            else
                % Do not send event for no changes
                return
            end
            
            [ObjectChangeList.Enabled] = deal(false);
            obj.OptionsObject = opts;
            [ObjectChangeList.Enabled] = deal(true);
            obj.notify('ObjectAltered');
            end
        
            function i_changecon(src, ~)
            % Callback from constraints edit
            opts = obj.OptionsObject;
            newnum = src.Value;
            oldnum  = numConstraints(opts);
            
            if newnum>oldnum
                for n = oldnum+1:newnum
                    opts = addModelConstraint(opts, ...
                        generateLabel(opts, 'Constraint'), ...
                        'lessthan', 0);
                end
            elseif newnum<oldnum
                lbls = getConstraintLabels(opts);
                for n = oldnum:-1:newnum+1
                    opts = removeConstraint(opts, lbls{n});
                end
            else
                % Do not send event for no changes
                return
            end
            
            [ObjectChangeList.Enabled] = deal(false);
            obj.OptionsObject = opts;
            [ObjectChangeList.Enabled] = deal(true);
            obj.notify('ObjectAltered');
            end
        
            function i_changeops(src, ~)
            % Callback from free operating point sets edit
            opts = obj.OptionsObject;
            newnum = src.Value;
            oldnum  = numOperatingPointSets(opts);
            
            if newnum>oldnum
                for n = oldnum+1:newnum
                    opts = addOperatingPointSet(opts, ...
                        generateLabel(opts, 'OperatingPointSet'), ...
                        {});
                end
            elseif newnum<oldnum
                lbls = getOperatingPointSetLabels(opts);
                for n = oldnum:-1:newnum+1
                    opts = removeOperatingPointSet(opts, lbls{n});
                end
            else
                % Do not send event for no changes
                return
            end
            
            [ObjectChangeList.Enabled] = deal(false);
            obj.OptionsObject = opts;
            [ObjectChangeList.Enabled] = deal(true);
            obj.notify('ObjectAltered');
            end
        end
        
        
        
        % Utility function for creating an editor row
        
    end  % constructor block
    
    methods
    end   % set and get functions
end  % classdef

function h = i_createEdit(obj, label, cb)
edt = mbcgui.widget.Spinner('Parent', obj.Parent, ...
    'Min', 0, ...
    'Max', realmax, ...
    'Rule', 'int', ...
    'Callback', cb);
h = xregGui.labelcontrol('parent', obj.Parent,...
    'LabelSizeMode','absolute',...
    'LabelSize',180,...
    'ControlSize',60,...
    'string', label,...
    'control', edt, ...
    'GrayDisable', 'off');
end


% Utility function for setting the values in an edit row
function i_setupedit(edt, num, mn, mx)
set(edt.Control, 'Value', num, 'Min', mn, 'Max', mx);
if mn==mx
    set(edt, 'Enable', 'inactive');
else
    set(edt, 'Enable', 'on');
end
end