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

interface

uses
  System.SysUtils, System.Types, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  FMX.Dialogs, FMXTee.Editor.Functions.Base, FMX.Edit, FMX.ListBox;

type
  TTeeFunctionEditor = class(TBaseFunctionEditor)
    Label1: TLabel;
    CBStyle: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    Label2: TLabel;
    ENum: TEdit;
    BChange: TButton;
    Label3: TLabel;
    CBAlign: TComboBox;
    ListBoxItem4: TListBoxItem;
    ListBoxItem5: TListBoxItem;
    ListBoxItem6: TListBoxItem;
    procedure BChangeClick(Sender: TObject);
    procedure CBAlignChange(Sender: TObject);
    procedure CBStyleChange(Sender: TObject);
    procedure ENumTyping(Sender: TObject);
  private
    { Private declarations }
  protected
    procedure ApplyFormChanges; override;
    Procedure SetFunction; override;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  Math, FMXTee.Engine, FMXTee.Editor.AxisIncr, FMXTee.Procs, FMXTee.Constants,
  System.UITypes;

procedure TTeeFunctionEditor.ApplyFormChanges;
begin
  inherited;

  With IFunction do
  begin
    PeriodAlign:=TFunctionPeriodAlign(CBAlign.ItemIndex);

    if CBStyle.ItemIndex=2 then PeriodStyle:=psRange
                           else PeriodStyle:=psNumPoints;

    Period:=StrToFloatDef(ENum.Text,0);
  end;
end;

procedure TTeeFunctionEditor.BChangeClick(Sender: TObject);

  Function CalcIsDateTime:Boolean;
  begin
    With IFunction.ParentSeries do
    if Assigned(DataSource) and (DataSource is TChartSeries) then
       result:=TChartSeries(DataSource).NotMandatoryValueList.DateTime
    else
       result:=False;
  end;

var tmp : TAxisIncrement;
begin
  tmp:=TAxisIncrement.Create(Self);

  with tmp do
  try
    IsDateTime :=CalcIsDateTime;
    IsExact    :=True;
    Increment  :=IFunction.Period;
    IStep      :=FindDateTimeStep(IFunction.Period);
    Caption    :=TeeMsg_PeriodRange;

    //PositionToCenter(tmp);

    TeeTranslateControl(tmp);

    if ShowModal=mrOk then
       ENum.Text:=FloatToStr(Increment);
  finally
    Free;
  end;
end;

procedure TTeeFunctionEditor.CBAlignChange(Sender: TObject);
begin
  EnableApply;
end;

procedure TTeeFunctionEditor.CBStyleChange(Sender: TObject);
var tmpMax : Double;
begin
  ENum.Enabled:=CBStyle.ItemIndex<>0;
  BChange.Enabled:=CBStyle.ItemIndex=2;

  if Assigned(IFunction) then tmpMax:=IFunction.Period
                         else tmpMax:=0;

  case CBStyle.ItemIndex of
    0: ENum.Text:='';
    1: ENum.Text:=FloatToStr(Round(Math.Max(0,tmpMax)))
  else ENum.Text:=FloatToStr(tmpMax);
  end;

  if ENum.Enabled then ENum.SetFocus;

  EnableApply;
end;

procedure TTeeFunctionEditor.ENumTyping(Sender: TObject);
begin
  EnableApply;
end;

Procedure TTeeFunctionEditor.SetFunction;
var CanRange : Boolean;
begin
  inherited;

  CanRange:=not (IFunction is TTeeMovingFunction);

  With IFunction do
  begin
    if (PeriodStyle=psRange) and CanRange then
       CBStyle.ItemIndex:=2
    else
    if Round(Period)=0 then CBStyle.ItemIndex:=0
                       else CBStyle.ItemIndex:=1;

    CBAlign.ItemIndex:=Ord(PeriodAlign);
  end;

  ENum.Enabled:=CBStyle.ItemIndex<>0;
  BChange.Enabled:=CBStyle.ItemIndex=2;

  CBStyleChange(Self);
end;

initialization
  RegisterSeriesEditor(TTeeFunctionEditor);
end.
