From 31fc89690d50704f68620ad9d5fdcdd74e27b054 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Tue, 14 Jan 2014 00:46:14 +0100 Subject: [PATCH] ask the user whether to stop a rendering process when accidentally dragging the mouse in an ongoing rendering session --- include/mitsuba/render/renderjob.h | 9 ++++++++ src/libpython/render.cpp | 4 +++- src/mtsgui/glwidget.cpp | 37 ++++++++++++++++++++++++++---- src/mtsgui/glwidget.h | 1 + 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/include/mitsuba/render/renderjob.h b/include/mitsuba/render/renderjob.h index bd908607..85eff44e 100644 --- a/include/mitsuba/render/renderjob.h +++ b/include/mitsuba/render/renderjob.h @@ -98,6 +98,15 @@ public: /// Get a pointer to the underlying scene (const version) inline const Scene *getScene() const { return m_scene.get(); } + /// Get a pointer to the underlying render queue + inline RenderQueue *getRenderQueue() { return m_queue.get(); } + + /// Get a pointer to the underlying render queue (const version) + inline const RenderQueue *getRenderQueue() const { return m_queue.get(); } + + /// Return the amount of time spent rendering the given job (in seconds) + inline Float getRenderTime() const { return m_queue->getRenderTime(this); } + MTS_DECLARE_CLASS() protected: /// Virtual destructor diff --git a/src/libpython/render.cpp b/src/libpython/render.cpp index 1fdb80f1..f598d0d9 100644 --- a/src/libpython/render.cpp +++ b/src/libpython/render.cpp @@ -362,12 +362,14 @@ void export_render() { .staticmethod("loadScene"); Scene *(RenderJob::*renderJob_getScene)(void) = &RenderJob::getScene; + RenderQueue *(RenderJob::*renderJob_getRenderQueue)(void) = &RenderJob::getRenderQueue; BP_CLASS(RenderJob, Thread, (bp::init())) .def(bp::init >()) .def("flush", &RenderJob::flush) .def("cancel", renderJob_cancel) .def("wait", &RenderJob::wait) - .def("getScene", renderJob_getScene, BP_RETURN_VALUE); + .def("getScene", renderJob_getScene, BP_RETURN_VALUE) + .def("getRenderQueue", renderJob_getRenderQueue, BP_RETURN_VALUE); BP_CLASS(RenderQueue, Object, bp::init<>()) .def("getJobCount", &RenderQueue::getJobCount) diff --git a/src/mtsgui/glwidget.cpp b/src/mtsgui/glwidget.cpp index 19432309..a620c84a 100644 --- a/src/mtsgui/glwidget.cpp +++ b/src/mtsgui/glwidget.cpp @@ -386,13 +386,11 @@ void GLWidget::focusOutEvent(QFocusEvent *event) { m_movementTimer->stop(); } - void GLWidget::timerImpulse() { if (!m_context || !m_context->scene || !m_preview->isRunning()) { m_movementTimer->stop(); return; } - if (m_animation) { Float x = std::min(m_animationTimer->getMilliseconds() / 500.0f, 1.0f); Float t = x*x*x*(x*(x*6-15)+10); // smootherstep by Ken Perlin @@ -453,6 +451,23 @@ void GLWidget::timerImpulse() { resetPreview(); } +bool GLWidget::askReallyCancelRendering() { + try { + Float renderTime = m_context->renderJob->getRenderTime(); + + if (renderTime < 10) /* Only ask for jobs that have been rendering for a bit */ + return true; + } catch (...) { + return true; + } + + bool cancel = QMessageBox::question(this, + "Really cancel rendering?", "Camera motion detected. Do you really want to cancel the rendering?", + QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes; + + return cancel; +} + void GLWidget::resetPreview() { if (!m_context || !m_context->scene || !m_preview->isRunning()) return; @@ -543,6 +558,12 @@ void GLWidget::keyPressEvent(QKeyEvent *event) { } if (!m_movementTimer->isActive() && (m_leftKeyDown || m_rightKeyDown || m_upKeyDown || m_downKeyDown)) { + + if (m_context->renderJob && !askReallyCancelRendering()) { + m_leftKeyDown = m_rightKeyDown = m_upKeyDown = m_downKeyDown = false; + return; + } + m_clock->reset(); m_movementTimer->start(); } @@ -615,6 +636,12 @@ void GLWidget::mouseMoveEvent(QMouseEvent *event) { nearClip = camera->getNearClip(), farClip = camera->getFarClip(); + bool setCursor = true; + if (m_context->renderJob) { + if (!askReallyCancelRendering()) + return; + setCursor = false; + } if (focusDistance <= nearClip || focusDistance >= farClip) { focusDistance = autoFocus(); camera->setFocusDistance(focusDistance); @@ -623,7 +650,7 @@ void GLWidget::mouseMoveEvent(QMouseEvent *event) { Point target = p + d * focusDistance; Vector up = m_context->up; - if (!m_didSetCursor) { + if (!m_didSetCursor && setCursor) { QApplication::setOverrideCursor(Qt::BlankCursor); m_didSetCursor = true; } @@ -828,11 +855,13 @@ void GLWidget::wheelEvent(QWheelEvent *event) { } else { if (!m_preview->isRunning() || m_context == NULL || m_context->scene == NULL || m_animation) return; - PerspectiveCamera *camera = getPerspectiveCamera(); if (!camera) return; + if (m_context->renderJob && !askReallyCancelRendering()) + return; + Float focusDistance = camera->getFocusDistance(), nearClip = camera->getNearClip(), farClip = camera->getFarClip(); diff --git a/src/mtsgui/glwidget.h b/src/mtsgui/glwidget.h index 16c8d8c1..65e50217 100644 --- a/src/mtsgui/glwidget.h +++ b/src/mtsgui/glwidget.h @@ -127,6 +127,7 @@ protected: Point2i upperLeft(bool flipY = false) const; void reveal(const AABB &aabb); Float autoFocus() const; + bool askReallyCancelRendering(); inline ProjectiveCamera *getProjectiveCamera() { Sensor *sensor = m_context->scene->getSensor();