From c25cf46ae62c43af4b505d8e1b81c2bee014f705 Mon Sep 17 00:00:00 2001 From: ask Date: Wed, 2 Feb 2011 13:20:31 +0000 Subject: [PATCH] TAChart: Auto-rename subcomponents when parent component name changes git-svn-id: trunk@29331 - --- components/tachart/tachartutils.pas | 48 +++++++++++++++++++++++- components/tachart/tagraph.pas | 23 +++++++++--- components/tachart/tatransformations.pas | 17 ++++++++- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/components/tachart/tachartutils.pas b/components/tachart/tachartutils.pas index ec66efe78b..fbe1be226f 100644 --- a/components/tachart/tachartutils.pas +++ b/components/tachart/tachartutils.pas @@ -113,14 +113,25 @@ type { TIndexedComponent } - TIndexedComponent = class (TComponent) + TIndexedComponent = class(TComponent) protected function GetIndex: Integer; virtual; abstract; procedure SetIndex(AValue: Integer); virtual; abstract; public + procedure ChangeNamePrefix(const AOld, ANew: String; var AFailed: String); + property Index: Integer read GetIndex write SetIndex; end; + TShowMessageProc = procedure (const AMsg: String); + + { TIndexedComponentList } + + TIndexedComponentList = class(TFPList) + public + procedure ChangeNamePrefix(const AOld, ANew: String); + end; + TBroadcaster = class; { TListener } @@ -291,9 +302,16 @@ operator :=(const ASize: TSize): TPoint; inline; var DrawData: TDrawDataRegistry; + ShowMessageProc: TShowMessageProc; + +resourcestring + tasFailedSubcomponentRename = 'Failed to rename components: %s'; implementation +uses + StrUtils; + const ORIENTATION_UNITS_PER_DEG = 10; @@ -929,6 +947,34 @@ begin Result.Y := ASize.cy; end; +{ TIndexedComponentList } + +procedure TIndexedComponentList.ChangeNamePrefix( + const AOld, ANew: String); +var + failed: String; + i: Integer; +begin + failed := ''; + for i := 0 to Count - 1 do + TIndexedComponent(Items[i]).ChangeNamePrefix(AOld, ANew, failed); + if (failed <> '') and Assigned(ShowMessageProc) then + ShowMessageProc(Format(tasFailedSubcomponentRename, [failed])); +end; + +{ TIndexedComponent } + +procedure TIndexedComponent.ChangeNamePrefix( + const AOld, ANew: String; var AFailed: String); +begin + if AnsiStartsStr(AOld, Name) then + try + Name := ANew + Copy(Name, Length(AOld) + 1, Length(Name)); + except on EComponentError do + AFailed += IfThen(AFailed = '', '', ', ') + Name; + end; +end; + { TIntervalList } procedure TIntervalList.AddPoint(APoint: Double); inline; diff --git a/components/tachart/tagraph.pas b/components/tachart/tagraph.pas index a2a986ae9b..a49e1fd05b 100644 --- a/components/tachart/tagraph.pas +++ b/components/tachart/tagraph.pas @@ -29,7 +29,7 @@ interface uses LCLType, LResources, - SysUtils, Classes, Controls, Graphics, Dialogs, + SysUtils, Classes, Controls, Graphics, TAChartUtils, TATypes, TALegend, TAChartAxis; type @@ -119,7 +119,7 @@ type TChartSeriesList = class(TPersistent) private - FList: TFPList; + FList: TIndexedComponentList; function GetItem(AIndex: Integer): TBasicChartSeries; public constructor Create; @@ -129,7 +129,7 @@ type function Count: Integer; public property Items[AIndex: Integer]: TBasicChartSeries read GetItem; default; - property List: TFPList read FList; + property List: TIndexedComponentList read FList; end; TChartAfterDrawEvent = procedure ( @@ -229,6 +229,7 @@ type procedure PrepareLegend( ACanvas: TCanvas; out ALegendItems: TChartLegendItems; var AClipRect: TRect; out ALegendRect: TRect); + procedure SetName(const AValue: TComponentName); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; @@ -351,7 +352,7 @@ var implementation uses - Clipbrd, GraphMath, LCLProc, Math, Types, TADrawUtils; + Clipbrd, Dialogs, GraphMath, LCLProc, Math, Types, TADrawUtils; function CompareZPosition(AItem1, AItem2: Pointer): Integer; begin @@ -949,6 +950,17 @@ begin Invalidate; end; +procedure TChart.SetName(const AValue: TComponentName); +var + oldName: String; +begin + if Name = AValue then exit; + oldName := Name; + inherited SetName(AValue); + if csDesigning in ComponentState then + Series.List.ChangeNamePrefix(oldName, AValue); +end; + procedure TChart.SetOnAfterDrawBackground(AValue: TChartAfterDrawEvent); begin if FOnAfterDrawBackground = AValue then exit; @@ -1245,7 +1257,7 @@ end; constructor TChartSeriesList.Create; begin - FList := TFPList.Create; + FList := TIndexedComponentList.Create; end; destructor TChartSeriesList.Destroy; @@ -1299,6 +1311,7 @@ initialization {$I tagraph.lrs} SkipObsoleteChartProperties; SeriesClassRegistry := TStringList.Create; + ShowMessageProc := @ShowMessage; finalization FreeAndNil(SeriesClassRegistry); diff --git a/components/tachart/tatransformations.pas b/components/tachart/tatransformations.pas index ba8a9f98d1..d682dc3401 100644 --- a/components/tachart/tatransformations.pas +++ b/components/tachart/tatransformations.pas @@ -70,15 +70,17 @@ type TAxisTransformClass = class of TAxisTransform; - TAxisTransformList = class(TFPList) + TAxisTransformList = class(TIndexedComponentList) end; { TChartAxisTransformations } - TChartAxisTransformations = class (TComponent) + TChartAxisTransformations = class(TComponent) private FBroadcaster: TBroadcaster; FList: TAxisTransformList; + protected + procedure SetName(const AValue: TComponentName); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; @@ -524,6 +526,17 @@ begin List.Move(i, Order); end; +procedure TChartAxisTransformations.SetName(const AValue: TComponentName); +var + oldName: String; +begin + if Name = AValue then exit; + oldName := Name; + inherited SetName(AValue); + if csDesigning in ComponentState then + List.ChangeNamePrefix(oldName, AValue); +end; + procedure TChartAxisTransformations.UpdateBounds(var AMin, AMax: Double); var i: Integer;