-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduleform.pas
395 lines (349 loc) · 10.7 KB
/
scheduleform.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
unit ScheduleForm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, types, sqldb, DB, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, ExtCtrls, Grids, MetaUnit, TableForm, Connect, Clipbrd, Buttons, EditForm;
const
TextHeight = 15;
BtnHeight = 30;
type
TCoord = record
Col, Row, Item: integer;
end;
TFieldsValues = array of string;
TMyGridItem = array of TFieldsValues;
TGridElem = record
GridElem: TMyGridItem;
AllUsed: boolean;
end;
TMyGrid = array of array of TGridElem;
TAxisElem = array of array [1..2] of string;
{ TSchedule }
TSchedule = class(TForm)
BtnDel: TBitBtn;
BtnChange: TBitBtn;
BtnAdd: TBitBtn;
DrawGrid: TDrawGrid;
HorzLabel: TLabel;
VertLabel: TLabel;
ScheduleShow: TButton;
CheckGroup: TCheckGroup;
HorComboBox: TComboBox;
VertComboBox: TComboBox;
DataSource: TDataSource;
SQLQuery: TSQLQuery;
procedure BtnAddClick(Sender: TObject);
procedure BtnChangeClick(Sender: TObject);
procedure BtnDelClick(Sender: TObject);
procedure DrawGridDblClick(Sender: TObject);
procedure DrawGridDrawCell(Sender: TObject; aCol, aRow: integer;
aRect: TRect; aState: TGridDrawState);
procedure DrawGridMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
procedure DrawGridLoseFocus(Sender: TObject);
procedure ScheduleShowClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure UpdateGrid;
procedure ComboBoxChange(Sender: TObject);
private
procedure AddValue(var Vert, Hor: integer);
procedure DrawTriangle(ARect: TRect; ACanvas: TCanvas);
function GetArray(AField: TMyField): TAxisElem;
procedure MoveBtn;
{ private declarations }
public
{ public declarations }
end;
var
Select, Order: string;
Schedule: TSchedule;
HorFieldsValues, VertFieldsValues: TAxisElem;
HorComboBoxIndex, VertComboBoxIndex, VisibleFieldNum: integer;
GridData: TMyGrid;
MouseCoord, PrevMouseCord: TCoord;
MousePos: TPoint;
implementation
{$R *.lfm}
{ TSchedule }
function Cord(aCol, aRow, aItem: integer): TCoord;
begin
Result.Col := aCol;
Result.Row := aRow;
Result.Item := aItem;
end;
procedure TSchedule.FormCreate(Sender: TObject);
var
i: integer;
begin
for i := 0 to Length(ScheduleTable.ArrOfFields) - 1 do
begin
CheckGroup.Items.Add(ScheduleTable.ArrOfFields[i].Caption);
CheckGroup.Checked[i] := True;
if i = 0 then
Continue;
HorComboBox.Items.Add(ScheduleTable.ArrOfFields[i].Caption);
VertComboBox.Items.Add(ScheduleTable.ArrOfFields[i].Caption);
end;
HorComboBox.ItemIndex := 0;
VertComboBox.ItemIndex := 1;
ComboBoxChange(VertComboBox);
Select := ScheduleTable.GetSQL();
end;
procedure TSchedule.AddValue(var Vert, Hor: integer);
var
FieldName: string;
Values: TFieldsValues;
i: integer;
begin
SetLength(Values, Length(ScheduleTable.ArrOfFields));
for i := 0 to High(ScheduleTable.ArrOfFields) do
begin
FieldName := ScheduleTable.ArrOfFields[i].Name;
Values[i] := SQLQuery.FieldByName(FieldName).AsString;
end;
while (HorFieldsValues[Hor, 1] <> Values[HorComboBoxIndex]) or
(VertFieldsValues[Vert, 1] <> Values[VertComboBoxIndex]) do
begin
Inc(Vert);
if Vert > High(VertFieldsValues) then
begin
Vert := 0;
Inc(Hor);
end;
end;
SetLength(GridData[Hor + 1][Vert + 1].GridElem,
Length(GridData[Hor + 1][Vert + 1].GridElem) + 1);
GridData[Hor + 1][Vert + 1].GridElem[High(GridData[Hor + 1][Vert + 1].GridElem)] := Values;
end;
procedure TSchedule.MoveBtn;
var
ARect: TRect;
begin
ARect := DrawGrid.CellRect(MouseCoord.Col, MouseCoord.Row);
BtnChange.Visible := False;
BtnAdd.Visible := False;
BtnDel.Visible := False;
if (MouseCoord.Row <> 0) then
begin
BtnAdd.Top := ARect.Bottom + DrawGrid.Top - BtnChange.Height;
BtnAdd.Left := ARect.Left + 10;
BtnAdd.Visible := True;
if (MouseCoord.Item >= 0) then
begin
BtnDel.Top := (MouseCoord.Item + 1) * TextHeight * VisibleFieldNum +
DrawGrid.Top + ARect.Top - BtnDel.Height * 2;
BtnChange.Top := (MouseCoord.Item + 1) * TextHeight * VisibleFieldNum +
DrawGrid.Top + ARect.Top - BtnChange.Height;
BtnDel.Left := ARect.Right - BtnDel.Width;
BtnChange.Left := ARect.Right - BtnChange.Width;
BtnChange.Visible := True;
BtnDel.Visible := True;
end;
end;
end;
procedure TSchedule.DrawGridMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: integer);
var
ACol, ARow, index: integer;
ARect: TRect;
begin
DrawGrid.MouseToCell(X, Y, ACol, ARow);
if (ACol * ARow = 0) or (Length(GridData) = 0) then
begin
MouseCoord := Cord(0, 0, 0);
MoveBtn();
Exit;
end;
ARect := DrawGrid.CellRect(ACol, ARow);
index := (Y - ARect.Top) div (TextHeight * VisibleFieldNum);
if (index < Length(GridData[ACol][ARow].GridElem)) and
((index + 1) * TextHeight * VisibleFieldNum < ARect.Bottom - ARect.Top) then
begin
if (MouseCoord.Col = ACol) and (MouseCoord.Row = ARow) and
(MouseCoord.Item = index) then
Exit;
MouseCoord := Cord(ACol, ARow, index);
MoveBtn();
end
else
begin
MouseCoord := Cord(ACol, ARow, -1);
MoveBtn();
end;
end;
procedure TSchedule.DrawGridLoseFocus(Sender: TObject);
begin
if (not BtnAdd.Focused) and (not BtnChange.Focused)
and (not BtnDel.Focused) then
begin
MouseCoord := Cord(0, 0, 0);
MoveBtn();
end;
end;
procedure TSchedule.UpdateGrid;
var
i, j, Vert, Hor: integer;
begin
SQLQuery.Close;
Clipboard.AsText := Select + Order;
SQLQuery.SQL.Text := Select + Order;
SQLQuery.Open;
Vert := 0;
Hor := 0;
for i := 0 to High(GridData) do
for j := 0 to High(GridData[i]) do
SetLength(GridData[i][j].GridElem, 0);
while not SQLQuery.EOF do
begin
AddValue(Vert, Hor);
SQLQuery.Next;
end;
DrawGrid.Invalidate;
end;
procedure TSchedule.DrawTriangle(ARect: TRect; ACanvas: TCanvas);
begin
with ACanvas do
begin
Pen.Color := clBlack;
Pen.Width := 1;
Brush.Style := bsSolid;
Brush.Color := clBlack;
Polygon([Point(ARect.Right - 10, ARect.Bottom),
Point(ARect.Right, ARect.Bottom - 10),
Point(ARect.Right, ARect.Bottom)]);
end;
end;
procedure TSchedule.DrawGridDrawCell(Sender: TObject; aCol, aRow: integer;
aRect: TRect; aState: TGridDrawState);
var
i, j, y: integer;
Str: string;
begin
if (aCol + aRow = 0) or (Length(GridData) = 0) then Exit;
if (aRow = 0) then
begin
TDrawGrid(Sender).Canvas.TextOut(aRect.Left, aRect.Top,
HorFieldsValues[aCol - 1, 1]);
Exit;
end;
if (aCol = 0) then
begin
TDrawGrid(Sender).Canvas.TextOut(aRect.Left, aRect.Top,
VertFieldsValues[aRow - 1, 1]);
Exit;
end;
y := 0;
for i := 0 to High(GridData[aCol][aRow].GridElem) do
begin
Canvas.Font.Size := TextHeight;
for j := 0 to Length(ScheduleTable.ArrOfFields) - 1 do
if (CheckGroup.Checked[j]) and (CheckGroup.CheckEnabled[j]) then
begin
Str := Format('%s : %s ', [CheckGroup.Items[j],
GridData[aCol][aRow].GridElem[i][j]]);
TDrawGrid(Sender).Canvas.TextOut(aRect.Left + 6, aRect.Top + y, Str);
y += TextHeight;
end;
with DrawGrid.Canvas do
begin
Brush.Style := bsClear;
Pen.Style := psDot;
Pen.Color := clBlack;
Line(aRect.Left, aRect.Top + y, aRect.Right, aRect.Top + y);
end;
if VisibleFieldNum * TextHeight + y + BtnHeight > DrawGrid.RowHeights[aRow] then
Break;
end;
if High(GridData[aCol][aRow].GridElem) > i then
DrawTriangle(aRect, DrawGrid.Canvas);
end;
procedure TSchedule.DrawGridDblClick(Sender: TObject);
var
Col, Row: integer;
begin
Col := MouseCoord.Col;
Row := MouseCoord.Row;
if (Col * Row <> 0) and (Length(GridData[Col][Row].GridElem) > 0) then
if TextHeight * Length(GridData[Col][Row].GridElem) * VisibleFieldNum +
BtnHeight <> DrawGrid.RowHeights[Row] then
DrawGrid.RowHeights[Row] := TextHeight * Length(GridData[Col][Row].GridElem)
* VisibleFieldNum + BtnHeight
else
DrawGrid.RowHeights[Row] := TextHeight * VisibleFieldNum + BtnHeight;
DrawGrid.Invalidate;
MoveBtn;
end;
procedure TSchedule.BtnAddClick(Sender: TObject);
begin
AddCell(-1, 8, @UpdateGrid);
end;
procedure TSchedule.BtnChangeClick(Sender: TObject);
begin
AddCell(StrToInt(GridData[MouseCoord.Col][MouseCoord.Row].GridElem
[MouseCoord.Item][0]), 8, @UpdateGrid);
end;
procedure TSchedule.BtnDelClick(Sender: TObject);
begin
if (MessageDlgPos('Удалить?', mtWarning, [mbOK, mbCancel], 0,
Self.Left + 100, Self.Top + 100) = mrCancel) then
Exit;
SQLQuery.Close;
SQLQuery.SQL.Text := Format(' DELETE FROM %s WHERE ID = %s ',
[ScheduleTable.Name, GridData[MouseCoord.Col]
[MouseCoord.Row].GridElem[MouseCoord.Item][0]]);
SQLQuery.ExecSQL;
UpdateGrid;
end;
procedure TSchedule.ComboBoxChange(Sender: TObject);
var
i: integer;
begin
for i := 0 to High(ScheduleTable.ArrOfFields) do
CheckGroup.CheckEnabled[i] := True;
CheckGroup.CheckEnabled[HorComboBox.ItemIndex + 1] := False;
CheckGroup.CheckEnabled[VertComboBox.ItemIndex + 1] := False;
end;
function TSchedule.GetArray(AField: TMyField): TAxisElem;
begin
SQLQuery.Close;
SQLQuery.SQL.Text := Format('Select * from %s order by %s asc ',
[AField.JoinTable.Name, AField.Order]);
SQLQuery.Open;
while not SQLQuery.EOF do
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result), 1] := SQLQuery.FieldByName(AField.Name).AsString;
Result[High(Result), 2] := SQLQuery.FieldByName('ID').AsString;
SQLQuery.Next;
end;
end;
procedure TSchedule.ScheduleShowClick(Sender: TObject);
var
HorField, VertField: TMyField;
i: integer;
begin
HorComboBoxIndex := HorComboBox.ItemIndex + 1;
VertComboBoxIndex := VertComboBox.ItemIndex + 1;
HorField := ScheduleTable.ArrOfFields[HorComboBoxIndex];
VertField := ScheduleTable.ArrOfFields[VertComboBoxIndex];
VisibleFieldNum := 0;
for i := 0 to High(ScheduleTable.ArrOfFields) do
if (CheckGroup.Checked[i]) and (CheckGroup.CheckEnabled[i]) then
Inc(VisibleFieldNum);
DrawGrid.DefaultRowHeight := TextHeight * VisibleFieldNum + BtnHeight;
HorFieldsValues := GetArray(HorField);
VertFieldsValues := GetArray(VertField);
DrawGrid.ColCount := Length(HorFieldsValues) + 1;
DrawGrid.RowCount := Length(VertFieldsValues) + 1;
DrawGrid.ColWidths[0] := VertField.Width;
DrawGrid.RowHeights[0] := 20;
SetLength(GridData, 0);
SetLength(GridData, Length(HorFieldsValues) + 1);
for i := 0 to High(GridData) do
SetLength(GridData[i], Length(VertFieldsValues) + 1);
Order := Format(' order by %s.%s asc, %s.%s asc ;',
[HorField.JoinTable.Name, HorField.Order, VertField.JoinTable.Name,
VertField.Order]);
UpdateGrid;
end;
end.