Fix roundings with lround()
This commit is contained in:
committed by
Jonas Kvinge
parent
d5b0794b00
commit
1a967597e8
@@ -21,6 +21,8 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
@@ -539,7 +541,7 @@ QString RatingItemDelegate::displayText(const QVariant &value, const QLocale&) c
|
|||||||
if (value.isNull() || value.toDouble() <= 0) return QString();
|
if (value.isNull() || value.toDouble() <= 0) return QString();
|
||||||
|
|
||||||
// Round to the nearest 0.5
|
// Round to the nearest 0.5
|
||||||
const double rating = double(int(value.toDouble() * RatingPainter::kStarCount * 2 + 0.5)) / 2;
|
const double rating = double(lround(value.toDouble() * RatingPainter::kStarCount * 2)) / 2;
|
||||||
|
|
||||||
return QString::number(rating, 'f', 1);
|
return QString::number(rating, 'f', 1);
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
@@ -180,7 +181,7 @@ class RatingComparatorDecorator : public SearchTermComparator {
|
|||||||
public:
|
public:
|
||||||
explicit RatingComparatorDecorator(SearchTermComparator *cmp) : cmp_(cmp) {}
|
explicit RatingComparatorDecorator(SearchTermComparator *cmp) : cmp_(cmp) {}
|
||||||
bool Matches(const QString &element) const override {
|
bool Matches(const QString &element) const override {
|
||||||
return cmp_->Matches(QString::number(static_cast<int>(element.toDouble() * 10.0 + 0.5)));
|
return cmp_->Matches(QString::number(lround(element.toDouble() * 10.0)));
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
QScopedPointer<SearchTermComparator> cmp_;
|
QScopedPointer<SearchTermComparator> cmp_;
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
|
|
||||||
#include "ratingwidget.h"
|
#include "ratingwidget.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include <QStyleOptionFrame>
|
#include <QStyleOptionFrame>
|
||||||
#include <QStylePainter>
|
#include <QStylePainter>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
@@ -91,7 +93,7 @@ double RatingPainter::RatingForPos(const QPoint pos, const QRect rect) {
|
|||||||
if (raw > 1) return 1;
|
if (raw > 1) return 1;
|
||||||
|
|
||||||
// Round to the nearest 0.1
|
// Round to the nearest 0.1
|
||||||
return double(int(raw * kStarCount * 2 + 0.5)) / (kStarCount * 2);
|
return double(lround(raw * kStarCount * 2)) / (kStarCount * 2);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +105,7 @@ void RatingPainter::Paint(QPainter *painter, const QRect rect, double rating) co
|
|||||||
rating *= kStarCount;
|
rating *= kStarCount;
|
||||||
|
|
||||||
// Draw the stars
|
// Draw the stars
|
||||||
const int star = qBound(0, int(rating * 2.0 + 0.5), kStarCount * 2);
|
const int star = qBound(0, static_cast<int>(lround(rating * 2.0)), kStarCount * 2);
|
||||||
painter->drawPixmap(QRect(pos, size), stars_[star], QRect(QPoint(0, 0), size));
|
painter->drawPixmap(QRect(pos, size), stars_[star], QRect(QPoint(0, 0), size));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user