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,
  cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer,
  cxEdit, cxCheckBox;

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;
    cbDesignPos: TcxCheckBox;
    cbNumPos: TcxCheckBox;
    cbFontSize: TcxCheckBox;
    cbCeilingHeightCopy: TcxCheckBox;
    cbCabinetCopy: TcxCheckBox;
    cbAreaCopy: TcxCheckBox;
    cbTabooAreaCopy: TcxCheckBox;
    cbDesignation: TcxCheckBox;
    GroupBox1: TGroupBox;
    cbApplySelected: TCheckBox;
    cbApplyByList: TCheckBox;
    cbApplyByProj: TCheckBox;
    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);
    procedure cbApplySelectedClick(Sender: TObject);
    procedure cbApplyByListClick(Sender: TObject);
    procedure cbApplyByProjClick(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;
    Procedure ShowApplyButtons;
    Procedure HideApplyButtons(aClear: Boolean = False);
    Procedure SwitchApplyParams(ObjectFrom: Integer);
  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.ShowApplyButtons;  // Tolik 15/09/2020 --
begin
  cbDesignation.Visible := True;
  cbDesignPos.Visible := True;
  cbNumPos.Visible := True;
  cbFontSize.Visible := True;

  cbCeilingHeightCopy.Visible := True;
  cbCabinetCopy.Visible := True;
  cbAreaCopy.Visible := True;
  cbTabooAreaCopy.Visible := True;
end;

Procedure TF_ObjectParams.HideApplyButtons(aClear: Boolean);  // Tolik 15/09/2020 --
begin
  cbDesignation.Visible := False;
  cbDesignPos.Visible := False;
  cbNumPos.Visible := False;
  cbFontSize.Visible := False;
  cbCeilingHeightCopy.Visible := False;
  cbCabinetCopy.Visible := False;
  cbAreaCopy.Visible := False;
  cbTabooAreaCopy.Visible := False;

  if aClear then
  begin
    cbDesignation.Checked := False;
    cbDesignPos.Checked := False;
    cbNumPos.Checked := False;
    cbFontSize.Checked := False;
    cbCeilingHeightCopy.Checked := False;
    cbCabinetCopy.Checked := False;
    cbAreaCopy.Checked := False;
    cbTabooAreaCopy.Checked := False;
  end;
end;

Procedure TF_ObjectParams.SwitchApplyParams(ObjectFrom: Integer); // Tolik 16/09/2020
var ShowButtons: Boolean;
begin

  ShowButtons := False;

  case ObjectFrom of
    1: begin
         if cbApplySelected.Checked then
         begin
           cbApplyByList.Checked := False;
           cbApplyByProj.Checked := False;
         end;

         ShowButtons := cbApplySelected.Checked;
       end;

    2: begin
         if cbApplyByList.Checked then
         begin
           cbApplySelected.Checked := False;
           cbApplyByProj.Checked := False;
         end;

         ShowButtons := cbApplyByList.Checked;
       end;

    3: begin
         if cbApplyByProj.Checked then
         begin
           cbApplySelected.Checked := False;
           cbApplyByList.Checked := False;
         end;

         ShowButtons := cbApplyByProj.Checked;
       end;
  end;

  if (not cbApplySelected.Checked) and (not cbApplyByList.Checked) and (not cbApplyByProj.Checked) then
    HideApplyButtons
  else
  begin
    if ShowButtons then
      if not cbDesignation.visible then
        ShowApplyButtons;
  end;
end;

procedure TF_ObjectParams.cbApplyByListClick(Sender: TObject);
begin
  SwitchApplyParams(2);
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.cbApplyByProjClick(Sender: TObject);
begin
  SwitchApplyParams(3);
end;

procedure TF_ObjectParams.cbApplySelectedClick(Sender: TObject);
begin
  SwitchApplyParams(1);
end;

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

  cbApplySelected.Checked := False;
  cbApplyByList.Checked := False;
  cbApplyByProj.Checked := 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_My(InputBox(cObjParamsMsg1, cObjParamsMsg2, OldValue)), umMetr, TF_Main(GForm).FUOM, 2));

            // Tolik -- 26/09/2016--
            // NewValue:= RoundCP(FloatInUOM(StrToFloat_My(InputBox(cObjParamsMsg1, cObjParamsMsg2, OldValue)), TF_Main(GForm).FUOM, umMetr, 2));
            NewValue:= RoundCP(FloatInUOM(StrToFloat_My(InputBox(cObjParamsMsg1, cObjParamsMsg2, FloatToStr(neSquare.Value))), TF_Main(GForm).FUOM, umMetr, 2));
            //
            if NewValue <> StrToFloat_My(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_My(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_My(InputBox(cObjParamsMsg1, cObjParamsMsg2, OldValue)), umMetr, TF_Main(GForm).FUOM, 2));
              if NewValue <> StrToFloat_My(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_My(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.
