www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptim/guiDatasetSelector.m

    function [optim, ok] = guiDatasetSelector(optim, indx, pPROJ)
%GUIDATASETSELECTOR  Gui to select dataset for optim
%
%  [OPTIM, OK] = GUIDATASETSELECTOR(OPTIM, IDX,  P_PROJECT) where OPTIM is
%  a cgoptim object, IDX is the index of the data item that is being edited
%  and P_PROJECT is a pointer to a cgproject to select datasets from.

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


nodes = filterbytype(pPROJ.info, cgtypes.cgdatasettype);
pAllDS = mbccellarrayeval(nodes, @address, {}, mbcpointer);

% Create a filtered list of those data sets with the required pointers
DSvals = get(optim, 'oppointValues');
pFilteredDS = i_FilterDisallowedDS(nodes, DSvals{indx}, pAllDS);

pSelOp = optim.oppoints.Datasets(indx);
pSelDS = mbcpointer(1);
if isvalid(pSelOp)
    for n = 1:length(nodes)
        if getdata(nodes{n})==pSelOp
            pSelDS = address(nodes{n});
            break
        end
    end
end

hDlg = mbcgui.container.Dialog('Name', 'Select Data Set', ...
    'Tag', 'cgdatasetselector', ...
    'Buttons', 'OK_CANCEL',...
    'Size', [260, 300], ...
    'PersistPosition', false, ...
    'Resize', 'off');
fh = hDlg.Figure;

check = uicontrol('Parent', fh, ...
    'Style', 'checkbox', ...
    'String', 'Only show data sets with the required factors', ...
    'Callback', @i_changefilter);
List = cgtools.nodeList('parent', fh, ...
    'DisplayTypeColumn', false, ...
    'DisplayHeaderRow', false);
txt = uicontrol('Parent', fh, ...
    'Style', 'text', ...
    'HorizontalAlignment', 'left', ...
    'String', 'Current selection:'); 
      
i_updateseltext;
if ~isvalid(pSelDS) || ismember(pSelDS, pFilteredDS)
    set(List, 'Items', pFilteredDS, ...
        'SelectedItems', pSelDS);
    set(check, 'Value', 1);
else
    set(List, 'Items', pAllDS, ...
        'SelectedItems', pSelDS);
    set(check, 'Value', 0);
end

ListListener = handle.listener(List, 'Open', @(src, evt) closeDialog(hDlg,'OK'));
ListSelListener = handle.listener(List, 'SelectionChanged', @i_updatesel);

lyt = xreggridbaglayout(fh, ...
    'packstatus', 'off', ...
    'dimension', [6 1], ...
    'rowsizes', [15 2 -1 5 15 7], ...
    'gapx', 7, ...
    'elements',{check, [], List, [], txt, []});

hDlg.Content = lyt;

% finally set dialog visible.  This call blocks the execution thread
closeMode = hDlg.showDialog;
OK = strcmp(closeMode, 'OK');

if OK
    % OK pressed
    if ~isempty(pSelDS) && isvalid(pSelDS)
       optim.oppoints = editDataset(optim.oppoints,indx , pSelDS.getdata);
    else
       optim.oppoints = editDataset(optim.oppoints,indx ,  mbcpointer(1));
    end
    ok = true;
else
    % Cancel pressed
    ok = false;
end
delete(hDlg)

    function i_changefilter(src, evt)
        if ~isempty(List.SelectedItems)
            pSelDS = List.SelectedItems;
        end
        if get(src, 'Value')==1
            set(List, 'Items', pFilteredDS, 'SelectedItems', pSelDS);
        else
            set(List, 'Items', pAllDS, 'SelectedItems', pSelDS);
        end      
    end

    function i_updatesel(src, evt)
        if ~isempty(List.SelectedItems)
            pSelDS = List.SelectedItems;
        end
        i_updateseltext;
    end

    function i_updateseltext
        if ~isempty(pSelDS) && isvalid(pSelDS)
            nm = pSelDS.name;
        else
            nm = '';
        end
        set(txt, 'String', sprintf('Current selection:  %s', nm));
    end

end

function out = i_FilterDisallowedDS(nodes, reqdptrs, pNodes)
IsOK = false(size(nodes));
for n = 1:length(nodes)
    pOp = getdata(nodes{n});
    ptrs = get(pOp.info, 'ptrlist');
    IsOK(n) = all(ismember(reqdptrs, ptrs));
end
out = pNodes(IsOK);
end