File: fpreportpreview.pas

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 (487 lines) | stat: -rw-r--r-- 12,130 bytes parent folder | download | duplicates (2)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
{
    This file is part of the Free Component Library.
    Copyright (c) 2016 Michael Van Canneyt, member of the Free Pascal development team

    FPReport LCL Preview form.

    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 fpreportpreview;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  ComCtrls, ActnList, Buttons, StdCtrls, Menus, fpreport, fpreportlclexport,
  fpreportformexport;

type

  { TFPReportPreviewForm }
  // Fool the IDE.
  TForm = TCustomFPreportPreviewForm;
  TOpenURLEvent = Procedure (Sender : TObject; Const AURL : String) of Object;

  TFPReportPreviewForm = class(TForm)
    AClose: TAction;
    ALast: TAction;
    AFirst: TAction;
    APrint: TAction;
    AExportPDF: TAction;
    AZoomReset: TAction;
    AZoomOut: TAction;
    AZoomIn: TAction;
    ANext: TAction;
    APrevious: TAction;
    AExport: TAction;
    ALPreview: TActionList;
    EPage: TEdit;
    ILPreview: TImageList;
    LPageCount: TLabel;
    PBPreview: TPaintBox;
    PButtons: TPanel;
    PBottom: TPanel;
    PMExport: TPopupMenu;
    SBPrevious: TSpeedButton;
    SBPrevious1: TSpeedButton;
    ScrollBox1: TScrollBox;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    TBPreview: TToolBar;
    TBClose: TToolButton;
    TBExport: TToolButton;
    TBPDF: TToolButton;
    TBPrint: TToolButton;
    TBFirst: TToolButton;
    TBLast: TToolButton;
    ToolButton3: TToolButton;
    TBPrevious: TToolButton;
    TBNext: TToolButton;
    ToolButton6: TToolButton;
    TBZoomIn: TToolButton;
    TBZoomReset: TToolButton;
    TBZoomOut: TToolButton;
    procedure ACloseExecute(Sender: TObject);
    procedure AExportExecute(Sender: TObject);
    procedure AExportPDFExecute(Sender: TObject);
    procedure AExportPDFUpdate(Sender: TObject);
    procedure AFirstExecute(Sender: TObject);
    procedure AFirstUpdate(Sender: TObject);
    procedure ALastExecute(Sender: TObject);
    procedure ALastUpdate(Sender: TObject);
    procedure ANextExecute(Sender: TObject);
    procedure ANextUpdate(Sender: TObject);
    procedure APreviousExecute(Sender: TObject);
    procedure APreviousUpdate(Sender: TObject);
    procedure APrintExecute(Sender: TObject);
    procedure AZoomInExecute(Sender: TObject);
    procedure AZoomInUpdate(Sender: TObject);
    procedure AZoomOutExecute(Sender: TObject);
    procedure AZoomOutUpdate(Sender: TObject);
    procedure AZoomResetExecute(Sender: TObject);
    procedure EPageEditingDone(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PBPreviewClick(Sender: TObject);
    procedure PBPreviewMouseLeave(Sender: TObject);
    procedure PBPreviewMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    FHorzOffset : Integer;
    FBitmap:TBitmap;
    FLastPage : Integer;
    FCurrentZoom : Integer;
    FOnOpenURL: TOpenURLEvent;
    FRender:TFPReportExportCanvas;
    procedure DoExport(Sender: TObject);
    procedure ExportReport(REC: TFPReportExporterClass);
    procedure FillExportMenu;
    function GetPageIndex: Integer;
    procedure RecreateBitmap;
    procedure ResizePreview;
    procedure SetCurrentZoom(AValue: Integer);
    procedure SetPageIndex(AValue: Integer);
  Protected
    function GetEnableHyperLinks: Boolean ; override;
    procedure SetEnableHyperLinks(AValue: Boolean); override;
    procedure SetReport(AValue: TFPCustomReport); override;
    procedure ShowHyperLink(const AURL: String); virtual;
      { private declarations }
  public
    procedure DoPaintReport(Sender: TObject);
    Class Function LoadFromResource : Boolean; override;
    { public declarations }
    Property CurrentZoom : Integer Read FCurrentZoom Write SetCurrentZoom;
    // Zero based  !
    Property PageIndex : Integer Read GetPageIndex Write SetPageIndex;
    // If not set, the OpenURL method of LCL will be called.
    Property OnOpenURL : TOpenURLEvent Read FOnOpenURL Write FOnOpenURL;
  end;

var
  FPReportPreviewForm: TFPReportPreviewForm;

implementation

{$R *.lfm}

uses dlginputcombo,lclintf;

Const
  MaxZoomIndex = 4;
  Zooms : Array[-4..4] of Double = (1/4, 1/2, 1/1.5, 1/1.25, 1, 1.25, 1.5, 2, 4);
  PDFExport = 'PDF';
  PrintExport = 'Print';

Resourcestring
  SPageCount = 'of %d';
  SExport = 'Export Report';
  SSelectExportFormat = 'Select export format';
  SErrNoHyperLinkSupport = 'Hyperlink support not enabled.';

Type
  TExportMenuItem = Class(TMenuItem)
  Public
    TheClass : TFPReportExporterClass;
  end;

{ TFPReportPreviewForm }

procedure TFPReportPreviewForm.ACloseExecute(Sender: TObject);
begin
  Close;
end;

procedure TFPReportPreviewForm.AExportExecute(Sender: TObject);

Var
  I : integer;
  L : TStringList;

begin
  // Do Export
  L:=TstringList.Create;
  try
    For I:=0 to PMExport.Items.Count-1 do
      L.AddObject(PMExport.Items[I].Caption,PMExport.Items[I]);
    if L.Count=0 then
        exit;
    L.Sort;
    I:=InputCombo(SExport,SSelectExportFormat,L);
    If I<>-1 then
      ExportReport((L.Objects[i] as TExportMenuItem).TheClass);
  finally
    L.Free;
  end;
end;

procedure TFPReportPreviewForm.AExportPDFExecute(Sender: TObject);
begin
  ExportReport(ReportExportManager.FindExporter(PDFExport));
end;

procedure TFPReportPreviewForm.AExportPDFUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=ReportExportManager.FindExporter(PDFExport)<>Nil;
end;

procedure TFPReportPreviewForm.AFirstExecute(Sender: TObject);
begin
  PageIndex:=0;
end;

procedure TFPReportPreviewForm.AFirstUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=PageIndex>0;
end;

procedure TFPReportPreviewForm.ALastExecute(Sender: TObject);
begin
  PageIndex:=ReportPages.Count-1;
end;

procedure TFPReportPreviewForm.ALastUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=PageIndex<ReportPages.Count-1;
end;

procedure TFPReportPreviewForm.ANextExecute(Sender: TObject);
begin
  PageIndex:=PageIndex+1;
end;

procedure TFPReportPreviewForm.ANextUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=PageIndex<ReportPages.Count-1;
end;

procedure TFPReportPreviewForm.APreviousExecute(Sender: TObject);
begin
  PageIndex:=PageIndex-1;
end;

procedure TFPReportPreviewForm.APreviousUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=PageIndex>0;
end;

procedure TFPReportPreviewForm.APrintExecute(Sender: TObject);
begin
  ExportReport(ReportExportManager.FindExporter(PrintExport));
end;

procedure TFPReportPreviewForm.AZoomInExecute(Sender: TObject);
begin
  if FCurrentZoom<MaxZoomIndex then
    CurrentZoom:=CurrentZoom+1;
end;

procedure TFPReportPreviewForm.AZoomInUpdate(Sender: TObject);
begin
  (Sender as Taction).Enabled:=FCurrentZoom<MaxZoomIndex;
end;

procedure TFPReportPreviewForm.AZoomOutExecute(Sender: TObject);
begin
  if FCurrentZoom>-MaxZoomIndex then
    CurrentZoom:=CurrentZoom-1;
end;

procedure TFPReportPreviewForm.AZoomOutUpdate(Sender: TObject);
begin
  (Sender as Taction).Enabled:=FCurrentZoom>-MaxZoomIndex;
end;

procedure TFPReportPreviewForm.AZoomResetExecute(Sender: TObject);
begin
  CurrentZoom:=0;
end;

procedure TFPReportPreviewForm.EPageEditingDone(Sender: TObject);
Var
  PN : Integer;
begin
  PN:=StrToIntDef(EPage.Text,-1);
  if (PN<1) or (PN>FRender.PageCount) then exit;
  FRender.PageIndex:=PN-1;
  PBPreview.Invalidate;
end;


procedure TFPReportPreviewForm.SetCurrentZoom(AValue: Integer);
begin
  if FCurrentZoom=AValue then Exit;
  FCurrentZoom:=AValue;
  FRender.HDPI:=PixelsPerInch;
  FRender.VDPI:=PixelsPerInch;
  FRender.Zoom:=Zooms[FCurrentZoom];
  ResizePreview;
  FLastPage:=-1; // Force recreate
  PBPreview.Invalidate;
end;

function TFPReportPreviewForm.GetPageIndex: Integer;
begin
  Result:=FRender.PageIndex;
end;

procedure TFPReportPreviewForm.SetPageIndex(AValue: Integer);
begin
  FRender.PageIndex:=AValue;
  EPage.Text:=IntToStr(AValue+1);
  PBPreview.Invalidate;
end;

function TFPReportPreviewForm.GetEnableHyperLinks: Boolean;
begin
  Result:=FRender.HyperLinksEnabled;
end;

procedure TFPReportPreviewForm.SetEnableHyperLinks(AValue: Boolean);
begin
  FRender.HyperLinksEnabled:=AValue;
end;

procedure TFPReportPreviewForm.SetReport(AValue: TFPCustomReport);
begin
  inherited SetReport(AValue);
  FRender.Report:=AValue;
  If Assigned(AValue) then
    begin
    FRender.Execute;
    LPageCount.Caption:=Format(SPageCount,[FRender.PageCount]);
    EPage.Text:='1';
    PageIndex:=0;
    end;
end;

procedure TFPReportPreviewForm.ResizePreview;

Var
  W,H : Integer;

begin
  FRender.GetCurrentPageRenderSize(W,H);
  PBPreview.Width:=W+FHorzOffset*2;
  PBPreview.Height:=H;
end;

procedure TFPReportPreviewForm.RecreateBitmap;

Var
  W,H : Integer;

begin
  FRender.GetCurrentPageRenderSize(W,H);
  FBitmap.SetSize(W,H);
  FRender.RenderCurrentPage;
  FLastPage:=FRender.PageIndex;
end;

procedure TFPReportPreviewForm.DoPaintReport(Sender: TObject);


begin
  if FLastPage<>FRender.PageIndex then
    RecreateBitmap;
  ResizePreview;
  PBPreview.Canvas.Draw(FHorzOffset*2,0,FBitmap);
end;

class function TFPReportPreviewForm.LoadFromResource: Boolean;
begin
  Result:=True;
end;

procedure TFPReportPreviewForm.FormCreate(Sender: TObject);

begin
  FHorzOffset:=100;
  FRender:=TFPReportExportCanvas.Create(Self);
  FRender.HyperLinksEnabled:=True;
  FBitmap:=TBitmap.Create;
  FLastPage:=-1;
  FRender.Zoom:=1;
  FRender.Canvas:=FBitmap.Canvas;
  PBPreview.OnPaint:=@DoPaintReport;
  AExportPDF.Enabled:=ReportExportManager.IndexOfExporter(PDFExport)<>-1;
  APrint.Enabled:=ReportExportManager.IndexOfExporter(PrintExport)<>-1;
  CurrentZoom:=0;
  FillExportMenu;
end;

procedure TFPReportPreviewForm.FormDestroy(Sender: TObject);
begin
  FBitmap.Free;
end;

procedure TFPReportPreviewForm.ShowHyperLink(const AURL: String);

begin
  if Assigned(FOnOpenURL) then
    FOnOpenURL(Self,AURL)
  else
    OpenURL(AURL);
end;

procedure TFPReportPreviewForm.PBPreviewClick(Sender: TObject);

Var
  P : TPoint;
  H : THyperLinkItem;

begin
  P:=PBPreview.ScreenToClient(Mouse.CursorPos);
  if Not Assigned(FRender.HyperLinks) then
    Raise EReportExportError.Create(SErrNoHyperlinkSupport);
  H:=FRender.HyperLinks.FindLinkAtPoint(P);
  if Assigned(H) then
    ShowHyperLink(H.URL);
end;

procedure TFPReportPreviewForm.PBPreviewMouseLeave(Sender: TObject);
begin
  if Screen.Cursor=crHandPoint then
    Screen.Cursor:=crDefault;
end;

procedure TFPReportPreviewForm.PBPreviewMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);

Var
  H : THyperLinkItem;

begin
  if Assigned(FRender.HyperLinks) then
    begin
    H:=FRender.HyperLinks.FindLinkAtPoint(Point(X,Y));
    If Assigned(h) then
      Screen.Cursor:=crHandPoint
    else
      Screen.Cursor:=crDefault;
    end;
end;


procedure TFPReportPreviewForm.ExportReport(REC : TFPReportExporterClass);

Var
  E : TFPReportExporter;

begin
  E:=Rec.Create(Self);
  try
    E.Report:=Self.Report;
    if E.ShowConfig then
      E.Execute;
  finally
    E.Free;
  end;
end;

procedure TFPReportPreviewForm.DoExport(Sender : TObject);

Var
  REC : TFPReportExporterClass;

begin
  REC:=(Sender As TExportMenuItem).TheClass;
  ExportReport(REC);
end;

procedure TFPReportPreviewForm.FillExportMenu;

Var
  REC : TFPReportExporterClass;
  I: Integer;
  MI : TExportMenuItem;

begin
  PMExport.Items.Clear;
  For I:=0 to ReportExportManager.ExporterCount-1 do
    begin
    REC:=ReportExportManager.Exporter[i];
    if REc<>TFPreportPreviewExport then
      begin
      MI:=TExportMenuItem.Create(Self);
      MI.TheClass:=Rec;
      MI.Caption:=Rec.Description;
      MI.Hint:=Rec.Description;
      MI.OnClick:=@DoExport;
      PMExport.Items.Add(MI);
      end;
    end;
end;

initialization
  TFPreportPreviewExport.DefaultPreviewFormClass:=TFPReportPreviewForm;
end.