www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/@xregtable/tbl_filtergui.m

    function filt=tbl_filtergui(tbl,varargin)
%TBL_FILTERGUI   Filter changing GUI for a table object
%   FILT=TBL_FILTERGUI(TBL)
%   GUI table method which pops up a modal dialog to handle
%   changing the filter on the given table.  The optional output
%   returns the filter type and settings in a structure

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


% create figure window for altering precision
figh = xregdialog('name','Table Filters',...
    'resize','off');
xregcenterfigure(figh, [350 240])

lyt=i_createlyt(figh,tbl);

% Add ok, cancel, apply and a remove filter buttons
cancbtn=uicontrol('Parent', figh, 'Style','pushbutton',...
    'Position',[0 0 65 25],...
    'String','Cancel',...
    'Callback',{@iCancel, figh});
okbtn=uicontrol('Parent', figh, 'Style','pushbutton',...
    'Position',[0 0 65 25],...
    'String','OK',...
    'Callback',{@iOK, figh});
applybtn=uicontrol('Parent', figh, 'Style','pushbutton',...
    'Position',[0 0 65 25],...
    'String','Apply',...
    'Callback',{@iApply, figh});
rembtn=uicontrol('Parent', figh, 'Style','pushbutton',...
    'Position',[0 0 90 25],...
    'String','Remove Filter',...
    'Callback',{@iRemove, figh});

oklyt = xregflowlayout(figh,'orientation','right/center','elements',{applybtn,cancbtn,okbtn},...
    'gap',7,'border',[0 0 -7 0],'packstatus','off');
remlyt = xregflowlayout(figh,'orientation','left/center','elements',{rembtn});
btns = xreglayerlayout(figh,'elements',{remlyt,oklyt});
main = xregborderlayout(figh,'center',lyt,'south',btns,'innerborder',[10 45 10 10]);

figh.LayoutManager = main;
set(main, 'packstatus', 'on');

figh.showDialog(okbtn);

% get output
tg=get(figh,'Tag');
switch lower(tg)
    case 'ok'
        filt=i_getfiltvals(figh);
    otherwise
        filt=0;
end

delete(figh);


function lyt=i_createlyt(figh,tbl)

% Add basic/advanced options.  the opts object holds a copy of the table
% for forcing access to this table method in the callbacks

% text fields describing the opptions

tabs = uitabgroup('Parent',figh,'Units','pixels');

basicTab = mbcgui.container.uitab('Parent',tabs,...
    'LayoutBorder',[15 10 10 10],...
    'Title','Basic');

% basic options
bs(1)=uicontrol('Parent', basicTab, 'Style','text',...
    'String',['The "Basic" option will construct a filter which prevents cells',...
    ' in the table which are equal to the specified value, within the specified',...
    ' tolerance, from being displayed.'],...
    'HorizontalAlignment','left');
div1=xregGui.dividerline('parent', basicTab, 'visible','off');
bs(2)=uicontrol('Parent', basicTab, 'Style','text',...
    'Position',[0 0 100 15],...
    'String','Value to filter out:',...
    'HorizontalAlignment','right');
bs(3)=uicontrol('Parent', basicTab, 'Style','text',...
    'Position',[0 0 100 15],...
    'String','Tolerance level:',...
    'HorizontalAlignment','right');
bs(4)=uicontrol('Parent', basicTab, 'Style','edit',...
    'Position',[0 0 60 20],...
    'String',num2str(get(tbl,'filters.value')),...
    'BackgroundColor',[1 1 1],...
    'Callback',@iEntryCheck);
bs(5)=uicontrol('Parent', basicTab, 'Style','edit',...
    'Position',[0 0 60 20],...
    'String',num2str(get(tbl,'filters.tolerance')),...
    'BackgroundColor',[1 1 1],...
    'Callback',@iEntryCheck);

flw1=xregflowlayout(basicTab,'orientation','left/center',...
    'elements',{bs(2),bs(4)},'gap',5);
flw2=xregflowlayout(basicTab,'orientation','left/center',...
    'elements',{bs(3),bs(5)},'gap',5);
grd=xreggridlayout(basicTab,'dimension',[3 1],'correctalg','on','rowsizes',[30 30 -1],...
    'elements',{flw1,flw2,[]});
tb1=xreggridlayout(basicTab,'dimension',[3 1],'correctalg','on','rowsizes',[45 20 -1],...
    'elements',{bs(1),div1,grd});
basicTab.LayoutComponent = tb1;

advancedTab = mbcgui.container.uitab('Parent',tabs,...
    'LayoutBorder',[15 10 10 10],...
    'Title','Advanced');
% add advanced options
ad(1)=uicontrol('Parent', advancedTab, 'Style','text',...
    'String',['The "Advanced" option will construct a filter using the settings',...
    ' below.  Cells which test true by the filter will not have their values',...
    ' displayed.'],...
    'HorizontalAlignment','left');
div2=xregGui.dividerline('parent', advancedTab);


