LazMapViewer: Extend fulldemo to show how a map can be printed.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8273 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-05-03 11:46:11 +00:00
parent 92fa4664af
commit 3c11c8c7d0
4 changed files with 110 additions and 5 deletions

View File

@ -25,13 +25,16 @@
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<RequiredPackages Count="2">
<RequiredPackages Count="3">
<Item1>
<PackageName Value="lazMapViewerPkg"/>
<PackageName Value="Printer4Lazarus"/>
</Item1>
<Item2>
<PackageName Value="LCL"/>
<PackageName Value="lazMapViewerPkg"/>
</Item2>
<Item3>
<PackageName Value="LCL"/>
</Item3>
</RequiredPackages>
<Units Count="5">
<Unit0>

View File

@ -5,7 +5,7 @@ program MapViewer_Demo;
uses
{$IFDEF UNIX}cthreads,{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, Main, gpslistform, globals, gpsptform
Forms, printer4lazarus, Main, gpslistform, globals, gpsptform
{ you can add units after this };
{$R *.res}

View File

@ -536,6 +536,22 @@ object MainForm: TMainForm
OnClick = BtnLoadGPXFileClick
TabOrder = 8
end
object BtnPrintMap: TButton
AnchorSideLeft.Control = BtnSaveToFile
AnchorSideTop.Control = BtnSaveToFile
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = BtnSaveToFile
AnchorSideRight.Side = asrBottom
Left = 6
Height = 25
Top = 550
Width = 110
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 8
Caption = 'Print...'
OnClick = BtnPrintMapClick
TabOrder = 9
end
end
object PgConfig: TTabSheet
Caption = 'Config'
@ -1255,4 +1271,8 @@ object MainForm: TMainForm
69754D32C924FBBF68FF0B9EFE66B6
}
end
object PrintDialog1: TPrintDialog
Left = 552
Top = 567
end
end

View File

@ -6,7 +6,7 @@ interface
uses
Classes, SysUtils, Types, Forms, Controls, Graphics, Dialogs,
ExtCtrls, StdCtrls, ComCtrls, Buttons, IntfGraphics, ColorBox,
ExtCtrls, StdCtrls, ComCtrls, Buttons, IntfGraphics, ColorBox, PrintersDlgs,
mvGeoNames, mvMapViewer, mvTypes, mvGpsObj, mvDrawingEngine;
type
@ -21,6 +21,7 @@ type
BtnSaveToFile: TButton;
BtnLoadGPXFile: TButton;
BtnPOITextFont: TButton;
BtnPrintMap: TButton;
CbDoubleBuffer: TCheckBox;
CbFoundLocations: TComboBox;
CbLocations: TComboBox;
@ -63,10 +64,12 @@ type
PgData: TTabSheet;
PgConfig: TTabSheet;
POIImages: TImageList;
PrintDialog1: TPrintDialog;
rgPOIMode: TRadioGroup;
ZoomTrackBar: TTrackBar;
procedure BtnGoToClick(Sender: TObject);
procedure BtnLoadGPXFileClick(Sender: TObject);
procedure BtnPrintMapClick(Sender: TObject);
procedure BtnSearchClick(Sender: TObject);
procedure BtnGPSPointsClick(Sender: TObject);
procedure BtnSaveToFileClick(Sender: TObject);
@ -122,6 +125,7 @@ implementation
uses
LCLType, IniFiles, Math, FPCanvas, FPImage, GraphType,
Printers, OSPrinters,
mvEngine, mvGPX,
globals, gpsPtForm, gpslistform;
@ -233,6 +237,84 @@ begin
end;
end;
procedure TMainForm.BtnPrintMapClick(Sender: TObject);
const
INCH = 25.4; // 1" = 25.4 mm
HOR_MARGIN = 10.0; // mm
VERT_MARGIN = 10.0; // mm
function px2mm(px, ppi: Integer): Double;
begin
Result := px / ppi * INCH;
end;
function mm2px(mm: Double; ppi: Integer): Integer;
begin
Result := round(mm / INCH * ppi);
end;
var
bmp: TBitmap;
PrintedRect: TRect;
aspectRatio_map: Double;
aspectRatio_page: Double;
mapWidth_mm: Double;
mapHeight_mm: Double;
pageWidth_mm: Double;
pageHeight_mm: Double;
printedWidth_mm: Double;
printedHeight_mm: Double;
factor: Double;
begin
if PrintDialog1.Execute then
begin
Printer.Orientation := poPortrait;
// Scale map such that it fits into the width of the printer paper with
// a margin at all sides
mapWidth_mm := px2mm(MapView.Width, ScreenInfo.PixelsPerInchX);
mapHeight_mm := px2mm(MapView.Height, ScreenInfo.PixelsPerInchY);
pageWidth_mm := px2mm(Printer.PageWidth, Printer.XDPI) - 2*HOR_MARGIN;
pageHeight_mm := px2mm(Printer.PageHeight, Printer.YDPI) - 2*VERT_MARGIN;
aspectRatio_map := mapHeight_mm / mapWidth_mm;
aspectRatio_page := pageHeight_mm / pageWidth_mm;
if aspectRatio_Map > aspectRatio_page then
begin
factor := pageHeight_mm / mapHeight_mm;
printedWidth_mm := mapWidth_mm * factor;
printedHeight_mm := pageHeight_mm;
end else
begin
factor := pageWidth_mm / mapWidth_mm;
printedWidth_mm := pageWidth_mm;
printedHeight_mm := mapHeight_mm * factor;
end;
// Calculate size of the output rectangle (on paper).
PrintedRect := Rect(0, 0, mm2px(printedWidth_mm, Printer.XDPI), mm2px(printedHeight_mm, Printer.YDPI));
// Center output rectangle horizontally on page
OffsetRect(PrintedRect, mm2px(HOR_MARGIN + (pageWidth_mm-printedWidth_mm) / 2 , Printer.XDPI), mm2px(VERT_MARGIN, Printer.YDPI));
// Begin printing
Printer.BeginDoc;
try
// Catch the displayed images as a bitmap.
bmp := TBitmap.Create;
try
bmp.SetSize(MapView.Width, MapView.Height);
MapView.DrawingEngine.PaintToCanvas(bmp.Canvas);
// Stretch-draw the map image to the output rectangle on the printer.
Printer.Canvas.StretchDraw(PrintedRect, bmp);
finally
bmp.Free;
end;
finally
Printer.EndDoc;
end;
end;
end;
procedure TMainForm.BtnSaveToFileClick(Sender: TObject);
begin
MapView.SaveToFile(TPortableNetworkGraphic, 'mapview.png');