ResInsight/ApplicationCode/Commands/IntersectionBoxCommands/RicBoxManipulatorEventHandler.cpp

157 lines
4.8 KiB
C++
Raw Normal View History

#include "RicBoxManipulatorEventHandler.h"
2016-09-29 04:43:47 -05:00
#include "cafBoxManipulatorPartManager.h"
#include "cafEffectGenerator.h"
2016-09-29 04:43:47 -05:00
#include "cafViewer.h"
2016-09-29 04:43:47 -05:00
#include "cvfCamera.h"
#include "cvfDrawableGeo.h"
#include "cvfHitItemCollection.h"
2016-09-29 04:43:47 -05:00
#include "cvfModelBasicList.h"
#include "cvfPart.h"
#include "cvfRay.h"
2016-09-29 04:43:47 -05:00
#include <QDebug>
#include <QMouseEvent>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicBoxManipulatorEventHandler::RicBoxManipulatorEventHandler(caf::Viewer* viewer)
: m_viewer(viewer)
{
m_partManager = new caf::BoxManipulatorPartManager;
m_model = new cvf::ModelBasicList;
2016-09-29 04:43:47 -05:00
m_viewer->installEventFilter(this);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2016-09-29 04:43:47 -05:00
RicBoxManipulatorEventHandler::~RicBoxManipulatorEventHandler()
{
2016-09-29 04:43:47 -05:00
m_viewer->removeEventFilter(this);
2016-09-29 04:43:47 -05:00
// Make sure the model owned by this manipulator is not used anywhere else
CVF_ASSERT(m_model->refCount() == 1);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2016-09-29 04:43:47 -05:00
void RicBoxManipulatorEventHandler::setOrigin(const cvf::Vec3d& origin)
{
2016-09-29 04:43:47 -05:00
m_partManager->setOrigin(origin);
2016-09-29 04:43:47 -05:00
updateParts();
emit notifyRedraw();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicBoxManipulatorEventHandler::setSize(const cvf::Vec3d& size)
{
2016-09-29 04:43:47 -05:00
m_partManager->setSize(size);
updateParts();
emit notifyRedraw();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Model* RicBoxManipulatorEventHandler::model()
{
return m_model.p();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicBoxManipulatorEventHandler::eventFilter(QObject *obj, QEvent* inputEvent)
{
if (inputEvent->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(inputEvent);
2016-09-29 04:43:47 -05:00
if (mouseEvent->button() == Qt::LeftButton)
{
2016-09-29 04:43:47 -05:00
cvf::HitItemCollection hitItems;
if (m_viewer->rayPick(mouseEvent->x(), mouseEvent->y(), &hitItems))
{
m_partManager->tryToActivateManipulator(hitItems.firstItem());
if(m_partManager->isManipulatorActive())
{
updateParts();
emit notifyRedraw();
return true;
}
}
2016-09-29 04:43:47 -05:00
}
}
else if (inputEvent->type() == QEvent::MouseMove)
{
if (m_partManager->isManipulatorActive())
2016-09-29 04:43:47 -05:00
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(inputEvent);
//qDebug() << "Inside mouse move";
//qDebug() << mouseEvent->pos();
int translatedMousePosX = mouseEvent->pos().x();
int translatedMousePosY = m_viewer->height() - mouseEvent->pos().y();
2016-09-29 04:43:47 -05:00
cvf::ref<cvf::Ray> ray = m_viewer->mainCamera()->rayFromWindowCoordinates(translatedMousePosX, translatedMousePosY);
{
m_partManager->updateManipulatorFromRay(ray.p());
2016-09-29 04:43:47 -05:00
updateParts();
2016-09-29 04:43:47 -05:00
cvf::Vec3d origin;
cvf::Vec3d size;
m_partManager->originAndSize(&origin, &size);
2016-09-29 04:43:47 -05:00
emit notifyUpdate(origin, size);
emit notifyRedraw();
return true;
}
}
}
2016-09-29 04:43:47 -05:00
else if (inputEvent->type() == QEvent::MouseButtonRelease)
{
if (m_partManager->isManipulatorActive())
2016-09-29 04:43:47 -05:00
{
m_partManager->endManipulator();
2016-09-29 04:43:47 -05:00
cvf::Vec3d origin;
cvf::Vec3d size;
m_partManager->originAndSize(&origin, &size);
2016-09-29 04:43:47 -05:00
emit notifyUpdate(origin, size);
2016-09-29 04:43:47 -05:00
return true;
}
}
2016-09-29 04:43:47 -05:00
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicBoxManipulatorEventHandler::updateParts()
{
m_model->removeAllParts();
m_partManager->appendPartsToModel(m_model.p());
}