// Copyright (c) 2020 OPEN CASCADE SAS
//
// This file is part of the examples of the Open CASCADE Technology software library.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

#include "GeomWidget.h"

#include <opencascade/Standard_WarningsDisable.hxx>
#include <QFrame>
#include <QBoxLayout>
#include <QTextEdit>
#include <QStackedLayout>
#include <QToolBar>
#include <QStackedWidget>
#include <opencascade/Standard_WarningsRestore.hxx>
#include <opencascade/Quantity_Color.hxx>
#include <opencascade/AIS_Shape.hxx>
 

GeomWidget::GeomWidget (DocumentCommon* theDocument3d, QWidget* theParent)
: QWidget(theParent),
  myDocument3d(theDocument3d)
{
  QVBoxLayout* aMainLayout = new QVBoxLayout(this);
  aMainLayout->setContentsMargins(0, 0, 0, 0);


  this->my3dVidget = new QWidget;// 3D模型 显示

  QVBoxLayout* a3dLayout = new QVBoxLayout(my3dVidget);
  a3dLayout->setContentsMargins(0, 0, 0, 0);
  a3dLayout->setSpacing(0);
  myView3d = new View(myDocument3d->getContext(), true, my3dVidget); // 创建一个 3D 模型 View
  
  QToolBar* aToolBar3d = new QToolBar;
  aToolBar3d->addActions(myView3d->getViewActions());
  aToolBar3d->addSeparator();
  aToolBar3d->addActions(myView3d->getRaytraceActions());
  a3dLayout->addWidget(aToolBar3d);
  a3dLayout->addWidget(myView3d);
  myStackWidget = new QStackedWidget(this);
  aMainLayout->addWidget(myStackWidget);
  myStackWidget->addWidget(my3dVidget);
  this->initEvents();
  FitAll();
}

void GeomWidget::FitAll()
{
    Show3d();
}

void GeomWidget::Show3d()
{
  myView3d->axo();
  myView3d->fitAll();
  QAction* aShadingAction = myView3d->getViewAction(ViewAction_Shading);
  aShadingAction->trigger();
  aShadingAction->setChecked(true);
  QAction* aHlrOffAction = myView3d->getViewAction(ViewAction_HlrOff);
  aHlrOffAction->trigger();
  aHlrOffAction->setChecked(true);
  myStackWidget->setCurrentWidget(my3dVidget);
  setStatusTip("Mouse buttons: Right-Zoom, Middle-Pan, Left-Rotate");
}

void GeomWidget::initEvents()
{
    QObject::connect(this->myView3d, SIGNAL(ViewMousePressEvent(QMouseEvent *)), this->myDocument3d, SLOT(ViewerMousePressEvent(QMouseEvent *)));
    QObject::connect(this->myView3d, SIGNAL(ViewMouseReleaseEvent(QMouseEvent*)), this->myDocument3d, SLOT(ViewerMouseReleaseEvent(QMouseEvent*)));
    QObject::connect(this->myDocument3d, SIGNAL(ShowSelectItemFullExtend(const TopoDS_Shape&)), this, SLOT(ShowExtend(const TopoDS_Shape&)));

}
 

void GeomWidget::ShowExtend(const TopoDS_Shape& Data_Shape) {
    // 缩放到选定的模型
    qDebug() << "缩放到选定的模型";
    this->myDocument3d->myContext->FitSelected(this->Get3dView()); // 缩放到选择图层
    this->myDocument3d->myContext->ClearSelected(Standard_True);

}