視圖用來表現(xiàn)數(shù)據(jù),也是用戶接口。當(dāng)數(shù)據(jù)更新時,視圖應(yīng)該隨之改變,在交互過程中,視圖需要響應(yīng)用戶,這些情況下都會引起視圖的繪制。 cxGrid可以用多種視圖表現(xiàn)關(guān)系型數(shù)據(jù),最常用的是表格視圖,我們以表格視圖作為默認的對象,分析cxGrid用以實現(xiàn)視圖繪制的基本結(jié)構(gòu)。
表格視圖按行,列布局,行優(yōu)先順序繪制,繪制的基本元素是行和單元(cell)。由于數(shù)據(jù)內(nèi)容的動態(tài)性,行是動態(tài)生成的。表格視圖提供了各種可定制性,包括可選的分組行風(fēng)格和若干自定義風(fēng)格,這些復(fù)雜特性背后有很好的設(shè)計。
視圖繪制相關(guān)的幾組類:
ViewInfo類族:
描述視圖元素的繪制信息,視圖元素要繪制成什么樣子,完全由其ViewInfo確定。祖先類是TcxCustomGridCellViewInfo,數(shù)據(jù)行,數(shù)據(jù)單元,分組行等視圖元素以及視圖本身都有相應(yīng)的派生類。
Painter類族:
按照ViewInfo,在畫布上繪制視圖元素。祖先類是TcxCustomGridCellPainter。 TcxCustomGridCellPainter = class
private
FCanvas: TcxCanvas;
FViewInfo: TcxCustomGridCellViewInfo;
View類族:
整個視圖的描述,包括了各種設(shè)置信息。祖先是TcxCustomGridView。
View和ViewInfo的不同在ViewInfo描述繪制信息,而View描述設(shè)置信息,ViewInfo是動態(tài)構(gòu)造的,當(dāng)Grid創(chuàng)建時,如果有一個分組多條記錄,那么每條記錄會生成一個ViewInfo實例,如果收起分組,這些實例將被銷毀,展開分組,會再次生成每條記錄的ViewInfo實例,而一個表格視圖始終只有一個實例。
LookAndFeelPainter類族:
用于進行有關(guān)分組行風(fēng)格的繪制。祖先是TcxCustomLookAndFeelPainter。
繪制視圖元素時,涉及到分組行風(fēng)格(GroupRowStyle)的繪制任務(wù),都被委托給LookAndFeelPainter類的實例,由此實現(xiàn)多態(tài)的風(fēng)格。
Style和Styles類族:
Style類的祖先是TcxCustomStyle,Styles類的祖先是TcxCustomStyles。TcxCustomStyle類表示一種自定義風(fēng)格,TcxCustomStyles類表示一組風(fēng)格。TcxCustomStyles內(nèi)部用一個map保存這些風(fēng)格,給出鍵,可通過Values屬性取出指定的風(fēng)格。
TcxCustomStyles = class(TcxInterfacedPersistent, IcxStyleChangeListener)
public
property Values[Index: Integer]: TcxCustomStyle read GetValue write SetValue;
鍵的取值范圍如下:
vsCustomTableFirst = vsCustomLast + 1;
vsContent = vsCustomTableFirst;
vsContentEven = vsCustomTableFirst + 1;
vsContentOdd = vsCustomTableFirst + 2;
vsFilterBox = vsCustomTableFirst + 3;
vsInactive = vsCustomTableFirst + 4;
vsIncSearch = vsCustomTableFirst + 5;
vsSelection = vsCustomTableFirst + 6;
// vsCustomTableLast = vsSelection;
vsHotTrack = vsCustomTableFirst + 7;
vsCustomTableLast = vsHotTrack;
考查以上幾組類的關(guān)系:
Painter和ViewInfo:
Painter類和ViewInfo類具有對應(yīng)關(guān)系,在視圖元素繪制時創(chuàng)建出Painter類的實例,其中有多態(tài)機制的作用,下面具體了解一下這個創(chuàng)建過程。
在TcxCustomGridCellPainter派生的一組類中,只有它自己有構(gòu)造函數(shù):
constructor TcxCustomGridCellPainter.Create(ACanvas: TcxCanvas;
AViewInfo: TcxCustomGridCellViewInfo);
begin
inherited Create;
FCanvas := ACanvas;
FViewInfo := AViewInfo;
end;
那么當(dāng)需要調(diào)用其派生類方法時,是如何構(gòu)造出派生類實例的?Painter類實例都是在ViewInfo類的方法中創(chuàng)建出來的,代碼如下:
GetPainterClass.Create()
TcxGridDataRowViewInfo = class(TcxCustomGridRowViewInfo)
protected:
function GetPainterClass: TcxCustomGridCellPainterClass; override;
function TcxGridDataRowViewInfo.GetPainterClass: TcxCustomGridCellPainterClass;
begin
Result := TcxGridDataRowPainter;
end;
這樣,創(chuàng)建哪個Painter類的實例決定于ViewInfo類的實例,通過某個ViewInfo實例調(diào)用GetPainterClass.Create(),就返回對應(yīng)Painter類的實例。
ViewInfo類進行繪制時,會調(diào)用Painter類的繪制方法;Painter類進行繪制時,也會調(diào)用ViewInfo類的繪制方法,比如:
procedure TcxCustomGridRecordsPainter.Paint;
var
I: Integer;
begin
with FViewInfo do
for I := 0 to Count - 1 do
with Items[I] do
if Calculated then Paint;
end;
跟蹤繪制過程可以看到,正是在兩種調(diào)用的交替中繪出了視圖的每個元素,最終在顯示設(shè)備上的繪制是由Painter類實現(xiàn)的,而ViewInfo的繪制方法會起到調(diào)度的作用,比如把數(shù)據(jù)行的繪制分解成各數(shù)據(jù)單元的繪制。
我們已經(jīng)了解到,前一種調(diào)用是通過構(gòu)造局部實例,那么后一種呢?
TcxCustomGridCellPainter中定義了FViewInfo私有成員,并將其定義為保護屬性
TcxCustomGridCellPainter = class
private
FCanvas: TcxCanvas;
FViewInfo: TcxCustomGridCellViewInfo;
protected
property ViewInfo: TcxCustomGridCellViewInfo read FViewInfo;
其派生類繼承了該屬性,并將其重新定義
TcxGridDataRowPainter = class(TcxCustomGridRowPainter)
private
function GetViewInfo: TcxGridDataRowViewInfo;
protected
property ViewInfo: TcxGridDataRowViewInfo read GetViewInfo;
function TcxGridDataRowPainter.GetViewInfo: TcxGridDataRowViewInfo;
begin
Result := TcxGridDataRowViewInfo(inherited ViewInfo);
end;
View和Styles:
視圖風(fēng)格是視圖的屬性:
TcxCustomGridView = class(TcxControlChildComponent, IcxStoredObject, IcxStoredParent,
IcxGridViewLayoutEditorSupport, IcxGridViewDesignerMenu)
private
FStyles: TcxCustomGridStyles;
Protected:
property Styles: TcxCustomGridStyles read FStyles write SetStyles;
procedure TcxCustomGridView.SetStyles(Value: TcxCustomGridStyles);
begin
FStyles.Assign(Value);
end;
特定的視圖使用特定的視圖風(fēng)格,以表視圖為例,可以看到兩者是如何對應(yīng)的。
TcxGridTableView = class(TcxCustomGridTableView)
Published:
property Styles: TcxGridTableViewStyles read GetStyles write SetStyles;
function TcxGridTableView.GetStyles: TcxGridTableViewStyles;
begin
Result := TcxGridTableViewStyles(inherited Styles);
end;
procedure TcxGridTableView.SetStyles(Value: TcxGridTableViewStyles);
begin
inherited Styles := Value;
end;
View和LookAndFeelPainter:
ViewInfo,GridStyles和GridView類都有LookAndFeelPainter屬性,因為分組行風(fēng)格設(shè)置是作用于整個視圖的,所以該屬性真正定義是在GridView中,GridStyles和ViewInfo只是返回一個引用,提供該屬性是為了方便,比如ViewInfo進行繪制時,要用到風(fēng)格的設(shè)置。
function TcxCustomGridView.GetLookAndFeelPainter: TcxCustomLookAndFeelPainterClass;
begin
if Control = nil then
Result := TcxStandardLookAndFeelPainter
else
Result := TcxCustomGrid(Control).LookAndFeelPainter;
end;
function TcxCustomGridStyles.GetLookAndFeelPainter: TcxCustomLookAndFeelPainterClass;
begin
Result := GridView.LookAndFeelPainter;
end;
/* TcxGridViewHandler是視圖ViewInfo的祖先類。*/
function TcxGridViewHandler.GetLookAndFeelPainter: TcxCustomLookAndFeelPainterClass;
begin
Result := FGridView.LookAndFeelPainter;
end;
/* TcxCustomGridViewCellViewInfo是視圖元素ViewInfo的祖先類,FGridViewInfo是GridViewInfo屬性的成員變量。*/
function TcxCustomGridViewCellViewInfo.GetLookAndFeelPainter: TcxCustomLookAndFeelPainterClass;
begin
Result := FGridViewInfo.LookAndFeelPainter;
end;