From 4d81db31f038edecb0ff399802a8e430391e5b1b Mon Sep 17 00:00:00 2001 From: Yoann Audouin Date: Tue, 27 Jul 2021 15:00:32 +0200 Subject: [PATCH] Added Mini viewer --- src/DummyApplication/CMakeLists.txt | 12 +++- src/DummyApplication/DummyDesktop.cxx | 43 +++++++++++--- src/DummyApplication/DummyDesktop.h | 3 + src/DummyApplication/MiniViewer.cxx | 85 +++++++++++++++++++++++++++ src/DummyApplication/MiniViewer.h | 53 +++++++++++++++++ 5 files changed, 186 insertions(+), 10 deletions(-) create mode 100644 src/DummyApplication/MiniViewer.cxx create mode 100644 src/DummyApplication/MiniViewer.h diff --git a/src/DummyApplication/CMakeLists.txt b/src/DummyApplication/CMakeLists.txt index 7161cb67f..5f2ae890c 100644 --- a/src/DummyApplication/CMakeLists.txt +++ b/src/DummyApplication/CMakeLists.txt @@ -24,6 +24,9 @@ INCLUDE(UseQtExt) # additional include directories INCLUDE_DIRECTORIES( ${QT_INCLUDES} + ${PROJECT_SOURCE_DIR}/src/SUIT + ${PROJECT_SOURCE_DIR}/src/VTKViewer + ${OpenCASCADE_INCLUDE_DIR} ${PROJECT_SOURCE_DIR}/src/Qtx ${PROJECT_SOURCE_DIR}/src/ObjBrowser ) @@ -32,7 +35,12 @@ INCLUDE_DIRECTORIES( ADD_DEFINITIONS(${QT_DEFINITIONS}) # libraries to link to -SET(_link_LIBRARIES ${PLATFORM_LIBS} ${QT_LIBRARIES} qtx ObjBrowser) +SET(_link_LIBRARIES + ${PLATFORM_LIBS} + ${QT_LIBRARIES} + qtx + ObjBrowser + VTK::GUISupportQt) # --- headers --- @@ -40,6 +48,7 @@ SET(_link_LIBRARIES ${PLATFORM_LIBS} ${QT_LIBRARIES} qtx ObjBrowser) SET(_moc_HEADERS DummyApplication.h DummyDesktop.h + MiniViewer.h ) # header files / no moc processing @@ -75,6 +84,7 @@ QT_WRAP_MOC(_moc_SOURCES ${_moc_HEADERS}) SET(_other_SOURCES DummyApplication.cxx DummyDesktop.cxx + MiniViewer.cxx ) # sources / to compile diff --git a/src/DummyApplication/DummyDesktop.cxx b/src/DummyApplication/DummyDesktop.cxx index 0b5a394a8..42c97596b 100644 --- a/src/DummyApplication/DummyDesktop.cxx +++ b/src/DummyApplication/DummyDesktop.cxx @@ -25,6 +25,8 @@ #include "utilities.h" +#include + #include #include @@ -60,8 +62,10 @@ DummyDesktop::DummyDesktop() myMenuMgr = new QtxActionMenuMgr( this ); myToolMgr = new QtxActionToolMgr( this ); myLogoMgr = new QtxLogoMgr( menuBar() ); + myViewer = new MiniViewer(this); createMenus(); + } /*! @@ -77,17 +81,10 @@ void DummyDesktop::createMenus() //QPushButton *mybutton = new QPushButton("Test", this); //connect(mybutton, &QPushButton::released, this, &DummyDesktop::RunScript); - //this->setCentralWidget(mybutton); + this->setCentralWidget(myViewer); QMenu *fileMenu = menuBar()->addMenu("File"); fileMenu->addAction("Open File", this, &DummyDesktop::RunScript, QKeySequence::Open); - fileMenu->addAction("Dummy", this, SLOT( Dummy()), QKeySequence::New); -} - -void DummyDesktop::Dummy() -{ - QMessageBox msgBox; - msgBox.setText("Dummy text"); - msgBox.exec(); + fileMenu->addAction("Open Mesh File", this, &DummyDesktop::OpenMeshFile ); } void DummyDesktop::RunScript() @@ -125,3 +122,31 @@ void DummyDesktop::RunScript() // Release GIL PyGILState_Release(gstate); } + +void DummyDesktop::OpenMeshFile() +{ + // Getting file to display + QString fileName = QFileDialog::getOpenFileName(this, tr("Open file"), "", + "VTK Files (*.vtk)"); + + // Open file + QFile file(fileName); + file.open(QIODevice::ReadOnly); + + // Return on Cancel + if (!file.exists()) + return; + + // Create reader + vtkSmartPointer reader = vtkSmartPointer::New(); + reader->SetFileName(fileName.toStdString().c_str()); + + // Read the file + reader->Update(); + + // Add data set to 3D view + vtkSmartPointer dataSet = reader->GetOutput(); + if (dataSet != nullptr) { + myViewer->addDataSet(reader->GetOutput()); + } +} \ No newline at end of file diff --git a/src/DummyApplication/DummyDesktop.h b/src/DummyApplication/DummyDesktop.h index 3225b4b65..92dcbffaa 100644 --- a/src/DummyApplication/DummyDesktop.h +++ b/src/DummyApplication/DummyDesktop.h @@ -23,6 +23,7 @@ #define DUMMYDESKTOP_H #include +#include "MiniViewer.h" class QtxLogoMgr; class QtxActionMenuMgr; @@ -38,12 +39,14 @@ public: void RunScript(); void Dummy(); + void OpenMeshFile(); void createMenus(); private: QtxActionMenuMgr* myMenuMgr; QtxActionToolMgr* myToolMgr; QtxLogoMgr* myLogoMgr; + MiniViewer* myViewer; }; #endif diff --git a/src/DummyApplication/MiniViewer.cxx b/src/DummyApplication/MiniViewer.cxx new file mode 100644 index 000000000..ac75e361b --- /dev/null +++ b/src/DummyApplication/MiniViewer.cxx @@ -0,0 +1,85 @@ +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "MiniViewer.h" + +#include +#include +#include +#include +#include + +MiniViewer::MiniViewer(QWidget* parent) + : QVTKOpenGLNativeWidget(parent) +{ + vtkNew window; + setRenderWindow(window.Get()); + + // Camera + vtkSmartPointer camera = vtkSmartPointer::New(); + camera->SetViewUp(0, 1, 0); + camera->SetPosition(0, 0, 10); + camera->SetFocalPoint(0, 0, 0); + + // Renderer + m_renderer = vtkSmartPointer::New(); + m_renderer->SetActiveCamera(camera); + m_renderer->SetBackground(0.5, 0.5, 0.5); + renderWindow()->AddRenderer(m_renderer); +} + +void MiniViewer::addDataSet(vtkSmartPointer dataSet) +{ + // Actor + vtkSmartPointer actor = vtkSmartPointer::New(); + + // Mapper + vtkSmartPointer mapper = vtkSmartPointer::New(); + mapper->SetInputData(dataSet); + actor->SetMapper(mapper); + + m_renderer->AddActor(actor); + m_renderer->ResetCamera(dataSet->GetBounds()); + + renderWindow()->Render(); +} + +void MiniViewer::removeDataSet() +{ + vtkActor* actor = m_renderer->GetActors()->GetLastActor(); + if (actor != nullptr) { + m_renderer->RemoveActor(actor); + } + + renderWindow()->Render(); +} + +void MiniViewer::zoomToExtent() +{ + // Zoom to extent of last added actor + vtkSmartPointer actor = m_renderer->GetActors()->GetLastActor(); + if (actor != nullptr) { + m_renderer->ResetCamera(actor->GetBounds()); + } + + renderWindow()->Render(); +} \ No newline at end of file diff --git a/src/DummyApplication/MiniViewer.h b/src/DummyApplication/MiniViewer.h new file mode 100644 index 000000000..7f556d451 --- /dev/null +++ b/src/DummyApplication/MiniViewer.h @@ -0,0 +1,53 @@ +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#ifndef MINIVIEWER_H +#define MINIVIEWER_H + +#include +#include +#include +#include + +class MiniViewer : public QVTKOpenGLNativeWidget { + Q_OBJECT +public: + explicit MiniViewer(QWidget* parent = nullptr); + + //! Add a data set to the scene + /*! + \param[in] dataSet The data set to add + */ + void addDataSet(vtkSmartPointer dataSet); + + //! Remove the data set from the scene + void removeDataSet(); + +public slots: + //! Zoom to the extent of the data set in the scene + void zoomToExtent(); + +private: + vtkSmartPointer m_renderer; +}; + +#endif // MINIVIEWER_H \ No newline at end of file -- 2.39.2