unit FMXTee.Editor.AxisIncr;

interface

uses
  SysUtils, Types, System.UITypes, Classes, Variants,
  FMX.Types, FMX.Controls, FMX.Forms,
  FMX.ListBox, FMX.Edit, FMX.Dialogs, FMXTee.Engine, FMXTee.Procs;

type
  TAxisIncrement = class(TForm)
    Label1: TLabel;
    CBSteps: TComboBox;
    ECustom: TEdit;
    BOk: TButton;
    BCancel: TButton;
    CBExact: TCheckBox;
    procedure CBStepsChange(Sender: TObject);
    procedure CBExactChange(Sender: TObject);
    procedure ECustomTyping(Sender: TObject);
  private
   { Private declarations }
    Axis : TChartAxis;

  public
   { Public declarations }
    IsDateTime : Boolean;
    IsExact    : Boolean;
    Increment  : Double;
    IStep      : TDateTimeStep;

    class function ChangeIncrement(AOwner:TComponent; AAxis: TChartAxis): Boolean;
    class Function GetIncrementText( AOwner:TComponent;
                           const Increment:Double;
                           IsDateTime,
                           ExactDateTime:Boolean;
                           const AFormat:String):String;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Constants, FMXTee.Chart;

procedure TAxisIncrement.CBStepsChange(Sender: TObject);
begin
  IStep:=TDateTimeStep(CBSteps.ItemIndex);
end;

class Function TAxisIncrement.GetIncrementText( AOwner:TComponent;
                           Const Increment:Double;
                           IsDateTime,
                           ExactDateTime:Boolean;
                           Const AFormat:String):String;

  Function GetDateTimeStepText(const Step:TDateTimeStep):String;
  begin
    result:='';

    with TAxisIncrement.Create(AOwner) do
    try
      result:=CBSteps.ListBox.Items[Ord(Step)];
    finally
      Free;
    end;
  end;

var tmp : TDateTimeStep;
begin
  if IsDateTime then
  begin
    tmp:=FindDateTimeStep(Increment);
    if ExactDateTime and (tmp<>dtNone) then result:=GetDateTimeStepText(tmp)
    else
    if Increment<=0 then result:=TimeToStr(DateTimeStep[dtOneSecond])
    else
    if Increment<=1 then result:=TimeToStr(Increment)
    else
    Begin
      result:=FloatToStr(Int(Increment));
      if Frac(Increment)<>0 then result:=result+' '+TimeToStr(Frac(Increment));
    end;
  end
  else
  begin
    result:=FormatFloat(AFormat,Increment);

    if StrToFloatDef(result,0)<>Increment then  // ie: AFormat=#,###.### and Increment = 0.00001
       result:=FloatToStr(Increment);  // another way, trying to append # to AFormat until result=Increment
  end;
end;

procedure TAxisIncrement.CBExactChange(Sender: TObject);
begin
  Axis.ExactDateTime:=CBExact.IsChecked;
end;

class function TAxisIncrement.ChangeIncrement(AOwner:TComponent; AAxis: TChartAxis): Boolean;
var tmp : TAxisIncrement;
begin
  result:=False;

  tmp:=TAxisIncrement.Create(AOwner);

  with tmp do
  try
    Axis:=AAxis;

    Caption:=Format(TeeMsg_DesiredIncrement,
                   [(Axis.ParentChart as TCustomChart).AxisTitleOrName(Axis)]);

    IsDateTime:=Axis.IsDateTime;
    IsExact:=Axis.ExactDateTime;
    Increment:=Axis.Increment;
    IStep:=FindDateTimeStep(Increment);

    CBExact.Visible:=IsDateTime;
    CBSteps.Visible:=IsDateTime;

    ECustom.Enabled:=not IsDateTime;

    Position:=TFormPosition.poOwnerFormCenter;

    ECustom.Text:=GetIncrementText(nil,Increment,IsDateTime,IsExact,Axis.AxisValuesFormat);

    TeeTranslateControl(tmp);

    if IsDateTime then
    begin
      CBSteps.ItemIndex:=Ord(IStep);
      CBSteps.SetFocus;
    end
    else
       ECustom.SetFocus;

    if ShowModal=mrOk then
    Begin
      if IsDateTime then
         Increment:=DateTimeStep[TDateTimeStep(CBSteps.ItemIndex)];

      Axis.Increment:=Increment;
      Axis.ExactDateTime:=IsExact;
      result:=True;
    end;
  finally
    Free;
  end;
end;

procedure TAxisIncrement.ECustomTyping(Sender: TObject);
begin
  Increment:=StrToFloatDef(ECustom.Text,Increment);
end;

end.
