unit Settings;

interface

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

type
  TSettingsForm = class(TForm)
    GroupBox1: TGroupBox;
    eTimeInterval: TEdit;
    Label1: TLabel;
    bOk: TButton;
    bCancel: TButton;
    GroupBox2: TGroupBox;
    bBrowse: TButton;
    eFileName: TEdit;
    chbSaveToFile: TCheckBox;
    Label2: TLabel;
    eCount: TEdit;
    eSampleRate: TEdit;
    eAcquisitionInterval: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    chbOnScreen: TCheckBox;
    procedure chbSaveToFileClick(Sender: TObject);
    procedure bBrowseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure bOkClick(Sender: TObject);
    procedure eTimeIntervalChange(Sender: TObject);
    procedure eSampleRateChange(Sender: TObject);
    procedure eAcquisitionIntervalChange(Sender: TObject);
    procedure eCountChange(Sender: TObject);
  private
//    function TryConvertInt(Sender : TCustomEdit;var  Value : integer; Range : boolean = false; Min: integer = 0; Max: integer = 100): boolean;
    function TryConvertFloat(Sender : TCustomEdit;var  Value : double; Range : boolean = false; Min: double = 0; Max: double = 100): boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  SettingsForm: TSettingsForm;

implementation

uses
  Main, Config;

{$R *.dfm}
{
function TSettingsForm.TryConvertInt(Sender : TCustomEdit;var  Value : integer; Range : boolean = false; Min: integer = 0; Max: integer = 100): boolean;
begin
  Result := False;
  try
    Value := StrToInt(Sender.Text);
    if Range and ((Value < Min) or (Value > Max)) then
        raise EConvertError.CreateFmt('Value is out of range [%d..%d]', [Min, Max]);
    Result := True;
  except
    on E : EConvertError do
    begin
//      PageControl.ActivePageIndex := TTabSheet(Sender.Parent.Parent).TabIndex;
      Sender.SetFocus;
      Application.ShowException(E);
    end;
  end;
end;
}
function TSettingsForm.TryConvertFloat(Sender : TCustomEdit;var  Value : double; Range : boolean = false; Min: double = 0; Max: double = 100): boolean;
begin
  Result := False;
  try
    Value := StrToFloat(Sender.Text);
    if Range and ((Value < Min) or (Value > Max)) then
        raise EConvertError.CreateFmt('Value is out of range [%f..%f]', [Min, Max]);
    Result := True;
  except
    on E : EConvertError do
    begin
//      PageControl.ActivePageIndex := TTabSheet(Sender.Parent.Parent).TabIndex;
      Sender.SetFocus;
      Application.ShowException(E);
    end;
  end;
end;

procedure TSettingsForm.chbSaveToFileClick(Sender: TObject);
begin
  eFileName.Enabled := chbSaveToFile.Checked;
  bBrowse.Enabled   := chbSaveToFile.Checked;  
end;

procedure TSettingsForm.bBrowseClick(Sender: TObject);
begin
  with TSaveDialog.Create(Self) do
  try
    FileName :=ChangeFileExt(ExtractFileName(eFileName.Text), '');
    Filter := 'Текстовый файл (разделители - табуляция)(*.txt)|*.txt|' +
               'CSV (разделители - запятые)(*.csv)|*.csv';
    if SameText(ExtractFileExt(eFileName.Text), '.csv') then
    begin
      DefaultExt  := 'csv';
      FilterIndex := 2;
    end
    else
    begin
      DefaultExt  := 'txt';
      FilterIndex := 1;
    end;

    if Execute then
        eFileName.Text := FileName;
  finally
    free;
  end;

end;

procedure TSettingsForm.FormCreate(Sender: TObject);
begin
  with Configuration do
  begin
    eTimeInterval.Text        := FloatToStr(MainItem.TimeInterval);
    chbSaveToFile.Checked     := MainItem.SaveToFile;
    chbOnScreen.Checked       := MainItem.OnScreen;
    eFileName.Text            := MainItem.FileName;
    eCount.Text               := FloatToStr(MainItem.Count);
    eAcquisitionInterval.Text := FloatToStr(MainItem.AcquisitionInterval);
    eSampleRate.Text          := FloatToStr(MainItem.SampleRate);
  end;
end;

procedure TSettingsForm.bOkClick(Sender: TObject);
var
  TimeInterval : double;
  Count        : double;
  SampleRate   : double;
  AcquisitionInterval : double;
  FileStream : TFileStream;
begin
  if TryConvertFloat(eTimeInterval, TimeInterval, true, 0, MaxInt) and
    TryConvertFloat(eSampleRate, SampleRate, true, 0, INFINITE) and
    TryConvertFloat(eAcquisitionInterval, AcquisitionInterval, true, 0, INFINITE) and
    TryConvertFloat(eCount, Count, true, 0, MaxInt)
    then
  begin
    if chbSaveToFile.Checked then
    begin
      FileStream := TFileStream.Create(eFileName.Text, fmCreate);
      FileStream.Free;
    end;

    with Configuration do
    begin
      MainItem.AcquisitionInterval := AcquisitionInterval;
      MainItem.SampleRate          := SampleRate;
      MainItem.TimeInterval        := TimeInterval;
      MainItem.FileName            := eFileName.Text;
      MainItem.SaveToFile          := chbSaveToFile.Checked;
      MainItem.OnScreen            := chbOnScreen.Checked;
      MainItem.Count               := Count;
    end;
    ModalResult := mrOk;
  end

end;

procedure TSettingsForm.eTimeIntervalChange(Sender: TObject);
begin
  if ActiveControl = Sender then
  begin
    try
      eSampleRate.Text := FloatToStr( 1 / StrToFloat(TCustomEdit(Sender).Text) );
    except
      eSampleRate.Text := '';
    end;
    eCountChange(eCount);
  end;
end;

procedure TSettingsForm.eSampleRateChange(Sender: TObject);
begin
  if ActiveControl = Sender then
  begin
    try
      eTimeInterval.Text := FloatToStr( 1 / StrToFloat(TCustomEdit(Sender).Text) );
    except
      eTimeInterval.Text := '';
    end;
    eCountChange(eCount);
  end;

end;

procedure TSettingsForm.eAcquisitionIntervalChange(Sender: TObject);
begin
  if ActiveControl = Sender then
      try
        eCount.Text := FloatToStr(  StrToFloat(TCustomEdit(Sender).Text) / StrToFloat(eTimeInterval.Text));
      except
        eCount.Text := '';
      end;

end;

procedure TSettingsForm.eCountChange(Sender: TObject);
begin
//  if ActiveControl = Sender then
      try
        eAcquisitionInterval.Text := FloatToStr( StrToFloat(TCustomEdit(Sender).Text) * StrToFloat(eTimeInterval.Text));
      except
        eAcquisitionInterval.Text := '';
      end;

end;

end.