ad(2)=uicontrol('Parent', advancedTab, 'Style','text',...
    'Position',[0 0 85 15],...
    'String','Exclude values:',...
    'HorizontalAlignment','center');
% Find initial value for popupmenu
cur_str=get(tbl,'filters.type');
switch cur_str
    case {'eq','none'}
        popval=1;
    case 'ne'
        popval=2;
    case 'lt'
        popval=3;
    case 'le'
        popval=4;
    case 'gt'
        popval=5;
    case 'ge'
        popval=6;
end
ad(3)=uicontrol('Parent', advancedTab, 'Style','popupmenu',...
    'Position',[0 0 50 20],...
    'String','==|~=|<|<=|>|>=',...
    'Value',popval,...
    'BackgroundColor','w',...
    'Callback',{@iSwapType, figh});
ad(4)=uicontrol('Parent', advancedTab, 'Style','edit',...
    'Position',[0 0 60 20],...
    'String',num2str(get(tbl,'filters.value')),...
    'BackgroundColor','w',...
    'Callback',@iEntryCheck,...
    'HorizontalAlignment','left');

% Using char(177) may be a bit dodgy for the +/- sign.  I don't want to resort
% to an axis for TeX commands though
ad(5)=uicontrol('Parent', advancedTab, 'Style','text',...
    'Position',[0 0 8 16],...
    'String', char(177),...
    'HorizontalAlignment','left');
ad(6)=uicontrol('Parent', advancedTab, 'Style','edit',...
    'Position',[0 0 60 20],...
    'String',num2str(get(tbl,'filters.tolerance')),...
    'BackgroundColor','w',...
    'Callback',@iEntryCheck,...
    'HorizontalAlignment','left');

flw1=xregflowlayout(advancedTab,'orientation','left/center',...
    'elements',{ad(2),ad(3),ad(4),ad(5),ad(6)},'gap',5);
grd=xreggridlayout(advancedTab,'dimension',[3 1],'correctalg','on','rowsizes',[10 30 -1],...
    'elements',{[],flw1,[]});
tb2=xreggridlayout(advancedTab,'dimension',[3 1],'correctalg','on','rowsizes',[45 20 -1],...
    'elements',{ad(1),div2,grd});
advancedTab.LayoutComponent = tb2;

ud.filttype='';
ud.advanced=ad;
ud.basic=bs;
ud.table=tbl;
ud.figure=figh;
ud.tabs=tabs;
lyt = tabs;

set(figh,'UserData',ud);



function i_applyfilt(figh)

ud=get(figh,'UserData');
tbl=ud.table;
switch find(ud.tabs.SelectedTab == ud.tabs.Children)
    case 1
        %Basic
        tp='eq';
        val=str2num(get(ud.basic(4),'String')); %#ok<*ST2NM>
        tol=str2num(get(ud.basic(5),'String'));
    case 2
        %Advanced
        tpval=get(ud.advanced(3),'Value');
        switch tpval
            case 1
                tp='eq';
            case 2
                tp='ne';
            case 3
                tp='lt';
            case 4
                tp='le';
            case 5
                tp='gt';
            case 6
                tp='ge';
        end
        val=str2num(get(ud.advanced(4),'String'));
        tol=str2num(get(ud.advanced(6),'String'));
end
ud.filttype=tp;
set(tbl,'filters.type',tp,'filters.value',val,'filters.tolerance',tol);
set(figh,'UserData',ud);



function filt=i_getfiltvals(figh)
ud=get(figh,'UserData');
filt.type=ud.filttype;
switch find(ud.tabs.SelectedTab == ud.tabs.Children)
    case 1
        %Basic
        filt.value=str2num(get(ud.basic(4),'String'));
        filt.tolerance=str2num(get(ud.basic(5),'String'));
    case 2
        %Advanced
        filt.value=str2num(get(ud.advanced(4),'String'));
        filt.tolerance=str2num(get(ud.advanced(6),'String'));
end



function iApply(~, ~, figh)
% Apply, don't close
i_applyfilt(figh);


function iOK(~, ~, figh)
i_applyfilt(figh);
set(figh,'Tag','ok', 'Visible', 'off');


function iCancel(~,~, figh)
set(figh,'Tag', 'cancel', 'Visible', 'off');


function iRemove(~,~,figh)
ud=get(figh,'UserData');
tbl=ud.table;
set(tbl,'filters.type','none');
ud.filttype='none';
set(figh,'UserData',ud);
set(figh,'Tag','ok', 'Visible', 'off');
        

function iSwapType(~,~,figh)
% Need to enable/disable tolerance appropriately.
ud=get(figh,'UserData');
val=get(ud.advanced(3),'Value');
switch val
    case {1,2}
        % enable tolerance
        set([ud.advanced(5);ud.advanced(6)],'Enable','on');
    otherwise
        set([ud.advanced(5);ud.advanced(6)],'Enable','off');
end


function iEntryCheck(src,~)
% check new entry in cell is numeric and ok.
str=get(src,'String');
num=str2double(str);

if isnan(num)
    % put a nul entry in object
    set(src,'String','0');
end