www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/+cageview/+optimoutput/ContourAction.m

    classdef ContourAction < matlab.mixin.SetGet & matlab.mixin.Copyable
    %cageview.optimoutput.ContourAction class
    %    cageview.optimoutput.ContourAction properties:
    %       Actions - Property is of type 'handle vector' (read only)
    %       ContourLevels - Property is of type 'int' (read only)
    %
    %    cageview.optimoutput.ContourAction methods:
    %       guiSetCustomLevels - Display a dialog for setting choosing contour levels.
    %       setContourLevels - Set the values to draw contour lines at.
    %       setContourOptions - Set contour display options.
    
    %  Copyright 2007-2015 The MathWorks, Inc.
    
    properties (Access=protected, AbortSet)
        %CONTOURGROUP Property is of type 'MATLAB array'
        ContourGroup = [];
    end
    
    properties (SetAccess=protected, AbortSet)
        %ACTIONS Property is of type 'handle vector' (read only)
        Actions = [];
        %CONTOURLEVELS Property is of type 'int' (read only)
        ContourLevels = 0;
    end
    
    
    methods  % constructor block
        function obj = ContourAction(varargin)
        %ContourAction Constructor for ContourAction
        %  OBJ = ContourAction(CONTOURGROUP) constructs a utility class that
        %  handles the setting of common contour properties. Specifically, this
        %  class handles the setting of contour labels, contour filling and the
        %  contour levels. The callbacks are performed in this object.
        
        if ~isempty(varargin)
            obj.ContourGroup = varargin{1};
            labelMenu = mbcgui.actions.ToggleAction({@i_labelcontours, obj}, ...
                '&Label Contour Lines');
            fillMenu = mbcgui.actions.ToggleAction({@i_fillcontours, obj}, ...
                '&Fill Contours');
            levelMenu = mbcgui.actions.ActionGroup('', '&Contour Levels');
            levelMenu.MenuType = 'submenu';
            Alvls1 = mbcgui.actions.ToggleAction({@i_setnumlevels, 0, obj}, '&Automatic');
            Alvls2 = mbcgui.actions.ToggleAction({@i_setnumlevels, 10, obj}, '&10 Levels');
            Alvls3 = mbcgui.actions.ToggleAction({@i_setnumlevels, 20, obj}, '&20 Levels');
            Alvls4 = mbcgui.actions.ToggleAction({@i_setnumlevels, 30, obj}, '&30 Levels');
            Alvls5 = mbcgui.actions.ToggleAction({@i_setcustomlevels, obj}, '&Custom Levels...');
            levelMenu.Actions = [Alvls1 Alvls2 Alvls3 Alvls4 Alvls5];
            switch obj.ContourLevels
                case 0
                    Alvls1.Selected = true;
                case 10
                    Alvls2.Selected = true;
                case 20
                    Alvls3.Selected = true;
                case 30
                    Alvls4.Selected = true;
                otherwise
                    Alvls5.Selected = true;
            end
            obj.Actions = [labelMenu fillMenu levelMenu];
        end
        end  % ContourAction
        
        
        %--------------------------------------------------------------------------
        
    end  % constructor block
    
    methods
    end   % set and get functions
    
    methods  % public methods
        %----------------------------------------
        function guiSetCustomLevels(obj)
        %GUISETCUSTOMLEVELS Display a dialog for setting choosing contour levels.
        %   GUISETCUSTOMLEVELS(OBJ) displays a dialog that allows the user to
        %   select either a custom number of levels or a custom vector of level
        %   values.
        
        dlg = mbcgui.container.Dialog('Name', 'Custom Contour Levels',...
            'Size',[350 120],...
            'Buttons','OK_CANCEL');
        hFig = dlg.Figure;
        
        SC = xregGui.SystemColorsDbl;
        MethodSel = xregGui.rbgroup('Parent', hFig, ...
            'nx', 1, 'ny', 2, ...
            'gapy', 10, ...
            'callback', @nSwitchOption);
        NumLevelsEdit = mbcgui.widget.Spinner('Parent', hFig, ...
            'Min', 1, ...
            'Rule', 'int');
        LevelsEdit = uicontrol('Parent', hFig, ...
            'Style', 'edit', ...
            'HorizontalAlignment', 'right');
        
        NumLevelsLabel = xregGui.labelcontrol('Parent', hFig, ...
            'String', 'Number of levels:', ...
            'LabelSizeMode', 'absolute', ...
            'LabelSize', 100, ...
            'ControlSize', 60, ...
            'Control', NumLevelsEdit);
        LevelsLabel = xregGui.labelcontrol('Parent', hFig, ...
            'String', 'Custom level values:', ...
            'LabelSizeMode', 'absolute', ...
            'LabelSize', 100, ...
            'ControlSizeMode', 'relative', ...
            'ControlSize', 1, ...
            'Control', LevelsEdit);
        
        lyt = xreggridbaglayout(hFig, ...
            'dimension', [2 2], ...
            'rowsizes', [20 20], ...
            'colsizes', [16 -1], ...
            'gapy', 10, ...
            'mergeblock', {[1 2], [1 1]}, ...
            'elements', {MethodSel, [], NumLevelsLabel, LevelsLabel});
        dlg.Content = lyt;
        
        % Initialise the controls appropriately
        if obj.ContourLevels>=0
            MethodSel.Selected = 1;
            if obj.ContourLevels==0
                NumLevelsEdit.Value = length(get(obj.ContourGroup, 'LevelList'));
            else
                NumLevelsEdit.Value = obj.ContourLevels;
            end
            set(NumLevelsEdit, 'Enable', 'on');
            set(LevelsEdit, 'Enable', 'off', 'BackgroundColor', SC.CTRL_BG);
        else
            MethodSel.Selected = 2;
            NumLevelsEdit.Value = length(get(obj.ContourGroup, 'LevelList'));
            set(NumLevelsEdit, 'Enable', 'off');
            set(LevelsEdit, 'Enable', 'on', 'BackgroundColor', SC.WINDOW_BG);
        end
        set(LevelsEdit, 'String', prettify(get(obj.ContourGroup, 'LevelList')));
        
        
        DLG_DONE = false;
        
        while ~DLG_DONE
            hFig.Tag = '';
            tg = hFig.showDialog();
            
            DLG_DONE = true;
            if strcmp(tg, 'OK')
                % Apply new settings
                Sel = MethodSel.Selected;
                if Sel==1
                    obj.setContourLevels(NumLevelsEdit.Value);
                else
                    Lvls = str2num(get(LevelsEdit, 'String')); %#ok<ST2NM>
                    if ~isempty(Lvls)
                        obj.setContourLevels([], Lvls);
                    else
                        % Make dialog re-show
                        DLG_DONE = false;
                        h = errordlg('Please enter a valid vector of contour level values.', ...
                            'Invalid Level Specification', ...
                            'modal');
                        waitfor(h);
                    end
                end
            end
        end
        delete(dlg);
        
            function nSwitchOption(src, ~)
            Sel = get(src, 'Selected');
            if Sel==1
                set(NumLevelsEdit, 'Enable', 'on');
                set(LevelsEdit, 'Enable', 'off', 'BackgroundColor', SC.CTRL_BG);
            else
                set(NumLevelsEdit, 'Enable', 'off');
                set(LevelsEdit, 'Enable', 'on', 'BackgroundColor', SC.WINDOW_BG);
            end
            
            end
        end
        
        %----------------------------------------
        function setContourLevels(obj, NLevels, CustomLevels)
        %SETCONTOURLEVELS Set the values to draw contour lines at.
        %   SETCONTOURLEVELS(OBJ, NLEVELS) sets the number of levels that contour
        %   lines are drawn at.
        %   SETCONTOURLEVELS(OBJ, [], LEVELS) sets an exact set of levels to draw
        %   contours at.
        %   SETCONTOURLEVELS(OBJ, 0) sets the levels back to being automatic.
        
        AG = obj.Actions;
        LevelActions = AG(3).Actions;
        set(LevelActions, 'Selected', false);
        if ~isempty(NLevels) && NLevels>0
            obj.ContourLevels = NLevels;
            LvlLim = mbcmakelimits(get(obj.ContourGroup, 'ZData'));
            
            % There is no point in having a level at exactly the maximum, so
            % linspace across N+1 levels and use the first N
            Lvls = linspace(LvlLim(1), LvlLim(2), NLevels+1);
            i_setlevels(obj.ContourGroup, Lvls(1:end-1));
            
            switch NLevels
                case 10
                    LevelActions(2).Selected = true;
                case 20
                    LevelActions(3).Selected = true;
                case 30
                    LevelActions(4).Selected = true;
                otherwise
                    LevelActions(5).Selected = true;
            end
            
        elseif ~isempty(NLevels) && NLevels==0
            obj.ContourLevels = 0;
            set(obj.ContourGroup, 'LevelListMode', 'auto');
            
            LevelActions(1).Selected = true;
            
        else
            obj.ContourLevels = -1;
            if nargin>2
                Lvls = CustomLevels;
            else
                Lvls = get(obj.ContourGroup, 'LevelList');
            end
            i_setlevels(obj.ContourGroup, Lvls);
            
            LevelActions(5).Selected = true;
        end
        end  % setContourLevels
        
        %----------------------------------------
        function setContourOptions(obj, Fill, Labels)
        %SETCONTOUROPTIONS Set contour display options.
        %   SETCONTOUROPTIONS(OBJ, FILL, LABELS) sets whether the contour graph
        %   should fill its contours and apply contour labels.  FILL and LABELS
        %   should be boolean flags, or an empty to specify that that option should
        %   not be changed.
        
        cg = obj.ContourGroup;
        AG = obj.Actions;
        if ~isempty(cg) && ~isempty(Fill)
            if Fill
                set(cg, 'Fill', 'on');
            else
                set(cg, 'Fill', 'off');
            end
            AG(2).Selected = Fill;
        end
        
        if ~isempty(cg) && nargin>2 && ~isempty(Labels)
            if Labels
                set(cg, 'ShowText', 'on');
            else
                set(cg, 'ShowText', 'off');
            end
            AG(1).Selected = Labels;
        end
        
        end  % setContourOptions
        
    end  % public methods
    
end  % classdef

function i_labelcontours(src, ~, obj)
obj.setContourOptions([], src.Selected);
end  % i_labelcontours

function i_fillcontours(src, ~, obj)
obj.setContourOptions(src.Selected);
end  % i_fillcontours

function i_setnumlevels(~, ~, NLevels, obj)
obj.setContourLevels(NLevels);
end  % i_setnumlevels


function i_setcustomlevels(~, ~, obj)
obj.guiSetCustomLevels;
end  % i_setcustomlevels

function i_setlevels(cg, Lvls)
if length(Lvls) < 3
    % Pad with dummy inf's to avoid a bug in contours with less than 3
    % levels
    Lvls = [Lvls, inf, inf, inf];
end

set(cg, 'LevelList', Lvls);
end  % i_setlevels