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;
    lGain2: TLabel;
    lRound2: TLabel;
    lOnOff2: TLabel;
    chbChannel8: TCheckBox;
    cbGain8: TComboBox;
    chbChannel10: TCheckBox;
    chbChannel12: TCheckBox;
    chbChannel14: TCheckBox;
    cbGain10: TComboBox;
    cbGain12: TComboBox;
    cbGain14: TComboBox;
    eRound8: TEdit;
    eRound10: TEdit;
    eRound12: TEdit;
    eRound14: TEdit;
    chbChannel9: TCheckBox;
    cbGain9: TComboBox;
    chbChannel11: TCheckBox;
    chbChannel13: TCheckBox;
    chbChannel15: TCheckBox;
    cbGain11: TComboBox;
    cbGain13: TComboBox;
    cbGain15: TComboBox;
    eRound9: TEdit;
    eRound11: TEdit;
    eRound13: TEdit;
    eRound15: TEdit;
    chbChannel16: TCheckBox;
    cbGain16: TComboBox;
    chbChannel18: TCheckBox;
    chbChannel20: TCheckBox;
    chbChannel22: TCheckBox;
    cbGain18: TComboBox;
    cbGain20: TComboBox;
    cbGain22: TComboBox;
    eRound16: TEdit;
    eRound18: TEdit;
    eRound20: TEdit;
    eRound22: TEdit;
    chbChannel17: TCheckBox;
    cbGain17: TComboBox;
    chbChannel19: TCheckBox;
    chbChannel21: TCheckBox;
    chbChannel23: TCheckBox;
    cbGain19: TComboBox;
    cbGain21: TComboBox;
    cbGain23: TComboBox;
    eRound17: TEdit;
    eRound19: TEdit;
    eRound21: TEdit;
    eRound23: TEdit;
    chbChannel24: TCheckBox;
    cbGain24: TComboBox;
    chbChannel26: TCheckBox;
    chbChannel28: TCheckBox;
    chbChannel30: TCheckBox;
    cbGain26: TComboBox;
    cbGain28: TComboBox;
    cbGain30: TComboBox;
    eRound24: TEdit;
    eRound26: TEdit;
    eRound28: TEdit;
    eRound30: TEdit;
    chbChannel25: TCheckBox;
    cbGain25: TComboBox;
    chbChannel27: TCheckBox;
    chbChannel29: TCheckBox;
    chbChannel31: TCheckBox;
    cbGain27: TComboBox;
    cbGain29: TComboBox;
    cbGain31: TComboBox;
    eRound25: TEdit;
    eRound27: TEdit;
    eRound29: TEdit;
    eRound31: TEdit;
    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, ADCDevice;

{$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;
var
  i : integer;
begin
  for i := 0 to gbADC.ControlCount - 1 do
  begin
    if (gbADC.Controls[i] is TCheckBox) then
        (gbADC.Controls[i] as TCheckBox).Checked := Configuration.General.Channel[gbADC.Controls[i].Tag];

    if (gbADC.Controls[i] is TComboBox) then
        (gbADC.Controls[i] as TComboBox).ItemIndex := Configuration.General.Gain[gbADC.Controls[i].Tag];

    if (gbADC.Controls[i] is TEdit) then
        (gbADC.Controls[i] as TEdit).Text := IntToStr(Configuration.General.Round[gbADC.Controls[i].Tag]);
  end;
end;

function TSettingsForm.SaveConfiguration: boolean;
var
  i : integer;
  Round :array [0..CHANNEL_COUNT - 1] of integer;
begin
  Result := true;
  for i := 0 to gbADC.ControlCount - 1 do
  begin
    if (gbADC.Controls[i] is TEdit) then
        if not TryConvertInt((gbADC.Controls[i] as TEdit), Round[gbADC.Controls[i].tag], true, 0, 15) then
            result := false;
  end;


  if Result then
  begin
    for i := 0 to CHANNEL_COUNT - 1 do
        Configuration.General.Round[i] := Round[i];

    for i := 0 to gbADC.ControlCount - 1 do
    begin
      if (gbADC.Controls[i] is TCheckBox) then
          Configuration.General.Channel[gbADC.Controls[i].Tag] := (gbADC.Controls[i] as TCheckBox).Checked;

      if (gbADC.Controls[i] is TComboBox) then
         Configuration.General.Gain[gbADC.Controls[i].Tag] := (gbADC.Controls[i] as TComboBox).ItemIndex;
    end;
  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.



