Qt 5.4+ QOpenGLWidget and graphing optimsations

This commit is contained in:
Mark Watkins 2015-08-27 11:37:10 +10:00
parent 62f90beb4f
commit 866b6fff5d
7 changed files with 73 additions and 22 deletions

View File

@ -1430,7 +1430,7 @@ int gGraph::minHeight()
return minheight; return minheight;
} }
void GetTextExtent(QString text, int &width, int &height, QFont *font) inline void GetTextExtent(QString text, int &width, int &height, QFont *font)
{ {
#ifdef ENABLE_THREADED_DRAWING #ifdef ENABLE_THREADED_DRAWING
static QMutex mut; static QMutex mut;

View File

@ -290,8 +290,10 @@ gGraph *gGraphView::popGraph()
gGraphView::gGraphView(QWidget *parent, gGraphView *shared) gGraphView::gGraphView(QWidget *parent, gGraphView *shared)
#ifdef BROKEN_OPENGL_BUILD #ifdef BROKEN_OPENGL_BUILD
: QWidget(parent), : QWidget(parent),
#else #elif QT_VERSION < QT_VERSION_CHECK(5,4,0)
: QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::DirectRendering | QGL::HasOverlay | QGL::Rgba),parent,shared), : QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::DirectRendering | QGL::HasOverlay | QGL::Rgba),parent,shared),
#else
:QOpenGLWidget(parent),
#endif #endif
m_offsetY(0), m_offsetX(0), m_scaleY(0.0), m_scrollbar(nullptr) m_offsetY(0), m_offsetX(0), m_scaleY(0.0), m_scrollbar(nullptr)
{ {
@ -354,7 +356,10 @@ gGraphView::gGraphView(QWidget *parent, gGraphView *shared)
#ifndef BROKEN_OPENGL_BUILD #ifndef BROKEN_OPENGL_BUILD
setAutoFillBackground(false); setAutoFillBackground(false);
#if QT_VERSION < QT_VERSION_CHECK(5,4,0)
// happens no matter what in 5.4+
setAutoBufferSwap(false); setAutoBufferSwap(false);
#endif
#endif #endif
context_menu = new QMenu(this); context_menu = new QMenu(this);
@ -432,8 +437,10 @@ void gGraphView::closeEvent(QCloseEvent * event)
} }
#ifdef BROKEN_OPENGL_BUILD #ifdef BROKEN_OPENGL_BUILD
QWidget::closeEvent(event); QWidget::closeEvent(event);
#else #elif QT_VERSION < QT_VERSION_CHECK(5,4,0)
QGLWidget::closeEvent(event); QGLWidget::closeEvent(event);
#else
QOpenGLWidget::closeEvent(event);
#endif #endif
} }
@ -909,7 +916,10 @@ void gGraphView::updateScale()
void gGraphView::resizeEvent(QResizeEvent *e) void gGraphView::resizeEvent(QResizeEvent *e)
{ {
// QWidget::resizeEvent(e); // This ques a redraw event.. #if QT_VERSION >= QT_VERSION_CHECK(5,4,0)
// This ques a needed redraw event..
QOpenGLWidget::resizeEvent(e);
#endif
updateScale(); updateScale();
@ -926,7 +936,11 @@ void gGraphView::scrollbarValueChanged(int val)
//qDebug() << "Scrollbar Changed" << val; //qDebug() << "Scrollbar Changed" << val;
if (m_offsetY != val) { if (m_offsetY != val) {
m_offsetY = val; m_offsetY = val;
#if QT_VERSION >= QT_VERSION_CHECK(5,4,0)
update();
#else
timedRedraw(); // do this on a timer? timedRedraw(); // do this on a timer?
#endif
} }
} }
@ -1373,7 +1387,9 @@ void gGraphView::paintGL()
painter.end(); painter.end();
#ifndef BROKEN_OPENGL_BUILD #ifndef BROKEN_OPENGL_BUILD
#if QT_VERSION < QT_VERSION_CHECK(5,4,0)
swapBuffers(); swapBuffers();
#endif
#endif #endif
if (this->isVisible() && !graphs_drawn && render_cube) { // keep the cube spinning if (this->isVisible() && !graphs_drawn && render_cube) { // keep the cube spinning
redrawtimer->setInterval(1000.0 / 50); // 50 FPS redrawtimer->setInterval(1000.0 / 50); // 50 FPS
@ -2666,8 +2682,10 @@ void gGraphView::keyReleaseEvent(QKeyEvent *event)
} }
#ifdef BROKEN_OPENGL_BUILD #ifdef BROKEN_OPENGL_BUILD
QWidget::keyReleaseEvent(event); QWidget::keyReleaseEvent(event);
#else #elif QT_VERSION < QT_VERSION_CHECK(5,4,0)
QGLWidget::keyReleaseEvent(event); QGLWidget::keyReleaseEvent(event);
#else
QOpenGLWidget::keyReleaseEvent(event);
#endif #endif
} }
@ -3380,6 +3398,6 @@ void gGraphView::redraw()
#ifdef BROKEN_OPENGL_BUILD #ifdef BROKEN_OPENGL_BUILD
repaint(); repaint();
#else #else
updateGL(); update();
#endif #endif
} }

