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

    function h=datasel(fig,max_menu,max_check)
%DATASEL Constructor for the data selection panel
%
%  DATASEL is the constructor for the panel which allows the user to select
%  from the available sets of data, e.g. model, strategy, both, error.
%
%  H=DATASEL(FIG,DATASETS,DISPLAYS) where:
%  FIG is a figure handle.
%  DATASETS is a cell array of strings giving names of data sets which
%  can be plotted, e.g. { 'model', 'strategy', 'error'}.
%  DISPLAYS is a cell array of strings describing how data can be displayed
%  e.g. {'surface height','surface colour'}.
%
%  The user is given the option "None" which is assigned index zero.

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


if (nargin<3)
	error(message('mbc:cgsurfview:datasel:TooFewInputs'))
end

h=cgsurfview.datasel;

h.fig=fig;
h.max_menu = max_menu;
h.max_check = max_check;

h.checkstates=zeros(1,max_check);

sc=xregGui.SystemColorsDbl;

labels = cell(max_menu,1);
combos = cell(max_menu,1);
checkboxes = cell(max_check,1);

hPanel = mbcgui.container.layoutpanel(...
    'Parent', fig, ...
    'BorderType', 'beveledout', ...
    'LayoutBorder', [5 0 5 5]);
for k=1:max_menu 
    combos{k}=uicontrol('Parent',hPanel,...
        'Style','popupmenu',...
        'Callback',{@i_MenuChange,h,k},...
        'Visible','off',...
        'BackgroundColor',sc.WINDOW_BG);
    labels{k}=xregGui.labelcontrol('parent',hPanel,...
        'visible','off',...
        'LabelAlignment','left','gap',5,...
        'LabelSize',1,'ControlSize',1,...
        'LabelSizeMode','relative','ControlSizeMode','relative',...
        'control',combos{k});
end
for k=1:max_check
    checkboxes{k} = uicontrol('Parent',hPanel,...
            'Style','checkbox',...
            'Callback',{@i_CheckboxChange,h,k},...
            'Visible','off');
end

h.grid = xreggridbaglayout(fig,'packstatus','off',...
    'dimension',[max_menu+max_check 1],...
    'gapy',5, ...
    'rowsizes',repmat(21,max_menu+max_check,1),...
    'elements',cell(max_check+max_menu,1) );
set(hPanel, 'LayoutComponent', {h.grid});
h.layout= hPanel;

h.labels = labels;
h.combos = combos;
h.checkboxes = checkboxes;

h.listeners = [
    handle.listener(h,h.findprop('datasets'),'PropertyPostSet',{@i_DataSetChange,h}),...
    handle.listener(h,h.findprop('displays'),'PropertyPostSet',{@i_DisplaySetChange,h}),...
    handle.listener(h,h.findprop('checknames'),'PropertyPostSet',{@i_CheckNamesChange,h}),...
    handle.listener(h,h.findprop('selection'),'PropertyPostSet',{@i_SelectionSet,h}),... 
    handle.listener(h,h.findprop('checkstates'),'PropertyPostSet',{@i_CheckStateSet,h}),... 
    handle.listener(h,h.findprop('cbenable'),'PropertyPostSet',{@i_CheckEnableSet,h}),...
];

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



%-----------------------------
function i_DataSetChange(src,evt,obj)

if isempty(obj.datasets)
    set([obj.combos{1:end}],'Enable','off');
    set([obj.combos{1:end}],'String',{''}, 'Value', 1);
else
    set([obj.combos{1:end}],'Enable','on');
    
    % set the strings in the combo boxes
    set(obj.combos{1},'String',obj.datasets, 'Value', 1);
    set([obj.combos{2:end}],'String',[{'None'},obj.datasets], 'Value', 1);
    
    % Reset the combobox selections.
    
    % Clear selections, including those in hidden popup menus.
    obj.selection = [1 zeros(1,obj.max_menu-1)];
end


%-----------------------------
function i_DisplaySetChange(src,evt,obj)

