unit DiagStep3;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  WizTmpl, ExtCtrls, StdCtrls, MiscRoutines, Matrices;

type
  PFactorInfo = ^TFactorInfo;
  TFactorInfo = record
    // фактор, подлежащий варьированию (нум. с 0);
    Number    : integer;
    // Начальное значение фактора
    Start     : double;
    // Конечное значение фактора
    Stop      : double;
    // Число точек вычисления
    Points    : integer;
  end;

{ ========================================================================
  Функция выполняет мастера подготовки данных для графика. На выходе:
    Истина - выполнение завершено успешно; при этом:
      FactorInfo - параметры варьирования
      Levels - вектор с уровнями остальных факторов.
    Лож - пользователь остановил подготовку.
======================================================================== }

function RunExportSurfaceWizard(var FactorInfo1,FactorInfo2:TFactorInfo;
  var Levels:TVector):Boolean;

{ ========================================================================
  Функция выполняет мастера подготовки данных для поверхности. На выходе:
    Истина - выполнение завершено успешно; при этом:
      FactorInfo1,FactorInfo2 - параметры варьирования
      Levels - вектор с уровнями остальных факторов.
    Лож - пользователь остановил подготовку.
======================================================================== }

function RunExportDiagramWizard(var FactorInfo:TFactorInfo;
  var Levels:TVector):Boolean;

type
  TDiagramStep3 = class(TWizardTemplateForm)
    Label1: TLabel;
    GBFactors: TGroupBox;
    GBLevels: TGroupBox;
    LBFactors: TListBox;
    ELevel: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure LBFactorsClick(Sender: TObject);
    procedure ELevelClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    CurrentIndex : integer;
    Level     : double;
    procedure ResvArrange(var f,s:integer);
    function  Index2F(Index:integer):integer;
  private
    function  Validate:Boolean;
    procedure Clean;
  public
    Reserved1,
    Reserved2 : integer;
    Levels : TVector;
    function GoForward:Boolean;override;
    function GoBackward:Boolean;override;
  end;

var
  DiagramStep3: TDiagramStep3;

implementation

{$R *.DFM}

uses MainUnit, Plan, DiagStep1, DiagStep2, SurfStep1, SurfStep2;

function TDiagramStep3.Validate;
begin
  if(ELevel.Enabled) then
    Result:=ContainDouble(ELevel.Text,Level,'Уровень')
  else
    Result:=True;
end;

procedure TDiagramStep3.Clean;
begin
  if(ELevel.Enabled) then
    ELevel.Text:=ValidDouble(ELevel.Text);
end;

procedure TDiagramStep3.ResvArrange;
begin
  if(Reserved1>Reserved2) then
  begin
    f:=Reserved2;
    s:=Reserved1;
  end
  else
  begin
    f:=Reserved1;
    s:=Reserved2;
  end;
end;

function TDiagramStep3.Index2F;
var
  f,s : integer;
begin
  ResvArrange(f,s);
  if(f=-1) then
  begin
    if(Index<s) then
      Result:=Index
    else
      Result:=Index+1;
  end
  else
  begin
    if(Index<f) then
      Result:=Index
    else
      if(Index<s) then
        Result:=Index+1
      else
        Result:=Index+2;
  end;
end;

function TDiagramStep3.GoForward;
begin
  Result:=True;
end;

function TDiagramStep3.GoBackward;
begin
  Clean;
  Result:=True;
end;

procedure TDiagramStep3.FormCreate(Sender: TObject);
begin
  inherited;
  Levels.Create(0);
  Reserved1:=-1;
  Reserved2:=-1;
end;

procedure TDiagramStep3.FormShow(Sender: TObject);
var
  i : integer;
  Origin : TStringList;
  f,s : integer;
