Changeset 8


Ignore:
Timestamp:
Apr 21, 2016, 1:12:45 PM (9 years ago)
Author:
chronos
Message:
  • Added: Also allow to edit acronym detailed description.
  • Added: Import from CSV file.
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/UAcronym.pas

    r6 r8  
    3030  TAcronymMeaning = class
    3131    Name: string;
     32    Description: string;
    3233    Language: string;
    3334    Acronym: TAcronym;
     
    7172    Name: string;
    7273    Meaning: string;
     74    Description: string;
    7375  end;
    7476
     
    8688    procedure SaveToFile(FileName: string);
    8789    procedure FilterList(AName: string; Items: TAcronymMeanings);
    88     procedure AddAcronym(AcronymName, MeaningName: string);
     90    function AddAcronym(AcronymName, MeaningName: string): TAcronymMeaning;
    8991    procedure RemoveAcronym(AcronymName, MeaningName: string);
    9092  end;
     
    324326end;
    325327
    326 procedure TAcronymDb.AddAcronym(AcronymName, MeaningName: string);
     328function TAcronymDb.AddAcronym(AcronymName, MeaningName: string): TAcronymMeaning;
    327329var
    328330  Acronym: TAcronym;
     
    342344    Acronym.Meanings.Add(Meaning);
    343345  end;
     346  Result := Meaning;
    344347  Modified := True;
    345348end;
  • trunk/UFormAcronym.lfm

    r7 r8  
    11object FormAcronym: TFormAcronym
    2   Left = 493
     2  Left = 554
    33  Height = 296
    44  Top = 409
     
    77  ClientHeight = 296
    88  ClientWidth = 420
     9  OnClose = FormClose
    910  OnKeyUp = FormKeyUp
    10   Position = poScreenCenter
     11  OnShow = FormShow
    1112  LCLVersion = '1.6.0.4'
    1213  object Label1: TLabel
     
    3031    Left = 8
    3132    Height = 20
    32     Top = 57
     33    Top = 49
    3334    Width = 61
    3435    Caption = 'Meaning:'
     
    3839    Left = 136
    3940    Height = 28
    40     Top = 56
     41    Top = 49
    4142    Width = 272
    4243    Anchors = [akTop, akLeft, akRight]
     
    6667    TabOrder = 3
    6768  end
     69  object Label3: TLabel
     70    Left = 8
     71    Height = 20
     72    Top = 88
     73    Width = 79
     74    Caption = 'Description:'
     75    ParentColor = False
     76  end
     77  object MemoDescription: TMemo
     78    Left = 136
     79    Height = 114
     80    Top = 88
     81    Width = 272
     82    Anchors = [akTop, akLeft, akRight]
     83    ScrollBars = ssAutoBoth
     84    TabOrder = 4
     85  end
    6886end
  • trunk/UFormAcronym.pas

    r5 r8  
    2020    Label1: TLabel;
    2121    Label2: TLabel;
     22    Label3: TLabel;
     23    MemoDescription: TMemo;
     24    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    2225    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
     26    procedure FormShow(Sender: TObject);
    2327  private
    2428    { private declarations }
     
    3539{$R *.lfm}
    3640
     41uses
     42  UFormMain;
     43
    3744{ TFormAcronym }
    3845
     
    4451end;
    4552
     53procedure TFormAcronym.FormClose(Sender: TObject; var CloseAction: TCloseAction
     54  );
     55begin
     56  FormMain.PersistentForm1.Save(Self);
     57end;
     58
     59procedure TFormAcronym.FormShow(Sender: TObject);
     60begin
     61  FormMain.PersistentForm1.Load(Self);
     62end;
     63
    4664procedure TFormAcronym.Load(Entry: TAcronymEntry);
    4765begin
    4866  EditAcronym.Text := Entry.Name;
    4967  EditMeaning.Text := Entry.Meaning;
     68  MemoDescription.Text := Entry.Description;
    5069end;
    5170
     
    5473  Entry.Name := EditAcronym.Text;
    5574  Entry.Meaning := EditMeaning.Text;
     75  Entry.Description := MemoDescription.Text;
    5676end;
    5777
  • trunk/UFormImport.lfm

    r7 r8  
    2727    TabOrder = 1
    2828  end
     29  object ComboBoxDataFormat: TComboBox
     30    Left = 256
     31    Height = 28
     32    Top = 5
     33    Width = 140
     34    ItemHeight = 20
     35    ItemIndex = 0
     36    Items.Strings = (
     37      'CSV'
     38      'MediaWiki'
     39    )
     40    Style = csDropDownList
     41    TabOrder = 2
     42    Text = 'CSV'
     43  end
     44  object Label1: TLabel
     45    Left = 153
     46    Height = 20
     47    Top = 9
     48    Width = 84
     49    Caption = 'Data format:'
     50    ParentColor = False
     51  end
     52  object ButtonLoadFromFile: TButton
     53    Left = 504
     54    Height = 25
     55    Top = 8
     56    Width = 139
     57    Anchors = [akTop, akRight]
     58    Caption = 'Load from file'
     59    OnClick = ButtonLoadFromFileClick
     60    TabOrder = 3
     61  end
     62  object OpenDialog1: TOpenDialog
     63    left = 331
     64    top = 187
     65  end
    2966end
  • trunk/UFormImport.pas

    r6 r8  
    1414
    1515  TFormImport = class(TForm)
     16    ButtonLoadFromFile: TButton;
    1617    ButtonImport: TButton;
     18    ComboBoxDataFormat: TComboBox;
     19    Label1: TLabel;
    1720    Memo1: TMemo;
     21    OpenDialog1: TOpenDialog;
    1822    procedure ButtonImportClick(Sender: TObject);
     23    procedure ButtonLoadFromFileClick(Sender: TObject);
    1924  private
    20     { private declarations }
     25    procedure ImportMediaWiki;
     26    procedure ImportCSV;
    2127  public
    2228    { public declarations }
     
    5561
    5662procedure TFormImport.ButtonImportClick(Sender: TObject);
     63begin
     64  if ComboBoxDataFormat.ItemIndex = 0 then ImportCSV;
     65  if ComboBoxDataFormat.ItemIndex = 1 then ImportMediaWiki;
     66end;
     67
     68procedure TFormImport.ButtonLoadFromFileClick(Sender: TObject);
     69begin
     70  if OpenDialog1.Execute then
     71    Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
     72end;
     73
     74procedure TFormImport.ImportMediaWiki;
    5775var
    5876  I: Integer;
    5977  Line: string;
    6078  AcronymName: string;
    61   AcronymDescription: string;
     79  AcronymMeaning: string;
    6280  AddedCount: Integer;
    6381  Acronym: TAcronym;
     
    7492    end;
    7593    if Copy(Line, 1, 1) = ':' then begin
    76       AcronymDescription := Trim(Copy(Line, 2, Length(Line)));
    77       if AcronymName <> '' then begin
     94      AcronymMeaning := Trim(Copy(Line, 2, Length(Line)));
     95      if (AcronymName <> '') and (AcronymMeaning <> '') then begin
    7896        Acronym := FormMain.AcronymDb.Acronyms.SearchByName(AcronymName);
    7997        if not Assigned(Acronym) then begin
     
    82100          FormMain.AcronymDb.Acronyms.Add(Acronym);
    83101        end;
    84         Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
     102        Meaning := Acronym.Meanings.SearchByName(AcronymMeaning);
    85103        if not Assigned(Meaning) then begin
    86104          Meaning := TAcronymMeaning.Create;
    87           Meaning.Name := AcronymDescription;
     105          Meaning.Name := AcronymMeaning;
    88106          Meaning.Acronym := Acronym;
    89107          Acronym.Meanings.Add(Meaning);
     
    97115end;
    98116
     117procedure TFormImport.ImportCSV;
     118var
     119  I: Integer;
     120  Line: string;
     121  Columns: TStringList;
     122  Acronym: TAcronym;
     123  Meaning: TAcronymMeaning;
     124  AcronymName: string;
     125  AcronymMeaning: string;
     126  AcronymDescription: string;
     127  AddedCount: Integer;
     128begin
     129  AddedCount := 0;
     130  Columns := TStringList.Create;
     131  Columns.StrictDelimiter := True;
     132  for I := 0 to Memo1.Lines.Count - 1 do begin
     133    Line := Trim(Memo1.Lines[I]);
     134    Columns.DelimitedText := Line;
     135    if Columns.Count > 0 then AcronymName := Trim(Columns[0])
     136      else AcronymName := '';
     137    if Columns.Count > 1 then AcronymMeaning := Trim(Columns[1])
     138      else AcronymMeaning := '';
     139    if Columns.Count > 2 then AcronymDescription := Trim(Columns[2])
     140      else AcronymDescription := '';
     141    if (AcronymName <> '') and (AcronymDescription <> '') then begin
     142      Acronym := FormMain.AcronymDb.Acronyms.SearchByName(AcronymName);
     143      if not Assigned(Acronym) then begin
     144        Acronym := TAcronym.Create;
     145        Acronym.Name := AcronymName;
     146        FormMain.AcronymDb.Acronyms.Add(Acronym);
     147      end;
     148      Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
     149      if not Assigned(Meaning) then begin
     150        Meaning := TAcronymMeaning.Create;
     151        Meaning.Name := AcronymMeaning;
     152        Meaning.Description := AcronymDescription;
     153        Meaning.Acronym := Acronym;
     154        Acronym.Meanings.Add(Meaning);
     155        Inc(AddedCount)
     156      end;
     157    end;
     158  end;
     159  Columns.Free;
     160  if AddedCount > 0 then FormMain.AcronymDb.Modified := True;
     161  ShowMessage(Format('Imported %d new acronyms.', [AddedCount]));
     162end;
     163
    99164end.
    100165
  • trunk/UFormMain.pas

    r7 r8  
    136136    'Acronyms were modified. Do you want to save them to file before exit?',
    137137      mtConfirmation, [mbYes, mbNo, mbCancel], 0);
    138     if ModalResult = mrYes then AFileSave.Execute
    139     else if ModalResult = mrNo then CanClose := True
    140     else CanClose := False;
     138    if ModalResult = mrYes then begin
     139      AFileSave.Execute;
     140      CanClose := True;
     141    end
     142    else if ModalResult = mrNo then begin
     143      CanClose := True;
     144      AcronymDb.Modified := False;
     145    end else CanClose := False;
    141146  end;
    142147end;
     
    202207var
    203208  TempEntry: TAcronymEntry;
     209  Meaning: TAcronymMeaning;
    204210begin
    205211  if Assigned(ListViewAcronyms.Selected) then
     
    208214    TempEntry.Name := Acronym.Name;
    209215    TempEntry.Meaning := Name;
     216    TempEntry.Description := Description;
    210217    FormAcronym.Load(TempEntry);
    211218    if FormAcronym.ShowModal = mrOk then begin
    212219      FormAcronym.Save(TempEntry);
    213220      if (TempEntry.Name <> Acronym.Name) or
    214       (TempEntry.Meaning <> Name) then begin
     221      (TempEntry.Meaning <> Name) or
     222      (TempEntry.Description <> Description) then begin
    215223        // TODO: Update item inplace if possible
    216224        AcronymDb.RemoveAcronym(Acronym.Name, Name);
    217         AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
     225        Meaning := AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
     226        Meaning.Description := TempEntry.Description;
    218227        UpdateAcronymsList;
    219228        UpdateInterface;
     
    250259var
    251260  TempEntry: TAcronymEntry;
     261  Meaning: TAcronymMeaning;
    252262begin
    253263  TempEntry := TAcronymEntry.Create;
    254264  TempEntry.Name := '';
    255265  TempEntry.Meaning := '';
     266  TempEntry.Description := '';
    256267  FormAcronym.Load(TempEntry);
    257268  if FormAcronym.ShowModal = mrOk then begin
    258269    FormAcronym.Save(TempEntry);
    259     AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
     270    Meaning := AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
     271    Meaning.Description := TempEntry.Description;
    260272    UpdateAcronymsList;
    261273    UpdateInterface;
     
    267279  FormImport.ShowModal;
    268280  UpdateAcronymsList;
     281  UpdateInterface;
    269282end;
    270283
Note: See TracChangeset for help on using the changeset viewer.