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

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, ExtCtrls, ComCtrls, StdCtrls, Grids, ActnList, Menus, IniFiles,
  ValEdit, ShellApi;

type
  TOrgaFrame = class(TFrame)
    Shape2: TShape;
    Panel1: TPanel;
    Shape1: TShape;
    SessionPopupMenu: TPopupMenu;
    EssaisPopupMenu: TPopupMenu;
    SessionsEssaisActionList: TActionList;
    ReinitSessionsAction: TAction;
    CopySessionAction: TAction;
    CollerSessionAction: TAction;
    CopierlaSession1: TMenuItem;
    CollerlaSession1: TMenuItem;
    N1: TMenuItem;
    RinitialiserlalesSessions1: TMenuItem;
    ValiderSessionsAction: TAction;
    MarquerlalesSessionscommeEFFECTUE1: TMenuItem;
    CopySelectionEssaisAction: TAction;
    CollerSelectionEssaisAction: TAction;
    ReinitSelectionEssaisAction: TAction;
    ValiderSelectionEssaisAction: TAction;
    AppSTAction1: TMenuItem;
    AppModeDemAction1: TMenuItem;
    N2: TMenuItem;
    CopySelectionEssaisAction1: TMenuItem;
    CollerSelectionEssaisAction1: TMenuItem;
    N3: TMenuItem;
    ReinitSelectionEssaisAction1: TMenuItem;
    ValiderSelectionEssaisAction1: TMenuItem;
    Manuel1: TMenuItem;
    Automatique1: TMenuItem;
    AppSTAction: TAction;
    AppModeDemAction: TAction;
    OpenDialogImport: TOpenDialog;
    SaveDialogExport: TSaveDialog;
    OrgaPageControl: TPageControl;
    OrgaTabSheet: TTabSheet;
    Splitter1: TSplitter;
    SessionsGroupBox: TGroupBox;
    SessionsPanel: TPanel;
    AjoutSessionButton: TButton;
    SuppSessionButton: TButton;
    SessionsStringGrid: TStringGrid;
    EssaisGroupBox: TGroupBox;
    EssaisStringGrid: TStringGrid;
    EssaiPanel: TPanel;
    Panel3: TPanel;
    ImporterOrgaButton: TButton;
    ExporterOrgaButton: TButton;
    SujetsTabSheet: TTabSheet;
    Splitter2: TSplitter;
    Panel4: TPanel;
    SujetsListBox: TListBox;
    Panel5: TPanel;
    SupprimerSujetButton: TButton;
    AjouterSujetButton: TButton;
    AppliquerOrgaSujetsButton: TButton;
    Panel8: TPanel;
    AttributsSujetListEditor: TValueListEditor;
    Splitter3: TSplitter;
    RepartirButton: TButton;
    StatSTButton: TButton;
    Panel2: TPanel;
    ParamsSujetStringGrid: TStringGrid;
    Splitter4: TSplitter;
    SessionsSujetStringGrid: TStringGrid;
    Action1: TAction;
    Image1: TImage;
    ImporterOrgaSujetButton: TButton;
    procedure StringGridDrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure SessionsStringGridMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure EssaisStringGridMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure AjoutSessionButtonClick(Sender: TObject);
    procedure SupprimeSessionButtonClick(Sender: TObject);
    procedure SessionsStringGridDblClick(Sender: TObject);
    procedure SessionPopupMenuPopup(Sender: TObject);
    procedure EssaisPopupMenuPopup(Sender: TObject);
    procedure ReinitSessionsActionExecute(Sender: TObject);
    procedure CollerSessionActionExecute(Sender: TObject);
    procedure CopySessionActionExecute(Sender: TObject);
    procedure ValiderSessionsActionExecute(Sender: TObject);
    procedure AppSTActionExecute(Sender: TObject);
    procedure AppModeDemActionExecute(Sender: TObject);
    procedure CopySelectionEssaisActionExecute(Sender: TObject);
    procedure CollerSelectionEssaisActionExecute(Sender: TObject);
    procedure ReinitSelectionEssaisActionExecute(Sender: TObject);
    procedure ValiderSelectionEssaisActionExecute(Sender: TObject);
    procedure ImporterOrgaButtonClick(Sender: TObject);
    procedure ExporterOrgaButtonClick(Sender: TObject);
    procedure SupprimerSujetButtonClick(Sender: TObject);
    procedure SujetsListBoxDblClick(Sender: TObject);
    procedure AjouterSujetButtonClick(Sender: TObject);
    procedure AppliquerOrgaSujetsButtonClick(Sender: TObject);
    procedure SujetsListBoxClick(Sender: TObject);
    procedure AttributsSujetListEditorDrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure AttributsSujetListEditorEnter(Sender: TObject);
    procedure AttributsSujetListEditorExit(Sender: TObject);
    procedure RepartirButtonClick(Sender: TObject);
    procedure StatSTButtonClick(Sender: TObject);
    procedure EssaisStringGridKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure HelpImageClick(Sender: TObject);
    procedure ImporterOrgaSujetButtonClick(Sender: TObject);
    procedure Panel2Resize(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
    SelectedSessionRow : Longint;
    SelectedEssaiRow : Longint;
    SelectedEssaiSujetRow : Longint;
    SelectedCol : Longint;
    NumSessCopy : integer;
    NumEssaiDebutCopy : integer;
    NumEssaiFinCopy : integer;
    procedure MajPresentationSessionsEssais;
    procedure MAJIHM;
		procedure ClearIHM;
		procedure MAJMAINMENU;
  end;

implementation

uses UDocometre, XMLClasses, USession, USujet, UAppliqueOrgaSujets,
  UAppliqueRepartEssais, UStatsRepartEssais, UImporterOrgaSujet, UGlobal;

{$R *.dfm}

procedure TOrgaFrame.MAJMAINMENU;
begin
  //
end;

procedure TOrgaFrame.MajPresentationSessionsEssais;
begin
  SessionsStringGrid.Cells[0,0] := 'Nom';
  SessionsStringGrid.Cells[1,0] := 'Nombre d''essais';
  SessionsStringGrid.DefaultColWidth := (SessionsStringGrid.ClientWidth div 2) - 2;
  EssaisStringGrid.Cells[1,0] := 'Nom de la séquence type';
  EssaisStringGrid.Cells[2,0] := 'Mode de démarrage';
  EssaisStringGrid.Cells[3,0] := 'Etat';
  EssaisStringGrid.DefaultColWidth := EssaisStringGrid.ClientWidth div 3 - 11 ;
  EssaisStringGrid.ColWidths[0] := 30;
  AttributsSujetListEditor.DefaultColWidth := AttributsSujetListEditor.ClientWidth div 2;
  SessionsSujetStringGrid.Cells[1,0] := 'Nom de la session';
  SessionsSujetStringGrid.Cells[2,0] := 'Nom de la séquence type';
  SessionsSujetStringGrid.Cells[3,0] := 'Mode de démarrage';
  SessionsSujetStringGrid.Cells[4,0] := 'Etat';
  SessionsSujetStringGrid.Cells[0,0] := '';
  SessionsSujetStringGrid.DefaultColWidth := SessionsSujetStringGrid.ClientWidth div 4 - 10 ;
  SessionsSujetStringGrid.ColWidths[0] := 30;
end;

procedure TOrgaFrame.StringGridDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  with (Sender as TStringGrid) do
  begin
    Canvas.Brush.Color := clWhite;
    if ARow > 0 then
    begin
      if (ARow mod 2) = 0 then Canvas.Brush.Color := $DDEEFF;
      if ((Sender as TStringGrid).Name = 'SessionsStringGrid') then
      begin
        if (Arow >= SessionsStringGrid.Selection.Top) and
           (ARow <= SessionsStringGrid.Selection.Bottom)
        then Canvas.Brush.Color := $FFF8DF;
        //if (ARow = SelectedSessionRow) then Canvas.Brush.Color := $FFF8DF;
      end;
      if ((Sender as TStringGrid).Name = 'EssaisStringGrid') then
      begin
        if (Arow >= EssaisStringGrid.Selection.Top) and
           (ARow <= EssaisStringGrid.Selection.Bottom)
        then Canvas.Brush.Color := $FFF8DF;
        //if SelectedEssaiRow = ARow then Canvas.Brush.Color := $FFF8DF;
      end;
      if ((Sender as TStringGrid).Name = 'SessionsSujetStringGrid') then
      begin
        if (Arow >= SessionsSujetStringGrid.Selection.Top) and
           (ARow <= SessionsSujetStringGrid.Selection.Bottom)
        then Canvas.Brush.Color := $FFF8DF;
        //if (ARow = SelectedSessionRow) then Canvas.Brush.Color := $FFF8DF;
      end;

    end else Canvas.Brush.Color := clBtnFace;
    Canvas.FillRect(Rect);
    DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect ,
                DT_CENTER or DT_VCENTER or DT_SINGLELINE	 );
  end;
