www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/+cgoptimgui/vectorEditor.m
classdef vectorEditor < mbcgui.widget.BasicContainer %cgoptimgui.vectorEditor class % cgoptimgui.vectorEditor properties: % Parent - Property is of type 'MATLAB array' (read only) % Position - Property is of type 'rect' % Visible - Property is of type 'on/off' % TableLabel - Property is of type 'ustring' % DataNames - Property is of type 'MATLAB array' % DataValues - Property is of type 'MATLAB array' % DataTypeLabel - Property is of type 'string' % Vector - Property is of type 'MATLAB array' % LayoutStyle - Property is of type 'string' % Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc. properties (AbortSet, SetObservable) %TABLELABEL Property is of type 'ustring' TableLabel = 'Weights'; %DATANAMES Property is of type 'MATLAB array' DataNames = []; %DATAVALUES Property is of type 'MATLAB array' DataValues = []; %DATATYPELABEL Property is of type 'string' DataTypeLabel = 'Data'; %VECTOR Property is of type 'MATLAB array' Vector = []; %LAYOUTSTYLE Property is of type 'string' LayoutStyle = 'Wide'; end properties (Access=protected, AbortSet, SetObservable) %PROPLISTENERS Property is of type 'handle vector' PropListeners = []; %LAYOUT Property is of type 'MATLAB array' Layout = []; %VECTOREDIT Property is of type 'MATLAB array' VectorEdit = []; %TABLEEDIT Property is of type 'handle' TableEdit = []; %DATASETCOLUMNSELECTOR Property is of type 'MATLAB array' DatasetColumnSelector = []; %VECTORRADIO Property is of type 'MATLAB array' VectorRadio = []; %TABLERADIO Property is of type 'MATLAB array' TableRadio = []; %DATASETRADIO Property is of type 'MATLAB array' DatasetRadio = []; %SELECTION Property is of type 'int' Selection = 0; end events VectorChanged end % events methods % constructor block function obj = vectorEditor(varargin) %VECTOREDITOR Constructor for vectorEditor object % OBJ = VECTOREDITOR(PROP, VAL, ...) % obj = cgoptimgui.vectorEditor; if nargin set(obj, varargin{:}); end if isempty(obj.Parent) obj.Parent = gcf; end sc = xregGui.SystemColorsDbl; obj.VectorRadio = uicontrol('Style','radiobutton',... 'Parent',obj.Parent,... 'Visible', obj.Visible, ... 'BackgroundColor',sc.CTRL_BACK,... 'String','MATLAB vector:', ... 'Value', 0, ... 'Callback', {@i_radioselect, obj, 2}); obj.TableRadio = uicontrol('Style','radiobutton',... 'Parent',obj.Parent,... 'Visible', obj.Visible, ... 'BackgroundColor',sc.CTRL_BACK,... 'String','Table entry:', ... 'Value', 1, ... 'Callback', {@i_radioselect, obj, 1}); obj.DatasetRadio = uicontrol('Style','radiobutton',... 'Parent',obj.Parent,... 'Visible', obj.Visible, ... 'BackgroundColor',sc.CTRL_BACK,... 'String',sprintf('%s column:', obj.DataTypeLabel), ... 'Value', 0, ... 'Callback', {@i_radioselect, obj, 3}); obj.DatasetColumnSelector = uicontrol('Parent',obj.Parent,... 'Style','popupmenu',... 'Visible', obj.Visible, ... 'BackgroundColor',sc.WINDOW_BG, ... 'String', {' '}, ... 'Value', 1, ... 'Callback', {@i_datasetchange, obj}); obj.VectorEdit = uicontrol('Style','edit',... 'Parent',obj.Parent,... 'Visible', obj.Visible, ... 'HorizontalAlignment','left',... 'BackgroundColor',sc.WINDOW_BG, ... 'Callback', {@i_editchange, obj}); obj.TableEdit = mbcwidgets.Table2D('numeric', ... 'parent', obj.Parent, ... 'visible', obj.Visible, ... 'editable', true, ... 'ShowHeaderSelection', false, ... 'valuechangedcallback', {@i_tablechange, obj}); obj.TableEdit.Peer.setRowHeaderWidth(50); obj.TableEdit.Peer.setCornerAsBlank; if strcmp(obj.LayoutStyle, 'Wide') obj.Layout = xreggridbaglayout(obj.Parent, ... 'packgroup', 'XREG_PERM_ON', ... 'dimension', [4 2], ... 'rowsizes', [20 -1 20 22] ,... 'colsizes', [100 -1], ... 'gapy', 5, ... 'gapx', 10, ... 'mergeblock', {[1 2],[2 2]}, ... 'elements', {obj.TableRadio, [], obj.VectorRadio, obj.DatasetRadio, ... obj.TableEdit, [], obj.VectorEdit, obj.DatasetColumnSelector}, ... 'position', obj.Position); else obj.Layout = xreggridbaglayout(obj.Parent, ... 'packgroup', 'XREG_PERM_ON', ... 'dimension', [8 2], ... 'rowsizes', [20 -1 5 20 20 5 20 22] ,... 'colsizes', [20 -1], ... 'mergeblock', {[1 1],[1 2]}, ... 'mergeblock', {[4 4],[1 2]}, ... 'mergeblock', {[7 7],[1 2]}, ... 'elements', {obj.TableRadio, [], [], obj.VectorRadio, [], [], obj.DatasetRadio, [],... [], obj.TableEdit, [], [], obj.VectorEdit, [], [], obj.DatasetColumnSelector}, ... 'position', obj.Position); end obj.ContentHandle = obj.Layout; obj.pSetupListeners; obj.pSetDataset; obj.pSelect(1); end % vectorEditor end % constructor block methods function set.DataNames(obj,value) obj.DataNames = i_checkdatanames(obj,value); end function set.DataValues(obj,value) obj.DataValues = i_checkdatavalues(obj,value); end function set.Vector(obj,value) obj.Vector = i_checkvector(obj,value); end function set.LayoutStyle(obj,value) obj.LayoutStyle = i_checklytstyle(obj,value); end end % set and get functions methods (Access=protected) % protected methods %---------------------------------------- function pSelect(obj, index) %PSELECT Select an input option % PSELECT(OBJ, INDEX) selects an input method. 1 is via table. 2 is an % edit box for entering a Matlab vectors. 3 is choosing a dataset column. if index>0 radio_state = {0;0;0}; radio_state{index} = 1; set([obj.TableRadio;obj.VectorRadio;obj.DatasetRadio], {'Value'}, radio_state); sc = xregGui.SystemColorsDbl; switch index case 1 i_disablevector(obj.VectorEdit, sc); i_disabledataset(obj.DatasetColumnSelector, sc); if ~isempty(obj.Vector) obj.TableEdit.Peer.setData(obj.Vector(:),obj.TableLabel); else obj.TableEdit.Peer.clearTable; end case 2 i_disabletable(obj.TableEdit); i_disabledataset(obj.DatasetColumnSelector, sc); set(obj.VectorEdit, 'Enable', 'on', ... 'BackgroundColor', sc.WINDOW_BG, ... 'String', prettify(obj.Vector)); case 3 i_disablevector(obj.VectorEdit, sc); i_disabletable(obj.TableEdit); set(obj.DatasetColumnSelector, 'Enable', 'on', ... 'BackgroundColor', sc.WINDOW_BG); % grab weights from data matrix immediately colindex = get(obj.DatasetColumnSelector, 'Value'); obj.pVectorChangeListener(false); obj.Vector = obj.DataValues(:, colindex); obj.pVectorChangeListener(true); obj.notify('VectorChanged'); end end obj.Selection = index; end % pSelect %---------------------------------------- function pSetDataset(obj) %PSETDATASET Update object after dataset change % PSETDATASET(OBJ) updates the popupmenu and if necessary the vector % property in response to the Dataset property being changed. % check that the dataset has the correct number of points Npts = size(obj.DataValues, 1); ENABLE_DS = (Npts==length(obj.Vector)) && ~isempty(obj.DataNames); sc = xregGui.SystemColorsDbl; if ENABLE_DS colnames = obj.DataNames; % Enable the dataset column option set(obj.DatasetRadio, 'Enable', 'on'); oldstrings = get(obj.DatasetColumnSelector, 'String'); idx = []; if iscell(oldstrings) selstring = oldstrings{get(obj.DatasetColumnSelector, 'Value')}; idx = find( strcmp( selstring,colnames ) ); end if isempty(idx) idx = 1; end set(obj.DatasetColumnSelector, 'String', colnames, ... 'Value', idx); % If this is currently selected, update weights property with the % column data if obj.Selection==3 obj.pSelect(3); end else % Disable the dataset column option set(obj.DatasetRadio, 'Enable', 'off'); set(obj.DatasetColumnSelector, 'Enable', 'off', ... 'String', ' ', ... 'Value', 1, ... 'BackgroundColor', sc.CTRL_BACK); % If this is currently selected, switch to table editing if obj.Selection==3 obj.pSelect(1); end end end % pSetDataset %---------------------------------------- function pSetLabel(obj) %PSETLABEL React to data label being changed % PSETLABEL(OBJ) if obj.Selection==1 % redraw table obj.TableEdit.Peer.setData(obj.Vector(:),obj.TableLabel); end end % pSetLabel %---------------------------------------- function pSetVector(obj) %PSETVECTOR React to vector property being set % PSETVECTOR(OBJ) if obj.Selection~=3 % Update current input option with data sel = obj.Selection; obj.Selection = 0; obj.pSelect(sel); end % Check if dataset option is still allowed obj.pSetDataset; end % pSetVector %---------------------------------------- function pSetupListeners(obj) %PSETUPLISTENERS Setup property listeners on object % PSETUPLISTENERS(OBJ) obj.PropListeners = [ ... event.proplistener(obj, obj.findprop('DataNames'), 'PostSet', @i_setDataset);... event.proplistener(obj, obj.findprop('DataValues'), 'PostSet', @i_setDataset);... event.proplistener(obj, obj.findprop('DataTypeLabel'), 'PostSet', @(h,evt) i_setDataLabel(h,evt,obj.DatasetRadio));... event.proplistener(obj, obj.findprop('Vector'), 'PostSet', @i_setVector);... event.proplistener(obj, obj.findprop('TableLabel'), 'PostSet', @i_setLabel);... event.proplistener(obj, obj.findprop('LayoutStyle'), 'PostSet', @(h,evt) i_setLayoutStyle(h,evt,obj.Layout));... ]; end % pSetupListeners %---------------------------------------- function pVectorChangeListener(obj, state) %PVECTORCHANGELISTENER Switch the state of reacting to vector-setting % PVECTORCHANGELISTENER(OBJ, STATE) where STATE is 'on' or 'off'. obj.PropListeners(4).Enabled = state; end % pVectorChangeListener end % protected methods end % classdef function val = i_checkvector(~, val) val = val(:); end % i_checkvector function val = i_checkdatanames(~, val) % ensure we have been passed a cell of strings if ~(isempty(val) || iscellstr(val)) error(message('mbc:cgoptimgui:vectorEditor:InvalidPropertyValue')); end end % i_checkdatanames function val = i_checkdatavalues(~, val) % ensure we have been passed a 2-D matrix if ~isempty(val) && ~ismatrix(val) error(message('mbc:cgoptimgui:vectorEditor:InvalidPropertyValue1')); end end % i_checkdatavalues function val = i_checklytstyle(~, val) if strcmpi(val, 'wide') val = 'Wide'; elseif strcmpi(val, 'narrow') val = 'Narrow'; else error(message('mbc:cgoptimgui:vectorEditor:InvalidPropertyValue2')); end end % i_checklytstyle function i_radioselect(src, ~, obj, index) if get(src, 'Value')==1 % RB was selected obj.pSelect(index); else % RB was deselected - force it back on set(src, 'Value', 1); end end % i_radioselect function i_editchange(src, ~, obj) newvect = str2num(get(src, 'String')); %#ok<ST2NM> if length(newvect)==length(obj.Vector) obj.Vector = newvect(:); obj.notify('VectorChanged'); else set(src, 'String', prettify(obj.Vector)); end end % i_editchange function i_datasetchange(~, ~, obj) obj.pSelect(3); end % i_datasetchange function i_tablechange(~, evt, obj) obj.pVectorChangeListener(false); obj.Vector(evt.data.Rows) = evt.data.NewValue; obj.pVectorChangeListener(true); obj.notify('VectorChanged'); end % i_tablechange function i_disablevector(edit, sc) set(edit, 'BackgroundColor',sc.CTRL_BACK, 'String', '', 'Enable', 'off'); end % i_disablevector function i_disabletable(table) table.Peer.clearTable; end % i_disabletable function i_disabledataset(popup, sc) set(popup, 'BackgroundColor',sc.CTRL_BACK, 'Enable', 'off'); end % i_disabledataset function i_setVector(~, evt) evt.AffectedObject.pSetVector; end % i_setVector function i_setDataset(~, evt) evt.AffectedObject.pSetDataset; end % i_setDataset function i_setLabel(~, evt) evt.AffectedObject.pSetLabel; end % i_setLabel function i_setDataLabel(~, evt, hRadio) set(hRadio, 'String', sprintf('%s column:', evt.AffectedObject.DataTypeLabel)); end % i_setDataLabel function i_setLayoutStyle(~, evt, lyt) els = get(lyt, 'elements'); els = els(~cellfun('isempty', els)); if strcmpi(evt.AffectedObject.LayoutStyle, 'wide') set(lyt, 'clearmerge', '', ... 'dimension', [4 2], ... 'rowsizes', [20 -1 20 22] ,... 'colsizes', [100 -1], ... 'gapy', 5, ... 'gapx', 10, ... 'mergeblock', {[1 2],[2 2]}, ... 'elements', {els{1}, [], els{2}, els{3}, ... els{4}, [], els{5}, els{6}}); else set(lyt, 'clearmerge', '', ... 'dimension', [8 2], ... 'rowsizes', [20 -1 5 20 20 5 20 22] ,... 'colsizes', [20 -1], ... 'mergeblock', {[1 1],[1 2]}, ... 'mergeblock', {[4 4],[1 2]}, ... 'mergeblock', {[7 7],[1 2]}, ... 'elements', {els{1}, [], [], els{2}, [], [], els{3}, [],... [], els{4}, [], [], els{5}, [], [], els{6}}); end end % i_setLayoutStyle