{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 UStimuli;

interface

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

type
  TStimuliForm = class(TForm)
    ActionList1: TActionList;
    EnregistrerAction: TAction;
    EnregistrerSousAction: TAction;
    QuitterAction: TAction;
    StimuliChart: TChart;
    SaveDialog: TSaveDialog;
    Panel1: TPanel;
    Panel2: TPanel;
    CanauxComboBox: TComboBox;
    AjouterCanalButton: TButton;
    Panel3: TPanel;
    SupprimerCanalButton: TButton;
    CanauxListBox: TListBox;
    Splitter1: TSplitter;
    Panel4: TPanel;
    DureeStimEdit: TEdit;
    ActionList2: TActionList;
    ApplCsteAction: TAction;
    ApplRampeAction: TAction;
    ApplSinusAction: TAction;
    ApplNivAction: TAction;
    ApplSigCarAction: TAction;
    ApplSigCarModAction: TAction;
    Label1: TLabel;
    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;
    BackColorAction: TAction;
    PrintAction: TAction;
    ColorDialog1: TColorDialog;
    ImportTextAction: TAction;
    ImportBinAction: TAction;
    OpenDialog1: TOpenDialog;
    ActionManager1: TActionManager;
    ActionMainMenuBar1: TActionMainMenuBar;
    ActionToolBar1: TActionToolBar;
    BitBtn1: TBitBtn;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure EnregistrerActionExecute(Sender: TObject);
    procedure EnregistrerSousActionExecute(Sender: TObject);
    procedure QuitterActionExecute(Sender: TObject);
    procedure DureeStimEditKeyPress(Sender: TObject; var Key: Char);
    procedure AjouterCanalButtonClick(Sender: TObject);
    procedure SupprimerCanalButtonClick(Sender: TObject);
    procedure CanauxListBoxClick(Sender: TObject);
    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 CanauxListBoxDblClick(Sender: TObject);
    procedure AfficherAxeAbscisseCheckBoxClick(Sender: TObject);
    procedure AfficherAxeOrdonneeCheckBoxClick(Sender: TObject);
    procedure AfficherGrilleCheckBoxClick(Sender: TObject);
    procedure CourbeEscalierCheckBoxClick(Sender: TObject);
    procedure AfficherPtsCheckBoxClick(Sender: TObject);
    procedure TypePtComboBoxChange(Sender: TObject);
    procedure TailleSpinEditChange(Sender: TObject);
    procedure AfficherValeurPtCheckBoxClick(Sender: TObject);
    procedure EpaisseurSpinEditChange(Sender: TObject);
    procedure BackColorActionExecute(Sender: TObject);
    procedure PrintActionExecute(Sender: TObject);
    procedure ImportTextActionExecute(Sender: TObject);
    procedure ImportBinActionExecute(Sender: TObject);
    procedure Splitter1Moved(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Déclarations privées }
    Stimuli : TStimuli;
    procedure AllowNothing;
    procedure AllowEveryThing;
    procedure AllowAnalog;
    procedure AllowNum;
  public
    { Déclarations publiques }
    XMLFunction : IXMLFONCTIONType;
    procedure MAJIHM;
  end;

var
  StimuliForm: TStimuliForm;

implementation

uses UDocometre, UStimuliAppl;

{$R *.dfm}

procedure TStimuliForm.FormShow(Sender: TObject);
var rep : integer;
    S : string;
begin
  //Recup liste des canaux

  if (FileExists(XMLFunction.FICHIER) or (XMLFunction.FICHIER='')) then
  begin
    Stimuli := TStimuli.Create(XMLFunction.FICHIER,StimuliChart);
    MAJIHM;
  end else
  begin
    S := 'Le fichier stimuli suivant n''existe pas : ' + XMLFunction.FICHIER + ' !';
    S := S + 'Souhaitez-vous redéfinir le chemin d''accès vers ce fichier ?';
    rep := MessageDlg(S,mtInformation,[mbYes,mbNo],0);
    if rep = mrYes then
    begin
      OpenDialog1.DefaultExt := 'dat';
      OpenDialog1.Title := 'Recherche d''un fichier Stimuli';
      OpenDialog1.Filter := 'Fichier Stimuli|*.dat';
      if OpenDialog1.Execute then
      begin
        XMLFunction.FICHIER := OpenDialog1.FileName;
        Stimuli := TStimuli.Create(XMLFunction.FICHIER,StimuliChart);
        Stimuli.Modified := true;
        MAJIHM;
      end;
    end else
    begin
      XMLFunction.FICHIER := '';
      Stimuli := TStimuli.Create(XMLFunction.FICHIER,StimuliChart);
      MAJIHM;
    end;
  end;
end;

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

procedure TStimuliForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if Stimuli <> nil then
  if Stimuli.GetModified
  then EnregistrerActionExecute(self);
  Stimuli.Free;
  CanauxListBox.Clear;
  CanauxCombobox.Clear;
end;

procedure TStimuliForm.EnregistrerActionExecute(Sender: TObject);
var SaveCursor : TCursor;
begin
  if XMLFunction.FICHIER <> '' then
  begin
    SaveCursor := Screen.Cursor;
    Screen.Cursor := crHourGlass;
    Stimuli.SaveToFile( XMLFunction.FICHIER);
    Screen.Cursor := SaveCursor;
    Caption := 'Edition des Stimuli - ' + XMLFunction.FICHIER;
  end else EnregistrerSousActionExecute(Self);
end;

procedure TStimuliForm.EnregistrerSousActionExecute(Sender: TObject);
var i : integer;
		isUsedByAnOtherST : boolean;
begin
	if SaveDialog.Execute then
	begin
		isUsedByAnOtherST := false;
		for i := 0 to DocDOCOMETRE.ST.Count - 1 do
		begin
			if i <> DocoForm.DescriptFrame.STComboBox.ItemIndex
			then isUsedByAnOtherST := isUsedByAnOtherST or DocDOCOMETRE.ST[i].UseStimFileWithName(XMLFunction.FICHIER);
		end;
		if not isUsedByAnOtherST then deleteFile(XMLFunction.FICHIER);
		XMLFunction.FICHIER := SaveDialog.FileName;
		EnregistrerActionExecute(self);
  end;
end;

procedure TStimuliForm.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
    Stimuli.SetDuree(strtofloat(DureeStimEdit.Text));
    XMLFunction.DUREE := strtofloat(DureeStimEdit.Text);
    StimuliApplForm.Duree_Stim := strtofloat(DureeStimEdit.Text);
  end;
end;

procedure TStimuliForm.MAJIHM;
var InfoCanaux : TInfoCanaux;
    i : integer;
    AStimulus : TStimulus;
begin
  //Recup liste des canaux
  InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;

  //maj canaux CheckListbox et combobox
  for i := 0 to InfoCanaux.Nom.Count - 1 do
  begin
    if ((InfoCanaux.inORoutORcanint[i] = 'out') or
        ((InfoCanaux.inORoutORcanint[i] = 'canint') and
         (InfoCanaux.purORparam[i] = 'PUR') and
         (InfoCanaux.ISArray[i] = '0'))) then
    begin
      AStimulus := Stimuli.Has(InfoCanaux.Nom[i]);
      if AStimulus <> nil then
      begin
        CanauxListBox.AddItem(InfoCanaux.Nom[i],AStimulus);
        if InfoCanaux.floatORint[i] = 'int'
        then AStimulus.anORnumORcanint := 'num'
        else if InfoCanaux.inORoutORcanint[i] = 'out'
             then AStimulus.anORnumORcanint := 'ana'
             else AStimulus.anORnumORcanint := 'canint';
      end else CanauxCombobox.Items.Add(InfoCanaux.Nom[i]);
    end;

  end;
  InfoCanaux.free;

  //maj duree
  DureeStimEdit.Text := floattostr(Stimuli.Duree);
  StimuliApplForm.Duree_Stim := Stimuli.Duree;

  if XMLFunction.FICHIER <> ''
  then Caption := 'Edition des Stimuli - ' + XMLFunction.FICHIER
  else Caption := 'Edition des Stimuli';

  AllowNothing;
end;

procedure TStimuliForm.AjouterCanalButtonClick(Sender: TObject);
var ASortie : IXMLSORTIEType;
    InfoCanaux : TInfoCanaux;
    AStimulus : TStimulus;
    RF : integer;
    Fe : double;
begin
  //Recup liste des canaux
  InfoCanaux := DocDOCOMETRE.RendreListeInfoCanaux;

  if CanauxCombobox.ItemIndex <> -1 then
  begin
    ASortie := XMLFunction.SORTIE.Add;
    ASortie.NOM := CanauxCombobox.Text;

    RF := strtoint(InfoCanaux.RappFreq[InfoCanaux.Nom.IndexOf(ASortie.NOM)]);
    Fe := DocDOCOMETRE.EXPERIENCE.FEG / RF;
    AStimulus := Stimuli.AddStimulus(ASortie.NOM,Fe);
    CanauxListBox.Items.AddObject(AStimulus.Title,AStimulus);

    if InfoCanaux.floatORint[InfoCanaux.Nom.IndexOf(ASortie.NOM)] = 'int'
        then AStimulus.anORnumORcanint := 'num'
        else if InfoCanaux.inORoutORcanint[InfoCanaux.Nom.IndexOf(ASortie.NOM)] = 'out'
             then AStimulus.anORnumORcanint := 'ana'
             else AStimulus.anORnumORcanint := 'canint';

    AStimulus.Active := false;

    CanauxCombobox.DeleteSelected;

    CanauxCombobox.ItemIndex := -1;

    CanauxCombobox.Repaint;
  end;

  //Recup liste des canaux
  InfoCanaux.Free;

end;

procedure TStimuliForm.SupprimerCanalButtonClick(Sender: TObject);
var i : integer;
begin
  if CanauxListBox.ItemIndex <> -1 then
  begin
    i := 0;
    repeat
      if XMLFunction.SORTIE[i].NOM = CanauxListBox.Items[CanauxListBox.ItemIndex] then
      begin
        XMLFunction.SORTIE.Delete(i);
        break;
      end;
      inc(i);
    until i = XMLFunction.SORTIE.Count;
    Stimuli.DeleteStimulus(CanauxListBox.Items.Objects[CanauxListBox.ItemIndex] as TStimulus);
    CanauxCombobox.Items.Add(CanauxListBox.Items[CanauxListBox.ItemIndex]);
		CanauxListBox.Items.Delete(CanauxListBox.ItemIndex);
		StimuliApplForm.CurentStimulus := nil;
  end;
end;

procedure TStimuliForm.CanauxListBoxClick(Sender: TObject);
var i : integer;
begin
  for i := 0 to CanauxListBox.Count - 1
  do (CanauxListBox.Items.Objects[i] as TStimulus).Active := CanauxListBox.Selected[i];
  AllowNothing;
  if CanauxListBox.SelCount = 1 then
  begin
    ImportTextAction.Enabled := true;
    ImportBinAction.Enabled := true;
    StimuliApplForm.CurentStimulus := CanauxListBox.Items.Objects[CanauxListBox.ItemIndex] as TStimulus;
    if (CanauxListBox.Items.Objects[CanauxListBox.ItemIndex] as TStimulus).anORnumORcanint = 'num'
    then AllowNum;
    if (CanauxListBox.Items.Objects[CanauxListBox.ItemIndex] as TStimulus).anORnumORcanint = 'ana'
    then AllowAnalog;
    if (CanauxListBox.Items.Objects[CanauxListBox.ItemIndex] as TStimulus).anORnumORcanint = 'canint'
    then AllowEveryThing;
  end else StimuliApplForm.CurentStimulus := nil;
end;

procedure TStimuliForm.AllowNothing;
begin
  ApplCsteAction.Enabled := false;
  ApplRampeAction.Enabled := false;
  ApplSinusAction.Enabled := false;
  ApplNivAction.Enabled := false;
  ApplSigCarAction.Enabled := false;
  ApplSigCarModAction.Enabled := false;
  ImportTextAction.Enabled := false;
  ImportBinAction.Enabled := false;
  StimuliApplForm.CsteTabSheet.TabVisible := false;
  StimuliApplForm.RampeTabSheet.TabVisible := false;
  StimuliApplForm.SinusTabSheet.TabVisible := false;
  StimuliApplForm.NivTabSheet.TabVisible := false;
  StimuliApplForm.SigCarTabSheet.TabVisible := false;
  StimuliApplForm.SigCarModTabSheet.TabVisible := false;
end;

procedure TStimuliForm.AllowEveryThing;
begin
  AllowAnalog;
  AllowNum;
end;

procedure TStimuliForm.AllowAnalog;
begin
  ApplCsteAction.Enabled := true;
  ApplRampeAction.Enabled := true;
  ApplSinusAction.Enabled := true;
  StimuliApplForm.CsteTabSheet.TabVisible := true;
  StimuliApplForm.RampeTabSheet.TabVisible := true;
  StimuliApplForm.SinusTabSheet.TabVisible := true;
end;

procedure TStimuliForm.AllowNum;
begin
  ApplNivAction.Enabled := true;
  ApplSigCarAction.Enabled := true;
  ApplSigCarModAction.Enabled := true;
  StimuliApplForm.NivTabSheet.TabVisible := true;
  StimuliApplForm.SigCarTabSheet.TabVisible := true;
  StimuliApplForm.SigCarModTabSheet.TabVisible := true;
end;

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

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

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

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

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

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

procedure TStimuliForm.CanauxListBoxDblClick(Sender: TObject);
begin
  StimuliApplForm.Show;
end;

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

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

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

procedure TStimuliForm.CourbeEscalierCheckBoxClick(Sender: TObject);
var i : integer;
    ACurve : TLineSeries;
begin
  for i:= 0 to StimuliChart.SeriesCount - 1 do
  begin
    ACurve := StimuliChart.SeriesList[i] as TLineSeries;
    ACurve.Stairs := CourbeEscalierCheckBox.Checked;
  end;
end;

procedure TStimuliForm.AfficherPtsCheckBoxClick(Sender: TObject);
var i : integer;
    ACurve : TLineSeries;
begin
  AfficherPtsGroupBox.Enabled := AfficherPtsCheckBox.Checked;
  TypePtCombobox.Enabled := AfficherPtsCheckBox.Checked;
  TailleSpinEdit.Enabled := AfficherPtsCheckBox.Checked;
  AfficherValeurPtCheckBox.Enabled := AfficherPtsCheckBox.Checked;
  for i := 0 to StimuliChart.SeriesCount - 1 do
  begin
    ACurve := StimuliChart.SeriesList[i] as TLineSeries;
    ACurve.Pointer.Visible := AfficherPtsCheckBox.Checked;
    ACurve.Marks.Visible := AfficherPtsCheckBox.Checked;
  end;
  if AfficherPtsCheckBox.Checked then
  begin
    TypePtComboBoxChange(self);
    TailleSpinEditChange(self);
    AfficherValeurPtCheckBoxClick(self);
  end;
end;

procedure TStimuliForm.TypePtComboBoxChange(Sender: TObject);
var i : integer;
    ACurve : TLineSeries;
begin
  for i := 0 to StimuliChart.SeriesCount - 1 do
  begin
    ACurve := StimuliChart.SeriesList[i] as TLineSeries;
    ACurve.Pointer.Style := TSeriesPointerStyle(TypePtComboBox.ItemIndex);
  end;  
end;

procedure TStimuliForm.AfficherValeurPtCheckBoxClick(Sender: TObject);
var i : integer;
    ACurve : TLineSeries;
begin
  for i := 0 to StimuliChart.SeriesCount - 1 do
  begin
    ACurve := StimuliChart.SeriesList[i] as TLineSeries;
    ACurve.Marks.Visible := AfficherValeurPtCheckBox.Checked;
  end;
end;

procedure TStimuliForm.TailleSpinEditChange(Sender: TObject);
var i : integer;
    ACurve : TLineSeries;
begin
  for i := 0 to StimuliChart.SeriesCount - 1 do
  begin
    ACurve := StimuliChart.SeriesList[i] as TLineSeries;
    ACurve.Pointer.HorizSize := TailleSpinEdit.Value;
    ACurve.Pointer.VertSize := TailleSpinEdit.Value;
  end;
end;

procedure TStimuliForm.EpaisseurSpinEditChange(Sender: TObject);
var i : integer;
    ACurve : TLineSeries;
begin
  for i := 0 to StimuliChart.SeriesCount - 1 do
  begin
    ACurve := StimuliChart.SeriesList[i] as TLineSeries;
    ACurve.LinePen.Width := EpaisseurSpinEdit.Value;
  end;
end;

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

procedure TStimuliForm.PrintActionExecute(Sender: TObject);
begin
  ChartPreview(StimuliForm,StimuliChart);
end;

procedure TStimuliForm.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.ImportFromTextFile(OpenDialog1.FileName);
    Stimuli.Modified := true;
  end;
end;

procedure TStimuliForm.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.ImportFromBinFile(Opendialog1.FileName);
    Stimuli.Modified := true;
  end;
end;

procedure TStimuliForm.Splitter1Moved(Sender: TObject);
begin
  OptionsGraphesGroupBox.Left := Panel4.Width div 2 - OptionsGraphesGroupBox.Width div 2;
  Panel1.Repaint;
end;

procedure TStimuliForm.BitBtn1Click(Sender: TObject);
var key : char;
begin
	//key := Char(VK_RETURN);
	//DureeStimEditKeyPress(Self,key);

  Stimuli.SetDuree(strtofloat(DureeStimEdit.Text));
  XMLFunction.DUREE := strtofloat(DureeStimEdit.Text);
  StimuliApplForm.Duree_Stim := strtofloat(DureeStimEdit.Text);

end;

end.
