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

    function h = Callbacks(TP,subfunc,varargin) %#ok<INUSL>
%CALLBACKS  Various cgfeature GUI callbacks
%
%  CALLBACKS(TP, 'GetHandles') returns a cell array of function handles
%  to subfunctions available:
%
%  CALLBACKS(TP, FUNC, ARGS) executes the internal function FUNC and passes
%  it ARGS

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

cgb = cgbrowser;
d = cgb.getViewData;
d = pMessage(d, '');
cgb.setViewData(d);


if ischar(subfunc)
    if strcmpi(subfunc,'gethandles')
        h=struct('HistoryEdit',@i_HistoryEdit,...
            'HistoryDetails',@i_HistoryDetails,...
            'HistoryLabelEdit', @i_HistoryLabelEdit,...
            'HistoryListClick', @i_HistoryListClick,...
            'Delete',@i_Delete,...
            'Clear',@i_Clear,...
            'SelModel',@i_SelectModel,...
            'DeselModel',@i_DeselectModel,...
            'FeatureModel',@i_FeatureToModel,...
            'SLEdit',@i_SLEdit,...
            'SLClose',@i_SLClose,...
            'SLImport',@i_SLImport,...
            'SLUpdate',@i_SLUpdate,...
            'SLExport',@i_SLExport,...
            'Initialise',@i_Initialise,...
            'Fill', @i_Fill,...
            'ClearFill',@i_ClearFill,...
            'Expansion',@i_Expansion,...
            'SetupViewMenu', @i_SetupViewMenu,...
            'ToggleLibrary', @i_ToggleLibrary);
    else
        feval(subfunc,varargin{:});
        h=[];
    end
end

% ----------------------------------------------------------
function i_SetupViewMenu(~, ~, varargin)
% ----------------------------------------------------------
% This sets up the checks on the View menu depending on what
% libraries are currently open.

c = cgbrowser;
N = c.CurrentNode;
d = c.getViewData;

libs = pGetFeatureLibraries;
menus = d.Handles.LibMenu;

for n=1:size(libs,1)
    isopen = feval(libs{n,2}{3}, N.info);
    if isopen
        set(menus(n), 'Checked','on');
    else
        set(menus(n), 'Checked','off');
    end
end
%----------------------------------------------------------
function i_ToggleLibrary(src, ~)
%----------------------------------------------------------
% callback from library menu item
% src's userdata stores the functions to call

c = cgbrowser;
N = c.CurrentNode;

fcns = get(src, 'UserData');
openCB = fcns{1};
closeCB = fcns{2};

oldState = get(src,'Checked');
switch oldState
    case 'on'
        feval(closeCB, N.info)
    case 'off'
        feval(openCB, N.info)
end

% ----------------------------------------------------------
function i_HistoryEdit(obj,~,action)
% ----------------------------------------------------------
% callback from the Add and Remove buttons

if nargin==0
    action = get(gcbo,'String');
elseif nargin<3 && ~isempty(obj)
    action = get(obj,'String');
end

CGBH = cgbrowser;
d = CGBH.getViewData;
pFN = CGBH.CurrentNode;
pF = pFN.getdata;

if isempty(pF)
    return
end

switch action
    case 'Remove'
        d = pHistoryManager( pFN, d, 'remove' );
    case 'Add'
        d = pHistoryManager( pFN, d, 'add' );        
end
CGBH.setViewData( d )

%---------------------------------------------------------------------------------------
function i_HistoryDetails(~,~)
%---------------------------------------------------------------------------------------
% respond to a change in the details for a history item

CGBH = cgbrowser;
d=CGBH.getViewData;
pFN = CGBH.CurrentNode;
d = pHistoryManager( pFN, d, 'detailsedit' ); 
CGBH.setViewData( d );

