Skip to content

Commit

Permalink
Convert QGLWidget usage to QOpenGLWidget
Browse files Browse the repository at this point in the history
QOpenGLWidget is the modern replacement and has several advantages. In
this case, it fixes gillesdegottex#86.

The only big change needed was replacing renderText calls with
QPainter::drawText ones.
  • Loading branch information
dsboger committed May 9, 2018
1 parent b8e3d8f commit 56f615d
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 69 deletions.
16 changes: 8 additions & 8 deletions src/CustomInstrumentTunerForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,31 +628,31 @@ void CustomInstrumentTunerForm::refresh_views()
{
// cout << "CustomInstrumentTunerForm::refresh_views " << endl;

// m_dialTune->repaint();
m_dialTune->repaint();

if(m_glGraph->setting_show->isChecked())
m_glGraph->updateGL();
m_glGraph->update();

if(m_glErrorHistory->setting_show->isChecked())
m_glErrorHistory->updateGL();
m_glErrorHistory->update();

if(m_glVolumeHistory->setting_show->isChecked())
m_glVolumeHistory->updateGL();
m_glVolumeHistory->update();

if(m_microtonalView->setting_show->isChecked())
m_microtonalView->update();

if(m_glSample->setting_show->isChecked())
m_glSample->updateGL();
m_glSample->update();

if(m_glFreqStruct->setting_show->isChecked())
m_glFreqStruct->updateGL();
m_glFreqStruct->update();

if(m_glFT->setting_show->isChecked())
m_glFT->updateGL();
m_glFT->update();

if(m_glStatistics->setting_show->isChecked())
m_glStatistics->updateGL();
m_glStatistics->update();

m_time_refresh_views.start();
}
Expand Down
29 changes: 21 additions & 8 deletions src/modules/GLErrorHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ using namespace std;
#include <qimage.h>
#include <qwidgetaction.h>
#include <qboxlayout.h>
#include <QPainter>
#include <Music/Music.h>

void GLErrorHistory::Note::init()
Expand Down Expand Up @@ -70,7 +71,7 @@ void GLErrorHistory::Note::addError(float err)
}

GLErrorHistory::GLErrorHistory(QWidget* parent)
: QGLWidget(parent)
: QOpenGLWidget(parent)
, View(tr("Error history"), this)
, m_font("Helvetica")
{
Expand Down Expand Up @@ -185,7 +186,9 @@ void GLErrorHistory::drawTextTickCent(int r, int dy)
// only work within range that is a pure multiple of r
int range = int(setting_spinScale->value()/r)*r;

QPainter painter(this);
m_font.setPixelSize(14);
painter.setFont(m_font);
QFontMetrics fm(m_font);

float scale = 50.0f/setting_spinScale->value();
Expand All @@ -197,8 +200,9 @@ void GLErrorHistory::drawTextTickCent(int r, int dy)
if(i==0) txt = QString(" ")+txt;
int y = height()/2 - int(height()*i/100.0f*scale);
if(y>fm.xHeight() && y<height()-fm.xHeight())
renderText(2, y+ fm.xHeight()/2, txt, m_font);
painter.drawText(2, y+ fm.xHeight()/2, txt);
}
painter.end();
}

//void GLErrorHistory::paintEvent(QPaintEvent * event){
Expand All @@ -219,8 +223,11 @@ void GLErrorHistory::paintGL()

// name
glColor3f(0.75,0.75,0.75);
QPainter painter(this);
m_font.setPixelSize(20);
renderText(2, 20, tr("Error"), m_font);
painter.setFont(m_font);
painter.drawText(2, 20, tr("Error"));
painter.end();

int char_size = 9;
int ticks_size = 2+3*char_size;
Expand Down Expand Up @@ -279,20 +286,23 @@ void GLErrorHistory::paintGL()
}
else
{
painter.begin(this);
m_font.setPixelSize(14);
painter.setFont(m_font);
QFontMetrics fm(m_font);
string sfraq, sufraq;
sufraq = string("1/")+QString::number(int(50/setting_spinScale->value())*2).toStdString();
sfraq = string("-")+sufraq;
renderText(2, 3*height()/4-dy+fm.xHeight(), QString(sfraq.c_str()), m_font);
painter.drawText(2, 3*height()/4-dy+fm.xHeight(), QString(sfraq.c_str()));
sfraq = string("+")+sufraq;
renderText(2, height()/4-dy+fm.xHeight(), QString(sfraq.c_str()), m_font);
painter.drawText(2, height()/4-dy+fm.xHeight(), QString(sfraq.c_str()));

sufraq = string("1/")+QString::number(int(50/setting_spinScale->value())*4).toStdString();
sfraq = string("-")+sufraq;
renderText(2, 5*height()/8-dy+fm.xHeight(), QString(sfraq.c_str()), m_font);
painter.drawText(2, 5*height()/8-dy+fm.xHeight(), QString(sfraq.c_str()));
sfraq = string("+")+sufraq;
renderText(2, 3*height()/8-dy+fm.xHeight(), QString(sfraq.c_str()), m_font);
painter.drawText(2, 3*height()/8-dy+fm.xHeight(), QString(sfraq.c_str()));
painter.end();
}

