Ignore:
Timestamp:
Nov 25, 2021, 11:32:55 AM (3 years ago)
Author:
chronos
Message:
  • Added: Bottom column filter bar in contacts list.
  • Added: Allow to sort columns in contacts list.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormContacts.pas

    r31 r32  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   ComCtrls, Menus, ActnList, UContact;
     9  ComCtrls, Menus, ActnList, UContact, UListViewSort, fgl, LazUTF8;
    1010
    1111type
     
    2020    ActionList1: TActionList;
    2121    ListView1: TListView;
     22    ListViewFilter1: TListViewFilter;
     23    ListViewSort1: TListViewSort;
    2224    MenuItem1: TMenuItem;
    2325    MenuItem2: TMenuItem;
     
    4042    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
    4143      Selected: Boolean);
     44    procedure ListViewFilter1Change(Sender: TObject);
     45    procedure ListViewSort1ColumnWidthChanged(Sender: TObject);
     46    function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
     47    procedure ListViewSort1Filter(ListViewSort: TListViewSort);
    4248  private
    4349    FContacts: TContacts;
     50    procedure FilterList(List: TFPGObjectList<TObject>);
    4451    procedure SetContacts(AValue: TContacts);
    4552  public
     
    6875procedure TFormContacts.ListView1Data(Sender: TObject; Item: TListItem);
    6976begin
    70   if Assigned(Contacts) and (Item.Index < Contacts.Count) then
    71   with TContact(Contacts[Item.Index]) do begin
     77  if Item.Index < ListViewSort1.List.Count then
     78  with TContact(ListViewSort1.List[Item.Index]) do begin
    7279    Item.Caption := Fields[cfFullName];
    7380    Item.SubItems.Add(Fields[cfFirstName]);
     
    9198end;
    9299
     100procedure TFormContacts.ListViewFilter1Change(Sender: TObject);
     101begin
     102  ReloadList;
     103end;
     104
     105procedure TFormContacts.ListViewSort1ColumnWidthChanged(Sender: TObject);
     106begin
     107  ListViewFilter1.UpdateFromListView(ListView1);
     108end;
     109
     110function TFormContacts.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
     111begin
     112  Result := 0;
     113  if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
     114    with ListViewSort1 do
     115    case Column of
     116      0: Result := CompareString(TContact(Item1).Fields[cfFullName], TContact(Item2).Fields[cfFullName]);
     117      1: Result := CompareString(TContact(Item1).Fields[cfFirstName], TContact(Item2).Fields[cfFirstName]);
     118      2: Result := CompareString(TContact(Item1).Fields[cfMiddleName], TContact(Item2).Fields[cfMiddleName]);
     119      3: Result := CompareString(TContact(Item1).Fields[cfLastName], TContact(Item2).Fields[cfLastName]);
     120      4: Result := CompareString(TContact(Item1).Fields[cfTelCell], TContact(Item2).Fields[cfTelCell]);
     121      5: Result := CompareString(TContact(Item1).Fields[cfTelHome], TContact(Item2).Fields[cfTelHome]);
     122    end;
     123    if ListViewSort1.Order = soDown then Result := -Result;
     124  end else Result := 0;
     125end;
     126
     127procedure TFormContacts.ListViewSort1Filter(ListViewSort: TListViewSort);
     128begin
     129  if Assigned(Contacts) then Contacts.AssignToList(ListViewSort1.List)
     130    else ListViewSort1.List.Clear;
     131  FilterList(ListViewSort1.List);
     132end;
     133
     134procedure TFormContacts.FilterList(List: TFPGObjectList<TObject>);
     135var
     136  I: Integer;
     137  FoundCount: Integer;
     138  EnteredCount: Integer;
     139begin
     140  EnteredCount := ListViewFilter1.TextEnteredCount;
     141  for I := List.Count - 1 downto 0 do begin
     142    if List.Items[I] is TContact then begin
     143      with TContact(List.Items[I]) do begin
     144         with ListViewFilter1 do
     145         if Visible and (EnteredCount > 0) then begin
     146           FoundCount := 0;
     147           if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
     148             UTF8LowerCase(TContact(List.Items[I]).Fields[cfFullName])) > 0 then Inc(FoundCount);
     149           if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
     150             UTF8LowerCase(TContact(List.Items[I]).Fields[cfFirstName])) > 0 then Inc(FoundCount);
     151           if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
     152             UTF8LowerCase(TContact(List.Items[I]).Fields[cfMiddleName])) > 0 then Inc(FoundCount);
     153           if Pos(UTF8LowerCase(StringGrid.Cells[3, 0]),
     154             UTF8LowerCase(TContact(List.Items[I]).Fields[cfLastName])) > 0 then Inc(FoundCount);
     155           if Pos(UTF8LowerCase(StringGrid.Cells[4, 0]),
     156             UTF8LowerCase(TContact(List.Items[I]).Fields[cfTelCell])) > 0 then Inc(FoundCount);
     157           if Pos(UTF8LowerCase(StringGrid.Cells[5, 0]),
     158             UTF8LowerCase(TContact(List.Items[I]).Fields[cfTelHome])) > 0 then Inc(FoundCount);
     159           if FoundCount <> EnteredCount then List.Delete(I);
     160         end;
     161      end;
     162    end else
     163    if TContact(List.Items[I]) is TContact then begin
     164      List.Delete(I);
     165    end;
     166  end;
     167end;
     168
    93169procedure TFormContacts.SetContacts(AValue: TContacts);
    94170begin
     
    106182  ReloadList;
    107183  UpdateInterface;
     184  ListViewFilter1.UpdateFromListView(ListView1);
    108185end;
    109186
     
    182259procedure TFormContacts.ReloadList;
    183260begin
    184   if Assigned(Contacts) then
    185     ListView1.Items.Count := Contacts.Count
    186     else ListView1.Items.Count := 0;
    187   ListView1.Refresh;
     261  ListViewSort1.Refresh;
    188262end;
    189263
Note: See TracChangeset for help on using the changeset viewer.