{ lrQRCode unit

  Copyright (C) 2017 Lagunov Aleksey alexs75@yandex.ru

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  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. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}

unit lrQRCode;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Types, LResources, Forms, Controls, Graphics, Dialogs,
  LR_Class, QRCodeCommon, DelphiZXingQRCode, QRGraphics;

type
  TlrQRCode = class(TComponent)
  private

  protected

  public

  published

  end;

  { TlrQRCodeView }

  TlrQRCodeView = class(TfrView)
  private
    FBackground: TColor;
    FCornerLine: Cardinal;
    FCorrectionLevel: TErrorCorrectionOrdinal;
    FDrawingMode: TQRDrawingMode;
    FEncoding: TQRCodeEncoding;
    FForeground: TColor;
    FProportional: boolean;
    FQuietZone: Cardinal;
    FQRCode: TDelphiZXingQRCode;
    procedure RemakeQR;

    function CreateBarcode: TBitmap;
  public
    constructor Create(AOwnerPage:TfrPage);override;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
    procedure LoadFromStream(Stream: TStream); override;
    procedure SaveToStream(Stream: TStream); override;
    procedure Draw(aCanvas: TCanvas); override;
    procedure Print(Stream: TStream); override;
//    procedure DefinePopupMenu({%H-}Popup: TPopupMenu); override;
    procedure LoadFromXML(XML: TLrXMLConfig; const Path: String); override;
    procedure SaveToXML(XML: TLrXMLConfig; const Path: String); override;

  published
    property Encoding:TQRCodeEncoding read FEncoding write FEncoding default qreAuto;
    property CorrectionLevel:TErrorCorrectionOrdinal read FCorrectionLevel write FCorrectionLevel default ecoL;
    property DrawingMode:TQRDrawingMode read FDrawingMode write FDrawingMode default drwBitmap;
    property CornerLine:Cardinal read FCornerLine write FCornerLine;
    property QuietZone:Cardinal read FQuietZone write FQuietZone default 4;
    property Background:TColor read FBackground write FBackground default clWhite;
    property Foreground:TColor read FForeground write FForeground default clBlack;
    property Proportional:boolean read FProportional write FProportional default true;

    property Memo;
    property Frames;
    property FrameColor;
    property FrameStyle;
    property FrameWidth;
    property Restrictions;
  end;

procedure Register;

implementation
uses QR_URL, QR_Win1251, LR_Utils;

{$R lrqrcode.res}

procedure Register;
begin
//  {$I lrqrcode_icon.lrs}
  RegisterComponents('LazReport',[TlrQRCode]);
end;


var
  lrBMP_QRCodeView : TBitmap = nil;

procedure InitLRComp;
begin
  if not assigned(lrBMP_QRCodeView) then
  begin
    lrBMP_QRCodeView := TBitmap.Create;
    lrBMP_QRCodeView.LoadFromResourceName(0, TlrQRCodeView.ClassName);
    frRegisterObject(TlrQRCodeView, lrBMP_QRCodeView, TlrQRCodeView.ClassName, nil, otlReportView, nil);
  end;
end;

{ TlrQRCodeView }

procedure TlrQRCodeView.RemakeQR;
begin
  FQRCode.BeginUpdate;
  FQRCode.Data := Trim(GetText);
  FQRCode.Encoding := Ord(Encoding);
  FQRCode.ErrorCorrectionOrdinal := FCorrectionLevel;
  FQRCode.QuietZone := FQuietZone;
  FQRCode.EndUpdate(True);
end;

function TlrQRCodeView.CreateBarcode: TBitmap;
var
  R: TRect;
begin
  RemakeQR;
  Result:=TBitMap.Create;
  Result.Width:=dx;
  Result.Height:=dy;
  Result.Canvas.Brush.Style:=bsSolid;
  Result.Canvas.Brush.Color:=FBackground;
  R:=Rect(0,0,dx,dy);
  Result.Canvas.FillRect(R);
  DrawQR(Result.Canvas, R, FQRCode, FCornerLine, FDrawingMode, FProportional);
end;

constructor TlrQRCodeView.Create(AOwnerPage: TfrPage);
begin
  inherited Create(AOwnerPage);

  FEncoding:=qreAuto;
  FCorrectionLevel:=ecoL;
  FDrawingMode:=drwBitmap;
  FQuietZone:=4;
  FBackground:=clWhite;
  FForeground:=clBlack;
  FProportional:=true;


  FQRCode := TDelphiZXingQRCode.Create;
  FQRCode.RegisterEncoder(ENCODING_WIN1251, TWin1251Encoder);
  FQRCode.RegisterEncoder(ENCODING_URL, TURLEncoder);
end;

destructor TlrQRCodeView.Destroy;
begin
  FreeAndNil(FQRCode);
  inherited Destroy;
end;

procedure TlrQRCodeView.Assign(Source: TPersistent);
begin
  inherited Assign(Source);
  if Source is TlrQRCodeView then
  begin
    FBackground:=TlrQRCodeView(Source).FBackground;
    FCornerLine:=TlrQRCodeView(Source).FCornerLine;
    FCorrectionLevel:=TlrQRCodeView(Source).FCorrectionLevel;
    FDrawingMode:=TlrQRCodeView(Source).FDrawingMode;
    FEncoding:=TlrQRCodeView(Source).FEncoding;
    FForeground:=TlrQRCodeView(Source).FForeground;
    FProportional:=TlrQRCodeView(Source).FProportional;
    FQuietZone:=TlrQRCodeView(Source).FQuietZone;
  end;