// errors
Expand Down Expand Up @@ -324,8 +334,11 @@ void GLErrorHistory::paintGL()
// the note name
string str = m_notes[i].getName().toStdString();
glColor3f(0.0,0.0,1.0);
painter.begin(this);
m_font.setPixelSize(14);
renderText(x+2, height()-2, QString(str.c_str()), m_font);
painter.setFont(m_font);
painter.drawText(x+2, height()-2, QString(str.c_str()));
painter.end();

// draw the error graph
glColor3f(0.0f,0.0f,0.0f);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/GLErrorHistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

#include <deque>
using namespace std;
#include <QGLWidget>
#include <QOpenGLWidget>
#include <qspinbox.h>
#include <qsettings.h>
#include <qaction.h>
class QTimer;
#include <Music/Music.h>
#include "View.h"

class GLErrorHistory : public QGLWidget, public View
class GLErrorHistory : public QOpenGLWidget, public View
{
Q_OBJECT

Expand Down
19 changes: 13 additions & 6 deletions src/modules/GLFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ using namespace std;
#include <qevent.h>
#include <qboxlayout.h>
#include <qwidgetaction.h>
#include <QPainter>
#include <Music/Music.h>
#include <Music/SPWindow.h>
using namespace Music;
#include <CppAddons/CAMath.h>
using namespace Math;

GLFT::GLFT(QWidget* parent)
: QGLWidget(parent)
: QOpenGLWidget(parent)
, View(tr("Fourier Transform"), this)
, m_font("Helvetica")
, m_components_max(1.0)
Expand Down Expand Up @@ -174,7 +175,7 @@ void GLFT::mousePressEvent(QMouseEvent* e)

double f = (m_maxf-m_minf)*double(m_press_x)/width()+m_minf;
m_text = tr("Frequency %1 [Hz]").arg(f);
updateGL();
update();
}
void GLFT::mouseDoubleClickEvent(QMouseEvent* e)
{
Expand All @@ -184,7 +185,7 @@ void GLFT::mouseDoubleClickEvent(QMouseEvent* e)
m_minf=0;
m_maxf=Music::GetSamplingRate()/2; // sr is surely -1 because not yet defined
resetaxis();
updateGL();
update();
}
void GLFT::mouseMoveEvent(QMouseEvent* e)
{
Expand Down Expand Up @@ -223,7 +224,7 @@ void GLFT::mouseMoveEvent(QMouseEvent* e)
m_maxA -= m_maxA*double(dy)/height();
}

updateGL();
update();
}

old_x = e->x();
Expand All @@ -246,8 +247,11 @@ void GLFT::paintGL()

// name
glColor3f(0.75,0.75,0.75);
QPainter painter(this);
m_font.setPixelSize(20);
renderText(2, 20, tr("Fourier Transform"), m_font);
painter.setFont(m_font);
painter.drawText(2, 20, tr("Fourier Transform"));
painter.end();

for(int i=0; i<int(win.size()); i++)
m_plan.in[i] = buff[i]*win[i];
Expand Down Expand Up @@ -298,8 +302,11 @@ void GLFT::paintGL()
{
glColor3f(0,0,0);

QPainter painter(this);
m_font.setPixelSize(14);
renderText(width()/2, 12, m_text, m_font);
painter.setFont(m_font);
painter.drawText(width()/2, 12, m_text);
painter.end();
}

glFlush();
Expand Down
4 changes: 2 additions & 2 deletions src/modules/GLFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
using namespace std;
#include <Music/CFFTW3.h>
// using namespace Music;
#include <QGLWidget>
#include <QOpenGLWidget>
#include <qspinbox.h>
#include <qaction.h>
#include "View.h"

