-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasksframe.pas
201 lines (166 loc) · 5.23 KB
/
tasksframe.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
unit TasksFrame;
// Todo: Too much duplicates with PeriodFrame unit. Need to make common parent class for grids.
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, ListViewFilterEdit, Forms, Controls, ComCtrls,
ExtCtrls, Menus, VirtualDBGrid, LocalizedForms, VirtualTrees, Graphics,
StdCtrls;
type
{ TTasksFrame }
TTasksFrame = class(TLocalizedFrame)
FilterEdit: TListViewFilterEdit;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem5: TMenuItem;
MenuItem6: TMenuItem;
TasksGridPopupMenu: TPopupMenu;
TasksDBGrid: TVirtualDBGrid;
ToolBar1: TToolBar;
AddToolButton: TToolButton;
RemoveToolButton: TToolButton;
EditToolButton: TToolButton;
SeparatorToolButton: TToolButton;
StartTrackingToolButton: TToolButton;
StopTrackingToolButton: TToolButton;
procedure FilterEditChange(Sender: TObject);
procedure TasksDBGridBeforePaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas);
procedure TasksDBGridGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: Integer);
procedure TasksDBGridKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure TasksDBGridPaintText(Sender: TBaseVirtualTree;
const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType);
procedure TasksDBGridRecordDblClick(Sender: TCustomVirtualDBGrid;
Column: TColumnIndex; RecordData: TRecordData);
private
FirstTimeGridShown: Boolean;
procedure UpdateTranslation(ALang: String); override;
procedure AutoFitGridColumns;
public
constructor Create(AOwner: TComponent); override;
procedure SelectTask(AnID: Integer);
end;
implementation
uses
Variants, DatabaseDM, LCLType, NonVisualCtrlsDM, WinMouse{, LCLTranslator};
resourcestring
RSFilterHint = '(filter)';
{$R *.lfm}
{ TTasksFrame }
procedure TTasksFrame.TasksDBGridPaintText(Sender: TBaseVirtualTree;
const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType);
var
Grid: TVirtualDBGrid;
begin
// Green color for active task
if (Column <= NoColumn) then
Exit;
Grid := (Sender as TVirtualDBGrid);
//if VarIsNull(Grid.GetNodeRecordData(Node).FieldValue['is_active']) then
// Exit;
if Boolean(Grid.GetNodeRecordData(Node).FieldValue['is_active']) then
begin
if Node <> Grid.FocusedNode then
TargetCanvas.Font.Color := clGreen;
TargetCanvas.Font.Style := [fsBold];
end;
end;
procedure TTasksFrame.TasksDBGridRecordDblClick(Sender: TCustomVirtualDBGrid;
Column: TColumnIndex; RecordData: TRecordData);
begin
NonVisualCtrlsDataModule.EditTaskAction.Execute;
end;
procedure TTasksFrame.UpdateTranslation(ALang: String);
begin
inherited;
FilterEdit.TextHint := RSFilterHint;
end;
procedure TTasksFrame.AutoFitGridColumns;
function GetTextWidth(const AText: String; const AFont: TFont): Integer;
var
Bmp: TBitmap;
begin
Result := 0;
Bmp := TBitmap.Create;
try
Bmp.Canvas.Font.Assign(AFont);
Result := Bmp.Canvas.TextWidth(AText);
finally
Bmp.Free;
end;
end;
var
ColIdx: Integer;
Column: TVirtualTreeColumn;
begin
with TasksDBGrid.Header do
begin
AutoFitColumns(False, smaAllColumns);
for ColIdx := 0 to Columns.Count - 1 do
begin
Column := Columns.Items[ColIdx];
Column.MinWidth := GetTextWidth(Column.Text, Font)
+ 20 {ToDo: Calculate REAL margin value};
end;
end;
end;
constructor TTasksFrame.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FirstTimeGridShown := True;
end;
procedure TTasksFrame.SelectTask(AnID: Integer);
begin
DatabaseDataModule.TasksSQLQuery.Locate('id', AnID, []);
end;
procedure TTasksFrame.FilterEditChange(Sender: TObject);
begin
DatabaseDataModule.TasksFilterText := FilterEdit.Caption;
end;
procedure TTasksFrame.TasksDBGridBeforePaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas);
begin
if FirstTimeGridShown then
begin
FirstTimeGridShown := False;
AutoFitGridColumns;
end;
end;
procedure TTasksFrame.TasksDBGridGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: Integer);
var
Grid: TVirtualDBGrid;
Col: TVirtualDBTreeColumn;
RecordData: TRecordData;
begin
if Column <= NoColumn then
Exit;
Grid := Sender as TVirtualDBGrid;
Col := TVirtualDBTreeColumn(Grid.Header.Columns[Column]);
if Col.FieldName = {'id'} 'name' then
begin
RecordData := Grid.GetNodeRecordData(Node);
if RecordData.FieldValue['is_active'] then
ImageIndex := StartIconIdx
else if RecordData.FieldValue['done'] then
ImageIndex := TickIconIdx;
end;
end;
procedure TTasksFrame.TasksDBGridKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_DELETE then
begin
NonVisualCtrlsDataModule.DeleteTaskAction.Execute;
Key := 0;
end;
end;
end.