{Copyright or © or Copr. :

CNRS - Laboratoire Mouvement & Perception
franck.buloup@univmed.fr

This software is a computer program whose purpose is to highly simplify  
the use of ADwinPro realtime systems when you develop experimental
protocols  on life's sciences area.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.}
unit UGenerator;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TeEngine, Series, Spin, StdCtrls, ExtCtrls, Menus, ActnList,
  TeeProcs, Chart, GeneratorClass, teeprevi, ComCtrls, ToolWin, ActnCtrls,
  ActnMan, ActnMenus, XPStyleActnCtrls;

type
  TGeneratorForm = class(TForm)
    GeneratorChart: TChart;
    ActionList1: TActionList;
    EnregistrerAction: TAction;
    EnregistrerSousAction: TAction;
    BackColorAction: TAction;
    PrintAction: TAction;
    SaveDialog: TSaveDialog;
    ActionList2: TActionList;
    ApplCsteAction: TAction;
    ApplRampeAction: TAction;
    ApplSinusAction: TAction;
    ApplNivAction: TAction;
    ApplSigCarAction: TAction;
    ApplSigCarModAction: TAction;
    ColorDialog1: TColorDialog;
    Panel4: TPanel;
    Label1: TLabel;
    DureeStimEdit: TEdit;
    OptionsGraphesGroupBox: TGroupBox;
    EpaisseurLabel: TLabel;
    AfficherPtsGroupBox: TGroupBox;
    TypePtLabel: TLabel;
    TailleLabel: TLabel;
    TypePtComboBox: TComboBox;
    AfficherValeurPtCheckBox: TCheckBox;
    TailleSpinEdit: TSpinEdit;
    AfficherGrilleCheckBox: TCheckBox;
    AfficherAxeOrdonneeCheckBox: TCheckBox;
    AfficherAxeAbscisseCheckBox: TCheckBox;
    CourbeEscalierCheckBox: TCheckBox;
    AfficherPtsCheckBox: TCheckBox;
    EpaisseurSpinEdit: TSpinEdit;
    Series1: TLineSeries;
    OuvrirAction: TAction;
    ImportTextAction: TAction;
    OpenDialog1: TOpenDialog;
    StatusBar1: TStatusBar;
    ImportBinAction: TAction;
    QuitterAction: TAction;
    ActionManager1: TActionManager;
    ActionMainMenuBar1: TActionMainMenuBar;
    ActionToolBar1: TActionToolBar;
    procedure FormShow(Sender: TObject);
    procedure AfficherAxeAbscisseCheckBoxClick(Sender: TObject);
    procedure AfficherGrilleCheckBoxClick(Sender: TObject);
    procedure AfficherAxeOrdonneeCheckBoxClick(Sender: TObject);
    procedure CourbeEscalierCheckBoxClick(Sender: TObject);
    procedure EpaisseurSpinEditChange(Sender: TObject);
    procedure AfficherPtsCheckBoxClick(Sender: TObject);
    procedure TypePtComboBoxChange(Sender: TObject);
    procedure TailleSpinEditChange(Sender: TObject);
    procedure AfficherValeurPtCheckBoxClick(Sender: TObject);
    procedure DureeStimEditKeyPress(Sender: TObject; var Key: Char);
    procedure ApplCsteActionExecute(Sender: TObject);
    procedure ApplRampeActionExecute(Sender: TObject);
    procedure ApplSinusActionExecute(Sender: TObject);
    procedure ApplNivActionExecute(Sender: TObject);
    procedure ApplSigCarActionExecute(Sender: TObject);
    procedure ApplSigCarModActionExecute(Sender: TObject);
    procedure EnregistrerActionExecute(Sender: TObject);
    procedure EnregistrerSousActionExecute(Sender: TObject);
    procedure BackColorActionExecute(Sender: TObject);
    procedure PrintActionExecute(Sender: TObject);
    procedure OuvrirActionExecute(Sender: TObject);
    procedure ImportTextActionExecute(Sender: TObject);
    procedure ImportBinActionExecute(Sender: TObject);
    procedure QuitterActionExecute(Sender: TObject);
    procedure GeneratorChartMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure GeneratorChartMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure GeneratorChartMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Déclarations privées }
    OLDX,OLDY : integer;
    Zooming : boolean;
    TheRectZoom : TRect;
  public
    { Déclarations publiques }
    ChannelName : string;
    Generator : TGenerator;
    procedure MAJIHM;
  end;

