{*********************************************}
{  TeeChart Pro Charting Library              }
{  Filters Gallery                            }
{  Copyright (c) 2006-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit TeeFilterGallery;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  Windows, Messages,
  {$ENDIF}
  SysUtils, Classes,
  Graphics, Controls, Forms, Dialogs, ExtCtrls, ComCtrls, StdCtrls,

  {$IFDEF LCL}
  jpeglib,
  {$ELSE}
  jpeg,
  {$ENDIF}

  TeeProcs, TeCanvas, TeeGalleryPanel, TeeFilters, Chart, Series;

type
  TFilterGallery = class(TForm)
    Panel1: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    ChartGalleryPanel1: TChartGalleryPanel;
    Panel2: TPanel;
    BOK: TButton;
    Button2: TButton;
    SampleImage: TImage;
    procedure ChartGalleryPanel1SelectedChart(Sender: TObject);
    procedure BOKClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure ChartGalleryPanel1ChangeChart(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    ISample : TPicture;
    Sample  : TGraphic;
    SelectedFilter : TFilterClass;

    procedure AddItems;
    function CurrentGallery:TChartGalleryPanel;
    //function GetSampleImage:TGraphic;
  public
    { Public declarations }
    class function Choose(AOwner:TComponent; const ASample:TGraphic=nil):TFilterClass;
    //class procedure StretchTo(const Graphic:TGraphic; const Picture:TPicture; AWidth,AHeight:Integer);
  end;

implementation

uses
  Math, TeEngine, TeeConst;

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

{ TFilterGallery }

class function TFilterGallery.Choose(AOwner: TComponent; const ASample:TGraphic=nil): TFilterClass;
begin
  result:=nil;

  with TFilterGallery.Create(AOwner) do
  try
    // Check max image size to resize it smaller.
    // ResizeSample;

    Sample:=ASample;

    if ShowModal=mrOk then
       result:=SelectedFilter;
  finally
    Free;
  end;
end;

procedure TFilterGallery.AddItems;
var tmpTotal,
    tmpRows,
    t         : Integer;
    tmpType   : TTeeSeriesType;
    tmpFilter : TFilterClass;
    tmpGall   : TChartGalleryPanel;
    tmpTab    : TTabSheet;
begin
  tmpType:=TeeSeriesTypes.Find(TLineSeries);

  tmpGall:=ChartGalleryPanel1;
  tmpGall.DisplaySub:=False;

  tmpRows:=tmpGall.NumRows;
  if tmpRows<4 then
     tmpRows:=4;

  tmpTotal:=tmpRows*tmpGall.NumCols;

  for t:=0 to TeeFilterClasses.Count-1 do
  begin
    if (t>1) and ((t mod tmpTotal)=1) then
    begin
      tmpTab:=TTabSheet.Create(Self);
      tmpTab.Caption:=TeeMsg_GalleryExtended;
      tmpTab.PageControl:=PageControl1;
      tmpGall:=TChartGalleryPanel.Create(Self);
      tmpGall.DisplaySub:=False;
      tmpGall.Align:=alClient;
      tmpGall.Parent:=tmpTab;
      tmpGall.OnSelectedChart:=ChartGalleryPanel1SelectedChart;
      tmpGall.OnChangeChart:=ChartGalleryPanel1ChangeChart;
    end;

    tmpFilter:=TFilterClass(GetClass(TeeFilterClasses[t]));

    // Strange, from XE8 up, tmpFilter can be nil !
    if tmpFilter<>nil then
    with tmpGall.CreateChart(tmpType) as TGalleryChart do
    begin
      FreeAllSeries;
      View3D:=False;
      Axes.Hide;

      Title.Caption:=tmpFilter.Description;

      BackImage.Graphic:=Self.Sample; //Self.GetSampleImage;
      BackImageInside:=True;
      BackImage.Filters.Add(tmpFilter).PrepareForGallery;
    end;
  end;
end;

procedure TFilterGallery.ChartGalleryPanel1SelectedChart(Sender: TObject);
begin
  BOKClick(Self);
end;

function TFilterGallery.CurrentGallery:TChartGalleryPanel;
begin
  result:=PageControl1.ActivePage.Controls[0] as TChartGalleryPanel;
end;

procedure TFilterGallery.BOKClick(Sender: TObject);
begin
  SelectedFilter:=TFilterClass(CurrentGallery.SelectedChart.BackImage.Filters.Item[0].ClassType);
  ModalResult:=mrOk;
end;

procedure TFilterGallery.PageControl1Change(Sender: TObject);
begin
  if CurrentGallery.SelectedChart=nil then
     if CurrentGallery.Charts.Count>0 then
        CurrentGallery.SelectedChart:=CurrentGallery.Charts[0];
end;

procedure TFilterGallery.ChartGalleryPanel1ChangeChart(Sender: TObject);
begin
  BOK.Enabled:=True;
end;

procedure TFilterGallery.FormShow(Sender: TObject);
begin
  AddItems;
  ChartGalleryPanel1.ResizeCharts;
  TeeTranslateControl(Self);
end;

(*
class procedure TFilterGallery.StretchTo(const Graphic:TGraphic; const Picture:TPicture; AWidth,AHeight:Integer);
var tmpDest : TBitmap;
    tmp     : TBitmap;
begin
  (*
  tmpDest:=TBitmap.Create;
  try
    TeeSetBitmapSize(tmpDest,Max(8,AWidth),Max(8,AHeight));

    if Graphic is TBitmap then
       tmp:=TBitmap(Graphic)
    else
    begin
      tmp:=TBitmap.Create;
      tmp.Assign(Graphic);
    end;

    try
      SmoothStretch(tmp,tmpDest);
      Picture.Assign(tmpDest);
    finally
      if not (Graphic is TBitmap) then
         tmp.Free;
    end;
  finally
    tmpDest.Free;
  end;
end;

function TFilterGallery.GetSampleImage:TGraphic;
var tmpW, tmpH : Integer;
begin
  if Assigned(ISample) then
     result:=ISample.Graphic
  else
  begin
    result:=Sample;

    if Assigned(result) then
    begin
      tmpW:=result.Width;
      tmpH:=result.Height;

      if tmpW*tmpH>(640*480) then
      begin
        while tmpW>640 do
        begin
          tmpW:=tmpW div 2;
          tmpH:=tmpH div 2;
        end;

        ISample:=TPicture.Create;
        StretchTo(Sample,ISample,tmpW,tmpH);
        result:=ISample.Graphic;
      end;
    end
    else
      result:=SampleImage.Picture.Graphic;
  end;
end;
*)

procedure TFilterGallery.FormDestroy(Sender: TObject);
begin
  ISample.Free;
end;

end.
