rptcontnr.pp 6.1 KB

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