{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012 by Steema Software      }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Gallery;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,
  FMXTee.Chart.GalleryPanel, FMXTee.Engine, FMX.ListBox, FMX.TabControl,
  FMX.Types3D, FMX.Layers3D, FMX.Layouts, FMX.Edit;

type
  TChartGallery = class(TForm)
    Panel1: TPanel;
    BOK: TButton;
    BCancel: TButton;
    CB3D: TCheckBox;
    CBPalette: TComboBox;
    TabTypes: TTabControl;
    TabSeries: TTabItem;
    TabFunctions: TTabItem;
    TabPages: TTabControl;
    TabFuncPages: TTabControl;
    Layout3D1: TLayout3D;
    PanelButtons: TPanel;
    CBCount: TComboTrackBar;
    procedure CB3DChange(Sender: TObject);
    procedure BOKClick(Sender: TObject);
    procedure BCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CBPaletteChange(Sender: TObject);
    procedure TabTypesChange(Sender: TObject);
    procedure CBCountChangeTracking(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    { Private declarations }
    FEnableCount,
    OnlyFunctions : Boolean;

    Procedure AddTabPages;
    function Gallery:TChartGalleryPanel;
    function SelectedPaletteIndex:Integer;
  public
    { Public declarations }
    class function ChangeSeries(AOwner:TComponent; ASeries:TChartSeries):TChartSeriesClass;

    class function ChooseFunction(AOwner:TComponent):TTeeFunctionClass;

    class function ChooseSeries(AOwner:TComponent;
                                out FunctionType:TTeeFunctionClass;
                                out SubIndex:Integer;
                                DefaultPalette:Integer=-1):TChartSeriesClass; overload;

    class function ChooseSeries(AOwner:TComponent;
                                out FunctionType:TTeeFunctionClass;
                                out SeriesCount:Integer;
                                out SubIndex:Integer;
                                DefaultPalette:Integer=-1;
                                AEnableCount:Boolean=True):TChartSeriesClass; overload;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Chart, FMXTee.Constants;

procedure TChartGallery.BOKClick(Sender: TObject);
begin
  ModalResult:=mrOk;
end;

procedure TChartGallery.BCancelClick(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure TChartGallery.CB3DChange(Sender: TObject);

  procedure DoTabs(AControl:TTabControl);
  var t: Integer;
  begin
    for t := 0 to AControl.TabCount-1 do
        TChartGalleryPanel(AControl.Tabs[t].TagObject).View3D:=CB3D.IsChecked;
  end;

begin
  DoTabs(TabPages);
  DoTabs(TabFuncPages);
end;

function TChartGallery.Gallery:TChartGalleryPanel;
begin
  if TabTypes.TabIndex=0 then
     result:=TChartGalleryPanel(TabPages.Tabs[TabPages.TabIndex].TagObject)
  else
     result:=TChartGalleryPanel(TabFuncPages.Tabs[TabFuncPages.TabIndex].TagObject)
end;

class function TChartGallery.ChangeSeries(AOwner: TComponent;
                                   ASeries: TChartSeries): TChartSeriesClass;
var tmp : TTeeFunctionClass;
    sub : Integer;
    tmpIndex : Integer;
begin
  with TChartGallery.Create(AOwner) do
  try
    tmpIndex:=(ASeries.ParentChart as TCustomChart).ColorPaletteIndex;
    if tmpIndex<>-1 then
       CBPalette.ItemIndex:=tmpIndex;

    CB3D.IsChecked:=ASeries.ParentChart.View3D;

    Gallery.SelectedSeries:=ASeries;

    if ShowModal=mrOk then
       Gallery.GetSeriesClass(result,tmp,Sub)
    else
       result:=nil;
  finally
    Free;
  end;
end;

class function TChartGallery.ChooseFunction(AOwner: TComponent): TTeeFunctionClass;
var tmpGally : TChartGallery;
    tmpClass : TChartSeriesClass;
    SubIndex : Integer;
begin
  result:=nil;

  tmpGally:=TChartGallery.Create(AOwner);

  with tmpGally do
  try
    TabSeries.Free;

    OnlyFunctions:=True;

    if ShowModal=mrOk then
       Gallery.GetSeriesClass(tmpClass,result,SubIndex);
  finally
    Free;
  end;
end;

class function TChartGallery.ChooseSeries(AOwner:TComponent;
                              out FunctionType:TTeeFunctionClass;
                              out SubIndex:Integer;
                              DefaultPalette:Integer=-1):TChartSeriesClass;
var tmp : Integer;
begin
  result:=ChooseSeries(AOwner,FunctionType,tmp,SubIndex,DefaultPalette,False);
end;

class function TChartGallery.ChooseSeries(AOwner: TComponent;
                              out FunctionType:TTeeFunctionClass;
                              out SeriesCount:Integer;
                              out SubIndex:Integer;
                              DefaultPalette:Integer=-1;
                              AEnableCount:Boolean=True): TChartSeriesClass;
begin
  FunctionType:=nil;

  with TChartGallery.Create(AOwner) do
  try
    if DefaultPalette<>-1 then
       CBPalette.ItemIndex:=DefaultPalette;

    FEnableCount:=AEnableCount;
    SeriesCount:=1;

    if ShowModal=mrOk then
    begin
      Gallery.GetSeriesClass(result,FunctionType,SubIndex);
      SeriesCount:=Round(CBCount.Value);
    end
    else
       result:=nil;
  finally
    Free;
  end;
end;

procedure TChartGallery.CBCountChangeTracking(Sender: TObject);
begin
  CBCount.Value:=Round(CBCount.Value);
  CBCount.Text:=IntToStr(Round(CBCount.Value));
end;

type
  TSeriesAccess=class(TChartSeries);

procedure TChartGallery.CBPaletteChange(Sender: TObject);

  procedure DoGallery(AGallery:TChartGalleryPanel);
  var t : Integer;
  begin
    if Assigned(AGallery) then
    with AGallery do
    for t:=0 to Charts.Count-1 do
    begin
      Charts[t].ColorPaletteIndex:=SelectedPaletteIndex;
      TSeriesAccess(Charts[t].Series[0]).GalleryChanged3D(CB3D.IsChecked);
    end;
  end;

  procedure DoTabs(AControl:TTabControl);
  var t : Integer;
  begin
    for t := 0 to AControl.TabCount-1 do
        DoGallery(TChartGalleryPanel(AControl.Tabs[t].TagObject));
  end;

begin
  if CBPalette.ItemIndex>-1 then
  begin
    DoTabs(TabPages);
    DoTabs(TabFuncPages);
  end;
end;

function TChartGallery.SelectedPaletteIndex:Integer;
begin
  result:=CBPalette.Selected.Tag;
end;

procedure TChartGallery.TabTypesChange(Sender: TObject);
begin
  AddTabPages;

  if TabTypes.TabIndex=0 then
     TabPages.TabIndex:=0
  else
     TabFuncPages.TabIndex:=0;
end;

Procedure TChartGallery.AddTabPages;

  Function TabIndexOf(Const GalleryPage:String):Integer;
  var t : Integer;
      tmp : TTabControl;
  begin
    if TabTypes.TabIndex=0 then
       tmp:=TabPages
    else
       tmp:=TabFuncPages;

    for t:=0 to tmp.TabCount-1 do
        if tmp.Tabs[t].Text=GalleryPage then
           Exit(t);

    result:=-1;
  end;

  procedure AddTab(AParent:TTabControl; const ACaption:String);
  var tmp : TTabItem;
      tmpGallery : TChartGalleryPanel;
  begin
    tmp:=TTabItem.Create(Self);
    tmp.Text:=ACaption;

    AParent.AddObject(tmp);

    // Fix:
    if AParent.TabIndex=-1 then
       AParent.TabIndex:=0;

    tmpGallery:=TChartGalleryPanel.Create(Self);
    tmpGallery.Align:=TAlignLayout.alClient;
    tmpGallery.OnSelectedChart:=BOKClick;

    tmp.AddObject(tmpGallery);

    tmp.TagObject:=tmpGallery;

    with tmpGallery do
    begin
      FunctionsVisible:=OnlyFunctions or (TabTypes.ActiveTab=TabFunctions);
      View3D:=CB3D.IsChecked;
      CreateGalleryPage(ACaption);
    end;
  end;

var t       : Integer;
    tmpPage : String;
begin
  With TeeSeriesTypes do
  for t:=0 to Count-1 do
  With Items[t] do
    if NumGallerySeries>0 then
    begin
      tmpPage:=GalleryPage^;

      if TabIndexOf(tmpPage)=-1 then

        if OnlyFunctions then
        begin
          if Assigned(FunctionClass) then
             AddTab(TabFuncPages, tmpPage);
        end
        else
        Case TabTypes.TabIndex of
         0: if not Assigned(FunctionClass) then AddTab(TabPages, tmpPage);
         1: if Assigned(FunctionClass) then AddTab(TabFuncPages, tmpPage);
        end;
    end;
end;

procedure TChartGallery.FormCreate(Sender: TObject);

  procedure ColorPalettesAddTo(ACombo:TComboBox);
  var t : Integer;
      tmp : TListBoxItem;
  begin
    for t:=0 to ColorPalettes.Count-1 do
    begin
      tmp:=TListBoxItem.Create(Self);
      tmp.Tag:=t;
      tmp.Text:=ColorPalettes[t];
      ACombo.AddObject(tmp);
    end;
  end;

begin
  CBPalette.Clear;
  ColorPalettesAddTo(CBPalette);
  CBPalette.ItemIndex:=13;
//  CBPalette.Sorted:=True;

  {$IFDEF TEELITEEMB}
  TabFunctions.Visible:=False;
  {$ENDIF}

  if TabTypes.ActiveTab=TabSeries then
     TabTypesChange(Self)
  else
     TabTypes.ActiveTab:=TabSeries;
end;

procedure TChartGallery.FormResize(Sender: TObject);
begin
  CBCount.Visible:=(Width>644) and FEnableCount;
end;

end.
