unit Settings;

interface

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

type
  TSettingsForm = class(TForm)
    bCancel: TButton;
    bOk: TButton;
    PageControl: TPageControl;
    tsSettings: TTabSheet;
    gbADC: TGroupBox;
    lGain: TLabel;
    chbChannel0: TCheckBox;
    cbGain0: TComboBox;
    chbChannel1: TCheckBox;
    chbChannel2: TCheckBox;
    chbChannel3: TCheckBox;
    chbChannel4: TCheckBox;
    chbChannel5: TCheckBox;
    chbChannel6: TCheckBox;
    chbChannel7: TCheckBox;
    cbGain1: TComboBox;
    cbGain2: TComboBox;
    cbGain3: TComboBox;
    cbGain4: TComboBox;
    cbGain5: TComboBox;
    cbGain6: TComboBox;
    cbGain7: TComboBox;
    tsCalibration: TTabSheet;
    gbCalibName: TGroupBox;
    lCalibChannel: TLabel;
    lK: TLabel;
    lS: TLabel;
    eK: TEdit;
    eS: TEdit;
    lCalibGain: TLabel;
    cbCalibGain: TComboBox;
    cbCalibChannel: TComboBox;
    bChange: TButton;
    eRound0: TEdit;
    eRound1: TEdit;
    eRound2: TEdit;
    eRound3: TEdit;
    eRound4: TEdit;
    eRound5: TEdit;
    eRound6: TEdit;
    eRound7: TEdit;
    lRound: TLabel;
    lOnOff: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure bOkClick(Sender: TObject);
    procedure cbCalibGainChange(Sender: TObject);
    procedure bChangeClick(Sender: TObject);
  private
    { Private declarations }
    // Загрузить конфигурацию из реестра
    procedure LoadConfiguration;

    // Сохранить конфигурацию в реестр
    function SaveConfiguration: boolean;
  public
    class procedure Execute;
    function TryConvertInt(Sender : TCustomEdit; var Value : integer; Range : boolean = false; Min: integer = 0; Max: integer = 100): boolean;
    { Public declarations }
  end;

implementation

uses
  Edit, Config, Calib;

{$R *.dfm}

class procedure TSettingsForm.Execute;
begin
  with TSettingsForm.Create(Application) do
  try
    ShowModal;
  finally
    Free;
  end;
end;

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;

procedure TSettingsForm.FormCreate(Sender: TObject);
begin
  LoadConfiguration;

  gbCalibName.Caption := Calibration.CalibName;
  Calibration.Load(gbCalibName.Caption);

  cbCalibGainChange(nil);

end;

procedure TSettingsForm.LoadConfiguration;
begin
  chbChannel0.Checked := Configuration.General.Channel[0];
  chbChannel1.Checked := Configuration.General.Channel[1];
  chbChannel2.Checked := Configuration.General.Channel[2];
  chbChannel3.Checked := Configuration.General.Channel[3];
  chbChannel4.Checked := Configuration.General.Channel[4];
  chbChannel5.Checked := Configuration.General.Channel[5];
  chbChannel6.Checked := Configuration.General.Channel[6];
  chbChannel7.Checked := Configuration.General.Channel[7];

  cbGain0.ItemIndex := Configuration.General.Gain[0];
  cbGain1.ItemIndex := Configuration.General.Gain[1];
  cbGain2.ItemIndex := Configuration.General.Gain[2];
  cbGain3.ItemIndex := Configuration.General.Gain[3];
  cbGain4.ItemIndex := Configuration.General.Gain[4];
  cbGain5.ItemIndex := Configuration.General.Gain[5];
  cbGain6.ItemIndex := Configuration.General.Gain[6];
  cbGain7.ItemIndex := Configuration.General.Gain[7];

  eRound0.Text := IntToStr(Configuration.General.Round[0]);
  eRound1.Text := IntToStr(Configuration.General.Round[1]);
  eRound2.Text := IntToStr(Configuration.General.Round[2]);
  eRound3.Text := IntToStr(Configuration.General.Round[3]);
  eRound4.Text := IntToStr(Configuration.General.Round[4]);
  eRound5.Text := IntToStr(Configuration.General.Round[5]);
  eRound6.Text := IntToStr(Configuration.General.Round[6]);
  eRound7.Text := IntToStr(Configuration.General.Round[7]);
