{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Tools.AxisBreaks;
{$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.Axis,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  FMX.ListBox, FMX.Edit, FMXTee.Tools.AxisBreaks, FMX.Layouts;

type
  TAxisBreaksEditor = class(TAxisToolEditor)
    BBrush: TButton;
    Panel2: TPanel;
    Panel3: TPanel;
    Label2: TLabel;
    SBAdd: TButton;
    SBDelete: TButton;
    Label3: TLabel;
    TrackBar1: TTrackBar;
    LBreaks: TListBox;
    LStart: TLabel;
    EStart: TEdit;
    LEnd: TLabel;
    EEnd: TEdit;
    procedure FormActivate(Sender: TObject);
    procedure BBrushClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure LBreaksClick(Sender: TObject);
    procedure LBreaksChangeCheck(Sender: TObject);
    procedure SBAddClick(Sender: TObject);
    procedure SBDeleteClick(Sender: TObject);
    procedure EStartChange(Sender: TObject);
    procedure EEndChange(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
  private
    { Private declarations }
    AxisBreaks : TAxisBreaksTool;

    procedure FillBreaks;
    function CurrentBreak:TAxisBreak;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Procs, FMXTee.Editor.Brush;

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

  if not Assigned(AxisBreaks) then
  begin
    AxisBreaks:=TAxisBreaksTool(TagObject);

    if Assigned(AxisBreaks) then
    With AxisBreaks do
    begin
      FillBreaks;
      TrackBar1.Value:=GapSize;
    end;
  end;
end;

procedure TAxisBreaksEditor.FillBreaks;
var t : Integer;
begin
  LBreaks.Clear;

  for t:=0 to AxisBreaks.Breaks.Count-1 do
  begin
    LBreaks.Items.Add(IntToStr(t));
    LBreaks.ListItems[t].IsChecked:=AxisBreaks.Breaks[t].Enabled;
  end;

  if LBreaks.Items.Count>0 then
     LBreaks.ItemIndex:=0
  else
     LBreaks.ItemIndex:=-1;

  LBreaksClick(Self);
end;

function TAxisBreaksEditor.CurrentBreak:TAxisBreak;
begin
  if LBreaks.ItemIndex>=0 then
    result:=AxisBreaks.Breaks[LBreaks.ItemIndex]
  else
    result:=nil;
end;

procedure TAxisBreaksEditor.EEndChange(Sender: TObject);
begin
  if Assigned(AxisBreaks) and (CurrentBreak<>nil) then
  with CurrentBreak do
  begin
    if (StartValue>StrToFloatDef(EEnd.Text,EndValue)) then
    begin
       EStart.Text:=EEnd.Text;
       EStartChange(EStart);
    end;

    if AxisBreaks.Axis.IsDateTime then
       EndValue:= StrToDateTimeDef(EEnd.Text,EndValue)
    else
       EndValue:=StrToFloatDef(EEnd.Text,EndValue);
  end;
end;

procedure TAxisBreaksEditor.EStartChange(Sender: TObject);
begin
  if Assigned(AxisBreaks) and (CurrentBreak<>nil) then
  with CurrentBreak do
  begin
    if EndValue<StrToFloatDef(EStart.Text,StartValue) then
    begin
       EEnd.Text := EStart.Text;
       EEndChange(EEnd);
    end;

    if AxisBreaks.Axis.IsDateTime then
       StartValue:= StrToDateTimeDef(EStart.Text,StartValue)
    else
       StartValue:=StrToFloatDef(EStart.Text,StartValue);
  end;
end;

procedure TAxisBreaksEditor.FormCreate(Sender: TObject);
begin
  inherited;
  EnableControls(False,[EStart,EEnd,LStart,LEnd,SBDelete]);
end;

procedure TAxisBreaksEditor.LBreaksChangeCheck(Sender: TObject);
begin
  if (LBreaks.ItemIndex<>-1) and Assigned(AxisBreaks) then
    CurrentBreak.Enabled:=LBreaks.ListItems[LBreaks.ItemIndex].IsChecked;
end;

procedure TAxisBreaksEditor.LBreaksClick(Sender: TObject);
begin
  EnableControls(LBreaks.ItemIndex<>-1, [EStart,EEnd,LStart,LEnd,SBDelete]);

  if (LBreaks.ItemIndex<>-1) and Assigned(AxisBreaks) then
  with AxisBreaks.Breaks[LBreaks.ItemIndex] do
  begin
    if AxisBreaks.Axis.IsDateTime then
    begin
      EStart.Text:=DateToStr(StartValue);
      EEnd.Text:=DateToStr(EndValue);
    end
    else
    begin
      EStart.Text:=FloatToStr(StartValue);
      EEnd.Text:=FloatToStr(EndValue);
    end;
  end
  else
  begin
    EStart.Text:='';
    EEnd.Text:='';
  end;
end;

procedure TAxisBreaksEditor.SBAddClick(Sender: TObject);
begin
  if Assigned(AxisBreaks) then
  begin
    AxisBreaks.Breaks.Add(0,0);
    FillBreaks;

    LBreaks.ItemIndex:=LBreaks.Items.Count-1;
    LBreaksClick(Self);
  end;
end;

procedure TAxisBreaksEditor.SBDeleteClick(Sender: TObject);
var tmp : Integer;
begin
  if Assigned(AxisBreaks) then
  begin
    tmp:=LBreaks.ItemIndex;

    {$IFDEF AUTOREFCOUNT}
    CurrentBreak.DisposeOf;
    {$ELSE}
    CurrentBreak.Free;
    {$ENDIF}
    
    FillBreaks;

    if tmp>0 then
      LBreaks.ItemIndex:=tmp-1;
  end;
end;

procedure TAxisBreaksEditor.TrackBar1Change(Sender: TObject);
begin
  if Assigned(AxisBreaks) then
     AxisBreaks.GapSize:=Round(TrackBar1.Value);
end;

procedure TAxisBreaksEditor.BBrushClick(Sender: TObject);
begin
  if Assigned(AxisBreaks) then
     TBrushEditor.Edit(Self,AxisBreaks.Brush);
end;

initialization
  RegisterSeriesEditor(TAxisBreaksEditor);
end.
