{********************************************}
{    TKDE Function Editor Dialog              }
{ Copyright (c) 1999-2020 by Steema Software }
{********************************************}
unit TeeKDEEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls, ComCtrls, StatChar,
  TeeBoxplot;

type
  TKDEEditor = class(TForm)
    PageControl1: TPageControl;
    TabViolin: TTabSheet;
    TabKDE: TTabSheet;
    GroupBox1: TGroupBox;
    LRes: TLabel;
    IntRes: TEdit;
    CheckBox1: TCheckBox;
    Label1: TLabel;
    IntBandW: TEdit;
    lblClamp: TLabel;
    cbClamp: TComboBox;
    Label3: TLabel;
    EdiClampMin: TEdit;
    Label4: TLabel;
    EdiClampMax: TEdit;
    GroupBox2: TGroupBox;
    Label5: TLabel;
    IntPos: TEdit;

    procedure FormShow(Sender: TObject);
    procedure IntResChange(Sender: TObject);
    procedure IntBandWChange(Sender: TObject);
    procedure IntWidthChange(Sender: TObject);
    procedure cbClampChange(Sender: TObject);
    procedure EdiClampMinChange(Sender: TObject);
    procedure EdiClampMaxChange(Sender: TObject);
    procedure IntPosChange(Sender: TObject);
  private
    { Private declarations }
    Violin : TViolinSeries;
    procedure EnableClampMinMax;
  public
    { Public declarations }
  end;

var
  KDEEditor: TKDEEditor;
  FIsBeeswarm : Boolean;


implementation

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

procedure TKDEEditor.IntWidthChange(Sender: TObject);
begin
  if Showing then
  Begin
     //Violin.KDEFunction.Options.Width:=StrToInt(IntWidth.Text);
     Violin.Repaint;
  End;
end;

procedure TKDEEditor.cbClampChange(Sender: TObject);
begin
  if Showing then
  Begin
     Violin.KDEFunction.Options.Clamp:=TKDEClamp(cbClamp.ItemIndex);
     if Violin.KDEFunction.Options.Clamp=kdcManual then
       if (Violin.KDEFunction.ClampMin=0) and (Violin.KDEFunction.ClampMax=0) then
       Begin
         Violin.KDEFunction.ClampMin := Violin.MinYValue;
         Violin.KDEFunction.ClampMax := Violin.MaxYValue;
       End;

     EnableClampMinMax;
     Violin.Repaint;
  End;
end;

procedure TKDEEditor.EdiClampMaxChange(Sender: TObject);
begin
  if Showing then
  Begin
     Violin.KDEFunction.ClampMax:=StrToFloat(EdiClampMax.Text);
     Violin.Update;
  End;
end;

procedure TKDEEditor.EdiClampMinChange(Sender: TObject);
begin
  if Showing then
  Begin
     Violin.KDEFunction.ClampMin:=StrToFloat(EdiClampMin.Text);
     Violin.Update;
  End;
end;

procedure TKDEEditor.EnableClampMinMax;
begin
  EdiClampMin.Text := FloatToStr(Violin.KDEFunction.ClampMin);
  EdiClampMax.Text := FloatToStr(Violin.KDEFunction.ClampMax);

  EdiClampMin.Enabled := not (Violin.KDEFunction.Options.Clamp = kdcMinMax);
  EdiClampMax.Enabled := not (Violin.KDEFunction.Options.Clamp = kdcMinMax);
end;

procedure TKDEEditor.FormShow(Sender: TObject);
begin
  FIsBeeswarm := False;

  Violin := TViolinSeries(Tag);

  if Violin.KDEFunction.Options.KDEType = kdtBeeSwarm then
    FIsBeeswarm := True;

  if FIsBeeswarm then
    Violin := TBeeSwarmSeries(Tag);

  IntPos.Text := FloatToStr(Violin.Position);

  IntRes.Text:=IntToStr(Violin.KDEFunction.Options.Resolution);
  IntBandW.Text := IntToStr(Violin.KDEFunction.Options.Bandwidth);
  //IntWidth.Text := IntToStr(Violin.KDEFunction.Options.Width);
  cbClamp.ItemIndex := Ord(Violin.KDEFunction.Options.Clamp);

  if FIsBeeswarm then
  Begin
    IntRes.Enabled := False;
    IntRes.Visible := False;
    LRes.Visible := False;
    IntBandW.Enabled := False;
    IntBandW.Visible := False;
    Label1.Visible := False;
  End;

  EnableClampMinMax;
end;

procedure TKDEEditor.IntBandWChange(Sender: TObject);
begin
  if Showing then
  Begin
     Violin.KDEFunction.Options.Bandwidth:=StrToInt(IntBandW.Text);
     Violin.Repaint;
  End;
end;

procedure TKDEEditor.IntPosChange(Sender: TObject);
begin
  Violin.Position := StrToInt(IntPos.Text);
  Violin.Repaint;
end;

procedure TKDEEditor.IntResChange(Sender: TObject);
begin
  if Showing then
  Begin
     Violin.KDEFunction.Options.Resolution:=StrToInt(IntRes.Text);
     Violin.Repaint;
  End;
end;

initialization
  RegisterClass(TKDEEditor);

end.
