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

    function obj = lookup2editor(varargin)
%LOOKUP2EDITOR Constructor for lookup2editor object
%
%  OBJ = LOOKUP2EDITOR(PROP, VALUE, ...)
%
%  lookup2editor consists of a table
%  (com.mathworks.toolbox.mbc.gui.peer.LookupTablePeer) and a surface
%  (cgtools.cgsurfacedragger). 
%
%  CAGE uses the convention Y=rows, X = columns. Table surfaces are, by
%  default, displayed using the right-hand rule with Y on the X-axis and X
%  on the Y-axis for surface views. The table always uses rows=Y,
%  columns=X. lookup2editor provides an option to display the surface using
%  Y on the Y-axis and X on the X-Axis (not the right-hand rule).

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


if nargin && isa(varargin{1}, 'cgtools.lookup2editor')
    obj = varargin{1};
    varargin(1) = [];
else
    obj = cgtools.lookup2editor;
end

% Call the inherited constructor
abstractlayoutcomponent(obj, varargin{:});

SurfPanel = mbcgui.container.layoutpanel(...
    'Parent', obj.Parent, ...
    'Visible', obj.Visible, ...
    'BorderType', 'beveledin');

wrapper = mbcgui.widget.AxesContainer(...
    'Parent',SurfPanel, ...
    'Border', [50 35 20 25]);
obj.Axes = wrapper.AxesHandle;
set(obj.Axes,'Position', [0 0 1 1], ...
    'Tag','2DLookupTableSurface',...
    'Box','on',...
    'XGrid', 'on', ...
    'YGrid', 'on', ...
    'ZGrid', 'on');

uic = uicontextmenu('Parent',ancestor(obj.Parent,'figure'));
uimenu(uic, 'Label', 'Reset Axes Orientation', ...
    'Callback', {@i_setview3d, obj.Axes})
vmenu.RotateSurf = uimenu(uic, ...
    'Label', '&Rotate Table Surface', ...
    'Checked', 'on', ...
    'Separator', 'on', ...
    'Callback', {@i_seteditoreditstate, obj, false});

vmenu.EditSurf = uimenu(uic, ...
    'Label', '&Edit Table Surface', ...
    'Callback', {@i_seteditoreditstate, obj, true});


orient = getpref(mbcprefs('mbc'),'TableRHView');
vmenu.RightHandRule = uimenu(uic, ...
    'Separator','on',...
    'Label', '2D Orientation &Y/X', ...
    'Checked',mbconoff(orient),...
    'Callback', {@i_rhrule, obj});
vmenu.LeftHandRule = uimenu(uic, ...
    'Label', '2D Orientation &X/Y', ...
    'Callback', {@i_lhrule, obj});

vmenu.PrintCopy = uimenu(uic, ...
    'Separator','on',...
    'Label', '&Print to Figure', ...
    'Checked',mbconoff(~orient),...
    'Callback', {@iPrintCopy, obj.Axes});
vmenu.TableShading = uimenu(uic, ...
    'Label', '&Shade Table', ...
    'Checked','on',...
    'Callback', {@i_TableShadingCbk, obj});

obj.ViewMenu = vmenu;
set(obj.Axes,'UIContextMenu', uic);
view(obj.Axes, 3);

set(get(obj.Axes,'XLabel'),'Interpreter','none');
set(get(obj.Axes,'YLabel'),'Interpreter','none');

obj.Dragger = cgtools.cgsurfacedragger('Parent', obj.Axes);

obj.StatusText = uicontrol('Parent', SurfPanel, ...
    'Enable', 'inactive', ...
    'Style','text',...
    'HorizontalAlignment','left');

if obj.GraphEditingEnable
    obj.Dragger.setEditingMode(true);
    set(obj.StatusText, 'String', ' Graph editing is enabled');
else
    mv_rotate3d(obj.Axes, 'ON');
end


P = com.mathworks.toolbox.mbc.gui.peer.LookupTablePeer;
obj.Table = mbcwidgets.Table2D(P, ...
    'parent',obj.Parent, ...
    'visible', obj.Visible, ...
    'Position', [0 0 1 1], ...
    'Tag','Lookup2DJavaTable',...
    'Editable', true, ...
    'UIContextMenu', obj.TableContextMenu, ...
    'SelectionChangedCallback',{@i_SelectionChanged,obj,obj.Dragger},...
    'ValueChangedCallback', {@i_EditCallback, obj,obj.Dragger});