end;

procedure TOrgaFrame.SessionsStringGridMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var ASession : IXMLSessionType;
    AEssai : IXMLEssaiType;
    NewSelectedSessionRow,i : integer;
begin
  SessionsStringGrid.MouseToCell(X,Y,SelectedCol,NewSelectedSessionRow);
  if SelectedSessionRow <> NewSelectedSessionRow then
  begin
    //MAJ Essais
    SelectedEssaiRow := 0;
    SelectedSessionRow := NewSelectedSessionRow;
    EssaisStringGrid.RowCount := 1;
    if SelectedSessionRow > 0 then
    begin
      ASession := DocDOCOMETRE.SESSIONS.Items[SelectedSessionRow - 1];
      for i := 0 to ASession.ESSAIS.Count - 1 do
      begin
        AEssai := ASession.ESSAIS.Items[i];
        EssaisStringGrid.RowCount := EssaisStringGrid.RowCount + 1;
        EssaisStringGrid.Cells[1,i+1] := AEssai.NOMST;
        EssaisStringGrid.Cells[2,i+1] := AEssai.MODEDEM;
        EssaisStringGrid.Cells[3,i+1] := AEssai.ETAT;
        EssaisStringGrid.Cells[0,i+1] := inttostr(i+1);
      end;
    end;
  end;
end;

procedure TOrgaFrame.EssaisStringGridMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
  begin
    if (Sender as TStringGrid).Name = 'EssaisStringGrid'
    then EssaisStringGrid.MouseToCell(X,Y,SelectedCol,SelectedEssaiRow);
    if (Sender as TStringGrid).Name = 'SessionsSujetStringGrid'
    then SessionsSujetStringGrid.MouseToCell(X,Y,SelectedCol,SelectedEssaiSujetRow);
  end;
