| 1 |
// Copyright 2004 "Gilles Degottex" |
| 2 |
|
| 3 |
// This file is part of "fmit" |
| 4 |
|
| 5 |
// "fmit" is free software; you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU General Public License as published by |
| 7 |
// the Free Software Foundation; either version 2 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// "fmit" is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with this program; if not, write to the Free Software |
| 17 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
|
| 19 |
#include "GLErrorHistory.h" |
| 20 |
|
| 21 |
#include <iostream> |
| 22 |
#include <limits> |
| 23 |
using namespace std; |
| 24 |
#include <qtimer.h> |
| 25 |
#include <qtooltip.h> |
| 26 |
#include <qimage.h> |
| 27 |
#include <qwidgetaction.h> |
| 28 |
#include <qboxlayout.h> |
| 29 |
#include <QPainter> |
| 30 |
#include <Music/Music.h> |
| 31 |
|
| 32 |
void GLErrorHistory::Note::init() |
| 33 |
{ |
| 34 |
min_err = numeric_limits<float>::max(); |
| 35 |
max_err = -numeric_limits<float>::max(); |
| 36 |
avg_err = 0.0f; |
| 37 |
} |
| 38 |
GLErrorHistory::Note::Note(int h) |
| 39 |
: ht(h) |
| 40 |
{ |
| 41 |
init(); |
| 42 |
} |
| 43 |
GLErrorHistory::Note::Note(int h, int num, int den) |
| 44 |
: ht(h) |
| 45 |
{ |
| 46 |
init(); |
| 47 |
factor = QString::number(num)+"/"+QString::number(den); |
| 48 |
} |
| 49 |
GLErrorHistory::Note::Note(int h, float cents) |
| 50 |
: ht(h) |
| 51 |
{ |
| 52 |
init(); |
| 53 |
factor = QString::number(cents); |
| 54 |
} |
| 55 |
QString GLErrorHistory::Note::getName() const |
| 56 |
{ |
| 57 |
return QString::fromStdString(Music::h2n(ht))+factor; |
| 58 |
} |
| 59 |
void GLErrorHistory::Note::addError(float err) |
| 60 |
{ |
| 61 |
min_err = (err<min_err)?err:min_err; |
| 62 |
max_err = (err>max_err)?err:max_err; |
| 63 |
|
| 64 |
if(!errors.empty()) |
| 65 |
avg_err *= errors.size(); |
| 66 |
|
| 67 |
errors.push_back(err); |
| 68 |
|
| 69 |
avg_err += err; |
| 70 |
avg_err /= errors.size(); |
| 71 |
} |
| 72 |
|
| 73 |
GLErrorHistory::GLErrorHistory(QWidget* parent) |
| 74 |
: QOpenGLWidget(parent) |
| 75 |
, View(tr("Error history"), this) |
| 76 |
, m_font("Helvetica") |
| 77 |
{ |
| 78 |
// settings |
| 79 |
setting_show->setIcon(QIcon(":/fmit/ui/images/module_errorhistory.svg")); |
| 80 |
setting_show->setChecked(true); |
| 81 |
|
| 82 |
setting_keep = new QAction(tr("Keep previous notes"), this); |
| 83 |
setting_keep->setCheckable(true); |
| 84 |
setting_keep->setChecked(false); |
| 85 |
connect(setting_keep, SIGNAL(toggled(bool)), this, SLOT(keepPreviousNotes(bool))); |
| 86 |
m_popup_menu.addAction(setting_keep); |
| 87 |
|
| 88 |
|
| 89 |
/* |
| 90 |
* AutoScroll |
| 91 |
* If activated, only the last N errors are plotted. |
| 92 |
*/ |
| 93 |
setting_autoScroll = new QAction(tr("AutoScroll"), this); |
| 94 |
setting_autoScroll->setCheckable(true); |
| 95 |
setting_autoScroll->setChecked(false); |
| 96 |
connect(setting_autoScroll, SIGNAL(toggled(bool)), this, SLOT(autoScroll(bool))); |
| 97 |
m_popup_menu.addAction(setting_autoScroll); |
| 98 |
|
| 99 |
QHBoxLayout* autoScrollActionLayout = new QHBoxLayout(&m_popup_menu); |
| 100 |
|
| 101 |
QLabel* autoScrollActionTitle = new QLabel(tr("AutoScroll Scale"), &m_popup_menu); |
| 102 |
autoScrollActionLayout->addWidget(autoScrollActionTitle); |
| 103 |
|
| 104 |
setting_hScale = new QSpinBox(&m_popup_menu); |
| 105 |
setting_hScale->setMinimum(1); |
| 106 |
setting_hScale->setMaximum(10000); |
| 107 |
setting_hScale->setSingleStep(1); |
| 108 |
setting_hScale->setValue(100); |
| 109 |
setting_hScale->setToolTip(tr("AutoScroll Scale")); |
| 110 |
setting_hScale->setEnabled(false); |
| 111 |
autoScrollActionLayout->addWidget(setting_hScale); |
| 112 |
|
| 113 |
QWidget* autoScrollActionWidget = new QWidget(&m_popup_menu); |
| 114 |
autoScrollActionWidget->setLayout(autoScrollActionLayout); |
| 115 |
|
| 116 |
QWidgetAction* autoScrollAction = new QWidgetAction(&m_popup_menu); |
| 117 |
autoScrollAction->setDefaultWidget(autoScrollActionWidget); |
| 118 |
m_popup_menu.addAction(autoScrollAction); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
setting_useCents = new QAction(tr("Use cents"), this); |
| 124 |
setting_useCents->setCheckable(true); |
| 125 |
setting_useCents->setChecked(true); |
| 126 |
connect(setting_useCents, SIGNAL(toggled(bool)), this, SLOT(update())); |
| 127 |
m_popup_menu.addAction(setting_useCents); |
| 128 |
|
| 129 |
|
| 130 |
QHBoxLayout* scaleActionLayout = new QHBoxLayout(&m_popup_menu); |
| 131 |
|
| 132 |
QLabel* scaleActionTitle = new QLabel(tr("Scale range"), &m_popup_menu); |
| 133 |
scaleActionLayout->addWidget(scaleActionTitle); |
| 134 |
|
| 135 |
setting_spinScale = new QSpinBox(&m_popup_menu); |
| 136 |
setting_spinScale->setMinimum(5); |
| 137 |
setting_spinScale->setMaximum(50); |
| 138 |
setting_spinScale->setSingleStep(1); |
| 139 |
setting_spinScale->setValue(50); |
| 140 |
setting_spinScale->setToolTip(tr("Scale range (in cents)")); |
| 141 |
connect(setting_spinScale, SIGNAL(valueChanged(int)), this, SLOT(update())); |
| 142 |
scaleActionLayout->addWidget(setting_spinScale); |
| 143 |
|
| 144 |
QWidget* scaleActionWidget = new QWidget(&m_popup_menu); |
| 145 |
scaleActionWidget->setLayout(scaleActionLayout); |
| 146 |
|
| 147 |
QWidgetAction* scaleAction = new QWidgetAction(&m_popup_menu); |
| 148 |
scaleAction->setDefaultWidget(scaleActionWidget); |
| 149 |
m_popup_menu.addAction(scaleAction); |
| 150 |
} |
| 151 |
|
| 152 |
void GLErrorHistory::save() |
| 153 |
{ |
| 154 |
s_settings->setValue("keep", setting_keep->isChecked()); |
| 155 |
s_settings->setValue("useCents", setting_useCents->isChecked()); |
| 156 |
s_settings->setValue("spinScale", setting_spinScale->value()); |
| 157 |
} |
| 158 |
void GLErrorHistory::load() |
| 159 |
{ |
| 160 |
setting_keep->setChecked(s_settings->value("keep", setting_keep->isChecked()).toBool()); |
| 161 |
setting_useCents->setChecked(s_settings->value("useCents", setting_useCents->isChecked()).toBool()); |
| 162 |
setting_spinScale->setValue(s_settings->value("spinScale", setting_spinScale->value()).toInt()); |
| 163 |
} |
| 164 |
void GLErrorHistory::clearSettings() |
| 165 |
{ |
| 166 |
s_settings->remove("keep"); |
| 167 |
s_settings->remove("useCents"); |
| 168 |
s_settings->remove("spinScale"); |
| 169 |
} |
| 170 |
|
| 171 |
void GLErrorHistory::addNote(GLErrorHistory::Note note) |
| 172 |
{ |
| 173 |
m_notes.push_back(note); |
| 174 |
|
| 175 |
if(!setting_keep->isChecked()) |
| 176 |
while(m_notes.size()>1) |
| 177 |
m_notes.pop_front(); |
| 178 |
} |
| 179 |
void GLErrorHistory::addError(float err) |
| 180 |
{ |
| 181 |
m_notes.back().addError(err); |
| 182 |
|
| 183 |
// If AutoScroll is enabled, old errors may be deleted |
| 184 |
if (setting_autoScroll->isChecked() ) |
| 185 |
{ |
| 186 |
int size = 0; |
| 187 |
for(int i = m_notes.size() - 1; i >= 0; i--) |
| 188 |
{ |
| 189 |
size += m_notes[i].errors.size() - 1; |
| 190 |
if (size > setting_hScale->value()) |
| 191 |
{ |
| 192 |
// Delete oldest error from this note |
| 193 |
m_notes[i].errors.pop_front(); |
| 194 |
// Delete note, if it is empty |
| 195 |
if (m_notes[i].errors.size() == 0) |
| 196 |
m_notes.erase(m_notes.begin() + i); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
void GLErrorHistory::keepPreviousNotes(bool keep) |
| 203 |
{ |
| 204 |
if(!keep) |
| 205 |
while(m_notes.size()>1) |
| 206 |
m_notes.pop_front(); |
| 207 |
} |
| 208 |
|
| 209 |
void GLErrorHistory::autoScroll(bool autoScroll) |
| 210 |
{ |
| 211 |
// AutoScroll-Spinbox can be edited iff AutoScroll is enabled |
| 212 |
setting_hScale->setEnabled(autoScroll); |
| 213 |
} |
| 214 |
|
| 215 |
void GLErrorHistory::initializeGL() |
| 216 |
{ |
| 217 |
// Set the clear color to black |
| 218 |
glClearColor(1.0, 1.0, 1.0, 0.0); |
| 219 |
|
| 220 |
// glShadeModel( GL_FLAT ); |
| 221 |
glShadeModel( GL_SMOOTH ); |
| 222 |
|
| 223 |
glLoadIdentity(); |
| 224 |
} |
| 225 |
|
| 226 |
void GLErrorHistory::drawTicksCent(int r, int ticks_size) |
| 227 |
{ |
| 228 |
// only work within range that is a pure multiple of r |
| 229 |
float range = int(setting_spinScale->value()/r)*r; |
| 230 |
|
| 231 |
float scale = 50.0f/setting_spinScale->value(); |
| 232 |
if((height()-ticks_size)*r/100.0f*scale>2) |
| 233 |
{ |
| 234 |
for(float i=-range; i<=range; i+=r) |
| 235 |
{ |
| 236 |
int y = height()/2 + int(height()*i/100.0f*scale); |
| 237 |
glVertex2i(ticks_size, y); |
| 238 |
glVertex2i(width(), y); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
void GLErrorHistory::drawTextTickCent(int r, int dy) |
| 243 |
{ |
| 244 |
Q_UNUSED(dy) |
| 245 |
|
| 246 |
// only work within range that is a pure multiple of r |
| 247 |
int range = int(setting_spinScale->value()/r)*r; |
| 248 |
|
| 249 |
QPainter painter(this); |
| 250 |
m_font.setPixelSize(14); |
| 251 |
painter.setFont(m_font); |
| 252 |
QFontMetrics fm(m_font); |
| 253 |
|
| 254 |
float scale = 50.0f/setting_spinScale->value(); |
| 255 |
QString txt; |
| 256 |
for(int i=-range; i<=range; i+=r) |
| 257 |
{ |
| 258 |
txt = QString::number(i); |
| 259 |
if(i>=0) txt = QString(" ")+txt; |
| 260 |
if(i==0) txt = QString(" ")+txt; |
| 261 |
int y = height()/2 - int(height()*i/100.0f*scale); |
| 262 |
if(y>fm.xHeight() && y<height()-fm.xHeight()) |
| 263 |
painter.drawText(2, y+ fm.xHeight()/2, txt); |
| 264 |
} |
| 265 |
painter.end(); |
| 266 |
} |
| 267 |
|
| 268 |
//void GLErrorHistory::paintEvent(QPaintEvent * event){ |
| 269 |
// QPainter painter; |
| 270 |
// painter.begin(this); |
| 271 |
// QBrush background = QBrush(Qt::white); |
| 272 |
// painter.fillRect(rect(), background); |
| 273 |
// painter.end(); |
| 274 |
//} |
| 275 |
|
| 276 |
void GLErrorHistory::paintGL() |
| 277 |
{ |
| 278 |
// cout << "GLErrorHistory::paintGL " << m_notes.size() << endl; |
| 279 |
|
| 280 |
glClear(GL_COLOR_BUFFER_BIT); |
| 281 |
|
| 282 |
glLineWidth(1.0f); |
| 283 |
|
| 284 |
// name |
| 285 |
glColor3f(0.75,0.75,0.75); |
| 286 |
QPainter painter(this); |
| 287 |
m_font.setPixelSize(20); |
| 288 |
painter.setFont(m_font); |
| 289 |
painter.drawText(2, 20, tr("Error")); |
| 290 |
painter.end(); |
| 291 |
|
| 292 |
int char_size = 9; |
| 293 |
int ticks_size = 2+3*char_size; |
| 294 |
int dy = char_size/2; |
| 295 |
|
| 296 |
// horiz lines |
| 297 |
if(setting_useCents->isChecked()) |
| 298 |
{ |
| 299 |
glBegin(GL_LINES); |
| 300 |
float gray = 0.87; |
| 301 |
// glColor3f(gray, gray, gray); |
| 302 |
// drawTicksCent(1, ticks_size); |
| 303 |
gray = 0.875; |
| 304 |
glColor3f(gray, gray, gray); |
| 305 |
drawTicksCent(2, ticks_size); |
| 306 |
gray = 0.75; |
| 307 |
glColor3f(gray, gray, gray); |
| 308 |
drawTicksCent(10, ticks_size); |
| 309 |
glEnd(); |
| 310 |
} |
| 311 |
else |
| 312 |
{ |
| 313 |
glBegin(GL_LINES); |
| 314 |
float gray = 0.5; |
| 315 |
glColor3f(gray, gray, gray); |
| 316 |
glVertex2i(ticks_size, height()/2); |
| 317 |
glVertex2i(width(), height()/2); |
| 318 |
gray = 0.75; |
| 319 |
glColor3f(gray, gray, gray); |
| 320 |
glVertex2i(ticks_size, height()/4); |
| 321 |
glVertex2i(width(), height()/4); |
| 322 |
glVertex2i(ticks_size, 3*height()/4); |
| 323 |
glVertex2i(width(), 3*height()/4); |
| 324 |
gray = 0.87; |
| 325 |
glColor3f(gray, gray, gray); |
| 326 |
glVertex2i(ticks_size, height()/8); |
| 327 |
glVertex2i(width(), height()/8); |
| 328 |
glVertex2i(ticks_size, 7*height()/8); |
| 329 |
glVertex2i(width(), 7*height()/8); |
| 330 |
glVertex2i(ticks_size, 3*height()/8); |
| 331 |
glVertex2i(width(), 3*height()/8); |
| 332 |
glVertex2i(ticks_size, 5*height()/8); |
| 333 |
glVertex2i(width(), 5*height()/8); |
| 334 |
glEnd(); |
| 335 |
} |
| 336 |
|
| 337 |
// text marks |
| 338 |
float gray = 0.5; |
| 339 |
glColor3f(gray, gray, gray); |
| 340 |
if(setting_useCents->isChecked()) |
| 341 |
{ |
| 342 |
int grad = 10; |
| 343 |
if(setting_spinScale->value() <= 25) grad=5; |
| 344 |
if(setting_spinScale->value() <= 10) grad=1; |
| 345 |
drawTextTickCent(grad, dy); |
| 346 |
} |
| 347 |
else |
| 348 |
{ |
| 349 |
painter.begin(this); |
| 350 |
m_font.setPixelSize(14); |
| 351 |
painter.setFont(m_font); |
| 352 |
QFontMetrics fm(m_font); |
| 353 |
string sfraq, sufraq; |
| 354 |
sufraq = string("1/")+QString::number(int(50/setting_spinScale->value())*2).toStdString(); |
| 355 |
sfraq = string("-")+sufraq; |
| 356 |
painter.drawText(2, 3*height()/4-dy+fm.xHeight(), QString(sfraq.c_str())); |
| 357 |
sfraq = string("+")+sufraq; |
| 358 |
painter.drawText(2, height()/4-dy+fm.xHeight(), QString(sfraq.c_str())); |
| 359 |
|
| 360 |
sufraq = string("1/")+QString::number(int(50/setting_spinScale->value())*4).toStdString(); |
| 361 |
sfraq = string("-")+sufraq; |
| 362 |
painter.drawText(2, 5*height()/8-dy+fm.xHeight(), QString(sfraq.c_str())); |
| 363 |
sfraq = string("+")+sufraq; |
| 364 |
painter.drawText(2, 3*height()/8-dy+fm.xHeight(), QString(sfraq.c_str())); |
| 365 |
painter.end(); |
| 366 |
} |
| 367 |
|
| 368 |
// errors |
| 369 |
if(!m_notes.empty()) |
| 370 |
{ |
| 371 |
float step; // width of one error |
| 372 |
|
| 373 |
if (setting_autoScroll->isChecked()) |
| 374 |
step = float(width() - ticks_size) / setting_hScale->value(); |
| 375 |
else |
| 376 |
{ |
| 377 |
int total_size = 0; |
| 378 |
for(size_t i=0; i<m_notes.size(); i++) |
| 379 |
total_size += int(m_notes[i].errors.size())-1; |
| 380 |
step = float(width() - ticks_size) / total_size; |
| 381 |
} |
| 382 |
|
| 383 |
// cout << "total_size=" << total_size << " step=" << step << endl; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
int curr_total = 0; |
| 388 |
for(size_t i=0; i<m_notes.size(); i++) |
| 389 |
{ |
| 390 |
float x = ticks_size+step*curr_total; |
| 391 |
|
| 392 |
// if it's not the first, add a separation |
| 393 |
if(i>0) |
| 394 |
{ |
| 395 |
glColor3f(0.75,0.75,0.75); |
| 396 |
glLineWidth(1.0f); |
| 397 |
glBegin(GL_LINES); |
| 398 |
glVertex2f(x, 0); glVertex2f(x, height()); |
| 399 |
glEnd(); |
| 400 |
} |
| 401 |
|
| 402 |
// the note name |
| 403 |
string str = m_notes[i].getName().toStdString(); |
| 404 |
glColor3f(0.0,0.0,1.0); |
| 405 |
painter.begin(this); |
| 406 |
m_font.setPixelSize(14); |
| 407 |
painter.setFont(m_font); |
| 408 |
painter.drawText(x+2, height()-2, QString(str.c_str())); |
| 409 |
painter.end(); |
| 410 |
|
| 411 |
// draw the error graph |
| 412 |
glColor3f(0.0f,0.0f,0.0f); |
| 413 |
glLineWidth(2.0f); |
| 414 |
glBegin(GL_LINE_STRIP); |
| 415 |
|
| 416 |
float scale; |
| 417 |
if(setting_useCents->isChecked()) |
| 418 |
scale = 50.0f/setting_spinScale->value(); |
| 419 |
else |
| 420 |
scale = int(50/setting_spinScale->value()); |
| 421 |
|
| 422 |
glVertex2f(x, int((scale*m_notes[i].errors[0])*height()) + height()/2); |
| 423 |
for(int j=1; j<int(m_notes[i].errors.size()); j++) |
| 424 |
glVertex2f(x+j*step, (scale*m_notes[i].errors[j])*height() + height()/2); |
| 425 |
|
| 426 |
glEnd(); |
| 427 |
|
| 428 |
curr_total += int(m_notes[i].errors.size())-1; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
glFlush(); |
| 433 |
} |
| 434 |
|
| 435 |
void GLErrorHistory::resizeGL( int w, int h ) |
| 436 |
{ |
| 437 |
// Set the new viewport size |
| 438 |
glViewport(0, 0, (GLint)w, (GLint)h); |
| 439 |
|
| 440 |
// Choose the projection matrix to be the matrix |
| 441 |
// manipulated by the following calls |
| 442 |
glMatrixMode(GL_PROJECTION); |
| 443 |
|
| 444 |
// Set the projection matrix to be the identity matrix |
| 445 |
glLoadIdentity(); |
| 446 |
|
| 447 |
// Define the dimensions of the Orthographic Viewing Volume |
| 448 |
glOrtho(0.0, w, 0.0, h, 0.0, 1.0); |
| 449 |
|
| 450 |
// Choose the modelview matrix to be the matrix |
| 451 |
// manipulated by further calls |
| 452 |
glMatrixMode(GL_MODELVIEW); |
| 453 |
} |
| 454 |
|