View File

@ -1,6 +1,6 @@
/* gGraphView Header /* gGraphView Header
* *
* Copyright (c) 2011-2014 Mark Watkins <jedimark@users.sourceforge.net> * Copyright (c) 2011-2015 Mark Watkins <jedimark@users.sourceforge.net>
* *
* This file is subject to the terms and conditions of the GNU General Public * This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of the Linux * License. See the file COPYING in the main directory of the Linux
@ -26,7 +26,11 @@
#include <QTimer> #include <QTimer>
#ifndef BROKEN_OPENGL_BUILD #ifndef BROKEN_OPENGL_BUILD
#if QT_VERSION < QT_VERSION_CHECK(5,4,0)
#include <QGLWidget> #include <QGLWidget>
#else
#include <QOpenGLWidget>
#endif
#endif #endif
#include <Graphs/gGraph.h> #include <Graphs/gGraph.h>
@ -286,8 +290,10 @@ struct SelectionHistoryItem {
class gGraphView class gGraphView
#ifdef BROKEN_OPENGL_BUILD #ifdef BROKEN_OPENGL_BUILD
:public QWidget :public QWidget
#else #elif QT_VERSION < QT_VERSION_CHECK(5,4,0)
:public QGLWidget :public QGLWidget
#else
:public QOpenGLWidget
#endif #endif
{ {
friend class gGraph; friend class gGraph;

View File

@ -60,6 +60,13 @@ int gXAxis::minimumHeight()
#endif #endif
} }
const QString months[] = {
QObject::tr("Jan"), QObject::tr("Feb"), QObject::tr("Mar"), QObject::tr("Apr"), QObject::tr("May"), QObject::tr("Jun"),
QObject::tr("Jul"), QObject::tr("Aug"), QObject::tr("Sep"), QObject::tr("Oct"), QObject::tr("Nov"), QObject::tr("Dec")
};
//static QString dow[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
void gXAxis::paint(QPainter &painter, gGraph &w, const QRegion &region) void gXAxis::paint(QPainter &painter, gGraph &w, const QRegion &region)
{ {
int left = region.boundingRect().left(); int left = region.boundingRect().left();
@ -67,12 +74,6 @@ void gXAxis::paint(QPainter &painter, gGraph &w, const QRegion &region)
int width = region.boundingRect().width(); int width = region.boundingRect().width();
int height = region.boundingRect().height(); int height = region.boundingRect().height();
QString months[] = {
QObject::tr("Jan"), QObject::tr("Feb"), QObject::tr("Mar"), QObject::tr("Apr"), QObject::tr("May"), QObject::tr("Jun"),
QObject::tr("Jul"), QObject::tr("Aug"), QObject::tr("Sep"), QObject::tr("Oct"), QObject::tr("Nov"), QObject::tr("Dec")
};
//static QString dow[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
QVector<QLine> ticks; QVector<QLine> ticks;
@ -81,6 +82,9 @@ void gXAxis::paint(QPainter &painter, gGraph &w, const QRegion &region)
// pixmap caching screws font size when printing // pixmap caching screws font size when printing
QFontMetrics fm(*defaultfont);
bool usepixmap = w.graphView()->usePixmapCache(); // Whether or not to use pixmap caching bool usepixmap = w.graphView()->usePixmapCache(); // Whether or not to use pixmap caching
if (!usepixmap || (usepixmap && w.invalidate_xAxisImage)) { if (!usepixmap || (usepixmap && w.invalidate_xAxisImage)) {
@ -169,7 +173,9 @@ void gXAxis::paint(QPainter &painter, gGraph &w, const QRegion &region)
int x, y; int x, y;
// grab the text extent of the dummy text fields above to know how much space is needed // grab the text extent of the dummy text fields above to know how much space is needed
GetTextExtent(fd, x, y); QRect r2 = fm.boundingRect(fd);
x = r2.width();
y = r2.height();
// Not sure when this was a problem... // Not sure when this was a problem...
Q_ASSERT(x > 0); Q_ASSERT(x > 0);

View File

@ -17,6 +17,8 @@
#include "Graphs/gGraphView.h" #include "Graphs/gGraphView.h"
#include "SleepLib/profiles.h" #include "SleepLib/profiles.h"
#include <QFontMetrics>
gXGrid::gXGrid(QColor col) gXGrid::gXGrid(QColor col)
: Layer(NoChannel) : Layer(NoChannel)
{ {
@ -38,7 +40,7 @@ void gXGrid::paint(QPainter &painter, gGraph &w, const QRegion &region)
int width = region.boundingRect().width(); int width = region.boundingRect().width();
int height = region.boundingRect().height(); int height = region.boundingRect().height();
int x, y; //int x, y;
EventDataType miny = w.physMinY(); EventDataType miny = w.physMinY();
EventDataType maxy = w.physMaxY(); EventDataType maxy = w.physMaxY();
@ -49,8 +51,10 @@ void gXGrid::paint(QPainter &painter, gGraph &w, const QRegion &region)
if (height < 0) { return; } if (height < 0) { return; }
static QString fd = "0"; // static QString fd = "0";
GetTextExtent(fd, x, y); // GetTextExtent(fd, x, y);
QFontMetrics fm(*defaultfont);
int y=fm.height();
double max_yticks = round(height / (y + 14.0*w.printScaleY())); // plus spacing between lines double max_yticks = round(height / (y + 14.0*w.printScaleY())); // plus spacing between lines
//double yt=1/max_yticks; //double yt=1/max_yticks;
@ -162,6 +166,9 @@ void gYAxis::paint(QPainter &painter, gGraph &w, const QRegion &region)
//Todo: clean this up as there is a lot of duplicate code between the sections //Todo: clean this up as there is a lot of duplicate code between the sections
QFontMetrics fm(*defaultfont);
static QString fd;
if (0) { if (0) {
} else { } else {
if (height < 0) { return; } if (height < 0) { return; }
@ -178,8 +185,9 @@ void gYAxis::paint(QPainter &painter, gGraph &w, const QRegion &region)
EventDataType dy = maxy - miny; EventDataType dy = maxy - miny;
static QString fd = "0"; // GetTextExtent(fd, x, y);
GetTextExtent(fd, x, y); y=fm.height();
x=0;
#ifdef DEBUG_LAYOUT #ifdef DEBUG_LAYOUT
painter.setPen(Qt::green); painter.setPen(Qt::green);
@ -237,6 +245,7 @@ void gYAxis::paint(QPainter &painter, gGraph &w, const QRegion &region)
QVector<QLine> ticks; QVector<QLine> ticks;
QRect r2;
float shorttick = 4.0 * w.printScaleX(); float shorttick = 4.0 * w.printScaleX();
for (double i = miny; i <= maxy + min_ytick - 0.00001; i += min_ytick) { for (double i = miny; i <= maxy + min_ytick - 0.00001; i += min_ytick) {
ty = (i - miny) * ymult; ty = (i - miny) * ymult;
@ -247,7 +256,10 @@ void gYAxis::paint(QPainter &painter, gGraph &w, const QRegion &region)
fd = Format(i * m_yaxis_scale, 1); fd = Format(i * m_yaxis_scale, 1);
} }
GetTextExtent(fd, x, y); // performance bottleneck.. r2 = fm.boundingRect(fd);
x = r2.width();
y = r2.height();
//GetTextExtent(fd, x, y); // performance bottleneck..
if (x > labelW) { labelW = x; } if (x > labelW) { labelW = x; }

View File

@ -61,6 +61,10 @@
#include "reports.h" #include "reports.h"
#include "statistics.h" #include "statistics.h"
#if QT_VERSION >= QT_VERSION_CHECK(5,4,0)
#include <QOpenGLFunctions>
#endif
QProgressBar *qprogress; QProgressBar *qprogress;
QLabel *qstatus; QLabel *qstatus;
QStatusBar *qstatusbar; QStatusBar *qstatusbar;
@ -75,7 +79,13 @@ QString getOpenGLVersionString()
QGLWidget w; QGLWidget w;
w.makeCurrent(); w.makeCurrent();
#if QT_VERSION < QT_VERSION_CHECK(5,4,0)
glversion = QString(QLatin1String(reinterpret_cast<const char*>(glGetString(GL_VERSION)))); glversion = QString(QLatin1String(reinterpret_cast<const char*>(glGetString(GL_VERSION))));
#else
QOpenGLFunctions f;
f.initializeOpenGLFunctions();
glversion = QString(QLatin1String(reinterpret_cast<const char*>(f.glGetString(GL_VERSION))));
#endif
qDebug() << "OpenGL Version:" << glversion; qDebug() << "OpenGL Version:" << glversion;
} }
return glversion; return glversion;

View File

@ -221,7 +221,6 @@ HEADERS += \
SleepLib/loader_plugins/cms50f37_loader.h \ SleepLib/loader_plugins/cms50f37_loader.h \
build_number.h build_number.h
FORMS += \ FORMS += \
daily.ui \ daily.ui \
overview.ui \ overview.ui \