umain.pas 6.4 KB

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