var
  GeneratorForm: TGeneratorForm;

implementation

uses UStimuliAppl, UDocometre;

{$R *.dfm}

procedure TGeneratorForm.MAJIHM;
var i : integer;
begin
  Series1.Clear;
  for i := 0 to Generator.Length - 1
  do Series1.AddXY(i/Generator.Fe,Generator.Values[i]);
  StatusBar1.Panels[1].Text := Generator.FileName;
end;

procedure TGeneratorForm.FormShow(Sender: TObject);
begin
  Caption := 'Simulation - Génération du signal "' + ChannelName +'"';

  DureeStimEdit.Text := floattostr(1000*(Generator.Length - 1)/Generator.Fe);
  StimuliApplForm.Duree_Stim := 1000*(Generator.Length - 1)/Generator.Fe;
  StimuliApplForm.CurentGenerator := Generator;

  MAJIHM;

  ApplCsteAction.Enabled := (Generator.FloatOrInt = 'float');
  ApplRampeAction.Enabled := (Generator.FloatOrInt = 'float');
  ApplSinusAction.Enabled := (Generator.FloatOrInt = 'float');
  ApplNivAction.Enabled := (Generator.FloatOrInt = 'int');
  ApplSigCarAction.Enabled := (Generator.FloatOrInt = 'int');
  ApplSigCarModAction.Enabled := (Generator.FloatOrInt = 'int');
  StimuliApplForm.CsteTabSheet.TabVisible := (Generator.FloatOrInt = 'float');
  StimuliApplForm.RampeTabSheet.TabVisible := (Generator.FloatOrInt = 'float');
  StimuliApplForm.SinusTabSheet.TabVisible := (Generator.FloatOrInt = 'float');
  StimuliApplForm.NivTabSheet.TabVisible := (Generator.FloatOrInt = 'int');
  StimuliApplForm.SigCarTabSheet.TabVisible := (Generator.FloatOrInt = 'int');
  StimuliApplForm.SigCarModTabSheet.TabVisible := (Generator.FloatOrInt = 'int');

end;

procedure TGeneratorForm.AfficherAxeAbscisseCheckBoxClick(Sender: TObject);
begin
  GeneratorChart.BottomAxis.Visible := AfficherAxeAbscisseCheckBox.Checked;
end;

procedure TGeneratorForm.AfficherGrilleCheckBoxClick(Sender: TObject);
begin
  GeneratorChart.BottomAxis.Grid.Visible := AfficherGrilleCheckBox.Checked;
  GeneratorChart.LeftAxis.Grid.Visible := AfficherGrilleCheckBox.Checked;
end;

procedure TGeneratorForm.AfficherAxeOrdonneeCheckBoxClick(Sender: TObject);
begin
  GeneratorChart.LeftAxis.Visible := AfficherAxeOrdonneeCheckBox.Checked;
end;

procedure TGeneratorForm.CourbeEscalierCheckBoxClick(Sender: TObject);
begin
  Series1.Stairs := CourbeEscalierCheckBox.Checked;
end;

procedure TGeneratorForm.EpaisseurSpinEditChange(Sender: TObject);
begin
  Series1.LinePen.Width := EpaisseurSpinEdit.Value;
end;

procedure TGeneratorForm.AfficherPtsCheckBoxClick(Sender: TObject);
begin
  AfficherPtsGroupBox.Enabled := AfficherPtsCheckBox.Checked;
  TypePtCombobox.Enabled := AfficherPtsCheckBox.Checked;
  TailleSpinEdit.Enabled := AfficherPtsCheckBox.Checked;
  AfficherValeurPtCheckBox.Enabled := AfficherPtsCheckBox.Checked;
  Series1.Pointer.Visible := AfficherPtsCheckBox.Checked;
  Series1.Marks.Visible := AfficherPtsCheckBox.Checked;
  if AfficherPtsCheckBox.Checked then
  begin
    TypePtComboBoxChange(self);
    TailleSpinEditChange(self);
    AfficherValeurPtCheckBoxClick(self);
  end;
