{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Tools.DataTable;
{$I TeeDefs.inc}

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Editor.Tools.Series,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMX.ListBox, FMXTee.Tools, FMX.Edit, FMX.TabControl, FMX.Layouts, FMX.Memo,
  FMXTee.Tools.DataTable, FMXTee.Editor.Margins;

type
  TDataTableEditor = class(TForm)
    TabControl1: TTabControl;
    TabFormat: TTabItem;
    TabLegend: TTabItem;
    CBInverted: TCheckBox;
    CBClip: TCheckBox;
    TabText: TTabItem;
    BRowsPen: TButton;
    BColumnsPen: TButton;
    GroupBox1: TGroupBox;
    CBAutoPos: TCheckBox;
    Label5: TLabel;
    UDLeft: TSpinBox;
    Label4: TLabel;
    UDTop: TSpinBox;
    TabControl2: TTabControl;
    TabLegendFormat: TTabItem;
    TabLegendSymbol: TTabItem;
    TabLegendText: TTabItem;
    CBLegVisible: TCheckBox;
    CBFontColor: TCheckBox;
    CBOtherSide: TCheckBox;
    procedure FormActivate(Sender: TObject);
    procedure CBInvertedChange(Sender: TObject);
    procedure UDLeftChange(Sender: TObject);
    procedure BRowsPenClick(Sender: TObject);
    procedure CBClipChange(Sender: TObject);
    procedure CBAutoPosChange(Sender: TObject);
    procedure UDTopChange(Sender: TObject);
    procedure BColumnsPenClick(Sender: TObject);
    procedure CBLegVisibleChange(Sender: TObject);
    procedure CBFontColorChange(Sender: TObject);
    procedure CBOtherSideChange(Sender: TObject);
  private
    { Private declarations }
    DataTable : TDataTableTool;
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Symbol, FMXTee.Editor.Font, FMXTee.Editor.Stroke, FMXTee.Procs;

procedure TDataTableEditor.BColumnsPenClick(Sender: TObject);
begin
  if Assigned(DataTable) then
     TStrokeEditor.Edit(Self, DataTable.ColumnPen);
end;

procedure TDataTableEditor.BRowsPenClick(Sender: TObject);
begin
  if Assigned(DataTable) then
     TStrokeEditor.Edit(Self, DataTable.RowPen);
end;

procedure TDataTableEditor.CBClipChange(Sender: TObject);
begin
  if Assigned(DataTable) then
    DataTable.ClipText:=CBClip.IsChecked;
end;

procedure TDataTableEditor.CBFontColorChange(Sender: TObject);
begin
  if Assigned(DataTable) and Assigned(DataTable.Legend) then
    DataTable.Legend.FontSeriesColor:=CBFontColor.IsChecked;
end;

procedure TDataTableEditor.CBAutoPosChange(Sender: TObject);
begin
  if Assigned(DataTable) then
  begin
    DataTable.AutoPosition:=CBAutoPos.IsChecked;

    if not DataTable.AutoPosition then
    begin
      UDLeft.Value:=DataTable.Left;
      UDTop.Value:=DataTable.Top;
    end;

    EnableControls(not DataTable.AutoPosition,[UDLeft,UDTop]);
  end;
end;

procedure TDataTableEditor.CBInvertedChange(Sender: TObject);
begin
  if Assigned(DataTable) then
    DataTable.Inverted:=CBInverted.IsChecked;
end;

procedure TDataTableEditor.CBLegVisibleChange(Sender: TObject);
begin
  if Assigned(DataTable) and Assigned(DataTable.Legend) then
    DataTable.Legend.Visible:=CBLegVisible.IsChecked;
end;

procedure TDataTableEditor.CBOtherSideChange(Sender: TObject);
begin
  if Assigned(DataTable) and Assigned(DataTable.Legend) then
    DataTable.Legend.OtherSide:=CBOtherSide.IsChecked;
end;

procedure TDataTableEditor.FormActivate(Sender: TObject);
var LegendSymbolForm: TSymbolEditor;
    LegendTextForm, TextForm: TForm;
begin
  if not Assigned(DataTable) then
  begin
    DataTable:=TDataTableTool(TagObject);;

    if Assigned(DataTable) then
    begin
      With DataTable do
      begin
        CBAutoPos.IsChecked:=AutoPosition;
        CBInverted.IsChecked:=Inverted;
        UDLeft.Value:=Left;
        UDTop.Value:=Top;
        if Assigned(Legend) then
        begin
          CBLegVisible.IsChecked:=Legend.Visible;
          CBFontColor.IsChecked:=Legend.FontSeriesColor;
          CBOtherSide.IsChecked:=Legend.OtherSide;

          LegendSymbolForm:=TSymbolEditor.CreateEditor(Self, DataTable.Legend.Symbol);
          LegendSymbolForm.Name:=TeeGetUniqueName(Self,'SymbolEditor');
          EmbeddForm(LegendSymbolForm, TabLegendSymbol);

          LegendTextForm:=TFontEditor.CreateEditor(Self, DataTable.Legend.Font);
          LegendTextForm.Name:=TeeGetUniqueName(Self,'FontEditor');
          EmbeddForm(LegendTextForm, TabLegendText);
        end;
        TextForm:=TFontEditor.CreateEditor(Self, DataTable.Font);
        TextForm.Name:=TeeGetUniqueName(Self,'FontEditor');
        EmbeddForm(TextForm, TabText);
      end;
    end;
  end;
end;

procedure TDataTableEditor.UDLeftChange(Sender: TObject);
begin
  if Assigned(DataTable) and not DataTable.AutoPosition then
    DataTable.Left:=Round(UDLeft.Value);
end;

procedure TDataTableEditor.UDTopChange(Sender: TObject);
begin
  if Assigned(DataTable) and not DataTable.AutoPosition then
    DataTable.Top:=Round(UDTop.Value);
end;

initialization
  RegisterSeriesEditor(TDataTableEditor);
end.
