mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-12 03:56:12 +02:00
LazReport, implemented template for printgrid
git-svn-id: trunk@17784 -
This commit is contained in:
parent
7163cdb30d
commit
90510e4cb6
@ -20,7 +20,7 @@ interface
|
|||||||
{$I LR_Vers.inc}
|
{$I LR_Vers.inc}
|
||||||
|
|
||||||
uses
|
uses
|
||||||
SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
|
SysUtils, Classes, Graphics, Controls, Forms, Dialogs, PropEdits,
|
||||||
DB, DBGrids, Printers, LR_DSet, LR_DBSet, LR_Class;
|
DB, DBGrids, Printers, LR_DSet, LR_DBSet, LR_Class;
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -53,6 +53,7 @@ type
|
|||||||
FWidth : Integer;
|
FWidth : Integer;
|
||||||
FDataSet : TDataset;
|
FDataSet : TDataset;
|
||||||
FColumnsInfo : array of TColumnInfo;
|
FColumnsInfo : array of TColumnInfo;
|
||||||
|
FTemplate : string;
|
||||||
|
|
||||||
procedure OnEnterRect(Memo: TStringList; View: TfrView);
|
procedure OnEnterRect(Memo: TStringList; View: TfrView);
|
||||||
procedure OnPrintColumn(ColNo: Integer; var Width: Integer);
|
procedure OnPrintColumn(ColNo: Integer; var Width: Integer);
|
||||||
@ -61,6 +62,8 @@ type
|
|||||||
{ Protected declarations }
|
{ Protected declarations }
|
||||||
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
|
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
|
||||||
procedure SetupColumns;
|
procedure SetupColumns;
|
||||||
|
function FindBand(APage: TFrPage; AType: TfrBandType): TFrBandView;
|
||||||
|
procedure ReplaceTemplate(APage:TFrPage; ABand: TFrBandView; ATemplate,AReplace:String);
|
||||||
public
|
public
|
||||||
constructor Create(AOwner: TComponent); override;
|
constructor Create(AOwner: TComponent); override;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
@ -72,6 +75,7 @@ type
|
|||||||
property Font: TFont read FFont write FFont;
|
property Font: TFont read FFont write FFont;
|
||||||
property TitleFont : TFont read FTitleFont write FTitleFont;
|
property TitleFont : TFont read FTitleFont write FTitleFont;
|
||||||
property Caption: String read FCaption write FCaption;
|
property Caption: String read FCaption write FCaption;
|
||||||
|
property Template: string read FTemplate write FTemplate;
|
||||||
property ShowCaption: Boolean read FShowCaption write FShowCaption;
|
property ShowCaption: Boolean read FShowCaption write FShowCaption;
|
||||||
property ShowHeaderOnAllPage : boolean read fShowHdOnAllPage write fShowHdOnAllPage default True;
|
property ShowHeaderOnAllPage : boolean read fShowHdOnAllPage write fShowHdOnAllPage default True;
|
||||||
property ShowProgress : Boolean read fShowProgress write fShowProgress default false;
|
property ShowProgress : Boolean read fShowProgress write fShowProgress default false;
|
||||||
@ -81,10 +85,6 @@ type
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
type
|
|
||||||
THackDBGrid = class(TDBGrid)
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ TfrPrintGrid }
|
{ TfrPrintGrid }
|
||||||
|
|
||||||
constructor TfrPrintGrid.Create(AOwner: TComponent);
|
constructor TfrPrintGrid.Create(AOwner: TComponent);
|
||||||
@ -142,6 +142,38 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TfrPrintGrid.FindBand(APage: TFrPage; AType:TfrBandType): TFrBandView;
|
||||||
|
var
|
||||||
|
i: Integer;
|
||||||
|
begin
|
||||||
|
for i:=0 to APage.Objects.Count-1 do begin
|
||||||
|
if not (TObject(APage.Objects[i]) is TFrBandView) then
|
||||||
|
continue;
|
||||||
|
Result := TFrBandView(APage.Objects[i]);
|
||||||
|
if Result.BandType=AType then
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
result := nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TfrPrintGrid.ReplaceTemplate(APage: TFrPage; ABand: TFrBandView;
|
||||||
|
ATemplate, AReplace: String);
|
||||||
|
var
|
||||||
|
i: Integer;
|
||||||
|
Obj: TfrObject;
|
||||||
|
begin
|
||||||
|
for i:=0 to APage.Objects.Count-1 do begin
|
||||||
|
Obj := TfrObject(APage.Objects[i]);
|
||||||
|
if Obj is TfrMemoView then begin
|
||||||
|
if (Obj.y>=ABand.y) and (Obj.y<(ABand.Y+ABand.Dy)) then begin
|
||||||
|
// this memo is on ABand
|
||||||
|
TfrMemoView(Obj).Memo.Text := StringReplace(TfrMemoView(Obj).Memo.Text,
|
||||||
|
ATemplate, AReplace, [rfReplaceAll, rfIgnoreCase]);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TfrPrintGrid.SetDBGrid(const AValue: TDBGrid);
|
procedure TfrPrintGrid.SetDBGrid(const AValue: TDBGrid);
|
||||||
begin
|
begin
|
||||||
fDBGrid:=aValue;
|
fDBGrid:=aValue;
|
||||||
@ -155,16 +187,23 @@ end;
|
|||||||
procedure TfrPrintGrid.PreviewReport;
|
procedure TfrPrintGrid.PreviewReport;
|
||||||
var
|
var
|
||||||
v: TfrView;
|
v: TfrView;
|
||||||
b: TfrBandView;
|
b,h: TfrBandView;
|
||||||
Page: TfrPage;
|
Page: TfrPage;
|
||||||
BM : TBookMark;
|
BM : TBookMark;
|
||||||
|
yPos: Integer;
|
||||||
begin
|
begin
|
||||||
if (FDBGrid = nil) or (DBGrid.Datasource = nil) or
|
if (FDBGrid = nil) or (DBGrid.Datasource = nil) or
|
||||||
(DBGrid.Datasource.Dataset = nil) then Exit;
|
(DBGrid.Datasource.Dataset = nil) then Exit;
|
||||||
|
|
||||||
FDataSet := DBGrid.Datasource.Dataset;
|
if (FTemplate<>'') and not FileExists(FTemplate) then
|
||||||
|
raise Exception.CreateFmt('Template file %s does not exists',[FTemplate]);
|
||||||
|
|
||||||
FReport := TfrReport.Create(Self);
|
FReport := TfrReport.Create(Self);
|
||||||
|
if FTemplate<>'' then
|
||||||
|
FReport.LoadFromFile(FTemplate);
|
||||||
|
|
||||||
|
FDataSet := DBGrid.Datasource.Dataset;
|
||||||
|
|
||||||
FReport.OnEnterRect :=@OnEnterRect;
|
FReport.OnEnterRect :=@OnEnterRect;
|
||||||
FReport.OnPrintColumn:=@OnPrintColumn;
|
FReport.OnPrintColumn:=@OnPrintColumn;
|
||||||
FReport.ShowProgress :=fShowProgress;
|
FReport.ShowProgress :=fShowProgress;
|
||||||
@ -182,13 +221,29 @@ begin
|
|||||||
|
|
||||||
try
|
try
|
||||||
FReportDataSet.DataSource := DBGrid.DataSource;
|
FReportDataSet.DataSource := DBGrid.DataSource;
|
||||||
FReport.Pages.Add;
|
if FReport.Pages.Count=0 then
|
||||||
Page := FReport.Pages[0];
|
FReport.Pages.add;
|
||||||
|
Page := FReport.Pages[FReport.Pages.Count-1];
|
||||||
|
|
||||||
with Page do
|
with Page do
|
||||||
ChangePaper(pgSize, Width, Height, FOrientation);
|
ChangePaper(pgSize, Width, Height, FOrientation);
|
||||||
|
|
||||||
if FShowCaption then
|
YPos := -1;
|
||||||
begin
|
b := FindBand(Page, btReportTitle);
|
||||||
|
if b<>nil then begin
|
||||||
|
if FShowCaption then
|
||||||
|
ReplaceTemplate(Page, b, '<title>', FCaption);
|
||||||
|
YPos := b.y + b.dy + 10;
|
||||||
|
end;
|
||||||
|
|
||||||
|
h := FindBand(Page, btPageHeader);
|
||||||
|
if h<>nil then begin
|
||||||
|
if FShowCaption then
|
||||||
|
ReplaceTemplate(Page, h, '<title>', FCaption);
|
||||||
|
YPos := h.y + h.dy + 10;
|
||||||
|
end;
|
||||||
|
|
||||||
|
if FShowCaption and (b=nil) and (h=nil) then begin
|
||||||
b := TfrBandView(frCreateObject(gtBand, ''));
|
b := TfrBandView(frCreateObject(gtBand, ''));
|
||||||
b.SetBounds(10, 20, 1000, 25);
|
b.SetBounds(10, 20, 1000, 25);
|
||||||
b.BandType := btReportTitle;
|
b.BandType := btReportTitle;
|
||||||
@ -199,17 +254,21 @@ begin
|
|||||||
TfrMemoView(v).Font.Assign(FTitleFont);
|
TfrMemoView(v).Font.Assign(FTitleFont);
|
||||||
v.Memo.Add(FCaption);
|
v.Memo.Add(FCaption);
|
||||||
Page.Objects.Add(v);
|
Page.Objects.Add(v);
|
||||||
|
YPos := b.y + b.dy + 10;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
if YPos<0 then
|
||||||
|
YPos := 60;
|
||||||
|
|
||||||
b := TfrBandView(frCreateObject(gtBand, ''));
|
b := TfrBandView(frCreateObject(gtBand, ''));
|
||||||
b.BandType := btMasterHeader;
|
b.BandType := btMasterHeader;
|
||||||
if self.fShowHdOnAllPage then
|
if self.fShowHdOnAllPage then
|
||||||
b.Flags:=b.Flags+flBandRepeatHeader;
|
b.Flags:=b.Flags+flBandRepeatHeader;
|
||||||
b.SetBounds(20, 60, 1000, 20);
|
b.SetBounds(20, YPos, 1000, 20);
|
||||||
Page.Objects.Add(b);
|
Page.Objects.Add(b);
|
||||||
|
|
||||||
v := frCreateObject(gtMemo, '');
|
v := frCreateObject(gtMemo, '');
|
||||||
v.SetBounds(20, 60, 20, 20);
|
v.SetBounds(20, YPos, 20, 20);
|
||||||
TfrMemoView(v).Alignment:=taCenter;
|
TfrMemoView(v).Alignment:=taCenter;
|
||||||
TfrMemoView(v).FillColor := clSilver;
|
TfrMemoView(v).FillColor := clSilver;
|
||||||
TfrMemoView(v).Font.Assign(FTitleFont);
|
TfrMemoView(v).Font.Assign(FTitleFont);
|
||||||
@ -218,10 +277,12 @@ begin
|
|||||||
v.Memo.Add('[Header]');
|
v.Memo.Add('[Header]');
|
||||||
Page.Objects.Add(v);
|
Page.Objects.Add(v);
|
||||||
|
|
||||||
|
YPos := YPos + 40;
|
||||||
|
|
||||||
b := TfrBandView(frCreateObject(gtBand, ''));
|
b := TfrBandView(frCreateObject(gtBand, ''));
|
||||||
b.BandType := btMasterData;
|
b.BandType := btMasterData;
|
||||||
b.Dataset := FReportDataSet.Name;
|
b.Dataset := FReportDataSet.Name;
|
||||||
b.SetBounds(0, 100, 1000, 18);
|
b.SetBounds(0, YPos, 1000, 18);
|
||||||
Page.Objects.Add(b);
|
Page.Objects.Add(b);
|
||||||
|
|
||||||
b := TfrBandView(frCreateObject(gtBand, ''));
|
b := TfrBandView(frCreateObject(gtBand, ''));
|
||||||
@ -231,7 +292,7 @@ begin
|
|||||||
Page.Objects.Add(b);
|
Page.Objects.Add(b);
|
||||||
|
|
||||||
v := frCreateObject(gtMemo, '');
|
v := frCreateObject(gtMemo, '');
|
||||||
v.SetBounds(20, 100, 20, 18);
|
v.SetBounds(20, YPos, 20, 18);
|
||||||
v.Memo.Add('[Cell]');
|
v.Memo.Add('[Cell]');
|
||||||
TfrMemoView(v).Font.Assign(FFont);
|
TfrMemoView(v).Font.Assign(FFont);
|
||||||
TfrMemoView(v).Frames:=frAllFrames;
|
TfrMemoView(v).Frames:=frAllFrames;
|
||||||
@ -265,7 +326,7 @@ begin
|
|||||||
exit;
|
exit;
|
||||||
|
|
||||||
C := TColumn(DbGrid.Columns[FColumnsInfo[i].Column]);
|
C := TColumn(DbGrid.Columns[FColumnsInfo[i].Column]);
|
||||||
if C<>nil then
|
if (C<>nil)and(Memo.Count>0) then
|
||||||
begin
|
begin
|
||||||
if (Memo[0]='[Cell]') and (C.Field<>nil) then
|
if (Memo[0]='[Cell]') and (C.Field<>nil) then
|
||||||
begin
|
begin
|
||||||
@ -288,5 +349,7 @@ begin
|
|||||||
Width := FColumnsInfo[ColNo-1].ColumnWidth;
|
Width := FColumnsInfo[ColNo-1].ColumnWidth;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
initialization
|
||||||
|
RegisterPropertyEditor(TypeInfo(AnsiString),
|
||||||
|
TFrPrintGrid,'Template',TFileNamePropertyEditor);
|
||||||
end.
|
end.
|
||||||
|
Loading…
Reference in New Issue
Block a user