{ QRCode 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 QRCode;

{$mode objfpc}{$H+}

interface

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

type

  { TQRCode }

  { TCustomQRCode }

  TCustomQRCode = class(TGraphicControl)
  private
    FBackground: TColor;
    FCornerLine: Cardinal;
    FCorrectionLevel: TErrorCorrectionOrdinal;
    FDrawingMode: TQRDrawingMode;
    FEncoding: TQRCodeEncoding;
    FForeground: TColor;
    FProportional: boolean;
    FQuietZone: Cardinal;
    FText: string;
    procedure SetBackground(AValue: TColor);
    procedure SetCornerLine(AValue: Cardinal);
    procedure SetCorrectionLevel(AValue: TErrorCorrectionOrdinal);
    procedure SetDrawingMode(AValue: TQRDrawingMode);
    procedure SetEncoding(AValue: TQRCodeEncoding);
    procedure SetForeground(AValue: TColor);
    procedure SetProportional(AValue: boolean);
    procedure SetQuietZone(AValue: Cardinal);
    procedure SetText(AValue: string);
  protected
    FQRCode: TDelphiZXingQRCode;
    procedure Paint; override;
    class function GetControlClassDefaultSize: TSize; override;
    procedure RemakeQR;
    function GetText: string; virtual;
    property Text:string read GetText write SetText;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Encoding:TQRCodeEncoding read FEncoding write SetEncoding default qreAuto;
    property CorrectionLevel:TErrorCorrectionOrdinal read FCorrectionLevel write SetCorrectionLevel default ecoL;
    property DrawingMode:TQRDrawingMode read FDrawingMode write SetDrawingMode default drwBitmap;
      //Corner line thickness (pixels)
    property CornerLine:Cardinal read FCornerLine write SetCornerLine;
    property QuietZone:Cardinal read FQuietZone write SetQuietZone default 4;
    property Background:TColor read FBackground write SetBackground default clWhite;
    property Foreground:TColor read FForeground write SetForeground default clBlack;
    property Proportional:boolean read FProportional write SetProportional default true;
  end;

  TQRCode = class(TCustomQRCode)

  published
    property Text;
  end;

procedure Register;

implementation
uses QR_URL, QR_Win1251;

procedure Register;
begin
  {$I qrcode_icon.lrs}
  RegisterComponents('Misc',[TQRCode]);
end;

{ TQRCode }

procedure TCustomQRCode.SetCorrectionLevel(AValue: TErrorCorrectionOrdinal);
begin
  if FCorrectionLevel=AValue then Exit;
  FCorrectionLevel:=AValue;
  RemakeQR;
end;

procedure TCustomQRCode.SetCornerLine(AValue: Cardinal);
begin
  if FCornerLine=AValue then Exit;
  FCornerLine:=AValue;
  Invalidate;
end;

procedure TCustomQRCode.SetBackground(AValue: TColor);
begin
  if FBackground=AValue then Exit;
  FBackground:=AValue;
  Invalidate;
end;

function TCustomQRCode.GetText: string;
begin
  Result:=FText;
end;

procedure TCustomQRCode.SetDrawingMode(AValue: TQRDrawingMode);
begin
  if FDrawingMode=AValue then Exit;
  FDrawingMode:=AValue;
  Invalidate;
end;

procedure TCustomQRCode.SetEncoding(AValue: TQRCodeEncoding);
begin
  if FEncoding=AValue then Exit;
  FEncoding:=AValue;
  RemakeQR;
end;

procedure TCustomQRCode.SetForeground(AValue: TColor);
begin
  if FForeground=AValue then Exit;
  FForeground:=AValue;
  Invalidate;
end;

procedure TCustomQRCode.SetProportional(AValue: boolean);
begin
  if FProportional=AValue then Exit;
  FProportional:=AValue;
  Invalidate;
end;

procedure TCustomQRCode.SetQuietZone(AValue: Cardinal);
begin
  if FQuietZone=AValue then Exit;
  FQuietZone:=AValue;
  RemakeQR;
end;

procedure TCustomQRCode.SetText(AValue: string);
begin
  if FText=AValue then Exit;
  FText:=AValue;
  RemakeQR;
end;

procedure TCustomQRCode.Paint;
begin
  Canvas.Pen.Color := FForeground;
  Canvas.Brush.Color := FBackground;
  DrawQR(Canvas, ClientRect, FQRCode, FCornerLine, FDrawingMode, FProportional);
end;

class function TCustomQRCode.GetControlClassDefaultSize: TSize;
begin
  Result.CX := 105;
  Result.CY := 105;
end;

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

constructor TCustomQRCode.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := ControlStyle + [csReplicatable];
  with GetControlClassDefaultSize do
    SetInitialBounds(0, 0, CX, CY);

  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 TCustomQRCode.Destroy;
begin
  FreeAndNil(FQRCode);
  inherited Destroy;
end;

end.
