{********************************************}
{     TeeChart Pro Charting Library          }
{ Copyright (c) 1995-2020 by Steema Software }
{         All Rights Reserved                }
{********************************************}
unit TeeEdiPeri;
{$I TeeDefs.inc}

interface

uses {$IFNDEF LINUX}
     Windows, Messages,
     {$ENDIF}
     SysUtils, Classes,

     Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Buttons,

     TeeProcs, TeEngine, TeCanvas, TeeBaseFuncEdit;

type
  TTeeFunctionEditor = class(TBaseFunctionEditor)
    ENum: TEdit;
    BChange: TButton;
    Label1: TLabel;
    CBAlign: TComboFlat;
    Label2: TLabel;
    CBStyle: TComboFlat;
    Label3: TLabel;
    procedure BChangeClick(Sender: TObject);
    procedure ENumChange(Sender: TObject);
    procedure CBAlignChange(Sender: TObject);
    procedure CBStyleChange(Sender: TObject);
  private
    { Private declarations }
  protected
    procedure ApplyFormChanges; override;
    Procedure SetFunction; override;
  public
    { Public declarations }
  end;

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

uses
  Math, TeeAxisIncr, TeeConst, TeePenDlg;

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;

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.ENumChange(Sender: TObject);
begin
  EnableApply;
end;

procedure TTeeFunctionEditor.ApplyFormChanges;
begin
  inherited;

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

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

      Period:=StrToFloatDef(ENum.Text,0);
    finally
      EndUpdate;
    end;
  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;

initialization
  RegisterClass(TTeeFunctionEditor);
end.
