source: tags/1.5.0/Forms/UFormImport.pas

Last change on this file was 184, checked in by chronos, 6 years ago
  • Added: New menu action Tools - Document check which shows a form for checking acronyms in text documents.
  • Added: Remember dimensions of Import, Export and Settings forms.
File size: 4.9 KB
Line 
1unit UFormImport;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 UAcronym;
10
11type
12
13 { TFormImport }
14
15 TFormImport = class(TForm)
16 ButtonLoadFromFile: TButton;
17 ButtonProcess: TButton;
18 ComboBoxDataFormat: TComboBox;
19 Label1: TLabel;
20 Memo1: TMemo;
21 OpenDialog1: TOpenDialog;
22 procedure ButtonProcessClick(Sender: TObject);
23 procedure ButtonLoadFromFileClick(Sender: TObject);
24 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
25 procedure FormCreate(Sender: TObject);
26 procedure FormShow(Sender: TObject);
27 private
28 procedure ImportMediaWiki;
29 procedure ImportCSV;
30 public
31 { public declarations }
32 end;
33
34var
35 FormImport: TFormImport;
36
37implementation
38
39{$R *.lfm}
40
41uses
42 UCore;
43
44resourcestring
45 SImportedNewAcronyms = 'Imported %d new acronyms.';
46
47
48{ TFormImport }
49
50procedure TFormImport.ButtonProcessClick(Sender: TObject);
51begin
52 if ComboBoxDataFormat.ItemIndex = 0 then ImportCSV;
53 if ComboBoxDataFormat.ItemIndex = 1 then ImportMediaWiki;
54end;
55
56procedure TFormImport.ButtonLoadFromFileClick(Sender: TObject);
57begin
58 if ComboBoxDataFormat.ItemIndex = 0 then OpenDialog1.DefaultExt := '.csv';
59 if ComboBoxDataFormat.ItemIndex = 1 then OpenDialog1.DefaultExt := '.txt';
60 if OpenDialog1.Execute then
61 Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
62end;
63
64procedure TFormImport.FormClose(Sender: TObject; var CloseAction: TCloseAction);
65begin
66 Core.PersistentForm1.Save(Self);
67end;
68
69procedure TFormImport.FormCreate(Sender: TObject);
70begin
71 Core.CoolTranslator1.TranslateComponentRecursive(Self);
72 Core.ThemeManager.UseTheme(Self);
73end;
74
75procedure TFormImport.FormShow(Sender: TObject);
76begin
77 Core.PersistentForm1.Load(Self);
78end;
79
80procedure TFormImport.ImportMediaWiki;
81var
82 I: Integer;
83 Line: string;
84 AcronymName: string;
85 AcronymMeaning: string;
86 AddedCount: Integer;
87 Acronym: TAcronym;
88 Meaning: TAcronymMeaning;
89begin
90 AddedCount := 0;
91 for I := 0 to Memo1.Lines.Count - 1 do begin
92 Line := Trim(Memo1.Lines[I]);
93 if Copy(Line, 1, 1) = ';' then begin
94 if Pos(':', Line) > 0 then begin
95 AcronymName := Trim(Copy(Line, 2, Pos(':', Line) - 2));
96 Line := Copy(Line, Pos(':', Line), Length(Line));
97 end else AcronymName := Trim(Copy(Line, 2, Length(Line)));
98 end;
99 if Copy(Line, 1, 1) = ':' then begin
100 AcronymMeaning := Trim(Copy(Line, 2, Length(Line)));
101 if (AcronymName <> '') and (AcronymMeaning <> '') then begin
102 Acronym := Core.AcronymDb.Acronyms.SearchByName(AcronymName);
103 if not Assigned(Acronym) then begin
104 Acronym := TAcronym.Create;
105 Acronym.Name := AcronymName;
106 Core.AcronymDb.Acronyms.Add(Acronym);
107 end;
108 Meaning := Acronym.Meanings.SearchByName(AcronymMeaning);
109 if not Assigned(Meaning) then begin
110 Meaning := TAcronymMeaning.Create;
111 Meaning.Name := AcronymMeaning;
112 Meaning.Acronym := Acronym;
113 Acronym.Meanings.Add(Meaning);
114 Inc(AddedCount)
115 end;
116 end;
117 end;
118 end;
119 if AddedCount > 0 then Core.AcronymDb.Modified := True;
120 ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
121end;
122
123procedure TFormImport.ImportCSV;
124var
125 I: Integer;
126 Line: string;
127 Columns: TStringList;
128 Acronym: TAcronym;
129 Meaning: TAcronymMeaning;
130 AcronymName: string;
131 AcronymMeaning: string;
132 AcronymDescription: string;
133 AddedCount: Integer;
134begin
135 AddedCount := 0;
136 Columns := TStringList.Create;
137 Columns.StrictDelimiter := True;
138 for I := 0 to Memo1.Lines.Count - 1 do begin
139 Line := Trim(Memo1.Lines[I]);
140 Columns.DelimitedText := Line;
141 if Columns.Count > 0 then AcronymName := Trim(Columns[0])
142 else AcronymName := '';
143 if Columns.Count > 1 then AcronymMeaning := Trim(Columns[1])
144 else AcronymMeaning := '';
145 if Columns.Count > 2 then AcronymDescription := Trim(Columns[2])
146 else AcronymDescription := '';
147 if (AcronymName <> '') and (AcronymDescription <> '') then begin
148 Acronym := Core.AcronymDb.Acronyms.SearchByName(AcronymName);
149 if not Assigned(Acronym) then begin
150 Acronym := TAcronym.Create;
151 Acronym.Name := AcronymName;
152 Core.AcronymDb.Acronyms.Add(Acronym);
153 end;
154 Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
155 if not Assigned(Meaning) then begin
156 Meaning := TAcronymMeaning.Create;
157 Meaning.Name := AcronymMeaning;
158 Meaning.Description := AcronymDescription;
159 Meaning.Acronym := Acronym;
160 Acronym.Meanings.Add(Meaning);
161 Inc(AddedCount)
162 end;
163 end;
164 end;
165 Columns.Free;
166 if AddedCount > 0 then Core.AcronymDb.Modified := True;
167 ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
168end;
169
170end.
171
Note: See TracBrowser for help on using the repository browser.