www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgsurfview/@app/app.m

    function h=app(nodes,selection,displayoptions)
%APP Constructor for CAGE surface
%
%  Viewer application class.  Parameter "nodes" is an array of pointers to
%  either feature or model nodes.  Parameter "selection" is optional and is
%  the index of the node to select initially.
%  Do not call this function directly.  Call "cgsurfaceviewer" instead.

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.


h = cgsurfview.app;
fig = xregfigure('Name','Surface Viewer',...
    'Position',[50 100 800 600],...
    'Visible','off',...
    'CloseRequestFcn',{@i_Close,h}, ...
    'Tag', 'cgsurfview');
xregpersistfigpos(fig);

h.fig = fig;
% Create menu, toolbar and statusbar before displaying figure
% create the evaluation panel
h.hEvalPanel = cgsurfview.evalpanel; % no figure supplied: non-graphical version
h.hEvalPanel.connect(h, 'up');

i_CreateMenu(h,fig);
toolbar = i_CreateToolbar(h,fig);

h.hStatusBar = xregGui.statusbar('parent', fig);
h.hStatusBar.WaitBar.Max = 100;
msgID = addMessage(h.hStatusBar,'Creating Surface Viewer...');
mainlayout = xreggridbaglayout(fig,'dimension',[3 1],...
    'elements',{toolbar,[],h.hStatusBar},...
    'rowsizes',[31 -1 20],'gapy',2,...
    'packstatus','off');
fig.LayoutManager = mainlayout;
set(mainlayout,'packstatus','on');

h.PR = xregGui.PointerRepository('create');
ptrid = h.PR.stackSetPointer(h.fig,'watch');
fig.Visible = 'on';
drawnow('expose');

% Set packstatus off immediately
setBoolPackstatus(mainlayout, false);

if nargin<3
    displayoptions = [];
    if nargin<2
        if ~isempty(nodes)
            selection=1;
        else
            selection=[];
        end
    end
end

% create data viewer, and extract information about available plot types.
h.hDataViewer = cgsurfview.dataviewer(fig,displayoptions,{@i_DataViewerStatus,h});
[h.ptnames,h.ptvars,h.ptdisplays,h.ptchecknames]=getPlotTypes(h.hDataViewer);
h.uptodate=zeros(length(h.ptnames),1); % all plot types are currently NOT up to date.

h.hStatusBar.WaitBar.Value = 15;


panel = mbcgui.container.titlebarpanel(...
    'Parent', fig, ...
    'Visible', 'off', ...
    'BarTitle','Data to Plot');

% create the data selector, on which the user is to assign datasets
% (e.g. 'Model','Error') to displays (e.g. 'Surface Height','Colour').
h.hDataSel = cgsurfview.datasel(panel,3,1);
h.hDataSel.checknames = h.ptchecknames{1};

% create the plot type selector
h.hPlotTypeSel=cgsurfview.plottype(panel,h.ptnames);


grid_inner=xreggridbaglayout(panel,'dimension',[2 1],...
    'rowsizes',[33 -1],...
    'elements',{h.hPlotTypeSel,h.hDataSel});
set(panel,'ContentHandle',{grid_inner});


% create the node selector
h.hNodeSel=cgsurfview.nodesel(fig,[],[]);

h.hStatusBar.WaitBar.Value = 30;

% initialise the input list
h.hInputList = cgsurfview.inputlist('Parent', fig, ...
    'Visible', 'off');

h.hStatusBar.WaitBar.Value = 42;

i_UpdateIL(h);

h.hStatusBar.WaitBar.Value = 55;

% create listeners
h.listeners = [...
    handle.listener(h.hPlotTypeSel, 'PlotTypeChange', {@i_PlotTypeChanged,h}),...
    handle.listener(h.hDataSel, 'DataSelectionChange', {@i_DataSelChanged,h}),...
    handle.listener(h.hDataSel, 'CheckStateChange', {@i_CheckStateChanged,h}),...
    handle.listener(h.hInputList, 'ValueChange', {@i_ValueChanged,h}),...
    handle.listener(h.hInputList, 'VariableChange', {@i_VariableChanged,h}),...
    handle.listener(h.hNodeSel, 'NodeSelectionChange', {@i_NodeSelChanged,h}),...
    handle.listener(h.hEvalPanel, 'EvaluateNow', {@i_EvaluateNow,h}),...
    handle.listener(h,'ObjectBeingDestroyed', {@i_ObjectBeingDestroyed,h})...
];

% layouts

grid_left=xreggridbaglayout(fig,'dimension',[3 1],...
    'visible','off', ...
    'elements',{h.hNodeSel, panel, h.hInputList},...
    'rowsizes',[-1 167 -1],...
    'gapy',2);
