-
Notifications
You must be signed in to change notification settings - Fork 19
/
LogViewer.Watches.View.pas
535 lines (474 loc) · 16.1 KB
/
LogViewer.Watches.View.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
{
Copyright (C) 2013-2022 Tim Sinaeve [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}
unit LogViewer.Watches.View;
{ View displaying watch values and value history as a master-detail. }
{ TODO: synchronise selected record in history list in sync mode. }
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ExtCtrls,
VirtualTrees, OMultiPanel,
Spring.Collections,
DSharp.Windows.TreeViewPresenter, DSharp.Windows.ColumnDefinitions,
LogViewer.Watches.Data, LogViewer.Watches.Settings,
LogViewer.DisplayValues.Settings;
type
TfrmWatchesView = class(TForm)
pnlMain : TOMultiPanel;
pnlWatches : TPanel;
pnlWatchHistory : TPanel;
private
FMessageId : Int64;
FWatches : TWatchList;
FVSTWatchValues : TVirtualStringTree;
FVSTWatchHistory : TVirtualStringTree;
FDisplayValuesSettings : TDisplayValuesSettings;
FWatchHistoryColumnDefinitions : IColumnDefinitions;
FTVPWatchValues : TTreeViewPresenter;
FTVPWatchHistory : TTreeViewPresenter;
FSettings : TWatchSettings;
{$REGION 'property access methods'}
function GetSelectedWatch: TWatch;
{$ENDREGION}
{$REGION 'event handlers'}
procedure FSettingsChanged(Sender: TObject);
procedure FTVPWatchValuesSelectionChanged(Sender: TObject);
procedure FTVPWatchValuesDoubleClick(Sender: TObject);
procedure FTVPWatchHistoryDoubleClick(Sender: TObject);
function FCDTimeStampGetText(
Sender : TObject;
ColumnDefinition : TColumnDefinition;
Item : TObject
): string;
function FCDTimeStampCustomDraw(
Sender : TObject;
ColumnDefinition : TColumnDefinition;
Item : TObject;
TargetCanvas : TCanvas;
CellRect : TRect;
ImageList : TCustomImageList;
DrawMode : TDrawMode;
Selected : Boolean
): Boolean;
function FCDTypeCustomDraw(
Sender : TObject;
ColumnDefinition : TColumnDefinition;
Item : TObject;
TargetCanvas : TCanvas;
CellRect : TRect;
ImageList : TCustomImageList;
DrawMode : TDrawMode;
Selected : Boolean
): Boolean;
function FCDValueCustomDraw(
Sender : TObject;
ColumnDefinition : TColumnDefinition;
Item : TObject;
TargetCanvas : TCanvas;
CellRect : TRect;
ImageList : TCustomImageList;
DrawMode : TDrawMode;
Selected : Boolean
): Boolean;
function FCDNameCustomDraw(
Sender : TObject;
ColumnDefinition : TColumnDefinition;
Item : TObject;
TargetCanvas : TCanvas;
CellRect : TRect;
ImageList : TCustomImageList;
DrawMode : TDrawMode;
Selected : Boolean
): Boolean;
function FCDIdCustomDraw(
Sender : TObject;
ColumnDefinition : TColumnDefinition;
Item : TObject;
TargetCanvas : TCanvas;
CellRect : TRect;
ImageList : TCustomImageList;
DrawMode : TDrawMode;
Selected : Boolean
): Boolean;
{$ENDREGION}
procedure ConnectWatchHistoryCDEvents;
procedure ConnectWatchValuesCDEvents;
procedure CreateObjects;
protected
procedure UpdateWatchHistory;
procedure UpdateActions; override;
property SelectedWatch: TWatch
read GetSelectedWatch;
public
constructor Create(
AOwner : TComponent;
AWatches : TWatchList;
ASettings : TWatchSettings;
ADisplayValuesSettings : TDisplayValuesSettings
); reintroduce; virtual;
destructor Destroy; override;
procedure UpdateView(AMessageId: Int64 = 0);
procedure GotoFirst;
procedure GotoLast;
procedure Clear;
function HasFocus: Boolean;
end;
implementation
{$R *.dfm}
uses
System.UITypes,
VirtualTrees.Types, VirtualTrees.Header,
DSharp.Windows.ControlTemplates,
DDuce.Factories.TreeViewPresenter, DDuce.Factories.VirtualTrees,
DDuce.Logger, DDuce.Logger.Interfaces,
LogViewer.Resources;
{$REGION 'construction and destruction'}
constructor TfrmWatchesView.Create(AOwner: TComponent; AWatches: TWatchList;
ASettings: TWatchSettings; ADisplayValuesSettings: TDisplayValuesSettings);
begin
inherited Create(AOwner);
FWatches := AWatches;
FSettings := ASettings;
FSettings.OnChanged.Add(FSettingsChanged);
FDisplayValuesSettings := ADisplayValuesSettings;
CreateObjects;
end;
destructor TfrmWatchesView.Destroy;
begin
FSettings.OnChanged.RemoveAll(Self);
FTVPWatchValues.View.ItemsSource := nil;
FTVPWatchHistory.View.ItemsSource := nil;
FTVPWatchValues.Free;
FTVPWatchHistory.Free;
FWatches.Clear;
FWatches := nil;
inherited Destroy;
end;
{$ENDREGION}
{$REGION 'property access methods'}
function TfrmWatchesView.GetSelectedWatch: TWatch;
begin
if Assigned(FTVPWatchValues.SelectedItem) then
Result := FTVPWatchValues.SelectedItem as TWatch
else
Result := nil;
end;
{$ENDREGION}
{$REGION 'event handlers'}
function TfrmWatchesView.FCDIdCustomDraw(Sender: TObject;
ColumnDefinition: TColumnDefinition; Item: TObject; TargetCanvas: TCanvas;
CellRect: TRect; ImageList: TCustomImageList; DrawMode: TDrawMode;
Selected: Boolean): Boolean;
begin
if DrawMode = dmPaintText then
begin
FDisplayValuesSettings.Id.AssignTo(TargetCanvas.Font);
end
else if DrawMode = dmBeforeCellPaint then
begin
FDisplayValuesSettings.Id.AssignTo(TargetCanvas);
TargetCanvas.FillRect(CellRect);
end;
Result := True;
end;
function TfrmWatchesView.FCDNameCustomDraw(Sender: TObject;
ColumnDefinition: TColumnDefinition; Item: TObject; TargetCanvas: TCanvas;
CellRect: TRect; ImageList: TCustomImageList; DrawMode: TDrawMode;
Selected: Boolean): Boolean;
begin
if DrawMode = dmPaintText then
begin
FDisplayValuesSettings.ValueName.AssignTo(TargetCanvas.Font);
end
else if DrawMode = dmBeforeCellPaint then
begin
FDisplayValuesSettings.ValueName.AssignTo(TargetCanvas);
TargetCanvas.FillRect(CellRect);
end;
Result := True;
end;
function TfrmWatchesView.FCDTimeStampCustomDraw(Sender: TObject;
ColumnDefinition: TColumnDefinition; Item: TObject; TargetCanvas: TCanvas;
CellRect: TRect; ImageList: TCustomImageList; DrawMode: TDrawMode;
Selected: Boolean): Boolean;
begin
if DrawMode = dmPaintText then
begin
FDisplayValuesSettings.TimeStamp.AssignTo(TargetCanvas.Font);
end
else if DrawMode = dmBeforeCellPaint then
begin
FDisplayValuesSettings.TimeStamp.AssignTo(TargetCanvas);
TargetCanvas.FillRect(CellRect);
end;
Result := True;
end;
function TfrmWatchesView.FCDValueCustomDraw(Sender: TObject;
ColumnDefinition: TColumnDefinition; Item: TObject; TargetCanvas: TCanvas;
CellRect: TRect; ImageList: TCustomImageList; DrawMode: TDrawMode;
Selected: Boolean): Boolean;
begin
if DrawMode = dmPaintText then
begin
FDisplayValuesSettings.Value.AssignTo(TargetCanvas.Font);
end
else if DrawMode = dmBeforeCellPaint then
begin
FDisplayValuesSettings.Value.AssignTo(TargetCanvas);
TargetCanvas.FillRect(CellRect);
end;
Result := True;
end;
function TfrmWatchesView.FCDTimeStampGetText(Sender: TObject;
ColumnDefinition: TColumnDefinition; Item: TObject): string;
begin
if Item is TWatch then
Result := FormatDateTime('hh:nn:ss:zzz', TWatch(Item).TimeStamp)
else
begin
Result := FormatDateTime('hh:nn:ss:zzz', TWatchValue(Item).TimeStamp)
end;
end;
function TfrmWatchesView.FCDTypeCustomDraw(Sender: TObject;
ColumnDefinition: TColumnDefinition; Item: TObject; TargetCanvas: TCanvas;
CellRect: TRect; ImageList: TCustomImageList; DrawMode: TDrawMode;
Selected: Boolean): Boolean;
begin
if DrawMode = dmPaintText then
begin
if TWatch(Item).MessageType = lmtCounter then
begin
FDisplayValuesSettings.Counter.AssignTo(TargetCanvas.Font);
end
else
begin
FDisplayValuesSettings.ValueType.AssignTo(TargetCanvas.Font);
end;
end
else if DrawMode = dmBeforeCellPaint then
begin
FDisplayValuesSettings.ValueType.AssignTo(TargetCanvas);
TargetCanvas.FillRect(CellRect);
end;
Result := True;
end;
procedure TfrmWatchesView.FTVPWatchHistoryDoubleClick(Sender: TObject);
begin
FTVPWatchHistory.TreeView.Header.AutoFitColumns(False, smaAllColumns);
end;
procedure TfrmWatchesView.FTVPWatchValuesDoubleClick(Sender: TObject);
begin
FTVPWatchValues.TreeView.Header.AutoFitColumns(False, smaAllColumns);
end;
procedure TfrmWatchesView.FTVPWatchValuesSelectionChanged(Sender: TObject);
begin
UpdateWatchHistory;
end;
procedure TfrmWatchesView.FSettingsChanged(Sender: TObject);
begin
FTVPWatchValues.ShowHeader := FSettings.ColumnHeadersVisible;
FTVPWatchHistory.ShowHeader := FSettings.ColumnHeadersVisible;
end;
{$ENDREGION}
{$REGION 'protected methods'}
procedure TfrmWatchesView.CreateObjects;
var
CDS : IColumnDefinitions;
CD : TColumnDefinition;
begin
FVSTWatchValues := TVirtualStringTreeFactory.CreateList(Self, pnlWatches);
FVSTWatchValues.Name := 'FVSTWatchValues'; // for debugging
FVSTWatchValues.AlignWithMargins := False;
CDS := TFactories.CreateColumnDefinitions;
CD := CDS.Add(COLUMNNAME_NAME);
CD.ValuePropertyName := COLUMNNAME_NAME;
CD.MinWidth := 60;
CD.Width := 100;
CD.HintPropertyName := CD.ValuePropertyName;
CD.OnCustomDraw := FCDNameCustomDraw;
CD := CDS.Add(COLUMNNAME_VALUE);
CD.ValuePropertyName := COLUMNNAME_VALUE;
CD.HintPropertyName := CD.ValuePropertyName;
// Only one column in the header can have AutoSize = True. This property
// determines the value of the Header.AutoSizeIndex property of the treeview.
// This property will cause the columns to fit to the width of the control,
// and will reduce the width of the AutoSizeIndex column if needed when the
// list width is adjusted.
CD.AutoSize := True;
CD.OnCustomDraw := FCDValueCustomDraw;
CD := CDS.Add(COLUMNNAME_VALUETYPE);
CD.ValuePropertyName := COLUMNNAME_VALUETYPE;
CD.HintPropertyName := CD.ValuePropertyName;
CD.MinWidth := 0;
CD.Width := 60;
CD.Caption := SType;
CD.OnCustomDraw := FCDTypeCustomDraw;
CD := CDS.Add(COLUMNNAME_TIMESTAMP);
CD.ValuePropertyName := COLUMNNAME_TIMESTAMP;
CD.MinWidth := 80;
CD.Width := 80;
CD.HintPropertyName := CD.ValuePropertyName;
CD.Alignment := taCenter;
CD.OnGetText := FCDTimeStampGetText;
CD.OnCustomDraw := FCDTimeStampCustomDraw;
FWatchHistoryColumnDefinitions := TFactories.CreateColumnDefinitions;
CD := FWatchHistoryColumnDefinitions.Add(COLUMNNAME_ID);
CD.ValuePropertyName := COLUMNNAME_ID;
CD.HintPropertyName := CD.ValuePropertyName;
CD.Width := 100;
CD.OnCustomDraw := FCDIdCustomDraw;
CD := FWatchHistoryColumnDefinitions.Add(COLUMNNAME_VALUE);
CD.ValuePropertyName := COLUMNNAME_VALUE;
CD.HintPropertyName := CD.ValuePropertyName;
CD.AutoSize := True;
CD.OnCustomDraw := FCDValueCustomDraw;
CD.ColumnOptions := CD.ColumnOptions - [coSortable];
CD := FWatchHistoryColumnDefinitions.Add(COLUMNNAME_TIMESTAMP);
CD.MinWidth := 80;
CD.Width := 80;
CD.ValuePropertyName := COLUMNNAME_TIMESTAMP;
CD.HintPropertyName := CD.ValuePropertyName;
CD.Alignment := taCenter;
CD.OnGetText := FCDTimeStampGetText;
CD.OnCustomDraw := FCDTimeStampCustomDraw;
FTVPWatchValues := TFactories.CreateTreeViewPresenter(
Self,
FVSTWatchValues,
FWatches.List as IObjectList,
CDS
);
ConnectWatchValuesCDEvents;
FTVPWatchValues.OnSelectionChanged := FTVPWatchValuesSelectionChanged;
FTVPWatchValues.OnDoubleClick := FTVPWatchValuesDoubleClick;
FVSTWatchHistory := TVirtualStringTreeFactory.CreateList(Self, pnlWatchHistory);
FVSTWatchHistory.Name := 'FVSTWatchHistory'; // for debugging
FVSTWatchHistory.AlignWithMargins := False;
FTVPWatchHistory := TFactories.CreateTreeViewPresenter(Self, FVSTWatchHistory);
FTVPWatchHistory.OnDoubleClick := FTVPWatchHistoryDoubleClick;
FTVPWatchValues.ShowHeader := FSettings.ColumnHeadersVisible;
FTVPWatchHistory.ShowHeader := FSettings.ColumnHeadersVisible;
end;
procedure TfrmWatchesView.Clear;
begin
FWatches.Clear;
FTVPWatchValues.Refresh;
UpdateWatchHistory;
end;
procedure TfrmWatchesView.ConnectWatchHistoryCDEvents;
var
CD : TColumnDefinition;
I : Integer;
begin
for I := 0 to FTVPWatchHistory.ColumnDefinitions.Count - 1 do
begin
CD := FTVPWatchHistory.ColumnDefinitions[I];
if CD.ValuePropertyName = COLUMNNAME_TIMESTAMP then
begin
CD.OnCustomDraw := FCDTimeStampCustomDraw;
CD.OnGetText := FCDTimeStampGetText;
end
else if CD.ValuePropertyName = COLUMNNAME_VALUE then
begin
CD.OnCustomDraw := FCDValueCustomDraw;
end;
end;
end;
procedure TfrmWatchesView.ConnectWatchValuesCDEvents;
var
CD : TColumnDefinition;
I : Integer;
begin
for I := 0 to FTVPWatchValues.ColumnDefinitions.Count - 1 do
begin
CD := FTVPWatchValues.ColumnDefinitions[I];
if CD.ValuePropertyName = COLUMNNAME_TIMESTAMP then
begin
CD.OnCustomDraw := FCDTimeStampCustomDraw;
CD.OnGetText := FCDTimeStampGetText;
end
else if CD.ValuePropertyName = COLUMNNAME_NAME then
begin
CD.OnCustomDraw := FCDNameCustomDraw;
end
else if CD.ValuePropertyName = COLUMNNAME_VALUETYPE then
begin
CD.OnCustomDraw := FCDTypeCustomDraw;
end
else if CD.ValuePropertyName = COLUMNNAME_VALUE then
begin
CD.OnCustomDraw := FCDValueCustomDraw;
end;
end;
end;
procedure TfrmWatchesView.UpdateActions;
begin
inherited UpdateActions;
pnlMain.PanelCollection[1].Visible := FSettings.WatchHistoryVisible;
end;
procedure TfrmWatchesView.UpdateView(AMessageId: Int64);
begin
FMessageId := AMessageId;
FWatches.Update(AMessageId);
FVSTWatchValues.Invalidate;
FVSTWatchHistory.Invalidate;
end;
procedure TfrmWatchesView.UpdateWatchHistory;
begin
if Assigned(SelectedWatch) then
begin
FTVPWatchHistory.BeginUpdate;
TFactories.InitializeTVP(
FTVPWatchHistory,
FVSTWatchHistory,
SelectedWatch.List as IObjectList,
FWatchHistoryColumnDefinitions
);
ConnectWatchHistoryCDEvents;
FTVPWatchHistory.EndUpdate;
if SelectedWatch.Count > 0 then
begin
FTVPWatchHistory.SelectedItem := SelectedWatch.CurrentWatchValue;
end;
end
else
begin
FVSTWatchHistory.Clear;
end;
end;
{$ENDREGION}
{$REGION 'public methods'}
procedure TfrmWatchesView.GotoFirst;
begin
Logger.Track(Self, 'GotoFirst');
if FVSTWatchValues.Focused then
FTVPWatchValues.View.MoveCurrentToFirst
else if FVSTWatchHistory.Focused then
FTVPWatchHistory.View.MoveCurrentToFirst;
end;
procedure TfrmWatchesView.GotoLast;
begin
Logger.Track(Self, 'GotoLast');
if FVSTWatchValues.Focused then
FTVPWatchValues.View.MoveCurrentToLast
else if FVSTWatchHistory.Focused then
FTVPWatchHistory.View.MoveCurrentToLast;
end;
{ Returns true if this form has the focused control. }
function TfrmWatchesView.HasFocus: Boolean;
begin
Result := Assigned(Screen.ActiveControl)
and (Screen.ActiveControl.Owner = Self);
end;
{$ENDREGION}
end.