end;

procedure TOrgaFrame.MAJIHM;
var ASession : IXMLSessionType;
    AEssai : IXMLEssaiType;
    i : integer;
    TheSelection : TGridRect;
begin
  //MAJ Sessions
  SessionsStringGrid.RowCount := 1;
  for i := 0 to DocDOCOMETRE.SESSIONS.Count - 1 do
  begin
    ASession := DocDOCOMETRE.SESSIONS.Items[i];
    SessionsStringGrid.RowCount := SessionsStringGrid.RowCount + 1;
    SessionsStringGrid.Cells[0,i+1] := ASession.NOM;
    SessionsStringGrid.Cells[1,i+1] := inttostr(ASession.ESSAIS.count);
  end;
  TheSelection.Top := SelectedSessionRow;
  TheSelection.Bottom := SelectedSessionRow;
  TheSelection.Left := 0;
  TheSelection.Right := 1;
  SessionsStringGrid.Selection := TheSelection;
  //MAJ Essais
  EssaisStringGrid.RowCount := 1;
  SelectedEssaiRow := 0;
  if SelectedSessionRow > 0 then
  begin
    ASession := DocDOCOMETRE.SESSIONS.Items[SelectedSessionRow - 1];
    for i := 0 to ASession.ESSAIS.Count - 1 do
    begin
      AEssai := ASession.ESSAIS.Items[i];
      EssaisStringGrid.RowCount := EssaisStringGrid.RowCount + 1;
      EssaisStringGrid.Cells[1,i+1] := AEssai.NOMST;
      EssaisStringGrid.Cells[2,i+1] := AEssai.MODEDEM;
      EssaisStringGrid.Cells[3,i+1] := AEssai.ETAT;
      EssaisStringGrid.Cells[0,i+1] := inttostr(i+1);
    end;
  end;
  //MAJ Sujets
  SujetsListBox.Clear;
  for i := 0 to DocDOCOMETRE.SUJETS.Count - 1
  do SujetsListBox.Items.Add(DocDOCOMETRE.SUJETS[i].NOM);
  SessionsSujetStringGrid.RowCount := 1;
  SelectedEssaiSujetRow := 0;
  AttributsSujetListEditor.Strings.Clear;
end;

procedure TOrgaFrame.ClearIHM;
begin
  SessionsStringGrid.RowCount := 1;
  EssaisStringGrid.RowCount := 1;
  SujetsListBox.Clear;
  SessionsSujetStringGrid.RowCount := 1;
  AttributsSujetListEditor.Strings.Clear;
end;

procedure TOrgaFrame.AjoutSessionButtonClick(Sender: TObject);
var NewSession : IXMLSESSIONType;
    NewEssai : IXMLESSAIType;
    i,n : integer;
begin
 //Creation d'une SESSION
  SessionDLGForm.Operation := 'C';
  if SessionDLGForm.ShowModal = mrOK then
  begin
    //Ajout SESSION dans DocDOCOMETRE
    NewSession := DocDOCOMETRE.SESSIONS.Add;
    NewSession.NOM := SessionDLGForm.NomSession.Text;
    for i := 0 to strtoint(SessionDLGForm.NBEssais.Text) - 1 do
    begin
      NewEssai := NewSession.ESSAIS.Add;
      NewEssai.NOMST := '?';
      NewEssai.MODEDEM := 'Manuel';
      NewEssai.ETAT := 'NON EFFECTUE';
    end;
    //Ajout d'une ligne a SessionsStringGrid
    SessionsStringGrid.RowCount := SessionsStringGrid.RowCount + 1;
    SessionsStringGrid.FixedRows := 1;
    n := SessionsStringGrid.RowCount - 1;
    //Ajout de la nouvelle entrée
    SessionsStringGrid.Cells[0,n] := NewSession.NOM;
    SessionsStringGrid.Cells[1,n] := inttostr(NewSession.ESSAIS.Count);
  end;
end;

procedure TOrgaFrame.SupprimeSessionButtonClick(Sender: TObject);
var i : integer;
begin
  i := DocoForm.OrgaFrame.SelectedSessionRow - 1;
  if i > -1 then
  begin
    //Supprimer ds le modele objet
    DocDocometre.SESSIONS.Delete(i);
    SelectedSessionRow := 0;
    SelectedEssaiRow := 0;
    MAJIHM;
  end;
end;

procedure TOrgaFrame.SessionsStringGridDblClick(Sender: TObject);
var i,n : integer;
    ASession : IXMLSESSIONType;
    NewEssai : IXMLESSAIType;
