From a062b1f83fac7976fc2d444ee0b49ff1d06b42e6 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 8 Jun 2016 12:57:06 +0200 Subject: spline-widget: fix float <-> double promote/demote --- spline-widget/functionconfig.cpp | 49 +++++++------ spline-widget/functionconfig.h | 14 ++-- spline-widget/qfunctionconfigurator.cpp | 120 +++++++++++++++++++------------- 3 files changed, 106 insertions(+), 77 deletions(-) (limited to 'spline-widget') diff --git a/spline-widget/functionconfig.cpp b/spline-widget/functionconfig.cpp index 72c3658a..9fdeebac 100644 --- a/spline-widget/functionconfig.cpp +++ b/spline-widget/functionconfig.cpp @@ -34,31 +34,31 @@ void Map::removeAllPoints() reload(); } -void Map::setMaxInput(float max_input) +void Map::setMaxInput(qreal max_input) { QMutexLocker l(&_mutex); max_x = max_input; } -void Map::setMaxOutput(float max_output) +void Map::setMaxOutput(qreal max_output) { QMutexLocker l(&_mutex); max_y = max_output; } -float Map::maxInput() const +qreal Map::maxInput() const { QMutexLocker l(&_mutex); return max_x; } -float Map::maxOutput() const +qreal Map::maxOutput() const { QMutexLocker l(&_mutex); return max_y; } -Map::Map(float maxx, float maxy) : +Map::Map(qreal maxx, qreal maxy) : _mutex(QMutex::Recursive), max_x(0), max_y(0), @@ -125,24 +125,27 @@ void Map::reload() { data = std::vector(value_count); const float mult = precision(); - const float mult_ = mult * 30; + const unsigned mult_ = unsigned(mult * 30); - const int sz = data.size(); + const unsigned sz = data.size(); - for (int i = 0; i < sz; i++) + for (unsigned i = 0; i < sz; i++) data[i] = -1; if (input.size() == 1 && input[0].x() > 1e-2) { - for (int k = 0; k < input[0].x() * mult; k++) { + const float x = float(input[0].x()); + const float y = float(input[0].y()); + const unsigned max = unsigned(x * mult); + for (unsigned k = 0; k < max; k++) { if (k < sz) - data[k] = input[0].y() * k / (input[0].x() * mult); + data[k] = y * k / max; } } else if (input[0].x() > 1e-2) input.prepend(QPointF(0, 0)); - for (int i = 0; i < sz; i++) { + for (unsigned i = 0; i < sz; i++) { const QPointF p0 = ensureInBounds(input, i - 1); const QPointF p1 = ensureInBounds(input, i); const QPointF p2 = ensureInBounds(input, i + 1); @@ -152,32 +155,32 @@ void Map::reload() { const float p0_y = p0.y(), p1_y = p1.y(), p2_y = p2.y(), p3_y = p3.y(); // multiplier helps fill in all the x's needed - const int end = std::min(sz, p2_x * mult_); - const int start = p1_x * mult; + const unsigned end = std::min(sz, unsigned(p2_x * mult_)); + const unsigned start = unsigned(p1_x * mult); - for (int j = start; j < end; j++) { + for (unsigned j = start; j < end; j++) { const float t = (j - start) / (float) (end - start); const float t2 = t*t; const float t3 = t*t*t; const int x = .5f * ((2 * p1_x) + - (-p0_x + p2_x) * t + - (2 * p0_x - 5 * p1_x + 4 * p2_x - p3_x) * t2 + - (-p0_x + 3 * p1_x - 3 * p2_x + p3_x) * t3) - * mult; + (-p0_x + p2_x) * t + + (2 * p0_x - 5 * p1_x + 4 * p2_x - p3_x) * t2 + + (-p0_x + 3 * p1_x - 3 * p2_x + p3_x) * t3) + * mult; const float y = .5f * ((2 * p1_y) + - (-p0_y + p2_y) * t + - (2 * p0_y - 5 * p1_y + 4 * p2_y - p3_y) * t2 + - (-p0_y + 3 * p1_y - 3 * p2_y + p3_y) * t3); + (-p0_y + p2_y) * t + + (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 < sz) + if (x >= 0 && x < (int)sz) data[x] = y; } } float last = 0; - for (int i = 0; i < sz; i++) + for (int i = 0; i < (int)sz; i++) { if (data[i] < 0) data[i] = last; diff --git a/spline-widget/functionconfig.h b/spline-widget/functionconfig.h index ced6934a..6c039831 100644 --- a/spline-widget/functionconfig.h +++ b/spline-widget/functionconfig.h @@ -39,15 +39,15 @@ private: MyMutex _mutex; QPointF last_input_value; - float max_x; - float max_y; + qreal max_x; + qreal max_y; volatile bool activep; State cur, saved; public: - float maxInput() const; - float maxOutput() const; + qreal maxInput() const; + qreal maxOutput() const; Map(); - Map(float maxx, float maxy); + Map(qreal maxx, qreal maxy); float getValue(float x); bool getLastPoint(QPointF& point); @@ -57,8 +57,8 @@ public: void addPoint(QPointF pt); void movePoint(int idx, QPointF pt); const QList getPoints(); - void setMaxInput(float MaxInput); - void setMaxOutput(float MaxOutput); + void setMaxInput(qreal MaxInput); + void setMaxOutput(qreal MaxOutput); void saveSettings(QSettings& settings, const QString& title); void loadSettings(QSettings& settings, const QString& title); diff --git a/spline-widget/qfunctionconfigurator.cpp b/spline-widget/qfunctionconfigurator.cpp index 538b5dc9..ec6a331e 100644 --- a/spline-widget/qfunctionconfigurator.cpp +++ b/spline-widget/qfunctionconfigurator.cpp @@ -28,7 +28,8 @@ QFunctionConfigurator::QFunctionConfigurator(QWidget *parent) : setMouseTracking(true); } -void QFunctionConfigurator::setConfig(Map* config, const QString& name) { +void QFunctionConfigurator::setConfig(Map* config, const QString& name) +{ mem iniFile = group::ini_file(); if (name != "") config->loadSettings(*iniFile, name); @@ -75,13 +76,13 @@ void QFunctionConfigurator::drawBackground() QPen pen(color__, 1, Qt::SolidLine); const int xstep = 10, ystep = 10; - const double maxx = _config->maxInput() + ystep; - const double maxy = _config->maxOutput() + xstep; + const qreal maxx = _config->maxInput() + ystep; + const qreal maxy = _config->maxOutput() + xstep; // horizontal grid for (int i = 0; i < maxy; i += xstep) { - const double y = pixel_bounds.height() - i * c.y() + pixel_bounds.y(); + const qreal y = pixel_bounds.height() - i * c.y() + pixel_bounds.y(); drawLine(&painter, QPointF(pixel_bounds.x(), y), QPointF(pixel_bounds.x() + pixel_bounds.width(), y), @@ -96,7 +97,7 @@ void QFunctionConfigurator::drawBackground() // vertical grid for (int i = 0; i < maxx; i += ystep) { - const double x = pixel_bounds.x() + i * c.x(); + const qreal x = pixel_bounds.x() + i * c.x(); drawLine(&painter, QPointF(x, pixel_bounds.y()), QPointF(x, pixel_bounds.y() + pixel_bounds.height()), @@ -122,45 +123,49 @@ void QFunctionConfigurator::drawFunction() QList points = _config->getPoints(); const int alpha = !isEnabled() ? 64 : 120; - for (int i = 0; i < points.size(); i++) { - drawPoint(&painter, - point_to_pixel(points[i]), - QColor(200, 200, 210, alpha), - isEnabled() ? QColor(50, 100, 120, 200) : QColor(200, 200, 200, 96)); + if (!_preview_only) + { + for (int i = 0; i < points.size(); i++) + { + drawPoint(&painter, + point_to_pixel(points[i]), + QColor(200, 200, 210, alpha), + isEnabled() ? QColor(50, 100, 120, 200) : QColor(200, 200, 200, 96)); + } } QColor color = spline_color; if (!isEnabled() && !_preview_only) { - const float avg = (color.red() + color.green() + color.blue())/3.f; - auto color_ = color; - color = QColor(int(color_.red() * .5 + avg * .5), - int(color_.green() * .5 + avg * .5), - int(color_.blue() * .5 + avg * .5), + const int avg = int(float(color.red() + color.green() + color.blue())/3); + color = QColor(int(float(color.red() + avg) * .5f), + int(float(color.green() + avg) * .5f), + int(float(color.blue() + avg) * .5f), 96); } QPen pen(color, 1.2, Qt::SolidLine); - const double step_ = line_length_pixels / c.x(); - const double step = std::max(1e-2, step_); - const double max = _config->maxInput(); + const qreal step_ = line_length_pixels / c.x(); + const qreal step = std::max(1e-2, step_); + const qreal max = _config->maxInput(); painter.save(); painter.setPen(pen); painter.setBrush(Qt::NoBrush); QPointF prev = point_to_pixel(QPointF(0, 0)); - for (double i = 0; i < max; i += step) { - const double val = _config->getValue(i); + for (qreal i = 0; i < max; i += step) + { + const qreal val = qreal(_config->getValue(float(i))); QPointF cur = point_to_pixel(QPointF(i, val)); painter.drawLine(prev, cur); prev = cur; } { - const double val = _config->getValue(max); + const qreal val = _config->getValue(float(max)); QPointF last = point_to_pixel(QPointF(max, val)); painter.drawLine(prev, last); } @@ -185,14 +190,19 @@ void QFunctionConfigurator::paintEvent(QPaintEvent *e) p.drawPixmap(e->rect(), _function); - if (_config) { + if (_config) + { QPen pen(Qt::white, 1, Qt::SolidLine); QList points = _config->getPoints(); - if (points.size() && moving_control_point_idx >= 0 && moving_control_point_idx < points.size()) { + if (points.size() && + moving_control_point_idx >= 0 && + moving_control_point_idx < points.size()) + { if (points[0].x() > 1e-2) points.prepend(QPointF(0, 0)); QPointF prev = point_to_pixel(points[0]); - for (int i = 1; i < points.size(); i++) { + for (int i = 1; i < points.size(); i++) + { auto tmp = point_to_pixel(points[i]); drawLine(&p, prev, tmp, pen); prev = tmp; @@ -203,7 +213,8 @@ void QFunctionConfigurator::paintEvent(QPaintEvent *e) // Show that point on the graph, with some lines to assist. // This new feature is very handy for tweaking the curves! QPointF last; - if (_config->getLastPoint(last) && isEnabled()) { + if (_config->getLastPoint(last) && isEnabled()) + { QPointF pixel_pos = point_to_pixel(last); drawPoint(&p, pixel_pos, QColor(255, 0, 0, 120)); } @@ -212,9 +223,6 @@ void QFunctionConfigurator::paintEvent(QPaintEvent *e) void QFunctionConfigurator::drawPoint(QPainter *painter, const QPointF &pos, QColor colBG, QColor border) { - if (_preview_only) - return; - painter->save(); painter->setPen(border); painter->setBrush( colBG ); @@ -238,18 +246,23 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) if (!_config || !isEnabled()) return; QList points = _config->getPoints(); - if (e->button() == Qt::LeftButton) { + if (e->button() == Qt::LeftButton) + { bool bTouchingPoint = false; moving_control_point_idx = -1; - if (_config) { - for (int i = 0; i < points.size(); i++) { - if (point_within_pixel(points[i], e->pos())) { + if (_config) + { + for (int i = 0; i < points.size(); i++) + { + if (point_within_pixel(points[i], e->pos())) + { bTouchingPoint = true; moving_control_point_idx = i; break; } } - if (!bTouchingPoint) { + if (!bTouchingPoint) + { bool too_close = false; const auto pos = e->pos(); @@ -270,17 +283,22 @@ void QFunctionConfigurator::mousePressEvent(QMouseEvent *e) } } - if (e->button() == Qt::RightButton) { - if (_config) { + if (e->button() == Qt::RightButton) + { + if (_config) + { int found_pt = -1; - for (int i = 0; i < points.size(); i++) { - if (point_within_pixel(points[i], e->pos())) { + for (int i = 0; i < points.size(); i++) + { + if (point_within_pixel(points[i], e->pos())) + { found_pt = i; break; } } - if (found_pt != -1) { + if (found_pt != -1) + { _config->removePoint(found_pt); } moving_control_point_idx = -1; @@ -297,7 +315,9 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e) QList points = _config->getPoints(); - if (moving_control_point_idx != -1 && moving_control_point_idx < points.size()) { + if (moving_control_point_idx != -1 && + moving_control_point_idx < points.size()) + { setCursor(Qt::ClosedHandCursor); bool overlap = false; @@ -346,20 +366,25 @@ void QFunctionConfigurator::mouseMoveEvent(QMouseEvent *e) update(); } } - else { + else + { bool is_on_point = false; - for (int i = 0; i < points.size(); i++) { + for (int i = 0; i < points.size(); i++) + { const QPoint pos = e->pos(); - if (point_within_pixel(points[i], pos)) { + if (point_within_pixel(points[i], pos)) + { is_on_point = true; break; } } - if (is_on_point) { + if (is_on_point) + { setCursor(Qt::CrossCursor); } - else { + else + { setCursor(Qt::ArrowCursor); } } @@ -370,7 +395,8 @@ void QFunctionConfigurator::mouseReleaseEvent(QMouseEvent *e) if (!_config || !isEnabled()) return; - if (e->button() == Qt::LeftButton) { + if (e->button() == Qt::LeftButton) + { mouseMoveEvent(e); setCursor(Qt::ArrowCursor); moving_control_point_idx = -1; @@ -410,8 +436,8 @@ QPointF QFunctionConfigurator::pixel_coord_to_point(const QPointF& point) if (!_config) return QPointF(-1, -1); - double x = (point.x() - pixel_bounds.x()) / c.x(); - double y = (pixel_bounds.height() - point.y() + pixel_bounds.y()) / c.y(); + qreal x = (point.x() - pixel_bounds.x()) / c.x(); + qreal y = (pixel_bounds.height() - point.y() + pixel_bounds.y()) / c.y(); if (snap_x > 0) x -= int(x) % snap_x; -- cgit v1.2.3