📄 sptprefs.m
字号:
function varargout = sptprefs(varargin)
%SPTPREFS Preferences dialog box for SPTool.
% prefs = sptprefs(prefs)
% Copyright (c) 1988-98 by The MathWorks, Inc.
% $Revision: 1.9 $
if ~isstr(varargin{1})
action = 'init';
prefs = varargin{1};
else
action = varargin{1};
end
switch action
case 'init'
SPTfig = gcf;
if nargin > 1
panelInd = varargin{2};
else
panelInd = 1;
end
figname = 'Preferences for SPTool';
okstring = 'OK';
cancelstring = 'Cancel';
fus = 5; % frame / uicontrol spacing
ffs = 8;
uh = 24; % uicontrol height
listw = 175; % list box width
lw = 170; % label width
bw = 110; % button width
bw1 = 90; % button width for Cancel and OK
uw = 250; % uicontrol width
lfs = 5; %label / frame spacing
lbs = 3; % label / box spacing
lh = 16; % label height
fp = get(0,'defaultfigureposition');
w = 2*ffs + listw + fus + 2*fus + lbs + lw + uw;
h = 8*(fus+uh) + lh + 2*fus + uh;
fp = [15 fp(2)+fp(4)-h w h]; % keep top of window fixed
fig_props = { ...
'name' figname ...
'resize' 'off' ...
'numbertitle' 'off' ...
'windowstyle' 'modal' ...
'createfcn' '' ...
'position' fp ...
'closerequestfcn' 'sbswitch(''sptprefs'',''cancel'')' ...
'color' get(0,'defaultuicontrolbackgroundcolor') ...
'units' 'pixels' ...
'handlevisibility' 'callback'};
fig = figure(fig_props{:});
cancel_btn = uicontrol('style','pushbutton',...
'units','pixels',...
'string',cancelstring,...
'position',[fp(3)-ffs-bw1-fus-bw1 ffs bw1 uh],...
'tag','cancelButton',...
'callback','sbswitch(''sptprefs'',''cancel'')');
ud.ok_btn = uicontrol('style','pushbutton',...
'units','pixels',...
'string',okstring,...
'enable','off',...
'tag','okButton',...
'position',[fp(3)-ffs-bw1 ffs bw1 uh],...
'callback','applyfilt(''ok'')');
ud.list = uicontrol('style','listbox',...
'units','pixels',...
'string',{prefs.panelDescription},...
'value',panelInd,...
'tag','listbox',...
'position',[ffs ffs+fus+uh listw fp(4)-2*ffs-fus-uh],...
'callback','sbswitch(''sptprefs'',''list'')',...
'backgroundcolor','w');
ud.helpButton = uicontrol('style','pushbutton',...
'units','pixels',...
'tag','helpButton',...
'string','Help...',...
'position',[ffs ffs listw uh],...
'callback','sbswitch(''sptprefs'',''help'')');
ud.prefs = prefs;
ud.factory = uicontrol('style','pushbutton',...
'units','pixels',...
'string','Factory Settings',...
'tag','factoryButton',...
'position',[ffs+fus+listw ffs bw uh],...
'callback','sbswitch(''sptprefs'',''factory'')');
ud.revert = uicontrol('style','pushbutton',...
'units','pixels',...
'string','Revert Panel',...
'tag','revertButton',...
'position',[ffs+fus+listw+fus+bw ffs bw uh],...
'callback','sbswitch(''sptprefs'',''revert'')');
pf_pos = [ffs+fus+listw 2*fus+uh 2*fus+lbs+lw+uw 8*(uh+fus)+lh/2];
ud.panelFrame = uicontrol('style','frame',...
'units','pixels',...
'tag','panelFrame',...
'position',pf_pos);
ud.panelLabel = uicontrol('style','text',...
'units','pixels',...
'tag','panelLabel',...
'horizontalalignment','left',...
'position',[pf_pos(1)+10 pf_pos(2)+pf_pos(4)-lh/2 lw lh],...
'string',ud.prefs(panelInd).panelDescription);
minwidthLeft(ud.panelLabel)
ud.panelControls = cell(length(prefs),1);
ud.panelControls{panelInd} = createPanel(prefs(panelInd),pf_pos,...
fus,lbs,lw,lh,uw,uh);
ud.whichControl = [];
ud.changedFlags = zeros(length(ud.prefs),1);
ud.flag = '';
set(fig,'userdata',ud)
setEnable(panelInd,ud.prefs,ud.panelControls,ud.factory,ud.revert)
done = 0;
while ~done
waitfor(fig,'userdata')
ud = get(fig,'userdata');
switch ud.flag
case 'list'
oldPanelInd = panelInd;
panelInd = get(ud.list,'value');
if panelInd ~= oldPanelInd
% change label
set(ud.panelLabel,'string',ud.prefs(panelInd).panelDescription);
minwidthLeft(ud.panelLabel)
if strcmp(computer, 'PCWIN') % avoid gec# 21842
tempos = get(ud.panelFrame,'position');
set(ud.panelFrame,'position',tempos+1);
set(ud.panelFrame,'position',tempos);
end
% hide old uicontrols
set([ud.panelControls{oldPanelInd}{:}],'visible','off')
% show or create uicontrols for this panel
if isempty(ud.panelControls{panelInd})
ud.panelControls{panelInd} = createPanel(prefs(panelInd),...
pf_pos,fus,lbs,lw,lh,uw,uh);
else
set([ud.panelControls{panelInd}{:}],'visible','on')
end
setEnable(panelInd,ud.prefs,ud.panelControls,ud.factory,ud.revert)
end
case 'change'
% if this is a radio button, set values of all visible
% radiobuttons with the same tag.
if strcmp('radiobutton',...
get(ud.panelControls{panelInd}{ud.whichControl}(1),'style'))
radioTag = ud.prefs(panelInd).controls{ud.whichControl,4};
if ~isempty(radioTag)
u = findobj('style','radiobutton','userdata',radioTag,...
'visible','on');
set(u,'value',0)
set(ud.panelControls{panelInd}{ud.whichControl},'value',1)
end
end
% check to see if current values are factory or original,
% and set enable property of Factory Settings and Revert Panel
% buttons appropriately.
setEnable(panelInd,ud.prefs,ud.panelControls,ud.factory,ud.revert)
case 'revert'
% revert panel button pushed
setPanel(ud.panelControls{panelInd},ud.prefs(panelInd).currentValue)
setEnable(panelInd,ud.prefs,ud.panelControls,ud.factory,ud.revert)
case 'factory'
% factory button pushed
setPanel(ud.panelControls{panelInd},ud.prefs(panelInd).controls(:,7))
setEnable(panelInd,ud.prefs,ud.panelControls,ud.factory,ud.revert)
case 'ok'
newprefs = ud.prefs;
err = 0;
for i=1:length(ud.prefs)
if ~isempty(ud.panelControls{i})
newprefs(i).currentValue = getPanel(ud.panelControls{i});
end
if ~isequal(newprefs(i).currentValue,ud.prefs(i).currentValue)
p = cell2struct(newprefs(i).currentValue,...
ud.prefs(i).controls(:,1));
for j=1:length(ud.prefs(i).clientList)
errstr = feval(ud.prefs(i).clientList{j},'setprefs',...
ud.prefs(i).panelName,p);
err = length(errstr)>0;
if err
break
end
end
if err
break
end
ud.prefs(i).currentValue = newprefs(i).currentValue;
sptool('setprefs',ud.prefs(i).panelName,p,SPTfig)
end
end
if err
done = 0;
msgbox(errstr,'Error','error','modal')
else
done = 1;
end
case 'cancel'
% do nothing and return
done = 1;
err = 1;
case 'help'
sptprefsHelp
end
if ~done
if strcmp(get(ud.revert,'enable'),'on')
ud.changedFlags(panelInd) = 1;
else
ud.changedFlags(panelInd) = 0;
end
if any(ud.changedFlags)
set(ud.ok_btn,'enable','on')
else
set(ud.ok_btn,'enable','off')
end
ud.flag = [];
set(fig,'userdata',ud)
end
end
delete(fig)
varargout{1} = ud.prefs;
varargout{2} = panelInd;
% ------
% action == 'ok' or 'cancel' or anything else
otherwise
fig = gcf;
ud = get(fig,'userdata');
ud.flag = action;
if nargin > 1
ud.whichControl = varargin{2};
end
set(fig,'userdata',ud)
end
function minwidthLeft(h,n)
%MINWIDTH Minimize width of left justified text object to be just wide
% enough for extent.
% optional second argument specifies additional pixels on either side
% of text, defaults to 2
if nargin == 1
n = 2;
end
for i=1:length(h)
ex = get(h(i),'extent');
pos = get(h(i),'position');
style = get(h(i),'style');
if strcmp(style,'checkbox') | strcmp(style,'radiobutton')
% add to width to account for radio or check box
set(h(i),'horizontalalignment','center',...
'position',[pos(1) pos(2) ex(3)+2*n+30 pos(4)])
else
set(h(i),'horizontalalignment','center',...
'position',[pos(1) pos(2) ex(3)+2*n pos(4)])
end
end
function panelControls = createPanel(prefs,pf_pos,fus,lbs,lw,lh,uw,uh);
%createPanel - create uicontrols for Panel
% Inputs:
% prefs - 1-by-1 struct with fields
% .panelName, .panelDescription, .controls, .currentValue
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -