umain.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. TMyConfig = class(TAppConfigJson)
  47. private
  48. fTitle : string;
  49. fHidden : Boolean;
  50. fSessionName: string;
  51. fSizes : TArraySizes;
  52. fLastFilename : string;
  53. fWindowPos : TWinPos;
  54. fHistory : TArray<TProcessType>;
  55. fComplex : TProcessType;
  56. fModifyDate : TDateTime;
  57. fWorkList : TObjectList<TWorker>;
  58. public
  59. constructor Create;
  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 : TArray<TProcessType> 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. ConfigJson : 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(ConfigJson);
  100. ConfigJson.Save;
  101. meInfo.Lines.Add(ConfigJson.ToJson);
  102. meInfo.Lines.Add('Saved Config in Json at ' + DateTimeToStr(ConfigJson.LastSaved));
  103. end;
  104. procedure TForm1.btnLoadJsonClick(Sender: TObject);
  105. begin
  106. meInfo.Lines.Add('Load ConfigJson');
  107. ConfigJson := TMyConfig.Create;
  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. //processtype := TProcessType.Create;
  165. //processtype.Id := 1;
  166. //processtype.Priority := msLow;
  167. //processtype.Redundant := True;
  168. //cConfig.History := [processtype];
  169. cConfig.ModifyDate := Now();
  170. end;
  171. { TMyConfig }
  172. constructor TMyConfig.Create;
  173. begin
  174. inherited;
  175. //WorkList := TObjectList<TWorker>.Create(True);
  176. DefaultValues;
  177. end;
  178. procedure TMyConfig.DefaultValues;
  179. begin
  180. fTitle := 'Default value';
  181. end;
  182. destructor TMyConfig.Destroy;
  183. begin
  184. //if Assigned(WorkList) then WorkList.Free;
  185. inherited;
  186. end;
  187. end.