-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreportfrm.pas
619 lines (525 loc) · 15.9 KB
/
reportfrm.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
unit ReportFrm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, StdCtrls, EditBtn, ExtCtrls,
CheckLst, TreeListView, Reports, TAGraph, TASeries,
TASources, DateTimePicker, ListFilterEdit, LocalizedForms;
type
TReportView = (rvTable, rvChart);
{ TReportFrame }
TReportFrame = class(TLocalizedFrame)
ButtonPanel: TPanel;
PeriodAllTimeCheckBox: TCheckBox;
LabelsChartSource: TListChartSource;
TaskListFilterEdit: TListFilterEdit;
PeriodBeginDateTimePicker: TDateTimePicker;
PeriodEndDateTimePicker: TDateTimePicker;
SelectAllTasksButton: TButton;
ReportChart: TChart;
SettingsPanel: TPanel;
TasksCheckListBox: TCheckListBox;
TasksGroupBox: TGroupBox;
PeriodBeginLabel: TLabel;
PeriodEndLabel: TLabel;
PeriodGroupBox: TGroupBox;
GroupByRadioGroup: TRadioGroup;
ReportTreeListView: TTreeListView;
ViewRadioGroup: TRadioGroup;
procedure GroupByRadioGroupClick(Sender: TObject);
procedure PeriodAllTimeCheckBoxChange(Sender: TObject);
procedure PeriodBeginDateTimePickerChange(Sender: TObject);
procedure PeriodEndDateTimePickerChange(Sender: TObject);
procedure ReportTreeListViewCustomItemDraw(sender: TObject;
eventTyp_cdet: TCustomDrawEventTyp; item: TTreeListItem;
var defaultDraw: Boolean);
procedure ReportTreeListViewCustomRecordItemDraw(sender: TObject;
eventTyp_cdet: TCustomDrawEventTyp; recordItem: TTreeListRecordItem;
var defaultDraw: Boolean);
procedure SelectAllTasksButtonClick(Sender: TObject);
procedure TasksCheckListBoxClickCheck(Sender: TObject);
procedure ViewRadioGroupClick(Sender: TObject);
private
function GetBeginDate: TDate;
procedure SetBeginDate(ADate: TDate);
function GetEndDate: TDate;
procedure SetEndDate(ADate: TDate);
function GetGroupBy: TReportGroupBy;
procedure SetGroupBy(AVal: TReportGroupBy);
function GetView: TReportView;
procedure SetView(AVal: TReportView);
function GetSelectedTasks: TTaskIds;
procedure SetNoDateLimit(AVal: Boolean);
function GetNoDateLimit: Boolean;
procedure UpdateTasksList;
//procedure CMShowingChanged(var AMsg: TMessage); message CM_SHOWINGCHANGED;
procedure FillReportTree(const AReport: TReport);
procedure FillReportChart(const AReport: TReport);
procedure CreateChartLabels;
procedure UpdateTranslation(ALang: String); override;
public
constructor Create(TheOwner: TComponent); override;
property BeginDate: TDate read GetBeginDate write SetBeginDate;
property EndDate: TDate read GetEndDate write SetEndDate;
property GroupBy: TReportGroupBy read GetGroupBy write SetGroupBy;
property View: TReportView read GetView write SetView;
property SelectedTasks: TTaskIds read GetSelectedTasks {write ...};
property NoDateLimit: Boolean read GetNoDateLimit write SetNoDateLimit;
procedure UpdateReport;
procedure OnShow;
end;
implementation
uses DateUtils, DatabaseDM, Utils, NonVisualCtrlsDM, Graphics, System.UITypes,
fgl, Math{, LCLTranslator};
resourcestring
RSYears = 'Years';
RSMonths = 'Months';
RSDays = 'Days';
RSTable = 'Table';
RSChart = 'Chart';
{$R *.lfm}
type
TBarSeriesMap = specialize TFPGMap<String, TBarSeries>;
{ TReportFrame }
procedure TReportFrame.ViewRadioGroupClick(Sender: TObject);
begin
View := View;
end;
procedure TReportFrame.GroupByRadioGroupClick(Sender: TObject);
begin
GroupBy := GroupBy;
end;
procedure TReportFrame.PeriodAllTimeCheckBoxChange(Sender: TObject);
begin
NoDateLimit := NoDateLimit;
end;
procedure TReportFrame.PeriodBeginDateTimePickerChange(Sender: TObject);
begin
BeginDate := BeginDate;
end;
procedure TReportFrame.PeriodEndDateTimePickerChange(Sender: TObject);
begin
EndDate := EndDate;
end;
procedure TReportFrame.ReportTreeListViewCustomItemDraw(sender: TObject;
eventTyp_cdet: TCustomDrawEventTyp; item: TTreeListItem;
var defaultDraw: Boolean);
begin
if (eventTyp_cdet = cdetPrePaint) and (not Assigned(item.Parent)) then
begin
if not item.SeemsSelected then
begin
(sender as TTreeListView).Canvas.Brush.Color:=clSkyBlue;
end;
// (sender as TTreeListView).Canvas.Font.Bold := True;
end;
defaultDraw := True;
end;
procedure TReportFrame.ReportTreeListViewCustomRecordItemDraw(sender: TObject;
eventTyp_cdet: TCustomDrawEventTyp; recordItem: TTreeListRecordItem;
var defaultDraw: Boolean);
begin
if (eventTyp_cdet = cdetPrePaint) and (Assigned(recordItem.Parent))
and (not Assigned(recordItem.Parent.Parent)) then
begin
(sender as TTreeListView).Canvas.Font.Bold := True;
end;
defaultDraw := True;
end;
procedure TReportFrame.SelectAllTasksButtonClick(Sender: TObject);
var
IsAllChecked: Boolean;
Idx: Integer;
begin
IsAllChecked := True;
for Idx := 0 to TasksCheckListBox.Count - 1 do
begin
if not TasksCheckListBox.Checked[Idx] then
begin
IsAllChecked := False;
Break;
end;
end;
if IsAllChecked then
TasksCheckListBox.CheckAll(cbUnchecked)
else
TasksCheckListBox.CheckAll(cbChecked);
UpdateReport;
end;
procedure TReportFrame.TasksCheckListBoxClickCheck(Sender: TObject);
begin
UpdateReport;
end;
procedure TReportFrame.UpdateReport;
var
Generator: TReportGenerator = nil;
Report: TReport = nil;
begin
Generator := TReportGenerator.Create;
try
if not NoDateLimit then
begin
Generator.BeginDate := BeginDate;
Generator.EndDate := EndDate;
end
else
begin
Generator.BeginDate := MinDateTime;
Generator.EndDate := MaxDateTime;
end;
Generator.GroupBy := GroupBy;
Generator.Tasks := SelectedTasks;
try
Report := Generator.GenerateReport;
case View of
rvTable:
begin
FillReportTree(Report);
end;
rvChart:
begin
FillReportChart(Report);
end;
end;
finally
Report.Free;
end;
finally
Generator.Free;
end;
end;
procedure TReportFrame.OnShow;
begin
UpdateTasksList;
UpdateReport;
end;
function TReportFrame.GetBeginDate: TDate;
begin
//if PeriodAllTimeCheckBox.Checked then
// Result := PeriodBeginDateTimePicker.MinDate
//else
Result := PeriodBeginDateTimePicker.Date;
end;
procedure TReportFrame.SetBeginDate(ADate: TDate);
begin
//if PeriodBeginDateTimePicker.Date = ADate then
// Exit;
PeriodBeginDateTimePicker.Date := ADate;
PeriodEndDateTimePicker.MinDate := ADate;
UpdateReport;
end;
function TReportFrame.GetEndDate: TDate;
begin
//if PeriodAllTimeCheckBox.Checked then
// Result := PeriodEndDateTimePicker.MaxDate
//else
Result := PeriodEndDateTimePicker.Date;
end;
procedure TReportFrame.SetEndDate(ADate: TDate);
begin
//if PeriodEndDateTimePicker.Date = ADate then
// Exit;
PeriodEndDateTimePicker.Date := ADate;
PeriodBeginDateTimePicker.MaxDate := ADate;
UpdateReport;
end;
function TReportFrame.GetGroupBy: TReportGroupBy;
begin
Result := TReportGroupBy(GroupByRadioGroup.ItemIndex);
end;
procedure TReportFrame.SetGroupBy(AVal: TReportGroupBy);
begin
//if GroupByRadioGroup.ItemIndex = Ord(AVal) then
// Exit;
GroupByRadioGroup.ItemIndex := Ord(AVal);
UpdateReport;
end;
function TReportFrame.GetView: TReportView;
begin
Result := TReportView(ViewRadioGroup.ItemIndex);
end;
procedure TReportFrame.SetView(AVal: TReportView);
begin
//if ViewRadioGroup.ItemIndex = Ord(AVal) then
// Exit;
ViewRadioGroup.ItemIndex := Ord(AVal);
UpdateReport;
case AVal of
rvTable:
begin
ReportChart.Hide;
ReportTreeListView.Show;
end;
rvChart:
begin
ReportTreeListView.Hide;
ReportChart.Show;
end;
end;
end;
function TReportFrame.GetSelectedTasks: TTaskIds;
var
CheckedCount: Integer = 0;
Idx, Idx2: Integer;
begin
for Idx := 0 to TasksCheckListBox.Count - 1 do
begin
if TasksCheckListBox.Checked[Idx] then
Inc(CheckedCount);
end;
SetLength(Result, CheckedCount);
Idx2 := 0;
for Idx := 0 to TasksCheckListBox.Count - 1 do
begin
if TasksCheckListBox.Checked[Idx] then
begin
Result[Idx2] := Integer(TasksCheckListBox.Items.Objects[Idx]);
Inc(Idx2);
end;
end;
end;
procedure TReportFrame.SetNoDateLimit(AVal: Boolean);
begin
PeriodAllTimeCheckBox.Checked := AVal;
PeriodBeginLabel.Enabled := not AVal;
PeriodBeginDateTimePicker.Enabled := not AVal;
PeriodEndLabel.Enabled := not AVal;
PeriodEndDateTimePicker.Enabled := not AVal;
UpdateReport;
end;
function TReportFrame.GetNoDateLimit: Boolean;
begin
Result := PeriodAllTimeCheckBox.Checked;
end;
procedure TReportFrame.UpdateTasksList;
begin
{TasksCheckListBox.Clear} TaskListFilterEdit.Items.Clear;
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'SELECT `id`, `name` FROM `tasks` ORDER BY `name` COLLATE UTF8_CI ASC;';
Open;
First;
while not EOF do
begin
{TasksCheckListBox.AddItem} TaskListFilterEdit.Items.AddObject(FieldByName('name').AsString,
TObject(FieldByName('id').AsInteger));
Next;
end;
Close;
end;
TaskListFilterEdit.InvalidateFilter;
end;
{procedure TReportFrame.CMShowingChanged(var AMsg: TMessage);
begin
inherited;
if Showing then
begin
UpdateTasksList;
UpdateReport;
end;
end; }
procedure TReportFrame.FillReportTree(const AReport: TReport);
var
PeriodIdx: Integer;
Period: String;
TaskIdx: Integer;
Task: String;
TaskTimeSeconds: Integer;
TaskTime: String;
PeriodSecondsSum: integer;
TaskItem: TTreeListItem;
Done: Boolean;
begin
ReportTreeListView.BeginUpdate;
ReportTreeListView.Items.Clear;
for PeriodIdx := 0 to AReport.Items.Count - 1 do
begin
Period := AReport.Items.Keys[PeriodIdx];
case GroupBy of
rgbDay: Period := FormatDateTime({'ddddd'} 'dddddd', ScanDateTime('yyyy-mm-dd', Period));
rgbMonth: Period := FormatDateTime('mmmm yyyy', ScanDateTime('yyyy-mm-dd', Period + '-01'));
//rgbYear: Period := AReport.Items.Keys[PeriodIdx];
end;
with ReportTreeListView.Items.Add(Period) do
begin
ImageIndex := NoIconIdx;
PeriodSecondsSum := 0;
for TaskIdx := 0 to AReport.Items.Data[PeriodIdx].Count - 1 do
begin
Task := AReport.Items.Data[PeriodIdx].Keys[TaskIdx];
if Task.EndsWith(' (done)') then
begin
Task := StringReplace(Task, ' (done)', '', []);
Done := True;
end
else
begin
Done := False;
end;
TaskTimeSeconds := AReport.Items.Data[PeriodIdx].Data[TaskIdx];
PeriodSecondsSum += TaskTimeSeconds;
TaskTime := DurationToStr(TaskTimeSeconds);
TaskItem := SubItems.Add(Task);
TaskItem.ImageIndex := IfThen(Done, TickIconIdx, NoIconIdx);
TaskItem.RecordItems.Add(TaskTime);
end;
RecordItems.Add(DurationToStr(PeriodSecondsSum));
end;
end;
ReportTreeListView.EndUpdate;
end;
procedure TReportFrame.FillReportChart(const AReport: TReport);
function FixColorConstant(AColor: TColorRec): TColor;
{ Bug fix for current FPC (v3.2.2 and earlier)
Issue: https://forum.lazarus.freepascal.org/index.php/topic,60069.0.html
Fixed by commit (not included in release yet):
https://gitlab.com/freepascal.org/fpc/source/-/commit/742ec5680f8edfdae744382f565a3fda804b0e5c?view=parallel
}
begin
{$if defined(FPC_FULLVERSION) and (FPC_FULLVERSION <= 030202)}
Result := RGBToColor(AColor.B, AColor.G, AColor.R);
{$ELSE}
{$Fatal ToDo: Check if it fixed in current FPC version}
Result := AColor;
{$ENDIF}
end;
function GetDefaultColor(AIdx: Integer): TColor;
var
Colors: array of {TColorRec} TColor = (
TColorRec.DodgerBlue,
TColorRec.Gold,
TColorRec.{OrangeRed}Red,
TColorRec.GreenYellow,
TColorRec.{DeepPink}Magenta,
TColorRec.{Sienna}Maroon,
TCOlorRec.Moccasin,
TColorRec.Navy,
TColorRec.MediumSeaGreen,
TColorRec.Silver,
TColorRec.DarkOrchid,
TColorRec.Tomato,
TColorRec.SpringGreen,
TColorRec.PaleTurquoise,
TColorRec.Orange
);
begin
if AIdx in [Low(Colors) .. High(Colors)] then
Result := FixColorConstant(Colors[AIdx])
else
Result := { $ffffff } Random(2 ** 24);
end;
var
PeriodIdx: Integer;
Period: String;
TaskIdx: Integer;
Task: String;
TaskTimeSeconds: Integer;
BarSeries: TBarSeries;
BarSeriesMap: TBarSeriesMap;
BarSeriesIdx: Integer;
XVal: TDate;
YVal: Double;
begin
ReportChart.ClearSeries;
BarSeriesMap := TBarSeriesMap.Create;
try
for PeriodIdx := 0 to AReport.Items.Count - 1 do
begin
Period := AReport.Items.Keys[PeriodIdx];
for TaskIdx := 0 to AReport.Items.Data[PeriodIdx].Count - 1 do
begin
Task := AReport.Items.Data[PeriodIdx].Keys[TaskIdx];
TaskTimeSeconds := AReport.Items.Data[PeriodIdx].Data[TaskIdx];
BarSeriesIdx := BarSeriesMap.IndexOf(Task);
if BarSeriesIdx = -1 then
begin
BarSeries := TBarSeries.Create(ReportChart);
BarSeries.Title := Task;
BarSeries.SeriesColor := GetDefaultColor(BarSeriesMap.Count);
BarSeriesMap.Add(Task, BarSeries);
end
else
begin
BarSeries := BarSeriesMap.Data[BarSeriesIdx];
end;
case GroupBy of
rgbYear: XVal := EncodeDate(StrToInt(Period), 1, 1);
rgbMonth: XVal := ScanDateTime('YYYY-MM-DD', Period + '-01');
rgbDay: XVal := ScanDateTime('YYYY-MM-DD', Period);
end;
YVal := TaskTimeSeconds / 60 / 60;
BarSeries.AddXY(XVal, YVal{, Period});
end;
end;
for BarSeriesIdx := 0 to BarSeriesMap.Count - 1 do
begin
with BarSeriesMap.Data[BarSeriesIdx] do
begin
BarWidthStyle := bwPercentMin;
BarWidthPercent := {Floor}Round(100 / (BarSeriesMap.Count + 1));
BarOffsetPercent := Round((100 / (BarSeriesMap.Count + 1))
* (BarSeriesIdx - (BarSeriesMap.Count - 1) / 2));
end;
ReportChart.AddSeries(BarSeriesMap.Data[BarSeriesIdx]);
end;
CreateChartLabels;
finally
BarSeriesMap.Free;
end;
end;
procedure TReportFrame.CreateChartLabels;
var
Date: TDate;
begin
LabelsChartSource.Clear;
Date := BeginDate;
while Date <= EndDate do
begin
case GroupBy of
rgbYear:
begin
LabelsChartSource.Add(EncodeDate(YearOf(Date), 1, 1), 0,
FormatDateTime('yyyy', Date));
Date := IncYear(Date);
end;
rgbMonth:
begin
LabelsChartSource.Add(EncodeDate(YearOf(Date), MonthOf(Date), 1), 0,
FormatDateTime('mmmm yyyy', Date));
Date := IncMonth(Date);
end;
rgbDay:
begin
LabelsChartSource.Add(Date, 0, FormatDateTime({'ddddd'} 'dddddd', Date));
Date := IncDay(Date);
end;
end;
end;
end;
procedure TReportFrame.UpdateTranslation(ALang: String);
begin
inherited;
with GroupByRadioGroup do
begin
Items[0] := RSYears;
Items[1] := RSMonths;
Items[2] := RSDays;
end;
with ViewRadioGroup do
begin
Items[0] := RSTable;
Items[1] := RSChart;
end;
end;
constructor TReportFrame.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
EndDate := Now;
BeginDate := IncWeek(EndDate, -1);
GroupBy := rgbDay;
View := rvChart;
//UpdateTasksList;
//UpdateReport;
end;
end.