www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/mv_options_gui.m
function mv_options_gui() %MV_OPTIONS_GUI GUI for setting user options in mbcmodel % % MV_OPTIONS_GUI displays a dialog that allows the user to edit public % preferences in MBC. % Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc. AP = mbcprefs('mbc'); allfiles = getpref(AP,'FileDefaults'); ud.options.files = allfiles.mbcmodel; ud.options.user = initfromprefs(mbcuser); dlg= mbcgui.container.Dialog('Size',[400,220],... 'Resize','off',... 'Owner',get(MBrowser,'Figure'),... 'Tag','MBCModelOptions',... 'Buttons','OK_CANCEL',... 'Name','Model Browser Preferences'); fh = dlg.Figure; tabs = uitabgroup('Parent',fh,'Units','pixels'); filelocTab = mbcgui.container.uitab('Parent',tabs,'Title','File Locations'); userinfoTab = mbcgui.container.uitab('Parent',tabs,'Title','User Information'); % store initial preference values, this will be used to determine if the % preference has been changed initialValues.Models = ud.options.files.Models; initialValues.Projects = ud.options.files.Projects; initialValues.Data = ud.options.files.Data; initialValues.Designs = ud.options.files.Designs; % File location tab % edit boxes for typing in a folder location ud.Hand.File.ModelDirEdit = uicontrol('Parent',filelocTab,... 'Style','edit',... 'String',ud.options.files.Models,... 'HorizontalAlignment','left',... 'BackgroundColor',[1 1 1], ... 'Tag', 'modelEdit'); ud.Hand.File.ProjectDirEdit = uicontrol('Parent',filelocTab,... 'Style','edit',... 'String',ud.options.files.Projects,... 'HorizontalAlignment','left',... 'BackgroundColor',[1 1 1], ... 'Tag', 'projectEdit'); ud.Hand.File.DataDirEdit = uicontrol('Parent',filelocTab,... 'Style','edit',... 'String',ud.options.files.Data,... 'HorizontalAlignment','left',... 'BackgroundColor',[1 1 1], ... 'Tag', 'dataEdit'); ud.Hand.File.TemplateDirEdit = uicontrol('Parent',filelocTab,... 'Style','edit',... 'String',ud.options.files.Designs,... 'HorizontalAlignment','left',... 'BackgroundColor',[1 1 1], ... 'Tag', 'templateEdit'); % combining the label with an edit box ud.Hand.File.ModelDir = xregGui.labelcontrol('parent',filelocTab,... 'string','Models:',... 'labelsizemode','absolute',... 'controlsizemode','relative',... 'labelsize',70,... 'controlsize',1,... 'border',[0 1 0 1],... 'Control', ud.Hand.File.ModelDirEdit); ud.Hand.File.ProjectDir = xregGui.labelcontrol('parent',filelocTab,... 'string','Projects:',... 'labelsizemode','absolute',... 'controlsizemode','relative',... 'labelsize',70,... 'controlsize',1,... 'border',[0 1 0 1],... 'Control',ud.Hand.File.ProjectDirEdit); ud.Hand.File.DataDir = xregGui.labelcontrol('parent',filelocTab,... 'string','Data files:',... 'labelsizemode','absolute',... 'controlsizemode','relative',... 'labelsize',70,... 'controlsize',1,... 'border',[0 1 0 1],... 'Control', ud.Hand.File.DataDirEdit); ud.Hand.File.TemplateDir = xregGui.labelcontrol('parent',filelocTab,... 'string','Templates:',... 'labelsizemode','absolute',... 'controlsizemode','relative',... 'labelsize',70,... 'controlsize',1,... 'border',[0 1 0 1],... 'Control', ud.Hand.File.TemplateDirEdit); % adding the directory browser folder button ud.Hand.File.Push(1)= xregGui.iconuicontrol('parent',filelocTab,... 'imageFile',[xregrespath,filesep,'fileOpen.bmp'],... 'transparentColor', [0 255 0],... 'callback',{@i_GetFile,'ModelDir'}, ... 'Tag', 'modelBrowse'); ud.Hand.File.Push(2)= xregGui.iconuicontrol('parent',filelocTab,... 'imageFile',[xregrespath,filesep,'fileOpen.bmp'],... 'transparentColor', [0 255 0],... 'callback',{@i_GetFile,'ProjectDir'}, ... 'Tag', 'projectBrowse'); ud.Hand.File.Push(3)= xregGui.iconuicontrol('parent',filelocTab,... 'imageFile',[xregrespath,filesep,'fileOpen.bmp'],... 'transparentColor', [0 255 0],... 'callback',{@i_GetFile,'DataDir'}, ... 'Tag', 'dataBrowse'); ud.Hand.File.Push(4)= xregGui.iconuicontrol('parent',filelocTab,... 'imageFile',[xregrespath,filesep,'fileOpen.bmp'],... 'transparentColor', [0 255 0],... 'callback',{@i_GetFile,'TemplateDir'}, ... 'Tag', 'templateBrowse'); % grid layout for the folder preferences tab filegrd = xreggridbaglayout(filelocTab,... 'packstatus','off',... 'dimension',[4 2],... 'gapx',5,... 'gapy',7,... 'border',[10 10 10 15],.... 'rowsizes',[22 22 22 22],... 'colsizes',[-1 22],... 'elements',{ud.Hand.File.ModelDir, ud.Hand.File.Push(1); ud.Hand.File.ProjectDir, ud.Hand.File.Push(2);... ud.Hand.File.DataDir, ud.Hand.File.Push(3); ud.Hand.File.TemplateDir, ud.Hand.File.Push(4)}); filelocTab.LayoutComponent = filegrd; % user infomation tab userobj = createguiobject(ud.options.user,'edit',userinfoTab); userlyt = xreglayerlayout(userinfoTab,'border',[10 10 10 15],... 'elements',{userobj}); userinfoTab.LayoutComponent = userlyt; dlg.Content = tabs; fh.UserData = ud; dlg.ValidationFcn = @() iValidate(ud, initialValues); % bring up dialog and wait for it to be made invisible closeMode = dlg.showDialog(); if strcmp(closeMode,'OK') % OK button was selected % update preferences with new values i_SetPrefs(ud,userobj); end delete(dlg); function i_GetFile(h,~,prefname) % bring up the directory chooser and store the result fh = ancestor(h,'figure'); ud = get(fh,'UserData'); edt = get(ud.Hand.File.(prefname),'Control'); oldpath = get(edt,'String'); newpath = uigetdir(oldpath,'Select path:'); if newpath~=0 set(edt,'String',newpath); end function i_SetPrefs(ud,userobj) % update the mbc preferences with the actual values in the controls ud.options.files.Models = get(ud.Hand.File.ModelDir.Control,'String'); ud.options.files.Projects = get(ud.Hand.File.ProjectDir.Control,'String'); ud.options.files.Data = get(ud.Hand.File.DataDir.Control,'String'); ud.options.files.Designs = get(ud.Hand.File.TemplateDir.Control,'String'); ud.options.user = initfromguiobject(ud.options.user,userobj); AP= mbcprefs('mbc'); allfiles = getpref(AP,'FileDefaults'); allfiles.mbcmodel = ud.options.files; setpref(AP,'FileDefaults',allfiles); sendtoprefs(ud.options.user); function ok = iValidate(ud, initialValues) %iValidate validate options [locationsOK, msgStr] = i_CheckLocations(ud, initialValues); ok = all(locationsOK); if ~ok uiwait(errordlg(msgStr, 'Invalid Folder Location', 'modal')); end function [locationsOK, msgStr] = i_CheckLocations(ud, initialValues) % Check that locations are valid, if not construct an error message string locationsOK = true; msgStr = ''; % Model dir modelDir = get(ud.Hand.File.ModelDir.Control,'String'); if ~strcmp(modelDir, initialValues.Models) && ~i_IsValidLocation(modelDir) locationsOK = false; msgStr = sprintf('%sInvalid folder specified for ''Models'' folder: ''%s''.\n', msgStr, modelDir); end % Project dir projectDir = get(ud.Hand.File.ProjectDir.Control,'String'); if ~strcmp(projectDir, initialValues.Projects) && ~i_IsValidLocation(projectDir) locationsOK = false; msgStr = sprintf('%sInvalid folder specified for ''Projects'' folder: ''%s''.\n', msgStr, projectDir); end % Data dir dataDir = get(ud.Hand.File.DataDir.Control,'String'); if ~strcmp(dataDir, initialValues.Data) && ~i_IsValidLocation(dataDir) locationsOK = false; msgStr = sprintf('%sInvalid folder specified for ''Data'' folder: ''%s''.\n', msgStr, dataDir); end % Template dir templateDir = get(ud.Hand.File.TemplateDir.Control,'String'); if ~strcmp(templateDir, initialValues.Designs) && ~i_IsValidLocation(templateDir); locationsOK = false; msgStr = sprintf('%sInvalid folder specified for ''Template'' folder: ''%s''.\n', msgStr, templateDir); end function isValid = i_IsValidLocation(location) % A valid location is either an empty or an existing directory isValid = isempty(location) || exist(location, 'dir');