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

interface

uses
  System.SysUtils, System.Types, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.ComboTrackBar, FMX.SpinBox,
  {$ENDIF}
  FMX.ListBox, FMX.Edit, FMX.Dialogs, FMXTee.Series, FMX.TabControl,
  FMXTee.Canvas, FMXTee.Editor.Stroke, FMXTee.Editor.Brush, FMXTee.Engine,
  FMXTee.Editor.Shadow, FMXTee.Procs, FMXTee.Editor.Series.Pointer, FMX.Colors,
  FMX.Layouts;

type
  TCustomSeriesEditor = class(TForm)
    TabControl1: TTabControl;
    TabFormat: TTabItem;
    TabOptions: TTabItem;
    TabBorder: TTabItem;
    TabPattern: TTabItem;
    TabShadow: TTabItem;
    TabEmboss: TTabItem;
    TabOutline: TTabItem;
    CBDark3D: TCheckBox;
    CBColorEach: TCheckBox;
    CBColorEachLine: TCheckBox;
    LTransp: TLabel;
    TBTransp: TTrackBar;
    GBStair: TGroupBox;
    CBStairs: TCheckBox;
    CBInvStairs: TCheckBox;
    CBSmoothed: TCheckBox;
    CBClick: TCheckBox;
    LHeight: TLabel;
    UDHeight: TSpinBox;
    Label1: TLabel;
    CBStack: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    ListBoxItem4: TListBoxItem;
    Label2: TLabel;
    CBNulls: TComboBox;
    ListBoxItem5: TListBoxItem;
    ListBoxItem6: TListBoxItem;
    ListBoxItem7: TListBoxItem;
    LDrawStyle: TLabel;
    CBDrawStyle: TComboBox;
    ListBoxItem8: TListBoxItem;
    ListBoxItem9: TListBoxItem;
    ListBoxItem10: TListBoxItem;
    Label4: TLabel;
    UDTolerance: TComboTrackBar;
    CBDefColor: TCheckBox;
    BLineColor: TComboColorBox;
    TabPointer: TTabItem;
    CBPointerBehind: TCheckBox;
    Layout1: TLayout;
    CBInflate: TCheckBox;
    procedure CBStackChange(Sender: TObject);
    procedure CBNullsChange(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure CBStairsChange(Sender: TObject);
    procedure CBInvStairsChange(Sender: TObject);
    procedure CBSmoothedChange(Sender: TObject);
    procedure CBColorEachChange(Sender: TObject);
    procedure CBDark3DChange(Sender: TObject);
    procedure CBClickChange(Sender: TObject);
    procedure CBColorEachLineChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure UDHeightChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure CBDrawStyleChange(Sender: TObject);
    procedure UDToleranceChange(Sender: TObject);
    procedure CBDefColorChange(Sender: TObject);
    procedure BLineColorChange(Sender: TObject);
    procedure CBPointerBehindChange(Sender: TObject);
    procedure CBInflateChange(Sender: TObject);
  private
   { Private declarations }
    Custom : TCustomSeries;

    IBorderEditor : TStrokeEditor;
    IShadowEditor : TForm;
    IEmbossEditor : TForm;
    IPatternEditor : TBrushEditor;
    IOutlineEditor : TStrokeEditor;
    IPointerEditor : TSeriesPointerEditor;

    procedure CheckDefColor;
    procedure RefreshCustom(const ACustom:TCustomSeries);
  public
   { Public declarations }
    class function CreateEditor(const AOwner:TComponent; const ACustom:TCustomSeries):TForm;
  end;

implementation

{$R *.fmx}

procedure TCustomSeriesEditor.FormActivate(Sender: TObject);
begin
  if Custom<>TagObject then
     RefreshCustom(TCustomSeries(TagObject));
end;

type
  TCustomAccess=class(TCustomSeries);

procedure TCustomSeriesEditor.RefreshCustom(const ACustom: TCustomSeries);
begin
  Custom:=ACustom;

  CBStairs.IsChecked:=Custom.Stairs;
  CBInvStairs.IsChecked:=Custom.InvertedStairs;

  {$IFDEF TEELITEEMB}
  CBSmoothed.Visible:=False;
  {$ELSE}
  CBSmoothed.IsChecked:=Custom.Smoothed;
  {$ENDIF}

  CBColorEach.IsChecked:=Custom.ColorEachPoint;
  CBDark3D.IsChecked:=Custom.Dark3D;
  CBClick.IsChecked:=Custom.ClickableLine;
  CBColorEachLine.IsChecked:=Custom.ColorEachLine;
  TBTransp.Value:=TCustomAccess(Custom).Transparency;
  CBNulls.ItemIndex:=Ord(Custom.TreatNulls);
  CBStack.ItemIndex:=Ord(TCustomAccess(Custom).Stacked);
  UDHeight.Value:=Custom.LineHeight;
  UDTolerance.Value:=Custom.ClickTolerance;

  CBPointerBehind.IsChecked:=Custom.PointerBehind;
  CBInflate.IsChecked:=Custom.InflateMargins;

  BLineColor.Color:=Custom.Color;
  CheckDefColor;

  if Custom is TLineSeries then
     CBDrawStyle.ItemIndex:=Ord(TLineSeries(Custom).DrawStyle)
  else
  begin
    CBDrawStyle.Visible:=False;
    LDrawStyle.Visible:=False;
  end;
end;

procedure TCustomSeriesEditor.CBStairsChange(Sender: TObject);
begin
  Custom.Stairs:=CBStairs.IsChecked;
end;

procedure TCustomSeriesEditor.CBDrawStyleChange(Sender: TObject);
begin
  case CBDrawStyle.ItemIndex of
    0: TLineSeries(Custom).DrawStyle:=dsSegments;
    1: TLineSeries(Custom).DrawStyle:=dsAll;
  else
    TLineSeries(Custom).DrawStyle:=dsCurve;
  end;
end;

procedure TCustomSeriesEditor.CBInflateChange(Sender: TObject);
begin
  Custom.InflateMargins:=CBInflate.IsChecked;
end;

procedure TCustomSeriesEditor.CBInvStairsChange(Sender: TObject);
begin
  Custom.InvertedStairs:=CBInvStairs.IsChecked;
end;

procedure TCustomSeriesEditor.CBSmoothedChange(Sender: TObject);
begin
  {$IFNDEF TEELITEEMB}
  Custom.Smoothed:=CBSmoothed.IsChecked;
  {$ENDIF}
end;

procedure TCustomSeriesEditor.CBColorEachChange(Sender: TObject);
begin
  Custom.ColorEachPoint:=CBColorEach.IsChecked;
end;

procedure TCustomSeriesEditor.CBDark3DChange(Sender: TObject);
begin
  Custom.Dark3D:=CBDark3D.IsChecked;
end;

procedure TCustomSeriesEditor.CheckDefColor;
begin
  CBDefColor.IsChecked:=Custom.Color=clTeeColor;
  CBDefColor.Enabled:=not CBDefColor.IsChecked;
end;

procedure TCustomSeriesEditor.BLineColorChange(Sender: TObject);
begin
  Custom.Color:=BLineColor.Color;
  CheckDefColor;
end;

type
  TSeriesAccess=class(TChartSeries);

procedure TCustomSeriesEditor.CBDefColorChange(Sender: TObject);
begin
  if CBDefColor.IsChecked then
  begin
    TSeriesAccess(Custom).SetDefaultColor;
    BLineColor.Repaint;
    CBDefColor.Enabled:=False;
  end;
end;

procedure TCustomSeriesEditor.CBStackChange(Sender: TObject);
begin
  TCustomAccess(Custom).Stacked:=TCustomSeriesStack(CBStack.ItemIndex);
end;

procedure TCustomSeriesEditor.CBClickChange(Sender: TObject);
begin
  Custom.ClickableLine:=CBClick.IsChecked;
end;

procedure TCustomSeriesEditor.CBColorEachLineChange(Sender: TObject);
begin
  Custom.ColorEachLine:=CBColorEachLine.IsChecked;
end;

class function TCustomSeriesEditor.CreateEditor(const AOwner: TComponent;
  const ACustom: TCustomSeries): TForm;
begin
  result:=TCustomSeriesEditor.Create(AOwner);
  TCustomSeriesEditor(result).RefreshCustom(ACustom);
end;

procedure TCustomSeriesEditor.CBNullsChange(Sender: TObject);
begin
  Custom.TreatNulls:=TTreatNullsStyle(CBNulls.ItemIndex);
end;

procedure TCustomSeriesEditor.CBPointerBehindChange(Sender: TObject);
begin
  Custom.PointerBehind:=CBPointerBehind.IsChecked;
end;

procedure TCustomSeriesEditor.TabControl1Change(Sender: TObject);
begin
  if TabControl1.ActiveTab=TabFormat then
     BLineColor.Repaint
  else
  if TabControl1.ActiveTab=TabBorder then
  begin
    if not Assigned(IBorderEditor) then
       IBorderEditor:=TStrokeEditor.InsertForm(Custom.LinePen, TabBorder)
    else
       IBorderEditor.RepaintButtonColor;
  end
  else
  if (TabControl1.ActiveTab=TabPattern) and (not Assigned(IPatternEditor)) then
     IPatternEditor:=TBrushEditor.InsertForm(Custom.Brush,TabPattern)
  else
  if (TabControl1.ActiveTab=TabShadow) and (not Assigned(IShadowEditor)) then
     IShadowEditor:=TShadowEditor.InsertForm(Custom.Shadow,TabShadow)
  else
  if (TabControl1.ActiveTab=TabEmboss) and (not Assigned(IEmbossEditor)) then
     IEmbossEditor:=TShadowEditor.InsertForm(Custom.Emboss,TabEmboss)
  else
  if (TabControl1.ActiveTab=TabOutline) and (not Assigned(IOutlineEditor)) then
     IOutlineEditor:=TStrokeEditor.InsertForm(Custom.OutLine,TabOutline)
  else
  if (TabControl1.ActiveTab=TabPointer) and (not Assigned(IPointerEditor)) then
     IPointerEditor:=TSeriesPointerEditor.InsertForm(Custom.Pointer,TabPointer);
end;

procedure TCustomSeriesEditor.TBTranspChange(Sender: TObject);
begin
  TCustomAccess(Custom).Transparency:=Round(TBTransp.Value);
end;

procedure TCustomSeriesEditor.UDHeightChange(Sender: TObject);
begin
  Custom.LineHeight:=Round(UDHeight.Value);
end;

procedure TCustomSeriesEditor.UDToleranceChange(Sender: TObject);
begin
  Custom.ClickTolerance:=Round(UDTolerance.Value);
end;

initialization
  RegisterSeriesEditor(TCustomSeriesEditor);
end.