begin
  inherited;
  Origin:=TStringList.Create;
  MainForm.MainPlan.LBFillWithNames(Origin);
  Levels.Recreate(Origin.Count);
  Levels.Zero;
  Assert(Reserved1<>-1);
  LBFactors.Items.Clear;
  if(Reserved2=-1) then
  begin
    for i:=0 to Reserved1-1 do
      LBFactors.Items.Add(Origin[i]);
    for i:=Reserved1+1 to Origin.Count-1 do
      LBFactors.Items.Add(Origin[i]);
  end
  else
  begin
    Assert(Reserved1<>Reserved2);
    ResvArrange(f,s);
    for i:=0 to f-1 do
      LBFactors.Items.Add(Origin[i]);
    for i:=f+1 to s-1 do
      LBFactors.Items.Add(Origin[i]);
    for i:=s+1 to Origin.Count-1 do
      LBFactors.Items.Add(Origin[i]);
  end;
  LBFactors.Enabled:=LBFactors.Items.Count>0;
  ELevel.Enabled:=LBFactors.Enabled;
  if(LBFactors.Enabled) then
  begin
    LBFactors.ItemIndex:=0;
    CurrentIndex:=0;
    ELevel.Text:='0';
  end
  else
  begin
    LBFactors.Items.Add('(фиксированных факторов нет)');
    ELevel.Text:='';
  end;
end;

procedure TDiagramStep3.LBFactorsClick(Sender: TObject);
begin
  inherited;
  if(LBFactors.Enabled) then
  begin
    Levels.Elements^[Index2F(CurrentIndex)]:=StrToFloat(ELevel.Text);
    CurrentIndex:=LBFactors.ItemIndex;
    ELevel.Text:=FloatToStr(Levels.Elements^[Index2F(CurrentIndex)]);
  end;
end;

procedure TDiagramStep3.ELevelClick(Sender: TObject);
begin
  inherited;
  if(LBFactors.Enabled) then
  begin
    if(not ContainDouble(ELevel.Text,Level,'Уровень')) then
      FocusControl(ELevel);
  end;
end;

// =======================================================================
// RunExportDiagramWizard
// =======================================================================

function RunExportDiagramWizard;
label
  EDWStep1,EDWStep2,EDWStep3;
begin
  Result:=False;
EDWStep1:
  if(DiagramStep1.ShowModal=mrCancel) then exit;
EDWStep2:
  case DiagramStep2.ShowModal of
    mrRetry : goto EDWStep1;
    mrCancel : exit;
  end;
EDWStep3:
  case DiagramStep3.ShowModal of
    mrRetry : goto EDWStep2;
    mrCancel : exit;
  end;
  FactorInfo.Number:=DiagramStep1.LBFactors.ItemIndex;
  FactorInfo.Start:=DiagramStep2.LeftBound;
  FactorInfo.Stop:=DiagramStep2.RightBound;
  FactorInfo.Points:=DiagramStep2.SEPoints.Value;
  Levels.Recreate(DiagramStep3.Levels.nSize);
  Levels._CopyFrom(DiagramStep3.Levels);
  Result:=True;
end;

// =======================================================================
// RunExportSurfaceWizard
// =======================================================================

function RunExportSurfaceWizard;
label
  ESWStep1,ESWStep2,ESWStep3;
begin
  Result:=False;
ESWStep1:
  if(SurfaceStep1.ShowModal=mrCancel) then exit;
ESWStep2:
  case SurfaceStep2.ShowModal of
    mrRetry : goto ESWStep1;
    mrCancel : exit;
  end;
ESWStep3:
  case DiagramStep3.ShowModal of
    mrRetry : goto ESWStep2;
    mrCancel : exit;
  end;
  FactorInfo1.Number:=SurfaceStep1.Reserved1;
  FactorInfo2.Number:=SurfaceStep1.Reserved2;
  FactorInfo1.Start:=SurfaceStep2.FirstLeft;
  FactorInfo2.Start:=SurfaceStep2.SecondLeft;
  FactorInfo1.Stop:=SurfaceStep2.FirstRight;
  FactorInfo2.Stop:=SurfaceStep2.SecondRight;
  FactorInfo1.Points:=SurfaceStep2.SEFirstPoints.Value;
  FactorInfo2.Points:=SurfaceStep2.SESecondPoints.Value;
  Levels.Recreate(DiagramStep3.Levels.nSize);
  Levels._CopyFrom(DiagramStep3.Levels);
  Result:=True;
end;

procedure TDiagramStep3.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  inherited;
  CanClose:=ValidateByResult(Validate,Clean,ModalResult);
end;

end.
