unit Vis2D_HistogramSettings;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VisCtl2D,
  TextEditor, ColorContainer, Vcl.StdCtrls, ScrollBoxEx, UxPanel, Vcl.ExtCtrls;

type
  TVis2D_HistogramSettingsFrm = class(TDrawableOptionsFrm)
    pnToolbar: TUxClient;
    btnStyle: TUxButton;
    btnData: TUxButton;
    btnBinSettings: TUxButton;
    pnStyle: TPanel;
    pnData: TPanel;
    pnBinSettings: TPanel;
    btnApply: TUxButton;
    ScrollBox1: TScrollBoxEx;
    Label9: TLabel;
    Label8: TLabel;
    eBinWidth: TTextEditor;
    eStartValue: TTextEditor;
    btnApplyData: TUxButton;
    eData: TTextEditor;
    procedure CallUpdateDrawable(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SetPage(Sender: TObject);
    procedure ScrollBox1Enter(Sender: TObject);
    procedure ScrollBox1Exit(Sender: TObject);
    procedure btnApplyDataClick(Sender: TObject);
  strict private
    FPageButtons: TArray<TUxButton>;
    FStyle: TDrawableOptionsFrm;
    FDataLoaded: Boolean;
    procedure LoadData;
    function GetData: TArray<Double>;
  strict protected
    procedure Initialize; override;
    procedure UpdateDrawable; override;
  public

  end;

implementation

uses
  Math, Vis2D_ElementSettings, Generics.Collections;

{$R *.dfm}

{ TVis2D_HistogramSettingsFrm }

procedure TVis2D_HistogramSettingsFrm.btnApplyDataClick(Sender: TObject);
var
  Histogram: THistogram;
begin

  if Initialized and FDataLoaded then
  begin
    Histogram := FDrawable as THistogram;
    Histogram.Data := GetData;
    UpdateDrawable;
  end;

end;

procedure TVis2D_HistogramSettingsFrm.CallUpdateDrawable(Sender: TObject);
begin
  UpdateDrawable;
end;

procedure TVis2D_HistogramSettingsFrm.FormCreate(Sender: TObject);
var
  Histogram: THistogram;
begin

  Histogram := FDrawable as THistogram;

  btnStyle.Tag := NativeInt(pnStyle);
  btnData.Tag := NativeInt(pnData);
  btnBinSettings.Tag := NativeInt(pnBinSettings);
  FPageButtons := [btnStyle, btnData, btnBinSettings];

  SetPage(btnStyle);

  FStyle := TVis2D_ElementSettingsFrm.Create(Self, Histogram.Style);
  FStyle.Parent := pnStyle;
  FStyle.BorderStyle := bsNone;
  FStyle.Align := alClient;
  FStyle.Show;

end;

function TVis2D_HistogramSettingsFrm.GetData: TArray<Double>;
var
  List: TList<Double>;
  i: Integer;
begin

  List := TList<Double>.Create;
  try
    for i := 0 to eData.LineCount - 1 do
      if not eData.TextFile.LineIsEmpty(i) then
        List.Add(StrToFloat(eData.Lines[i]));
    Result := List.ToArray;
  finally
    List.Free;
  end;

end;

procedure TVis2D_HistogramSettingsFrm.Initialize;
var
  Histogram: THistogram;
begin

  inherited;

  Histogram := FDrawable as THistogram;

  eBinWidth.PlainText := Histogram.BinWidth.ToString;
  eStartValue.PlainText := Histogram.StartAt.ToString;

end;

procedure TVis2D_HistogramSettingsFrm.LoadData;
var
  Histogram: THistogram;
  i: Integer;
begin

  Histogram := FDrawable as THistogram;

  eData.BeginVisualUpdate;
  try
    eData.BeginAddLine;
    try
      eData.Clear;
      for i := 0 to High(Histogram.Data) do
        eData.AddLine(Histogram.Data[i].ToString);
    finally
      eData.EndAddLine;
    end;
  finally
    eData.EndVisualUpdate(True);
  end;

  FDataLoaded := True;

end;

procedure TVis2D_HistogramSettingsFrm.ScrollBox1Enter(Sender: TObject);
begin
  btnApply.Default := True;
end;

procedure TVis2D_HistogramSettingsFrm.ScrollBox1Exit(Sender: TObject);
begin
  btnApply.Default := False;
end;

procedure TVis2D_HistogramSettingsFrm.SetPage(Sender: TObject);
var
  B: TUxButton;
  P: TPanel;
begin
  for B in FPageButtons do
  begin
    if B.Tag = 0 then
      Continue;
    P := TObject(B.Tag) as TPanel;
    P.Align := alClient;
    P.Visible := B = Sender;
    B.Down := B = Sender;
    if P = pnData then
      LoadData;
    if (B = Sender) and Visible and P.Visible and P.CanFocus then
      P.SetFocus;
  end;
end;

procedure TVis2D_HistogramSettingsFrm.UpdateDrawable;
var
  Histogram: THistogram;
begin

  if not Initialized then
    Exit;

  Histogram := FDrawable as THistogram;

  Histogram.BinWidth := StrToFloat(eBinWidth.PlainText);
  Histogram.StartAt := StrToFloat(eStartValue.PlainText);

  inherited;

end;

end.
