File: adaptivedoublespinbox.cpp

package info (click to toggle)
apitrace 13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,752 kB
  • sloc: cpp: 183,974; python: 33,969; ansic: 25,566; sh: 169; makefile: 88; sed: 3
file content (29 lines) | stat: -rw-r--r-- 829 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "adaptivedoublespinbox.h"

#include <cmath>

AdaptiveDoubleSpinBox::AdaptiveDoubleSpinBox(QWidget *parent)
    : QDoubleSpinBox(parent)
{
}

void AdaptiveDoubleSpinBox::stepBy(int steps)
{
    const double oldValue = value();
    const double oldAbsValue = qAbs(oldValue);
    const double oldStep = std::pow(10, std::floor(std::log10(oldAbsValue)));

    const double newValueWithOldStep = oldValue + steps * oldStep;
    const double newAbsValueWithOldStep = qAbs(newValueWithOldStep);
    const double newStep = std::pow(10, std::floor(std::log10(newAbsValueWithOldStep)));

    if (newStep < oldStep) {
        if (oldAbsValue != oldStep) {
            setValue(oldStep);
        } else {
            setValue(oldValue + steps * (oldStep / 10));
        }
    } else {
        setValue(newValueWithOldStep);
    }
}