begin
  //Modif d'une EAN
  if SelectedSessionRow > 0 then
  begin
    SessionDLGForm.Operation := 'M';
    if SessionDLGForm.ShowModal = mrOK then
    begin
      //Modif dans DocDOCOMETRE
      i := SelectedSessionRow - 1;
      ASession := DocDOCOMETRE.SESSIONS.Items[i];
      ASession.NOM := SessionDLGForm.NomSession.Text;
      n := ASession.ESSAIS.Count;
      if n < strtoint(SessionDLGForm.NBEssais.Text) then
      begin
        for i := n to strtoint(SessionDLGForm.NBEssais.Text) - 1 do
        begin
          NewEssai := ASession.ESSAIS.Add;
          NewEssai.NOMST := '?';
          NewEssai.MODEDEM := 'Manuel';
          NewEssai.ETAT := 'NON EFFECTUE';
        end;
      end;
      if n > strtoint(SessionDLGForm.NBEssais.Text) then
      begin
        repeat
          ASession.ESSAIS.Delete(ASession.ESSAIS.Count - 1);
        until ASession.ESSAIS.Count = strtoint(SessionDLGForm.NBEssais.Text);
      end;
      MAJIHM;
    end;
  end;
end;

procedure TOrgaFrame.SessionPopupMenuPopup(Sender: TObject);
begin
  CopySessionAction.Enabled := (SelectedSessionRow > 0) and
  (SessionsStringGrid.Selection.Top = SessionsStringGrid.Selection.Bottom);
  CollerSessionAction.Enabled := (SelectedSessionRow > 0 );
  ReinitSessionsAction.Enabled := (SelectedSessionRow > 0 );
  ValiderSessionsAction.Enabled := (SelectedSessionRow > 0 );
end;

procedure TOrgaFrame.EssaisPopupMenuPopup(Sender: TObject);
var i : integer;
    NewMenuItem : TMenuItem;
begin

  if(SelectedEssaiRow = 0)
  then if EssaisStringGrid.Selection.Top > 0
       then SelectedEssaiRow := EssaisStringGrid.Selection.Top;


  AppSTAction1.Enabled := (SelectedEssaiRow > 0 );
  AppModeDemAction1.Enabled := (SelectedEssaiRow > 0 );
  AppSTAction.Enabled := (SelectedEssaiRow > 0 );
  AppModeDemAction1.Enabled := (SelectedEssaiRow > 0 );
  CopySelectionEssaisAction.Enabled := (SelectedEssaiRow > 0 );
  CollerSelectionEssaisAction.Enabled := (SelectedEssaiRow > 0 );
  ReinitSelectionEssaisAction.Enabled := (SelectedEssaiRow > 0 );
  ValiderSelectionEssaisAction.Enabled := (SelectedEssaiRow > 0 );
  AppSTAction1.Clear;
  for i := 0 to DocDOCOMETRE.ST.Count - 1 do
  begin
    NewMenuItem := TMenuItem.Create(EssaisPopupMenu);
    NewMenuItem.Name := 'ST'+inttostr(i);
    NewMenuItem.OnClick := AppSTActionExecute;
    NewMenuItem.Caption := DocDOCOMETRE.ST.Items[i].NOM;
    AppSTAction1.Add(NewMenuItem);
  end;
end;

procedure TOrgaFrame.ReinitSessionsActionExecute(Sender: TObject);
var i : integer;
begin
  for i := 0 to DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS.Count - 1
  do DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[i].ETAT := 'NON EFFECTUE';
  MAJIHM;
end;

procedure TOrgaFrame.ValiderSessionsActionExecute(Sender: TObject);
var i : integer;
begin
  for i := 0 to DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS.Count - 1
  do DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[i].ETAT := 'EFFECTUE';
  MAJIHM;
end;

procedure TOrgaFrame.CopySessionActionExecute(Sender: TObject);
begin
  NumSessCopy := SelectedSessionRow;
end;

procedure TOrgaFrame.CollerSessionActionExecute(Sender: TObject);
var i,j : integer;
    NewEssai : IXMLEssaiType;
begin
  if (NumSessCopy > 0) then
  begin
    for i := SessionsStringGrid.Selection.Top to SessionsStringGrid.Selection.Bottom do
    begin
      if i <> NumSessCopy then
      begin
        DocDOCOMETRE.SESSIONS[i-1].ESSAIS.Clear;
        for j := 0 to DocDOCOMETRE.SESSIONS[NumSessCopy-1].ESSAIS.Count - 1 do
        begin
          NewEssai := DocDOCOMETRE.SESSIONS[i-1].ESSAIS.add;
          NewEssai.NOMST := DocDOCOMETRE.SESSIONS[NumSessCopy-1].ESSAIS[j].NOMST;
          NewEssai.MODEDEM := DocDOCOMETRE.SESSIONS[NumSessCopy-1].ESSAIS[j].MODEDEM;
          NewEssai.ETAT := 'NON EFFECTUE';
        end;
      end;
    end;
    MAJIHM;
  end;
end;

procedure TOrgaFrame.AppSTActionExecute(Sender: TObject);
var i : integer;
    S : string;
    selectrionGrid : TGridRect;
begin
  S := (Sender as TMenuItem).Caption;
  delete(S,pos('&',S),1);
  for i := EssaisStringGrid.Selection.Top to EssaisStringGrid.Selection.Bottom
  do DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[i-1].NOMST := S;
  selectrionGrid := EssaisStringGrid.Selection;
  MAJIHM;
  EssaisStringGrid.Selection := selectrionGrid;
  EssaisStringGrid.TopRow := selectrionGrid.Top;
end;

procedure TOrgaFrame.AppModeDemActionExecute(Sender: TObject);
var i : integer;
    S : string;  
    selectrionGrid : TGridRect;
