File: lazideregfpreport.pp

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (436 lines) | stat: -rw-r--r-- 9,822 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
{
    This file is part of the Free Component Library.
    Copyright (c) 2017 Michael Van Canneyt, member of the Free Pascal development team

    Register FPReport components in the Lazarus IDE.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit lazideregfpreport;

{$mode objfpc}{$H+}

{$IFNDEF WINDOWS}
{$DEFINE USEFPIMAGE}
{$ENDIF}

interface

uses
  Classes, SysUtils, controls, fpreport, fpjsonreport, fpreportdb, fpreportdesigner,
  fpreportpdfexport,
{$IFDEF USEFPIMAGE}
  fpreporthtmlexport,
  fpreportfpimageexport,
  cfgfpreporthtmlexport,
  cfgfpreportimageexport,
{$ENDIF}
  fpreportlclexport,
  fpreportformexport,
  fpreportpreview,
  fpreportprinterexport,
  cfgfpreportpdfexport,
  fpreportjson,
  fpreportcontnr,
  ComponentEditors,
  forms,
  menus;

Type

  { TFPReportComponentEditor }

  TFPReportComponentEditor = Class (TComponentEditor)
  private
    procedure AssignLocalData;
    procedure DesignReport;
    procedure LoadReport;
    procedure PreviewReport;
    procedure RunReport;
    procedure SaveReport;
  Public
    Procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
    Function Report : TFPCustomReport;
  end;

  { TFPReportDesignerComponentEditor }

  TFPReportDesignerComponentEditor = Class (TComponentEditor)
  Public
    Procedure ExecuteVerb(Index: Integer); override;
    Procedure PrepareItem(Index: Integer; const AnItem: TMenuItem); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
    Function ReportDesigner : TFPReportDesigner;
  end;

  { TFPReportExportComponentEditor }

  TFPReportExportComponentEditor = Class (TComponentEditor)
  Public
    Procedure ExecuteVerb(Index: Integer); override;
    Procedure PrepareItem(Index: Integer; const AnItem: TMenuItem); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
    Function Exporter : TFPReportExporter;
  end;

procedure Register;

implementation

Uses
  // Make sure these are included
  dialogs,
  frmfprdresizeelements,
  frmfpreportalignelements,
  regfpdesigner,
  LResources,
  frmideselectreportdata;


procedure Register;

begin
  RegisterFPReportPropEditors;
  RegisterComponentEditor(TFPCustomReport,TFPReportComponentEditor);
  RegisterComponentEditor(TFPReportExporter,TFPReportExportComponentEditor);
  RegisterComponentEditor(TFPReportDesigner,TFPReportDesignerComponentEditor);
  {$I fpreporticons.lrs}
  RegisterComponents('FPReport',[
     TFPReport,
     TFPJSONReport,
     TFPReportDesigner,
     TFPReportUserData,
     TFPReportDatasetData,
     TFPReportJSONData,
     TFPReportCollectionData,
     TFPReportObjectListData,
{$IFDEF USEFPIMAGE}
     TFPReportExportHTML,
     TFPReportExportfpImage,
{$ENDIF}
     TFPReportExportPDF,
     TFPreportPreviewExport,
     TFPreportPrinterExport
  ]);
end;

Resourcestring
  SVerbDesignReport = 'Design report';
  SVerbAssignLocalData = 'Assign local data';
  SVerbPreview = 'Preview report';
  SVerbExportReport = 'Export report';
  SVerbRunReport = 'Run report';
  SVerbSave = 'Save design to file';
  SVerbLoad = 'Load design from file';
  SVerbOpenDesigner = 'Open designer';
  SVerbExportConfigure = 'Configuration...';

{ TFPReportDesignerComponentEditor }

procedure TFPReportDesignerComponentEditor.ExecuteVerb(Index: Integer);
begin
  if Not Assigned(ReportDesigner) then
    exit;
  Case index of
    0 :
      begin
      ReportDesigner.Execute;
      Designer.Modified;
      end;
  else
    inherited ExecuteVerb(Index)
  end;
end;

procedure TFPReportDesignerComponentEditor.PrepareItem(Index: Integer;
  const AnItem: TMenuItem);
begin
  inherited PrepareItem(Index, AnItem);
  if (Index=0) then
    anItem.Enabled:=Assigned(ReportDesigner) and Assigned(ReportDesigner.Report);
end;

function TFPReportDesignerComponentEditor.GetVerb(Index: Integer): string;
begin
  Case Index of
    0 : Result:=SVerbOpenDesigner;
  else
    Result:=inherited GetVerb(Index);
  end;
end;

function TFPReportDesignerComponentEditor.GetVerbCount: Integer;
begin
  Result:=1;
end;

function TFPReportDesignerComponentEditor.ReportDesigner: TFPReportDesigner;
begin
  if Component is TFPReportDesigner then
    Result:=Component as TFPReportDesigner
  else
    Result:=Nil;
end;

{ TFPReportExportComponentEditor }

procedure TFPReportExportComponentEditor.ExecuteVerb(Index: Integer);
begin
  case Index of
    0 : Exporter.Execute;
    1 : if Exporter.ShowConfig then
          Designer.Modified;
  else
    inherited ExecuteVerb(Index);
  end;
end;

procedure TFPReportExportComponentEditor.PrepareItem(Index: Integer;
  const AnItem: TMenuItem);

Var
  B : Boolean;

begin
  inherited PrepareItem(Index, AnItem);
  Case Index of
    0 : anItem.Enabled:=Assigned(Exporter) and Assigned(Exporter.Report);
    1 :
      begin
      B:=Assigned(Exporter);
      if B then
        B:=Assigned(ReportExportManager.ExporterConfigHandler(TFPReportExporterClass(Exporter.ClassType)));
      anItem.enabled:=B;
      end;
    end;
end;

function TFPReportExportComponentEditor.GetVerb(Index: Integer): string;
begin
  Case Index of
    0 : Result:=SVerbExportReport;
    1 : Result:=SVerbExportConfigure;
  else
    Result:=inherited GetVerb(Index);
  end;
end;

function TFPReportExportComponentEditor.GetVerbCount: Integer;
begin
  Result:=0;
  if Exporter=Nil then
    exit;
  Result:=2;
end;

function TFPReportExportComponentEditor.Exporter: TFPReportExporter;
begin
  if Component is TFPReportExporter then
    Result:=Component as TFPReportExporter
  else
    Result:=Nil;
end;


{ TFPReportComponentEditor }

procedure TFPReportComponentEditor.PreviewReport;

Var
  F : TFPreportPreviewExport;

begin
  F:=TFPreportPreviewExport.Create(Application);
  try
    F.AutoRun:=True;
    F.Report:=Report;
    F.Execute;
    F.Report.ClearPreparedReport;
  finally
    F.Free;
  end;
end;

procedure TFPReportComponentEditor.RunReport;

begin
  Report.RunReport;
end;

procedure TFPReportComponentEditor.DesignReport;

Var
  F : TFPReportDesigner;

begin
  F:=TFPReportDesigner.Create(Application);
  try
    F.Report:=Self.Report;
    F.Options:=AllReportDesignOptions-[rdoManageData,rdoAllowFileDrop];
    F.ModalWindow:=True;
    F.Execute;
    F.Report.ClearPreparedReport;
    Designer.Modified;
  finally
    F.Free;
  end;
end;

procedure TFPReportComponentEditor.AssignLocalData;

  Procedure GetLocalData(L : TFPList);

  Var
    O,C : TComponent;

  begin
    O:=Report.Owner;
    While (O<>Nil) do
      begin
      For C in O do
        if C is TFPReportData then
          L.Add(C);
      if (O is TCustomForm) or (O is TDataModule) or (O is TFrame) then
        O:=Nil
      else
        O:=O.Owner;
      end;
  end;

Var
  All,Selected : TFPList;
  F : TSelectReportDataSourcesForm;
  I : integer;

begin
  Selected:=nil;
  F:=nil;
  AlL:=TFPList.Create;
  try
    GetLocalData(All);
    F:=TSelectReportDataSourcesForm.Create(Application);
    F.AllData:=All;
    if (F.ShowModal=mrOK) then
      begin
      Selected:=TFPList.Create;
      F.GetSelected(Selected);
      Report.ReportData.Clear;
      For I:=0 to Selected.Count-1 do
        Report.ReportData.AddReportData(TFPReportData(Selected[i]));
      Designer.Modified;
      end;
  finally
    F.Free;
    Selected.Free;
    AlL.Free;
  end;
end;

procedure TFPReportComponentEditor.SaveReport;

Var
  J : TFPJSONReport;

begin
  With TSaveDialog.Create(Application) do
    try
      DefaultExt:='.json';
      Filter:='Report files|*.json|All files'+AllFilesMask;
      Options:=[ofPathMustExist];
      If Execute then
         If Component is TFPJSONReport then
           begin
           J:=Component as TFPJSONReport;
           if J.PageCount=0 then
             J.LoadFromJSON(J.DesignTimeJSON);
           TFPJSONReport(Component).SaveToFile(FileName);
           end;
    finally
      Free;
    end;
end;

procedure TFPReportComponentEditor.LoadReport;

Var
  J : TFPJSONReport;
  N : String;

begin
  With TOpenDialog.Create(Application) do
    try
      DefaultExt:='.json';
      Filter:='Report files|*.json|All files'+AllFilesMask;
      Options:=[ofFileMustExist];
      If Execute then
         If Component is TFPJSONReport then
           begin
           N:=J.Name;
           J:=Component as TFPJSONReport;
           J.Clear;
           J.LoadFromFile(FileName);
           J.DesignTimeJSON.Clear;
           J.SavetoJSON(J.DesignTimeJSON);
           J.Name:=N;
           end;
    finally
      Free;
    end;
end;

procedure TFPReportComponentEditor.ExecuteVerb(Index: Integer);
begin
  if Report=Nil then
     exit;
  Case Index of
    0 : DesignReport;
    1 : AssignLocalData;
    2 : PreviewReport;
    3 : RunReport;
    4 : SaveReport;
    5 : LoadReport;
  end;
end;

function TFPReportComponentEditor.GetVerb(Index: Integer): string;
begin
  Case Index of
    0 : Result:=SVerbDesignReport;
    1 : Result:=SVerbAssignLocalData;
    2 : Result:=SVerbPreview;
    3 : Result:=SVerbRunReport;
    4 : Result:=SVerbSave;
    5 : Result:=SVerbLoad;
  else
    Result:='';
  end
end;

function TFPReportComponentEditor.GetVerbCount: Integer;
begin
  if Component is TFPJSONReport then
    Result:=6
  else
    Result:=4;
end;

function TFPReportComponentEditor.Report: TFPCustomReport;
begin
  If (Component is TFPCustomReport) then
    Result:=Component as TFPCustomReport
  else
    Result:=nil
end;

end.