aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/common/numericspinbox.cpp
blob: 5a4803337466c08457220a5df22e4c83ccb0b5bb (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "numericspinbox.h"
#include "common/unused.h"
#include <QLineEdit>
#include <QVariant>
#include <QDebug>

NumericSpinBox::NumericSpinBox(QWidget *parent) :
    QAbstractSpinBox(parent)
{
    connect(lineEdit(), &QLineEdit::textChanged, this, &NumericSpinBox::valueEdited);
}

void NumericSpinBox::stepBy(int steps)
{
    if (isReadOnly())
        return;

    switch (value.userType())
    {
        case QVariant::Double:
            stepDoubleBy(steps);
            break;
        case QVariant::Int:
        case QVariant::LongLong:
            stepIntBy(steps);
            break;
        default:
            break;
    }
    updateText();
}

QValidator::State NumericSpinBox::validate(QString& input, int& pos) const
{
    UNUSED(input);
    UNUSED(pos);
    emit modified();

    if (strict)
        return validateStrict(input, pos);

    return QValidator::Acceptable;
}

void NumericSpinBox::stepIntBy(int steps)
{
    qint64 intVal = value.toLongLong();
    intVal += steps;
    value = intVal;
    emit modified();
}

void NumericSpinBox::stepDoubleBy(int steps)
{
    double doubleVal = value.toDouble();
    doubleVal += steps;
    value = doubleVal;
    emit modified();
}

void NumericSpinBox::updateText()
{
    lineEdit()->setText(value.toString());
}

QValidator::State NumericSpinBox::validateStrict(QString& input, int& pos) const
{
    if (input.trimmed().isEmpty())
        return allowEmpty ? QValidator::Acceptable : QValidator::Invalid;

    QIntValidator vint;
    if (vint.validate(input, pos) != QValidator::Invalid)
        return QValidator::Acceptable;

    QDoubleValidator dint;
    if (dint.validate(input, pos) != QValidator::Invalid)
        return QValidator::Acceptable;

    return QValidator::Invalid;
}
bool NumericSpinBox::getAllowEmpty() const
{
    return allowEmpty;
}

void NumericSpinBox::setAllowEmpty(bool value)
{
    allowEmpty = value;
}


bool NumericSpinBox::isStrict() const
{
    return strict;
}

void NumericSpinBox::setStrict(bool value, bool allowEmpty)
{
    strict = value;
    this->allowEmpty = allowEmpty;
}

void NumericSpinBox::valueEdited(const QString& value)
{
    setValueInternal(value);
}

QAbstractSpinBox::StepEnabled NumericSpinBox::stepEnabled() const
{
    return StepDownEnabled|StepUpEnabled;
}

QVariant NumericSpinBox::getFixedVariant(const QVariant& value)
{
    bool ok;
    qint64 longVal = value.toLongLong(&ok);
    if (ok)
        return longVal;

    return value.toDouble();
}

void NumericSpinBox::setValueInternal(const QVariant& newValue)
{
    switch (newValue.userType())
    {
        case QVariant::String:
            value = getFixedVariant(newValue);
            break;
        case QVariant::Double:
        case QVariant::Int:
        case QVariant::LongLong:
            value = newValue;
            break;
        default:
            value = 0;
    }
}

QVariant NumericSpinBox::getValue() const
{
    return value;
}

void NumericSpinBox::setValue(const QVariant& newValue, bool nullAsZero)
{
    setValueInternal(newValue);
    if (!nullAsZero && newValue.isNull())
        value = newValue;

    updateText();
}