diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-03-17 15:45:38 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-03-18 22:34:13 +0100 |
commit | 180f78d12680f1954f9ea3f4b00200ecfa1a853e (patch) | |
tree | abfb3c1c010ee777304c5bd5f36dc9255c3ca593 /src/widgets/widgets/qdial.cpp | |
parent | 2b9bc46670cd4d7e71d7ad157619552dd90a57a8 (diff) |
QDial: use qRound to round
The calculation rounds early and often, which is intentional. Add
unit test to make sure we don't regress.
Fixes static analzyer report about incorrect rounding in
c903a34347776fe3b89785faa35c446d.
Address some outdated comments and documentation. The property
is read only and calculated, so don't imply that it can be changed
from its default value.
Change-Id: If2dbd9890e533dfccda3eae4cbc96db4f1246f4d
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'src/widgets/widgets/qdial.cpp')
-rw-r--r-- | src/widgets/widgets/qdial.cpp | 22 |
1 files changed, 8 insertions, 14 deletions
diff --git a/src/widgets/widgets/qdial.cpp b/src/widgets/widgets/qdial.cpp index 934be4fb6e..594fe0a779 100644 --- a/src/widgets/widgets/qdial.cpp +++ b/src/widgets/widgets/qdial.cpp @@ -370,12 +370,10 @@ bool QDial::wrapping() const \property QDial::notchSize \brief the current notch size - The notch size is in range control units, not pixels, and if - possible it is a multiple of singleStep() that results in an + The notch size is in range control units, not pixels, and is + calculated to be a multiple of singleStep() that results in an on-screen notch size near notchTarget(). - By default, this property has a value of 1. - \sa notchTarget(), singleStep() */ @@ -383,21 +381,17 @@ int QDial::notchSize() const { Q_D(const QDial); // radius of the arc - int r = qMin(width(), height())/2; + qreal r = qMin(width(), height())/2.0; // length of the whole arc - int l = (int)(r * (d->wrapping ? 6 : 5) * Q_PI / 6); + int l = qRound(r * (d->wrapping ? 6.0 : 5.0) * Q_PI / 6.0); // length of the arc from minValue() to minValue()+pageStep() if (d->maximum > d->minimum + d->pageStep) - l = (int)(0.5 + l * d->pageStep / (d->maximum - d->minimum)); + l = qRound(l * d->pageStep / double(d->maximum - d->minimum)); // length of a singleStep arc - l = l * d->singleStep / (d->pageStep ? d->pageStep : 1); - if (l < 1) - l = 1; + l = qMax(l * d->singleStep / (d->pageStep ? d->pageStep : 1), 1); // how many times singleStep can be draw in d->target pixels - l = (int)(0.5 + d->target / l); - // we want notchSize() to be a non-zero multiple of lineStep() - if (!l) - l = 1; + l = qMax(qRound(d->target / l), 1); + // we want notchSize() to be a non-zero multiple of singleStep() return d->singleStep * l; } |