end;

procedure TGeneratorForm.TypePtComboBoxChange(Sender: TObject);
begin
  Series1.Pointer.Style := TSeriesPointerStyle(TypePtComboBox.ItemIndex);
end;

procedure TGeneratorForm.TailleSpinEditChange(Sender: TObject);
begin
  Series1.Pointer.HorizSize := TailleSpinEdit.Value;
  Series1.Pointer.VertSize := TailleSpinEdit.Value;
end;

procedure TGeneratorForm.AfficherValeurPtCheckBoxClick(Sender: TObject);
begin
  Series1.Marks.Visible := AfficherValeurPtCheckBox.Checked;
end;

procedure TGeneratorForm.DureeStimEditKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not(key in ['0'..'9','.',Char(VK_BACK),Char(VK_RETURN)]) then key := #0;
  if key = Char(VK_RETURN) then
  begin
    Generator.SetDuree(strtofloat(DureeStimEdit.Text));
    StimuliApplForm.Duree_Stim := strtofloat(DureeStimEdit.Text);
    MAJIHM;
  end;
end;

procedure TGeneratorForm.ApplCsteActionExecute(Sender: TObject);
begin
  StimuliApplForm.CalledFromGenerator := true;
  StimuliApplForm.PageControl1.ActivePage := StimuliApplForm.CsteTabSheet;
  StimuliApplForm.Show;
end;

procedure TGeneratorForm.ApplRampeActionExecute(Sender: TObject);
begin
  StimuliApplForm.CalledFromGenerator := true;
  StimuliApplForm.PageControl1.ActivePage := StimuliApplForm.RampeTabSheet;
  StimuliApplForm.Show;
end;

procedure TGeneratorForm.ApplSinusActionExecute(Sender: TObject);
begin
  StimuliApplForm.CalledFromGenerator := true;
  StimuliApplForm.PageControl1.ActivePage := StimuliApplForm.SinusTabSheet;
  StimuliApplForm.Show;
end;

procedure TGeneratorForm.ApplNivActionExecute(Sender: TObject);
begin
  StimuliApplForm.CalledFromGenerator := true;
  StimuliApplForm.PageControl1.ActivePage := StimuliApplForm.NivTabSheet;
  StimuliApplForm.Show;
end;

procedure TGeneratorForm.ApplSigCarActionExecute(Sender: TObject);
begin
  StimuliApplForm.CalledFromGenerator := true;
  StimuliApplForm.PageControl1.ActivePage := StimuliApplForm.SigCarTabSheet;
  StimuliApplForm.Show;
end;

procedure TGeneratorForm.ApplSigCarModActionExecute(Sender: TObject);
begin
  StimuliApplForm.CalledFromGenerator := true;
  StimuliApplForm.PageControl1.ActivePage := StimuliApplForm.SigCarModTabSheet;
  StimuliApplForm.Show;
end;


procedure TGeneratorForm.EnregistrerActionExecute(Sender: TObject);
var SaveCursor : TCursor;
begin
  if Generator.FileName <> '' then
  begin
    SaveCursor := Screen.Cursor;
    Screen.Cursor := crHourGlass;
    Generator.SaveToFile;
    Screen.Cursor := SaveCursor;
  end else EnregistrerSousActionExecute(Self);
end;

procedure TGeneratorForm.EnregistrerSousActionExecute(Sender: TObject);
begin
  if SaveDialog.Execute then
  begin
    Generator.FileName := SaveDialog.FileName;
    EnregistrerActionExecute(self);
    MAJIHM;
  end;
end;

procedure TGeneratorForm.BackColorActionExecute(Sender: TObject);
begin
  if ColorDialog1.Execute then
  GeneratorChart.BackColor := ColorDialog1.Color;
