diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2014-09-18 07:24:52 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2014-09-18 07:39:58 +0200 |
commit | cf5ab7886e8228d1622566cc59277bcf089199a9 (patch) | |
tree | 2f35b7e417a634e8736f5a9a75113aee21ec5b57 /qfunctionconfigurator | |
parent | f95add4571874c58b35ff4929bcea75dc4080e16 (diff) |
cleanup qfc
Issue: #44
Remove confusing logic and retarded comments by the usual offender. Change
prototypes a bit for clarity's sake again.
Diffstat (limited to 'qfunctionconfigurator')
-rw-r--r-- | qfunctionconfigurator/functionconfig.cpp | 11 | ||||
-rw-r--r-- | qfunctionconfigurator/functionconfig.h | 2 | ||||
-rw-r--r-- | qfunctionconfigurator/qfunctionconfigurator.cpp | 188 | ||||
-rw-r--r-- | qfunctionconfigurator/qfunctionconfigurator.h | 72 |
4 files changed, 128 insertions, 145 deletions
diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index 640db1ef..159f350e 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -14,7 +14,7 @@ #include <QtAlgorithms> #include <QtAlgorithms> #include <QSettings> -#include <math.h> +#include <cmath> #include <QPixmap> void FunctionConfig::setTrackingActive(bool blnActive) @@ -156,13 +156,10 @@ void FunctionConfig::movePoint(int idx, QPointF pt) { } } -QList<QPointF> FunctionConfig::getPoints() { - QList<QPointF> ret; +const QList<QPointF> FunctionConfig::getPoints() { QMutexLocker foo(&_mutex); - for (int i = 0; i < input.size(); i++) { - ret.append(input[i]); - } - return ret; + // NB can't pass by reference + return input; } void FunctionConfig::loadSettings(QSettings& settings, const QString& title) { diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index 22bfc406..ee2087a0 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -85,7 +85,7 @@ public: void addPoint(QPointF pt); void movePoint(int idx, QPointF pt); - QList<QPointF> getPoints(); + const QList<QPointF> getPoints(); void setMaxInput(int MaxInput) { max_x = MaxInput; } diff --git a/qfunctionconfigurator/qfunctionconfigurator.cpp b/qfunctionconfigurator/qfunctionconfigurator.cpp index a197dadb..17431986 100644 --- a/qfunctionconfigurator/qfunctionconfigurator.cpp +++ b/qfunctionconfigurator/qfunctionconfigurator.cpp @@ -1,10 +1,12 @@ -/* Copyright (c) 2011-2012 Stanislaw Halik <sthalik@misaki.pl> - * Adapted to FaceTrackNoIR by Wim Vriend. +/* Copyright (c) 2011-2014 Stanislaw Halik <sthalik@misaki.pl> + * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. */ +// Adapted to FaceTrackNoIR by Wim Vriend. + #include "qfunctionconfigurator/qfunctionconfigurator.h" #include <QPainter> #include <QPaintEvent> @@ -25,35 +27,33 @@ static const int pointSize = 5; -QFunctionConfigurator::QFunctionConfigurator(QWidget *parent) - : QWidget(parent) +QFunctionConfigurator::QFunctionConfigurator(QWidget *parent) : + QWidget(parent), + _config(nullptr), + moving_control_point_idx(-1), + _draw_function(true) { - movingPoint = -1; - _config = 0; - _draw_background = true; - _draw_function = true; update_range(); setMouseTracking(true); } -void QFunctionConfigurator::setConfig(FunctionConfig* config) { +void QFunctionConfigurator::setConfig(FunctionConfig* config, const QString& name) { QSettings settings("opentrack"); QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString(); QSettings iniFile( currentFile, QSettings::IniFormat ); config->loadSettings(iniFile, name); _config = config; - _draw_function = _draw_background = true; + _draw_function = true; update_range(); update(); } -void QFunctionConfigurator::saveSettings(QString settingsFile) { - QSettings iniFile( settingsFile, QSettings::IniFormat ); // Application settings (in INI-file) +void QFunctionConfigurator::saveSettings(QString settingsFile, const QString& name) { + QSettings iniFile(settingsFile, QSettings::IniFormat); - if (_config) { + if (_config) _config->saveSettings(iniFile, name); - } } void QFunctionConfigurator::drawBackground() @@ -61,11 +61,13 @@ void QFunctionConfigurator::drawBackground() if (!_config) return; _background = QPixmap(width(), height()); + QPainter painter(&_background); painter.fillRect(rect(), QColor::fromRgb(204, 204, 204)); painter.setRenderHint(QPainter::Antialiasing); + QColor bg_color(112, 154, 209); - painter.fillRect(range, bg_color); + painter.fillRect(pixel_bounds, bg_color); QFont font; font.setPointSize(8); @@ -79,61 +81,59 @@ void QFunctionConfigurator::drawBackground() const int maxy = _config->maxOutput(); // horizontal grid - for (int i = 0; i < maxy; i += xstep) { - double y = range.height() - i * c.y() + range.y(); + double y = pixel_bounds.height() - i * c.y() + pixel_bounds.y(); drawLine(&painter, - QPointF(range.x(), y), - QPointF(range.x() + range.width(), y), + QPointF(pixel_bounds.x(), y), + QPointF(pixel_bounds.x() + pixel_bounds.width(), y), pen); painter.drawText(QRectF(10, y - metrics.height()/2, - range.left(), + pixel_bounds.left(), metrics.height()), QString::number(i)); } { const int i = maxy; - double y = range.height() - i * c.y() + range.y(); + double y = pixel_bounds.height() - i * c.y() + pixel_bounds.y(); drawLine(&painter, - QPointF(range.x(), y), - QPointF(range.x() + range.width(), y), + QPointF(pixel_bounds.x(), y), + QPointF(pixel_bounds.x() + pixel_bounds.width(), y), pen); painter.drawText(QRectF(10, y - metrics.height()/2, - range.x() - 10, + pixel_bounds.x() - 10, metrics.height()), QString::number(i)); } // vertical grid - for (int i = 0; i < maxx; i += ystep) { - double x = range.x() + i * c.x(); + double x = pixel_bounds.x() + i * c.x(); drawLine(&painter, - QPointF(x, range.y()), - QPointF(x, range.y() + range.height()), + QPointF(x, pixel_bounds.y()), + QPointF(x, pixel_bounds.y() + pixel_bounds.height()), pen); const QString text = QString::number(i); painter.drawText(QRectF(x - metrics.width(text)/2, - range.height() + 10 + metrics.height(), + pixel_bounds.height() + 10 + metrics.height(), metrics.width(text), metrics.height()), text); } { const int i = maxx; - double x = range.x() + i * c.x(); + double x = pixel_bounds.x() + i * c.x(); drawLine(&painter, - QPointF(x, range.y()), - QPointF(x, range.y() + range.height()), + QPointF(x, pixel_bounds.y()), + QPointF(x, pixel_bounds.y() + pixel_bounds.height()), pen); const QString text = QString::number(i); painter.drawText(QRectF(x - metrics.width(text)/2, - range.height() + 10 + metrics.height(), + pixel_bounds.height() + 10 + metrics.height(), metrics.width(text), metrics.height()), text); @@ -144,9 +144,6 @@ void QFunctionConfigurator::drawFunction() { if (!_config) return; - int i; - QPointF prevPoint; - QPointF currentPoint; _function = QPixmap(_background); QPainter painter(&_function); @@ -156,19 +153,18 @@ void QFunctionConfigurator::drawFunction() QList<QPointF> points = _config->getPoints(); - for (i = 0; i < points.size(); i++) { - currentPoint = point_to_pixel( points[i] ); // Get the next point and convert it to Widget measures - drawPoint(&painter, currentPoint, QColor(200, 200, 210, 120)); - lastPoint = currentPoint; // Remember which point is the rightmost in the graph + for (int i = 0; i < points.size(); i++) { + drawPoint(&painter, + point_to_pixel(points[i]), + QColor(200, 200, 210, 120)); } + QPen pen(spline_color, 1.2, Qt::SolidLine); - QPen pen(colBezier, 1.2, Qt::SolidLine); - - prevPoint = point_to_pixel( QPointF(0,0) ); // Start at the Axis - double max = _config->maxInput(); + static const constexpr double step = 1.02; + const double max = _config->maxInput(); + QPointF prev = point_to_pixel(QPointF(0, 0)); - const double step = 1.01; for (double i = 0; i < max; i += step) { double val = _config->getValue(i); QPointF cur = point_to_pixel(QPointF(i, val)); @@ -180,66 +176,54 @@ void QFunctionConfigurator::drawFunction() void QFunctionConfigurator::paintEvent(QPaintEvent *e) { - QPointF prevPoint; - QPointF currentPoint; - QPointF actualPos; - int i; - QPainter p(this); p.setRenderHint(QPainter::Antialiasing); - if (_draw_background) { + if (_background.isNull()) drawBackground(); - _draw_background = false; - } p.drawPixmap(e->rect(), _background); if (_draw_function) { - drawFunction(); // Draw the Function on a Pixmap _draw_function = false; + drawFunction(); } - p.drawPixmap(e->rect(), _function); // Always draw the background and the function + p.drawPixmap(e->rect(), _function); if (_config) { QPen pen(Qt::white, 1, Qt::SolidLine); QList<QPointF> points = _config->getPoints(); - if (movingPoint >= 0 && movingPoint < points.size()) { - prevPoint = point_to_pixel( QPointF(0,0) ); // Start at the Axis - for (i = 0; i < points.size(); i++) { - currentPoint = point_to_pixel( points[i] ); // Get the next point and convert it to Widget measures - drawLine(&p, prevPoint, currentPoint, pen); - prevPoint = currentPoint; + if (moving_control_point_idx >= 0 && moving_control_point_idx < points.size()) { + QPointF prev; + for (int i = 0; i < points.size(); i++) { + auto tmp = point_to_pixel(points[i]); + drawLine(&p, prev, tmp, pen); + prev = tmp; } pen.setWidth(1); pen.setColor( Qt::white ); pen.setStyle( Qt::DashLine ); - actualPos = point_to_pixel(points[movingPoint]); - drawLine(&p, QPoint(range.left(), actualPos.y()), QPoint(actualPos.x(), actualPos.y()), pen); - drawLine(&p, QPoint(actualPos.x(), actualPos.y()), QPoint(actualPos.x(), range.height() + range.top()), pen); + QPointF pixel_pos = point_to_pixel(points[moving_control_point_idx]); + drawLine(&p, QPoint(pixel_bounds.left(), pixel_pos.y()), QPoint(pixel_pos.x(), pixel_pos.y()), pen); + drawLine(&p, QPoint(pixel_pos.x(), pixel_pos.y()), QPoint(pixel_pos.x(), pixel_bounds.height() + pixel_bounds.top()), pen); } - // // If the Tracker is active, the 'Last Point' it requested is recorded. // Show that point on the graph, with some lines to assist. // This new feature is very handy for tweaking the curves! - // - if (_config->getLastPoint( currentPoint )) { - actualPos = point_to_pixel( QPointF(fabs(currentPoint.x()), fabs(currentPoint.y())) ); - drawPoint(&p, actualPos, QColor(255, 0, 0, 120)); + QPointF last; + if (_config->getLastPoint(last)) { + QPointF pixel_pos = point_to_pixel( QPointF(fabs(last.x()), fabs(last.y())) ); + drawPoint(&p, pixel_pos, QColor(255, 0, 0, 120)); pen.setWidth(1); pen.setColor( Qt::black ); pen.setStyle( Qt::SolidLine ); - drawLine(&p, QPoint(range.left(), actualPos.y()), QPoint(actualPos.x(), actualPos.y()), pen); - drawLine(&p, QPoint(actualPos.x(), actualPos.y()), QPoint(actualPos.x(), range.width()), pen); + drawLine(&p, QPoint(pixel_bounds.left(), pixel_pos.y()), QPoint(pixel_pos.x(), pixel_pos.y()), pen); + drawLine(&p, QPoint(pixel_pos.x(), pixel_pos.y()), QPoint(pixel_pos.x(), pixel_bounds.width()), pen); } - } } -// -// Draw the handle, to move the Bezier-curve. -// void QFunctionConfigurator::drawPoint(QPainter *painter, const QPointF &pos, QColor colBG ) { painter->save(); @@ -251,7 +235,7 @@ void QFunctionConfigurator::drawPoint(QPainter *painter, const QPointF &pos, QCo painter->restore(); } -void QFunctionConfigurator::drawLine(QPainter *painter, const QPointF &start, const QPointF &end, QPen pen) +void QFunctionConfigurator::drawLine(QPainter *painter, const QPointF &start, const QPointF &end, QPen &pen) { painter->save(); painter->setPen(pen); @@ -267,12 +251,12 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) QList<QPointF> points = _config->getPoints(); if (e->button() == Qt::LeftButton) { bool bTouchingPoint = false; - movingPoint = -1; + moving_control_point_idx = -1; if (_config) { for (int i = 0; i < points.size(); i++) { if ( point_within_pixel(points[i], e->pos() ) ) { bTouchingPoint = true; - movingPoint = i; + moving_control_point_idx = i; timer.restart(); break; } @@ -296,7 +280,7 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) if (found_pt != -1) { _config->removePoint(found_pt); } - movingPoint = -1; + moving_control_point_idx = -1; } } _draw_function = true; @@ -307,29 +291,33 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e) { if (!_config) return; + + static const constexpr int min_refresh_delay = 25; + + if (timer.isValid() && timer.elapsed() < min_refresh_delay) + return; + + static const constexpr int refresh_delay = 50; QList<QPointF> points = _config->getPoints(); - const int refresh_delay = 50; - if (movingPoint >= 0 && movingPoint < points.size()) { + if (moving_control_point_idx >= 0 && moving_control_point_idx < points.size()) { setCursor(Qt::ClosedHandCursor); if (timer.isValid() && timer.elapsed() > refresh_delay) { timer.restart(); QPointF new_pt = pixel_coord_to_point(e->pos()); - points[movingPoint] = new_pt; - _config->movePoint(movingPoint, new_pt); + points[moving_control_point_idx] = new_pt; + _config->movePoint(moving_control_point_idx, new_pt); _draw_function = true; update(); } } else { bool bTouchingPoint = false; - if (_config) { - for (int i = 0; i < points.size(); i++) { - if ( point_within_pixel(points[i], e->pos() ) ) { - bTouchingPoint = true; - } + for (int i = 0; i < points.size(); i++) { + if ( point_within_pixel(points[i], e->pos() ) ) { + bTouchingPoint = true; } } @@ -346,37 +334,39 @@ void QFunctionConfigurator::mouseReleaseEvent(QMouseEvent *e) { if (!_config) return; + QList<QPointF> points = _config->getPoints(); if (e->button() == Qt::LeftButton) { timer.invalidate(); - if (movingPoint >= 0 && movingPoint < points.size()) { + if (moving_control_point_idx >= 0 && moving_control_point_idx < points.size()) { if (_config) { - _config->movePoint(movingPoint, pixel_coord_to_point(e->pos())); + _config->movePoint(moving_control_point_idx, pixel_coord_to_point(e->pos())); } } setCursor(Qt::ArrowCursor); - movingPoint = -1; + moving_control_point_idx = -1; } _draw_function = true; update(); } -bool QFunctionConfigurator::point_within_pixel(QPointF pt, QPointF pixel) const +bool QFunctionConfigurator::point_within_pixel(const QPointF &pt, const QPointF &pixel) { - QPointF pixel2(range.x() + pt.x() * c.x(), (range.y() + range.height() - pt.y() * c.y())); + QPointF pixel2(pixel_bounds.x() + pt.x() * c.x(), + (pixel_bounds.y() + pixel_bounds.height() - pt.y() * c.y())); return pixel2.x() >= pixel.x() - pointSize && pixel2.x() < pixel.x() + pointSize && pixel2.y() >= pixel.y() - pointSize && pixel2.y() < pixel.y() + pointSize; } -QPointF QFunctionConfigurator::pixel_coord_to_point(QPointF point) const +QPointF QFunctionConfigurator::pixel_coord_to_point(const QPointF& point) { if (!_config) return QPointF(-1, -1); - double x = (point.x() - range.x()) / c.x(); - double y = (range.height() - point.y() + range.y()) / c.y(); + double x = (point.x() - pixel_bounds.x()) / c.x(); + double y = (pixel_bounds.height() - point.y() + pixel_bounds.y()) / c.y(); if (x < 0) x = 0; @@ -391,10 +381,10 @@ QPointF QFunctionConfigurator::pixel_coord_to_point(QPointF point) const return QPointF(x, y); } -QPointF QFunctionConfigurator::point_to_pixel(QPointF point) const +QPointF QFunctionConfigurator::point_to_pixel(const QPointF& point) { - return QPointF(range.x() + point.x() * c.x(), - range.y() + range.height() - point.y() * c.y()); + return QPointF(pixel_bounds.x() + point.x() * c.x(), + pixel_bounds.y() + pixel_bounds.height() - point.y() * c.y()); } void QFunctionConfigurator::resizeEvent(QResizeEvent *) diff --git a/qfunctionconfigurator/qfunctionconfigurator.h b/qfunctionconfigurator/qfunctionconfigurator.h index e4af0062..4643f0d5 100644 --- a/qfunctionconfigurator/qfunctionconfigurator.h +++ b/qfunctionconfigurator/qfunctionconfigurator.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2011-2012 Stanislaw Halik <sthalik@misaki.pl> +/* Copyright (c) 2011-2014 Stanislaw Halik <sthalik@misaki.pl> * Adapted to FaceTrackNoIR by Wim Vriend. * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -16,73 +16,69 @@ class FTNOIR_TRACKER_BASE_EXPORT QFunctionConfigurator : public QWidget { Q_OBJECT - Q_PROPERTY(QString Name READ get_name WRITE set_name) + Q_PROPERTY(QColor colorBezier READ colorBezier WRITE setColorBezier) +public: + QFunctionConfigurator(QWidget *parent = 0); + + FunctionConfig* config(); + void setConfig(FunctionConfig* config, const QString &name); + + void saveSettings(QString settingsFile, const QString &name); QColor colorBezier() const { - return colBezier; + return spline_color; } void setColorBezier(QColor color) { - colBezier = color; + spline_color = color; update(); } - QString get_name() const { - return name; - } - void set_name(QString name) - { - this->name = name; - } -public: - QFunctionConfigurator(QWidget *parent = 0); - FunctionConfig* config(); - - void setConfig(FunctionConfig* config); - void saveSettings(QString settingsFile); -public slots: protected slots: void paintEvent(QPaintEvent *e); void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); -protected: +private: void drawBackground(); void drawFunction(); void drawPoint(QPainter *painter, const QPointF &pt, QColor colBG ); - void drawLine(QPainter *painter, const QPointF &start, const QPointF &end, QPen pen); - bool point_within_pixel(QPointF pt, QPointF pixel) const; + void drawLine(QPainter *painter, const QPointF &start, const QPointF &end, QPen& pen); + bool point_within_pixel(const QPointF& pt, const QPointF& pixel); protected: virtual void resizeEvent(QResizeEvent *); - private: - QString name; void update_range() { if (!_config) return; - double w = width(), h = height(); + + const double w = width(), h = height(); const double mwl = 40, mhl = 20; const double mwr = 15, mhr = 35; - range = QRectF(mwl, mhl, (w - mwl - mwr), (h - mhl - mhr)); - c = QPointF(range.width() / _config->maxInput(), range.height() / _config->maxOutput()); - _draw_function = _draw_background = true; + + pixel_bounds = QRectF(mwl, mhl, (w - mwl - mwr), (h - mhl - mhr)); + c = QPointF(pixel_bounds.width() / _config->maxInput(), pixel_bounds.height() / _config->maxOutput()); + _draw_function = true; + + _background = QPixmap(); + _function = QPixmap(); } - QRectF range; - QPointF lastPoint; - QPointF pixel_coord_to_point (QPointF point) const; - QPointF point_to_pixel (QPointF point) const; + QPointF pixel_coord_to_point (const QPointF& point); + QPointF point_to_pixel (const QPointF& point); - int movingPoint; + FunctionConfig* _config; + + // bounds of the rectangle user can interact with + QRectF pixel_bounds; + + int moving_control_point_idx; QElapsedTimer timer; QPointF c; - QColor colBezier; - - bool _draw_background; + QColor spline_color; + QPixmap _background; - bool _draw_function; QPixmap _function; - - FunctionConfig* _config; + bool _draw_function; }; |