www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/+mbcmodelview/SpecialPlots.m

    classdef SpecialPlots < mbcgui.multiview.View
    %SpecialPlots base special plots view
    
    %  Copyright 2014-2016 The MathWorks, Inc. and Ford Global Technologies, Inc.

    properties 
        PlotType
    end
    
    properties (SetAccess=protected)
        %ShowLegend 
        ShowLegend = true;
    end
    
    properties (Dependent, AbortSet,SetAccess=protected)
        %Axes main axes
        Axes
    end
    
    methods
        function obj = SpecialPlots(varargin)
        %SpecialPlots constructor
        
        obj@mbcgui.multiview.View(varargin{:});
        create(obj)
        createActions(obj);
        % update view before listeners are turned on
        
        if strcmp(obj.Visible,'on') && hasData(obj.MessageService)
            update(obj)
        end

        % obj.FirstResizeListener = event.listener(obj.Axes,'SizeChanged',@obj.onFirstResize);
        
        % add some message service listeners
        ms=obj.MessageService;
        addMessageServiceListener(obj, ...
            event.proplistener(ms,findprop(ms,'ShowTestNumbers'),'PostSet',@obj.showTestNumbers));
        addMessageServiceListener(obj, 'NodeUpdated',@obj.onNodeUpdated)
        
        end
        
        function ah = get.Axes(obj)
        
        if ~isempty(obj.ContentHandle)
            ah = obj.ContentHandle.AxesHandle;
        else
            ah = [];
        end
        end
        
        function set.Axes(obj,ax)
        if ~isempty(obj.ContentHandle)
            obj.ContentHandle.AxesHandle = ax;
        end
        end
        
        function update(obj)
        %update full update of view 
        
        ms = obj.MessageService;
        cla(obj.Axes);
        if ms.Status~=0
            % enable all actions
            
            set(obj.Actions,'Enabled',true);
            % do particular plot action
            plot(obj)

            % assign outlier line to 'main line'
            lh = findobj(obj.Axes,'Type','line');
            MainLine = findobj(lh,'Tag','main line');
            if ~isempty('main line')
                otherLines = lh(MainLine~=lh);
                if ~isempty(otherLines)
                    % all other lines must be set to PickableNone for the
                    % outlier line to work
                    mbcgui.hgclassesutil.setNotPickable(otherLines)
                end
                add(ms.OutlierLine,MainLine);
            else
                mbcgui.hgclassesutil.setNotPickable(lh)
            end
            
            % show legend and test numbers as appropriate
            showLegend(obj)
            showTestNumbers(obj)
        else
            % disable all options when model is not fitted
             set(obj.Actions,'Enabled',false);
        end
        title(obj.Axes,ms.Title,'Interpreter','none')
        
        end
        
        function t = gettitle(obj)
        t = obj.PlotType;
        end
        
        
        function ah = printCopy(obj, fig)
        %PRINTCOPY Create a printable version of the component
        %   NEWOBJ = PRINTCOPY(H, FIG) creates and returns a handle to a new
        %   component, NEWOBJ, parented by the specified figure, FIG. The new
        %   object will be used for printing a copy of this component.
        %
        %   Normally the printable version of a component is not a complete
        %   copy of the object.  There are often controls that should not
        %   appear, such as popup menus, or components whose information needs
        %   to be transformed, such as edit boxes.
        %
        %   See also canPrint, print, printSize.
        
        if isgraphics(obj.Axes)
            % copy axes to new figure
            hAxes = copyobj(obj.Axes,fig);
            ah = mbcgui.widget.AxesContainer('Parent',fig,...
                'AxesHandle',hAxes,...
                'PositionSetting','outer');
            if obj.ShowLegend
                showLegend(obj,hAxes);
            end
        else
            ah = [];
        end
        end
        
        function OK = canPrint(obj)
        %canPrint can always print
        OK = true;
        end      
        
        function s = serializeView(obj) 
        %SERIALIZEVIEW Get serializable setup data for the view
        %
        %  OUT = SERIALIZEVIEW(OBJ) retuns a MATLAB array that contains the setup
        %  data that will allow this view to be recreated via the deserializeView
        %  function in the future.  Typically this function will return a cell
        %  array of property names and values, however any MATLAB array type is
        %  allowed.
        
        s.ShowLegend = obj.ShowLegend;    
        
        end
        
        function deserializeView(obj,s)
        %DESERIALIZEVIEW Set saved state data
        %  DESERIALIZEVIEW(OBJ, DATA) sets a copy of the serialized state of this
        %  View.  DATA is a MATLAB array that will have been created by a
        %  previous call to SERIALIZEVIEW on this View object.
        
        obj.ShowLegend = s.ShowLegend;    
        
        end           
        
        
    end
    
    methods (Access=private)
        
        function showTestNumbers(obj,~,~)
        % ShowTestNumbers is called by MessageService for view axes
        obj.MessageService.showTestNumbers(obj.Axes);
        end
        
        
        function onNodeUpdated(obj,~,~)
        update(obj)
        end
        
        function onButtonDown(obj,hAxes,~)
        % allow zoom in axes
        notify(obj,'ButtonDown')
        mv_zoom(hAxes)
        end
        
        function onShowLegend(obj,h,~)
        % toggle show legend
        obj.ShowLegend = h.Selected;
        showLegend(obj)
        end        
        
    end
    
    methods(Access=protected)
        function setUIContextMenu(obj)
        if ~isempty(obj.Axes)
            % attach context menu to axes
            set(obj.Axes,'UIContextMenu',obj.UIContextMenu);
        end
        setUIContextMenu@mbcgui.multiview.View(obj);
        end    
        
        function createActions(obj)
        %createActions Outlier actions, Show Legend and Test Numbers actions
        
        ms = obj.MessageService;
        AGoutliers = mbcgui.actions.ActionGroup('','&Outliers');
        aOutliers = ms.Actions.Outliers;
        AGoutliers.Actions = [aOutliers.MultiSelect, aOutliers.Clear, aOutliers.Remove, aOutliers.Restore ms.Actions.View.TestNumbers];
        AGoutliers.MenuType = 'separate';
        obj.Actions = AGoutliers;
        
        A = mbcgui.actions.ToggleAction(@obj.onShowLegend,'Show &Legend');
        set(A,'Selected',obj.ShowLegend)
        obj.Options = A;
        end     
        
        function plot(obj)
        %plot default plot calls specialPlots (should not be used)
        ms = obj.MessageService;
        specialPlots(ms.Model,obj.PlotType,obj.Axes,ms.XData,ms.YData,ms.DataOK);
        end
        
        function create(obj)
        %create create axes container
        
        obj.ContentHandle = mbcgui.widget.AxesContainer(...
            'Parent',obj.Parent,...
            'Border', [5 0 25 10],...
            'PositionSetting','outer');
        set(obj.Axes,...
            'Tag','Special Plot Axes',...
            'Units','pixels',...
            'NextPlot','add',...
            'XGrid','on',...
            'Box','on',...
            'YGrid','on',...
            'UIContextMenu',obj.UIContextMenu,...
            'ButtonDownFcn',@obj.onButtonDown);
        end        
        
        function showLegend(obj,AxHand)
        %showLegend show legend for model node
        
        if nargin<2
            AxHand = obj.Axes;
        end
        if obj.ShowLegend
            %labels are defined in message service
            [lineh,legLabels] = legendLabels(obj.MessageService,AxHand);
            
            if ~isempty(lineh)
                legend(lineh, legLabels, ...
                    'Location', 'NorthWest'); 
            end
        else
            legend(AxHand,'off');
        end
        end
        
    end
    
end