rptqrcode.pp 6.4 KB

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