Main.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. unit Main;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. Winapi.Messages,
  6. System.SysUtils,
  7. System.Variants,
  8. System.Classes,
  9. Vcl.Graphics,
  10. Vcl.Controls,
  11. Vcl.Forms,
  12. Vcl.Dialogs,
  13. Vcl.StdCtrls,
  14. System.Generics.Collections,
  15. Quick.Config.Registry;
  16. type
  17. TMyPriority = (msLow, msMed, msHigh);
  18. TWinPos = record
  19. public
  20. PosX : Integer;
  21. PosY : Integer;
  22. end;
  23. TProcessType = record
  24. Id : Integer;
  25. Priority : TMyPriority;
  26. Redundant : Boolean;
  27. end;
  28. TWorker = class
  29. private
  30. fName : string;
  31. fActive : Boolean;
  32. published
  33. property Name : string read fName write fName;
  34. property Active : Boolean read fActive write fActive;
  35. end;
  36. TMyConfig = class(TAppConfigRegistry)
  37. private
  38. fTitle : string;
  39. fHidden : Boolean;
  40. fSessionName: string;
  41. fSizes : TArray<Integer>;
  42. fLastFilename : string;
  43. fWindowPos : TWinPos;
  44. fHistory : TArray<TProcessType>;
  45. fComplex : TProcessType;
  46. fModifyDate : TDateTime;
  47. fWorkList : TObjectList<TWorker>;
  48. published
  49. property Sizes : TArray<Integer> read fSizes write fSizes;
  50. property LastFilename : string read fLastFilename write fLastFilename;
  51. property WindowPos : TWinPos read fWindowPos write fWindowPos;
  52. property History : TArray<TProcessType> read fHistory write fHistory;
  53. property Complex : TProcessType read fComplex write fComplex;
  54. property ModifyDate : TDateTime read fModifyDate write fModifyDate;
  55. property Title : string read fTitle write fTitle;
  56. property SessionName : string read fSessionName write fSessionName;
  57. property WorkList : TObjectList<TWorker> read fWorkList write fWorkList;
  58. public
  59. destructor Destroy; override;
  60. procedure Init; override;
  61. procedure DefaultValues; override;
  62. end;
  63. TMainForm = class(TForm)
  64. meInfo: TMemo;
  65. btnLoadRegistry: TButton;
  66. SaveRegistry: TButton;
  67. procedure FormCreate(Sender: TObject);
  68. procedure SaveRegistryClick(Sender: TObject);
  69. procedure btnLoadRegistryClick(Sender: TObject);
  70. procedure SetConfig(cConfig: TMyConfig);
  71. function TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  72. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  73. end;
  74. var
  75. MainForm: TMainForm;
  76. ConfigTest : TMyConfig;
  77. ConfigReg : TMyConfig;
  78. implementation
  79. {$R *.dfm}
  80. procedure TMainForm.btnLoadRegistryClick(Sender: TObject);
  81. begin
  82. meInfo.Lines.Add('Load ConfigReg');
  83. ConfigReg.Load;
  84. meInfo.Lines.Add(ConfigReg.ToJSON);
  85. if TestConfig(configtest,ConfigReg) then meInfo.Lines.Add('Test passed successfully!');
  86. end;
  87. procedure TMainForm.SaveRegistryClick(Sender: TObject);
  88. begin
  89. SetConfig(ConfigReg);
  90. ConfigReg.Save;
  91. meInfo.Lines.Add('Saved Config in Registry at ' + DateTimeToStr(ConfigReg.LastSaved));
  92. end;
  93. procedure TMainForm.SetConfig(cConfig : TMyConfig);
  94. var
  95. winpos : TWinpos;
  96. protype : TProcessType;
  97. i : Integer;
  98. worker : TWorker;
  99. begin
  100. cConfig.LastFilename := 'library.txt';
  101. cConfig.Sizes := [23,11,554,12,34,29,77,30,48,59,773,221,98,3,22,983,122,231,433,12,31,987];
  102. winpos.PosX := 640;
  103. winpos.PosX := 480;
  104. cConfig.WindowPos := winpos;
  105. protype.Priority := msHigh;
  106. protype.Redundant := False;
  107. cConfig.Complex := protype;
  108. cConfig.Title := 'a fresh title';
  109. cConfig.SessionName := 'First Session';
  110. for I := 0 to 22 do
  111. begin
  112. worker := TWorker.Create;
  113. worker.Name := 'Process ' + i.ToString;
  114. worker.Active := Boolean(Random(1));
  115. cConfig.WorkList.Add(worker);
  116. end;
  117. for i := 0 to 15 do
  118. begin
  119. protype.Id := i;
  120. protype.Priority := msLow;
  121. protype.Redundant := True;
  122. cConfig.History := cConfig.History + [protype];
  123. end;
  124. cConfig.ModifyDate := Now();
  125. end;
  126. function TMainForm.TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  127. var
  128. i : Integer;
  129. begin
  130. Result := False;
  131. try
  132. Assert(cConfig1.LastFilename = cConfig2.LastFilename);
  133. for i := Low(cConfig1.Sizes) to High(cConfig1.Sizes) do
  134. Assert(cConfig1.Sizes[i] = cConfig2.Sizes[i]);
  135. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  136. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  137. Assert(cConfig1.Complex.Priority = cConfig2.Complex.Priority);
  138. Assert(cConfig1.Complex.Redundant = cConfig2.Complex.Redundant);
  139. Assert(cConfig1.Title = cConfig2.Title);
  140. for i := 0 to cConfig1.WorkList.Count - 1 do
  141. begin
  142. Assert(cConfig1.WorkList[i].Name = cConfig2.WorkList[i].Name);
  143. Assert(cConfig1.WorkList[i].Active = cConfig2.WorkList[i].Active);
  144. end;
  145. for i := 0 to High(cConfig1.History) do
  146. begin
  147. Assert(cConfig1.History[i].Priority = cConfig2.History[i].Priority);
  148. Assert(cConfig1.History[i].Redundant = cConfig2.History[i].Redundant);
  149. end;
  150. Result := True;
  151. except
  152. ShowMessage('Configuration not has been saved previously or has a corruption problem');
  153. end;
  154. end;
  155. procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
  156. begin
  157. if Assigned(ConfigTest) then ConfigTest.Free;
  158. if Assigned(ConfigReg) then ConfigReg.Free;
  159. end;
  160. procedure TMainForm.FormCreate(Sender: TObject);
  161. begin
  162. ConfigReg := TMyConfig.Create;
  163. ConfigReg.Provider.HRoot := HKEY_CURRENT_USER;
  164. ConfigReg.Provider.MainKey := '_AppConfig';
  165. //create config test to compare later
  166. ConfigTest := TMyConfig.Create;
  167. SetConfig(ConfigTest);
  168. end;
  169. { TMyConfig }
  170. procedure TMyConfig.Init;
  171. begin
  172. inherited;
  173. WorkList := TObjectList<TWorker>.Create(True);
  174. DefaultValues;
  175. end;
  176. procedure TMyConfig.DefaultValues;
  177. begin
  178. inherited;
  179. fTitle := 'Default value';
  180. end;
  181. destructor TMyConfig.Destroy;
  182. begin
  183. if Assigned(WorkList) then WorkList.Free;
  184. inherited;
  185. end;
  186. end.