-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainUnit.pas
397 lines (337 loc) · 11.9 KB
/
MainUnit.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxEdit,
DB, cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, DataModuleUnit,
StdCtrls, ExtCtrls, CompetitorFormUnit, ChangeDefaultsUnit,CompetitorTableUnit,
Menus, cxExportGrid4Link;
type
TMainForm = class(TForm)
cxGrid1DBTableView1: TcxGridDBTableView;
cxGrid1Level1: TcxGridLevel;
cxGrid1: TcxGrid;
Panel1: TPanel;
cxGrid1DBTableView1DBColumn1: TcxGridDBColumn;
cxGrid1DBTableView1DBColumn2: TcxGridDBColumn;
cxGrid1DBTableView1DBColumn3: TcxGridDBColumn;
cxGrid1DBTableView1DBColumn4: TcxGridDBColumn;
cxGrid1DBTableView1DBColumn5: TcxGridDBColumn;
cxGrid1DBTableView1DBColumn6: TcxGridDBColumn;
cxGrid1DBTableView1DBColumn7: TcxGridDBColumn;
AddBtn: TButton;
EditBtn: TButton;
DeleteBtn: TButton;
TemplateBtn: TButton;
cxGrid1DBTableView1DBColumn8: TcxGridDBColumn;
cxGrid1DBTableView1DBColumn9: TcxGridDBColumn;
ChangeDefaultsBtn: TButton;
CompetitorTableBtn: TButton;
Panel3: TPanel;
GroupBox1: TGroupBox;
SearchEdit: TEdit;
Button1: TButton;
SearchLabel: TLabel;
PopupMenu1: TPopupMenu;
Export2Excel: TMenuItem;
procedure FormResize(Sender: TObject);
procedure AddBtnClick(Sender: TObject);
procedure EditBtnClick(Sender: TObject);
procedure TemplateBtnClick(Sender: TObject);
// procedure RadioButton1Click(Sender: TObject);
// procedure CheckBox1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure cxGrid1DBTableView1DBColumn9GetDisplayText(
Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
var AText: String);
procedure DeleteBtnClick(Sender: TObject);
procedure ChangeDefaultsBtnClick(Sender: TObject);
procedure CompetitorTableBtnClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure SearchEditChange(Sender: TObject);
procedure Export2ExcelClick(Sender: TObject);
// procedure RadioButton2Click(Sender: TObject);
private
{ Private declarations }
CompetitorForm: TCompetitorForm; //ôîðìà çàïîëíåíèÿ èíôîðìàöèè îá ö÷àñòíèêå
function add_record:integer; //äîáàâëåíèå çàïîëíåííîé èíôîðìàöèè îá ó÷àòñíèêå â áàçó
function edit_record:integer; //èçìåíåíèå ãîòîâîé çàïèñè
procedure prepare_form; //çàïîëíåíèå àíêåòû ó÷àñòíèêà èíôîðìàöèåé
function load_values_to_query: integer; //ñ÷èòûâàåò äàííûå ñ ôîðìû è çàïèñûâàåò èõ â çàïðîñ. âîçâðàùàåò îøèáóè(0-õîðîøî, íå 0 - ïëîõî)
// procedure radio_changed; //èçìåíåíèå ñîñòîÿíèÿ ðàäèîáàòîíîâ
public
{ Public declarations }
procedure refresh_data; //îáíîâëåíèå äàííûõ â òàáëèöå
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.FormResize(Sender: TObject);
var h,b:integer;
begin
//ïîäñòðàèâàåì ÿ÷åéêè òàáëèöû ïîä èçìåíåííûé ðàçìåð
cxGrid1DBTableView1.OptionsView.ColumnAutoWidth := true;
cxGrid1DBTableView1.OptionsView.CellAutoHeight := true;
//èçìåíÿåì âûñîòó êíîïîê â çàâèñèìîñòè îò âûñîòû îêíà
b := 8; //ïðîìåæóòîê ìåæäó êíîïêàìè
h := (panel1.Height - b*(6+1)) div 6;//6 êíîïîê
AddBtn.Height := h;
EditBtn.Height := h;
EditBtn.Top := AddBtn.Top + h + b;
TemplateBtn.Height := h;
TemplateBtn.Top := EditBtn.Top + h + b;
DeleteBtn.Height := h;
DeleteBtn.Top := TemplateBtn.Top + h + b;
ChangeDefaultsBtn.Height := h;
ChangeDefaultsBtn.Top := DeleteBtn.Top + h + b;
CompetitorTableBtn.Height := h;
CompetitorTableBtn.Top := ChangeDefaultsBtn.Top + h + b;
Button1.Width := Panel3.Width - (600+8);
end;
procedure TMainForm.refresh_data;
begin
DataModule1.MainQuery.Close;
DataModule1.MainQuery.Open;
end;
procedure TMainForm.AddBtnClick(Sender: TObject);
begin
CompetitorForm := TCompetitorForm.Create(self);
while CompetitorForm.ShowModal = mrOk do //Ââîäèì ó÷àñòíèêîâ â öèêëå,÷òîá áûñòðåå çàïîëíÿòü òàáëèöó
begin
if add_record <> 0 then
ShowMessage('Ïðè ïîïûòêå âíåñåíèÿ äàííûõ â áàçó âîçíèêëè îøèáêè.');
refresh_data;
end;
CompetitorForm.Destroy;
end;
function TMainForm.add_record: integer;
var r:integer;
begin
with DataModule1 do
begin
MainQuery.Insert;
r := load_values_to_query;
MainQuery.Post;
end;
result := r;
end;
procedure TMainForm.EditBtnClick(Sender: TObject); //èçìåíåíèå òåêóùåé çàïèñè
var num : integer; //íîìåð òåêóùåé çàïèñè
begin
CompetitorForm := TCompetitorForm.Create(self);
prepare_form; //çàïîëíÿåì èñõîäíûìè äàííûìè
if CompetitorForm.ShowModal = mrOk then
if edit_record <> 0 then
ShowMessage('Âîçíèêëà îøèáêà ïðè èçìåíåíèè çàïèñè');
CompetitorForm.Destroy;
//cxGrid1DBTableView1.datacontroller.Refresh;
num := DataModule1.MainDataSource.DataSet.RecNo;
//îáíîâëÿåì îêíî
refresh_data;
DataModule1.MainDataSource.DataSet.RecNo := num;
end;
function TMainForm.edit_record: integer;
var r:integer;
begin
with DataModule1 do
begin
MainQuery.Edit;
r := load_values_to_query;
MainQuery.Post;
end;
result := r;
end;
procedure TMainForm.prepare_form;
var str:string;
begin
//çàïîëíÿåì ôîðìó äàííûìè äëÿ ðåäàêòèðîâàíèÿ
with DataModule1 do
begin
CompetitorForm.FIOEdit.Text := MainQuery.fieldByName('FIO').AsString;
//àîçðàñò
str := MainQuery.fieldByName('t_ages.age').AsString;
CompetitorForm.AgeBox.ItemIndex := CompetitorForm.AgeBox.Items.IndexOf(str);
//ñïèñîê âåñà ïîäãðóæàåì îòäåëüíî
//èíèöèèðóåì ñèãíàë èçìåíåíèÿ ëèñòà âîçðàñòà, ÷òîáû ïîäãðóçèòü ñïèñîê âåñîâ
CompetitorForm.AgeBoxChange(nil);
str := MainQuery.fieldByName('t_weights.weight').AsString;
CompetitorForm.weightBox.ItemIndex := CompetitorForm.weightBox.Items.IndexOf(str);
//Ïîÿñ
str := MainQuery.fieldByName('t_belts.belt').AsString;
CompetitorForm.BeltBox.ItemIndex := CompetitorForm.BeltBox.Items.IndexOf(str);
//ãîðîä
str := MainQuery.fieldByName('t_cities.city').AsString;
CompetitorForm.CityBox.ItemIndex := CompetitorForm.CityBox.Items.IndexOf(str);
//êëóá
str := MainQuery.fieldByName('t_clubs.club').AsString;
CompetitorForm.ClubBox.ItemIndex := CompetitorForm.ClubBox.Items.IndexOf(str);
//îòìåòêà ó÷àòèÿ
CompetitorForm.ParticipationMark.Checked := MainQuery.fieldByName('participation').asBoolean;
end
end;
procedure TMainForm.TemplateBtnClick(Sender: TObject); //èñïîëüçîâàòü òåêóùóþ çàïèñü, êàê øàáëîí.
begin
//äåëàåì âñå, êàê â ýäèò, òîëüêî âìåñòî èñïðàâëåíèÿ äîáàâëÿåì íîâîå
CompetitorForm := TCompetitorForm.Create(self);
prepare_form; //çàïîëíÿåì èñõîäíûìè äàííûìè
while CompetitorForm.ShowModal = mrOk do
begin
if add_record <> 0 then
ShowMessage('Ïðè ïîïûòêå âíåñåíèÿ äàííûõ â áàçó âîçíèêëè îøèáêè.');
//îáíîâëÿåì îêíî
refresh_data;
end;
CompetitorForm.Destroy;
end;
function TMainForm.load_values_to_query: integer;
var r:integer;
v: variant;
begin
with DataModule1 do
begin
MainQuery.FieldByName('FIO').AsString := CompetitorForm.FIOEdit.Text;
r := find_value_querry('t_ages','age','id',CompetitorForm.AgeBox.Text, v);
MainQuery.FieldByName('t_main.age').AsVariant := v;
r := r + find_value_querry('t_weights','weight','id',CompetitorForm.weightBox.Text, v);
MainQuery.FieldByName('t_main.weight').AsVariant := v;
r := r + find_value_querry('t_belts','belt','id',CompetitorForm.beltBox.Text, v);
MainQuery.FieldByName('t_main.belt').AsVariant := v;
r := r + find_value_querry('t_cities','city','id',CompetitorForm.cityBox.Text, v);
MainQuery.FieldByName('t_main.city').AsVariant := v;
r := r + find_value_querry('t_clubs','club','id',CompetitorForm.clubBox.Text, v);
MainQuery.FieldByName('t_main.club').AsVariant := v;
MainQuery.FieldByName('participation').AsBoolean := CompetitorForm.ParticipationMark.Checked;
end;
result := r;
end;
{procedure TMainForm.RadioButton1Click(Sender: TObject);
begin
radio_changed;
end; }
{procedure TMainForm.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then //âêë/âûêë ñîðòèðîâêó ïî âðåìåíè
begin
cxGrid1DBTableView1DBColumn8.Options.Sorting := true;
RadioButton1.Enabled := true;
RadioButton2.Enabled := true;
end
else
begin
RadioButton1.Enabled := false;
RadioButton2.Enabled := false;
cxGrid1DBTableView1DBColumn8.Options.Sorting := false;
end
end; }
{procedure TMainForm.radio_changed;
begin
if RadioButton1.Checked then
cxGrid1DBTableView1DBColumn8.SortOrder := TcxDataSortOrder(soAscending);
if RadioButton2.Checked then
cxGrid1DBTableView1DBColumn8.SortOrder := TcxDataSortOrder(soDescending);
end; }
procedure TMainForm.FormCreate(Sender: TObject);
begin
//ñòàâèì ôîðìó ïî öåíòðó ýêðàíà
self.Left := (screen.Width - self.Width) div 2;
self.Top := (screen.Height - self.Height) div 2;
end;
{procedure TMainForm.RadioButton2Click(Sender: TObject);
begin
radio_changed;
end; }
procedure TMainForm.cxGrid1DBTableView1DBColumn9GetDisplayText(
Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
var AText: String);
begin
AText := IntToStr(Sender.GridView.DataController.GetRowIndexByRecordIndex(ARecord.RecordIndex, false)+1);
end;
procedure TMainForm.DeleteBtnClick(Sender: TObject);
begin
if DataModule1.MainQuery.IsEmpty then
begin
ShowMessage('Ñïèñîê ïóñò, óäàëåíèå íåâîçìîæíî.');
Exit;
end;
if MessageDlg('Äàííîå äåéñòâèå ïðèâåäåò ê áåçâîçâðàòíîìó óäàëåíèþ çàïèñè èç ÁÄ. Âû óâåðåíû, ÷òî õîòèòå óäàëèòü åå?',
mtWarning,mbOKCancel,0) = mrOk then
begin
with DataModule1 do
begin
//äåëàåì çàïðîñ íà ïîëó÷åíèå çàïèñè èç áä, ÿòîáû êóäàëèòü
DBQuery.Close;
DBQuery.SQL.Text := 'SELECT * FROM t_main WHERE id = ' + MainQuery.fieldByName('t_main.id').AsString;
try
DBQuery.Open;
DBQuery.Delete;
except
ShowMessage('Íå óäàëîñü âûïîëíèòü óäàëåíèå âûáðàííîé çàïèñè. Ïîïðîáóéòå åùå ðàç.');
end;
DBQuery.Close;
end
end;
//cxGrid1DBTableView1.datacontroller.Refresh; //íå ðàáîòàåò íà óäàëåíèå
refresh_data;
end;
procedure TMainForm.ChangeDefaultsBtnClick(Sender: TObject);
begin
ChangeDefaultsForm := TChangeDefaultsForm.Create(self);
ChangeDefaultsForm.ShowModal;
ChangeDefaultsForm.Destroy;
refresh_data;
end;
procedure TMainForm.CompetitorTableBtnClick(Sender: TObject);//ñòðîèì òóðíèðíóþ òàáëèöó
begin
CompetitorTableForm := TCompetitorTableForm.Create(self);
CompetitorTableForm.ShowModal;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
DataModule1.MainQuery.Edit;
DataModule1.MainQuery.FieldByName('participation').AsBoolean := not DataModule1.MainQuery.FieldByName('participation').AsBoolean;
DataModule1.MainQuery.Post;
cxGrid1DBTableView1.datacontroller.Refresh;
end;
//procedure TMainForm.SearchBtnClick(Sender: TObject);
//begin
// cxGrid1DBTableView1.OptionsBehavior.IncSearch := true;
// cxGrid1DBTableView1.OptionsBehavior.IncSearch.
// cxGrid1DBTableView1.DataController.Search.
//end;
procedure TMainForm.SearchEditChange(Sender: TObject);
begin
if Trim(SearchEdit.Text) <> '' then //åñëè ïîëå ïîèñêà íåïóñòîå
if DataModule1.MainQuery.Locate('FIO',SearchEdit.Text,[loCaseInsensitive,loPartialKey]) = true then
begin
SearchLabel.Caption := 'Çàïèñü íàéäåíà';
SearchLabel.Font.Color := clGreen;
end
else
begin
SearchLabel.Caption := 'Çàïèñü îòñóòñòâóåò';
SearchLabel.Font.Color := clRed;
end
else //åñëè ïîëå ïóñòîå
SearchLabel.Caption := '';
end;
procedure TMainForm.Export2ExcelClick(Sender: TObject);
begin
with TSaveDialog.Create(self) do
try
DefaultExt := 'xls';
Filter := 'Excel files|*.xls';
if Execute then
if FileExists(FileName) = true then
begin
if MessageDlg('Ôàéë ñ äàííûì èìåíåì óæå ñóùåñòâóåò. Ïåðåçàïèñàòü?', mtWarning,mbOKCancel, 0) = mrOk then
ExportGrid4ToExcel(FileName, cxGrid1, True, True, false);
end
else
ExportGrid4ToExcel(FileName, cxGrid1, True, True, false);
finally
Free;
end;
end;
end.