begin
  S := (Sender as TMenuItem).Caption;
  delete(S,pos('&',S),1);
  for i := EssaisStringGrid.Selection.Top to EssaisStringGrid.Selection.Bottom
  do DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[i-1].MODEDEM := S;
  selectrionGrid := EssaisStringGrid.Selection;
  MAJIHM;
  EssaisStringGrid.Selection := selectrionGrid;   
  EssaisStringGrid.TopRow := selectrionGrid.Top;
end;

procedure TOrgaFrame.CopySelectionEssaisActionExecute(Sender: TObject);
begin
  NumEssaiDebutCopy := EssaisStringGrid.Selection.Top;
  NumEssaiFinCopy := EssaisStringGrid.Selection.Bottom;
  NumSessCopy := SelectedSessionRow;
end;

procedure TOrgaFrame.CollerSelectionEssaisActionExecute(Sender: TObject);
var i,n : integer;
begin
  for i := NumEssaiDebutCopy to NumEssaiFinCopy do
  begin
    n := SelectedEssaiRow + (i - NumEssaiDebutCopy);
    if n <= DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS.Count then
    begin
      DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[n-1].NOMST :=
      DocDOCOMETRE.SESSIONS[NumSessCopy-1].ESSAIS[i-1].NOMST;
      DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[n-1].MODEDEM :=
      DocDOCOMETRE.SESSIONS[NumSessCopy-1].ESSAIS[i-1].MODEDEM;
      DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[n-1].ETAT := 'NON EFFECTUE';
    end else break;
  end;
  MAJIHM;
end;

procedure TOrgaFrame.ReinitSelectionEssaisActionExecute(Sender: TObject);
var i : integer;
    selectrionGrid : TGridRect;
begin
  for i := EssaisStringGrid.Selection.Top to EssaisStringGrid.Selection.Bottom
  do DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[i-1].ETAT := 'NON EFFECTUE';
  selectrionGrid := EssaisStringGrid.Selection;
  MAJIHM;
  EssaisStringGrid.Selection := selectrionGrid;
  EssaisStringGrid.TopRow := selectrionGrid.Top;
end;

procedure TOrgaFrame.ValiderSelectionEssaisActionExecute(Sender: TObject);
var i : integer;
    selectrionGrid : TGridRect;
begin
  for i := EssaisStringGrid.Selection.Top to EssaisStringGrid.Selection.Bottom
  do DocDOCOMETRE.SESSIONS[SelectedSessionRow-1].ESSAIS[i-1].ETAT := 'EFFECTUE';
  selectrionGrid := EssaisStringGrid.Selection;
  MAJIHM;
  EssaisStringGrid.Selection := selectrionGrid;
  EssaisStringGrid.TopRow := selectrionGrid.Top;
end;

procedure TOrgaFrame.ImporterOrgaButtonClick(Sender: TObject);
var i,j : integer;
    FichierImport : TIniFile;
    S,Value : string;
    fin : boolean;
    NewSession : IXMLSessionType;
    NewEssai : IXMLEssaiType;
begin
  if OpenDialogImport.Execute then
  begin
    if FileExists(OpenDialogImport.FileName) then
    begin
      FichierImport := TIniFile.Create(OpenDialogImport.FileName);
      try
        i := 0;
        fin := false;
        if FichierImport.SectionExists('SESSION'+inttostr(i+1))
        then DocDOCOMETRE.SESSIONS.Clear;
        repeat
          if FichierImport.SectionExists('SESSION'+inttostr(i+1)) then
          begin
            NewSession := DocDOCOMETRE.SESSIONS.Add;
            NewSession.NOM := FichierImport.ReadString('SESSION'+inttostr(i+1),'NOM','SESSION'+inttostr(i+1));
            j := 0;
            repeat
              Value := FichierImport.ReadString('SESSION'+inttostr(i+1),'Essai'+inttostr(j+1),'');
              S := Value;
              inc(j);
              if S <> '' then
              begin
                NewEssai := DocDOCOMETRE.SESSIONS[i].ESSAIS.Add;
                NewEssai.NOMST := copy(S,1,pos(';',S)-1);delete(S,1,pos(';',S));
                NewEssai.MODEDEM := copy(S,1,pos(';',S)-1);delete(S,1,pos(';',S));
                NewEssai.ETAT := S;S := '';
              end;
            until Value = '';
            inc(i);
          end else fin := true;
        until fin;
      finally
        FichierImport.Free;
        MAJIHM;
      end;
    end;
  end;
end;

procedure TOrgaFrame.ExporterOrgaButtonClick(Sender: TObject);
var i,j : integer;
    FichierExport : TIniFile;
    Value : string;
begin
  Value := DocoForm.XMLDoco.FileName;
  Value := ChangeFileExt( Value, '.ini' );
  SaveDialogExport.FileName :=   value;
  if SaveDialogExport.Execute then
  begin
    FichierExport := TIniFile.Create(SaveDialogExport.FileName);
    try
      for i := 0 to DocDOCOMETRE.SESSIONS.Count - 1 do
      begin
        FichierExport.WriteString('SESSION'+inttostr(i+1),'NOM',DocDOCOMETRE.SESSIONS[i].NOM);
        for j := 0 to DocDOCOMETRE.SESSIONS[i].ESSAIS.Count - 1 do
        begin
          Value := DocDOCOMETRE.SESSIONS[i].ESSAIS[j].NOMST;
          Value := Value + ';' + DocDOCOMETRE.SESSIONS[i].ESSAIS[j].MODEDEM;
          Value := Value + ';' + DocDOCOMETRE.SESSIONS[i].ESSAIS[j].ETAT;
          FichierExport.WriteString('SESSION'+inttostr(i+1),'Essai'+inttostr(j+1),Value);
        end;
      end;
      FichierExport.UpdateFile;
    finally
      FichierExport.Free;
    end;
  end;
