2
0

umain.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. unit uMain;
  2. {$mode delphi}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  6. {$IFDEF FPC}
  7. registry,
  8. {$ENDIF}
  9. Quick.Config.Registry,
  10. Generics.Collections;
  11. type
  12. TMyPriority = (msLow, msMed, msHigh);
  13. TWinPos = class
  14. private
  15. fPosX : Integer;
  16. fPosY : Integer;
  17. published
  18. property PosX : Integer read fPosX write fPosX;
  19. property PosY : Integer read fPosY write fPosY;
  20. end;
  21. TProcessType = class
  22. private
  23. fId : Integer;
  24. fPriority : TMyPriority;
  25. fRedundant : Boolean;
  26. published
  27. property Id : Integer read fId write fId;
  28. property Priority : TMyPriority read fPriority write fPriority;
  29. property Redundant : Boolean read fRedundant write fRedundant;
  30. end;
  31. TWorker = class
  32. private
  33. fName : string;
  34. fActive : Boolean;
  35. published
  36. property Name : string read fName write fName;
  37. property Active : Boolean read fActive write fActive;
  38. end;
  39. TMyConfig2 = class(TAppConfigRegistry)
  40. private
  41. fhola : Integer;
  42. published
  43. property hola : Integer read fhola write fhola;
  44. end;
  45. TArraySizes = array of Integer;
  46. TArrayHistory = array of TProcessType;
  47. TMyConfig = class(TAppConfigRegistry)
  48. private
  49. fTitle : string;
  50. fHidden : Boolean;
  51. fSessionName: string;
  52. fSizes : TArraySizes;
  53. fLastFilename : string;
  54. fWindowPos : TWinPos;
  55. fHistory : TArrayHistory;
  56. fComplex : TProcessType;
  57. fModifyDate : TDateTime;
  58. //fWorkList : TObjectList<TWorker>;
  59. public
  60. destructor Destroy; override;
  61. procedure DefaultValues; override;
  62. property Hidden : Boolean read fHidden write fHidden;
  63. published
  64. property Title : string read fTitle write fTitle;
  65. property SessionName : string read fSessionName write fSessionName;
  66. property Sizes : TArraySizes read fSizes write fSizes;
  67. property LastFilename : string read fLastFilename write fLastFilename;
  68. property WindowPos : TWinPos read fWindowPos write fWindowPos;
  69. property History : TArrayHistory read fHistory write fHistory;
  70. property Complex : TProcessType read fComplex write fComplex;
  71. property ModifyDate : TDateTime read fModifyDate write fModifyDate;
  72. //property WorkList : TObjectList<TWorker> read fWorkList write fWorkList;
  73. end;
  74. { TForm1 }
  75. TForm1 = class(TForm)
  76. btnSaveJson: TButton;
  77. btnLoadJson: TButton;
  78. meInfo: TMemo;
  79. procedure btnLoadJsonClick(Sender: TObject);
  80. procedure btnSaveJsonClick(Sender: TObject);
  81. procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  82. procedure FormCreate(Sender: TObject);
  83. private
  84. public
  85. procedure SetConfig(cConfig: TMyConfig);
  86. function TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  87. end;
  88. var
  89. Form1: TForm1;
  90. ConfigReg : TMyConfig;
  91. ConfigTest : TMyConfig;
  92. implementation
  93. {$R *.lfm}
  94. uses
  95. Quick.Json.Serializer;
  96. { TForm1 }
  97. procedure TForm1.btnSaveJsonClick(Sender: TObject);
  98. begin
  99. SetConfig(ConfigReg);
  100. ConfigReg.Save;
  101. meInfo.Lines.Add(ConfigReg.ToJson);
  102. meInfo.Lines.Add('Saved Config in Json at ' + DateTimeToStr(ConfigReg.LastSaved));
  103. end;
  104. procedure TForm1.btnLoadJsonClick(Sender: TObject);
  105. var
  106. NewConfig : TMyConfig;
  107. begin
  108. meInfo.Lines.Add('Load ConfigJson');
  109. NewConfig := TMyConfig.Create(ConfigReg.Provider.HRoot,ConfigReg.Provider.MainKey);
  110. try
  111. NewConfig.Load;
  112. meInfo.Lines.Add(NewConfig.ToJSON);
  113. if TestConfig(ConfigTest,NewConfig) then meInfo.Lines.Add('Test passed successfully!');
  114. finally
  115. NewConfig.Free;
  116. end;
  117. end;
  118. function TForm1.TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  119. var
  120. i : Integer;
  121. begin
  122. try
  123. Assert(cConfig1.LastFilename = cConfig2.LastFilename);
  124. for i := Low(cConfig1.Sizes) to High(cConfig1.Sizes) do
  125. Assert(cConfig1.Sizes[i] = cConfig2.Sizes[i]);
  126. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  127. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  128. Assert(cConfig1.Complex.Priority = cConfig2.Complex.Priority);
  129. Assert(cConfig1.Complex.Redundant = cConfig2.Complex.Redundant);
  130. Assert(cConfig1.Title = cConfig2.Title);
  131. //for i := 0 to cConfig1.WorkList.Count - 1 do
  132. //begin
  133. // Assert(cConfig1.WorkList[i].Name = cConfig2.WorkList[i].Name);
  134. // Assert(cConfig1.WorkList[i].Active = cConfig2.WorkList[i].Active);
  135. //end;
  136. for i := 0 to High(cConfig1.History) do
  137. begin
  138. Assert(cConfig1.History[i].Priority = cConfig2.History[i].Priority);
  139. Assert(cConfig1.History[i].Redundant = cConfig2.History[i].Redundant);
  140. end;
  141. Result := True;
  142. except
  143. ShowMessage('Configuration not has been saved previously or has a corruption problem');
  144. end;
  145. end;
  146. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  147. begin
  148. if Assigned(ConfigTest) then ConfigTest.Free;
  149. if Assigned(ConfigReg) then ConfigReg.Free;
  150. end;
  151. procedure TForm1.FormCreate(Sender: TObject);
  152. begin
  153. ConfigTest := TMyConfig.Create;
  154. SetConfig(ConfigTest);
  155. ConfigReg := TMyConfig.Create(HKEY_CURRENT_USER,'_AppConfig2');
  156. ConfigReg.Provider.CreateIfNotExists := True;
  157. end;
  158. procedure TForm1.SetConfig(cConfig: TMyConfig);
  159. var
  160. processtype : TProcessType;
  161. begin
  162. cConfig.Title := 'hola';
  163. cConfig.SessionName := 'Session01';
  164. cConfig.LastFileName := 'C:\library.txt';
  165. cConfig.Sizes := [1,2,3,4,5,6,7];
  166. cConfig.Complex := TProcessType.Create;
  167. cConfig.Complex.Id := 1;
  168. cConfig.Complex.Redundant := True;
  169. cConfig.Complex.Priority := TMyPriority.msMed;
  170. cConfig.WindowPos := TWinPos.Create;
  171. cConfig.WindowPos.PosX := 100;
  172. cConfig.WindowPos.PosY := 200;
  173. processtype := TProcessType.Create;
  174. processtype.Id := 1;
  175. processtype.Priority := msLow;
  176. processtype.Redundant := True;
  177. cConfig.History := [processtype];
  178. cConfig.ModifyDate := Now();
  179. end;
  180. { TMyConfig }
  181. procedure TMyConfig.DefaultValues;
  182. begin
  183. fTitle := 'Default value';
  184. end;
  185. destructor TMyConfig.Destroy;
  186. begin
  187. //if Assigned(WorkList) then WorkList.Free;
  188. inherited;
  189. end;
  190. end.