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

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, ToolWin, TeeProcs, TeEngine, Chart, Series,
  StdCtrls, ActnList, Menus;
type
  TGrapheSimulFrame = class(TFrame)
    NomCanalChartPanel: TPanel;
    Splitter1: TSplitter;
    CanalChart: TChart;
    ChartColorDialog: TColorDialog;
    Series1: TLineSeries;
    OrdoLabel: TLabel;
    AbscLabel: TLabel;
    ActionList1: TActionList;
    ColorAction: TAction;
    ChartPanel: TPanel;
    Button1: TButton;
    Button2: TButton;
    saveAction: TAction;
    SaveDialog1: TSaveDialog;
    procedure Splitter1Moved(Sender: TObject);
    procedure CanalChartMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure CanalChartMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CanalChartMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CanalChartUndoZoom(Sender: TObject);
    procedure CanalChartScroll(Sender: TObject);
    procedure CanalChartZoom(Sender: TObject);
    procedure ColorActionExecute(Sender: TObject);
    procedure saveActionExecute(Sender: TObject);
  private
    { Déclarations privées }
    Zooming : boolean;
    DrawRectZooming : boolean;
    UndoZooming : boolean;
    Scrolling : boolean;
    TheRectZoom : TRect;
    BoxSize : integer;
  public
    { Déclarations publiques }
    Fe : double;
    Data_Index : integer;
    inORoutORcanint : string;
    NextIndex : integer;
    BufferIndex : integer;

    IncrementIndex : integer;
    DataBuffer : array of array of double;
    BufferLength : integer;
    VisuLength : integer;
    OLDX,OLDY : integer;
    OLDXPos,OLDYPos,XPos,YPos : integer;
    ListeGrapheSimulFrame : TList;
    procedure SuppRefGrapheSimulFrame(AGrapheSimulFrame:TGrapheSimulFrame);
    constructor Create(AOwner:TComponent);override;
    procedure SetVisuBufferLength(pVisuWidth,pMAJAfter:double);
    function NeedNewValue(pIndex:integer):boolean;
    function BufferIsFull:boolean;
    procedure AddNewValue(X,Y:double);
    procedure Reinit;
    procedure FlushGraph;
    procedure zoomRect(Rect : TRect);
  protected
    procedure WndProc(var Message : TMessage);override;
  end;

implementation

uses UDocometre, UGenerator;

{$R *.dfm}

constructor TGrapheSimulFrame.Create(AOwner:TComponent);
var TheColor : TColor;
begin
  inherited Create(AOwner);
  OldX := -1;
  OldY := -1;
  OldXPos := -1;
  OldYPos := -1;
  ListeGrapheSimulFrame := TList.Create;
  NextIndex := 1;
  TheColor := clBlack;
  case random(16) of
    0:TheColor :=clAqua;
    1:TheColor :=clBlue;
    2:TheColor := clCream;
    3:TheColor := clFuchsia;
    4:TheColor := clGreen   ;
    5:TheColor := clLime  ;
    6:TheColor := clMaroon ;
    7:TheColor := clMoneyGreen;
    8:TheColor := clNavy   ;
    9:TheColor := clOlive  ;
    10:TheColor := clPurple ;
    11:TheColor := clRed    ;
    12:TheColor := clSkyBlue;
    13:TheColor := clTeal  ;
    14:TheColor := clWhite ;
    15:TheColor := clYellow;
  end;
  Series1.SeriesColor := TheColor;
  NomCanalChartPanel.Font.Color := TheColor;
  ChartPanel.Color := TheColor;
  BoxSize := 5;
end;

procedure TGrapheSimulFrame.zoomRect(Rect : TRect);
var theRect : TRect;
begin
  theRect := Self.CanalChart.ChartRect;
  theRect.Left := Rect.Left;
  theRect.Right := Rect.Right;
  Self.CanalChart.ZoomRect(theRect);
end;

procedure TGrapheSimulFrame.SuppRefGrapheSimulFrame(AGrapheSimulFrame:TGrapheSimulFrame);
var i : integer;
begin
  for i := 0 to ListeGrapheSimulFrame.Count - 1 do
  begin
    if ListeGrapheSimulFrame.Items[i] = AGrapheSimulFrame then
    begin
      ListeGrapheSimulFrame.Delete(i);
      break;
    end;
  end
end;

procedure TGrapheSimulFrame.Splitter1Moved(Sender: TObject);
begin
  DocoForm.SimulFrame.MAJ_SplitterGraphes(NomCanalChartPanel.Width);
