unit U_ArchWndDoorParams;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,

  U_BaseCommon, U_BaseConstants, U_ArchCommon, RzButton, Mask, RzEdit,
  RzPanel, ExtCtrls, siComp, siLngLnk, RzRadChk;

type
  TF_ArchWndDoorParams = class(TForm)
    pnMain: TRzPanel;
    pnOkCancel: TRzPanel;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    lng_Forms: TsiLangLinked;
    pnGeneral: TRzPanel;
    lbCoordz: TLabel;
    lbWidth: TLabel;
    lbHeight: TLabel;
    fCoordz: TRzNumericEdit;
    fWidth: TRzNumericEdit;
    fHeight: TRzNumericEdit;
    pnOuterSlope: TRzPanel;
    RzGroupBox2: TRzGroupBox;
    fosLbWidth: TLabel;
    fosLbHeight: TLabel;
    fosLbDepth: TLabel;
    fosWidth: TRzNumericEdit;
    fosHeight: TRzNumericEdit;
    fosDepth: TRzNumericEdit;
    fosSlopeOn: TRzCheckBox;
    pnInnerSlope: TRzPanel;
    RzGroupBox1: TRzGroupBox;
    fisLbWidth: TLabel;
    fisLbHeight: TLabel;
    fisLbDepth: TLabel;
    fisWidth: TRzNumericEdit;
    fisHeight: TRzNumericEdit;
    fisDepth: TRzNumericEdit;
    fisSlopeOn: TRzCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure pnOkCancelResize(Sender: TObject);
    procedure SlopeOnClick(Sender: TObject);
    procedure fHeightChange(Sender: TObject);
    procedure fWidthChange(Sender: TObject);

  private
    GForm: TForm;
    FPropsUOM: TStringList;
    FType: Integer;
    FIsEditing: Boolean;
    FIsSettingParams: Boolean; // Флаг - установка параметров на форму

    function GetSlopeParams(AArchWndInfo: TArchWndInfo; const APropPrefix: string): TArchSlopeInfo;

    procedure AutoChangeSlopeParams(AEditingControl, AParam1, AParam2: TRzNumericEdit);
    procedure SetCaptions;
    procedure SetControls;
    procedure SetSlopeParams(AChecked: Boolean; ASlopeInfo: TArchSlopeInfo; const APropPrefix: string);
  public
    Constructor Create(AOwner: TComponent; AForm: TForm);
    Destructor Destroy; override;

    function Execute(AType: Integer; AArchWndInfo: TComponent): TArchWndInfo;
  published
    property PropMainForm: TForm read GForm;
    property PropsUOM: TStringList read FPropsUOM;
  end;

  function ShowArchWndDoorParams(AType: Integer; AArchWndInfo: TComponent): TArchWndInfo;

var
  F_ArchWndDoorParams: TF_ArchWndDoorParams;

implementation
Uses U_Main;
{$R *.dfm}

constructor TF_ArchWndDoorParams.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_ArchWndDoorParams.Destroy;
begin
  inherited;
end;

function TF_ArchWndDoorParams.Execute(AType: Integer; AArchWndInfo: TComponent): TArchWndInfo;
var
  WndInfo: TArchWndInfo;
  //Slope: ^TArchSlopeInfo;
  Obj: TObject;
  InnerSlopeType: Integer;
  OuterSlopeType: Integer;
begin
  Result := nil;
  FType := AType;
  FIsEditing := AArchWndInfo <> nil;

  InnerSlopeType := -1;
  OuterSlopeType := -1;
  case AType of
    ctArhWindow:
      begin
        InnerSlopeType := ctArhWndInnerSlope;
        OuterSlopeType := ctArhWndOuterSlope;
      end;
    ctArhDoor:
      InnerSlopeType := ctArhDoorInnerSlope;
  end;

  SetCaptions;
  SetControls;

  WndInfo := TArchWndInfo.Create(nil);
  if AArchWndInfo <> nil then
    WndInfo.Assign(AArchWndInfo)
  else
  begin
    if LoadArchObjDefaultParams(WndInfo, AType) = nil then
    //if LoadArchObjDefaultParams(TComponent(WndInfo), AType) = nil then
    begin
      // Параметры по умолчанию
      if (AType = ctArhWindow)then
        WndInfo.Coordz := 0.8;
      if (AType = ctArhDoor) or (AType = ctArhArc) then
      begin
        WndInfo.Height := 2.1;
        WndInfo.Width := 1;
      end
      else
      begin
        WndInfo.Height := 1.4;
        WndInfo.Width := 1.4;
      end;
    end;
    WndInfo.CreateChilds;

    if LoadArchObjDefaultParams(WndInfo.InnerSlope, InnerSlopeType) = nil then
    //if LoadArchObjDefaultParams(TComponent(Pointer(@WndInfo.InnerSlope)^), InnerSlopeType) = nil then
    begin
      if (AType = ctArhDoor) or (AType = ctArhArc) then
      begin
        WndInfo.InnerSlope.Height := 2.0;
        WndInfo.InnerSlope.Width := 1;
        WndInfo.InnerSlope.Depth := 0.1
      end
      else
      begin
        WndInfo.InnerSlope.Height := 1.4;
        WndInfo.InnerSlope.Width := 1.4;
        WndInfo.InnerSlope.Depth := 0.1
      end;
    end;

    LoadArchObjDefaultParams(WndInfo.OuterSlope, OuterSlopeType);
    //LoadArchObjDefaultParams(TComponent(Pointer(@WndInfo.OuterSlope)^), OuterSlopeType);
  end;

  fHeight.OnChange := nil;
  fWidth.OnChange := nil;
  FIsSettingParams := true;
  try
    ObjectPropsToForm(WndInfo, Self);
    SetSlopeParams(WndInfo.InnerSlopeOn, WndInfo.InnerSlope, 'fis');
    SetSlopeParams(WndInfo.OuterSlopeOn, WndInfo.OuterSlope, 'fos');

    // Применить параметры объекта для откосов
    fHeightChange(fHeight);
    fWidthChange(fWidth);
  finally
    FIsSettingParams := false;
    fHeight.OnChange := fHeightChange;
    fWidth.OnChange := fWidthChange;
  end;

  //if WndInfo.InnerSlope = nil then