end;

procedure TOrgaFrame.RepartirButtonClick(Sender: TObject);
begin
  if SelectedSessionRow > 0 then AppliqueRepartEssaisDLGForm.Show;
end;

procedure TOrgaFrame.StatSTButtonClick(Sender: TObject);
begin
  if SelectedSessionRow > 0 then StatsRepartEssaisDLGForm.Show;
end;

//-----------------------------------
//GESTION SUJETS
//-----------------------------------

procedure TOrgaFrame.AjouterSujetButtonClick(Sender: TObject);
var ASujet : IXMLSujetType;
    index : integer;
begin
  SujetDLGForm.Operation := 'C';
  if SujetDLGForm.ShowModal = mrOK then
  begin
    index := DocoForm.OrgaFrame.SujetsListBox.Items.IndexOf(SujetDLGForm.NomSujet.Text);
    if (index=-1) then
    begin
      ASujet := DocDOCOMETRE.SUJETS.Add;
      ASujet.NOM := SujetDLGForm.NomSujet.Text;
      ASujet.PARAMSFILEPATH := ExtractFilePath(DocoForm.XMLDoco.FileName)+ASujet.NOM+'.prm';
      SujetsListBox.Items.Add(ASujet.NOM);
    end else showmessage('Un autre sujet porte le même nom !');
  end;
  MAJIHM;
end;

procedure TOrgaFrame.SupprimerSujetButtonClick(Sender: TObject);
var i : integer;
		fos: TSHFileOpStruct;
		from : string;
begin
	i := DocoForm.OrgaFrame.SujetsListBox.ItemIndex;
	if i > -1 then
	begin
		if messageDlg('Etes-vous certain de vouloir supprimer ce sujet ?',mtConfirmation,[mbYes,mbNo],0) = mrYes then
		begin
			if DirectoryExists(rep_manip + DocDOCOMETRE.SUJETS[i].NOM) then
			begin
				ZeroMemory(@fos, SizeOf(fos));
				with fos do
				begin
					wFunc := FO_DELETE;
					fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
					from := rep_manip + DocDOCOMETRE.SUJETS[i].NOM + #0;
					pFrom := PChar(from);
				end;
				if (ShFileOperation(fos) <> 0)
				then showmessage ('Impossible de supprimer "' + rep_manip + DocDOCOMETRE.SUJETS[i].NOM +'"');
			end;

			DocDOCOMETRE.SUJETS.Delete(i);
			SujetsListBox.DeleteSelected;
			ParamsSujetStringGrid.Visible := false;
			Splitter4.Visible := false;
			ParamsSujetStringGrid.RowCount := 0;
			ParamsSujetStringGrid.ColCount := 0;

		end;
  end;
	MAJIHM;
	DocoForm.InspectFrame.MAJIHM;
end;

procedure TOrgaFrame.SujetsListBoxDblClick(Sender: TObject);
var i,index : integer;
    ASujet : IXMLSujetType;
		OldFileName : string;
begin
  SujetDLGForm.Operation := 'M';
  if SujetDLGForm.ShowModal = mrOK then
	begin
    i := DocoForm.OrgaFrame.SujetsListBox.ItemIndex;
    index := DocoForm.OrgaFrame.SujetsListBox.Items.IndexOf(SujetDLGForm.NomSujet.Text);
    if (index=-1) or (i=index) then
		begin
			ASujet := DocDOCOMETRE.SUJETS[i];
			if DirectoryExists(rep_manip + ASujet.NOM) then
			begin
				if RenameFile(rep_manip + ASujet.NOM,rep_manip + SujetDLGForm.NomSujet.Text) = false
				then showmessage ('Impossible de renommer le repertoire "' + rep_manip + ASUJET.NOM +'"');
			end;
			ASujet.NOM := SujetDLGForm.NomSujet.Text;
			SujetsListBox.Items[i] := ASujet.NOM;
			OldFileName := ASujet.PARAMSFILEPATH;
			ASujet.PARAMSFILEPATH := ExtractFilePath(DocoForm.XMLDoco.FileName)+ASujet.NOM+'.prm';
			RenameFile(OldFileName,ASujet.PARAMSFILEPATH);
			DocoForm.InspectFrame.MAJIHM;
    end else showmessage('Un autre sujet porte le même nom !');
  end;
end;

procedure TOrgaFrame.AppliquerOrgaSujetsButtonClick(Sender: TObject);
var i,j : integer;
    ASujet : IXMLSujetType;
    NewSESSION : IXMLSESSIONType;