end;

function TSettingsForm.SaveConfiguration: boolean;
var
  Round :array [0..7] of integer;
begin
  Result := TryConvertInt(eRound0, Round[0], true, 0, 15) and
    TryConvertInt(eRound1, Round[1], true, 0, 15) and
    TryConvertInt(eRound2, Round[2], true, 0, 15) and
    TryConvertInt(eRound3, Round[3], true, 0, 15) and
    TryConvertInt(eRound4, Round[4], true, 0, 15) and
    TryConvertInt(eRound5, Round[5], true, 0, 15) and
    TryConvertInt(eRound6, Round[6], true, 0, 15) and
    TryConvertInt(eRound7, Round[7], true, 0, 15);

  if Result then
  begin
    Configuration.General.Round[0] := Round[0];
    Configuration.General.Round[1] := Round[1];
    Configuration.General.Round[2] := Round[2];
    Configuration.General.Round[3] := Round[3];
    Configuration.General.Round[4] := Round[4];
    Configuration.General.Round[5] := Round[5];
    Configuration.General.Round[6] := Round[6];
    Configuration.General.Round[7] := Round[7];

    Configuration.General.Channel[0] := chbChannel0.Checked;
    Configuration.General.Channel[1] := chbChannel1.Checked;
    Configuration.General.Channel[2] := chbChannel2.Checked;
    Configuration.General.Channel[3] := chbChannel3.Checked;
    Configuration.General.Channel[4] := chbChannel4.Checked;
    Configuration.General.Channel[5] := chbChannel5.Checked;
    Configuration.General.Channel[6] := chbChannel6.Checked;
    Configuration.General.Channel[7] := chbChannel7.Checked;

    Configuration.General.Gain[0] := cbGain0.ItemIndex;
    Configuration.General.Gain[1] := cbGain1.ItemIndex;
    Configuration.General.Gain[2] := cbGain2.ItemIndex;
    Configuration.General.Gain[3] := cbGain3.ItemIndex;
    Configuration.General.Gain[4] := cbGain4.ItemIndex;
    Configuration.General.Gain[5] := cbGain5.ItemIndex;
    Configuration.General.Gain[6] := cbGain6.ItemIndex;
    Configuration.General.Gain[7] := cbGain7.ItemIndex;
  end;
end;


procedure TSettingsForm.bOkClick(Sender: TObject);
begin
  if SaveConfiguration then
      Calibration.Save(gbCalibName.Caption)
  else
      ModalResult := mrNone;
end;

procedure TSettingsForm.cbCalibGainChange(Sender: TObject);
begin
  eK.Text := FloatToStr(Calibration.Calib.K[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex]);
  eS.Text := FloatToStr(Calibration.Calib.S[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex]);
end;

procedure TSettingsForm.bChangeClick(Sender: TObject);
begin
  with TEditForm.Create(Self) do
  try
    GroupBox.Caption := Format('Канал: %s; Усиление: %s', [cbCalibChannel.Text, cbCalibGain.Text]);
    eK.Text := FloatToStr(Calibration.Calib.K[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex]);
    eS.Text := FloatToStr(Calibration.Calib.S[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex]);
    ShowModal;
    if ModalResult = mrOk then
    begin
      Calibration.Calib.K[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex] := StrToFloat(eK.Text);
      Calibration.Calib.S[cbCalibChannel.ItemIndex, cbCalibGain.ItemIndex] := StrToFloat(eS.Text);
      cbCalibGainChange(nil);
    end;
  finally
    free;
  end;

end;

end.



