Main.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. unit Main;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. Winapi.Messages,
  6. System.SysUtils,
  7. System.Variants,
  8. System.Classes,
  9. Vcl.Graphics,
  10. Vcl.Controls,
  11. Vcl.Forms,
  12. Vcl.Dialogs,
  13. Vcl.StdCtrls,
  14. System.Generics.Collections,
  15. Quick.Commons,
  16. Quick.Config.YAML;
  17. type
  18. TMyPriority = (msLow, msMed, msHigh);
  19. TWinPos = record
  20. public
  21. PosX : Integer;
  22. PosY : Integer;
  23. end;
  24. TProcessType = record
  25. Id : Integer;
  26. Priority : TMyPriority;
  27. Redundant : Boolean;
  28. end;
  29. TJob = class
  30. private
  31. fJobName : string;
  32. fTimeElapsed : Integer;
  33. published
  34. property JobName : string read fJobName write fJobName;
  35. property TimeElapsed : Integer read fTimeElapsed write fTimeElapsed;
  36. end;
  37. TWorker = class
  38. private
  39. fName : string;
  40. fJob : TJob;
  41. fLevels : TArray<Integer>;
  42. fActive : Boolean;
  43. published
  44. property Name : string read fName write fName;
  45. property Job : TJob read fJob write fJob;
  46. property Levels : TArray<Integer> read fLevels write fLevels;
  47. property Active : Boolean read fActive write fActive;
  48. public
  49. constructor Create;
  50. destructor Destroy; override;
  51. end;
  52. TMyConfig = class(TAppConfigYAML)
  53. private
  54. fTitle : string;
  55. fHidden : Boolean;
  56. fSessionName: string;
  57. fSizes : TArray<Integer>;
  58. fMethods : TArray<string>;
  59. fLastFilename : string;
  60. fWindowPos : TWinPos;
  61. fHistory : TArray<TProcessType>;
  62. fComplex : TProcessType;
  63. fDefaultWorker : TWorker;
  64. fModifyDate : TDateTime;
  65. fWorkList : TObjectList<TWorker>;
  66. published
  67. [TCommentProperty('Sizes array is simple')]
  68. property Sizes : TArray<Integer> read fSizes write fSizes;
  69. property LastFilename : string read fLastFilename write fLastFilename;
  70. property Methods : TArray<string> read fMethods write fMethods;
  71. property WindowPos : TWinPos read fWindowPos write fWindowPos;
  72. [TCommentProperty('Array of records')]
  73. property History : TArray<TProcessType> read fHistory write fHistory;
  74. property Complex : TProcessType read fComplex write fComplex;
  75. property DefaultWorker : TWorker read fDefaultWorker write fDefaultWorker;
  76. property ModifyDate : TDateTime read fModifyDate write fModifyDate;
  77. property Title : string read fTitle write fTitle;
  78. property SessionName : string read fSessionName write fSessionName;
  79. [TCommentProperty('List of work tasks config')]
  80. property WorkList : TObjectList<TWorker> read fWorkList write fWorkList;
  81. public
  82. destructor Destroy; override;
  83. procedure Init; override;
  84. procedure DefaultValues; override;
  85. end;
  86. TMainForm = class(TForm)
  87. meInfo: TMemo;
  88. btnLoadFile: TButton;
  89. btnSaveFile: TButton;
  90. procedure FormCreate(Sender: TObject);
  91. procedure btnSaveFileClick(Sender: TObject);
  92. procedure btnLoadFileClick(Sender: TObject);
  93. procedure SetConfig(cConfig: TMyConfig);
  94. function TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  95. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  96. procedure OnFileModified;
  97. end;
  98. var
  99. MainForm: TMainForm;
  100. ConfigTest : TMyConfig;
  101. ConfigYaml : TMyConfig;
  102. implementation
  103. {$R *.dfm}
  104. procedure TMainForm.btnLoadFileClick(Sender: TObject);
  105. var
  106. sl : TStringList;
  107. s : string;
  108. begin
  109. meInfo.Lines.Add('Load ConfigReg');
  110. ConfigYaml.Load;
  111. meInfo.Lines.Add(ConfigYaml.ToYAML);
  112. if TestConfig(configtest,ConfigYaml) then meInfo.Lines.Add('Test passed successfully!');
  113. end;
  114. procedure TMainForm.btnSaveFileClick(Sender: TObject);
  115. begin
  116. ConfigYaml.Free;
  117. ConfigYaml := TMyConfig.Create('.\config.yml');
  118. SetConfig(ConfigYaml);
  119. ConfigYaml.Save;
  120. meInfo.Lines.Add('Saved Config in Yaml at ' + DateTimeToStr(ConfigYaml.LastSaved));
  121. end;
  122. procedure TMainForm.SetConfig(cConfig : TMyConfig);
  123. var
  124. winpos : TWinpos;
  125. protype : TProcessType;
  126. i : Integer;
  127. worker : TWorker;
  128. begin
  129. cConfig.LastFilename := 'library.txt';
  130. cConfig.Sizes := [23,11,554,12,34,29,77,30,48,59,773,221,98,3,22,983,122,231,433,12,31,987];
  131. cConfig.DefaultWorker.Levels := [10,12,14,18,20];
  132. winpos.PosX := 640;
  133. winpos.PosX := 480;
  134. cConfig.Methods := ['GET','POST','PUT','DELETE','HEAD'];
  135. cConfig.WindowPos := winpos;
  136. protype.Id := 5;
  137. protype.Priority := msHigh;
  138. protype.Redundant := False;
  139. cConfig.Complex := protype;
  140. cConfig.DefaultWorker.Name := 'Process ' + i.ToString;
  141. cConfig.DefaultWorker.Job.JobName := 'Job ' + i.ToString;
  142. cConfig.DefaultWorker.Job.TimeElapsed := i * Random(1000);
  143. cConfig.DefaultWorker.Active := Boolean(Random(1));
  144. cConfig.Title := 'a fresh title';
  145. cConfig.SessionName := 'First Session';
  146. for I := 0 to 5 do
  147. begin
  148. worker := TWorker.Create;
  149. worker.Name := 'Process ' + i.ToString;
  150. worker.Levels := [10,12,14,18,20];
  151. worker.Job.JobName := 'Job ' + i.ToString;
  152. worker.Job.TimeElapsed := i * Random(1000);
  153. worker.Active := Boolean(Random(1));
  154. cConfig.WorkList.Add(worker);
  155. end;
  156. for i := 0 to 2 do
  157. begin
  158. protype.Id := i;
  159. protype.Priority := msLow;
  160. protype.Redundant := True;
  161. cConfig.History := cConfig.History + [protype];
  162. end;
  163. cConfig.ModifyDate := Now();
  164. end;
  165. function TMainForm.TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  166. var
  167. i : Integer;
  168. begin
  169. Result := False;
  170. try
  171. Assert(cConfig1.LastFilename = cConfig2.LastFilename);
  172. for i := Low(cConfig1.Sizes) to High(cConfig1.Sizes) do
  173. Assert(cConfig1.Sizes[i] = cConfig2.Sizes[i]);
  174. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  175. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  176. Assert(cConfig1.Complex.Priority = cConfig2.Complex.Priority);
  177. Assert(cConfig1.Complex.Redundant = cConfig2.Complex.Redundant);
  178. Assert(cConfig1.Title = cConfig2.Title);
  179. for i := 0 to cConfig1.WorkList.Count - 1 do
  180. begin
  181. Assert(cConfig1.WorkList[i].Name = cConfig2.WorkList[i].Name);
  182. Assert(cConfig1.WorkList[i].Active = cConfig2.WorkList[i].Active);
  183. end;
  184. for i := 0 to High(cConfig1.History) do
  185. begin
  186. Assert(cConfig1.History[i].Priority = cConfig2.History[i].Priority);
  187. Assert(cConfig1.History[i].Redundant = cConfig2.History[i].Redundant);
  188. end;
  189. Result := True;
  190. except
  191. ShowMessage('Configuration not has been saved previously or has a corruption problem');
  192. end;
  193. end;
  194. procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
  195. begin
  196. if Assigned(ConfigYaml) then ConfigYaml.Free;
  197. if Assigned(ConfigTest) then ConfigTest.Free;
  198. end;
  199. procedure TMainForm.FormCreate(Sender: TObject);
  200. begin
  201. ConfigYaml := TMyConfig.Create('.\config.yml');
  202. ConfigYaml.Provider.OnFileModified := OnFileModified;
  203. ConfigYaml.Provider.ReloadIfFileChanged := False;
  204. //create config test to compare later
  205. ConfigTest := TMyConfig.Create('');
  206. SetConfig(ConfigTest);
  207. end;
  208. procedure TMainForm.OnFileModified;
  209. begin
  210. meInfo.Lines.Add('Config file modified. Config will be reload');
  211. end;
  212. { TMyConfig }
  213. procedure TMyConfig.Init;
  214. begin
  215. inherited;
  216. fWorkList := TObjectList<TWorker>.Create(True);
  217. fDefaultWorker := TWorker.Create;
  218. DefaultValues;
  219. end;
  220. procedure TMyConfig.DefaultValues;
  221. begin
  222. inherited;
  223. fTitle := 'Default value';
  224. end;
  225. destructor TMyConfig.Destroy;
  226. begin
  227. if Assigned(fWorkList) then fWorkList.Free;
  228. if Assigned(fDefaultWorker) then fDefaultWorker.Free;
  229. inherited;
  230. end;
  231. { TWorker }
  232. constructor TWorker.Create;
  233. begin
  234. fJob := TJob.Create;
  235. end;
  236. destructor TWorker.Destroy;
  237. begin
  238. fJob.Free;
  239. inherited;
  240. end;
  241. end.