begin
	if AppliquerOrgaSujetDLGForm.ShowModal = mrOK then
	begin
		for i := 0 to AppliquerOrgaSujetDLGForm.ListeSujetsCheckListBox.Count - 1 do
		begin
			if AppliquerOrgaSujetDLGForm.ListeSujetsCheckListBox.Checked[i] then
			begin
				ASujet := DocDOCOMETRE.SUJETS[i];
				ASujet.SESSIONS.Clear;
				for j := 0 to DocDOCOMETRE.SESSIONS.Count - 1 do
				begin
					NewSESSION := DocDOCOMETRE.SESSIONS[j].CloneNode(true) as IXMLSESSIONType;
					ASujet.SESSIONS.ChildNodes.Add(NewSESSION);
				end;
			end;
		end;
	end;
end;

procedure TOrgaFrame.ImporterOrgaSujetButtonClick(Sender: TObject);
var i,j : integer;
		ASujet : IXMLSujetType;
		NewSESSION : IXMLSESSIONType;
begin
	if ImporterOrgaSujetDLGForm.ShowModal = mrOK then
	begin
		for i := 0 to ImporterOrgaSujetDLGForm.ListeSujetsCheckListBox.Count - 1 do
		begin
			if ImporterOrgaSujetDLGForm.ListeSujetsCheckListBox.Checked[i] then
			begin
				ASujet := DocDOCOMETRE.SUJETS[i];
				DocDOCOMETRE.SESSIONS.Clear;
				for j := 0 to DocDOCOMETRE.SUJETS[i].SESSIONS.Count - 1 do
				begin
					NewSESSION := DocDOCOMETRE.SUJETS[i].SESSIONS[j].CloneNode(true) as IXMLSESSIONType;
					DocDOCOMETRE.SESSIONS.ChildNodes.Add(NewSESSION);
				end;
			end;
			MAJIHM;
		end;
	end;
end;

