{********************************************}
{ TSeriesStatsTool and Editor Dialog         }
{ Copyright (c) 2006-2020 by Steema Software }
{********************************************}
unit TeeSeriesStatsEditor;
{$I TeeDefs.inc}

// Tool that provides easy access to choosen Series statistics like
// trend, averages, minimum and maximum values, etc.

interface

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

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

  TeeToolSeriesEdit, TeeProcs, TeEngine, Chart, TeCanvas, TeeSeriesStats;

type
  TSeriesStatsEditor = class(TSeriesToolEditor)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    CheckBox3: TCheckBox;
    Button3: TButton;
    CheckBox4: TCheckBox;
    Button4: TButton;
    CheckBox5: TCheckBox;
    Button5: TButton;
    CheckBox6: TCheckBox;
    Button6: TButton;
    BRemoveAll: TButton;
    procedure CBSeriesChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
    procedure CheckBox4Click(Sender: TObject);
    procedure CheckBox5Click(Sender: TObject);
    procedure CheckBox6Click(Sender: TObject);
    procedure BRemoveAllClick(Sender: TObject);
  private
    { Private declarations }
    procedure CheckFunction(const ABox:TCheckBox; AFunctionClass:TTeeFunctionClass);
    procedure EnableControls;
    procedure EnableRemoveAll;
    procedure RefreshInfo;
  public
    { Public declarations }
  end;

implementation

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

uses
  Math,
  TeeFunci, StatChar, CurvFitt, TeePenDlg, TeeEdiSeri, TeeConst, TeeProCo;

procedure TSeriesStatsEditor.CBSeriesChange(Sender: TObject);
begin
  inherited;

  EnableControls;
  RefreshInfo;
end;

type
  TSeriesStatsToolAccess=class(TSeriesStatsTool);

procedure TSeriesStatsEditor.EnableControls;

  procedure EnableCheck(const AButton:TButton; AClass:TTeeFunctionClass);
  var tmp : TCheckBox;
  begin
    tmp:=TCheckBox(AButton.Tag);

    tmp.Enabled:=Assigned(Tool) and (TheSeries<>nil);

    if Assigned(Tool) then
       tmp.Tag:=ObjectToTag(TSeriesStatsToolAccess(Tool).GetStatFunction(AClass));

    tmp.Checked:=tmp.Tag<>0;
    AButton.Enabled:=tmp.Checked;
  end;

begin
  EnableCheck(Button1,TAverageTeeFunction);
  EnableCheck(Button2,TTrendFunction);
  EnableCheck(Button3,TLowTeeFunction);
  EnableCheck(Button4,THighTeeFunction);
  EnableCheck(Button5,TMedianTeeFunction);
  EnableCheck(Button6,TCurveFittingFunction);

  EnableRemoveAll;
end;

procedure TSeriesStatsEditor.EnableRemoveAll;
begin
  BRemoveAll.Enabled:=Button1.Enabled or Button2.Enabled or Button3.Enabled or
                      Button4.Enabled or Button5.Enabled or Button6.Enabled;
end;

procedure TSeriesStatsEditor.RefreshInfo;
begin
  Memo1.Lines.Clear;

  if TheSeries<>nil then
     (Tool as TSeriesStatsTool).Statistics(Memo1.Lines);
end;

procedure TSeriesStatsEditor.PageControl1Change(Sender: TObject);
begin
  if PageControl1.ActivePage=TabSheet2 then
     RefreshInfo;
end;

procedure TSeriesStatsEditor.FormCreate(Sender: TObject);
begin
  inherited;
  PageControl1.ActivePage:=TabSheet1;

  Button1.Tag:=ObjectToTag(CheckBox1);
  Button2.Tag:=ObjectToTag(CheckBox2);
  Button3.Tag:=ObjectToTag(CheckBox3);
  Button4.Tag:=ObjectToTag(CheckBox4);
  Button5.Tag:=ObjectToTag(CheckBox5);
  Button6.Tag:=ObjectToTag(CheckBox6);
end;

procedure TSeriesStatsEditor.CheckFunction(const ABox:TCheckBox; AFunctionClass:TTeeFunctionClass);
var tmp : TTeeFunction;
begin
  tmp:=TSeriesStatsToolAccess(Tool).GetStatFunction(AFunctionClass);

  // If a series exists, just change its visibility:
  if Assigned(tmp) then
     tmp.ParentSeries.Visible:=ABox.Checked
  else
  if ABox.Checked then
  begin
    // Otherwise, if we want that series, create it:

    tmp:=AFunctionClass.Create(Tool.Owner);
    tmp.ParentSeries:=TChartSeriesClass(TheSeries.ClassType).Create(Tool.Owner);
    tmp.ParentSeries.ShowInEditor:=False;
    tmp.ParentSeries.ParentChart:=Tool.ParentChart;
    tmp.ParentSeries.DataSource:=TheSeries;
    tmp.ParentSeries.FunctionType:=tmp;

    ABox.Tag:=ObjectToTag(tmp);
  end;

  EnableRemoveAll;
end;

procedure TSeriesStatsEditor.CheckBox1Click(Sender: TObject);
begin
  Button1.Enabled:=CheckBox1.Checked;
  CheckFunction(CheckBox1,TAverageTeeFunction);
end;

procedure TSeriesStatsEditor.Button1Click(Sender: TObject);
var tmp : TCheckBox;
begin
  tmp:=TCheckBox((Sender as TButton).Tag);
  EditSeriesDialog(Self,TTeeFunction(tmp.Tag).ParentSeries);
end;

procedure TSeriesStatsEditor.CheckBox2Click(Sender: TObject);
begin
  Button2.Enabled:=CheckBox2.Checked;
  CheckFunction(CheckBox2,TTrendFunction);
end;

procedure TSeriesStatsEditor.FormShow(Sender: TObject);
begin
  inherited;
  EnableControls;
end;

procedure TSeriesStatsEditor.CheckBox3Click(Sender: TObject);
begin
  Button3.Enabled:=CheckBox3.Checked;
  CheckFunction(CheckBox3,TLowTeeFunction);
end;

procedure TSeriesStatsEditor.CheckBox4Click(Sender: TObject);
begin
  Button4.Enabled:=CheckBox4.Checked;
  CheckFunction(CheckBox4,THighTeeFunction);
end;

procedure TSeriesStatsEditor.CheckBox5Click(Sender: TObject);
begin
  Button5.Enabled:=CheckBox5.Checked;
  CheckFunction(CheckBox5,TMedianTeeFunction);
end;

procedure TSeriesStatsEditor.CheckBox6Click(Sender: TObject);
begin
  Button6.Enabled:=CheckBox6.Checked;
  CheckFunction(CheckBox6,TCurveFittingFunction);
end;

procedure TSeriesStatsEditor.BRemoveAllClick(Sender: TObject);
begin
  (Tool as TSeriesStatsTool).FreeStatsSeries;
  EnableControls;
end;

initialization
  RegisterClass(TSeriesStatsEditor);
end.
