{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Tools.Fibonacci;
{$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}
  FMXTee.Procs, FMX.ListBox, FMX.Edit, FMXTee.Tools.Fibonacci, FMX.TabControl,
  FMX.Colors;

type
  TFibonacciEditor = class(TSeriesToolEditor)
    TabControl1: TTabControl;
    TabTrendline: TTabItem;
    TabLevels: TTabItem;
    GroupBox2: TGroupBox;
    EXStart: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    EYStart: TEdit;
    BRemoveLevel: TButton;
    BAdd: TButton;
    CBStyle: TComboBox;
    Label5: TLabel;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    TabLabels: TTabItem;
    GroupBox1: TGroupBox;
    EXEnd: TEdit;
    Label2: TLabel;
    Label8: TLabel;
    EYEnd: TEdit;
    BTrendPen: TButton;
    BReset: TButton;
    GBLevels: TGroupBox;
    BPen: TButton;
    UDLevel: TSpinBox;
    ELevelValue: TEdit;
    CBLabels: TCheckBox;
    BFont: TButton;
    Label3: TLabel;
    UDAngle: TSpinBox;
    procedure FormActivate(Sender: TObject);
    procedure UDLevelChange(Sender: TObject);
    procedure EXStartChange(Sender: TObject);
    procedure EYStartChange(Sender: TObject);
    procedure BTrendPenClick(Sender: TObject);
    procedure CBLabelsChange(Sender: TObject);
    procedure EXEndChange(Sender: TObject);
    procedure EYEndChange(Sender: TObject);
    procedure BAddClick(Sender: TObject);
    procedure BPenClick(Sender: TObject);
    procedure BRemoveLevelClick(Sender: TObject);
    procedure BResetClick(Sender: TObject);
    procedure UDAngleChange(Sender: TObject);
    procedure CBStyleChange(Sender: TObject);
    procedure BFontClick(Sender: TObject);
    procedure ELevelValueChange(Sender: TObject);
  private
    { Private declarations }
    Fibonacci : TFibonacciTool;
    procedure SetLevel(Index:Integer);
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

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

procedure TFibonacciEditor.SetLevel(Index:Integer);
begin
  GBLevels.Enabled:=(Fibonacci.Levels.Count>0);
  BRemoveLevel.Enabled:=GBLevels.Enabled;

  if GBLevels.Enabled then
  begin
    UDLevel.Max:=Fibonacci.Levels.Count-1;

    if Index>UDLevel.Max then
       Index:=Round(UDLevel.Max);

    UDLevel.Value:=Index;
    UDLevelChange(UDLevel);
  end;
end;

procedure TFibonacciEditor.FormActivate(Sender: TObject);
begin
  inherited;

  if not Assigned(Fibonacci) then
  begin
    Fibonacci:=TFibonacciTool(TagObject);

    if Assigned(Fibonacci) then
    With Fibonacci do
    begin
      CBStyle.ItemIndex:=Ord(DrawStyle);
      CBLabels.IsChecked:=ShowLabels;

      EXStart.Text:=FloatToStr(StartX);
      EYStart.Text:=FloatToStr(StartY);

      EXEnd.Text:=FloatToStr(EndX);
      EYEnd.Text:=FloatToStr(EndY);

      UDAngle.Value:=LabelsAngle;

      SetLevel(0);
    end;
  end;
end;

procedure TFibonacciEditor.BAddClick(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    SetLevel(Fibonacci.Levels.Add.Index);
end;

procedure TFibonacciEditor.BFontClick(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    TFontEditor.Edit(Self, Fibonacci.LabelsFont);
end;

procedure TFibonacciEditor.BPenClick(Sender: TObject);
begin
  if GBLevels.Enabled and Assigned(Fibonacci) then
  with Fibonacci.Levels[Round(UDLevel.Value)] do
    TStrokeEditor.Edit(Self, Pen);
end;

procedure TFibonacciEditor.BRemoveLevelClick(Sender: TObject);
begin
  if Assigned(Fibonacci) then
  begin
    Fibonacci.Levels.Delete(Round(UDLevel.Value));

    SetLevel(Round(UDLevel.Value));
  end;
end;

procedure TFibonacciEditor.BResetClick(Sender: TObject);
begin
  if Assigned(Fibonacci) then
  begin
    Fibonacci.CreateDefaultLevels;
    SetLevel(0);
  end;
end;

procedure TFibonacciEditor.BTrendPenClick(Sender: TObject);
begin
  if Assigned(Fibonacci) then
     TStrokeEditor.Edit(Self, Fibonacci.Pen);
end;

procedure TFibonacciEditor.CBLabelsChange(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    Fibonacci.ShowLabels:=CBLabels.IsChecked;
end;

procedure TFibonacciEditor.CBStyleChange(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    Fibonacci.DrawStyle:=TFibonacciStyle(CBStyle.ItemIndex);
end;

procedure TFibonacciEditor.ELevelValueChange(Sender: TObject);
begin
  if GBLevels.Enabled and Assigned(Fibonacci) then
  with Fibonacci.Levels[Round(UDLevel.Value)] do
    Value:=StrToFloatDef(ELevelValue.Text, Value);
end;

procedure TFibonacciEditor.EXEndChange(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    Fibonacci.EndX:=StrToFloatDef(EXEnd.Text,Fibonacci.EndX);
end;

procedure TFibonacciEditor.EXStartChange(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    Fibonacci.StartX:=StrToFloatDef(EXStart.Text,Fibonacci.StartX);
end;

procedure TFibonacciEditor.EYEndChange(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    Fibonacci.EndY:=StrToFloatDef(EYEnd.Text,Fibonacci.EndY);
end;

procedure TFibonacciEditor.EYStartChange(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    Fibonacci.StartY:=StrToFloatDef(EYStart.Text,Fibonacci.StartY);
end;

procedure TFibonacciEditor.UDAngleChange(Sender: TObject);
begin
  if Assigned(Fibonacci) then
    Fibonacci.LabelsAngle:=Round(UDAngle.Value);
end;

procedure TFibonacciEditor.UDLevelChange(Sender: TObject);
begin
  if GBLevels.Enabled and Assigned(Fibonacci) then
  with Fibonacci.Levels[Round(UDLevel.Value)] do
    ELevelValue.Text:=FloatToStr(Value);
end;

initialization
  RegisterSeriesEditor(TFibonacciEditor);
end.