nshow = length(obj.displays); % number to show
nhide = obj.max_menu - nshow; % number to hide

if (nshow>obj.max_menu)
	error(message('mbc:cgsurfview:datasel:InvalidState'));
end

elements = get(obj.grid,'elements');

elements = [ obj.labels(1:nshow) ; ...
             repmat({[]},nhide,1) ; ...
             elements(obj.max_menu+1:obj.max_menu+obj.max_check) ];

set(obj.grid,'elements',elements);

for k=1:nshow
    set(obj.labels{k},'Visible','on');
end
for k=nshow+1:obj.max_menu
    set(obj.labels{k},'Visible','off');
end

selection = zeros(1,obj.max_menu);
for k=1:nshow
    set(obj.labels{k},'String',[obj.displays{k} ' : ']);
    selection(k) = get(obj.combos{k},'Value');
    if k>1
        selection(k) = selection(k) - 1;
    end
end

set(obj.listeners(4),'enabled','off'); % briefly turn off the listener on "selection".
obj.selection = selection;
set(obj.listeners(4),'enabled','on');



%-----------------------------
function i_CheckNamesChange(src,evt,obj)

nshow = length(obj.checknames); % number to show
nhide = obj.max_check - nshow; % number to hide

if (nshow>obj.max_menu)
	error(message('mbc:cgsurfview:datasel:InvalidState'));
end

elements = get(obj.grid,'elements');

elements = [ elements(1:obj.max_menu) ; ...
             obj.checkboxes(1:nshow) ; ...
             repmat({[]},nhide,1) ];

set(obj.grid,'elements',elements);
set([obj.checkboxes{:}],'Enable',mbconoff(obj.cbenable));

for k=1:nshow
    set(obj.checkboxes{k},'Visible','on');
end
for k=nshow+1:obj.max_check
    set(obj.checkboxes{k},'Visible','off');
end

for k=1:nshow
    set(obj.checkboxes{k},'String',obj.checknames{k});
end

%------------------------------
function i_MenuChange(control,data,obj,index)

x=obj.selection;
if (index==1)
    x(index)=get(control,'Value');
else
    % subtract one from the index to get the selection number
    x(index)=get(control,'Value')-1;
end

set(obj.listeners(4),'enabled','off'); % briefly turn off the listener on "selection".
obj.selection=x;
set(obj.listeners(4),'enabled','on');

send(obj,'DataSelectionChange',handle.EventData(obj,'DataSelectionChange'));


%-%------------------------------
function i_CheckboxChange(control,data,obj,index)

x=obj.checkstates;
if (index==1)
    x(index)=get(control,'Value');
end

set(obj.listeners(5),'enabled','off'); % briefly turn off the listener on "checkstates".
obj.checkstates=x;
set(obj.listeners(5),'enabled','on');

send(obj,'CheckStateChange',handle.EventData(obj,'CheckStateChange'));

%------------------------------
function i_SelectionSet(scr,data,obj)

% The selection can be set for hidden popup menus, but it MUST
% be set for visible ones.
if length(obj.selection)<length(obj.displays)
	error(message('mbc:cgsurfview:datasel:InvalidState2'));
end
len=length(obj.displays);
for k=1:len
    if (k==1)
        set(obj.combos{k},'Value',obj.selection(k));
    else
        % add one to compensate for the extra string containing 'None'
        set(obj.combos{k},'Value',obj.selection(k)+1);
    end
end


%-------------------------------
function i_CheckStateSet(src,data,obj)

if length(obj.checkstates)~=length(obj.checknames)
	error(message('mbc:cgsurfview:datasel:InvalidState3'));
end
len=length(obj.checknames);
for k=1:len
    if (k==1)
        set(obj.checkboxes{k},'Value',obj.checkstates(k));
    end
end

%----------------------------
function i_CheckEnableSet(src,data,obj)

if obj.max_check>0
    set([obj.checkboxes{:}],'Enable',mbconoff(obj.cbenable));
end