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

    classdef optimfuncseditor < mbcgui.widget.BasicContainer
    %cgoptimgui.optimfuncseditor class
    %    cgoptimgui.optimfuncseditor properties:
    %       OptimFuncs - Property is of type 'MATLAB array'
    %
    %    cgoptimgui.optimfuncseditor methods:
    %       pGetSelectedFunction - Return information about the selected function.
    %       pSetSelectedIndex - Set the selected function
    
    %  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (AbortSet, SetObservable)
        %OPTIMFUNCS Property is of type 'MATLAB array'
        OptimFuncs = [];
    end
    
    properties (Access=protected, AbortSet, SetObservable)
        %SELECTEDINDEX Property is of type 'MATLAB array'
        SelectedIndex = [];
        %HLISTENERS Property is of type 'handle vector'
        hListeners = [];
        %HGRID Property is of type 'MATLAB array'
        hGrid = [];
        %HLIST Property is of type 'handle'
        hList = [];
        %HADD Property is of type 'MATLAB array'
        hAdd = [];
        %HREMOVE Property is of type 'MATLAB array'
        hRemove = [];
        %HTEST Property is of type 'MATLAB array'
        hTest = [];
    end
    
    methods  % constructor block
        function obj = optimfuncseditor(varargin)
        %OPTIMFUNCSEDITOR Constructor for optimfuncseditor object
        %  OBJ = OPTIMFUNCSEDITOR(PROP, VAL, ...)
        
        % obj = cgoptimgui.optimfuncseditor;
        obj@mbcgui.widget.BasicContainer(varargin{:});
        
        obj.hList = mbcwidgets.Table1D( 'list',...
            'Parent', obj.Parent,...
            'SelectionMode', 'SingleRow' );
        obj.hList.Peer.setColumnData( {'Name', 'Location'} );
        obj.hList.Peer.setColumnWidths( [110 180] );
        
        hText = uicontrol('Parent', obj.Parent, ...
            'Style', 'text', ...
            'HorizontalAlignment', 'left', ...
            'String', 'User-defined optimization functions:');
        obj.hAdd = uicontrol('Parent', obj.Parent, ...
            'Style', 'pushbutton', ...
            'String', 'Add...', ...
            'Callback', {@i_Add, obj});
        obj.hRemove = uicontrol('Parent', obj.Parent, ...
            'Style', 'pushbutton', ...
            'String', 'Remove', ...
            'Callback', {@i_Remove, obj});
        obj.hTest = uicontrol('Parent', obj.Parent, ...
            'Style', 'pushbutton', ...
            'String', 'Test', ...
            'Callback', {@i_Test, obj});
        obj.hGrid = xreggridbaglayout(obj.Parent, ...
            'dimension', [5 2], ...
            'rowsizes', [15 25 25 25 -1], ...
            'colsizes', [-1 65], ...
            'gapx', 10, ...
            'gapy', 5, ...
            'mergeblock', {[2 5], [1 1]}, ...
            'elements', {hText, obj.hList, [],[],[], [], obj.hAdd, obj.hRemove, obj.hTest});
        
        obj.hListeners = {...
            event.proplistener(obj, obj.findprop('OptimFuncs'), 'PostSet', @i_setOptimFuncs), ...
            handle.listener(obj.hList, 'SelectionChanged', {@i_listSelectionChanged, obj}), ...
            };
        obj.ContentHandle = obj.hGrid;
        pUpdateList(obj);
        pUpdateButtons(obj);
        end  % optimfuncseditor
        
    end  % constructor block
    
    methods  % public methods
        %----------------------------------------
        function [idx, name, location] = pGetSelectedFunction(obj)
        %PGETSELECTEDFUNCTION Return information about the selected function.
        %   [IDX, NAME, LOCATION] = PGETSELECTEDFUNCTION(OBJ)
        
        idx = obj.SelectedIndex;
        
        % When nothing selected, return quickly.
        if isempty( idx )
            name = '';
            location = '';
            return;
        end
        % Only do the minimum amount of work - no point aking these questions if
        % the answers are not needed.
        if nargout>1
            [names, status] = getfunctions( obj.OptimFuncs );
            name = names{idx};
            status = status(idx);
        end
        
        if nargout>2
            if status
                location = fileparts( which( name ) );
            else
                location = '';
            end
        end
        
        end  % pGetSelectedFunction
        
        %----------------------------------------
        function pSetSelectedIndex(obj, index)
        %PSETSELECTEDINDEX Set the selected function
        %   PSETSELECTEDINDEX(OBJ, INDEX)
        
        obj.SelectedIndex = index;
        if ~isempty( obj.SelectedIndex )
            obj.hList.selectRows( obj.SelectedIndex );
        end
        
        end  % pSetSelectedIndex
        
    end  % public methods
    
    
    methods (Hidden) % possibly private or hidden
        %----------------------------------------
        function pDoAdd(obj)
        %PDOADD Private method for adding a new function
        %  PDOADD(OBJ)
        
        % Pop up a file chooser dialog to get a new function name.  Get the initial
        % path from current item's location.
        [selitem, ~, location] = pGetSelectedFunction(obj);
        
        if ~isempty(selitem) && ~isempty( location )
            startpth = [location, filesep];
        else
            startpth = '';
        end
        [file, path, ~] = uigetfile('*.m', 'Select Optimization Function', startpth);
        if file~=0
            % Remove extension
            [~, file] = fileparts(file);
            % Remove trailing slash from path
            [path, ~] = fileparts(path);
            [obj.OptimFuncs, OK] = addfunction(obj.OptimFuncs, file);
            if ~OK
                h = errordlg('An optimization function with this name already exists.',...
                    'CAGE Error', 'modal');
                waitfor(h);
            else
                % Warn user if location does not match the path they chose;
                loc = fileparts(which(file));
                if isempty(loc)
                    h = warndlg(['The function you chose will not be used because it is', ...
                        ' not on MATLAB''s search path.  You must add its location to', ...
                        ' MATLAB''s path before it can be used.'], 'CAGE Warning', 'modal');
                    waitfor(h);
                elseif ~strcmpi(loc, path)
                    h = warndlg(['The function you chose will not be used because it is ', ...
                        'being overloaded by a function with the same name located in ', ...
                        loc, '.  Your optimization function must be above this function ', ...
                        'on MATLAB''s search path.'], 'CAGE Warning', 'modal');
                    waitfor(h);
                end
            end
        end
        
        end  % pDoAdd
        
        %----------------------------------------
        function pDoTest(obj)
        %PDOTEST Private method to test a function
        %  PDOTEST(OBJ)
        
        [idx, name] = obj.pGetSelectedFunction();
        if ~isempty(idx)
            [ok, report] = testfunction(obj.OptimFuncs, idx);
            if ~ok
                % Pop up dialog with report in it
                nErrors = size(report,1);
                if nErrors ==1
                    msg = sprintf('%d error was found during testing:',nErrors);
                else
                    msg = sprintf(['%d errors were found during testing.', ...
                        '  A detailed report is below.'],nErrors);
                end
                reportmsg = '';
                for n = 1:size(report,1)
                    reportmsg = [reportmsg, sprintf('+  %s\n\n%s\n\n\n', report{n,1}, report{n,2})]; %#ok<AGROW>
                end
            else
                msg = 'Testing completed with no errors.';
                reportmsg = '';
            end
            i_showfigure(name, msg, reportmsg);
        end
        end  % pDoTest
        
        %----------------------------------------
        function pUpdateButtons(obj)
        %PUPDATEBUTTONS Private methods to update status of buttons
        %  PUPDATEBUTTONS(OBJ)
        
        optionsobj = obj.OptimFuncs;
        if isempty(optionsobj)
            set([obj.hAdd, obj.hRemove, obj.hTest], 'Enable', 'off');
        else
            [names, ~] = getfunctions(optionsobj);
            nItems = length(names);
            if nItems
                set([obj.hRemove, obj.hTest], 'Enable', 'on');
            else
                set([obj.hRemove, obj.hTest], 'Enable', 'off');
            end
            set(obj.hAdd, 'Enable', 'on');
        end
        
        end  % pUpdateButtons
        
        %----------------------------------------
        function pUpdateList(obj)
        %PUPDATELIST Private gui update function
        %  PUPDATELIST(OBJ)
        
        optionsobj = obj.OptimFuncs;
        if isempty(optionsobj)
            listData = cell( 0, 2 );
            icons = cell(0,1);
        else
            [names, status] = getfunctions(optionsobj);
            nItems = length(names);
            listData = cell( nItems, 2 );
            icons = cell( nItems, 1 );
            for n = 1:nItems
                if ~status(n)
                    location = 'Function not found.';
                    icons{n} = xregrespath( 'errorsmall.bmp' );
                else
                    location = fileparts(which(names{n}));
                    icons{n} = '';
                end
                listData(n,:) = {names{n}, location};
            end
        end
        obj.hList.Peer.setData( listData );
        obj.hList.Peer.setIconData( icons, 1 );
        
        % If we have any functions we should select one
        if ~isempty( listData )
            if isempty( obj.SelectedIndex ) || obj.SelectedIndex>nItems
                obj.pSetSelectedIndex( 1 );
            else
                obj.pSetSelectedIndex( obj.SelectedIndex );
            end
        else
            obj.pSetSelectedIndex( [] );
        end
        
        end  % pUpdateList
        
    end  % possibly private or hidden
    
