paysages3d/src/interface/modeler/quickapp/OpenGLView.cpp

140 lines
2.7 KiB
C++
Raw Normal View History

#include "OpenGLView.h"
#include <QQuickWindow>
2014-08-27 16:19:48 +00:00
#include <QHoverEvent>
2014-08-28 08:29:12 +00:00
#include "MainModelerWindow.h"
#include "OpenGLRenderer.h"
2014-09-26 15:46:39 +00:00
#include "ModelerCameras.h"
OpenGLView::OpenGLView(QQuickItem *parent) :
QQuickItem(parent)
{
initialized = false;
2014-08-27 16:19:48 +00:00
window = NULL;
2014-08-28 08:29:12 +00:00
renderer = NULL;
2014-08-27 16:19:48 +00:00
setAcceptedMouseButtons(Qt::AllButtons);
2014-09-26 15:46:39 +00:00
mouse_button = Qt::NoButton;
2014-08-27 16:19:48 +00:00
connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
startTimer(50);
}
void OpenGLView::handleWindowChanged(QQuickWindow *win)
{
if (win)
{
2014-08-28 08:29:12 +00:00
window = qobject_cast<MainModelerWindow *>(win);
if (window)
{
renderer = window->getRenderer();
2014-08-27 16:19:48 +00:00
2014-08-28 08:29:12 +00:00
connect(win, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
2014-08-28 08:29:12 +00:00
win->setClearBeforeRendering(false);
2014-08-28 08:29:12 +00:00
initialized = false;
}
}
}
void OpenGLView::paint()
{
2014-09-26 15:46:39 +00:00
if (not initialized or not renderer)
{
2014-08-28 08:29:12 +00:00
renderer->initialize();
initialized = true;
}
2014-08-28 08:29:12 +00:00
renderer->resize(width(), height());
renderer->prepareOpenGLState();
renderer->paint();
2014-08-27 16:19:48 +00:00
if (window)
{
window->resetOpenGLState();
}
}
2014-09-26 15:46:39 +00:00
void OpenGLView::wheelEvent(QWheelEvent *event)
2014-08-27 16:19:48 +00:00
{
if (not acceptInputs())
{
return;
}
2015-07-22 19:30:56 +00:00
double factor = getSpeedFactor(event);
window->getCamera()->processZoom(0.1 * factor * (double)event->angleDelta().y());
2014-09-26 15:46:39 +00:00
}
2014-08-28 08:29:12 +00:00
2014-09-26 15:46:39 +00:00
void OpenGLView::mousePressEvent(QMouseEvent *event)
{
if (not acceptInputs())
{
return;
}
2014-09-26 15:46:39 +00:00
mouse_button = event->button();
mouse_pos = event->windowPos();
}
2014-08-27 16:19:48 +00:00
2014-09-26 15:46:39 +00:00
void OpenGLView::mouseReleaseEvent(QMouseEvent *)
{
if (not acceptInputs())
{
return;
}
2014-09-26 15:46:39 +00:00
mouse_button = Qt::NoButton;
}
2014-08-27 16:19:48 +00:00
2014-09-26 15:46:39 +00:00
void OpenGLView::mouseMoveEvent(QMouseEvent *event)
{
if (not acceptInputs())
{
return;
}
2015-07-22 19:30:56 +00:00
double factor = getSpeedFactor(event);
2014-09-26 15:46:39 +00:00
QPointF diff = event->windowPos() - mouse_pos;
2015-07-22 16:05:24 +00:00
if (mouse_button == Qt::LeftButton)
2014-08-27 16:19:48 +00:00
{
2015-07-22 19:30:56 +00:00
window->getCamera()->processPanning(0.006 * factor * diff.x(), 0.002 * factor * diff.y());
2014-09-26 15:46:39 +00:00
}
2015-07-22 16:05:24 +00:00
else if (mouse_button == Qt::RightButton)
{
2015-07-22 19:30:56 +00:00
window->getCamera()->processScroll(-0.1 * factor * diff.x(), 0.1 * factor * diff.y());
2015-07-22 16:05:24 +00:00
}
2014-09-26 15:46:39 +00:00
mouse_pos = event->windowPos();
2014-08-27 16:19:48 +00:00
}
void OpenGLView::timerEvent(QTimerEvent *)
{
if (window)
{
window->update();
}
}
2015-07-22 19:30:56 +00:00
bool OpenGLView::acceptInputs() const
{
return window->getState() != "Render Dialog";
}
2015-07-22 19:30:56 +00:00
double OpenGLView::getSpeedFactor(QInputEvent *event)
{
if (event->modifiers() & Qt::ControlModifier)
{
return 0.2;
}
else if (event->modifiers() & Qt::ShiftModifier)
{
return 3.0;
}
else
{
return 1.0;
}
}