class GLFT : public QGLWidget, public View
class GLFT : public QOpenGLWidget, public View
{
Q_OBJECT

Expand Down
18 changes: 11 additions & 7 deletions src/modules/GLFreqStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ using namespace std;
#include <qimage.h>
#include <qboxlayout.h>
#include <qwidgetaction.h>
#include <QPainter>
#include <Music/Music.h>
using namespace Music;

GLFreqStruct::GLFreqStruct(QWidget* parent)
: QGLWidget(parent)
: QOpenGLWidget(parent)
, View(tr("Harmonics"), this)
, m_font("Helvetica")
, m_components_max(1.0)
Expand Down Expand Up @@ -155,28 +156,31 @@ void GLFreqStruct::paintGL()
}

glColor3f(0.5f,0.5f,0.5f);
QPainter painter(this);
m_font.setPixelSize(14);
painter.setFont(m_font);
QFontMetrics fm(m_font);
int dy = -fm.xHeight()/2;
string sfraq = "-10dB";
renderText(2, 10*(height()-scale_height)/50-dy, QString(sfraq.c_str()), m_font);
painter.drawText(2, 10*(height()-scale_height)/50-dy, QString(sfraq.c_str()));
sfraq = "-20dB";
renderText(2, 20*(height()-scale_height)/50-dy, QString(sfraq.c_str()), m_font);
painter.drawText(2, 20*(height()-scale_height)/50-dy, QString(sfraq.c_str()));
sfraq = "-30dB";
renderText(2, 30*(height()-scale_height)/50-dy, QString(sfraq.c_str()), m_font);
painter.drawText(2, 30*(height()-scale_height)/50-dy, QString(sfraq.c_str()));
sfraq = "-40dB";
renderText(2, 40*(height()-scale_height)/50-dy, QString(sfraq.c_str()), m_font);
painter.drawText(2, 40*(height()-scale_height)/50-dy, QString(sfraq.c_str()));

// scale
m_font.setPixelSize(10);
glColor3f(0,0,0);
for(size_t i=0; i<m_components.size(); i++)
renderText(int((i+0.5)*step)+s-3, height()-2, QString::number(i+1), m_font);
painter.drawText(int((i+0.5)*step)+s-3, height()-2, QString::number(i+1));

// name
glColor3f(0.75,0.75,0.75);
m_font.setPixelSize(20);
renderText(2, 20, tr("Harmonics' amplitude"), m_font);
painter.drawText(2, 20, tr("Harmonics' amplitude"));
painter.end();

glFlush();
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/GLFreqStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

#include <vector>
using namespace std;
#include <QGLWidget>
#include <QOpenGLWidget>
#include <qspinbox.h>
#include <qaction.h>
#include "View.h"

class GLFreqStruct : public QGLWidget, public View
class GLFreqStruct : public QOpenGLWidget, public View
{
Q_OBJECT

Expand Down
12 changes: 8 additions & 4 deletions src/modules/GLGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ using namespace std;
#include <qimage.h>
#include <qboxlayout.h>
#include <qwidgetaction.h>
#include <QPainter>
#include <Music/Music.h>
using namespace Music;

GLGraph::GLGraph(QWidget* parent)
: QGLWidget(parent)
: QOpenGLWidget(parent)
, View(tr("Captured Sound"), this)
, m_font("Helvetica")
, m_skip(1)
Expand Down Expand Up @@ -247,7 +248,7 @@ void GLGraph::update_maxs()
m_maxs.push_back(make_pair(smin, smax));
}

updateGL();
update();
}

void GLGraph::base_paint(float graph_gray)
Expand All @@ -256,12 +257,15 @@ void GLGraph::base_paint(float graph_gray)

// cout<<"m_pending_queue="<<m_pending_queue.size()<<" m_queue="<<m_queue.size()<<" m_maxs="<<m_maxs.size()<<endl;

int width = QGLWidget::width();
int width = QOpenGLWidget::width();

// name
glColor3f(0.75,0.75,0.75);
QPainter painter(this);
m_font.setPixelSize(20);
renderText(2, 20, tr("Captured Sound"), m_font);
painter.setFont(m_font);
painter.drawText(2, 20, tr("Captured Sound"));
painter.end();

if(setting_showWaveForm->isChecked() && !m_queue.empty())
{
Expand Down
4 changes: 2 additions & 2 deletions src/modules/GLGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

#include <deque>
using namespace std;
#include <QGLWidget>
#include <QOpenGLWidget>
#include <qaction.h>
#include <qspinbox.h>
class QTimer;
#include <Music/Music.h>
#include "View.h"

class GLGraph : public QGLWidget, public View
class GLGraph : public QOpenGLWidget, public View
{
Q_OBJECT

Expand Down
Loading

0 comments on commit 56f615d

Please sign in to comment.