-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainQuickStat.pas
More file actions
916 lines (854 loc) · 28.7 KB
/
Copy pathMainQuickStat.pas
File metadata and controls
916 lines (854 loc) · 28.7 KB
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
unit MainQuickStat;
interface
uses
QuickStat.Collectors,
QuickStat.Connections,
QuickStat.Selection,
EPR.QA.GUI.Grid.Study,
EPR.VclFrame.Populations,
EPR.PeriodDictionary,
{QA Mtarxi}
EPR.QA.Matrix,
EPR.QA.Matrix.Row,
EPR.QA.Matrix.Interfaces,
EPR.QA.Collector.Standard,
EPR.QA.DataPoint,
EPR.QA.SQL,
{CRF}
CRF.Patient.List,
CRF.Population,
CRF.Population.Interfaces,
CRF.Context.Facade,
{General GUI}
Emetra.VclUtil.Spotlight,
Emetra.VclUtil.Listbox,
Emetra.VclUtil.Settings,
Emetra.VclUtil.Settings.Interfaces,
Emetra.VclUtil.Module.Interfaces,
Emetra.VclForm.EditAndMemo,
Emetra.VclForm.Period,
{General classes}
Emetra.Adapters.Office,
Emetra.Database.Simple,
Emetra.Database.ParameterDictionary,
Emetra.Settings.Test,
Emetra.Settings.IniFile,
Emetra.StrUtils,
Emetra.Win.Launcher,
Emetra.Win.User,
Emetra.Classes.Transient,
Emetra.VclUtil.ArenaColors,
{General interfaces}
Emetra.Database.Interfaces,
Emetra.Logging.Interfaces,
Emetra.Progress.Interfaces,
Emetra.Settings.Interfaces,
Emetra.Dictionary.Interfaces,
{Third party}
RzPanel, RzSplit, RzTabs, RzStatus,
{Standard}
Winapi.Windows, Winapi.Messages,
System.Contnrs, System.Generics.Collections, System.SysUtils, System.Variants, System.Classes, System.Actions,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.Grids, Vcl.ComCtrls, Vcl.ExtCtrls,
Vcl.ImgList, Vcl.CheckLst, Vcl.ActnList, Vcl.ToolWin, Vcl.ActnMan, Vcl.ActnCtrls, Vcl.PlatformDefaultStyleActnCtrls,
Vcl.Menus, System.ImageList;
type
TfrmQuickStat = class( TForm, IStatus, IProgress, ILoginObserver, IPopulationObserver )
actCollectData: TAction;
actDeletePackage: TAction;
actExportData: TAction;
ActionManager1: TActionManager;
actSaveDataPackage: TAction;
actSaveDataset: TAction;
actSavePatientSelection: TAction;
btnCollectData: TSpeedButton;
cbDataCollector: TCheckListBox;
cbExportDates: TCheckBox;
cbProject: TComboBox;
cbShowDataHint: TCheckBox;
cbWideColumns: TCheckBox;
Deletethispackage1: TMenuItem;
edtPackageFilter: TEdit;
FileSaveDialog1: TFileSaveDialog;
hdrDatabase: TLabel;
hdrElements: TLabel;
hdrExportOptions: TLabel;
hdrPackages: TLabel;
hdrPopulation: TLabel;
hdrPopulationName: TLabel;
imgAppIcon: TImage;
imgzFolderList: TImageList;
lblAppName: TLabel;
lblDataElementInfo: TLabel;
lblDataHint: TLabel;
lblHintPopulation: TLabel;
lblInfo: TLabel;
lblProgress: TLabel;
lbPackagedGrids: TListBox;
lstActiveImages: TImageList;
lstDisabledImages: TImageList;
mnuExportToExcel: TMenuItem;
mnuGridPopup: TPopupMenu;
mnuPackagePopup: TPopupMenu;
mnuSaveDataPackage: TMenuItem;
mnuSaveDataset: TMenuItem;
N1: TMenuItem;
Panel1: TPanel;
panExportSettings: TPanel;
panGrid: TPanel;
panHdrDatabase: TPanel;
panHdrElements: TPanel;
panHdrExportOptions: TPanel;
panHdrPackages: TPanel;
panHdrPopulation: TPanel;
panHdrYourDataset: TPanel;
panHint: TPanel;
panPopulation: TPanel;
panProgress: TPanel;
panSettings: TPanel;
panWhiteTop: TPanel;
pbProgress: TProgressBar;
pgDataset: TRzPageControl;
pgSelections: TRzPageControl;
RzVersionInfo1: TRzVersionInfo;
RzVersionInfoStatus1: TRzVersionInfoStatus;
splMain: TRzSplitter;
tbrPackages: TToolBar;
tbsDataElements: TRzTabSheet;
tbsOverview: TRzTabSheet;
tbsPackages: TRzTabSheet;
tbsPopulation: TRzTabSheet;
tbsTimeSeries: TRzTabSheet;
ToolButton1: TToolButton;
rbRandomisePids: TRadioButton;
rbKeepPids: TRadioButton;
rbFullIdentification: TRadioButton;
procedure FormCreate( Sender: TObject );
procedure FormDestroy( Sender: TObject );
procedure FormShow( Sender: TObject );
procedure FormClose( Sender: TObject; var Action: TCloseAction );
procedure actCollectDataExecute( Sender: TObject );
procedure actExportToExcelExecute( Sender: TObject );
procedure actSaveDataPackageExecute( Sender: TObject );
procedure actSavePatientSelectionExecute( Sender: TObject );
procedure actSaveDatasetToCsvExecute( Sender: TObject );
procedure cbWideColumnsChecked( Sender: TObject );
procedure actDeletePackageExecute( Sender: TObject );
strict private
{ Private declarations }
fConnection: TQuickStatConnection;
fQuickStatConnections: TConnectionList;
fCrfContext: TCRFSimpleContext;
fPersonList: TPatientList;
fGridPopulation: IPopulation;
fPackagedQuickStatGrids: TObjectList;
fQuickStat: TQuickStatCollectors;
fSettings: IScopedSettingsReadWrite;
fFilesThatMustBeDeleted: TStringList;
{ GUI }
fSelContext: TSpotLightContext;
frmPopulations: TfrmPopulations;
fGrid: TStudyOverviewGrid;
fListBoxPainter: TCustomListControlPainter;
fGuiSettings: IGuiSettings;
{ Key event handlers }
procedure AddCaptions;
procedure PreparePackagedSelection( Sender: TObject );
procedure SelectConnection( Sender: TObject );
procedure ToggleGridAnonymity( Sender: TObject );
procedure UpdateDataHintPanel( Sender: TObject );
procedure ValidateCollectorSelection( Sender: TObject );
procedure ApplyColors;
procedure UpdateGridInfo;
private
{ Other members }
function GetTemporaryCsvFileName: string;
function PersonGridIdentification: TPersonIdentification;
function TryFindCollector( const ACollectorName: string; out AFoundAt: integer ): boolean;
procedure LoadPopulationIntoGrid( APopulation: IPopulation );
procedure LoadPackagedSelections( Sender: TObject );
protected
{ ILoginObserver }
function FriendlyName: string;
procedure AfterLogin( Sender: IDatabaseConnection );
{ IPopulationObserver }
procedure AfterPopulationSelect( APopulation: IPopulation );
{ IProgress }
function GetInfo: string;
procedure Done;
procedure SetProgress( const AProgressPercent: double );
procedure SetInfo( const s: string );
procedure SetHeader( const s: string );
public
{ Public declarations }
end;
var
frmQuickStat: TfrmQuickStat;
implementation
uses
EPR.QA.Collector.Drug,
EPR.QA.CaptionRecord,
Spring,
Math, Db;
resourcestring
MSG_UNKNOWN_POPULATION =
{ } 'The selection is based on an unknown population (ProcId=%d).\n' +
{ } 'The data collection can not be performed at this time.\n' +
{ } 'Perhaps the population is from a different protocol?';
MSG_UNKNOWN_COLLECTOR =
{ } 'The selection contains an unknown data element.\n' +
{ } 'Element name was "%s".\n' +
{ } 'The data collection will be incomplete.\n' +
{ } 'Perhaps the selection was created in a later version?';
TXT_SAVE_SPEC = 'Save specification';
TXT_SAVE_SELECTION = 'Save selection';
TXT_TASK_COMPLETED = 'Task completed';
MSG_SAVE_SUCCESSFUL = 'Selection was successfully saved.';
WARN_SAVE_FAILED = 'There was a problem:\n%s';
WARN_NO_PACKAGE_SELECTED = 'You need to select a package for this operation.';
CONFIRM_DELETE_PACKAGE = 'Do you really want to delete this package:\n"%s"?';
TXT_PROJECT_SELECTED = 'New project selected';
TXT_CONNECTING = 'Connecting to %s ...';
MSG_NO_POPULATION = 'No population selected!';
ERR_POPULATION_NOT_SELECTED =
{ } 'The population was not selected as expected,\n' +
{ } 'You may need an updated version of QuickStat.';
ERR_CONFIG_FILE_MISSING =
{ } 'The configuration file %s was not found.\n' +
{ } 'QuickStat can not be used without this file.';
resourcestring
{ Gui elements in population frame }
rsFilterSearchText = 'Filter / search text';
rsFrequentlyUsedOnly = 'Frequently used only';
rsSimplifiedView = 'Simplified';
rsFilterTextHint = 'Type filter text here';
rsGridInfo = 'Population: %d "%s". Grid size: %d x %d';
const
COL_WIDTH = 64;
{$R *.dfm}
{$REGION 'Initialize Form'}
procedure TfrmQuickStat.FormCreate( Sender: TObject );
const
PROC_NAME = 'FormCreate';
begin
TDrugCollector.GroupResults := false;
GlobalLog.Enabled := true;
GlobalLog.LogCallStack := true;
GlobalLog.EnterMethod( Self, PROC_NAME );
try
fSettings := TIniSettings.Create;
fGuiSettings := TGuiSettings.Create( Self, fSettings, GlobalLog );
{ Prepare key business objects }
fFilesThatMustBeDeleted := TStringList.Create;
fPackagedQuickStatGrids := TObjectList.Create;
fQuickStatConnections := TConnectionList.Create( [doOwnsValues] );
fCrfContext := TCRFSimpleContext.Create( GlobalLog );
fCrfContext.Database.LogSql := true;
fCrfContext.Database.AddLoginObserver( Self );
fPersonList := TPatientList.Create( fCrfContext, TParameterDictionary.Create( TPeriodDictionary.Create( fSettings, GlobalLog ), fCrfContext, GlobalLog ), fCrfContext.Database, GlobalLog );
{ Prepare population view }
fQuickStat := TQuickStatCollectors.Create( fCrfContext.Database, Self, GlobalLog );
{ Create grid view }
fGrid := TStudyOverviewGrid.Create( nil, fPersonList, Self, fCrfContext.Database, GlobalLog );
fGrid.OnGetColor := fQuickStat.ProvideColor;
fGrid.Prepare( panGrid, alClient );
fGrid.DataColWidth := COL_WIDTH;
fGrid.PopupMenu := mnuGridPopup;
fGrid.Anonymous := ( rbRandomisePids.Checked or rbKeepPids.Checked );
{ Move on top of the grid }
panHint.Parent := panGrid;
panHint.Visible := false;
{ Create population view }
frmPopulations := TfrmPopulations.Create( nil, fCrfContext.DatabaseInfo, fSettings, fCrfContext.Database, GlobalLog );
frmPopulations.Prepare( panPopulation, alClient );
frmPopulations.AddPopulationObserver( Self );
frmPopulations.edtPopFilter.TextHint := rsFilterTextHint;
frmPopulations.lblFilterHeader.Caption := rsFilterSearchText;
frmPopulations.cbShowCommon.Caption := rsFrequentlyUsedOnly;
frmPopulations.cbSimpleView.Caption := rsSimplifiedView;
fCrfContext.Session.AddStudyObserver( frmPopulations );
{ Custom painting / handling of saved selections }
fSelContext := TSpotLightContext.Create( lbPackagedGrids, fPackagedQuickStatGrids, edtPackageFilter, nil, nil, GlobalLog );
fListBoxPainter := TCustomListControlPainter.Create( Self );
fListBoxPainter.Attach( lbPackagedGrids );
{ Final preparation of GUI }
tbsDataElements.TabVisible := false;
tbsDataElements.Enabled := false;
pgSelections.ActivePageIndex := 0;
pgDataset.ActivePageIndex := 0;
pbProgress.Max := 100;
{ Wire up events to gui }
cbProject.OnChange := SelectConnection;
rbRandomisePids.OnClick := ToggleGridAnonymity;
rbKeepPids.OnClick := ToggleGridAnonymity;
rbFullIdentification.OnClick := ToggleGridAnonymity;
cbDataCollector.OnClickCheck := ValidateCollectorSelection; { Check box click triggers enable/disable button }
cbShowDataHint.OnClick := UpdateDataHintPanel;
fGrid.OnClick := UpdateDataHintPanel; { Moving around in grid triggers update hint view }
lbPackagedGrids.OnDblClick := PreparePackagedSelection; { Double click on packaged selection activates in }
finally
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
procedure TfrmQuickStat.FormDestroy( Sender: TObject );
const
PROC_NAME = 'FormDestroy';
begin
GlobalLog.EnterMethod( Self, PROC_NAME );
try
fCrfContext.Disconnect;
fListBoxPainter.Detach( lbPackagedGrids );
while ( fFilesThatMustBeDeleted.Count > 0 ) do
begin
try
System.SysUtils.DeleteFile( fFilesThatMustBeDeleted[0] );
except
on E: Exception do
GlobalLog.Event( E.Message );
end;
fFilesThatMustBeDeleted.Delete( 0 );
end;
cbDataCollector.Clear;
FreeAndNil( fFilesThatMustBeDeleted );
FreeAndNil( fQuickStatConnections );
FreeAndNil( fPackagedQuickStatGrids );
FreeAndNil( fGrid );
FreeAndNil( fQuickStat );
FreeAndNil( frmPopulations );
FreeAndNil( fPersonList );
FreeAndNil( fCrfContext );
FreeAndNil( fSelContext );
finally
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
procedure TfrmQuickStat.ApplyColors;
const
FONT_NAME = 'Calibri';
FONT_SIZE = 9;
begin
pgSelections.BackgroundColor := Self.Color;
pgDataset.BackgroundColor := Self.Color;
// exit;
TArenaColors.StyleForm( Self );
TArenaColors.StyleTabs( pgSelections );
TArenaColors.StyleTabs( pgDataset );
TArenaColors.StyleFrame( frmPopulations );
lblProgress.Font.Name := FONT_NAME;
lblProgress.Font.Size := FONT_SIZE + 1;
{ Set up panel }
TArenaColors.StyleHeaderPanel( panHdrDatabase );
TArenaColors.StyleHeaderPanel( panHdrPopulation );
TArenaColors.StyleHeaderPanel( panHdrYourDataset );
TArenaColors.StyleHeaderPanel( panHdrElements );
TArenaColors.StyleHeaderPanel( panHdrPackages );
TArenaColors.StyleHeaderPanel( panHdrExportOptions );
TArenaColors.StyleListView( frmPopulations.ListView );
TArenaColors.StyleSimpleCheckbox( frmPopulations.cbSimpleView );
splMain.Color := clMyGreenColor;
fGrid.FixedColor := clMyGreenColor;
fGrid.FixedFontColor := clMenuBackgroundDarkBrush;
cbWideColumns.Font.Size := FONT_SIZE;
cbWideColumns.Top := hdrPopulationName.Top;
cbWideColumns.Left := hdrPopulationName.Width - cbWideColumns.Width;
end;
procedure TfrmQuickStat.FormShow( Sender: TObject );
const
PROC_NAME = 'FormShow';
var
configFileName: string;
begin
GlobalLog.EnterMethod( Self, PROC_NAME );
try
fGuiSettings.RestoreFormState;
configFileName := ChangeFileExt( ParamStr( 0 ), '.config.xml' );
if FileExists( configFileName ) then
begin
fQuickStatConnections.Load( configFileName );
fQuickStatConnections.AddToStrings( cbProject.Items );
end
else
GlobalLog.Event( ERR_CONFIG_FILE_MISSING, [ExtractFileName( configFileName )], ltException );
cbProject.Sorted := true;
cbDataCollector.Sorted := true;
Application.CreateForm( TfrmSaveSpec, frmSaveSpec );
ApplyColors;
finally
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
procedure TfrmQuickStat.FormClose( Sender: TObject; var Action: TCloseAction );
begin
fGuiSettings.SaveFormState;
end;
function TfrmQuickStat.FriendlyName: string;
begin
Result := Caption;
end;
{$ENDREGION}
{$REGION 'IProgress'}
function TfrmQuickStat.GetInfo: string;
begin
Result := lblInfo.Caption;
end;
procedure TfrmQuickStat.Done;
begin
pbProgress.Position := 100;
pbProgress.State := pbsNormal;
SetInfo( TXT_TASK_COMPLETED );
end;
procedure TfrmQuickStat.SetHeader( const s: string );
begin
lblProgress.Caption := s;
end;
procedure TfrmQuickStat.SetInfo( const s: string );
begin
lblInfo.Caption := s;
lblInfo.Update;
end;
procedure TfrmQuickStat.SetProgress( const AProgressPercent: double );
begin
pbProgress.State := pbsNormal;
pbProgress.Position := round( AProgressPercent );
end;
{$ENDREGION}
{$REGION 'Callback for observer methods'}
procedure TfrmQuickStat.AddCaptions;
begin
{ Add captions }
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUID.RED', 'DDI-R', 'Drug-Drug interactions, red level' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUID.YELLOW', 'DDI-Y', 'Drug-Drug interactions, yellow level' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUID.ORANGE', 'DDI-O', 'Drug-Drug interactions, orange level' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUID.GREEN', 'DDI-G', 'Drug-Drug interactions, green level' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUG.F', 'Regular' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUG.B', 'AsNeeded' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUG.U', 'Weekly' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUG.X', 'Unspec' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUG.K', 'Cure' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUG.NOATC', 'NoAtc' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUG.RESISTANCE_DRIVING', 'Resist', 'Resistance-driving antibiotics' ) );
fGrid.Data.AddCaption( TCaptionRecord.Create( 'DRUG.METFORMIN', 'Metform', 'Metformin' ) );
fGrid.Data.LoadCaptions( true, false );
end;
procedure TfrmQuickStat.AfterLogin( Sender: IDatabaseConnection );
const
PROC_NAME = 'AfterLogin';
var
n: integer;
begin
GlobalLog.EnterMethod( Self, PROC_NAME );
try
fGrid.Data.PrepareStudy( fCrfContext.StudyName );
fQuickStat.PrepareStudy( fCrfContext );
cbDataCollector.Clear;
n := 0;
while n < fQuickStat.Collectors.Count do
begin
cbDataCollector.Items.AddObject( fQuickStat.Collectors[n].Title, fQuickStat.Collectors[n] );
inc( n );
end;
LoadPackagedSelections( Self );
ValidateCollectorSelection( Self );
finally
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
procedure TfrmQuickStat.SelectConnection( Sender: TObject );
begin
Screen.Cursor := crSqlWait;
try
Application.ProcessMessages;
SetInfo( TXT_PROJECT_SELECTED );
if fCrfContext.Connected then
fCrfContext.Disconnect;
{ Retrieve the connection object from combobox }
with cbProject do
if ItemIndex = -1 then
fConnection := nil
else
fConnection := Items.Objects[ItemIndex] as TQuickStatConnection;
if Assigned( fConnection ) then
try
SetInfo( Format( TXT_CONNECTING, [fConnection.Name] ) );
fCrfContext.Connect( fConnection.StudyName, fConnection.ConnectionString );
finally
Done;
end;
finally
Screen.Cursor := crDefault;
end;
end;
procedure TfrmQuickStat.AfterPopulationSelect( APopulation: IPopulation );
const
PROC_NAME = 'AfterPopulationSelect';
var
crSaved: TCursor;
begin
crSaved := Screen.Cursor;
Screen.Cursor := crSqlWait;
GlobalLog.EnterMethod( Self, PROC_NAME );
try
pgDataset.ActivePage := tbsOverview;
fGrid.Clear;
if frmPopulations.IsHighlighted( APopulation ) then
begin
fGridPopulation := nil;
fPersonList.Load( APopulation );
if not fPersonList.IncludesNationalId then
fPersonList.AddNationalIds;
LoadPopulationIntoGrid( APopulation );
pgSelections.ActivePage := tbsDataElements;
fGrid.StartPainting;
end
else
GlobalLog.Event( ERR_POPULATION_NOT_SELECTED, ltError );
finally
Screen.Cursor := crSaved;
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
{$ENDREGION}
procedure TfrmQuickStat.LoadPopulationIntoGrid( APopulation: IPopulation );
const
PROC_NAME = 'LoadPopulationIntoGrid';
begin
GlobalLog.EnterMethod( Self, PROC_NAME );
try
if not Assigned( APopulation ) then
GlobalLog.Event( MSG_NO_POPULATION, ltMessage )
else
begin
fGrid.Data.ClearPopulation;
fGrid.Data.SortBy := sbPersonId;
fGrid.Data.PreparePopulation( fPersonList );
fGridPopulation := APopulation;
tbsDataElements.TabVisible := fGrid.Data.DataRows > 0;
tbsDataElements.Enabled := tbsDataElements.TabVisible;
UpdateGridInfo;
end;
finally
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
procedure TfrmQuickStat.UpdateDataHintPanel( Sender: TObject );
var
strHint: string;
thisPatient: TPersonGridRow;
thisDatapoint: TDataPoint;
panRect: TRect;
begin
panHint.Visible := false;
if cbShowDataHint.Checked then
try
if fGrid.Data.TryGetPatientAtRow( fGrid.Row, thisPatient ) then
begin
if fGrid.Anonymous then
strHint := Format( 'PersonId = %d', [thisPatient.PersonId] )
else
strHint := thisPatient.FullName;
if fGrid.Data.TryGetDatapoint( fGrid.Col, fGrid.Row, thisDatapoint ) then
begin
strHint := strHint + sLineBreak + thisDatapoint.AsString;
lblDataHint.Caption := strHint;
panRect := fGrid.CellRect( fGrid.Col, fGrid.Row );
OffsetRect( panRect, 3, 3 );
panHint.Top := fGrid.Top + panRect.Top + fGrid.DefaultRowHeight + 1;
panHint.Left := fGrid.Left + panRect.Left;
panHint.ClientHeight := ( 8 * abs( lblDataHint.Font.Height ) + panHint.BorderWidth * 2 + 8 );
panHint.Visible := true;
panHint.BringToFront;
end;
end;
except
on E: Exception do
begin
lblInfo.Font.Color := clRed;
lblInfo.Caption := E.Message;
end;
end;
end;
procedure TfrmQuickStat.UpdateGridInfo;
begin
hdrPopulationName.Caption := Format( rsGridInfo, [fGridPopulation.ProcId, fGridPopulation.Title, fGrid.Data.DataRows, fGrid.Data.FieldCount] );
end;
function TfrmQuickStat.GetTemporaryCsvFileName: string;
begin
Result := GetTempDir + GetNewStrippedGuid + '.csv';
fFilesThatMustBeDeleted.Add( Result );
end;
procedure TfrmQuickStat.cbWideColumnsChecked( Sender: TObject );
begin
if cbWideColumns.Checked then
fGrid.DataColWidth := 120
else
fGrid.DataColWidth := COL_WIDTH;
end;
procedure TfrmQuickStat.actCollectDataExecute( Sender: TObject );
const
PROC_NAME = 'actCollectDataExecute';
var
n: integer;
selectedCollector: IGridDataCollector;
crSaved: TCursor;
savedTopIndex: integer;
savedItemIndex: integer;
begin
GlobalLog.EnterMethod( Self, PROC_NAME );
crSaved := Screen.Cursor;
try
Screen.Cursor := crSqlWait;
fGrid.Data.ClearVariables;
AddCaptions;
with cbDataCollector do
begin
{ Save Scroll info }
savedTopIndex := TopIndex;
savedItemIndex := ItemIndex;
n := 0;
{ Loop through every collector and add data for those that are checked }
while n < Items.Count do
begin
Update;
if Checked[n] then
begin
ItemIndex := n;
if Supports( Items.Objects[n], IGridDataCollector, selectedCollector ) then
begin
SetInfo( selectedCollector.Title );
fGrid.Data.AddData( selectedCollector );
end;
end;
inc( n );
end;
actExportData.Enabled := fGrid.Data.HasData;
{ Restore scroll status }
TopIndex := savedTopIndex;
ItemIndex := savedItemIndex;
end;
fGrid.Lock;
finally
UpdateGridInfo;
Done;
Screen.Cursor := crSaved;
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
procedure TfrmQuickStat.ToggleGridAnonymity( Sender: TObject );
begin
fGrid.Anonymous := not( rbFullIdentification.Checked );
end;
procedure TfrmQuickStat.ValidateCollectorSelection( Sender: TObject );
const
PROC_NAME = 'CheckValidSelection';
var
collectorIndex: integer;
begin
GlobalLog.EnterMethod( Self, PROC_NAME );
try
actCollectData.Enabled := false;
actSaveDataPackage.Enabled := false;
collectorIndex := 0;
while collectorIndex < cbDataCollector.Count do
begin
if cbDataCollector.Checked[collectorIndex] then
begin
actCollectData.Enabled := true;
actSaveDataPackage.Enabled := true;
break;
end;
inc( collectorIndex );
end;
finally
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
function TfrmQuickStat.TryFindCollector( const ACollectorName: string; out AFoundAt: integer ): boolean;
var
dataCollector: IGridDataCollector;
begin
AFoundAt := 0;
Result := false;
while AFoundAt < cbDataCollector.Items.Count do
begin
if Supports( cbDataCollector.Items.Objects[AFoundAt], IGridDataCollector, dataCollector ) then
if SameText( ACollectorName, dataCollector.Name ) or SameText( ACollectorName, dataCollector.Title ) then
begin
Result := true;
break;
end;
inc( AFoundAt );
end;
end;
procedure TfrmQuickStat.actSavePatientSelectionExecute( Sender: TObject );
begin
frmSaveSpec.SetHeader( TXT_SAVE_SELECTION );
if frmSaveSpec.ShowModal = mrOk then
try
fGrid.Data.SaveToSelection( frmSaveSpec.Title, frmSaveSpec.Comment );
GlobalLog.Event( MSG_SAVE_SUCCESSFUL, ltMessage );
except
on E: Exception do
GlobalLog.Event( WARN_SAVE_FAILED, [E.Message], ltWarning );
end;
end;
{$REGION 'Save and export actions' }
procedure TfrmQuickStat.actExportToExcelExecute( Sender: TObject );
var
fileName: string;
begin
fileName := Self.GetTemporaryCsvFileName;
fGrid.SaveToFile( fileName, PersonGridIdentification, cbExportDates.Checked );
TExcelAdapter.LoadWithFile( fileName, GlobalLog );
end;
procedure TfrmQuickStat.actSaveDatasetToCsvExecute( Sender: TObject );
var
fileName: string;
begin
if FileSaveDialog1.Execute then
begin
fileName := FileSaveDialog1.fileName;
fGrid.SaveToFile( fileName, PersonGridIdentification, cbExportDates.Checked );
end;
end;
{$ENDREGION}
{$REGION 'Packaged selections'}
procedure TfrmQuickStat.PreparePackagedSelection( Sender: TObject );
const
PROC_NAME = 'PrepareSavedSelection';
var
n: integer;
packagedSelection: TPackagedSelection;
selectedPopulation: IPopulation;
foundAt: integer;
begin
GlobalLog.EnterMethod( Self, PROC_NAME );
try
with lbPackagedGrids do
begin
if ItemIndex <> -1 then
begin
{ First find the correct population from selection and load it }
packagedSelection := Items.Objects[ItemIndex] as TPackagedSelection;
if not frmPopulations.TrySelect( packagedSelection.PopulationId, true, selectedPopulation ) then
GlobalLog.Event( MSG_UNKNOWN_POPULATION, [packagedSelection.PopulationId], ltWarning )
else
begin
{ Load transfer population to grid }
LoadPopulationIntoGrid( selectedPopulation );
{ Make the selection in the listbox, based on name }
cbDataCollector.CheckAll( cbUnchecked );
n := 0;
while n < packagedSelection.CollectorCount do
begin
if TryFindCollector( packagedSelection.Collector[n], foundAt ) then
cbDataCollector.Checked[foundAt] := true
else
GlobalLog.Event( MSG_UNKNOWN_COLLECTOR, [packagedSelection.Collector[n]], ltWarning );
inc( n );
end;
actCollectDataExecute( Self );
hdrPopulationName.Caption := packagedSelection.Title;
end;
end;
end;
finally
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
procedure TfrmQuickStat.LoadPackagedSelections( Sender: TObject );
const
PROC_NAME = 'LoadPackagedSelections';
var
packagedSelection: TPackagedSelection;
thisDataset: TDataset;
begin
GlobalLog.EnterMethod( Self, Format( '%s("%s")', [PROC_NAME, fCrfContext.StudyName] ) );
try
fPackagedQuickStatGrids.Clear;
thisDataset := fCrfContext.Database.FastQuery( QRY_GET_PACKAGES, [fGrid.Data.StudyId] );
with thisDataset do
try
while not EOF do
begin
packagedSelection := TPackagedSelection.Create;
packagedSelection.Load( thisDataset );
fPackagedQuickStatGrids.Add( packagedSelection );
Next;
end;
fSelContext.RefreshList( Self );
finally
Close;
end;
finally
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
end;
procedure TfrmQuickStat.actSaveDataPackageExecute( Sender: TObject );
const
PROC_NAME = 'actSaveExecute';
var
collectorIndex: integer;
collectorName: string;
collectorNameList: TStringList;
dataCollector: IGridDataCollector;
dataElementSelection: TPackagedSelection;
begin
GlobalLog.EnterMethod( Self, PROC_NAME );
collectorNameList := TStringList.Create;
try
Guard.CheckNotNull( fGridPopulation, 'GridPopulation' );
collectorIndex := 0;
while collectorIndex < cbDataCollector.Count do
begin
if cbDataCollector.Checked[collectorIndex] and Supports( cbDataCollector.Items.Objects[collectorIndex], IGridDataCollector, dataCollector ) then
collectorNameList.Add( dataCollector.Name );
inc( collectorIndex );
end;
frmSaveSpec.Clear;
frmSaveSpec.SetHeader( TXT_SAVE_SPEC );
if frmSaveSpec.ShowModal = mrOk then
begin
dataElementSelection := TPackagedSelection.Create( fGrid.Data.StudyId, fGridPopulation.ProcId, frmSaveSpec.Title, frmSaveSpec.Comment );
for collectorName in collectorNameList do
dataElementSelection.AddCollector( collectorName );
fPackagedQuickStatGrids.Add( dataElementSelection );
dataElementSelection.Save( fCrfContext.Database );
fSelContext.RefreshList( Self );
end;
finally
collectorNameList.Free;
end;
GlobalLog.LeaveMethod( Self, PROC_NAME );
end;
procedure TfrmQuickStat.actDeletePackageExecute( Sender: TObject );
var
packagedSelection: TPackagedSelection;
begin
with lbPackagedGrids do
begin
if ItemIndex = -1 then
GlobalLog.Event( WARN_NO_PACKAGE_SELECTED, ltWarning )
else
begin
packagedSelection := Items.Objects[ItemIndex] as TPackagedSelection;
if GlobalLog.LogYesNo( Format( CONFIRM_DELETE_PACKAGE, [packagedSelection.Title] ), ltWarning ) then
begin
packagedSelection.Delete( fCrfContext.Database );
Items.Delete( ItemIndex );
end;
end;
end;
end;
{$ENDREGION}
function TfrmQuickStat.PersonGridIdentification: TPersonIdentification;
begin
if rbFullIdentification.Checked then
Result := pgiFull
else if rbKeepPids.Checked then
Result := pgiPersonIdOnly
else if rbRandomisePids.Checked then
Result := pgiRandomPersonId
else
raise EAbort.Create( 'Unhandled identification strategy.' );
end;
end.