rptqrcode.pp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. unit rptQRCode;
  2. {$mode objfpc}{$H+}
  3. {$I demos.inc}
  4. interface
  5. uses
  6. Classes,
  7. SysUtils,
  8. fpreport,
  9. fpreportcontnr,
  10. fpreportqrcode,
  11. udapp;
  12. type
  13. { TCountry }
  14. TCountry = Class(TCollectionItem)
  15. private
  16. FName: String;
  17. FPopulation: Int64;
  18. Published
  19. Property Name : String Read FName Write FName;
  20. Property Population : Int64 Read FPopulation Write FPopulation;
  21. end;
  22. { TCollectionDemo }
  23. { TQRCodeDemo }
  24. TQRCodeDemo = class(TReportDemoApp)
  25. private
  26. procedure SetQRCodeValue(Sender: TFPReportElement);
  27. Protected
  28. FReportData : TFPReportObjectData;
  29. FQRCode: TFPReportQRcode;
  30. public
  31. procedure InitialiseData; override;
  32. constructor Create(AOWner :TComponent); override;
  33. Class function Description : string; override;
  34. procedure CreateReportDesign;override;
  35. procedure LoadDesignFromFile(const AFilename: string);
  36. procedure HookupData(const AComponentName: string; const AData: TFPReportData);
  37. destructor Destroy; override;
  38. end;
  39. implementation
  40. uses
  41. fpReportStreamer,
  42. fpTTF,
  43. fpJSON,
  44. jsonparser;
  45. procedure TQRCodeDemo.CreateReportDesign;
  46. var
  47. p: TFPReportPage;
  48. TitleBand: TFPReportTitleBand;
  49. DataBand: TFPReportDataBand;
  50. GroupHeader: TFPReportGroupHeaderBand;
  51. Memo: TFPReportMemo;
  52. PageFooter: TFPReportPageFooterBand;
  53. QR : TFPReportQRcode;
  54. begin
  55. Inherited;
  56. rpt.Author := 'Michael Van Canneyt';
  57. rpt.Title := 'FPReport Demo : QR Codes';
  58. p := TFPReportPage.Create(rpt);
  59. p.Orientation := poPortrait;
  60. p.PageSize.PaperName := 'A4';
  61. { page margins }
  62. p.Margins.Left := 30;
  63. p.Margins.Top := 20;
  64. p.Margins.Right := 30;
  65. p.Margins.Bottom := 20;
  66. p.Data := FReportData;
  67. p.Font.Name := 'LiberationSans';
  68. TitleBand := TFPReportTitleBand.Create(p);
  69. TitleBand.Layout.Height := 40;
  70. {$ifdef ColorBands}
  71. TitleBand.Frame.Shape := fsRectangle;
  72. TitleBand.Frame.BackgroundColor := clReportTitleSummary;
  73. {$endif}
  74. Memo := TFPReportMemo.Create(TitleBand);
  75. Memo.Layout.Left := 35;
  76. Memo.Layout.Top := 20;
  77. Memo.Layout.Width := 80;
  78. Memo.Layout.Height := 10;
  79. Memo.Text := 'COUNTRY AND POPULATION AS OF 2014';
  80. QR:= TFPReportQRcode.Create(TitleBand);
  81. QR.Layout.Left := 1;
  82. QR.Layout.Top := 1;
  83. QR.Layout.Width := 34;
  84. QR.Layout.Height := 34;
  85. QR.Value:='https://www.nayuki.io/page/qr-code-generator-library/';
  86. QR.Center:=True;
  87. QR:= TFPReportQRcode.Create(TitleBand);
  88. QR.Layout.Left := 115;
  89. QR.Layout.Top := 1;
  90. QR.Layout.Width := 34;
  91. QR.Layout.Height := 34;
  92. QR.Value:='https://freepascal.org/';
  93. QR.Center:=True;
  94. GroupHeader := TFPReportGroupHeaderBand.Create(p);
  95. GroupHeader.Layout.Height := 15;
  96. GroupHeader.GroupCondition := 'copy(''[Name]'',1,1)';
  97. {$ifdef ColorBands}
  98. GroupHeader.Frame.Shape := fsRectangle;
  99. GroupHeader.Frame.BackgroundColor := clGroupHeaderFooter;
  100. {$endif}
  101. Memo := TFPReportMemo.Create(GroupHeader);
  102. Memo.Layout.Left := 0;
  103. Memo.Layout.Top := 5;
  104. Memo.Layout.Width := 10;
  105. Memo.Layout.Height := 8;
  106. Memo.UseParentFont := False;
  107. Memo.Text := '[copy(Name,1,1)]';
  108. Memo.Font.Size := 16;
  109. DataBand := TFPReportDataBand.Create(p);
  110. DataBand.Layout.Height := 35;
  111. {$ifdef ColorBands}
  112. DataBand.Frame.Shape := fsRectangle;
  113. DataBand.Frame.BackgroundColor := clDataBand;
  114. {$endif}
  115. Memo := TFPReportMemo.Create(DataBand);
  116. Memo.Layout.Left := 15;
  117. Memo.Layout.Top := 1;
  118. Memo.Layout.Width := 50;
  119. Memo.Layout.Height := 20;
  120. Memo.Text := '[Name]';
  121. Memo := TFPReportMemo.Create(DataBand);
  122. Memo.Layout.Left := 70;
  123. Memo.Layout.Top := 1;
  124. Memo.Layout.Width := 30;
  125. Memo.Layout.Height := 5;
  126. Memo.Text := '[formatfloat(''#,##0'', Population)]';
  127. FQRCode := TFPReportQRCode.Create(DataBand);
  128. FQRCode.Layout.Left := 100;
  129. FQRCode.Layout.Top := 1;
  130. FQRCode.Layout.Width := 32;
  131. FQRCode.Layout.Height := 32;
  132. FQRCode.Center:=True;
  133. // Only one of the 2 ways must be used: either set expression, either use callback.
  134. FQRCode.Expression:='''http://en.wikipedia.org/wiki/''+Name';
  135. // Databand.OnBeforePrint:=@SetQRCodeValue;
  136. PageFooter := TFPReportPageFooterBand.Create(p);
  137. PageFooter.Layout.Height := 20;
  138. {$ifdef ColorBands}
  139. PageFooter.Frame.Shape := fsRectangle;
  140. PageFooter.Frame.BackgroundColor := clPageHeaderFooter;
  141. {$endif}
  142. Memo := TFPReportMemo.Create(PageFooter);
  143. Memo.Layout.Left := 130;
  144. Memo.Layout.Top := 13;
  145. Memo.Layout.Width := 20;
  146. Memo.Layout.Height := 5;
  147. Memo.Text := 'Page [PageNo]';
  148. Memo.TextAlignment.Vertical := tlCenter;
  149. Memo.TextAlignment.Horizontal := taRightJustified;
  150. end;
  151. procedure TQRCodeDemo.LoadDesignFromFile(const AFilename: string);
  152. var
  153. rs: TFPReportJSONStreamer;
  154. fs: TFileStream;
  155. lJSON: TJSONObject;
  156. begin
  157. if AFilename = '' then
  158. Exit;
  159. if not FileExists(AFilename) then
  160. raise Exception.CreateFmt('The file "%s" can not be found', [AFilename]);
  161. fs := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
  162. try
  163. lJSON := TJSONObject(GetJSON(fs));
  164. finally
  165. fs.Free;
  166. end;
  167. rs := TFPReportJSONStreamer.Create(nil);
  168. rs.JSON := lJSON; // rs takes ownership of lJSON
  169. try
  170. rpt.ReadElement(rs);
  171. finally
  172. rs.Free;
  173. end;
  174. end;
  175. procedure TQRCodeDemo.HookupData(const AComponentName: string; const AData: TFPReportData);
  176. var
  177. b: TFPReportCustomBandWithData;
  178. begin
  179. b := TFPReportCustomBandWithData(rpt.FindRecursive(AComponentName));
  180. if Assigned(b) then
  181. b.Data := AData;
  182. end;
  183. destructor TQRCodeDemo.Destroy;
  184. begin
  185. FreeAndNil(FReportData);
  186. inherited Destroy;
  187. end;
  188. constructor TQRCodeDemo.Create(AOWner: TComponent);
  189. begin
  190. inherited;
  191. FReportData := TFPReportCollectionData.Create(nil);
  192. TFPReportCollectionData(FReportData).OwnsCollection:=True;
  193. end;
  194. class function TQRCodeDemo.Description: string;
  195. begin
  196. Result:='Demo showing native support for QRCodes';
  197. end;
  198. { TQRCodeDemo }
  199. procedure TQRCodeDemo.SetQRcodeValue(Sender: TFPReportElement);
  200. begin
  201. FQRCode.Value:='http://en.wikipedia.org/wiki/'+FReportData.FieldValues['Name'];
  202. end;
  203. procedure TQRCodeDemo.InitialiseData;
  204. var
  205. SL : TStringList;
  206. i : Integer;
  207. N,V : String;
  208. C : TCountry;
  209. Coll : TCollection;
  210. begin
  211. Coll:=TCollection.Create(TCountry);
  212. TFPReportCollectionData(FReportData).Collection:=coll;
  213. SL:=TStringList.Create;
  214. try
  215. {$I countries.inc}
  216. SL.Sort;
  217. For I:=0 to SL.Count-1 do
  218. begin
  219. C:=Coll.Add As TCountry;
  220. SL.GetNameValue(I,N,V);
  221. C.Name:=N;
  222. C.Population:=StrToInt64Def(V,0);
  223. end;
  224. finally
  225. SL.Free;
  226. end;
  227. end;
  228. end.