end;

procedure TGrapheSimulFrame.WndProc(var Message : TMessage);
var i : integer;
    AGrapheSimulFrame : TGrapheSimulFrame;
begin
  case Message.Msg of
    CM_MOUSEENTER :
    begin
      CanalChart.Refresh;
      OLDX := -1;
      OLDY := -1;
      OLDXPos := -1;
      OLDYPos := -1;
      for i := 0 to ListeGrapheSimulFrame.Count - 1 do
      begin
        AGrapheSimulFrame := ListeGrapheSimulFrame.Items[i];
        AGrapheSimulFrame.CanalChart.refresh;
        AGrapheSimulFrame.OLDX := -1;
        AGrapheSimulFrame.OLDY := -1;
        AGrapheSimulFrame.OLDXPos := -1;
        AGrapheSimulFrame.OLDYPos := -1;
        AGrapheSimulFrame.AbscLabel.Visible := true;
        AGrapheSimulFrame.OrdoLabel.Visible := true;
      end;
      AbscLabel.Visible := true;
      OrdoLabel.Visible := true;
    end;
    CM_MOUSELEAVE :
    begin
      with CanalChart,Canvas do
      begin
        Pen.Color := clRed;
        Pen.Style := psSolid;
        Pen.Mode := pmXor;
        Pen.Width := 1;
        MoveTo(OLDX,ChartRect.Top-Height3D);
        LineTo(OLDX,ChartRect.Bottom-Height3D);
        //MoveTo(ChartRect.Left+Width3D,OLDY);
        //LineTo(ChartRect.Right+Width3D,OLDY);
        Pen.Width := 2;
        if OLDXPos>-1 then
        Rectangle(OLDXPos-BoxSize+1,OLDYPos-BoxSize+1,OLDXPos+BoxSize+1,OLDYPos+BoxSize+1);
        OLDX := -1;
        OLDY := -1;
        OLDXPos := -1;
        OLDYPos := -1;
      end;
      for i := 0 to ListeGrapheSimulFrame.Count - 1 do
      begin
        AGrapheSimulFrame := ListeGrapheSimulFrame.Items[i];
        with AGrapheSimulFrame,CanalChart,Canvas do
        begin
          Pen.Color := clYellow;
          Pen.Style := psSolid;
          Pen.Mode := pmXor;
          Pen.Width := 1;
          MoveTo(OLDX,ChartRect.Top - Height3D);
          LineTo(OLDX,ChartRect.Bottom - Height3D);
          Pen.Width := 2;
          if OLDXPos>-1 then        
          Rectangle(OLDXPos-BoxSize+1,OLDYPos-BoxSize+1,OLDXPos+BoxSize+1,OLDYPos+BoxSize+1);
          OLDX := -1;
          OLDXPos := -1;
          OLDYPos := -1;
          AGrapheSimulFrame.OrdoLabel.Visible := false;
          AGrapheSimulFrame.AbscLabel.Visible := false;
        end;
      end;
      OrdoLabel.Visible := false;
      AbscLabel.Visible := false;
    end;
  end;
  inherited;
end;

procedure TGrapheSimulFrame.CanalChartMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var i,j : integer;
    tmpX,tmpY : double;
    AGrapheSimulFrame : TGrapheSimulFrame;
