diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2014-09-17 12:40:39 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2014-09-17 12:41:45 +0200 |
commit | 679f424c250a38473c82bf1f997b75fbea4d3758 (patch) | |
tree | 06fee960550a851387fb9a26d85d44e5fc56649b /qfunctionconfigurator | |
parent | 4fdc2d9b3733f2b8d3edd4e10fdba0b7a525ae22 (diff) |
nix explicit malloc
simplifies, allows for copy operator
Diffstat (limited to 'qfunctionconfigurator')
-rw-r--r-- | qfunctionconfigurator/functionconfig.cpp | 143 | ||||
-rw-r--r-- | qfunctionconfigurator/functionconfig.h | 79 | ||||
-rw-r--r-- | qfunctionconfigurator/qfunctionconfigurator.cpp | 13 | ||||
-rw-r--r-- | qfunctionconfigurator/qfunctionconfigurator.h | 23 |
4 files changed, 131 insertions, 127 deletions
diff --git a/qfunctionconfigurator/functionconfig.cpp b/qfunctionconfigurator/functionconfig.cpp index 85d058fb..640db1ef 100644 --- a/qfunctionconfigurator/functionconfig.cpp +++ b/qfunctionconfigurator/functionconfig.cpp @@ -17,36 +17,17 @@ #include <math.h> #include <QPixmap> -FunctionConfig::FunctionConfig(QString title, int intMaxInput, int intMaxOutput) : - _mutex(QMutex::Recursive) -{ - _title = title; - _points = QList<QPointF>(); - _data = 0; - _size = 0; - lastValueTracked = QPointF(0,0); - _tracking_active = false; - _max_Input = intMaxInput; - _max_Output = intMaxOutput; - QSettings settings("opentrack"); - QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString(); - QSettings iniFile( currentFile, QSettings::IniFormat ); - loadSettings(iniFile); - reload(); -} - void FunctionConfig::setTrackingActive(bool blnActive) { - _tracking_active = blnActive; + activep = blnActive; } FunctionConfig::FunctionConfig() : _mutex(QMutex::Recursive), - _data(0), - _size(0), - _tracking_active(false), - _max_Input(0), - _max_Output(0) + data(0), + activep(false), + max_x(0), + max_y(0) { } @@ -54,31 +35,30 @@ float FunctionConfig::getValue(float x) { QMutexLocker foo(&_mutex); int x2 = (int) (std::min<float>(std::max<float>(x, -360), 360) * MEMOIZE_PRECISION); float ret = getValueInternal(x2); - lastValueTracked.setX(x); - lastValueTracked.setY(ret); + last_input_value.setX(x); + last_input_value.setY(ret); return ret; } bool FunctionConfig::getLastPoint(QPointF& point ) { QMutexLocker foo(&_mutex); - point = lastValueTracked; - return _tracking_active; + point = last_input_value; + return activep; } float FunctionConfig::getValueInternal(int x) { float sign = x < 0 ? -1 : 1; x = x < 0 ? -x : x; float ret; - if (!_data) - ret = 0; - else if (_size == 0) + int sz = data.size(); + if (sz == 0) ret = 0; else if (x < 0) ret = 0; - else if (x < _size && x >= 0) - ret = _data[x]; + else if (x < sz && x >= 0) + ret = data[x]; else - ret = _data[_size - 1]; + ret = data[sz - 1]; return ret * sign; } @@ -96,35 +76,32 @@ static bool sortFn(const QPointF& one, const QPointF& two) { } void FunctionConfig::reload() { - _size = 0; + if (input.size()) + qStableSort(input.begin(), input.end(), sortFn); - if (_points.size()) - qStableSort(_points.begin(), _points.end(), sortFn); + if (input.size()) { + data = std::vector<float>(MEMOIZE_PRECISION * input[input.size() - 1].x()); + + const int sz = data.size(); - if (_data) - delete[] _data; - _data = NULL; - if (_points.size()) { - _data = new float[_size = MEMOIZE_PRECISION * _points[_points.size() - 1].x()]; + for (int i = 0; i < sz; i++) + data[i] = -1; - for (int i = 0; i < _size; i++) - _data[i] = -1e6; - - for (int k = 0; k < _points[0].x() * MEMOIZE_PRECISION; k++) { - if (k < _size) - _data[k] = _points[0].y() * k / (_points[0].x() * MEMOIZE_PRECISION); + for (int k = 0; k < input[0].x() * MEMOIZE_PRECISION; k++) { + if (k < sz) + data[k] = input[0].y() * k / (input[0].x() * MEMOIZE_PRECISION); } - for (int i = 0; i < _points.size(); i++) { - QPointF p0 = ensureInBounds(_points, i - 1); - QPointF p1 = ensureInBounds(_points, i); - QPointF p2 = ensureInBounds(_points, i + 1); - QPointF p3 = ensureInBounds(_points, i + 2); + for (int i = 0; i < sz; i++) { + QPointF p0 = ensureInBounds(input, i - 1); + QPointF p1 = ensureInBounds(input, i); + QPointF p2 = ensureInBounds(input, i + 1); + QPointF p3 = ensureInBounds(input, i + 2); - int end = p2.x() * MEMOIZE_PRECISION; + int end = std::min<int>(sz, p2.x() * MEMOIZE_PRECISION); int start = p1.x() * MEMOIZE_PRECISION; - for (int j = start; j < end && j < _size; j++) { + for (int j = start; j < end; j++) { double t = (j - start) / (double) (end - start); double t2 = t*t; double t3 = t*t*t; @@ -140,47 +117,41 @@ void FunctionConfig::reload() { (2. * p0.y() - 5. * p1.y() + 4. * p2.y() - p3.y()) * t2 + (-p0.y() + 3. * p1.y() - 3. * p2.y() + p3.y()) * t3); - if (x >= 0 && x < _size) - _data[x] = y; + if (x >= 0 && x < sz) + data[x] = y; } } float last = 0; - - for (int i = 0; i < _size; i++) + for (int i = 0; i < sz; i++) { - if (_data[i] == -1e6) - _data[i] = last; - last = _data[i]; + if (data[i] <= 0) + data[i] = last; + last = data[i]; } } } -FunctionConfig::~FunctionConfig() { - if (_data) - delete[] _data; -} - void FunctionConfig::removePoint(int i) { QMutexLocker foo(&_mutex); - if (i >= 0 && i < _points.size()) + if (i >= 0 && i < input.size()) { - _points.removeAt(i); + input.removeAt(i); reload(); } } void FunctionConfig::addPoint(QPointF pt) { QMutexLocker foo(&_mutex); - _points.append(pt); + input.append(pt); reload(); } void FunctionConfig::movePoint(int idx, QPointF pt) { QMutexLocker foo(&_mutex); - if (idx >= 0 && idx < _points.size()) + if (idx >= 0 && idx < input.size()) { - _points[idx] = pt; + input[idx] = pt; reload(); } } @@ -188,45 +159,45 @@ void FunctionConfig::movePoint(int idx, QPointF pt) { QList<QPointF> FunctionConfig::getPoints() { QList<QPointF> ret; QMutexLocker foo(&_mutex); - for (int i = 0; i < _points.size(); i++) { - ret.append(_points[i]); + for (int i = 0; i < input.size(); i++) { + ret.append(input[i]); } return ret; } -void FunctionConfig::loadSettings(QSettings& settings) { +void FunctionConfig::loadSettings(QSettings& settings, const QString& title) { QMutexLocker foo(&_mutex); QPointF newPoint; QList<QPointF> points; - settings.beginGroup(QString("Curves-%1").arg(_title)); + settings.beginGroup(QString("Curves-%1").arg(title)); int max = settings.value("point-count", 0).toInt(); for (int i = 0; i < max; i++) { newPoint = QPointF(settings.value(QString("point-%1-x").arg(i), 0).toFloat(), settings.value(QString("point-%1-y").arg(i), 0).toFloat()); - if (newPoint.x() > _max_Input) { - newPoint.setX(_max_Input); + if (newPoint.x() > max_x) { + newPoint.setX(max_x); } - if (newPoint.y() > _max_Output) { - newPoint.setY(_max_Output); + if (newPoint.y() > max_y) { + newPoint.setY(max_y); } points.append(newPoint); } settings.endGroup(); - _points = points; + input = points; reload(); } -void FunctionConfig::saveSettings(QSettings& settings) { +void FunctionConfig::saveSettings(QSettings& settings, const QString& title) { QMutexLocker foo(&_mutex); - settings.beginGroup(QString("Curves-%1").arg(_title)); - int max = _points.size(); + settings.beginGroup(QString("Curves-%1").arg(title)); + int max = input.size(); settings.setValue("point-count", max); for (int i = 0; i < max; i++) { - settings.setValue(QString("point-%1-x").arg(i), _points[i].x()); - settings.setValue(QString("point-%1-y").arg(i), _points[i].y()); + settings.setValue(QString("point-%1-x").arg(i), input[i].x()); + settings.setValue(QString("point-%1-y").arg(i), input[i].y()); } for (int i = max; true; i++) diff --git a/qfunctionconfigurator/functionconfig.h b/qfunctionconfigurator/functionconfig.h index 9b43f0bd..22bfc406 100644 --- a/qfunctionconfigurator/functionconfig.h +++ b/qfunctionconfigurator/functionconfig.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2011-2012, Stanislaw Halik <sthalik@misaki.pl> +/* 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, @@ -14,36 +14,72 @@ #include <QSettings> #include <QMutex> #include "ftnoir_tracker_base/ftnoir_tracker_base.h" +#include <vector> #define MEMOIZE_PRECISION 100 +class MyMutex { +private: + QMutex inner; + +public: + QMutex* operator->() { return &inner; } + QMutex* operator->() const { return &const_cast<MyMutex*>(this)->inner; } + + MyMutex operator=(const MyMutex& datum) + { + auto mode = + datum->isRecursive() + ? QMutex::Recursive + : QMutex::NonRecursive; + + return MyMutex(mode); + } + + MyMutex(const MyMutex& datum) + { + *this = datum; + } + + MyMutex(QMutex::RecursionMode mode = QMutex::NonRecursive) : + inner(mode) + { + } + + QMutex* operator&() + { + return &inner; + } +}; + class FTNOIR_TRACKER_BASE_EXPORT FunctionConfig { private: - QMutex _mutex; - QList<QPointF> _points; - void reload(); - float* _data; - int _size; - QString _title; + void reload(); float getValueInternal(int x); - QPointF lastValueTracked; - volatile bool _tracking_active; - int _max_Input; - int _max_Output; - FunctionConfig(const FunctionConfig&) = delete; + + MyMutex _mutex; + QList<QPointF> input; + std::vector<float> data; + QPointF last_input_value; + volatile bool activep; + int max_x; + int max_y; public: - int maxInput() const { return _max_Input; } - int maxOutput() const { return _max_Output; } + int maxInput() const { return max_x; } + int maxOutput() const { return max_y; } FunctionConfig(); - FunctionConfig(QString title, int intMaxInput, int intMaxOutput); - ~FunctionConfig(); + FunctionConfig(int maxx, int maxy) + { + setMaxInput(maxx); + setMaxOutput(maxy); + } float getValue(float x); bool getLastPoint(QPointF& point); void removePoint(int i); void removeAllPoints() { QMutexLocker foo(&_mutex); - _points.clear(); + input.clear(); reload(); } @@ -51,15 +87,14 @@ public: void movePoint(int idx, QPointF pt); QList<QPointF> getPoints(); void setMaxInput(int MaxInput) { - _max_Input = MaxInput; + max_x = MaxInput; } void setMaxOutput(int MaxOutput) { - _max_Output = MaxOutput; + max_y = MaxOutput; } - void saveSettings(QSettings& settings); - void loadSettings(QSettings& settings); + void saveSettings(QSettings& settings, const QString& title); + void loadSettings(QSettings& settings, const QString& title); void setTrackingActive(bool blnActive); - QString getTitle() { return _title; } }; diff --git a/qfunctionconfigurator/qfunctionconfigurator.cpp b/qfunctionconfigurator/qfunctionconfigurator.cpp index e94eded4..a197dadb 100644 --- a/qfunctionconfigurator/qfunctionconfigurator.cpp +++ b/qfunctionconfigurator/qfunctionconfigurator.cpp @@ -41,7 +41,7 @@ void QFunctionConfigurator::setConfig(FunctionConfig* config) { QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/settings/default.ini" ).toString(); QSettings iniFile( currentFile, QSettings::IniFormat ); - config->loadSettings(iniFile); + config->loadSettings(iniFile, name); _config = config; _draw_function = _draw_background = true; update_range(); @@ -52,7 +52,7 @@ void QFunctionConfigurator::saveSettings(QString settingsFile) { QSettings iniFile( settingsFile, QSettings::IniFormat ); // Application settings (in INI-file) if (_config) { - _config->saveSettings(iniFile); + _config->saveSettings(iniFile, name); } } @@ -279,7 +279,6 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) } if (!bTouchingPoint) { _config->addPoint(pixel_coord_to_point(e->pos())); - emit CurveChanged( true ); } } } @@ -296,7 +295,6 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) if (found_pt != -1) { _config->removePoint(found_pt); - emit CurveChanged( true ); } movingPoint = -1; } @@ -353,7 +351,6 @@ void QFunctionConfigurator::mouseReleaseEvent(QMouseEvent *e) if (e->button() == Qt::LeftButton) { timer.invalidate(); if (movingPoint >= 0 && movingPoint < points.size()) { - emit CurveChanged( true ); if (_config) { _config->movePoint(movingPoint, pixel_coord_to_point(e->pos())); } @@ -400,12 +397,6 @@ QPointF QFunctionConfigurator::point_to_pixel(QPointF point) const range.y() + range.height() - point.y() * c.y()); } -void QFunctionConfigurator::setColorBezier(QColor color) -{ - colBezier = color; - update(); -} - void QFunctionConfigurator::resizeEvent(QResizeEvent *) { update_range(); diff --git a/qfunctionconfigurator/qfunctionconfigurator.h b/qfunctionconfigurator/qfunctionconfigurator.h index d3a8741f..e4af0062 100644 --- a/qfunctionconfigurator/qfunctionconfigurator.h +++ b/qfunctionconfigurator/qfunctionconfigurator.h @@ -16,40 +16,47 @@ class FTNOIR_TRACKER_BASE_EXPORT QFunctionConfigurator : public QWidget { Q_OBJECT - Q_PROPERTY(QColor colorBezier READ colorBezier WRITE setColorBezier) + Q_PROPERTY(QString Name READ get_name WRITE set_name) + QColor colorBezier() const { return colBezier; } + void setColorBezier(QColor color) + { + colBezier = 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); - -signals: - void CurveChanged(bool); - public slots: - void setColorBezier(QColor); protected slots: void paintEvent(QPaintEvent *e); void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); - protected: 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; - protected: virtual void resizeEvent(QResizeEvent *); private: + QString name; void update_range() { if (!_config) return; |