unit FMXTee.Editor.Themes;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.TabControl, FMX.ListBox,
  FMX.Grid, FMX.Layouts, FMXTee.Chart.Grid, FMXTee.Engine, FMXTee.Procs,
  FMXTee.Chart, FMX.Types3D, FMXTee.Chart3D;

type
  TChartThemeSelector = class(TForm)
    Panel1: TPanel;
    CheckBox1: TCheckBox;
    PanelButtons: TPanel;
    BOk: TButton;
    Button2: TButton;
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    TabControlPreview: TTabControl;
    Splitter1: TSplitter;
    TabPreview: TTabItem;
    TabPalette: TTabItem;
    PreviewChart2D: TChart;
    Panel4: TPanel;
    LBThemes: TListBox;
    Label1: TLabel;
    CBPalette: TComboBox;
    Viewport3D1: TViewport3D;
    Chart3DChart1: TChart;
    PreviewChart3D: TChart3D;
    Light1: TLight;
    procedure Panel4Resize(Sender: TObject);
    procedure CBPaletteChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure TabControlPreviewChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CheckBox1Change(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BOkClick(Sender: TObject);
    procedure LBThemesChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    Chart : TCustomChart;
    PreviewChart : TCustomChart;

    PaletteSeries : TChartSeries;

    procedure InitPreviewChart;
    procedure RefreshColorGrid;
    Function SelectedTheme:TChartThemeClass;
  public
    { Public declarations }
    class function Choose(AOwner:TComponent; AChart:TCustomChart):Boolean;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Themes, FMXTee.Constants, FMXTee.ProConstants, FMXTee.Editor.Chart;

procedure TChartThemeSelector.BOkClick(Sender: TObject);
begin
  Chart.View3D:=CheckBox1.IsChecked;

  if SelectedTheme<>nil then
  begin
    if CBPalette.ItemIndex=0 then
       ApplyChartTheme(SelectedTheme,Chart)
    else
       ApplyChartTheme(SelectedTheme,Chart,CBPalette.ItemIndex-1);
  end
  else
    Chart.ColorPaletteIndex:=CBPalette.ItemIndex-1;

  BOK.Enabled:=False;
end;

Function TChartThemeSelector.SelectedTheme:TChartThemeClass;
begin
  if LBThemes.ItemIndex<=0 then
     result:=nil
  else
     result:=TChartThemeClass(LBThemes.Items.Objects[LBThemes.ItemIndex]);
end;

type
  TSeriesAccess=class(TChartSeries);

procedure TChartThemeSelector.InitPreviewChart;
var t : Integer;
    Old : Boolean;
begin
  PreviewChart.FreeAllSeries;

  if Assigned(Chart) then
  begin
    PreviewChart.Assign(Chart);

    if Chart.Parent is TCustomChart3D then
    begin
      PreviewChart3D.RotationAngle:=TCustomChart3D(Chart.Parent).RotationAngle;

      with TCustomChart3D(Chart.Parent) do
           Self.PreviewChart3D.SetSize(Width,Height,Depth);
    end;

    for t:=0 to Chart.SeriesCount-1 do
    begin
      Old:=TSeriesAccess(Chart[t]).ManualData;
      TSeriesAccess(Chart[t]).ManualData:=True;
      try
        {$IFNDEF TEEOCX} //pending fix ax
        CloneChartSeries(Chart[t],PreviewChart,PreviewChart);
        {$ENDIF}
      finally
        TSeriesAccess(Chart[t]).ManualData:=Old;
      end;
    end;

    CheckBox1.IsChecked:=Chart.View3D;
  end;
end;

procedure TChartThemeSelector.CBPaletteChange(Sender: TObject);
begin
  if CBPalette.ItemIndex=0 then
     LBThemesChange(Self)
  else
     PreviewChart.ColorPaletteIndex:=CBPalette.ItemIndex-1;

  RefreshColorGrid;

  BOK.Enabled:=True;
end;

procedure TChartThemeSelector.RefreshColorGrid;
var t : Integer;
begin
  if Assigned(PaletteSeries) then
  begin
    PaletteSeries.Clear;
    for t:=0 to Length(PreviewChart.ColorPalette)-1 do
        PaletteSeries.Add(t,'',PreviewChart.ColorPalette[t]);

    //ChartGrid1.RecalcDimensions;
    //ChartGrid1.Invalidate;
  end;
end;

procedure TChartThemeSelector.CheckBox1Change(Sender: TObject);
begin
  PreviewChart.View3D:=CheckBox1.IsChecked;
  BOk.Enabled:=True;
end;

class function TChartThemeSelector.Choose(AOwner: TComponent;
  AChart: TCustomChart): Boolean;
begin
  with TChartThemeSelector.Create(AOwner) do
  try
    Chart:=AChart;
    PanelButtons.Visible:=True;
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

Procedure AddChartThemes(Items:TStrings);
var t : Integer;
    tmp : TChartTheme;
begin
  for t:=0 to ChartThemes.Count-1 do
  begin
    tmp:=ChartThemes[t].Create(nil);
    try
      Items.AddObject(tmp.Description,TObject(ChartThemes[t]));
    finally
      tmp.Free;
    end;
  end;
end;

procedure TChartThemeSelector.FormActivate(Sender: TObject);
begin
  if LBThemes.Count>0 then
     Exit;

  if not Assigned(Chart) then
     Chart:=TCustomChart(TagObject);

  if Chart.Parent is TCustomChart3D then
  begin
    PreviewChart2D.Visible:=False;
    Viewport3D1.Visible:=True;

    PreviewChart:=PreviewChart3D.Chart;
  end;

  // Fill listbox with registered Themes...
  LBThemes.Clear;
  LBThemes.Items.Add(TeeMsg_Current);

  AddChartThemes(LBThemes.Items);
  LBThemes.ItemIndex:=0;

  // Fill Color Palette combo...
  CBPalette.Clear;
  ColorPalettes.AddTo(CBPalette.Items,True);

  TabControlPreview.ActiveTab:=TabPreview;

  if Assigned(Chart) then
     if Chart.ColorPaletteIndex=0 then
        CBPalette.ItemIndex:=0
     else
        CBPalette.ItemIndex:=Chart.ColorPaletteIndex+1;

  InitPreviewChart;

  BOk.Enabled:=False;

  TeeTranslateControl(Self);
end;

procedure TChartThemeSelector.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if BOk.Enabled then
  Case MessageDlg(TeeMsg_SureToApply,TMsgDlgType.mtConfirmation,
                  mbYesNoCancel,0) of
    mrYes: begin
             BOkClick(Self);
             CanClose:=not BOk.Enabled;
           end;
    mrNo: begin
            BOk.Enabled:=False;
            CanClose:=True;
          end;
    mrCancel: CanClose:=False;
  end;
end;

procedure TChartThemeSelector.FormCreate(Sender: TObject);
begin
  PreviewChart:=PreviewChart2D;
end;

procedure TChartThemeSelector.FormDestroy(Sender: TObject);
begin
  PaletteSeries.Free;
end;

procedure TChartThemeSelector.LBThemesChange(Sender: TObject);
begin
  if LBThemes.ItemIndex>0 then
  begin
    if CBPalette.ItemIndex=0 then
       ApplyChartTheme(SelectedTheme,PreviewChart)
    else
       ApplyChartTheme(SelectedTheme,PreviewChart,CBPalette.ItemIndex-1);

    BOK.Enabled:=True;
  end
  else
  begin
    InitPreviewChart;
    BOK.Enabled:=False;
  end;

  RefreshColorGrid;
end;

procedure TChartThemeSelector.Panel4Resize(Sender: TObject);
begin
  CBPalette.Width:=Panel4.Width-12;
end;

procedure TChartThemeSelector.TabControlPreviewChange(Sender: TObject);

  procedure InitColorGrid;
  begin
    PaletteSeries:=TChartSeries.Create(nil);
    PaletteSeries.MandatoryValueList.Name:=TeeMsg_Index;

    (*
    ChartGrid1.Series:=PaletteSeries;
    ChartGrid1.ShowFields:=False;
    ChartGrid1.EditorMode:=False;
    ChartGrid1.AllowAppend:=False;

    {$IFNDEF CLR}
    ChartGrid1.ColWidths[2]:=90;
    {$ELSE}
    {$IFDEF D10}
    ChartGrid1.ColWidths[2]:=90;
    {$ENDIF}
    {$ENDIF}
    *)
  end;

begin
  if TabControlPreview.ActiveTab=TabPalette then
  begin
    //if ChartGrid1.Series=nil then
       InitColorGrid;

    RefreshColorGrid;
  end;
end;

initialization
  TChartEditForm.ThemesEditor:=TChartThemeSelector;
end.
