mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-14 08:59:13 +02:00
Qt,Qt5: TComboBox.Sorted property implemented. issue #28045
git-svn-id: trunk@60858 -
This commit is contained in:
parent
48a07c9bb8
commit
58bf52dbc4
@ -43,8 +43,11 @@ type
|
|||||||
|
|
||||||
TQtComboStrings = class(TStringList)
|
TQtComboStrings = class(TStringList)
|
||||||
private
|
private
|
||||||
|
FSorted: Boolean;
|
||||||
FWinControl: TWinControl;
|
FWinControl: TWinControl;
|
||||||
FOwner: TQtComboBox;
|
FOwner: TQtComboBox;
|
||||||
|
FChanging: boolean;
|
||||||
|
procedure SetSorted(AValue: Boolean);
|
||||||
protected
|
protected
|
||||||
procedure Put(Index: Integer; const S: string); override;
|
procedure Put(Index: Integer; const S: string); override;
|
||||||
{$IFNDEF HAS_INHERITED_INSERTITEM}
|
{$IFNDEF HAS_INHERITED_INSERTITEM}
|
||||||
@ -54,13 +57,18 @@ type
|
|||||||
public
|
public
|
||||||
constructor Create(AWinControl: TWinControl; AOwner: TQtComboBox);
|
constructor Create(AWinControl: TWinControl; AOwner: TQtComboBox);
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
|
function Add(const S: String): Integer; override;
|
||||||
procedure Assign(Source: TPersistent); override;
|
procedure Assign(Source: TPersistent); override;
|
||||||
procedure Clear; override;
|
procedure Clear; override;
|
||||||
procedure Delete(Index: Integer); override;
|
procedure Delete(Index: Integer); override;
|
||||||
|
function Find(const S: String; out Index: Integer): Boolean;
|
||||||
|
function IndexOf(const S: String): Integer; override;
|
||||||
|
procedure Insert(Index: Integer; const S: String); override;
|
||||||
procedure Sort; override;
|
procedure Sort; override;
|
||||||
procedure Exchange(AIndex1, AIndex2: Integer); override;
|
procedure Exchange(AIndex1, AIndex2: Integer); override;
|
||||||
public
|
public
|
||||||
property Owner: TQtComboBox read FOwner;
|
property Owner: TQtComboBox read FOwner;
|
||||||
|
property Sorted: Boolean read FSorted write SetSorted;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
@ -460,6 +468,24 @@ end;
|
|||||||
|
|
||||||
{ TQtComboStrings }
|
{ TQtComboStrings }
|
||||||
|
|
||||||
|
procedure TQtComboStrings.SetSorted(AValue: Boolean);
|
||||||
|
var
|
||||||
|
i: Integer;
|
||||||
|
begin
|
||||||
|
if FSorted=AValue then Exit;
|
||||||
|
FSorted:=AValue;
|
||||||
|
if not FSorted then Exit;
|
||||||
|
|
||||||
|
for i := 0 to Count - 2 do
|
||||||
|
begin
|
||||||
|
if UTF8CompareText(Strings[i], Strings[i + 1]) < 0 then
|
||||||
|
begin
|
||||||
|
Sort;
|
||||||
|
Break;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TQtComboStrings.Put(Index: Integer; const S: string);
|
procedure TQtComboStrings.Put(Index: Integer; const S: string);
|
||||||
begin
|
begin
|
||||||
inherited Put(Index, S);
|
inherited Put(Index, S);
|
||||||
@ -467,6 +493,7 @@ begin
|
|||||||
FOwner.setItemText(Index, S);
|
FOwner.setItemText(Index, S);
|
||||||
FOwner.EndUpdate;
|
FOwner.EndUpdate;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{$IFNDEF HAS_INHERITED_INSERTITEM}
|
{$IFNDEF HAS_INHERITED_INSERTITEM}
|
||||||
procedure TQtComboStrings.InsertItem(Index: Integer; const S: string);
|
procedure TQtComboStrings.InsertItem(Index: Integer; const S: string);
|
||||||
var
|
var
|
||||||
@ -487,6 +514,7 @@ begin
|
|||||||
FOwner.EndUpdate;
|
FOwner.EndUpdate;
|
||||||
end;
|
end;
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
|
|
||||||
procedure TQtComboStrings.InsertItem(Index: Integer; const S: string; O: TObject);
|
procedure TQtComboStrings.InsertItem(Index: Integer; const S: string; O: TObject);
|
||||||
var
|
var
|
||||||
FSavedIndex: Integer;
|
FSavedIndex: Integer;
|
||||||
@ -512,6 +540,8 @@ begin
|
|||||||
inherited Create;
|
inherited Create;
|
||||||
FWinControl := AWinControl;
|
FWinControl := AWinControl;
|
||||||
FOwner := AOwner;
|
FOwner := AOwner;
|
||||||
|
FSorted := TComboBox(AOwner.LCLObject).Sorted;
|
||||||
|
FChanging := False;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TQtComboStrings.Destroy;
|
destructor TQtComboStrings.Destroy;
|
||||||
@ -520,12 +550,38 @@ begin
|
|||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TQtComboStrings.Assign(Source: TPersistent);
|
function TQtComboStrings.Add(const S: String): Integer;
|
||||||
begin
|
begin
|
||||||
|
if FSorted then
|
||||||
|
begin
|
||||||
|
Find(S, Result);
|
||||||
|
FChanging := True;
|
||||||
|
Insert(Result, S);
|
||||||
|
FChanging := False;
|
||||||
|
end else
|
||||||
|
Result := inherited Add(S);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TQtComboStrings.Assign(Source: TPersistent);
|
||||||
|
var
|
||||||
|
AList: TStringList;
|
||||||
|
begin
|
||||||
|
if (Source = Self) or (Source = nil) then Exit;
|
||||||
if Assigned(FWinControl) and (FWinControl.HandleAllocated) then
|
if Assigned(FWinControl) and (FWinControl.HandleAllocated) then
|
||||||
begin
|
begin
|
||||||
FOwner.BeginUpdate;
|
FOwner.BeginUpdate;
|
||||||
inherited Assign(Source);
|
if Sorted then
|
||||||
|
begin
|
||||||
|
AList := TStringList.Create;
|
||||||
|
try
|
||||||
|
AList.Assign(Source);
|
||||||
|
AList.Sort;
|
||||||
|
inherited Assign(AList);
|
||||||
|
finally
|
||||||
|
AList.Free;
|
||||||
|
end;
|
||||||
|
end else
|
||||||
|
inherited Assign(Source);
|
||||||
FOwner.EndUpdate;
|
FOwner.EndUpdate;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -553,6 +609,56 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TQtComboStrings.Find(const S: String; out Index: Integer): Boolean;
|
||||||
|
var
|
||||||
|
L, R, I: Integer;
|
||||||
|
CompareRes: PtrInt;
|
||||||
|
begin
|
||||||
|
Result := False;
|
||||||
|
// Use binary search.
|
||||||
|
L := 0;
|
||||||
|
R := Count - 1;
|
||||||
|
while (L <= R) do
|
||||||
|
begin
|
||||||
|
I := L + (R - L) div 2;
|
||||||
|
CompareRes := UTF8CompareText(S, Strings[I]);
|
||||||
|
if (CompareRes > 0) then
|
||||||
|
L := I + 1
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
R := I - 1;
|
||||||
|
if (CompareRes = 0) then
|
||||||
|
begin
|
||||||
|
Result := True;
|
||||||
|
L := I; // forces end of while loop
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
Index := L;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TQtComboStrings.IndexOf(const S: String): Integer;
|
||||||
|
begin
|
||||||
|
Result := -1;
|
||||||
|
if FSorted then
|
||||||
|
begin
|
||||||
|
//Binary Search
|
||||||
|
if not Find(S, Result) then
|
||||||
|
Result := -1;
|
||||||
|
end else
|
||||||
|
Result := inherited IndexOf(S);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TQtComboStrings.Insert(Index: Integer; const S: String);
|
||||||
|
begin
|
||||||
|
if FSorted and not FChanging then
|
||||||
|
begin
|
||||||
|
inherited Insert(Index, S);
|
||||||
|
Sort;
|
||||||
|
end else
|
||||||
|
inherited Insert(Index, S);
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TQtComboStrings.Sort;
|
procedure TQtComboStrings.Sort;
|
||||||
var
|
var
|
||||||
I: Integer;
|
I: Integer;
|
||||||
|
@ -43,8 +43,11 @@ type
|
|||||||
|
|
||||||
TQtComboStrings = class(TStringList)
|
TQtComboStrings = class(TStringList)
|
||||||
private
|
private
|
||||||
|
FSorted: Boolean;
|
||||||
FWinControl: TWinControl;
|
FWinControl: TWinControl;
|
||||||
FOwner: TQtComboBox;
|
FOwner: TQtComboBox;
|
||||||
|
FChanging: boolean;
|
||||||
|
procedure SetSorted(AValue: Boolean);
|
||||||
protected
|
protected
|
||||||
procedure Put(Index: Integer; const S: string); override;
|
procedure Put(Index: Integer; const S: string); override;
|
||||||
{$IFNDEF HAS_INHERITED_INSERTITEM}
|
{$IFNDEF HAS_INHERITED_INSERTITEM}
|
||||||
@ -54,13 +57,18 @@ type
|
|||||||
public
|
public
|
||||||
constructor Create(AWinControl: TWinControl; AOwner: TQtComboBox);
|
constructor Create(AWinControl: TWinControl; AOwner: TQtComboBox);
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
|
function Add(const S: String): Integer; override;
|
||||||
procedure Assign(Source: TPersistent); override;
|
procedure Assign(Source: TPersistent); override;
|
||||||
procedure Clear; override;
|
procedure Clear; override;
|
||||||
procedure Delete(Index: Integer); override;
|
procedure Delete(Index: Integer); override;
|
||||||
|
function Find(const S: String; out Index: Integer): Boolean;
|
||||||
|
function IndexOf(const S: String): Integer; override;
|
||||||
|
procedure Insert(Index: Integer; const S: String); override;
|
||||||
procedure Sort; override;
|
procedure Sort; override;
|
||||||
procedure Exchange(AIndex1, AIndex2: Integer); override;
|
procedure Exchange(AIndex1, AIndex2: Integer); override;
|
||||||
public
|
public
|
||||||
property Owner: TQtComboBox read FOwner;
|
property Owner: TQtComboBox read FOwner;
|
||||||
|
property Sorted: Boolean read FSorted write SetSorted;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
@ -463,6 +471,24 @@ end;
|
|||||||
|
|
||||||
{ TQtComboStrings }
|
{ TQtComboStrings }
|
||||||
|
|
||||||
|
procedure TQtComboStrings.SetSorted(AValue: Boolean);
|
||||||
|
var
|
||||||
|
i: Integer;
|
||||||
|
begin
|
||||||
|
if FSorted=AValue then Exit;
|
||||||
|
FSorted:=AValue;
|
||||||
|
if not FSorted then Exit;
|
||||||
|
|
||||||
|
for i := 0 to Count - 2 do
|
||||||
|
begin
|
||||||
|
if UTF8CompareText(Strings[i], Strings[i + 1]) < 0 then
|
||||||
|
begin
|
||||||
|
Sort;
|
||||||
|
Break;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TQtComboStrings.Put(Index: Integer; const S: string);
|
procedure TQtComboStrings.Put(Index: Integer; const S: string);
|
||||||
begin
|
begin
|
||||||
inherited Put(Index, S);
|
inherited Put(Index, S);
|
||||||
@ -470,6 +496,7 @@ begin
|
|||||||
FOwner.setItemText(Index, S);
|
FOwner.setItemText(Index, S);
|
||||||
FOwner.EndUpdate;
|
FOwner.EndUpdate;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{$IFNDEF HAS_INHERITED_INSERTITEM}
|
{$IFNDEF HAS_INHERITED_INSERTITEM}
|
||||||
procedure TQtComboStrings.InsertItem(Index: Integer; const S: string);
|
procedure TQtComboStrings.InsertItem(Index: Integer; const S: string);
|
||||||
var
|
var
|
||||||
@ -490,6 +517,7 @@ begin
|
|||||||
FOwner.EndUpdate;
|
FOwner.EndUpdate;
|
||||||
end;
|
end;
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
|
|
||||||
procedure TQtComboStrings.InsertItem(Index: Integer; const S: string; O: TObject);
|
procedure TQtComboStrings.InsertItem(Index: Integer; const S: string; O: TObject);
|
||||||
var
|
var
|
||||||
FSavedIndex: Integer;
|
FSavedIndex: Integer;
|
||||||
@ -516,6 +544,8 @@ begin
|
|||||||
inherited Create;
|
inherited Create;
|
||||||
FWinControl := AWinControl;
|
FWinControl := AWinControl;
|
||||||
FOwner := AOwner;
|
FOwner := AOwner;
|
||||||
|
FSorted := TComboBox(AOwner.LCLObject).Sorted;
|
||||||
|
FChanging := False;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TQtComboStrings.Destroy;
|
destructor TQtComboStrings.Destroy;
|
||||||
@ -524,12 +554,38 @@ begin
|
|||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TQtComboStrings.Assign(Source: TPersistent);
|
function TQtComboStrings.Add(const S: String): Integer;
|
||||||
begin
|
begin
|
||||||
|
if FSorted then
|
||||||
|
begin
|
||||||
|
Find(S, Result);
|
||||||
|
FChanging := True;
|
||||||
|
Insert(Result, S);
|
||||||
|
FChanging := False;
|
||||||
|
end else
|
||||||
|
Result := inherited Add(S);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TQtComboStrings.Assign(Source: TPersistent);
|
||||||
|
var
|
||||||
|
AList: TStringList;
|
||||||
|
begin
|
||||||
|
if (Source = Self) or (Source = nil) then Exit;
|
||||||
if Assigned(FWinControl) and (FWinControl.HandleAllocated) then
|
if Assigned(FWinControl) and (FWinControl.HandleAllocated) then
|
||||||
begin
|
begin
|
||||||
FOwner.BeginUpdate;
|
FOwner.BeginUpdate;
|
||||||
inherited Assign(Source);
|
if Sorted then
|
||||||
|
begin
|
||||||
|
AList := TStringList.Create;
|
||||||
|
try
|
||||||
|
AList.Assign(Source);
|
||||||
|
AList.Sort;
|
||||||
|
inherited Assign(AList);
|
||||||
|
finally
|
||||||
|
AList.Free;
|
||||||
|
end;
|
||||||
|
end else
|
||||||
|
inherited Assign(Source);
|
||||||
FOwner.EndUpdate;
|
FOwner.EndUpdate;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -557,6 +613,56 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TQtComboStrings.Find(const S: String; out Index: Integer): Boolean;
|
||||||
|
var
|
||||||
|
L, R, I: Integer;
|
||||||
|
CompareRes: PtrInt;
|
||||||
|
begin
|
||||||
|
Result := False;
|
||||||
|
// Use binary search.
|
||||||
|
L := 0;
|
||||||
|
R := Count - 1;
|
||||||
|
while (L <= R) do
|
||||||
|
begin
|
||||||
|
I := L + (R - L) div 2;
|
||||||
|
CompareRes := UTF8CompareText(S, Strings[I]);
|
||||||
|
if (CompareRes > 0) then
|
||||||
|
L := I + 1
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
R := I - 1;
|
||||||
|
if (CompareRes = 0) then
|
||||||
|
begin
|
||||||
|
Result := True;
|
||||||
|
L := I; // forces end of while loop
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
Index := L;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TQtComboStrings.IndexOf(const S: String): Integer;
|
||||||
|
begin
|
||||||
|
Result := -1;
|
||||||
|
if FSorted then
|
||||||
|
begin
|
||||||
|
//Binary Search
|
||||||
|
if not Find(S, Result) then
|
||||||
|
Result := -1;
|
||||||
|
end else
|
||||||
|
Result := inherited IndexOf(S);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TQtComboStrings.Insert(Index: Integer; const S: String);
|
||||||
|
begin
|
||||||
|
if FSorted and not FChanging then
|
||||||
|
begin
|
||||||
|
inherited Insert(Index, S);
|
||||||
|
Sort;
|
||||||
|
end else
|
||||||
|
inherited Insert(Index, S);
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TQtComboStrings.Sort;
|
procedure TQtComboStrings.Sort;
|
||||||
var
|
var
|
||||||
I: Integer;
|
I: Integer;
|
||||||
|
Loading…
Reference in New Issue
Block a user