-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainApplication.pas
332 lines (292 loc) · 8.08 KB
/
MainApplication.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
unit MainApplication;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, TOperativeUnit, ComCtrls, StdCtrls, DateUtils, FormAdd, FormEdit,
System.RegularExpressions, Vcl.ExtCtrls, TWatermarkEditUnit;
procedure Append(item: TElemType);
stdcall external 'LinkedList.dll' name 'Append';
procedure WriteEach;
stdcall external 'LinkedList.dll' name 'WriteEach';
procedure Seed;
stdcall external 'LinkedList.dll' name 'Seed';
procedure WriteToFile;
stdcall external 'LinkedList.dll' name 'WriteToFile';
procedure ReadFromFile;
stdcall external 'LinkedList.dll' name 'ReadFromFile';
function GetHead: PElem;
stdcall external 'LinkedList.dll' name 'GetHead';
function EqualTOperatives: PElem;
stdcall external 'LinkedList.dll' name 'EqualTOperatives';
procedure Remove(op: TOperative);
stdcall external 'LinkedList.dll' name 'Remove';
procedure UpdateOperative(a, b: TOperative); stdcall;
external 'LinkedList.dll' name 'UpdateOperative';
type
TMainForm = class(TForm)
lvOperatives: TListView;
btnDelete: TButton;
btnAddOperative: TButton;
btnEdit: TButton;
btnSave: TButton;
Label1: TLabel;
Label2: TLabel;
tbSearchNickName: TWatermarkEdit;
tbSearchLastName: TWatermarkEdit;
procedure FormCreate(Sender: TObject);
procedure btnAddOperativeClick(Sender: TObject);
procedure btnDeleteClick(Sender: TObject);
procedure lvOperativesColumnClick(Sender: TObject; Column: TListColumn);
procedure lvOperativesCompare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
procedure lvOperativesSelectItem(Sender: TObject; item: TListItem;
Selected: Boolean);
procedure btnEditClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure tbSearchNickNameChanged(Sender: TObject);
procedure tbSearchLastNameChanged(Sender: TObject);
private
SortDescending: Bool;
SortColumn: Integer;
PendingChanges: Bool;
DefaultImagePath : String;
procedure FillListBox;
procedure FilterListBox;
function OperativeFromSelected: TOperative;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
dialogResult: Integer;
begin
if PendingChanges = True then
begin
dialogResult := MessageDlg('Zapisać zmiany?', mtConfirmation,
mbYesNoCancel, 0);
if dialogResult = mrYes then
begin
WriteToFile;
CanClose := True;
end;
if dialogResult = mrNo then
begin
CanClose := True;
end;
if dialogResult = mrCancel then
begin
CanClose := False;
end;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
DefaultImagePath := GetCurrentDir + '\\default.bmp';
ReadFromFile;
PendingChanges := False;
tbSearchNickName.Watermark := 'Pseudonim';
tbSearchNickName.Clear;
tbSearchLastName.Watermark := 'Nazwisko';
tbSearchLastName.Clear;
FillListBox;
tbSearchNickName.CallFocusLost;
tbSearchLastName.CallFocusLost;
end;
procedure TMainForm.lvOperativesColumnClick(Sender: TObject;
Column: TListColumn);
begin
TListView(Sender).SortType := stData;
if Column.Index <> SortColumn then
begin
SortColumn := Column.Index;
SortDescending := False;
end
else
begin
SortDescending := not SortDescending;
TListView(Sender).SortType := stData;
end;
lvOperatives.AlphaSort;
end;
procedure TMainForm.lvOperativesCompare(Sender: TObject;
Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
begin
if SortColumn = 0 then
begin
Compare := CompareText(Item1.Caption, Item2.Caption)
end
else if SortColumn = 3 then
begin
Compare := CompareDate(StrToDate(Item1.SubItems[2]),
StrToDate(Item2.SubItems[2]));
end
else
begin
Compare := CompareText(Item1.SubItems[SortColumn - 1],
Item2.SubItems[SortColumn - 1]);
end;
if SortDescending then
Compare := -Compare;
end;
procedure TMainForm.lvOperativesSelectItem(Sender: TObject; item: TListItem;
Selected: Boolean);
var
op : TOperative;
begin
btnEdit.Enabled := Selected;
btnDelete.Enabled := Selected;
end;
procedure TMainForm.FillListBox;
var
iter: PElem;
item: TListItem;
begin
FilterListBox;
end;
procedure TMainForm.FilterListBox;
var
lastName, nickName: string;
iter: PElem;
item: TListItem;
regexp1, regexp2: TRegEx;
begin
lastName := tbSearchLastName.GetValue;
nickName := tbSearchNickName.GetValue;
if lastName.Length or nickName.Length > 0 then
begin
lvOperatives.Items.Clear;
iter := GetHead;
if lastName = '' then
lastName := '.*';
if nickName = '' then
nickName := '.*';
regexp1.Create(lastName, [roIgnoreCase]);
regexp2.Create(nickName, [roIgnoreCase]);
while iter <> nil do
begin
if regexp1.IsMatch(iter^.Val.LastName) and
regexp2.IsMatch(iter^.Val.NickName) then
begin
item := lvOperatives.Items.Add;
item.Caption := iter^.Val.FirstName;
item.SubItems.Add(iter^.Val.LastName);
item.SubItems.Add(iter^.Val.NickName);
item.SubItems.Add(DateToStr(iter^.Val.DateOfBirth));
item.SubItems.Add(iter^.Val.BirthPlace);
end;
iter := iter^.Next;
end;
end
else
begin
lvOperatives.Clear;
iter := GetHead;
while iter <> nil do
begin
item := lvOperatives.Items.Add;
item.Caption := iter^.Val.FirstName;
item.SubItems.Add(iter^.Val.LastName);
item.SubItems.Add(iter^.Val.NickName);
item.SubItems.Add(DateToStr(iter^.Val.DateOfBirth));
item.SubItems.Add(iter^.Val.BirthPlace);
iter := iter^.Next;
end;
end;
end;
procedure TMainForm.btnAddOperativeClick(Sender: TObject);
var
row: TOperative;
begin
FormAdd.Form1.ShowModal;
FillListBox;
if FormAdd.Form1.HasAdded then
begin
PendingChanges := True;
end;
tbSearchLastName.CallFocusLost;
end;
function TMainForm.OperativeFromSelected: TOperative;
var
op: TOperative;
i: Integer;
begin
for i := 0 to lvOperatives.Items.Count - 1 do
begin
if lvOperatives.Items[i].Selected then // DAFUQ
begin
op.FirstName := lvOperatives.Items[i].Caption;
op.LastName := lvOperatives.Items[i].SubItems[0];
op.NickName := lvOperatives.Items[i].SubItems[1];
op.DateOfBirth := StrToDate(lvOperatives.Items[i].SubItems[2]);
op.BirthPlace := lvOperatives.Items[i].SubItems[3];
Result := op;
Exit;
end;
end;
end;
procedure TMainForm.tbSearchLastNameChanged(Sender: TObject);
begin
FilterListBox();
//tbSearchNickName.CallFocusLost;
end;
procedure TMainForm.tbSearchNickNameChanged(Sender: TObject);
begin
FilterListBox();
//tbSearchLastName.CallFocusLost;
end;
procedure TMainForm.btnDeleteClick(Sender: TObject);
var
op: TOperative;
dlgResult: Integer;
begin
if lvOperatives.Selected <> nil then
begin
dlgResult := MessageDlg('Na pewno usunąć?', mtConfirmation, mbYesNo, 0);
if dlgResult = mrYes then
begin
op := OperativeFromSelected;
Remove(op);
FillListBox;
PendingChanges := True;
end;
end;
btnDelete.Enabled := False;
btnEdit.Enabled := False;
tbSearchLastName.CallFocusLost;
end;
procedure TMainForm.btnEditClick(Sender: TObject);
var
row, old: TOperative;
begin
if lvOperatives.Selected = nil then
Exit;
old := OperativeFromSelected;
FormEdit.Form2.OperativeBeingEdited := @row;
FormEdit.Form2.edtFirstName.Text := old.FirstName;
FormEdit.Form2.edtLastName.Text := old.LastName;
FormEdit.Form2.edtNickName.Text := old.NickName;
FormEdit.Form2.dtpBirthDate.Date := old.DateOfBirth;
FormEdit.Form2.edtPlaceOfBirth.Text := old.BirthPlace;
FormEdit.Form2.DialogResult := False;
FormEdit.Form2.ShowModal;
if FormEdit.Form2.DialogResult = True then
begin
UpdateOperative(old, row);
PendingChanges := True;
end;
FillListBox;
tbSearchLastName.CallFocusLost;
end;
procedure TMainForm.btnSaveClick(Sender: TObject);
begin
WriteToFile;
ShowMessage('Zapisano');
tbSearchNickName.CallFocusLost;
PendingChanges := False;
end;
end.