split=xregsplitlayout(fig,'visible', 'off', ...
    'split',[0.2867 0.7133],...
    'dividerwidth',4,'dividerstyle','flat', ...
    'left',grid_left,'right',h.hDataViewer);
set(mainlayout, 'elements', {toolbar,split,h.hStatusBar},...
    'packstatus','on');
set(mainlayout,'Visible','on');

h.hStatusBar.WaitBar.Value = 75;

h.setnodes(nodes,selection);
h.hDataSel.displays = h.ptdisplays{1};

h.hStatusBar.WaitBar.Value = 100;

h.PR.stackRemovePointer(h.fig, ptrid);
removeMessage(h.hStatusBar, msgID);

h.appstatus = addMessage(h.hStatusBar,'Ready');
h.hStatusBar.WaitBar.Value = 0;

h.connect(xregfigurehook(fig), 'up');
fig.UserData = h;

i_PlotTypeChanged([],[],h);

%-------------------------
function i_UpdateIL(obj)

msgID=addMessage(obj.hStatusBar,'Updating controls...');
ptrID=obj.PR.stackSetPointer(obj.fig,'watch');

% Wrap this section in a "try" block so that we change the pointer
% and status bar back even if there is an error
try
	ptrs=[];
	selectedindices=obj.hNodeSel.selection;
	for i=1:length(selectedindices)
		nodeindex=selectedindices(i);
		nodeptr=obj.hNodeSel.nodes(nodeindex);
		ptrs=[nodeptr.getinputs ptrs];
	end
	ptrs=unique(ptrs);

    obj.hInputList.setInputs(ptrs, obj.ptvars{obj.hPlotTypeSel.plottype});
    i_EnableOpPoint(obj)

    drawnow('expose');
    obj.PR.stackRemovePointer(obj.fig,ptrID);
    removeMessage(obj.hStatusBar,msgID);
catch ME
    obj.PR.stackRemovePointer(obj.fig,ptrID);
    removeMessage(obj.hStatusBar,msgID);
    rethrow(ME);
end


%-----------------------------
function i_PlotTypeChanged(~,~,obj)

% Turn off cursor mode (for the previous plot type) if
% it was on
i_SetCursorMode(obj,'off');

plottypeindex=obj.hPlotTypeSel.plottype;

if obj.hDataViewer.canPrint(plottypeindex)
    set(obj.printmenus,'Enable','on');
    set(obj.printbtns,'Enable','on');
else
    set(obj.printmenus,'Enable','off');
    set(obj.printbtns,'Enable','off');
end

% update the data selector
obj.hDataSel.displays=obj.ptdisplays{plottypeindex};
obj.hDataSel.checknames=obj.ptchecknames{plottypeindex};

% update the input list
msgID=addMessage(obj.hStatusBar,'Updating controls...');
ptrID=obj.PR.stackSetPointer(obj.fig,'watch');

obj.hInputList.AxisNames = obj.ptvars{plottypeindex};

state=obj.hDataViewer.optionsAllowed(plottypeindex);
set(obj.optionsbutton,'Enable',state);
set(obj.optionsmenu,'Enable',state);

state=obj.hDataViewer.cursorAllowed(plottypeindex);
set(obj.cursorbutton,'Enable',state);
set(obj.cursormenu,'Enable',state);

obj.PR.stackRemovePointer(obj.fig,ptrID);
removeMessage(obj.hStatusBar,msgID);

i_DoUpdate(obj);

%-----------------------------
function i_DataSelChanged(~,~,obj)

% this renders all plots out of date.
obj.uptodate=zeros(length(obj.ptnames),1);
i_DoUpdate(obj);

%------------------------------
function i_VariableChanged(~,~,obj)

% this renders all plots out of date.
obj.uptodate=zeros(length(obj.ptnames),1);
i_DoUpdate(obj);


%------------------------------
function i_ValueChanged(~,~,obj)

% this renders all plots out of date.
obj.uptodate=zeros(length(obj.ptnames),1);
i_DoUpdate(obj);


%-----------------------------
function i_NodeSelChanged(~,~,obj)

i_UpdateIL(obj);
% this renders all plots out of date.
obj.uptodate=zeros(length(obj.ptnames),1);
i_DoUpdate(obj);


%-----------------------------
function i_EvaluateNow(~,~,obj)

obj.redraw;


%------------------------------
function i_DoUpdate(obj)

plottypeindex=obj.hPlotTypeSel.plottype;
if obj.uptodate(plottypeindex)~=0
	% this plot is already up to date.  show it.
	obj.hDataViewer.show(plottypeindex);
