Ignore:
Timestamp:
May 4, 2016, 12:28:37 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Ability to search acronyms by exact match and by columns in main window.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r17 r18  
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
    99  ComCtrls, StdCtrls, ExtCtrls, ActnList, UAcronym, UPersistentForm, Contnrs,
    10   URegistry, ULastOpenedList, UCoolTranslator, Registry;
     10  URegistry, ULastOpenedList, UListViewSort, UCoolTranslator, Registry,
     11  SpecializedList, LazUTF8;
    1112
    1213type
     
    3132    AExit: TAction;
    3233    ActionList1: TActionList;
     34    CheckBoxExactMath: TCheckBox;
    3335    CoolTranslator1: TCoolTranslator;
    34     EditSearch: TEdit;
    3536    LastOpenedList1: TLastOpenedList;
    3637    ListViewAcronyms: TListView;
     38    ListViewFilter1: TListViewFilter;
     39    ListViewSort1: TListViewSort;
    3740    MainMenu1: TMainMenu;
    3841    MenuItem1: TMenuItem;
     
    8083    procedure AShowCategoriesExecute(Sender: TObject);
    8184    procedure AShowExecute(Sender: TObject);
     85    procedure CheckBoxExactMathChange(Sender: TObject);
    8286    procedure EditSearchChange(Sender: TObject);
    8387    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
     
    9296    procedure ListViewAcronymsSelectItem(Sender: TObject; Item: TListItem;
    9397      Selected: Boolean);
     98    procedure ListViewFilter1Change(Sender: TObject);
     99    function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
     100    procedure ListViewSort1Filter(ListViewSort: TListViewSort);
    94101    procedure TrayIcon1Click(Sender: TObject);
    95102  private
     
    98105    procedure SetAlwaysOnTop(AValue: Boolean);
    99106  private
    100     FoundAcronyms: TAcronymMeanings;
    101107    RegistryContext: TRegistryContext;
    102108    ProjectClosed: Boolean;
     109    procedure FilterList(List: TListObject);
    103110    procedure OpenRecentClick(Sender: TObject);
    104111    procedure UpdateAcronymsList;
     
    140147begin
    141148  AcronymDb := TAcronymDb.Create;
    142   FoundAcronyms := TAcronymMeanings.Create(False);
    143149end;
    144150
     
    355361end;
    356362
     363procedure TFormMain.CheckBoxExactMathChange(Sender: TObject);
     364begin
     365  UpdateAcronymsList;
     366end;
     367
    357368procedure TFormMain.FormDestroy(Sender: TObject);
    358369begin
    359   FreeAndNil(FoundAcronyms);
    360370  FreeAndNil(AcronymDb);
    361371end;
     
    369379    AcronymDB.LoadFromFile(LastOpenedList1.Items[0]);
    370380  UpdateAcronymsList;
     381  ListViewFilter1.UpdateFromListView(ListViewAcronyms);
    371382  UpdateInterface;
    372383end;
     
    378389
    379390procedure TFormMain.ListViewAcronymsData(Sender: TObject; Item: TListItem);
    380 var
    381   I: Integer;
    382   ContextCombined: string;
    383 begin
    384   if Item.Index < FoundAcronyms.Count then
    385   with TAcronymMeaning(FoundAcronyms[Item.Index]) do begin
     391begin
     392  if Item.Index < ListViewSort1.List.Count then
     393  with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
    386394    Item.Caption := Acronym.Name;
    387395    Item.SubItems.Add(Name);
    388     Item.Data := TAcronymMeaning(FoundAcronyms[Item.Index]);
    389     ContextCombined := '';
    390     for I := 0 to Categories.Count - 1 do
    391       ContextCombined := ContextCombined + TAcronymCategory(Categories[I]).Name + ',';
    392     Delete(ContextCombined, Length(ContextCombined), 1);
    393     Item.SubItems.Add(ContextCombined);
     396    Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
     397    Item.SubItems.Add(Categories.GetString);
    394398  end;
    395399end;
     
    408412begin
    409413  UpdateInterface;
     414end;
     415
     416procedure TFormMain.ListViewFilter1Change(Sender: TObject);
     417begin
     418  UpdateAcronymsList;
     419end;
     420
     421function TFormMain.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
     422begin
     423  Result := 0;
     424  if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
     425    with ListViewSort1 do
     426    case Column of
     427      0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
     428      1: Result := CompareString(TAcronymMeaning(Item1).Description, TAcronymMeaning(Item2).Description);
     429      2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
     430    end;
     431    if ListViewSort1.Order = soDown then Result := -Result;
     432  end else Result := 0;
     433end;
     434
     435procedure TFormMain.ListViewSort1Filter(ListViewSort: TListViewSort);
     436begin
     437  AcronymDb.Acronyms.Sort(AcronymComparer);
     438  AcronymDb.AssignToList(ListViewSort1.List);
     439  FilterList(ListViewSort1.List);
    410440end;
    411441
     
    436466end;
    437467
     468procedure TFormMain.FilterList(List: TListObject);
     469var
     470  I: Integer;
     471  FoundCount: Integer;
     472  EnteredCount: Integer;
     473begin
     474  EnteredCount := ListViewFilter1.TextEnteredCount;
     475  for I := List.Count - 1 downto 0 do begin
     476    if List.Items[I] is TAcronymMeaning then begin
     477      with TAcronymMeaning(List.Items[I]) do begin
     478         with ListViewFilter1 do
     479         if Visible and (EnteredCount > 0) then begin
     480           FoundCount := 0;
     481           if CheckBoxExactMath.Checked then begin
     482             if TextEnteredColumn(0) and (StringGrid.Cells[0, 0] =
     483               TAcronymMeaning(List.Items[I]).Acronym.Name) then Inc(FoundCount);
     484             if TextEnteredColumn(1) and (StringGrid.Cells[1, 0] =
     485               TAcronymMeaning(List.Items[I]).Name) then Inc(FoundCount);
     486             if TextEnteredColumn(2) and (StringGrid.Cells[2, 0] =
     487               TAcronymMeaning(List.Items[I]).Categories.GetString) then Inc(FoundCount);
     488           end else begin
     489             if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
     490               UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
     491             if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
     492               UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
     493             if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
     494               UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
     495           end;
     496           if FoundCount <> EnteredCount then List.Delete(I);
     497         end else List.Delete(I);
     498      end;
     499    end else
     500    if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
     501      List.Delete(I);
     502    end;
     503  end;
     504end;
     505
    438506procedure TFormMain.OpenRecentClick(Sender: TObject);
    439507begin
     
    451519begin
    452520  if Assigned(AcronymDb) then begin
    453     AcronymDb.Acronyms.Sort(AcronymComparer);
    454     AcronymDb.FilterList(EditSearch.Text, FoundAcronyms);
    455   end else FoundAcronyms.Clear;
    456   ListViewAcronyms.Items.Count := FoundAcronyms.Count;
    457   ListViewAcronyms.Refresh;
     521    ListViewSort1.Refresh;
     522  end else ListViewSort1.List.Clear;
    458523end;
    459524
     
    470535  AFileSave.Enabled := Assigned(AcronymDb) and AcronymDb.Modified;
    471536  AFileSaveAs.Enabled := Assigned(AcronymDb);
    472   EditSearch.Enabled := Assigned(AcronymDb);
    473537  AImport.Enabled := Assigned(AcronymDb);
    474538  AShowCategories.Enabled := Assigned(AcronymDb);
     
    494558      else CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode('');
    495559    AlwaysOnTop := ReadBoolWithDefault('AlwaysOnTop', False);
     560    CheckBoxExactMath.Checked := ReadBoolWithDefault('ExactMatch', False);
    496561  finally
    497562    Free;
     
    511576      else DeleteValue('LanguageCode');
    512577    WriteBool('AlwaysOnTop', AlwaysOnTop);
     578    WriteBool('ExactMatch', CheckBoxExactMath.Checked);
    513579  finally
    514580    Free;
Note: See TracChangeset for help on using the changeset viewer.