You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWTAI-Platform/Src/nmNum/nmRender/nmVTKInteractionManager.cpp

122 lines
3.1 KiB
C++

#include "nmVTKInteractionManager.h"
#include <QCoreApplication>
#include <QThread>
#include <vtkInteractorStyle.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
namespace {
// InteractorStyle 会注册 VTK 观察关系,绑定与解除都限定在 GUI 线程。
bool isInteractionGuiThread()
{
QCoreApplication* pApplication = QCoreApplication::instance();
return pApplication == NULL ||
pApplication->thread() == QThread::currentThread();
}
}
nmVTKInteractionManager::nmVTKInteractionManager()
: m_pInteractor(NULL),
m_pDefaultStyle(
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New()),
m_pActiveStyle(NULL)
{
Q_ASSERT(isInteractionGuiThread());
}
nmVTKInteractionManager::~nmVTKInteractionManager()
{
Q_ASSERT(isInteractionGuiThread());
// Interactor 持有 Style 引用,必须先清除绑定再释放智能指针。
unbindInteractor();
m_pActiveStyle = NULL;
m_pDefaultStyle = NULL;
}
bool nmVTKInteractionManager::bindInteractor(
vtkRenderWindowInteractor* pInteractor)
{
Q_ASSERT(isInteractionGuiThread());
if(!isInteractionGuiThread() || pInteractor == NULL ||
m_pDefaultStyle == NULL) {
return false;
}
if(m_pInteractor != pInteractor) {
// 切换对象前先清除旧 Interactor 持有的 Style 引用。
unbindInteractor();
m_pInteractor = pInteractor;
}
return restoreDefaultInteraction();
}
bool nmVTKInteractionManager::installInteractionStyle(
vtkInteractorStyle* pStyle)
{
Q_ASSERT(isInteractionGuiThread());
if(!isInteractionGuiThread() || m_pInteractor == NULL || pStyle == NULL) {
return false;
}
// 管理器和 Interactor 各持有一个 VTK 引用,直到恢复或清除当前模式。
m_pActiveStyle = pStyle;
m_pInteractor->SetInteractorStyle(m_pActiveStyle);
return true;
}
bool nmVTKInteractionManager::restoreDefaultInteraction()
{
Q_ASSERT(isInteractionGuiThread());
if(!isInteractionGuiThread() || m_pInteractor == NULL ||
m_pDefaultStyle == NULL) {
return false;
}
// 后续工具临时替换样式后,可通过本入口回到唯一默认交互模式。
return installInteractionStyle(m_pDefaultStyle);
}
void nmVTKInteractionManager::clearInteractionStyle()
{
Q_ASSERT(isInteractionGuiThread());
if(!isInteractionGuiThread()) {
return;
}
if(m_pInteractor != NULL) {
m_pInteractor->SetInteractorStyle(NULL);
}
m_pActiveStyle = NULL;
}
void nmVTKInteractionManager::unbindInteractor()
{
Q_ASSERT(isInteractionGuiThread());
if(!isInteractionGuiThread()) {
return;
}
// 本管理器定义单一活动模式边界,解绑时不保留任何旧 Style。
clearInteractionStyle();
m_pInteractor = NULL;
}
vtkRenderWindowInteractor* nmVTKInteractionManager::getInteractor() const
{
return m_pInteractor;
}
vtkInteractorStyleTrackballCamera*
nmVTKInteractionManager::getDefaultStyle() const
{
return m_pDefaultStyle;
}
vtkInteractorStyle* nmVTKInteractionManager::getActiveStyle() const
{
return m_pActiveStyle;
}