www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimitem/guiTypeChooser.m

    function [obj, ok] = guiTypeChooser(obj, pProj, Optim, varargin)
%GUITYPECHOOSER Open a dialog for selecting a new constraint type
%
%   [OBJ, OK] = GUITYPECHOOSER(OBJ, PPROJ, OPTIM) opens a dialog that
%   allows the user to select a new item type and set it up. PPROJ must be
%   a pointer to a cgproject object and OPTIM must be a cgoptim object.
%
%   [OBJ, OK] = GUITYPECHOOSER(OBJ, PPROJ, OPTIM, 'EditableName', ISEDIT)
%   sets whether the name of OBJ is editable or not.
%
%   See also: CGOPTIMITEM/GETTYPECHOOSEROPTIONS.

%  Copyright 2005-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.


% Determine whether names can be edited for this object
isNameEditable = false;
if nargin > 3
    for i = 1:2:nargin-3
        if strcmpi(varargin{i}, 'editablename')
            isNameEditable = varargin{i+1};
            % Remove these arguments from varargin, so varargin can be
            % passed into the MultiObjectDialog constructor
            varargin(i:i+1) = [];
        end
    end
end

% Ask the object for the information on the different classes of object
% available.
[groupname, options] = getTypeChooserOptions(obj, pProj, Optim);

Names = {options.Type};
Funcs = {options.Function};
for n =1:length(Funcs)
    % Absorb argument passed to object creation func
    Funcs{n} = @(dlg) Funcs{n}();
end

% Find this object in the list of types, or append
thisidx = find( strcmp( getType( obj ),Names ) );
if isempty(thisidx)
    thisidx = length(Names)+1;
    Names{end+1} = getType(obj);
    Funcs{end+1} = obj;
else
    Funcs{thisidx} = obj;
end
hDialog = mbcgui.dialog.MultiObjectDialog( ...
    'PageInfoFunction', @(obj, hGroup) generateEditorPages(obj, hGroup, pProj, Optim), ...
    'DisplaySinglePageAsTab', false, ...
    'Title', sprintf('Edit %s', groupname), ...
    'ChooserLabel', sprintf('%s type:', groupname), ...
    'ChooserLabelWidth', 90, ...
    'OptionStrings', Names, ...
    'OptionFunctions', Funcs, ...
    'OptionDestructors', repmat({@destroy}, size(Funcs)), ...
    'CurrentOption', thisidx, ...
    'DialogPanelSize', [600 320], ...
    'LayoutFunction', @i_createchooserlayout, ...
    varargin{:});

ok = hDialog.show;
if ok
    % The updated object
    obj = hDialog.Object;

    % Set the object's name. It may have been updated.
    hName = findobj(hDialog.Figure, 'Tag', 'NameEdit');
    obj = setName(obj, get(hName, 'String'));
end
delete(hDialog);

    function L = i_createchooserlayout(dlg, hLabel, hCards)

        SC = xregGui.SystemColorsDbl;
        if isNameEditable
            hNameEnable = 'on';
        else
            hNameEnable = 'off';
        end
        currName = getName(obj);
        hName = uicontrol('Parent', dlg.Figure, ...
            'Tag', 'NameEdit',...
            'Style', 'edit', ...
            'HorizontalAlignment', 'left', ...
            'BackgroundColor', SC.WINDOW_BG, ...
            'Visible', 'off', ...
            'Callback', @i_hNameCB, ...
            'String', currName);
        hNameLabel = xregGui.labelcontrol('parent', dlg.Figure, ...
            'visible', 'off', ...
            'String', sprintf('%s name:', groupname), ...
            'LabelSize', 90, ...
            'LabelSizeMode', 'absolute', ...
            'ControlSize', 1, ...
            'ControlSizeMode', 'relative', ...
            'Enable', hNameEnable, ...
            'Gap', 5, ...
            'Control', hName);
        descriptxt = axestext(dlg.Figure, ...
            'verticalalignment','top', ...
            'visible', 'off');
        descripim = mbcgui.widget.Image('Parent',dlg.Figure, ...
            'Visible', 'off');
        div1 = xregGui.dividerline('parent', dlg.Figure, ...
            'visible', 'off');
        L = xreggridbaglayout(dlg.Figure, ...
            'dimension', [4 3], ...
            'rowsizes', [20 20 3 -1], ...
            'colsizes', [-1 305 45], ...
            'gapy', 5, ...
            'gapx', 20, ...
            'mergeblock', {[1 2] [2 2]}, ...
            'mergeblock', {[1 2] [3 3]}, ...
            'mergeblock', {[3 3] [1 3]}, ...            
            'mergeblock', {[4 4] [1 3]}, ...                        
            'elements', {     hLabel, hNameLabel, div1, hCards, ...
                          descriptxt,         [],   [],     [], ...
                           descripim,         [],   [],     []});

        % Callback for name edit
        function i_hNameCB(~, ~)
            newName = get(hName, 'String');
            if strcmp(newName, getName(obj))
                canRename = true;
            else
                OptimSetup = getSetup(Optim);
                newUniqueName = generateLabel(OptimSetup, newName, 'allowroot');
                canRename = isvarname(newName) && strcmp(newUniqueName, newName);
            end
            if canRename
                currName = newName;
            else
                set(hName, 'String', currName);
                msgLine1 = 'Could not rename item. ';
                msgLine2 = 'The name must be a valid MATLAB variable name and be unique among objectives and constraints.';
                errstr = sprintf('%s%s', msgLine1, msgLine2);                
                hError = errordlg(errstr, get(dlg.Figure, 'Name'), 'modal');
                waitfor(hError);
            end
        end
                       
        % Listener to keep description and picture updated
        hList = event.listener(dlg, 'OptionChanged', @i_switchoption);
        set(L, 'UserData', hList);

        function i_switchoption(dlg, ~)
            thisObj = dlg.Object;
            set(descriptxt,'String',texDescription(thisObj));
            set(descripim,'ImageFile',largeIcon(thisObj));
        end

     end

end