end;
{
function TlrQRCodeView.GenerateBitmap: TBitmap;
begin

end;
}
procedure TlrQRCodeView.LoadFromStream(Stream: TStream);
begin
  inherited LoadFromStream(Stream);
  Stream.Read(FEncoding, SizeOf(TQRCodeEncoding));
  Stream.Read(FCorrectionLevel, SizeOf(TErrorCorrectionOrdinal));
  Stream.Read(FDrawingMode, SizeOf(TQRDrawingMode));
  Stream.Read(FCornerLine, SizeOf(FCornerLine));
  Stream.Read(FQuietZone, SizeOf(FQuietZone));
  Stream.Read(FBackground, SizeOf(FBackground));
  Stream.Read(FForeground, SizeOf(FForeground));
  Stream.Read(FProportional, SizeOf(FProportional));
end;

procedure TlrQRCodeView.SaveToStream(Stream: TStream);
begin
  inherited SaveToStream(Stream);
  Stream.Write(FEncoding, SizeOf(TQRCodeEncoding));
  Stream.Write(FCorrectionLevel, SizeOf(TErrorCorrectionOrdinal));
  Stream.Write(FDrawingMode, SizeOf(TQRDrawingMode));
  Stream.Write(FCornerLine, SizeOf(FCornerLine));
  Stream.Write(FQuietZone, SizeOf(FQuietZone));
  Stream.Write(FBackground, SizeOf(FBackground));
  Stream.Write(FForeground, SizeOf(FForeground));
  Stream.Write(FProportional, SizeOf(FProportional));
end;

procedure TlrQRCodeView.Draw(aCanvas: TCanvas);
var
  Bmp: TBitmap;
begin
  BeginDraw(aCanvas);
  Bmp := CreateBarcode;
  if Bmp <> nil then
  try
    CalcGaps;
    ShowBackground;
    aCanvas.StretchDraw(DRect,Bmp);
    ShowFrame;
  finally
    Bmp.Free;
    RestoreCoord;
  end;

{  BeginDraw(aCanvas);

  try
    aCanvas.Pen.Color := FForeground;
    aCanvas.Brush.Color := FBackground;
    RemakeQR;
    CalcGaps;
    DrawQR(aCanvas, Rect(DRect.Left,DRect.Top,
                      DRect.Right ,
                      DRect.Bottom), FQRCode, FCornerLine, FDrawingMode, FProportional);
    ShowFrame;
  finally
    RestoreCoord;
  end;}

end;

procedure TlrQRCodeView.Print(Stream: TStream);
begin
  BeginDraw(Canvas);
  Memo1.Assign(Memo);
  CurReport.InternalOnEnterRect(Memo1, Self);
  frInterpretator.DoScript(Script);
  if not Visible then Exit;

  if Memo1.Count > 0 then
    if (Length(Memo1[0]) > 0) and (Pos('[',Memo1[0])<>0) then
      Memo1[0] := frParser.Calc(Memo1[0]);
  Stream.Write(Typ, 1);
  frWriteString(Stream, ClassName);
  SaveToStream(Stream);
end;

procedure TlrQRCodeView.LoadFromXML(XML: TLrXMLConfig; const Path: String);
begin
  inherited LoadFromXML(XML, Path);

  FEncoding := TQRCodeEncoding(XML.GetValue(Path+'Encoding/Value'{%H-}, 0));
  FCorrectionLevel := TErrorCorrectionOrdinal(XML.GetValue(Path+'CorrectionLevel/Value'{%H-}, 0));
  FDrawingMode := TQRDrawingMode(XML.GetValue(Path+'DrawingMode/Value'{%H-}, 0));
  FCornerLine := XML.GetValue(Path+'CornerLine/Value'{%H-}, 0);
  FQuietZone := XML.GetValue(Path+'QuietZone/Value'{%H-}, 0);
  FBackground := TColor(XML.GetValue(Path+'Background/Value'{%H-}, clWhite));
  FForeground := TColor(XML.GetValue(Path+'Foreground/Value'{%H-}, clBlack));
  FProportional := XML.GetValue(Path+'Proportional/Value'{%H-}, true);
end;

procedure TlrQRCodeView.SaveToXML(XML: TLrXMLConfig; const Path: String);
begin
  inherited SaveToXML(XML, Path);
  XML.SetValue(Path+'Encoding/Value', Ord(FEncoding));
  XML.SetValue(Path+'CorrectionLevel/Value', Ord(FCorrectionLevel));
  XML.SetValue(Path+'DrawingMode/Value', Ord(FDrawingMode));
  XML.SetValue(Path+'CornerLine/Value', FCornerLine);
  XML.SetValue(Path+'QuietZone/Value', FQuietZone);
  XML.SetValue(Path+'Background/Value', FBackground);
  XML.SetValue(Path+'Foreground/Value', FForeground);
  XML.SetValue(Path+'Proportional/Value', FProportional);
end;

initialization
  InitLRComp;

finalization
  if Assigned(lrBMP_QRCodeView) then
    FreeAndNil(lrBMP_QRCodeView);
end.
