umain.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. unit uMain;
  2. {$mode delphi}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  6. Quick.Config,
  7. Quick.Config.Provider.Registry,
  8. Generics.Collections,
  9. registry,
  10. Quick.Config.Provider.Json;
  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(TAppConfig)
  40. private
  41. fhola : Integer;
  42. published
  43. property hola : Integer read fhola write fhola;
  44. end;
  45. TMyConfig = class(TAppConfig)
  46. private
  47. fTitle : string;
  48. fHidden : Boolean;
  49. fSessionName: string;
  50. fSizes : TArray<Integer>;
  51. fLastFilename : string;
  52. fWindowPos : TWinPos;
  53. fHistory : TArray<TProcessType>;
  54. fComplex : TProcessType;
  55. fModifyDate : TDateTime;
  56. fWorkList : TObjectList<TWorker>;
  57. public
  58. constructor Create; override;
  59. destructor Destroy; override;
  60. procedure DefaultValues;
  61. property Hidden : Boolean read fHidden write fHidden;
  62. published
  63. property Title : string read fTitle write fTitle;
  64. property SessionName : string read fSessionName write fSessionName;
  65. property Sizes : TArray<Integer> read fSizes write fSizes;
  66. property LastFilename : string read fLastFilename write fLastFilename;
  67. //property WindowPos : TWinPos read fWindowPos write fWindowPos;
  68. property History : TArray<TProcessType> read fHistory write fHistory;
  69. property Complex : TProcessType read fComplex write fComplex;
  70. property ModifyDate : TDateTime read fModifyDate write fModifyDate;
  71. //property WorkList : TObjectList<TWorker> read fWorkList write fWorkList;
  72. end;
  73. { TForm1 }
  74. TForm1 = class(TForm)
  75. btnLoadRegistry: TButton;
  76. btnSaveJson: TButton;
  77. btnLoadJson: TButton;
  78. btnSaveRegistry: TButton;
  79. meInfo: TMemo;
  80. procedure btnLoadJsonClick(Sender: TObject);
  81. procedure btnLoadRegistryClick(Sender: TObject);
  82. procedure btnSaveJsonClick(Sender: TObject);
  83. procedure btnSaveRegistryClick(Sender: TObject);
  84. procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  85. procedure FormCreate(Sender: TObject);
  86. private
  87. public
  88. procedure SetConfig(cConfig: TMyConfig);
  89. function TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  90. end;
  91. var
  92. Form1: TForm1;
  93. ConfigJson : TMyConfig;
  94. ConfigReg : TMyConfig;
  95. ConfigTest : TMyConfig;
  96. AppConfigJson : TAppConfigJsonProvider<TMyConfig>;
  97. AppConfigReg : TAppConfigRegistryProvider<TMyConfig>;
  98. implementation
  99. {$R *.lfm}
  100. uses
  101. Quick.Json.Serializer;
  102. { TForm1 }
  103. procedure TForm1.btnSaveJsonClick(Sender: TObject);
  104. begin
  105. SetConfig(ConfigJson);
  106. AppConfigJson.Save(ConfigJson);
  107. meInfo.Lines.Add(ConfigJson.ToJson);
  108. meInfo.Lines.Add('Saved Config in Json at ' + DateTimeToStr(ConfigJson.LastSaved));
  109. end;
  110. procedure TForm1.btnSaveRegistryClick(Sender: TObject);
  111. begin
  112. SetConfig(ConfigJson);
  113. AppConfigReg.Save(ConfigJson);
  114. meInfo.Lines.Add(ConfigJson.ToJson);
  115. meInfo.Lines.Add('Saved Config in Registry at ' + DateTimeToStr(ConfigJson.LastSaved));
  116. end;
  117. procedure TForm1.btnLoadJsonClick(Sender: TObject);
  118. begin
  119. meInfo.Lines.Add('Load ConfigJson');
  120. ConfigJson := TMyConfig.Create;
  121. AppConfigJson.Load(ConfigJson);
  122. meInfo.Lines.Add(ConfigJson.ToJSON);
  123. if TestConfig(ConfigTest,ConfigJson) then meInfo.Lines.Add('Test passed successfully!');
  124. end;
  125. procedure TForm1.btnLoadRegistryClick(Sender: TObject);
  126. begin
  127. meInfo.Lines.Add('Load ConfigRegistry');
  128. ConfigJson := TMyConfig.Create;
  129. AppConfigReg.Load(ConfigJson);
  130. meInfo.Lines.Add(ConfigJson.ToJSON);
  131. if TestConfig(ConfigTest,ConfigJson) then meInfo.Lines.Add('Test passed successfully!');
  132. end;
  133. function TForm1.TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  134. var
  135. i : Integer;
  136. begin
  137. try
  138. Assert(cConfig1.LastFilename = cConfig2.LastFilename);
  139. for i := Low(cConfig1.Sizes) to High(cConfig1.Sizes) do
  140. Assert(cConfig1.Sizes[i] = cConfig2.Sizes[i]);
  141. //Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  142. //Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  143. Assert(cConfig1.Complex.Priority = cConfig2.Complex.Priority);
  144. Assert(cConfig1.Complex.Redundant = cConfig2.Complex.Redundant);
  145. Assert(cConfig1.Title = cConfig2.Title);
  146. //for i := 0 to cConfig1.WorkList.Count - 1 do
  147. //begin
  148. // Assert(cConfig1.WorkList[i].Name = cConfig2.WorkList[i].Name);
  149. // Assert(cConfig1.WorkList[i].Active = cConfig2.WorkList[i].Active);
  150. //end;
  151. for i := 0 to High(cConfig1.History) do
  152. begin
  153. Assert(cConfig1.History[i].Priority = cConfig2.History[i].Priority);
  154. Assert(cConfig1.History[i].Redundant = cConfig2.History[i].Redundant);
  155. end;
  156. Result := True;
  157. except
  158. ShowMessage('Configuration not has been saved previously or has a corruption problem');
  159. end;
  160. end;
  161. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  162. begin
  163. if Assigned(AppConfigJson) then AppConfigJson.Free;
  164. if Assigned(AppConfigReg) then AppConfigReg.Free;
  165. if Assigned(ConfigTest) then ConfigTest.Free;
  166. if Assigned(ConfigReg) then ConfigReg.Free;
  167. if Assigned(ConfigJson) then ConfigJson.Free;
  168. end;
  169. procedure TForm1.FormCreate(Sender: TObject);
  170. begin
  171. ConfigTest := TMyConfig.Create;
  172. SetConfig(ConfigTest);
  173. AppConfigJson := TAppConfigJsonProvider<TMyConfig>.Create(ConfigJson);
  174. AppConfigJson.CreateIfNotExists := True;
  175. AppConfigJson.Filename := '.\Config.json';
  176. ConfigJson := TMyConfig.Create;
  177. AppConfigReg := TAppConfigRegistryProvider<TMyConfig>.Create(ConfigReg);
  178. AppConfigReg.HRoot := HKEY_CURRENT_USER;
  179. AppConfigReg.MainKey := '_AppConfig';
  180. end;
  181. procedure TForm1.SetConfig(cConfig: TMyConfig);
  182. var
  183. processtype : TProcessType;
  184. begin
  185. cConfig.Title := 'hola';
  186. cConfig.Complex := TProcessType.Create;
  187. cConfig.Complex.Id := 1;
  188. cConfig.Complex.Redundant := True;
  189. cConfig.Complex.Priority := TMyPriority.msMed;
  190. //processtype := TProcessType.Create;
  191. //processtype.Id := 1;
  192. //processtype.Priority := msLow;
  193. //processtype.Redundant := True;
  194. //cConfig.History := [processtype];
  195. cConfig.ModifyDate := Now();
  196. end;
  197. { TMyConfig }
  198. constructor TMyConfig.Create;
  199. begin
  200. inherited;
  201. //WorkList := TObjectList<TWorker>.Create(True);
  202. DefaultValues;
  203. end;
  204. procedure TMyConfig.DefaultValues;
  205. begin
  206. fTitle := 'Default value';
  207. end;
  208. destructor TMyConfig.Destroy;
  209. begin
  210. //if Assigned(WorkList) then WorkList.Free;
  211. inherited;
  212. end;
  213. end.