end  % classdef

function i_setOptimFuncs(~, evt)
obj = evt.AffectedObject;
pUpdateList(obj);
pUpdateButtons(obj);
end  % i_setOptimFuncs

function i_listSelectionChanged( ~, evt, obj )
obj.pSetSelectedIndex( evt.data.SelectedDataRows );
end  % i_listSelectionChanged

function i_Add(~, ~, obj)
obj.pDoAdd;
end  % i_Add

function i_Remove(~, ~, obj)
[index, fcn]= obj.pGetSelectedFunction();
if ~isempty(index)
    obj.OptimFuncs = removefunction(obj.OptimFuncs, fcn);
end
end  % i_Remove

function i_Test(~, ~, obj)
pDoTest(obj);
end  % i_Test

function i_showfigure(fcn, msg, reportmsg)
dlg = mbcgui.container.Dialog(...
    'Name', 'Test Results', ...
    'Tag', 'OptimTestOutput',...
    'Buttons', 'CLOSE',...
    'Size', [400, 450] );
hFig = dlg.Figure;

t1 = uicontrol('Parent', hFig, ...
    'Style', 'text', ...
    'String', ['Test results for optimization function ', fcn, ':'], ...
    'HorizontalAlignment', 'left');
t3 = uicontrol('Parent', hFig, ...
    'Style', 'edit', ...
    'Enable', 'inactive', ...
    'Max', 2, ...
    'String', [msg sprintf('\n\n') reportmsg], ...
    'HorizontalAlignment', 'left');
lyt = xreggridbaglayout(hFig, ...
    'dimension', [2 1], ...
    'rowsizes', [15 -1], ...
    'gapy', 10, ...
    'elements', {t1, t3});
dlg.Content = lyt;
dlg.showDialog();
delete(dlg);
end  % i_showfigure