2
0

Main.pas 7.7 KB

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