obj.Table.Peer.setShowRegionMask(false);
if obj.TableShading
    CMap = int16(get(ancestor(obj.Axes,'figure'),'ColorMap')*255);
    obj.Table.Peer.setColorMap(CMap(:,1),CMap(:,2),CMap(:,3));
end

grid = xreggridbaglayout(SurfPanel, ...
    'packstatus', 'off', ...
    'dimension', [2 1], ...
    'rowsizes', [-1 15], ...
    'gapy', 30, ...
    'elements', {wrapper, obj.StatusText});
set(SurfPanel, 'LayoutComponent', {grid});

obj.Display = xregsnapsplitlayout(obj.Parent,...
    'visible', obj.Visible, ...
    'position', obj.Position, ...
    'orientation','ud',...
    'split',[0.3 0.7], ...
    'barstyle', 1, ...
    'style', 'tobottom', ...
    'elements',{obj.Table,SurfPanel});

obj.addPropertyListeners({'TableContextMenu', 'GraphEditingEnable','RightHandRule','TableShading'}, ...
    {{@i_settablecontext, obj.Table}, ...
    {@i_setgraphedit, obj.Dragger, obj.StatusText},...
    {@i_RightHandRule, obj},...
    {@i_TableShading,obj,obj.Table,obj.Axes}});
% surfacedragger events
obj.addListeners([...
    handle.listener(obj.Dragger,'PointGrab', {@i_PointGrab, obj.Table,obj}); ...
    handle.listener(obj.Dragger,'PointDrag', {@i_PointDrag, obj.Table,obj}); ...
    handle.listener(obj.Dragger,'Update', {@i_PointUpdate, obj.Table, obj}); ...
    ]);


function i_settablecontext(~, evt, hTable)
%i_settablecontext set context menu for table
hTable.UIContextMenu = evt.NewValue;

function i_setview3d(~, ~, hAxes)
%i_setview3d reset surface to default view
view(hAxes, 3);

function i_setgraphedit(~, evt, hDragger, hText)
%i_setgraphedit update surface edit or rotate

hDragger.setEditingMode(evt.NewValue);
obj = evt.AffectedObject;
if evt.NewValue
    mv_rotate3d(hDragger.Parent, 'OFF');
    set(hText, 'String', ' Graph editing is enabled');
else
    mv_rotate3d(hDragger.Parent, 'ON');
    set(hText, 'String', '');
end
set(obj.ViewMenu.RotateSurf, 'Checked', mbconoff(~obj.GraphEditingEnable));
set(obj.ViewMenu.EditSurf, 'Checked', mbconoff(obj.GraphEditingEnable));

function i_RightHandRule(~,~, obj)
%i_RightHandRule change the right-hand rule
update(obj)
% update contextmenu checks
set(obj.ViewMenu.LeftHandRule, 'Checked', mbconoff(~obj.RightHandRule));
set(obj.ViewMenu.RightHandRule, 'Checked', mbconoff(obj.RightHandRule));


function i_TableShading(~,~,obj,hTable,hAx)
%i_TableShading toggle table shading in table 

if obj.TableShading
    CMap = int16(get(ancestor(hAx,'figure'),'ColorMap')*255);
else
    % white background
    CMap = int16([255,255,255]);
end
hTable.Peer.setColorMap(CMap(:,1),CMap(:,2),CMap(:,3));
set(obj.ViewMenu.TableShading,'Checked',mbconoff(obj.TableShading));

function i_TableShadingCbk(~,~,obj)

obj.TableShading = ~obj.TableShading;

function i_PointGrab(~, evt, hTable,obj)
%i_PointGrab select table cell at start of surface table edit

[rowIndex,colIndex] = iAxisIndexToRowCol(evt.data.DragIndex,obj.RightHandRule);
hTable.selectCell(rowIndex, colIndex);

function i_PointDrag(~, evt, hTable,obj)
%i_PointDrag update table value during drag of table cell in surface


[rowIndex,colIndex] = iAxisIndexToRowCol(evt.data.DragIndex,obj.RightHandRule);

%update table
hTable.Peer.setTableDataAt(rowIndex-1, colIndex-1, evt.data.DragValue);


function i_PointUpdate(hDragger, evt,table, obj)
%i_PointDrag update table value after table edit in surface

[rowIndex,colIndex] = iAxisIndexToRowCol(evt.data.ChangedIndex,obj.RightHandRule);

i_changevalue(obj, rowIndex, colIndex, ...
    evt.data.NewValue, table,hDragger,'Manual edit using graph');