end;

procedure TGeneratorForm.PrintActionExecute(Sender: TObject);
begin
  ChartPreview(GeneratorForm,GeneratorChart);
end;

procedure TGeneratorForm.OuvrirActionExecute(Sender: TObject);
begin
  OpenDialog1.DefaultExt := 'sim';
  OpenDialog1.Title := 'Ouvrir';
  OpenDialog1.Filter := 'Fichier simulation|*.sim';
  if OpenDialog1.Execute then
  begin
    Generator.ReadFromFile(OpenDialog1.FileName);
    DureeStimEdit.Text := floattostr(1000*(Generator.Length - 1)/Generator.Fe);
    StimuliApplForm.Duree_Stim := 1000*(Generator.Length - 1)/Generator.Fe;
    MAJIHM;
  end;
end;

procedure TGeneratorForm.ImportTextActionExecute(Sender: TObject);
begin
  OpenDialog1.DefaultExt := 'txt';
  OpenDialog1.Title := 'Importation à partir d''un fichier Texte';
  OpenDialog1.Filter := 'Fichier Texte|*.txt';
  if OpenDialog1.Execute then
  begin
    StimuliApplForm.CalledFromGenerator := true;
    StimuliApplForm.ImportFromTextFile(OpenDialog1.FileName);
    DureeStimEdit.Text := floattostr(1000*(Generator.Length - 1)/Generator.Fe);
    StimuliApplForm.Duree_Stim := 1000*(Generator.Length - 1)/Generator.Fe;
  end;
end;

procedure TGeneratorForm.ImportBinActionExecute(Sender: TObject);
begin
  OpenDialog1.DefaultExt := 'sau';
  OpenDialog1.Title := 'Importation à partir d''un fichier binaire (.sau)';
  OpenDialog1.Filter := 'Fichier Binaire|*.sau';
  if OpenDialog1.Execute then
  begin
    StimuliApplForm.CalledFromGenerator := true;
    StimuliApplForm.ImportFromBinFile(Opendialog1.FileName);
    DureeStimEdit.Text := floattostr(1000*(Generator.Length - 1)/Generator.Fe);
    StimuliApplForm.Duree_Stim := 1000*(Generator.Length - 1)/Generator.Fe;
  end;
end;

procedure TGeneratorForm.QuitterActionExecute(Sender: TObject);
begin
  close;
end;

procedure TGeneratorForm.GeneratorChartMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    TheRectZoom.Left := X;
    TheRectZoom.Top := Y;
    OLDX := X; OLDY := Y;
    zooming := true;
  end;
end;

procedure TGeneratorForm.GeneratorChartMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  if Zooming then
  begin
    with GeneratorChart,Canvas do
    begin
      Pen.Color := clInfoBk;
      Pen.Style := psSolid;
      Pen.Mode := pmXor;
      Pen.Width := 1;
      MoveTo(TheRectZoom.Left,TheRectZoom.Top);
      LineTo(TheRectZoom.Left,OLDY);
      MoveTo(TheRectZoom.Left,TheRectZoom.Top);
      LineTo(OLDX,TheRectZoom.Top);
      MoveTo(OLDX,OLDY);
      LineTo(OLDX,TheRectZoom.Top);
      MoveTo(OLDX,OLDY);
      LineTo(TheRectZoom.Left,OLDY);

      MoveTo(TheRectZoom.Left,TheRectZoom.Top);
      LineTo(TheRectZoom.Left,Y);
      MoveTo(TheRectZoom.Left,TheRectZoom.Top);
      LineTo(X,TheRectZoom.Top);
      MoveTo(X,Y);
      LineTo(X,TheRectZoom.Top);
      MoveTo(X,Y);
      LineTo(TheRectZoom.Left,Y);
      OLDX := X; OLDY := Y;
    end;
  end;
end;

procedure TGeneratorForm.GeneratorChartMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    TheRectZoom.Right := X;
    TheRectZoom.Bottom := Y;
    zooming := false;
  end;
end;

end.