begin
  if DrawRectZooming then
  begin
    with CanalChart,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);
    end;
  end;
  if not (Zooming or UndoZooming or Scrolling) then
  begin
    if (PtInRect(CanalChart.ChartRect,CanalChart.ScreenToClient(Mouse.CursorPos)) and (Series1.Count>0)) then
    begin
      with CanalChart,Canvas,Series1 do
      begin
        Pen.Color := clRed;
        Pen.Style := psSolid;
        Pen.Mode := pmXor;
        Pen.Width := 1;

        Brush.Color := clBlack;
        Brush.Style := bsSolid;//bsClear;

        Pen.Width := 1;
        MoveTo(OLDX,ChartRect.Top-Height3D);
        LineTo(OLDX,ChartRect.Bottom-Height3D);
        //MoveTo(ChartRect.Left+Width3D,OLDY);
        //LineTo(ChartRect.Right+Width3D,OLDY);
        MoveTo(X,ChartRect.Top-Height3D);
        LineTo(X,ChartRect.Bottom-Height3D);
        //MoveTo(ChartRect.Left+Width3D,Y);
        //LineTo(ChartRect.Right+Width3D,Y);
        OLDX := X;
        OLDY := Y;
        
        GetCursorValues(tmpX,TmpY);
         
        i := 0;
        if Series1.Count >0 then
        begin
          repeat
            if abs(tmpX-Series1.XValues[i]) < 1/Fe then
            begin
              Pen.Width := 2;
              OrdoLabel.Caption := 'A = ' + GetVertAxis.LabelValue(Series1.YValues[i]);
              if(OldXPos>-1) then
              Rectangle(OLDXPos-BoxSize+1,OLDYPos-BoxSize+1,OLDXPos+BoxSize+1,OLDYPos+BoxSize+1);

              YPos := Series1.CalcYPosValue(Series1.YValues[i]);
              XPos := Series1.CalcXPosValue(tmpX);//Series1.CalcXPosValue(Series1.XValues[i]);
              OLDXPos := XPos;
              OLDYPos := YPos;
              Rectangle(XPos-BoxSize+1,YPos-BoxSize+1,Xpos+BoxSize+1,YPos+BoxSize+1);
            end;
            inc(i);
          until (abs(tmpX-Series1.XValues[i-1]) < 1/Fe) or (i = Series1.Count);
        end;
        //OrdoLabel.Caption := 'A = ' + GetVertAxis.LabelValue(Series1.YValues[i]);
        AbscLabel.Caption := 't = ' + GetHorizAxis.LabelValue(tmpX);
        //OrdoLabel.Left := X;
        //OrdoLabel.Top := ChartRect.Top;
        //AbscLabel.Left := ChartRect.Right - AbscLabel.Width;
        //AbscLabel.Top := Y - AbscLabel.Height + 1;
      end;
      for i := 0 to ListeGrapheSimulFrame.Count - 1 do
      begin
        AGrapheSimulFrame := ListeGrapheSimulFrame.Items[i];
        with AGrapheSimulFrame,CanalChart,Canvas do
        begin
          Pen.Color := clYellow;
          Pen.Style := psSolid;
          Pen.Mode := pmXor;
          Pen.Width := 1;
          MoveTo(OLDX,ChartRect.Top - Height3D);
          LineTo(OLDX,ChartRect.Bottom - Height3D);
          X := Series1.CalcXPosValue(tmpX);
          MoveTo(X,ChartRect.Top - Height3D);
          LineTo(X,ChartRect.Bottom - Height3D);

          Brush.Color := clBlack;
          Brush.Style := bsSolid;//bsClear;
          Pen.Width := 2;

          for j := 0 to Series1.Count - 1 do
          begin
            if abs(tmpX-Series1.XValues[j]) < 1/Fe then break;
          end;

          if (j>-1) and (j<Series1.Count) then
          begin
            YPos := Series1.CalcYPosValue(Series1.YValues[j]);
            XPos := Series1.CalcXPosValue(tmpX);//Series1.CalcXPosValue(Series1.XValues[ValueIndex]);
            if(OldXPos>-1) then
            Rectangle(OLDXPos-BoxSize+1,OLDYPos-BoxSize+1,OLDXPos+BoxSize+1,OLDYPos+BoxSize+1);
            OLDXPos := XPos;
            OLDYPos := YPos;
            Rectangle(XPos-BoxSize+1,YPos-BoxSize+1,Xpos+BoxSize+1,YPos+BoxSize+1);

            OrdoLabel.Caption := 'A = ' + Series1.GetVertAxis.LabelValue(Series1.YValues[j]);
            AbscLabel.Caption := 't = ' + Series1.GetHorizAxis.LabelValue(tmpX);

          end;

          OLDX := X;
        end;
      end;
    end;
  end else
  begin
    OLDX := -1;
    OLDY := -1;
    OLDXPos := -1;
    OLDYPos := -1;
    for i := 0 to ListeGrapheSimulFrame.Count - 1 do
    begin
      AGrapheSimulFrame := ListeGrapheSimulFrame.Items[i];
      if Zooming
      //then AGrapheSimulFrame.CanalChart.ZoomRect(TheRectZoom)
      then AGrapheSimulFrame.ZoomRect(TheRectZoom)
      else if UndoZooming
           then AGrapheSimulFrame.CanalChart.UndoZoom
           else begin
             AGrapheSimulFrame.CanalChart.BottomAxis.Minimum :=  CanalChart.BottomAxis.Minimum;
             AGrapheSimulFrame.CanalChart.BottomAxis.Maximum := CanalChart.BottomAxis.Maximum;
             //AGrapheSimulFrame.CanalChart.LeftAxis.Minimum :=  CanalChart.LeftAxis.Minimum;
             //AGrapheSimulFrame.CanalChart.LeftAxis.Maximum := CanalChart.LeftAxis.Maximum;
           end;
      AGrapheSimulFrame.OLDX := -1;
      AGrapheSimulFrame.OLDXPos := -1;
      AGrapheSimulFrame.OLDYPos := -1;
      AGrapheSimulFrame.Zooming := false;
      AGrapheSimulFrame.UndoZooming := false;
      AGrapheSimulFrame.Scrolling := false;
    end;
    UndoZooming := false;
    Scrolling := false;
    Zooming := false;
  end;

