diff options
Diffstat (limited to 'src/templates/qquickspinbox.cpp')
-rw-r--r-- | src/templates/qquickspinbox.cpp | 137 |
1 files changed, 91 insertions, 46 deletions
diff --git a/src/templates/qquickspinbox.cpp b/src/templates/qquickspinbox.cpp index 255715e1..78a2d245 100644 --- a/src/templates/qquickspinbox.cpp +++ b/src/templates/qquickspinbox.cpp @@ -56,13 +56,14 @@ static const int AUTO_REPEAT_INTERVAL = 100; \instantiates QQuickSpinBox \inqmlmodule Qt.labs.controls \ingroup input - \brief A spinbox control. + \brief A spinbox control that allows the user from a set of preset values. \image qtlabscontrols-spinbox.png SpinBox allows the user to choose an integer value by clicking the up - or down indicator buttons, by pressing up or down on the keyboard, or - by entering a text value in the input field. + or down indicator buttons, or by pressing up or down on the keyboard. + Optionally, SpinBox can be also made \l editable, so the user can enter + a text value in the input field. By default, SpinBox provides discrete values in the range of \c [0-99] with a \l stepSize of \c 1. @@ -90,8 +91,8 @@ class QQuickSpinBoxPrivate : public QQuickControlPrivate Q_DECLARE_PUBLIC(QQuickSpinBox) public: - QQuickSpinBoxPrivate() : from(0), to(99), value(0), stepSize(1), - delayTimer(0), repeatTimer(0), up(Q_NULLPTR), down(Q_NULLPTR), validator(Q_NULLPTR) { } + QQuickSpinBoxPrivate() : editable(false), from(0), to(99), value(0), stepSize(1), + delayTimer(0), repeatTimer(0), up(nullptr), down(nullptr), validator(nullptr) { } int boundValue(int value) const; void updateValue(); @@ -107,6 +108,7 @@ public: bool handleMouseReleaseEvent(QQuickItem *child, QMouseEvent *event); bool handleMouseUngrabEvent(QQuickItem *child); + bool editable; int from; int to; int value; @@ -260,12 +262,13 @@ int QQuickSpinBox::from() const void QQuickSpinBox::setFrom(int from) { Q_D(QQuickSpinBox); - if (d->from != from) { - d->from = from; - emit fromChanged(); - if (isComponentComplete()) - setValue(d->value); - } + if (d->from == from) + return; + + d->from = from; + emit fromChanged(); + if (isComponentComplete()) + setValue(d->value); } /*! @@ -284,12 +287,13 @@ int QQuickSpinBox::to() const void QQuickSpinBox::setTo(int to) { Q_D(QQuickSpinBox); - if (d->to != to) { - d->to = to; - emit toChanged(); - if (isComponentComplete()) - setValue(d->value); - } + if (d->to == to) + return; + + d->to = to; + emit toChanged(); + if (isComponentComplete()) + setValue(d->value); } /*! @@ -309,10 +313,11 @@ void QQuickSpinBox::setValue(int value) if (isComponentComplete()) value = d->boundValue(value); - if (d->value != value) { - d->value = value; - emit valueChanged(); - } + if (d->value == value) + return; + + d->value = value; + emit valueChanged(); } /*! @@ -331,21 +336,45 @@ int QQuickSpinBox::stepSize() const void QQuickSpinBox::setStepSize(int step) { Q_D(QQuickSpinBox); - if (d->stepSize != step) { - d->stepSize = step; - emit stepSizeChanged(); - } + if (d->stepSize == step) + return; + + d->stepSize = step; + emit stepSizeChanged(); +} + +/*! + \qmlproperty bool Qt.labs.controls::SpinBox::editable + + This property holds whether the spinbox is editable. The default value is \c false. + + \sa validator +*/ +bool QQuickSpinBox::isEditable() const +{ + Q_D(const QQuickSpinBox); + return d->editable; +} + +void QQuickSpinBox::setEditable(bool editable) +{ + Q_D(QQuickSpinBox); + if (d->editable == editable) + return; + + d->editable = editable; + emit editableChanged(); } /*! \qmlproperty Validator Qt.labs.controls::SpinBox::validator - This property holds the input text validator. By default, SpinBox uses - \l IntValidator to accept input of integer numbers. + This property holds the input text validator for editable spinboxes. By + default, SpinBox uses \l IntValidator to accept input of integer numbers. \snippet SpinBox.qml validator - \sa textFromValue, valueFromText, {Control::locale}{locale} + \sa editable, textFromValue, valueFromText, {Control::locale}{locale} */ QValidator *QQuickSpinBox::validator() const { @@ -356,10 +385,11 @@ QValidator *QQuickSpinBox::validator() const void QQuickSpinBox::setValidator(QValidator *validator) { Q_D(QQuickSpinBox); - if (d->validator != validator) { - d->validator = validator; - emit validatorChanged(); - } + if (d->validator == validator) + return; + + d->validator = validator; + emit validatorChanged(); } /*! @@ -597,6 +627,19 @@ void QQuickSpinBox::timerEvent(QTimerEvent *event) } } +void QQuickSpinBox::wheelEvent(QWheelEvent *event) +{ + Q_D(QQuickSpinBox); + QQuickControl::wheelEvent(event); + if (d->wheelEnabled) { + const int oldValue = d->value; + const QPointF angle = event->angleDelta(); + const qreal delta = (qFuzzyIsNull(angle.y()) ? angle.x() : angle.y()) / QWheelEvent::DefaultDeltasPerStep; + setValue(oldValue + qRound(d->effectiveStepSize() * delta)); + event->setAccepted(d->value != oldValue); + } +} + void QQuickSpinBox::itemChange(ItemChange change, const ItemChangeData &value) { Q_D(QQuickSpinBox); @@ -622,7 +665,7 @@ QAccessible::Role QQuickSpinBox::accessibleRole() const class QQuickSpinButtonPrivate : public QObjectPrivate { public: - QQuickSpinButtonPrivate() : pressed(false), indicator(Q_NULLPTR) { } + QQuickSpinButtonPrivate() : pressed(false), indicator(nullptr) { } bool pressed; QQuickItem *indicator; }; @@ -641,10 +684,11 @@ bool QQuickSpinButton::isPressed() const void QQuickSpinButton::setPressed(bool pressed) { Q_D(QQuickSpinButton); - if (d->pressed != pressed) { - d->pressed = pressed; - emit pressedChanged(); - } + if (d->pressed == pressed) + return; + + d->pressed = pressed; + emit pressedChanged(); } QQuickItem *QQuickSpinButton::indicator() const @@ -656,16 +700,17 @@ QQuickItem *QQuickSpinButton::indicator() const void QQuickSpinButton::setIndicator(QQuickItem *indicator) { Q_D(QQuickSpinButton); - if (d->indicator != indicator) { - delete d->indicator; - d->indicator = indicator; - if (indicator) { - if (!indicator->parentItem()) - indicator->setParentItem(static_cast<QQuickItem *>(parent())); - indicator->setAcceptedMouseButtons(Qt::LeftButton); - } - emit indicatorChanged(); + if (d->indicator == indicator) + return; + + delete d->indicator; + d->indicator = indicator; + if (indicator) { + if (!indicator->parentItem()) + indicator->setParentItem(static_cast<QQuickItem *>(parent())); + indicator->setAcceptedMouseButtons(Qt::LeftButton); } + emit indicatorChanged(); } QT_END_NAMESPACE |