//  //if AArchWndInfo = nil then
//  begin
//    if (AType = ctArhDoor) or (AType = ctArhArc) then
//    begin
//      fisHeight.Value := 2.0;
//      fisWidth.Value := 1;
//      fisDepth.Value := 0.1
//    end
//    else
//    begin
//      fisHeight.Value := 1.4;
//      fisWidth.Value := 1.4;
//      fisDepth.Value := 0.1
//    end;
//  end;

  WndInfo.Free;

  if ShowModal = mrOk then
  begin
    Result := TArchWndInfo.Create(nil);
    ObjectPropsFromForm(Result, Self);

    Result.InnerSlope := GetSlopeParams(Result, 'fis');
    Result.OuterSlope := GetSlopeParams(Result, 'fos');
    Result.InnerSlopeOn := fisSlopeOn.Checked;
    Result.OuterSlopeOn := fosSlopeOn.Checked;

    // Сохраняем параметры как по-умолчанию
    SetArchObjToDefaultParams(Result, AType);
    SetArchObjToDefaultParams(Result.InnerSlope, InnerSlopeType);
    SetArchObjToDefaultParams(Result.OuterSlope, OuterSlopeType);
  end;
end;

function TF_ArchWndDoorParams.GetSlopeParams(AArchWndInfo: TArchWndInfo; const APropPrefix: string): TArchSlopeInfo;
var
  CheckBox: TRzCheckBox;
begin
  Result := nil;
  CheckBox := TRzCheckBox(Self.FindComponent(APropPrefix + 'SlopeOn'));
  if (CheckBox <> nil) then
  //if (CheckBox <> nil) and CheckBox.Checked then
  begin
    Result := TArchSlopeInfo.Create(AArchWndInfo);
    ObjectPropsFromForm(Result, Self, APropPrefix);
  end;
end;

procedure TF_ArchWndDoorParams.AutoChangeSlopeParams(AEditingControl, AParam1, AParam2: TRzNumericEdit);
begin
  if AParam1.Enabled then
    AParam1.Value := AEditingControl.Value;
  if AParam2.Enabled then
    AParam2.Value := AEditingControl.Value;
end;

procedure TF_ArchWndDoorParams.SetCaptions;
var
  i: integer;
  Labl: TLabel;
  lbCaptionLen: String;
begin
  if FType = ctArhWindow then
    Caption := cArchWndDoorParams_Msg01
  else if FType = ctArhDoor then
    Caption := cArchWndDoorParams_Msg02
  else if FType = ctArhArc then
    Caption := cArchWndDoorParams_Msg03;

//  //lbCoordz.Caption := cArchParams_Msg05 +', '+ GetNameUOM(TF_Main(GForm).FUOM, true);
//  //lbWidth.Caption := cArchParams_Msg02 +', '+ GetNameUOM(TF_Main(GForm).FUOM, true);
//  //lbHeight.Caption := cArchParams_Msg06 +', '+ GetNameUOM(TF_Main(GForm).FUOM, true);
//
//  for i := 0 to ComponentCount - 1 do
//  begin
//    if Components[i] is TLabel then
//    begin
//      Labl := TLabel(Components[i]);
//      lbCaptionLen := '';
//      if Pos('Coordz', Labl.Name) <> 0 then
//        lbCaptionLen := cArchParams_Msg05
//      else if Pos('Width', Labl.Name) <> 0 then
//        lbCaptionLen := cArchParams_Msg02
//      else if Pos('Height', Labl.Name) <> 0 then
//        lbCaptionLen := cArchParams_Msg06
//      else if Pos('Depth', Labl.Name) <> 0 then
//        lbCaptionLen := cArchParams_Msg07
//      else if Pos('Length', Labl.Name) <> 0 then
//        lbCaptionLen := cArchParams_Msg10;
//
//      if lbCaptionLen <> '' then
//        Labl.Caption := lbCaptionLen +', '+ GetNameUOM(TF_Main(GForm).FUOM, true);
//    end;
//  end;

  SetLableCaptions(Self, TF_Main(GForm).FUOM);