end;

procedure TGrapheSimulFrame.SetVisuBufferLength(pVisuWidth,pMAJAfter:double);
begin
  BufferLength := trunc(pMAJAFTER*FE+0.5);
  if BufferLength = 0 then ShowMessage('Attention taille Buffer Graphe nulle');
  VisuLength := trunc(pVisuWidth*FE+0.5);
  if VisuLength = 0 then ShowMessage('Attention taille Buffer Visu nulle');
  SetLength(DataBuffer,2);
  SetLength(DataBuffer[0],BufferLength);
  SetLength(DataBuffer[1],BufferLength);
  BufferIndex := 0;
end;

function TGrapheSimulFrame.NeedNewValue(pIndex:integer):boolean;
begin
  NeedNewValue := false;
  if NextIndex = pIndex then
  begin
    NextIndex := NextIndex + IncrementIndex;
    NeedNewValue := true;
  end;
end;

function TGrapheSimulFrame.BufferIsFull:boolean;
begin
  if BufferIndex = BufferLength
  then BufferIsFull := true
  else BufferIsFull := false;
end;

procedure TGrapheSimulFrame.AddNewValue(X,Y:double);
var i : integer;
begin
  DataBuffer[0][BufferIndex] := X;
  DataBuffer[1][BufferIndex] := Y;
  Inc(BufferIndex);
  if BufferIsFull then
  begin
    for i := 0 to BufferLength - 1 do
    begin
      Series1.AddXY(DataBuffer[0][i],DataBuffer[1][i]);
      if Series1.Count > (VisuLength+1)
      then Series1.Delete(0);
    end;
    BufferIndex := 0;
  end;
end;

procedure TGrapheSimulFrame.FlushGraph;
var i : integer;
begin
  if BufferIndex > 0 then
  begin
    for i := 0 to BufferIndex - 1 do
    begin
      Series1.AddXY(DataBuffer[0][i],DataBuffer[1][i]);
      if Series1.Count > (VisuLength+1)
      then Series1.Delete(0);
    end;
  end;
end;

procedure TGrapheSimulFrame.Reinit;
begin
  series1.Clear;
  NextIndex := 1;
end;

procedure TGrapheSimulFrame.CanalChartMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    TheRectZoom.Left := X;
    TheRectZoom.Top := Y;
    DrawRectZooming := true;
  end;
end;

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

procedure TGrapheSimulFrame.CanalChartUndoZoom(Sender: TObject);
begin
  UndoZooming := true;
end;

procedure TGrapheSimulFrame.CanalChartScroll(Sender: TObject);
begin
  Scrolling := true;
end;

procedure TGrapheSimulFrame.CanalChartZoom(Sender: TObject);
begin
  Zooming := true;
end;

procedure TGrapheSimulFrame.ColorActionExecute(Sender: TObject);
begin
  if ChartColorDialog.Execute then
  begin
    NomCanalChartPanel.Font.Color := ChartColorDialog.Color;
    Series1.SeriesColor := ChartColorDialog.Color;
    ChartPanel.Color := ChartColorDialog.Color;
  end;
end;

procedure TGrapheSimulFrame.saveActionExecute(Sender: TObject);
var i : integer;
    buffer : TMemoryStream;
    temp : single;
    fileStream : TFileStream;
begin
  if SaveDialog1.Execute then
  begin
    if series1.count > 0 then
    begin
      buffer := TMemoryStream.Create;
      for i := 0 to series1.Count - 1 do
      begin
        temp := series1.YValue[i];
        buffer.Write(temp,4);
      end;
      fileStream := TFileStream.Create(SaveDialog1.FileName,fmCreate);
      buffer.SaveToStream(fileStream);
      fileStream.Free;
      buffer.Clear;
      buffer.Free;
    end else showmessage('Aucun point dans le canal...');
  end;
end;

end.