function [rowIndex,colIndex] = iAxisIndexToRowCol(AxisIndex,RightHandRule)
%iAxisIndexToRowCol convert axis index in surface to table rows and columns
if RightHandRule
    % use convention rows=xaxis, cols=yaxis
    rowIndex = AxisIndex(1);
    colIndex = AxisIndex(2);
else
    % flip row and columns for surface
    % use convention rows=yaxis, cols=xaxis
    rowIndex = AxisIndex(2);
    colIndex = AxisIndex(1);
end

function i_EditCallback(table, evt, obj, hDragger)
%i_EditCallback edit a table cell in table

% use convention y=rows, x=cols (Table doesn't use RightHandRule)
rowIndices = evt.data.Rows;
colIndices = evt.data.Columns;

if length(evt.data.NewValue)>1
    % can only change more than one value by pasting
    i_changevalue(obj,rowIndices, colIndices, evt.data.NewValue, table, hDragger, 'Paste operation');
else
    % assume manual edit when editing a single cell
    i_changevalue(obj, rowIndices, colIndices, evt.data.NewValue, table, hDragger, 'Manual edit');
end

function i_SelectionChanged(~,evt,obj,hDragger)
%i_SelectionChanged update selection in surface based on selection in table

if ~isempty(evt.data.SelectedRows) && ~isempty(evt.data.SelectedColumns)
    % use convention xaxis=rows, yaxis=cols
    rowIndices = evt.data.SelectedRows;
    colIndices = evt.data.SelectedColumns;
    if obj.RightHandRule
        % uses convention xaxis = rows(Y), yaxis = cols(X)
        xaxis = rowIndices;
        yaxis = colIndices;
    else
        % flip row and columns for surface
        % uses convention xaxis = cols, yaxis = rows
        xaxis = colIndices;
        yaxis = rowIndices;
    end
    % make grid of selected points
    [Xgrid,Ygrid]=ndgrid(xaxis,yaxis);
    Selected = [Xgrid(:),Ygrid(:)];
else
    Selected = zeros(0,2);
end
% update surface selection
setTableData(hDragger, hDragger.XData, hDragger.YData, hDragger.ZData, hDragger.LockData,Selected)

    
function i_changevalue(obj, rowIndices, colIndices, newval, hTable, hDragger, ChangeString)
%i_changevalue update new value in table

T = obj.TablePtr;
tbl = T.info;

table_data = get(tbl, 'values');
table_changed_data = table_data(rowIndices, colIndices);
if all(table_changed_data(:)==newval(:) ...
        | (isnan(table_changed_data(:)) & isnan(newval(:))))
    % Data has not been altered
    return
end

% Update table values in cglookuptwo
new_table_data = table_data;
new_table_data(rowIndices, colIndices) = newval;
% limits in cglookuptwo could mean that the change is not applied
tbl = set(tbl, 'values', {new_table_data, ChangeString});
new_table_changed_data = new_table_data(rowIndices, colIndices);
% Use the table-filtered values for updating
new_table_data = get(tbl, 'values');

% update table
hTable.Peer.setTableData(new_table_data);
% update color range 
lims = mbcmakelimits(new_table_data, 'loose');
hTable.Peer.setColorRange(lims(1),lims(2));

% Always update the surface with the new table values
if obj.RightHandRule
    hDragger.zdata = new_table_data;
else
    % surface data needs transposing when ~obj.RightHandRule
    hDragger.zdata = new_table_data';
end
set(hDragger.Parent, 'ZLim', lims);

if all(table_changed_data(:)==new_table_changed_data(:) ...
        | (isnan(table_changed_data(:)) & isnan(new_table_changed_data(:))))
    % The set operation filtered the value back to the original
    return
end

% Update table pointer
T.info = tbl;

% Send a change event
send(obj,'ValuesChanged', handle.EventData(obj, 'ValuesChanged') );


function i_seteditoreditstate(~, ~, obj, state)
%i_seteditoreditstate change edit mode for surface
obj.GraphEditingEnable = state;


function i_rhrule(~,~,obj)
%i_rhrule use Right-Hand rule in surface  (row=y, col=x)
obj.RightHandRule = true;

function i_lhrule(~,~,obj)
%i_lhrule don't use Right-Hand rule in surface (row=x, col=y)
obj.RightHandRule = false;

function iPrintCopy(~,~,hAxes)
%iPrintCopy copy table surface to separate figure
f = figure;
ax = copyobj(hAxes,f);
set(ax,'Units','Normalized',...
    'Position',[0.1300 0.1100 0.7750 0.8150],...
    'UIContextMenu',[],...
    'ButtonDownFcn',[]);
set(findobj(ax),'ButtonDownFcn',[]);