unit FilePropsUnit;
// ---------------------------------------
// Display / Edit EDR data file properties
// ---------------------------------------
// 25.07.06

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, global, maths, fileio, ValidatedEdit ;

type
  TFilePropsDlg = class(TForm)
    Channels: TGroupBox;
    ChannelTable: TStringGrid;
    bCancel: TButton;
    bOK: TButton;
    GroupBox1: TGroupBox;
    edSamplingInterval: TValidatedEdit;
    Label1: TLabel;
    Label2: TLabel;
    edADCVoltageRange: TValidatedEdit;
    Label3: TLabel;
    edADCResolution: TValidatedEdit;
    procedure FormShow(Sender: TObject);
    procedure bOKClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure bCancelClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FilePropsDlg: TFilePropsDlg;

implementation

uses MDIForm, math ;

const
     ChNum = 0 ;
     ChName = 1 ;
     ChCal = 2 ;
     ChUnits = 3 ;

{$R *.dfm}

procedure TFilePropsDlg.FormShow(Sender: TObject);
// --------------------------------------
// Initialisations when form is displayed
// --------------------------------------
var
    ch : Integer ;
begin

     edSamplingInterval.Value := CDRFH.dt ;
     edADCVoltageRange.Value := CDRFH.ADCVoltageRange ;

     edADCResolution.Value := Log2(Channel[0].ADCMaxValue+1)+1 ;

     { Set channel calibration table }
     ChannelTable.cells[ChNum,0] := 'Ch.' ;
     ChannelTable.colwidths[ChNum] := ChannelTable.DefaultColWidth div 2 ;
     ChannelTable.cells[ChName,0] := 'Name' ;
     ChannelTable.colwidths[ChName] := ChannelTable.DefaultColWidth ;
     ChannelTable.cells[ChCal,0] := 'V/Units' ;
     ChannelTable.colwidths[ChCal] := (5*ChannelTable.DefaultColWidth) div 4 ;
     ChannelTable.cells[ChUnits,0] := 'Units' ;
     ChannelTable.colwidths[ChUnits] := ChannelTable.DefaultColWidth ;
     ChannelTable.RowCount := CDRFH.NumChannels + 1;
     ChannelTable.options := [goEditing,goHorzLine,goVertLine] ;

     for ch := 0 to CDRfH.NumChannels-1 do begin
         ChannelTable.cells[ChNum,ch+1] := IntToStr(ch) ;
         ChannelTable.cells[ChName,ch+1] := Channel[ch].ADCName ;
         ChannelTable.cells[ChCal,ch+1] := Format( '%5.4g',[Channel[ch].ADCCalibrationFactor] ) ;
         ChannelTable.cells[ChUnits,ch+1] := Channel[ch].ADCUnits ;
         end ;


     end;

procedure TFilePropsDlg.bOKClick(Sender: TObject);
// -------------------------------
// Update changes to file settings
// -------------------------------
var
    ch : Integer ;
begin

     CDRFH.dt := edSamplingInterval.Value ;
     CDRFH.ADCVoltageRange := edADCVoltageRange.Value ;
     CDRFH.ADCMaxBits := Round(Power(2.0,edADCResolution.Value-1)) - 1 ;

     { Channel calibration }
     for ch := 0 to CDRFH.NumChannels-1 do begin

         Channel[0].ADCMaxValue := Round(CDRFH.ADCMaxBits) ;
         Channel[ch].ADCName := ChannelTable.cells[ChName,ch+1] ;
         Channel[ch].ADCCalibrationFactor := ExtractFloat(
                                             ChannelTable.cells[ChCal,ch+1],
                                             RecChannel[ch].ADCCalibrationFactor);
         Channel[ch].ADCUnits := ChannelTable.cells[ChUnits,ch+1] ;
         end ;

     // Save to file header
     SaveCDRHeader( CDRFH ) ;

     Main.UpdateChannelScalingFactors ; ;

     Close ;

     end;


procedure TFilePropsDlg.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
     Action := caFree ;
     end;

procedure TFilePropsDlg.bCancelClick(Sender: TObject);
begin
     Close ;
     end;

end.
