unit U_ObjectParams;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel, RzSpnEdt, StdCtrls, Mask, RzEdit, RzButton,
  U_BaseCommon, U_BaseConstants, U_SCSComponent, ComCtrls, siComp, siLngLnk, RzTabs, RzBtnEdt,
  RzLabel, RzRadChk, Buttons, RzCmboBx, Menus, U_Cad, U_Constants, U_ESCadClasess;

type
  TF_ObjectParams = class(TForm)
    GroupBox_Main: TRzGroupBox;
    GroupBox_OkCancel: TRzGroupBox;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    edName: TRzEdit;
    Label1: TLabel;
    lng_Forms: TsiLangLinked;
    Label2: TLabel;
    seCurrIndex: TRzSpinEdit;
    lbHeightCeiling: TLabel;
    neHeightCeiling: TRzNumericEdit;
    Label4: TLabel;
    edNameShort: TRzEdit;
    lbSquare: TLabel;
    neSquare: TRzNumericEdit;
    lbVolume: TLabel;
    neVolume: TRzNumericEdit;
    lbHeight: TLabel;
    neHeight: TRzNumericEdit;
    RzGroupBox1: TRzGroupBox;
    RzWorkRoom: TRzCheckBox;
    RzPlenumArea: TRzCheckBox;
    RzUnRoutableArea: TRzCheckBox;
    RzLabel1: TRzLabel;
    RzLabel2: TRzLabel;
    RzLabel3: TRzLabel;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    TimerEnableHint: TTimer;
    CabinetSortNamePos: TRzComboBox;
    CabinetNumPos: TRzComboBox;
    CabinetExtPoints: TRzComboBox;
    CabinetExtNumPos: TRzComboBox;
    CabinetsSize: TRzComboBox;
    CabinetSquare_Change: TRzBitBtn;
    SetSquare_PopupMenu: TPopupMenu;
    N1: TMenuItem;
    SetNewSquare_Value: TRzBitBtn;
    procedure edNameChange(Sender: TObject);
    procedure btOkClick(Sender: TObject);
    procedure GroupBox_OkCancelResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure neHeightCeilingChange(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure TimerEnableHintTimer(Sender: TObject);
    procedure CabinetSquare_ChangeClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure SetNewSquare_ValueClick(Sender: TObject);
  private
    GForm: TForm;
    FMakeEdit: TMakeEdit;
    FListOwner: TSCSList;
    FRoomSquare: Double;

    procedure DefineParamsRelatedToHeightCeiling;
  public
    //Tolik
    CabinetSquareChanged: boolean;
    RoomParams: TObjectParams;
    //
    constructor Create(AOwner: TComponent; AForm: TForm);
    destructor Destroy; override;
     //Tolik
    //function MakeEditRoom(AMakeEdit: TMakeEdit; var ARoomParams: TObjectParams; AListOwner: TSCSList): Boolean;
    function MakeEditRoom(AMakeEdit: TMakeEdit; var ARoomParams: TObjectParams; AListOwner: TSCSList; AUserSquare: Boolean = False): Boolean;
  end;

//var
//  F_ObjectParams: TF_ObjectParams;

implementation
Uses U_Main, U_Common, DrawObjects;
{$R *.dfm}

{ TF_ObjectParams }

constructor TF_ObjectParams.Create(AOwner: TComponent; AForm: TForm);
begin
    GForm := AForm;
    Inherited Create(AOwner);
end;

destructor TF_ObjectParams.Destroy;
begin
    inherited;
end;

procedure TF_ObjectParams.DefineParamsRelatedToHeightCeiling;
var
  HeightCeiling: Double;
  RoomHeight: Double;
begin
  HeightCeiling := FloatInUOM(neHeightCeiling.Value, TF_Main(GForm).FUOM, umMetr);
  if HeightCeiling <= 0 then
    HeightCeiling := FListOwner.Setting.HeightCeiling;
  RoomHeight := FListOwner.Setting.HeightRoom - HeightCeiling;
  if RoomHeight < 0 then
    RoomHeight := 0;

  neHeight.Value := RoundCP(FloatInUOM(RoomHeight, umMetr, TF_Main(GForm).FUOM));
  if FMakeEdit = meMake then
    neVolume.Value := 0
  else
  begin
    neVolume.Value := RoundCP(FloatInUOM(RoomHeight * FRoomSquare, umMetr, TF_Main(GForm).FUOM, 3));
  end;
end;
//Tolik
//function TF_ObjectParams.MakeEditRoom(AMakeEdit: TMakeEdit; var ARoomParams: TObjectParams; AListOwner: TSCSList): Boolean;
function TF_ObjectParams.MakeEditRoom(AMakeEdit: TMakeEdit; var ARoomParams: TObjectParams; AListOwner: TSCSList; AUserSquare: Boolean = False): Boolean;
//

var
  RoomSquare: Double;
  RoomVolume: Double;
  i: Integer;

begin
  //Tolik
  RoomParams := ARoomParams;
  //
  i := 0;
  Result := false;
  Caption := '';
  case AMakeEdit of
    meMake:
      begin
        Caption := cObjectParams_Msg1_1;
      end;
    meEdit:
      begin
        Caption := cObjectParams_Msg1_2;
      end;
    else
      Exit; ///// EXIT /////
  end;

  FMakeEdit := AMakeEdit;
  FListOwner := AListOwner;

  lbHeightCeiling.Caption := cObjectParams_Msg3 +', '+ GetNameUOM(TF_Main(GForm).FUOM, true);
  //lbHeightCeilingUOM.Caption := GetNameUOM(TF_Main(GForm).FUOM, true);
  lbHeight.Caption := cObjectParams_Msg6 +', '+ GetNameUOM(TF_Main(GForm).FUOM, true);
  lbSquare.Caption := cObjectParams_Msg4 +', '+ GetNameUOM2(TF_Main(GForm).FUOM);
  lbVolume.Caption := cObjectParams_Msg5 +', '+ GetNameUOM3(TF_Main(GForm).FUOM);

  edName.Text := ARoomParams.Name;
  edNameShort.Text := ARoomParams.NameShort;
  seCurrIndex.Value := ARoomParams.MarkID;
  neHeightCeiling.Value := RoundCP(FloatInUOM(ARoomParams.HeightCeiling, umMetr, TF_Main(GForm).FUOM));
  //neHeight.Value := RoundCP(FloatInUOM(AListOwner.Setting.HeightRoom, umMetr, TF_Main(GForm).FUOM));
  //Проверка на класс кабинета
  if not ARoomParams.CabinetConfig.IsCabinetExt  then
  begin
    CabinetEXTPoints.Visible := false;
    CabinetExtNumPos.Visible := false;
    CabinetNumPos.Visible := true;
    CabinetSortNamePos.Visible := true;
    if CabinetNumPos.Items[ARoomParams.CabinetConfig.CabinetNumPos] <> '' then
      CabinetNumPos.ItemIndex := ARoomParams.CabinetConfig.CabinetNumPos
    else
      CabinetNumPos.ItemIndex := 1;
    if CabinetSortNamePos.Items[ARoomParams.CabinetConfig.CabinetSignPos] <> '' then
      CabinetSortNamePos.ItemIndex := ARoomParams.CabinetConfig.CabinetSignPos
    else
      CabinetSortNamePos.ItemIndex := 1;
  end
  else
  begin
    CabinetEXTPoints.Items.Clear;
    CabinetExtNumPos.Items.Clear;
    CabinetEXTPoints.Items.Add(lng_Forms.GetText('DontShow'));
    CabinetExtNumPos.Items.Add(lng_Forms.GetText('DontShow'));
    CabinetExtNumPos.Items.Add(lng_Forms.GetText('DefaultValue'));
    for i := 1 to ARoomParams.CabinetConfig.PointCount do
      CabinetEXTPoints.Items.Add(Inttostr(i));
    for i := 1 to ARoomParams.CabinetConfig.PointCount do
      CabinetExtNumPos.Items.Add(Inttostr(i));
    CabinetEXTPoints.Visible := True;
    CabinetExtNumPos.Visible := true;
    CabinetNumPos.Visible := false;
    CabinetSortNamePos.Visible := false;
    if CabinetExtNumPos.Items[ARoomParams.CabinetConfig.CabinetNumPos] <> '' then
      CabinetExtNumPos.ItemIndex := ARoomParams.CabinetConfig.CabinetNumPos
    else
      CabinetExtNumPos.ItemIndex := 2;
    if CabinetEXTPoints.Items[ARoomParams.CabinetConfig.CabinetSignPos] <> '' then
      CabinetEXTPoints.ItemIndex := ARoomParams.CabinetConfig.CabinetSignPos
    else
      CabinetEXTPoints.ItemIndex := 1;
  end;
  CabinetsSize.ItemIndex := ARoomParams.CabinetConfig.NumRadius;

  //Галочки...
  if AMakeEdit = meMake then
  begin
    RzWorkRoom.Checked := True;
    RzPlenumArea.Checked := False;
    RzUnRoutableArea.Checked := False;
  end
  else
  begin
    RzWorkRoom.Checked := ARoomParams.CabinetConfig.aWorkRoom;
    RzPlenumArea.Checked := ARoomParams.CabinetConfig.aPlenumArea;
    RzUnRoutableArea.Checked := ARoomParams.CabinetConfig.aUnroutableArea;
  end;

  RoomSquare := 0;
  if AMakeEdit = meMake then
  begin
    neSquare.Value := 0;
    neVolume.Value := 0;
  end
  else
  begin
    RoomSquare := GetRoomSquare(AListOwner.SCSID, ARoomParams.ID);
    RoomVolume := GetRoomVolume(AListOwner.SCSID, ARoomParams.ID, RoomSquare);
    neSquare.Value := RoundCP(FloatInUOM(RoomSquare, umMetr, TF_Main(GForm).FUOM, 2));
    if aUserSquare then
      //neSquare.Color := clSkyBlue
      //neSquare.Color := TColor($FFFF00)
      neSquare.Color := TColor($C0C0C0)
    else
      neSquare.Color :=clWindow;
    //neVolume.Value := RoundCP(FloatInUOM(RoomVolume, umMetr, TF_Main(GForm).FUOM, 2));
  end;
  FRoomSquare := RoomSquare;

  DefineParamsRelatedToHeightCeiling;

  if ShowModal = mrOk then
  begin
    ARoomParams.Name := edName.Text;
    ARoomParams.NameShort := edNameShort.Text;
    ARoomParams.MarkID := Round(seCurrIndex.Value);
    ARoomParams.HeightCeiling := FloatInUOM(neHeightCeiling.Value, TF_Main(GForm).FUOM, umMetr);
    if ARoomParams.HeightCeiling > AListOwner.Setting.HeightRoom then
      ARoomParams.HeightCeiling := AListOwner.Setting.HeightRoom;
    ARoomParams.CabinetConfig.aWorkRoom := RzWorkRoom.Checked;
    ARoomParams.CabinetConfig.aPlenumArea := RzPlenumArea.Checked;
    ARoomParams.CabinetConfig.aUnroutableArea := RzUnRoutableArea.Checked;
    if not ARoomParams.CabinetConfig.IsCabinetExt then
    begin
      ARoomParams.CabinetConfig.CabinetNumPos := CabinetNumPos.ItemIndex;
      ARoomParams.CabinetConfig.CabinetSignPos := CabinetSortNamePos.ItemIndex;
    end
    else
    begin
      ARoomParams.CabinetConfig.CabinetNumPos := CabinetExtNumPos.ItemIndex;
      ARoomParams.CabinetConfig.CabinetSignPos := CabinetEXTPoints.ItemIndex;
    end;
      ARoomParams.CabinetConfig.NumRadius := CabinetsSize.ItemIndex;
    Result := true;
  end;
end;

procedure TF_ObjectParams.edNameChange(Sender: TObject);
begin
    {if TCustomEdit(Sender).Text = '' then
      btOk.Enabled := false
    else
      btCancel.Enabled := true;}
end;

procedure TF_ObjectParams.btOkClick(Sender: TObject);
begin
  if edName.Text = '' then
  begin
    ModalResult := mrNone;
    ShowMessageByType(Self.Handle, smtDisplay, cObjectParams_Msg2, Application.Title, MB_OK or MB_ICONINFORMATION);
  end;
end;

procedure TF_ObjectParams.GroupBox_OkCancelResize(Sender: TObject);
begin
  SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;

procedure TF_ObjectParams.FormCreate(Sender: TObject);
begin
  // Tolik 01/11/2019 --
  seCurrIndex.Min := 0;
  seCurrIndex.Max := 0;
  //
  GroupBox_OkCancelResize(GroupBox_OkCancel);
end;

procedure TF_ObjectParams.neHeightCeilingChange(Sender: TObject);
begin
  DefineParamsRelatedToHeightCeiling;
end;

procedure TF_ObjectParams.Image1Click(Sender: TObject);
begin
  TImage(Sender).ShowHint := False;
  ShowHintInCursorPos(TImage(Sender).Hint, 3000);
  TimerEnableHint.Enabled := True;
end;


procedure TF_ObjectParams.TimerEnableHintTimer(Sender: TObject);
begin
  TimerEnableHint.Enabled := False;
  Image1.ShowHint := True;
  Image2.ShowHint := True;
  Image3.ShowHint := True;
end;
//Tolik
procedure TF_ObjectParams.CabinetSquare_ChangeClick(Sender: TObject);
var RoomSquare: Double;
    currList: TSCSList;
begin
  neSquare.Value := 0;
  neVolume.Value := 0;
  RoomSquare := 0;
  currList := F_ProjMan.GSCSBase.CurrProject.CurrList;
  RoomSquare := GetRoomSquare(currList.SCSID, RoomParams.ID, True);
  neSquare.Value := RoundCP(FloatInUOM(RoomSquare, umMetr, TF_Main(GForm).FUOM, 2));
  neSquare.Color := clWindow;
  neVolume.Value := RoundCP(FloatInUOM(RoomSquare * neHeight.Value, umMetr, TF_Main(GForm).FUOM, 2));
end;

procedure TF_ObjectParams.FormShow(Sender: TObject);
begin
  CabinetSquareChanged := False;
end;

procedure TF_ObjectParams.N1Click(Sender: TObject);
var Cabinet: TFigure;
    NewValue: Double;
    OldValue: string;
    currList: TSCSList;
    ACadList: TF_CAD;
begin
  currList := F_ProjMan.GSCSBase.CurrProject.CurrList;
  NewValue := 0;
  if currList <> nil then
  begin
    ACadList := GetListbyID(currList.CurrID);
    if ACadList <> nil then
    begin
      Cabinet := FindCabinetBySCSID(ACadList, RoomParams.ID);
      if Cabinet <> nil then
      begin
        if CheckFigureByClassName(Cabinet, cTCabinet) then
        begin
          try
            NewValue := -1;
            OldValue := FloatToStr(GetRoomSquare(currList.SCSID, RoomParams.ID));
            //Tolik -- 26/09/2016 --
            //NewValue:= RoundCP(FloatInUOM(strtofloat(InputBox(cObjParamsMsg1, cObjParamsMsg2, OldValue)), umMetr, TF_Main(GForm).FUOM, 2));

            // Tolik -- 26/09/2016--
            // NewValue:= RoundCP(FloatInUOM(strtofloat(InputBox(cObjParamsMsg1, cObjParamsMsg2, OldValue)), TF_Main(GForm).FUOM, umMetr, 2));
            NewValue:= RoundCP(FloatInUOM(strtofloat(InputBox(cObjParamsMsg1, cObjParamsMsg2, FloatToStr(neSquare.Value))), TF_Main(GForm).FUOM, umMetr, 2));
            //
            if NewValue <> strtofloat(OldValue) then
            begin
              TCabinet(Cabinet).FCabinetSquare := NewValue;
              //neSquare.Value := RoundCP(FloatInUOM(NewValue, TF_Main(GForm).FUOM, umMetr,  2));
              neSquare.Value := RoundCP(FloatInUOM(NewValue, umMetr, TF_Main(GForm).FUOM, 2));
              //neSquare.Color := clSkyBlue;
              //neSquare.Color := TColor($FFFF00);
              neSquare.Color := TColor($C0C0C0);
              neVolume.Value := RoundCP(FloatInUOM((NewValue * neHeight.Value), umMetr, TF_Main(GForm).FUOM, 2));
               if not GProjectChanged then // Tolik 28/08/2019 --
                SetProjectChanged(true);
            end;
          except
            on E: Exception do
            begin
              TCabinet(Cabinet).FCabinetSquare := strtofloat(OldValue);
              addExceptionToLogEx('TF_ObjectParams.USER_CabinetSquare_Change', E.Message);
            end;
          end;
        end
        else
        begin
          if CheckFigureByClassName(Cabinet, cTCabinetExt) then
          begin
            try
              NewValue := -1;
              OldValue := floattostr(GetRoomSquare(currList.SCSID, RoomParams.ID));
              NewValue:= RoundCP(FloatInUOM(strtofloat(InputBox(cObjParamsMsg1, cObjParamsMsg2, OldValue)), umMetr, TF_Main(GForm).FUOM, 2));
              if NewValue <> strtofloat(OldValue) then
              begin
                TCabinetExt(Cabinet).FCabinetSquare :=  NewValue;
                neSquare.Value := NewValue;
                //neSquare.Color := clSkyBlue;
                //neSquare.Color := TColor($FFFF00);
                neSquare.Color := TColor($C0C0C0);
                neVolume.Value := RoundCP(FloatInUOM((NewValue * neHeight.Value), umMetr, TF_Main(GForm).FUOM, 2));
                if not GProjectChanged then // Tolik 28/08/2019 --
                  SetProjectChanged(true);
              end;
            except
              on E: Exception do
              begin
                TCabinetExt(Cabinet).FCabinetSquare := strtofloat(OldValue);
                addExceptionToLogEx('TF_ObjectParams.USER_CabinetSquare_Change', E.Message);
              end;
            end;
          end;
        end;
      end;
    end;
  end;
end;

procedure TF_ObjectParams.SetNewSquare_ValueClick(Sender: TObject);
begin
  N1Click(Sender);
end;

end.
