mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-09-01 15:00:24 +02:00
ValEdit: Refactoring. Store ItemProps in TFPObjectList, so it's easier
to Insert, Exchange, Clear and keep it in sync with Strings. git-svn-id: trunk@40294 -
This commit is contained in:
parent
7ce04606cc
commit
5acb32b369
127
lcl/valedit.pas
127
lcl/valedit.pas
@ -5,7 +5,8 @@ unit ValEdit;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, Controls, StdCtrls, SysUtils, Grids, LResources, Dialogs, LazUtf8, variants, LCLProc;
|
Classes, Controls, StdCtrls, SysUtils, Grids, LResources, Dialogs, LazUtf8, variants, LCLProc,
|
||||||
|
ContNrs;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
@ -47,16 +48,34 @@ type
|
|||||||
property ReadOnly: Boolean read FReadOnly write SetReadOnly;
|
property ReadOnly: Boolean read FReadOnly write SetReadOnly;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TItemProps = array of TItemProp;
|
TItemPropList = class
|
||||||
|
private
|
||||||
|
FList: TFPObjectList;
|
||||||
|
function GetCount: Integer;
|
||||||
|
function GetItem(Index: Integer): TItemProp;
|
||||||
|
procedure SetItem(Index: Integer; AValue: TItemProp);
|
||||||
|
protected
|
||||||
|
public
|
||||||
|
procedure Add(AValue: TItemProp);
|
||||||
|
procedure Clear;
|
||||||
|
procedure Delete(Index: Integer);
|
||||||
|
procedure Exchange(Index1, Index2: Integer);
|
||||||
|
procedure Insert(Index: Integer; AValue: TItemProp);
|
||||||
|
public
|
||||||
|
constructor Create;
|
||||||
|
destructor Destroy; override;
|
||||||
|
public
|
||||||
|
property Count: Integer read GetCount;
|
||||||
|
property Items[Index: Integer]: TItemProp read GetItem write SetItem; default;
|
||||||
|
end;
|
||||||
|
|
||||||
{ TValueListStrings }
|
{ TValueListStrings }
|
||||||
|
|
||||||
TValueListStrings = class(TStringList)
|
TValueListStrings = class(TStringList)
|
||||||
private
|
private
|
||||||
FOwner: TValueListEditor;
|
FOwner: TValueListEditor;
|
||||||
FItemProps: TItemProps;
|
FItemProps: TItemPropList;
|
||||||
function GetItemProp(const AKeyOrIndex: Variant): TItemProp;
|
function GetItemProp(const AKeyOrIndex: Variant): TItemProp;
|
||||||
procedure FreeItemProps;
|
|
||||||
protected
|
protected
|
||||||
procedure SetTextStr(const Value: string); override;
|
procedure SetTextStr(const Value: string); override;
|
||||||
procedure InsertItem(Index: Integer; const S: string; AObject: TObject); override;
|
procedure InsertItem(Index: Integer; const S: string; AObject: TObject); override;
|
||||||
@ -351,6 +370,62 @@ begin
|
|||||||
FKeyDesc := AValue;
|
FKeyDesc := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
{ TItemPropList }
|
||||||
|
|
||||||
|
function TItemPropList.GetItem(Index: Integer): TItemProp;
|
||||||
|
begin
|
||||||
|
Result := TItemProp(FList.Items[Index]);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TItemPropList.GetCount: Integer;
|
||||||
|
begin
|
||||||
|
Result := FList.Count;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TItemPropList.SetItem(Index: Integer; AValue: TItemProp);
|
||||||
|
begin
|
||||||
|
FList.Items[Index] := AValue;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TItemPropList.Insert(Index: Integer; AValue: TItemProp);
|
||||||
|
begin
|
||||||
|
FList.Insert(Index, AValue);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TItemPropList.Add(AValue: TItemProp);
|
||||||
|
begin
|
||||||
|
FList.Add(AValue);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TItemPropList.Delete(Index: Integer);
|
||||||
|
begin
|
||||||
|
FList.Delete(Index);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TItemPropList.Exchange(Index1, Index2: Integer);
|
||||||
|
begin
|
||||||
|
FList.Exchange(Index1, index2);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TItemPropList.Clear;
|
||||||
|
begin
|
||||||
|
FList.Clear;
|
||||||
|
end;
|
||||||
|
|
||||||
|
constructor TItemPropList.Create;
|
||||||
|
begin
|
||||||
|
FList := TFPObjectList.Create(True);
|
||||||
|
end;
|
||||||
|
|
||||||
|
destructor TItemPropList.Destroy;
|
||||||
|
begin
|
||||||
|
FList.Free;
|
||||||
|
inherited Destroy;
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{ TValueListStrings }
|
{ TValueListStrings }
|
||||||
|
|
||||||
procedure TValueListStrings.SetTextStr(const Value: string);
|
procedure TValueListStrings.SetTextStr(const Value: string);
|
||||||
@ -369,11 +444,7 @@ begin
|
|||||||
IsShowingEditor := goAlwaysShowEditor in FOwner.Options;
|
IsShowingEditor := goAlwaysShowEditor in FOwner.Options;
|
||||||
if IsShowingEditor then FOwner.Options := FOwner.Options - [goAlwaysShowEditor];
|
if IsShowingEditor then FOwner.Options := FOwner.Options - [goAlwaysShowEditor];
|
||||||
inherited InsertItem(Index, S, AObject);
|
inherited InsertItem(Index, S, AObject);
|
||||||
|
FItemProps.Insert(Index, TItemProp.Create(FOwner));
|
||||||
SetLength(FItemProps, Count);
|
|
||||||
for i := Count-2 downto Index do
|
|
||||||
FItemProps[i+1] := FItemProps[i];
|
|
||||||
FItemProps[Index] := nil;
|
|
||||||
//only restore this _after_ FItemProps is updated!
|
//only restore this _after_ FItemProps is updated!
|
||||||
if IsShowingEditor then FOwner.Options := FOwner.Options + [goAlwaysShowEditor];
|
if IsShowingEditor then FOwner.Options := FOwner.Options + [goAlwaysShowEditor];
|
||||||
end;
|
end;
|
||||||
@ -405,11 +476,12 @@ constructor TValueListStrings.Create(AOwner: TValueListEditor);
|
|||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
FOwner := AOwner;
|
FOwner := AOwner;
|
||||||
|
FItemProps := TItemPropList.Create;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TValueListStrings.Destroy;
|
destructor TValueListStrings.Destroy;
|
||||||
begin
|
begin
|
||||||
FreeItemProps;
|
FItemProps.Free;
|
||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -426,8 +498,8 @@ begin
|
|||||||
IsShowingEditor := goAlwaysShowEditor in FOwner.Options;
|
IsShowingEditor := goAlwaysShowEditor in FOwner.Options;
|
||||||
if IsShowingEditor then FOwner.Options := FOwner.Options - [goAlwaysShowEditor];
|
if IsShowingEditor then FOwner.Options := FOwner.Options - [goAlwaysShowEditor];
|
||||||
inherited Clear;
|
inherited Clear;
|
||||||
|
FItemProps.Clear;
|
||||||
if IsShowingEditor then FOwner.Options := FOwner.Options + [goAlwaysShowEditor];
|
if IsShowingEditor then FOwner.Options := FOwner.Options + [goAlwaysShowEditor];
|
||||||
FreeItemProps;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TValueListStrings.CustomSort(Compare: TStringListSortCompare);
|
procedure TValueListStrings.CustomSort(Compare: TStringListSortCompare);
|
||||||
@ -445,14 +517,7 @@ begin
|
|||||||
if IsShowingEditor then FOwner.Options := FOwner.Options - [goAlwaysShowEditor];
|
if IsShowingEditor then FOwner.Options := FOwner.Options - [goAlwaysShowEditor];
|
||||||
inherited Delete(Index);
|
inherited Delete(Index);
|
||||||
// Delete also ItemProps
|
// Delete also ItemProps
|
||||||
if Index<=Count then begin
|
FItemProps.Delete(Index);
|
||||||
if Assigned(FItemProps[Index]) then FItemProps[Index].Free;
|
|
||||||
for i := Index to Length(FItemProps)-2 do
|
|
||||||
begin
|
|
||||||
FItemProps[i] := FItemProps[i+1];
|
|
||||||
end;
|
|
||||||
SetLength(FItemProps, Count);
|
|
||||||
end;
|
|
||||||
//only restore this _after_ FItemProps is updated!
|
//only restore this _after_ FItemProps is updated!
|
||||||
if IsShowingEditor then FOwner.Options := FOwner.Options + [goAlwaysShowEditor];
|
if IsShowingEditor then FOwner.Options := FOwner.Options + [goAlwaysShowEditor];
|
||||||
end;
|
end;
|
||||||
@ -460,7 +525,7 @@ end;
|
|||||||
procedure TValueListStrings.Exchange(Index1, Index2: Integer);
|
procedure TValueListStrings.Exchange(Index1, Index2: Integer);
|
||||||
begin
|
begin
|
||||||
inherited Exchange(Index1, Index2);
|
inherited Exchange(Index1, Index2);
|
||||||
// ToDo: Exchange also ItemProps
|
FItemProps.Exchange(Index1, Index2);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TValueListStrings.GetItemProp(const AKeyOrIndex: Variant): TItemProp;
|
function TValueListStrings.GetItemProp(const AKeyOrIndex: Variant): TItemProp;
|
||||||
@ -480,28 +545,12 @@ begin
|
|||||||
if i = -1 then
|
if i = -1 then
|
||||||
raise Exception.Create('TValueListStrings.GetItemProp: Key not found: '+s);
|
raise Exception.Create('TValueListStrings.GetItemProp: Key not found: '+s);
|
||||||
end;
|
end;
|
||||||
if i >= Length(FItemProps) then
|
Result := FItemProps.Items[i];
|
||||||
SetLength(FItemProps, i+1);
|
if not Assigned(Result) then
|
||||||
Result := FItemProps[i];
|
Raise Exception.Create(Format('TValueListStrings.GetItemProp: Index=%d Result=Nil',[i]));
|
||||||
if not Assigned(Result) then begin
|
|
||||||
Result := TItemProp.Create(FOwner);
|
|
||||||
FItemProps[i] := Result;
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TValueListStrings.FreeItemProps;
|
|
||||||
var
|
|
||||||
i: Integer;
|
|
||||||
begin
|
|
||||||
//{$R+}
|
|
||||||
//debugln('TValueListStrings.Destroy: Length(FItemProps) = ',dbgs(Length(FItemProps)));
|
|
||||||
for i := 0 to Length(FItemProps) - 1 do
|
|
||||||
begin
|
|
||||||
if Assigned(FItemProps[i]) then FItemProps[i].Free;
|
|
||||||
end;
|
|
||||||
SetLength(FItemProps, 0);
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ TValueListEditor }
|
{ TValueListEditor }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user