procedure TOrgaFrame.SujetsListBoxClick(Sender: TObject);
var i,j,n : integer;
    ASujet : IXMLSujetType;
    FichierPARAMS : TFileStream;
    S : string;
    LigneParams,ListeParams,ListeNomsParams : TStringList;

    procedure  ReadFichierParams;
    var pp,ii,jj : integer;
    begin
      pp := pos(Char(#13)+Char(#10),S);
      while (pp <> 0) do
      begin
        LignePARAMS.Add(copy(S,1,pp-1));
        delete(S,1,pp+1);
        pp := pos(Char(#13)+Char(#10),S);
      end;
      if S <> '' then LignePARAMS.Add(S);

      ParamsSujetStringGrid.RowCount := LignePARAMS.Count;
      for ii := 0 to LignePARAMS.Count - 1 do
      begin
        S := LignePARAMS[ii];
        ListePARAMS.Clear;
        pp := pos(';',S);
        while (pp <> 0) do
        begin
          ListePARAMS.Add(copy(S,1,pp-1));
          delete(S,1,pp);
          pp := pos(';',S);
        end;
        if S <> '' then ListePARAMS.Add(S);

        if ii = 0
        then ParamsSujetStringGrid.ColCount := ListePARAMS.Count;

        for jj := 0 to ListePARAMS.Count - 1 do
        begin
          if ii = 0
          then if ListeNomsParams.IndexOf(ListePARAMS[jj]) = -1
               then showmessage('Le canal ' + ListePARAMS[jj] + ' n''existe pas ou n''est pas un paramètre');
          ParamsSujetStringGrid.Cells[jj+1,ii] := ListePARAMS[jj];
        end;
      end;
    end;

begin
  AttributsSujetListEditor.Strings.Clear;
  SessionsSujetStringGrid.ColCount := 5;
  SessionsSujetStringGrid.RowCount := 1;
  ASujet := DocDOCOMETRE.SUJETS[SujetsListBox.ItemIndex];
  n := 1;
  for i := 0 to ASujet.SESSIONS.Count - 1 do
  begin
    for j := 0 to ASujet.SESSIONS[i].ESSAIS.Count - 1 do
    begin
      SessionsSujetStringGrid.RowCount := n + 1;
      SessionsSujetStringGrid.Cells[1,n] := ASujet.SESSIONS[i].NOM;
      SessionsSujetStringGrid.Cells[2,n] := ASujet.SESSIONS[i].ESSAIS[j].NOMST;
      SessionsSujetStringGrid.Cells[3,n] := ASujet.SESSIONS[i].ESSAIS[j].MODEDEM;
      SessionsSujetStringGrid.Cells[4,n] := ASujet.SESSIONS[i].ESSAIS[j].ETAT;
      SessionsSujetStringGrid.Cells[0,n] := inttostr(n);
      inc(n);
    end;
  end;

  for i := 0 to ASujet.ATTRIBUTS.Count - 1
  do AttributsSujetListEditor.InsertRow(ASujet.ATTRIBUTS[i].NOM,
                                   ASujet.ATTRIBUTS[i].VALEUR,
                                   true);


  ParamsSujetStringGrid.Visible := false;
  Splitter4.Visible := false;

  ParamsSujetStringGrid.ColCount := 0;
  ParamsSujetStringGrid.RowCount := 0;
  ListeNomsParams := TStringList.Create;
  ListeNomsParams.Sorted := true;
  for i := 0 to DocDOCOMETRE.CANINT.Count - 1 do
  begin
    if DocDOCOMETRE.CANINT[i].GENRE = 'PARAMETRE'
    then ListeNomsParams.Add(DocDOCOMETRE.CANINT[i].NOM);
  end;
  if ListeNomsParams.Count > 0 then
  begin
    if FileExists(ASujet.PARAMSFILEPATH) then
    begin
      FichierPARAMS := TFileStream.Create(ASujet.PARAMSFILEPATH,fmOpenRead);
      if FichierPARAMS.size > 0 then
      begin
        SetLength(S,FichierPARAMS.size);
        FichierPARAMS.Read(S[1],FichierPARAMS.size);
        LignePARAMS := TStringList.Create;
        LignePARAMS.Sorted := false;
        ListePARAMS := TStringList.Create;
        ListePARAMS.Sorted := false;
        ReadFichierParams;
        ListePARAMS.Free;
        LignePARAMS.Free;
      end else
      begin
        ParamsSujetStringGrid.ColCount := ListeNomsParams.Count;
        for i := 0 to ListeNomsParams.Count - 1
        do ParamsSujetStringGrid.Cells[i,0] := ListeNomsParams[i];
      end;
      FichierPARAMS.Free;
      ParamsSujetStringGrid.ColCount := ParamsSujetStringGrid.ColCount + 1;
      for i := 0 to ParamsSujetStringGrid.RowCount - 1
      do ParamsSujetStringGrid.Cells[0,i+1] := inttostr(i+1);

      ParamsSujetStringGrid.Visible := true;
      Splitter4.Visible := true;

      //ParamsSujetStringGrid.DefaultColWidth := ParamsSujetStringGrid.ClientWidth div (ParamsSujetStringGrid.ColCount - 1) - (4*(ParamsSujetStringGrid.ColCount - 1));
      //ParamsSujetStringGrid.ColWidths[0] := 30;

    end else
    begin
      //ParamsSujetStringGrid.Visible := false;
      //Splitter4.Visible := false;
      //ParamsSujetStringGrid.ColCount := ListeNomsParams.Count;
      //for i := 0 to ListeNomsParams.Count - 1
      //do ParamsSujetStringGrid.Cells[i,0] := ListeNomsParams[i];
    end;
  end;
  ListeNomsParams.Free;
end;

procedure TOrgaFrame.AttributsSujetListEditorDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  with (Sender as TValueListEditor) do
  begin
    Canvas.Brush.Color := clWhite;
    if ARow > 0 then
    begin
      if (ARow mod 2) = 0 then Canvas.Brush.Color := $DDEEFF;
    end else Canvas.Brush.Color := clBtnFace;
    Canvas.FillRect(Rect);
    DrawText(Canvas.Handle, PChar(Cells[ACol,ARow]), -1, Rect ,
                DT_VCENTER or DT_SINGLELINE	 );
  end;
end;

procedure TOrgaFrame.AttributsSujetListEditorEnter(Sender: TObject);
begin
  DocoForm.ActionMainMenuBar1.Enabled := false;
  DocoForm.ActionToolBar1.Enabled := false;

end;

procedure TOrgaFrame.AttributsSujetListEditorExit(Sender: TObject);
var i : integer;
    NewAttribut : IXMLATTRIBUTType;
begin
  DocoForm.ActionMainMenuBar1.Enabled := true;
  DocoForm.ActionToolBar1.Enabled := true;

  if SujetsListBox.ItemIndex > -1 then
  begin
    DocDOCOMETRE.SUJETS[SujetsListBox.ItemIndex].ATTRIBUTS.Clear;
    for i := 1 to AttributsSujetListEditor.RowCount - 1 do
    begin
      if AttributsSujetListEditor.Keys[i] <> '' then
      begin
        NewAttribut := DocDOCOMETRE.SUJETS[SujetsListBox.ItemIndex].ATTRIBUTS.Add;
        NewAttribut.NOM := AttributsSujetListEditor.Keys[i];
        NewAttribut.VALEUR := AttributsSujetListEditor.Values[AttributsSujetListEditor.Keys[i]];
      end;
    end;
  end else AttributsSujetListEditor.Strings.Clear;
end;



procedure TOrgaFrame.EssaisStringGridKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
var myRect : TGridRect;
begin
  if (ssCtrl in Shift) and ((key = Ord('A')) or (key = Ord('a'))) then
  begin
    if EssaisStringGrid.RowCount > 1 then
    begin
      myRect.Left := 0;
      myRect.Top := 1;
      myRect.Right := 3;
      myRect.Bottom := EssaisStringGrid.RowCount - 1;
      EssaisStringGrid.Selection := myRect;
      SelectedEssaiRow := 1;
    end;
  end;
end;

procedure TOrgaFrame.HelpImageClick(Sender: TObject);
begin
  DocoForm.ShowHelp((Sender as TImage).Tag);
end;

procedure TOrgaFrame.Panel2Resize(Sender: TObject);
var w : integer;
begin
 { if ParamsSujetStringGrid.ColCount > 1 then
  begin
    w :=  ParamsSujetStringGrid.ClientWidth;
    w := w div (ParamsSujetStringGrid.ColCount - 1);
    w := w - (4*(ParamsSujetStringGrid.ColCount - 1));

    if(w<30) then w := 30;

    ParamsSujetStringGrid.DefaultColWidth := w;

    //ParamsSujetStringGrid.ClientWidth div (ParamsSujetStringGrid.ColCount - 1) -
//(4*(ParamsSujetStringGrid.ColCount - 1));
    ParamsSujetStringGrid.ColWidths[0] := 30;
  end;            }
end;

end.

{



}