elseif (obj.hEvalPanel.autoeval)
	% recalculate now
	obj.redraw;
else
	% wait for the user to ask for an update
	obj.hDataViewer.show(0);
end


%-----------------------------
function i_CreateMenu(obj,fig)

m=uimenu(fig, 'Label', '&File');
uimenu(m, 'Label', '&Export to CSV...', 'Callback', {@i_Export,obj});
obj.printmenus = [ ...
    uimenu(m, 'Label', '&Print...', 'Callback',{@i_Print,obj,0},'Accelerator','p'),...
    uimenu(m, 'Label', 'C&opy to Clipboard', 'Callback',{@i_Print,obj,1},'Accelerator','c') ...
    uimenu(m, 'Label', 'Print to &Figure', 'Callback',{@i_Print,obj,2},'Accelerator','f') ...
];
uimenu(m, 'Label', '&Close', 'Callback', {@i_Close,obj},'Separator','on','Accelerator','w');

m=uimenu(fig, 'Label', '&View');
obj.statsmenu = uimenu(m, 'Label', '&Statistics', 'Callback', {@i_Stats,obj});
obj.optionsmenu=uimenu(m, 'Label', '&Display Options', 'Callback', {@i_DisplayOptions,obj});

obj.cursormenu = uimenu(m, 'Label', '&Cursor Mode', 'Callback', {@i_DataViewerCursor,obj,1});

m=uimenu(fig,'Label', '&Tools');
obj.autoevalmenu=uimenu(m, 'Label', '&Auto-Evaluate',...
                           'Checked', obj.hEvalPanel.getautoeval,...
                           'Callback', {@i_AutoEvalMenu,obj});
uimenu(m, 'Label', '&Evaluate Now', 'Callback', {@i_EvalNow,obj});
obj.oppointmenu = uimenu(m, 'Label', '&Select Operating Point', 'Callback', {@i_SelectOpPoint,obj});



cgwinlist(fig);
cghelpmenu(fig,{'&Surface Viewer Help','CGSURFVIEW'});


%-----------------------------
function toolbarlayout = i_CreateToolbar(obj,fig)
toolbarlayout = mbcgui.container.layoutpanel(...
    'Parent', fig, ...
    'BorderType', 'beveledin');
toolbar=xregGui.uitoolbar('Parent',toolbarlayout,'ResourceLocation',cgrespath);
toolbar.setRedraw(false);
obj.printbtns = xregGui.uipushtool(toolbar,'ImageFile','print.bmp',...
                           'transparentcolor',[0 255 0],...
                           'TooltipString','Print',...
                           'ClickedCallback',{@i_Print,obj,0});
obj.printbtns(2) = xregGui.uipushtool(toolbar,'ImageFile','copy.bmp',...
                           'transparentcolor',[0 255 0],...
                           'TooltipString','Copy to clipboard',...
                           'ClickedCallback',{@i_Print,obj,1});
obj.printbtns(3) = xregGui.uipushtool(toolbar,'ImageFile',xregrespath('printtofigure.bmp'),...
                           'transparentcolor',[0 255 0],...
                           'TooltipString','Print to figure',...
                           'ClickedCallback',{@i_Print,obj,2});
obj.autoevalbutton=xregGui.uitoggletool(toolbar,'ImageFile','evalauto.bmp',...
                           'transparentcolor',[0 255 0],...
                           'Separator','on',...
                           'TooltipString','Auto-evaluate',...
                           'State', obj.hEvalPanel.getautoeval,...
                           'OnCallback',{@i_AutoEvalButton,obj,1},...
                           'OffCallback',{@i_AutoEvalButton,obj,0});
xregGui.uipushtool(toolbar,'ImageFile','eval.bmp',...
                           'transparentcolor',[0 255 0],...
                           'TooltipString','Evaluate now',...
                           'ClickedCallback',{@i_EvalNow,obj});
obj.oppointbutton=xregGui.uipushtool(toolbar,'ImageFile','svSelectSwitchPoint.bmp',...
                           'transparentcolor',[0 255 0],...
                           'TooltipString','Select operating point...',...
                           'ClickedCallback',{@i_SelectOpPoint,obj}); 
obj.statsbutton = xregGui.uipushtool(toolbar,'ImageFile','stats.bmp',...
                           'transparentcolor',[0 255 0],...
                           'Separator','on',...
                           'TooltipString','View statistics',...
                           'ClickedCallback',{@i_Stats,obj});
