rptcontnr.pp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. unit rptcontnr;
  2. {$mode objfpc}{$H+}
  3. {$I demos.inc}
  4. interface
  5. uses
  6. Classes,
  7. SysUtils,
  8. fpreport,
  9. fpreportcontnr,
  10. contnrs,
  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. TContnrDemo = class(TReportDemoApp)
  24. Protected
  25. FReportData : TFPReportObjectData;
  26. public
  27. procedure CreateReportDesign;override;
  28. procedure LoadDesignFromFile(const AFilename: string);
  29. procedure HookupData(const AComponentName: string; const AData: TFPReportData);
  30. destructor Destroy; override;
  31. end;
  32. TCollectionDemo = class(TContnrDemo)
  33. Protected
  34. procedure InitialiseData; override;
  35. Public
  36. constructor Create(AOWner :TComponent); override;
  37. Class function Description : string; override;
  38. end;
  39. { TObjectListDemo }
  40. TObjectListDemo = class(TContnrDemo)
  41. Protected
  42. procedure InitialiseData; override;
  43. Public
  44. constructor Create(AOWner :TComponent); override;
  45. Class function Description : string; override;
  46. end;
  47. implementation
  48. uses
  49. fpReportStreamer,
  50. fpTTF,
  51. fpJSON,
  52. jsonparser;
  53. { TObjectListDemo }
  54. procedure TObjectListDemo.InitialiseData;
  55. Var
  56. SL : TStringList;
  57. i : Integer;
  58. N,V : String;
  59. C : TCountry;
  60. List : TFPObjectList;
  61. begin
  62. List:=TFPObjectList.Create(True);
  63. TFPReportObjectListData(FReportData).List:=List;
  64. SL:=TStringList.Create;
  65. try
  66. {$I countries.inc}
  67. SL.Sort;
  68. For I:=0 to SL.Count-1 do
  69. begin
  70. C:=TCountry.Create(Nil);
  71. List.Add(C);
  72. SL.GetNameValue(I,N,V);
  73. C.Name:=N;
  74. C.Population:=StrToInt64Def(V,0);
  75. end;
  76. finally
  77. SL.Free;
  78. end;
  79. end;
  80. constructor TObjectListDemo.Create(AOWner: TComponent);
  81. begin
  82. inherited Create(AOWner);
  83. FReportData := TFPReportObjectListData.Create(nil);
  84. TFPReportObjectListData(FReportData).OwnsList:=True;
  85. end;
  86. class function TObjectListDemo.Description: string;
  87. begin
  88. Result:='Demo to show support for object Lists as data loop';
  89. end;
  90. procedure TContnrDemo.CreateReportDesign;
  91. var
  92. p: TFPReportPage;
  93. TitleBand: TFPReportTitleBand;
  94. DataBand: TFPReportDataBand;
  95. GroupHeader: TFPReportGroupHeaderBand;
  96. Memo: TFPReportMemo;
  97. PageFooter: TFPReportPageFooterBand;
  98. begin
  99. Inherited;
  100. rpt.Author := 'Graeme Geldenhuys';
  101. rpt.Title := 'FPReport Demo 12 - JSON Data';
  102. p := TFPReportPage.Create(rpt);
  103. p.Orientation := poPortrait;
  104. p.PageSize.PaperName := 'A4';
  105. { page margins }
  106. p.Margins.Left := 30;
  107. p.Margins.Top := 20;
  108. p.Margins.Right := 30;
  109. p.Margins.Bottom := 20;
  110. p.Data := FReportData;
  111. p.Font.Name := 'LiberationSans';
  112. TitleBand := TFPReportTitleBand.Create(p);
  113. TitleBand.Layout.Height := 40;
  114. {$ifdef ColorBands}
  115. TitleBand.Frame.Shape := fsRectangle;
  116. TitleBand.Frame.BackgroundColor := clReportTitleSummary;
  117. {$endif}
  118. Memo := TFPReportMemo.Create(TitleBand);
  119. Memo.Layout.Left := 35;
  120. Memo.Layout.Top := 20;
  121. Memo.Layout.Width := 80;
  122. Memo.Layout.Height := 10;
  123. Memo.Text := 'COUNTRY AND POPULATION AS OF 2014';
  124. GroupHeader := TFPReportGroupHeaderBand.Create(p);
  125. GroupHeader.Layout.Height := 15;
  126. GroupHeader.GroupCondition := 'copy(''[Name]'',1,1)';
  127. {$ifdef ColorBands}
  128. GroupHeader.Frame.Shape := fsRectangle;
  129. GroupHeader.Frame.BackgroundColor := clGroupHeaderFooter;
  130. {$endif}
  131. Memo := TFPReportMemo.Create(GroupHeader);
  132. Memo.Layout.Left := 0;
  133. Memo.Layout.Top := 5;
  134. Memo.Layout.Width := 10;
  135. Memo.Layout.Height := 8;
  136. Memo.UseParentFont := False;
  137. Memo.Text := '[copy(Name,1,1)]';
  138. Memo.Font.Size := 16;
  139. DataBand := TFPReportDataBand.Create(p);
  140. DataBand.Layout.Height := 8;
  141. {$ifdef ColorBands}
  142. DataBand.Frame.Shape := fsRectangle;
  143. DataBand.Frame.BackgroundColor := clDataBand;
  144. {$endif}
  145. Memo := TFPReportMemo.Create(DataBand);
  146. Memo.Layout.Left := 15;
  147. Memo.Layout.Top := 0;
  148. Memo.Layout.Width := 50;
  149. Memo.Layout.Height := 5;
  150. Memo.Text := '[Name]';
  151. Memo := TFPReportMemo.Create(DataBand);
  152. Memo.Layout.Left := 70;
  153. Memo.Layout.Top := 0;
  154. Memo.Layout.Width := 30;
  155. Memo.Layout.Height := 5;
  156. Memo.Text := '[formatfloat(''#,##0'', Population)]';
  157. PageFooter := TFPReportPageFooterBand.Create(p);
  158. PageFooter.Layout.Height := 20;
  159. {$ifdef ColorBands}
  160. PageFooter.Frame.Shape := fsRectangle;
  161. PageFooter.Frame.BackgroundColor := clPageHeaderFooter;
  162. {$endif}
  163. Memo := TFPReportMemo.Create(PageFooter);
  164. Memo.Layout.Left := 130;
  165. Memo.Layout.Top := 13;
  166. Memo.Layout.Width := 20;
  167. Memo.Layout.Height := 5;
  168. Memo.Text := 'Page [PageNo]';
  169. Memo.TextAlignment.Vertical := tlCenter;
  170. Memo.TextAlignment.Horizontal := taRightJustified;
  171. end;
  172. procedure TContnrDemo.LoadDesignFromFile(const AFilename: string);
  173. var
  174. rs: TFPReportJSONStreamer;
  175. fs: TFileStream;
  176. lJSON: TJSONObject;
  177. begin
  178. if AFilename = '' then
  179. Exit;
  180. if not FileExists(AFilename) then
  181. raise Exception.CreateFmt('The file "%s" can not be found', [AFilename]);
  182. fs := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
  183. try
  184. lJSON := TJSONObject(GetJSON(fs));
  185. finally
  186. fs.Free;
  187. end;
  188. rs := TFPReportJSONStreamer.Create(nil);
  189. rs.JSON := lJSON; // rs takes ownership of lJSON
  190. try
  191. rpt.ReadElement(rs);
  192. finally
  193. rs.Free;
  194. end;
  195. end;
  196. procedure TContnrDemo.HookupData(const AComponentName: string; const AData: TFPReportData);
  197. var
  198. b: TFPReportCustomBandWithData;
  199. begin
  200. b := TFPReportCustomBandWithData(rpt.FindRecursive(AComponentName));
  201. if Assigned(b) then
  202. b.Data := AData;
  203. end;
  204. destructor TContnrDemo.Destroy;
  205. begin
  206. FreeAndNil(FReportData);
  207. inherited Destroy;
  208. end;
  209. constructor TCollectionDemo.Create(AOWner: TComponent);
  210. begin
  211. inherited;
  212. FReportData := TFPReportCollectionData.Create(nil);
  213. TFPReportCollectionData(FReportData).OwnsCollection:=True;
  214. end;
  215. class function TCollectionDemo.Description: string;
  216. begin
  217. Result:='Demo showing native support for collections as data loop';
  218. end;
  219. { TCollectionDemo }
  220. procedure TCollectionDemo.InitialiseData;
  221. Var
  222. SL : TStringList;
  223. i : Integer;
  224. N,V : String;
  225. C : TCountry;
  226. Coll : TCollection;
  227. begin
  228. Coll:=TCollection.Create(TCountry);
  229. TFPReportCollectionData(FReportData).Collection:=coll;
  230. SL:=TStringList.Create;
  231. try
  232. {$I countries.inc}
  233. SL.Sort;
  234. For I:=0 to SL.Count-1 do
  235. begin
  236. C:=Coll.Add As TCountry;
  237. SL.GetNameValue(I,N,V);
  238. C.Name:=N;
  239. C.Population:=StrToInt64Def(V,0);
  240. end;
  241. finally
  242. SL.Free;
  243. end;
  244. end;
  245. end.