% ----------------------------------------------------------
function i_HistoryLabelEdit(~,evt)
% ----------------------------------------------------------
% respond to a click on the listview
CGBH = cgbrowser;
d = CGBH.getViewData;
pFN = CGBH.CurrentNode;
newname = evt.data.NewValue;
row = evt.data.Rows;
d = pHistoryManager( pFN, d, 'rename', newname, row );
CGBH.setViewData( d );

% ----------------------------------------------------------
function i_HistoryListClick(~,varargin)
% ----------------------------------------------------------
% respond to a click on the listview
CGBH = cgbrowser;
d = CGBH.getViewData;
pFN = CGBH.CurrentNode;
% just need to refresh 
d = pHistoryManager( pFN, d, 'refreshdetails' );
CGBH.setViewData( d );



%---------------------------------------------------------------------------------------
function i_Delete(~,~)
%---------------------------------------------------------------------------------------
i_Clear([],[]);
CGBH = cgbrowser;
pFN = CGBH.CurrentNode;
pF = pFN.getdata;
CGBH.setnewnode(pFN.Parent,xregpointer);
pFN.delete;
freeptr(pF);
doDrawTree(CGBH);

%---------------------------------------------------------------------------------------
function i_SelectModel(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;

pFN = CGBH.CurrentNode;
pF = pFN.getdata;
[newF, ok] = gui_setmodel(pF.info, address(pFN.project));
if ok
    pF.info = newF;
    CGBH.doDrawTree(pFN,'update');
    ViewNode(CGBH);
end

%---------------------------------------------------------------------------------------
function i_DeselectModel(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
pFN = CGBH.CurrentNode;
pF = pFN.getdata;
pF.info = pF.set('model',[]);
CGBH.doDrawTree(pFN,'update');
ViewNode(CGBH);

%---------------------------------------------------------------------------------------
function i_FeatureToModel(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
pFN = CGBH.CurrentNode;
pF = pFN.getdata;
 
eq = pF.get('equation');
if isempty( eq )
    % empty pointer
    i_error('Failed to create model because this feature has no strategy.');
    return
else
    % Check that all chain inputs are OK.  We need a special case function
    % for any subfeatures
    isOK = pveceval(eq.getAllInputs, @i_CheckOKForModel);
    if ~all([isOK{:}])
        i_error(['A model could not be created from this feature because it' ...
            ' was empty or not properly initialized.']);
        return
    end
end
    
inputPtrs = pF.getdditems;
e = cgexprmodel(pF, inputPtrs);
M = xregpointer(cgmodexpr(pF.getname, e));
M.info = M.set('ptrlist',inputPtrs);

pFN.addtoproject(M);

d = CGBH.getViewData;
d = pMessage(d, sprintf( 'Feature %s successfully converted to model %s', pF.getname, M.getname ) );
CGBH.setViewData(d);

CGBH.ViewNode;

% Checking function that doesn't call the plain isempty on cgfeature
% objects
function OK = i_CheckOKForModel(obj)
if ~isa(obj, 'cgfeature')
    OK = ~isempty(obj);
else
    % Check whether the feature has an equation
    OK = ~isempty(get(obj, 'equation'));
end


%---------------------------------------------------------------------------------------
function i_SLEdit(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
pFN = CGBH.CurrentNode;
d = CGBH.getViewData;
set([d.Handles.LibMenu d.Handles.ParseMenu],'Enable','on');

[pFN.info, d.sys] = editstrategy( pFN.info );
handleToSys = handle(get_param(d.sys,'Handle'));
d.sysCloseListener = handle.listener(handleToSys, 'CloseEvent', {@iSimulinkCloseEvent, CGBH});
d = pMessage(d, 'Double click on a blue outport to parse the required strategy');
CGBH.setViewData(d);

%---------------------------------------------------------------------------------------
function i_SLImport(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
pFN = CGBH.CurrentNode;
pFN.importstrategy;


%---------------------------------------------------------------------------------------
function i_SLUpdate(hSource,~)
%---------------------------------------------------------------------------------------

CGBH = cgbrowser;
pFN = CGBH.CurrentNode;
if isempty( hSource )
    % this is a callback from the simulink diagram
    sys = get_param(gcs,'handle');
    block = get_param(gcb,'handle');
else
    % this is a menu callback
    d=CGBH.getViewData;
    sys = d.sys;
    if ~isempty( gcb )
        block = get_param(gcb,'handle');
    end
    if bdroot(block) ~= sys || ~strcmp(get_param(block,'blocktype'),'Outport')
        % gcb is the wrong block
        block = find_system(sys,'searchdepth',1,'BlockType','Outport');
        if ~isscalar(block) 
            return;
        end
    end
end

% Now we have the correct system and block, read the strategy in
PR = xregGui.PointerRepository;
ptrID = PR.stackSetPointer(CGBH.Figure, 'watch');
[~,OK]= pFN.readstrategy( sys, block );

CGBH.doDrawTree(pFN,'refresh');
CGBH.ViewNode;
PR.stackRemovePointer(CGBH.Figure, ptrID);

if OK
    % close library files
    i_SLClose
    if ~isempty( hSource )
        % close Simulink if parsed from menu callback
        close_system(sys,0)
    else
        % can't close when callback is from the Simulink Diagram
        % so do this in a timer so the close occurs after the end of the callback
        f = @(t,d)close_system(sys,0);
        t = timer('TimerFcn',f,'StopFcn',@(t,d)delete(t),'StartDelay',1);
        start(t);
    end
end

%---------------------------------------------------------------------------------------
function i_Clear(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
pFN = CGBH.CurrentNode;
pFN.clearstrategy;
CGBH.doDrawTree(pFN,'refresh');
CGBH.ViewNode;

%---------------------------------------------------------------------------------------
function i_SLExport(~,~)
%---------------------------------------------------------------------------------------

CGBH = cgbrowser;
pFN = CGBH.CurrentNode;
pFN.exportstrategy;

%---------------------------------------------------------------------------------------
function i_Expansion(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
d=CGBH.getViewData;

if strcmp(get(d.Handles.expansionMenuCheck,'Checked'),'on');
    set(d.Handles.expansionMenuCheck,'Checked','off');
    d.Handles.equationview.ShortMode = 'on';
else
    set(d.Handles.expansionMenuCheck,'Checked','on');
    d.Handles.equationview.ShortMode = 'off';
end

%---------------------------------------------------------------------------------------
function i_Initialise(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
d=CGBH.getViewData;

cnode = CGBH.currentnode;
feat = cnode.getdata;

% if the initialisation manager is empty, then (re)create it
if isempty(d.InitialisationManager)
    [d.InitialisationManager, setupOK, msg] = init(feat.info);
else
    setupOK = 1;
end

if ~setupOK
    d.InitialisationManager = [];
    CGBH.setViewData(d);
    i_error(['Cannot initialize this feature. ' msg]);
    return
end

d = pMessage(d, 'Creating the initialization option tree ...');
PR = xregGui.PointerRepository;
ptrID = PR.stackSetPointer(CGBH.Figure,'watch');
[d.InitialisationManager,OK] = gui_setup(d.InitialisationManager,'figure',{'expanded',1,'title', 'Feature Initialization Options'});
PR.stackRemovePointer(CGBH.Figure, ptrID);
d = pMessage(d, '');

if ~OK
    d.InitialisationManager = [];
    CGBH.setViewData(d);
else
    drawnow('expose');
    CGBH.setViewData(d); 
    try
        [feat.info, ~, runOK, msg] = run(d.InitialisationManager, feat.info, []);
        if ~runOK
            i_error(msg);
        else
            d = pMessage(d, sprintf('Initialized feature %s', feat.getname ) );
            CGBH.setViewData(d);
        end
        
        % Update icons on tree and the view
        CGBH.doDrawTree([], 'update');
        CGBH.ViewNode;
    catch ME
        i_error(['Unknown error occurred during feature initialization. ' ME.message]);
    end
    PR.stackClearAndReset(CGBH.Figure);
end


%---------------------------------------------------------------------------------------
function i_Fill(~,~)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
d = CGBH.getViewData;
p= get(CGBH,'currentnode');
pf= p.getdata;
cgp= get(CGBH,'rootnode');

F = pf.get('cgsimfill');
% find which fill to run
row = d.Handles.FillDetails.SelectedRows;
if ~isempty(row)
    % selected fill setting
    pf.info = set(pf.info,'cgsimfill',F(row));
elseif ~isempty(F)
    % no row selected - trying to fill existing settings but none selected in table
    return  
end
[runOK,thisFill]= cgFillWizard(pf,'Project',cgp);
if runOK
    if ~isempty(row)
        F(row) = thisFill;
    else
        F = thisFill;
        row = length(F);
    end
    details = detailsTable(thisFill);
    comment = sprintf('%s filled using %s with %s.  RMSE=%g',details{1},details{2},details{3},thisFill.RMSE);
    pf.info = pf.addhistoryitem(comment, '');
    
    pMessage(d, sprintf('Filled feature %s', pf.getname ) );
end
pf.info = set(pf.info,'cgsimfill',F);

CGBH.doDrawTree([], 'update');
CGBH.ViewNode;

if ~isempty(row)
    d.Handles.FillDetails.SelectedRows = row;
end


function i_ClearFill(~,~)

CGBH = cgbrowser;
p= get(CGBH,'currentnode');
pf= p.getdata;
% clear fill settings
pf.info = set(pf.info,'cgsimfill',[]);
% disable clear menu
CGBH.ViewNode;
%---------------------------------------------------------------------------------------
function i_error(str)
%---------------------------------------------------------------------------------------
uiwait(errordlg(str,'CAGE Error','modal'));


%--------------------------------------------------------------------------
function iSimulinkCloseEvent(~, ~, CGBH)
%--------------------------------------------------------------------------
d = CGBH.getViewData;
d.sysCloseListener = [];
CGBH.setViewData(d);
doCloseSimulinkModel(CGBH)


%--------------------------------------------------------------------------
function i_SLClose(~, ~)
%--------------------------------------------------------------------------
% CloseFcn of the main simulink model when editing or importing strategies
CGBH = cgbrowser;
doCloseSimulinkModel(CGBH);

%--------------------------------------------------------------------------
function doCloseSimulinkModel(CGBH)
%--------------------------------------------------------------------------
if( CGBH.GUIExists )
    d = CGBH.getViewData;
    n = CGBH.CurrentNode;
    sysName = gcs;
    if shouldDisableMenus(d, sysName)
        % Set menu disabled
        set([d.Handles.LibMenu, d.Handles.ParseMenu],'Enable','off');
    end
else
    % for testing only - need a feature node to pass to pToggleLibs
    n = cgfeaturenode;
end

% toggle the libraries closed
pToggleLibs(0, n);

%--------------------------------------------------------------------------
function disableMenus = shouldDisableMenus(d, sysBeingClosedName)
%--------------------------------------------------------------------------
%shouldDisableMenus Menus should be disabled if the menu items exist and
% the name of the system that is being closed matches the one that we have 
% stored
if ~isempty(d) && isfield(d,'Handles')
    hasMenus = isfield(d.Handles, 'LibMenu') && isfield(d.Handles, 'ParseMenu');
else
    hasMenus = false;
end
if isfield(d,'sys') && ~isempty(d.sys) && ishandle(d.sys)
    % compare closing system name with stored system name
    sysName = get(d.sys,'Name');
    sysNameMatches = strcmp(sysName, sysBeingClosedName);
else
    sysNameMatches = false;
end
disableMenus = hasMenus && sysNameMatches;