obj.optionsbutton=xregGui.uipushtool(toolbar,'ImageFile','options.bmp',...
                           'transparentcolor',[0 255 0],...
                           'TooltipString','Display options...',...
                           'ClickedCallback',{@i_DisplayOptions,obj});
obj.cursorbutton = xregGui.uitoggletool(toolbar,'ImageFile','cgdatatip.bmp',...
                           'transparentcolor',[0 255 0],...
                           'TooltipString','Turn on cursor more',...
                           'ClickedCallback',{@i_DataViewerCursor,obj,0});

                      
                       
cghelptoolbutton(toolbar,'CGSURFVIEW');
toolbar.setRedraw(true);
toolbar.drawToolBar;
set(toolbarlayout, 'LayoutComponent', toolbar);



%-----------------------------
function i_Stats(~,~,obj)

fig = obj.stats;
waitfor(fig,'visible');

%---------------------------------
function i_Print(~,~,obj,flag)

obj.print(flag);

%---------------------------------
function i_Close(~,~,obj)

obj.Visible = 'off';

%-------------------------------
function i_Export(~,~,obj)

obj.export([]);

%-------------------------------
function i_AutoEvalButton(~,~,obj,newval)

obj.hEvalPanel.autoeval = newval;
set(obj.autoevalmenu,'Checked',obj.hEvalPanel.getautoeval);

%-------------------------------
function i_AutoEvalMenu(~,~,obj)

state=get(obj.autoevalmenu,'Checked');
% the menu doesn't automatically change state when it is clicked
if strcmpi(state,'on')
    state='off';
else
    state='on';
end
obj.hEvalPanel.setautoeval(state);
set(obj.autoevalmenu,'Checked',state);
obj.autoevalbutton.state=state;

%--------------------------------
function i_EvalNow(~,~,obj)

obj.hEvalPanel.evalnow;


%--------------------------------
function i_DisplayOptions(~,~,obj)

out = obj.hDataViewer.editOptions(obj.hPlotTypeSel.plottype);
if out~=0
    obj.redraw;
end


%--------------------------------
function i_CheckStateChanged(~,~,obj)

obj.redraw;


%--------------------------------
function i_ObjectBeingDestroyed(~,~,obj)
if isgraphics(obj.fig) && ~mbcgui.util.isBeingDestroyed(obj.fig)
    delete(obj.fig);
end

%--------------------------------
function i_DataViewerStatus(~,evt,obj)

action = evt.action;

switch action
    case 'set'
        string = evt.string;
        if obj.dvstatus
            obj.hStatusBar.changeMessage(obj.dvstatus,string);
        else
            obj.dvstatus = obj.hStatusBar.addMessage(string);
        end
    case 'clear'
        obj.hStatusBar.removeMessage(obj.dvstatus);
        obj.dvstatus = 0;
    otherwise
        error(message('mbc:cgsurfview:app:InvalidArgument'));
end

%-----------------------
function i_DataViewerCursor(src,~,obj,ismenu)

if ismenu
    on_off = {'on', 'off'};
    % Get the current menu state
    checked = get(src,'Checked');
    % Need to invert it - if the menu is checked then we are requesting
    % that the cursor should be turned off (and vice versa)
    state = on_off{~strcmp( on_off, checked )};
else
    state = get(src,'State');
end

i_SetCursorMode(obj,state)

%----------------------
function i_SetCursorMode(obj,newstate)

if strcmp(newstate,'on')
    set(obj.cursorbutton,'State','on','TooltipString','Turn cursor mode off');
else
    set(obj.cursorbutton,'State','off','TooltipString','Turn cursor mode on');
end
set(obj.cursormenu,'Checked',newstate);
setCursorMode(obj.hDataViewer,newstate);
obj.hStatusBar.removeMessage(obj.dvstatus);
obj.dvstatus = 0;


function i_SelectOpPoint(~,~,obj)

setOperatingPoints(obj)

function i_EnableOpPoint(obj)

sel = obj.hNodeSel.selection;
nodeptr = obj.hNodeSel.nodes(sel);
OpPointOK =  ~isempty(nodeptr);
if OpPointOK 
    nodeptr = nodeptr(1);
    OpPointOK = (strncmpi(nodeptr.gettype,'Point-by-point model',14) || ...
        strncmpi(nodeptr.gettype,'Composite model',15) )&& ...
        isequal(nodeptr.getinputs,getinports(info(nodeptr.getdata)));
end
if OpPointOK
    set(obj.oppointmenu,'Enable','on')
    set(obj.oppointbutton,'Enable','on')
else
    set(obj.oppointmenu,'Enable','off')
    set(obj.oppointbutton,'Enable','off')
end