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;
    chbChannel0: TCheckBox;
    chbChannel1: TCheckBox;
    chbChannel2: TCheckBox;
    chbChannel3: TCheckBox;
    chbChannel4: TCheckBox;
    chbChannel5: TCheckBox;
    chbChannel6: TCheckBox;
    chbChannel7: TCheckBox;
    tsCalibration: TTabSheet;
    gbCalibName: TGroupBox;
    lCalibChannel: TLabel;
    lK: TLabel;
    lS: TLabel;
    eK: TEdit;
    eS: TEdit;
    cbCalibChannel: TComboBox;
    bChange: TButton;
    eRound0: TEdit;
    eRound1: TEdit;
    eRound2: TEdit;
    eRound3: TEdit;
    eRound4: TEdit;
    eRound5: TEdit;
    eRound6: TEdit;
    eRound7: TEdit;
    lRound: TLabel;
    lOnOff: TLabel;
    cbDiapason1: TComboBox;
    cbDiapason2: TComboBox;
    cbDiapason3: TComboBox;
    cbDiapason4: TComboBox;
    cbDiapason5: TComboBox;
    cbDiapason6: TComboBox;
    cbDiapason7: TComboBox;
    Label1: TLabel;
    cbDiapason0: TComboBox;
    cbADCS: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure bOkClick(Sender: TObject);
    procedure cbCalibGainChange(Sender: TObject);
    procedure bChangeClick(Sender: TObject);
    procedure cbADCSChange(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]);

  cbDiapason0.ItemIndex := Configuration.General.Diapason[0];
  cbDiapason1.ItemIndex := Configuration.General.Diapason[1];
  cbDiapason2.ItemIndex := Configuration.General.Diapason[2];
  cbDiapason3.ItemIndex := Configuration.General.Diapason[3];
  cbDiapason4.ItemIndex := Configuration.General.Diapason[4];
  cbDiapason5.ItemIndex := Configuration.General.Diapason[5];
  cbDiapason6.ItemIndex := Configuration.General.Diapason[6];
  cbDiapason7.ItemIndex := Configuration.General.Diapason[7];

  cbADCS.ItemIndex := Configuration.General.ADCS;
  cbADCSChange(nil);
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;
}
    Configuration.General.Diapason[0] := cbDiapason0.ItemIndex;
    Configuration.General.Diapason[1] := cbDiapason1.ItemIndex;
    Configuration.General.Diapason[2] := cbDiapason2.ItemIndex;
    Configuration.General.Diapason[3] := cbDiapason3.ItemIndex;
    Configuration.General.Diapason[4] := cbDiapason4.ItemIndex;
    Configuration.General.Diapason[5] := cbDiapason5.ItemIndex;
    Configuration.General.Diapason[6] := cbDiapason6.ItemIndex;
    Configuration.General.Diapason[7] := cbDiapason7.ItemIndex;

    Configuration.General.ADCS := cbADCS.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]);
    GroupBox.Caption := Format('Канал: %s', [cbCalibChannel.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;

procedure TSettingsForm.cbADCSChange(Sender: TObject);
var i : integer;
begin
  case cbADCS.ItemIndex of
    0 : begin
          chbChannel0.Visible := true;
          chbChannel1.Visible := false;
          chbChannel2.Visible := false;
          chbChannel3.Visible := false;
          chbChannel4.Visible := false;
          chbChannel5.Visible := false;
          chbChannel6.Visible := false;
          chbChannel7.Visible := false;

          eRound0.Visible := true;
          eRound1.Visible := false;
          eRound2.Visible := false;
          eRound3.Visible := false;
          eRound4.Visible := false;
          eRound5.Visible := false;
          eRound6.Visible := false;
          eRound7.Visible := false;

          cbDiapason0.Visible := true;
          cbDiapason1.Visible := false;
          cbDiapason2.Visible := false;
          cbDiapason3.Visible := false;
          cbDiapason4.Visible := false;
          cbDiapason5.Visible := false;
          cbDiapason6.Visible := false;
          cbDiapason7.Visible := false;

          cbCalibChannel.Items.Text := '1';
          cbCalibChannel.ItemIndex  := 0;

          i := cbDiapason0.ItemIndex;
          cbDiapason0.Items.Text := '0 В…+5 В'#13'-5 В…+5 В';
          cbDiapason0.ItemIndex  := i;
        end;
    1 : begin
          chbChannel0.Visible := true;
          chbChannel1.Visible := true;
          chbChannel2.Visible := true;
          chbChannel3.Visible := true;
          chbChannel4.Visible := true;
          chbChannel5.Visible := true;
          chbChannel6.Visible := true;
          chbChannel7.Visible := true;

          eRound0.Visible := true;
          eRound1.Visible := true;
          eRound2.Visible := true;
          eRound3.Visible := true;
          eRound4.Visible := true;
          eRound5.Visible := true;
          eRound6.Visible := true;
          eRound7.Visible := true;

          cbDiapason0.Visible := true;
          cbDiapason1.Visible := true;
          cbDiapason2.Visible := true;
          cbDiapason3.Visible := true;
          cbDiapason4.Visible := true;
          cbDiapason5.Visible := true;
          cbDiapason6.Visible := true;
          cbDiapason7.Visible := true;

          cbCalibChannel.Items.Text := '1'#13'2'#13'3'#13'4'#13'5'#13'6'#13'7'#13'8';
          cbCalibChannel.ItemIndex  := 0;

          i := cbDiapason0.ItemIndex;
          cbDiapason0.Items.Text := '0 В…+5 В'#13'-10 В…+10 В';
          cbDiapason0.ItemIndex  := i;

        end;
  end;
end;

end.



