aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickspinbox.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quicktemplates2/qquickspinbox.cpp')
-rw-r--r--src/quicktemplates2/qquickspinbox.cpp86
1 files changed, 69 insertions, 17 deletions
diff --git a/src/quicktemplates2/qquickspinbox.cpp b/src/quicktemplates2/qquickspinbox.cpp
index 1989d768..ef1c9d97 100644
--- a/src/quicktemplates2/qquickspinbox.cpp
+++ b/src/quicktemplates2/qquickspinbox.cpp
@@ -108,6 +108,7 @@ class QQuickSpinBoxPrivate : public QQuickControlPrivate
public:
QQuickSpinBoxPrivate()
: editable(false),
+ wrap(false),
from(0),
to(99),
value(0),
@@ -121,9 +122,10 @@ public:
{
}
- int boundValue(int value) const;
+ int boundValue(int value, bool wrap) const;
void updateValue();
- bool setValue(int value, bool modified);
+ bool setValue(int value, bool wrap, bool modified);
+ bool stepBy(int steps, bool modified);
void increase(bool modified);
void decrease(bool modified);
@@ -145,6 +147,7 @@ public:
void handleUngrab() override;
bool editable;
+ bool wrap;
int from;
int to;
int value;
@@ -159,9 +162,20 @@ public:
Qt::InputMethodHints inputMethodHints;
};
-int QQuickSpinBoxPrivate::boundValue(int value) const
+int QQuickSpinBoxPrivate::boundValue(int value, bool wrap) const
{
- return from > to ? qBound(to, value, from) : qBound(from, value, to);
+ bool inverted = from > to;
+ if (!wrap)
+ return inverted ? qBound(to, value, from) : qBound(from, value, to);
+
+ int f = inverted ? to : from;
+ int t = inverted ? from : to;
+ if (value < f)
+ value = t;
+ else if (value > t)
+ value = f;
+
+ return value;
}
void QQuickSpinBoxPrivate::updateValue()
@@ -175,17 +189,17 @@ void QQuickSpinBoxPrivate::updateValue()
QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine);
QJSValue loc(v4, QQmlLocale::wrap(v4, locale));
QJSValue val = q->valueFromText().call(QJSValueList() << text.toString() << loc);
- setValue(val.toInt(), true);
+ setValue(val.toInt(), /* allowWrap = */ false, /* modified = */ true);
}
}
}
}
-bool QQuickSpinBoxPrivate::setValue(int newValue, bool modified)
+bool QQuickSpinBoxPrivate::setValue(int newValue, bool allowWrap, bool modified)
{
Q_Q(QQuickSpinBox);
if (q->isComponentComplete())
- newValue = boundValue(newValue);
+ newValue = boundValue(newValue, allowWrap);
if (value == newValue)
return false;
@@ -201,14 +215,19 @@ bool QQuickSpinBoxPrivate::setValue(int newValue, bool modified)
return true;
}
+bool QQuickSpinBoxPrivate::stepBy(int steps, bool modified)
+{
+ return setValue(value + steps, wrap, modified);
+}
+
void QQuickSpinBoxPrivate::increase(bool modified)
{
- setValue(value + effectiveStepSize(), modified);
+ setValue(value + effectiveStepSize(), wrap, modified);
}
void QQuickSpinBoxPrivate::decrease(bool modified)
{
- setValue(value - effectiveStepSize(), modified);
+ setValue(value - effectiveStepSize(), wrap, modified);
}
int QQuickSpinBoxPrivate::effectiveStepSize() const
@@ -228,7 +247,7 @@ void QQuickSpinBoxPrivate::updateUpEnabled()
if (!upIndicator)
return;
- upIndicator->setEnabled(from < to ? value < to : value > to);
+ upIndicator->setEnabled(wrap || (from < to ? value < to : value > to));
}
bool QQuickSpinBoxPrivate::downEnabled() const
@@ -243,7 +262,7 @@ void QQuickSpinBoxPrivate::updateDownEnabled()
if (!downIndicator)
return;
- downIndicator->setEnabled(from < to ? value > from : value < from);
+ downIndicator->setEnabled(wrap || (from < to ? value > from : value < from));
}
void QQuickSpinBoxPrivate::updateHover(const QPointF &pos)
@@ -384,7 +403,7 @@ void QQuickSpinBox::setFrom(int from)
d->from = from;
emit fromChanged();
if (isComponentComplete()) {
- if (!d->setValue(d->value, false)) {
+ if (!d->setValue(d->value, /* allowWrap = */ false, /* modified = */ false)) {
d->updateUpEnabled();
d->updateDownEnabled();
}
@@ -413,7 +432,7 @@ void QQuickSpinBox::setTo(int to)
d->to = to;
emit toChanged();
if (isComponentComplete()) {
- if (!d->setValue(d->value, false)) {
+ if (!d->setValue(d->value, /* allowWrap = */false, /* modified = */ false)) {
d->updateUpEnabled();
d->updateDownEnabled();
}
@@ -434,7 +453,7 @@ int QQuickSpinBox::value() const
void QQuickSpinBox::setValue(int value)
{
Q_D(QQuickSpinBox);
- d->setValue(value, false);
+ d->setValue(value, /* allowWrap = */ false, /* modified = */ false);
}
/*!
@@ -706,6 +725,34 @@ bool QQuickSpinBox::isInputMethodComposing() const
}
/*!
+ \since QtQuick.Controls 2.3 (Qt 5.10)
+ \qmlproperty bool QtQuick.Controls::SpinBox::wrap
+
+ This property holds whether the spinbox wraps. The default value is \c false.
+
+ If wrap is \c true, stepping past \l to changes the value to \l from and vice versa.
+*/
+bool QQuickSpinBox::wrap() const
+{
+ Q_D(const QQuickSpinBox);
+ return d->wrap;
+}
+
+void QQuickSpinBox::setWrap(bool wrap)
+{
+ Q_D(QQuickSpinBox);
+ if (d->wrap == wrap)
+ return;
+
+ d->wrap = wrap;
+ if (d->value == d->from || d->value == d->to) {
+ d->updateUpEnabled();
+ d->updateDownEnabled();
+ }
+ emit wrapChanged();
+}
+
+/*!
\qmlmethod void QtQuick.Controls::SpinBox::increase()
Increases the value by \l stepSize, or \c 1 if stepSize is not defined.
@@ -827,8 +874,8 @@ void QQuickSpinBox::wheelEvent(QWheelEvent *event)
if (d->wheelEnabled) {
const QPointF angle = event->angleDelta();
const qreal delta = (qFuzzyIsNull(angle.y()) ? angle.x() : angle.y()) / QWheelEvent::DefaultDeltasPerStep;
- if (!d->setValue(d->value + qRound(d->effectiveStepSize() * delta), true))
- event->ignore();
+ if (!d->stepBy(qRound(d->effectiveStepSize() * delta), true))
+ event->ignore();
}
}
#endif
@@ -837,7 +884,7 @@ void QQuickSpinBox::componentComplete()
{
Q_D(QQuickSpinBox);
QQuickControl::componentComplete();
- if (!d->setValue(d->value, false)) {
+ if (!d->setValue(d->value, /* allowWrap = */ false, /* modified = */ false)) {
d->updateUpEnabled();
d->updateDownEnabled();
}
@@ -874,6 +921,11 @@ QFont QQuickSpinBox::defaultFont() const
return QQuickControlPrivate::themeFont(QPlatformTheme::EditorFont);
}
+QPalette QQuickSpinBox::defaultPalette() const
+{
+ return QQuickControlPrivate::themePalette(QPlatformTheme::TextLineEditPalette);
+}
+
#if QT_CONFIG(accessibility)
QAccessible::Role QQuickSpinBox::accessibleRole() const
{