Merge pull request #2301 from Geenz/19514

Code Review for Job #19514
This commit is contained in:
Brad Hefta-Gaub 2014-03-13 16:31:06 -07:00
commit 89a6ecd1c1
2 changed files with 52 additions and 2 deletions

View file

@ -11,18 +11,26 @@
#include "GLCanvas.h" #include "GLCanvas.h"
#include <QMimeData> #include <QMimeData>
#include <QUrl> #include <QUrl>
#include <QMainWindow>
GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::NoDepthBuffer, QGL::NoStencilBuffer)) { GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::NoDepthBuffer, QGL::NoStencilBuffer)),
_throttleRendering(false),
_idleRenderInterval(100)
{
} }
void GLCanvas::initializeGL() { void GLCanvas::initializeGL() {
Application::getInstance()->initializeGL(); Application::getInstance()->initializeGL();
setAttribute(Qt::WA_AcceptTouchEvents); setAttribute(Qt::WA_AcceptTouchEvents);
setAcceptDrops(true); setAcceptDrops(true);
connect(Application::getInstance(), SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(activeChanged(Qt::ApplicationState)));
connect(&_frameTimer, SIGNAL(timeout()), this, SLOT(throttleRender()));
} }
void GLCanvas::paintGL() { void GLCanvas::paintGL() {
Application::getInstance()->paintGL(); if (!_throttleRendering && !Application::getInstance()->getWindow()->isMinimized()) {
Application::getInstance()->paintGL();
}
} }
void GLCanvas::resizeGL(int width, int height) { void GLCanvas::resizeGL(int width, int height) {
@ -49,6 +57,38 @@ void GLCanvas::mouseReleaseEvent(QMouseEvent* event) {
Application::getInstance()->mouseReleaseEvent(event); Application::getInstance()->mouseReleaseEvent(event);
} }
void GLCanvas::activeChanged(Qt::ApplicationState state) {
switch (state) {
case Qt::ApplicationActive:
// If we're active, stop the frame timer and the throttle.
_frameTimer.stop();
_throttleRendering = false;
break;
case Qt::ApplicationSuspended:
case Qt::ApplicationHidden:
// If we're hidden or are about to suspend, don't render anything.
_throttleRendering = false;
_frameTimer.stop();
break;
default:
// Otherwise, throttle.
if (!_throttleRendering) {
_frameTimer.start(_idleRenderInterval);
_throttleRendering = true;
}
break;
}
}
void GLCanvas::throttleRender() {
_frameTimer.start(_idleRenderInterval);
if (!Application::getInstance()->getWindow()->isMinimized()) {
Application::getInstance()->paintGL();
}
}
int updateTime = 0; int updateTime = 0;
bool GLCanvas::event(QEvent* event) { bool GLCanvas::event(QEvent* event) {
switch (event->type()) { switch (event->type()) {

View file

@ -10,13 +10,19 @@
#define __hifi__GLCanvas__ #define __hifi__GLCanvas__
#include <QGLWidget> #include <QGLWidget>
#include <QTimer>
/// customized canvas that simply forwards requests/events to the singleton application /// customized canvas that simply forwards requests/events to the singleton application
class GLCanvas : public QGLWidget { class GLCanvas : public QGLWidget {
Q_OBJECT
public: public:
GLCanvas(); GLCanvas();
protected: protected:
QTimer _frameTimer;
bool _throttleRendering;
int _idleRenderInterval;
virtual void initializeGL(); virtual void initializeGL();
virtual void paintGL(); virtual void paintGL();
virtual void resizeGL(int width, int height); virtual void resizeGL(int width, int height);
@ -34,6 +40,10 @@ protected:
virtual void dragEnterEvent(QDragEnterEvent *event); virtual void dragEnterEvent(QDragEnterEvent *event);
virtual void dropEvent(QDropEvent* event); virtual void dropEvent(QDropEvent* event);
private slots:
void activeChanged(Qt::ApplicationState state);
void throttleRender();
}; };
#endif /* defined(__hifi__GLCanvas__) */ #endif /* defined(__hifi__GLCanvas__) */