end;

procedure TF_ArchWndDoorParams.SetControls;
begin
  {// Внешний откос
  pnOuterSlope.Visible := (FType = ctArhWindow) and Not FIsEditing;
  // Внутренний откос
  pnInnerSlope.Visible := (FType in [ctArhWindow, ctArhDoor]) and Not FIsEditing;

  Self.AutoSize := false;
  pnMain.AutoSize := false;
  pnMain.AutoSize := true;
  Self.AutoSize := true;}

  Self.AutoSize := false;
  pnMain.AutoSize := false;

  //pnMain.Height := pnGeneral.Height + pnInnerSlope.Height + pnOuterSlope.Height + 1;
  // Внутренний откос
  pnInnerSlope.Visible := (FType in [ctArhWindow, ctArhDoor]) and Not FIsEditing;
  // Внешний откос
  pnOuterSlope.Top := pnInnerSlope.Top + pnInnerSlope.Height;
  pnOuterSlope.Visible := (FType = ctArhWindow) and Not FIsEditing;

  pnMain.AutoSize := true;
  Self.AutoSize := true;

  {Self.AutoSize := false;
  pnMain.AutoSize := false;
  Self.AutoSize := true;
  pnMain.AutoSize := true;}
end;

procedure TF_ArchWndDoorParams.SetSlopeParams(AChecked: Boolean; ASlopeInfo: TArchSlopeInfo; const APropPrefix: string);
var
  CheckBox: TRzCheckBox;
begin
  CheckBox := TRzCheckBox(Self.FindComponent(APropPrefix + 'SlopeOn'));
  if Assigned(ASlopeInfo) then
  begin
    ObjectPropsToForm(ASlopeInfo, Self, APropPrefix);
    if (CheckBox <> nil) and AChecked then
      CheckBox.Checked := true;
    SlopeOnClick(CheckBox);
  end
  else
    SlopeOnClick(CheckBox);
end;

procedure TF_ArchWndDoorParams.FormCreate(Sender: TObject);
begin
  pnOkCancelResize(pnOkCancel);

  FPropsUOM := TStringList.Create;
  //FPropsUOM.Add(fCoordz.Name);
  //FPropsUOM.Add(fWidth.Name);
  //FPropsUOM.Add(fHeight.Name);

  fisSlopeOn.OnClick := SlopeOnClick;
  fosSlopeOn.OnClick := SlopeOnClick;
  FIsSettingParams := false;
  
  SetFormControlDisplayFormat(Self);
end;

procedure TF_ArchWndDoorParams.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FPropsUOM);
end;

function ShowArchWndDoorParams(AType: Integer; AArchWndInfo: TComponent): TArchWndInfo;
begin
  Result := nil;
  try
    if F_ArchWndDoorParams = nil then
      F_ArchWndDoorParams := TF_ArchWndDoorParams.Create(F_ProjMan, F_ProjMan);

    Result := F_ArchWndDoorParams.Execute(AType, AArchWndInfo);
  except
    on E: Exception do AddExceptionToLogEx('ShowArchWndDoorParams', E.Message);
  end;
end;

procedure TF_ArchWndDoorParams.pnOkCancelResize(Sender: TObject);
begin
  SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;

procedure TF_ArchWndDoorParams.SlopeOnClick(Sender: TObject);
var
  Prefix: String;
  i: integer;
  Compon: TWinControl;
begin
  Prefix := Copy(TComponent(Sender).Name, 1, 3);
  if Prefix <> '' then
  begin
    for i := 0 to ComponentCount - 1 do
    begin
      if (Components[i] is TWinControl) then
      begin
        Compon := TWinControl(Components[i]);
        if (Compon <> Sender) and (Pos(Prefix, Compon.Name) = 1) then
          Compon.Enabled := TRzCheckBox(Sender).Checked;
      end;
    end;
  end;

  // Автоизменеие свойств откосов при включении откосов
  if TRzCheckBox(Sender).Checked and Not FIsSettingParams then
  begin
    if Sender = fisSlopeOn then
    begin
      fisHeight.Value := fHeight.Value;
      fisWidth.Value := fWidth.Value;
    end
    else if Sender = fosSlopeOn then
    begin
      fosHeight.Value := fHeight.Value;
      fosWidth.Value := fWidth.Value;
    end;
  end;
end;

procedure TF_ArchWndDoorParams.fHeightChange(Sender: TObject);
begin
  AutoChangeSlopeParams(fHeight, fisHeight, fosHeight);
end;

procedure TF_ArchWndDoorParams.fWidthChange(Sender: TObject);
begin
  AutoChangeSlopeParams(fWidth, fisWidth, fosWidth);
end;

end.
