umain.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.Json,
  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(TAppConfigJson)
  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(TAppConfigJson)
  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. constructor Create;
  61. destructor Destroy; override;
  62. procedure DefaultValues; override;
  63. property Hidden : Boolean read fHidden write fHidden;
  64. published
  65. property Title : string read fTitle write fTitle;
  66. property SessionName : string read fSessionName write fSessionName;
  67. property Sizes : TArraySizes read fSizes write fSizes;
  68. property LastFilename : string read fLastFilename write fLastFilename;
  69. property WindowPos : TWinPos read fWindowPos write fWindowPos;
  70. property History : TArrayHistory read fHistory write fHistory;
  71. property Complex : TProcessType read fComplex write fComplex;
  72. property ModifyDate : TDateTime read fModifyDate write fModifyDate;
  73. //property WorkList : TObjectList<TWorker> read fWorkList write fWorkList;
  74. end;
  75. { TForm1 }
  76. TForm1 = class(TForm)
  77. btnSaveJson: TButton;
  78. btnLoadJson: TButton;
  79. meInfo: TMemo;
  80. procedure btnLoadJsonClick(Sender: TObject);
  81. procedure btnSaveJsonClick(Sender: TObject);
  82. procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  83. procedure FormCreate(Sender: TObject);
  84. private
  85. public
  86. procedure SetConfig(cConfig: TMyConfig);
  87. function TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  88. end;
  89. var
  90. Form1: TForm1;
  91. ConfigJson : TMyConfig;
  92. ConfigTest : TMyConfig;
  93. implementation
  94. {$R *.lfm}
  95. uses
  96. Quick.Json.Serializer;
  97. { TForm1 }
  98. procedure TForm1.btnSaveJsonClick(Sender: TObject);
  99. begin
  100. SetConfig(ConfigJson);
  101. ConfigJson.Save;
  102. meInfo.Lines.Add(ConfigJson.ToJson);
  103. meInfo.Lines.Add('Saved Config in Json at ' + DateTimeToStr(ConfigJson.LastSaved));
  104. end;
  105. procedure TForm1.btnLoadJsonClick(Sender: TObject);
  106. begin
  107. meInfo.Lines.Add('Load ConfigJson');
  108. ConfigJson.Load;
  109. meInfo.Lines.Add(ConfigJson.ToJSON);
  110. if TestConfig(ConfigTest,ConfigJson) then meInfo.Lines.Add('Test passed successfully!');
  111. end;
  112. function TForm1.TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  113. var
  114. i : Integer;
  115. begin
  116. try
  117. Assert(cConfig1.LastFilename = cConfig2.LastFilename);
  118. for i := Low(cConfig1.Sizes) to High(cConfig1.Sizes) do
  119. Assert(cConfig1.Sizes[i] = cConfig2.Sizes[i]);
  120. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  121. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  122. Assert(cConfig1.Complex.Priority = cConfig2.Complex.Priority);
  123. Assert(cConfig1.Complex.Redundant = cConfig2.Complex.Redundant);
  124. Assert(cConfig1.Title = cConfig2.Title);
  125. //for i := 0 to cConfig1.WorkList.Count - 1 do
  126. //begin
  127. // Assert(cConfig1.WorkList[i].Name = cConfig2.WorkList[i].Name);
  128. // Assert(cConfig1.WorkList[i].Active = cConfig2.WorkList[i].Active);
  129. //end;
  130. for i := 0 to High(cConfig1.History) do
  131. begin
  132. Assert(cConfig1.History[i].Priority = cConfig2.History[i].Priority);
  133. Assert(cConfig1.History[i].Redundant = cConfig2.History[i].Redundant);
  134. end;
  135. Result := True;
  136. except
  137. ShowMessage('Configuration not has been saved previously or has a corruption problem');
  138. end;
  139. end;
  140. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  141. begin
  142. if Assigned(ConfigTest) then ConfigTest.Free;
  143. if Assigned(ConfigJson) then ConfigJson.Free;
  144. end;
  145. procedure TForm1.FormCreate(Sender: TObject);
  146. begin
  147. ConfigTest := TMyConfig.Create;
  148. SetConfig(ConfigTest);
  149. ConfigJson := TMyConfig.Create('.\Config.json');
  150. //ConfigJson.Provider.CreateIfNotExists := True;
  151. end;
  152. procedure TForm1.SetConfig(cConfig: TMyConfig);
  153. var
  154. processtype : TProcessType;
  155. begin
  156. cConfig.Title := 'hola';
  157. cConfig.SessionName := 'Session01';
  158. cConfig.LastFileName := 'C:\library.txt';
  159. cConfig.Sizes := [1,2,3,4,5,6,7];
  160. cConfig.Complex := TProcessType.Create;
  161. cConfig.Complex.Id := 1;
  162. cConfig.Complex.Redundant := True;
  163. cConfig.Complex.Priority := TMyPriority.msMed;
  164. cConfig.WindowPos := TWinPos.Create;
  165. cConfig.WindowPos.PosX := 100;
  166. cConfig.WindowPos.PosY := 200;
  167. processtype := TProcessType.Create;
  168. processtype.Id := 1;
  169. processtype.Priority := msLow;
  170. processtype.Redundant := True;
  171. cConfig.History := [processtype];
  172. cConfig.ModifyDate := Now();
  173. end;
  174. { TMyConfig }
  175. constructor TMyConfig.Create;
  176. begin
  177. inherited;
  178. //WorkList := TObjectList<TWorker>.Create(True);
  179. DefaultValues;
  180. end;
  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.