2015-10-10 14:07:28 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-04-14 05:57:25 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
2015-10-10 14:07:28 +00:00
|
|
|
** Contact: http://www.qt.io/licensing/
|
|
|
|
**
|
2016-04-14 05:57:25 +00:00
|
|
|
** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
|
2015-10-10 14:07:28 +00:00
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL3$
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see http://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl.html.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or later as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.GPL included in
|
|
|
|
** the packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 2.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/licenses/gpl-2.0.html.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qquickspinbox_p.h"
|
|
|
|
#include "qquickcontrol_p_p.h"
|
|
|
|
|
|
|
|
#include <QtGui/qguiapplication.h>
|
|
|
|
#include <QtGui/qstylehints.h>
|
|
|
|
|
2015-10-26 15:36:32 +00:00
|
|
|
#include <QtQml/qqmlinfo.h>
|
|
|
|
#include <QtQml/private/qqmllocale_p.h>
|
|
|
|
#include <QtQml/private/qqmlengine_p.h>
|
|
|
|
|
2015-10-10 14:07:28 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
// copied from qabstractbutton.cpp
|
|
|
|
static const int AUTO_REPEAT_DELAY = 300;
|
|
|
|
static const int AUTO_REPEAT_INTERVAL = 100;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\qmltype SpinBox
|
|
|
|
\inherits Control
|
|
|
|
\instantiates QQuickSpinBox
|
2016-04-21 14:56:59 +00:00
|
|
|
\inqmlmodule QtQuick.Controls
|
2016-05-25 06:41:21 +00:00
|
|
|
\since 5.7
|
2015-10-10 14:07:28 +00:00
|
|
|
\ingroup input
|
2016-02-12 15:54:15 +00:00
|
|
|
\brief A spinbox control that allows the user to select from a set of preset values.
|
2015-10-10 14:07:28 +00:00
|
|
|
|
2016-04-13 18:30:26 +00:00
|
|
|
\image qtquickcontrols2-spinbox.png
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
SpinBox allows the user to choose an integer value by clicking the up
|
2016-02-22 13:13:39 +00:00
|
|
|
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.
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
By default, SpinBox provides discrete values in the range of \c [0-99]
|
|
|
|
with a \l stepSize of \c 1.
|
|
|
|
|
2016-04-13 18:30:26 +00:00
|
|
|
\snippet qtquickcontrols2-spinbox.qml 1
|
2015-10-10 14:07:28 +00:00
|
|
|
|
2015-10-26 15:36:32 +00:00
|
|
|
\section2 Custom Values
|
|
|
|
|
2016-04-13 18:30:26 +00:00
|
|
|
\image qtquickcontrols2-spinbox-textual.png
|
2015-10-26 15:36:32 +00:00
|
|
|
|
|
|
|
Even though SpinBox works on integer values, it can be customized to
|
|
|
|
accept arbitrary input values. The following snippet demonstrates how
|
|
|
|
\l validator, \l textFromValue and \l valueFromText can be used to
|
|
|
|
customize the default behavior.
|
|
|
|
|
2016-04-13 18:30:26 +00:00
|
|
|
\snippet qtquickcontrols2-spinbox-textual.qml 1
|
2015-10-26 15:36:32 +00:00
|
|
|
|
2016-05-11 13:06:51 +00:00
|
|
|
In the same manner, SpinBox can be customized to accept floating point
|
|
|
|
numbers:
|
|
|
|
|
|
|
|
\image qtquickcontrols2-spinbox-double.png
|
|
|
|
|
|
|
|
\snippet qtquickcontrols2-spinbox-double.qml 1
|
|
|
|
|
2015-10-10 14:07:28 +00:00
|
|
|
\sa Tumbler, {Customizing SpinBox}
|
|
|
|
*/
|
|
|
|
|
|
|
|
class QQuickSpinBoxPrivate : public QQuickControlPrivate
|
|
|
|
{
|
|
|
|
Q_DECLARE_PUBLIC(QQuickSpinBox)
|
|
|
|
|
|
|
|
public:
|
2016-02-22 13:13:39 +00:00
|
|
|
QQuickSpinBoxPrivate() : editable(false), from(0), to(99), value(0), stepSize(1),
|
2016-01-20 16:31:18 +00:00
|
|
|
delayTimer(0), repeatTimer(0), up(nullptr), down(nullptr), validator(nullptr) { }
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
int boundValue(int value) const;
|
|
|
|
void updateValue();
|
|
|
|
|
|
|
|
int effectiveStepSize() const;
|
|
|
|
|
2016-05-23 14:23:16 +00:00
|
|
|
bool upEnabled() const;
|
|
|
|
void updateUpEnabled();
|
|
|
|
bool downEnabled() const;
|
|
|
|
void updateDownEnabled();
|
|
|
|
|
2015-10-10 14:07:28 +00:00
|
|
|
void startRepeatDelay();
|
|
|
|
void startPressRepeat();
|
|
|
|
void stopPressRepeat();
|
|
|
|
|
|
|
|
bool handleMousePressEvent(QQuickItem *child, QMouseEvent *event);
|
|
|
|
bool handleMouseMoveEvent(QQuickItem *child, QMouseEvent *event);
|
|
|
|
bool handleMouseReleaseEvent(QQuickItem *child, QMouseEvent *event);
|
|
|
|
bool handleMouseUngrabEvent(QQuickItem *child);
|
|
|
|
|
2016-02-22 13:13:39 +00:00
|
|
|
bool editable;
|
2015-10-10 14:07:28 +00:00
|
|
|
int from;
|
|
|
|
int to;
|
|
|
|
int value;
|
|
|
|
int stepSize;
|
|
|
|
int delayTimer;
|
|
|
|
int repeatTimer;
|
2015-10-30 15:56:51 +00:00
|
|
|
QQuickSpinButton *up;
|
|
|
|
QQuickSpinButton *down;
|
2015-10-26 15:36:32 +00:00
|
|
|
QValidator *validator;
|
2016-01-29 08:46:25 +00:00
|
|
|
mutable QJSValue textFromValue;
|
|
|
|
mutable QJSValue valueFromText;
|
2015-10-10 14:07:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int QQuickSpinBoxPrivate::boundValue(int value) const
|
|
|
|
{
|
|
|
|
return from > to ? qBound(to, value, from) : qBound(from, value, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBoxPrivate::updateValue()
|
|
|
|
{
|
|
|
|
Q_Q(QQuickSpinBox);
|
|
|
|
if (contentItem) {
|
|
|
|
QVariant text = contentItem->property("text");
|
2015-10-26 15:36:32 +00:00
|
|
|
if (text.isValid()) {
|
2016-03-09 10:14:13 +00:00
|
|
|
QQmlEngine *engine = qmlEngine(q);
|
|
|
|
if (engine) {
|
|
|
|
QV4::ExecutionEngine *v4 = QQmlEnginePrivate::getV4Engine(engine);
|
|
|
|
QJSValue loc(v4, QQmlLocale::wrap(v4, locale));
|
|
|
|
QJSValue val = q->valueFromText().call(QJSValueList() << text.toString() << loc);
|
|
|
|
q->setValue(val.toInt());
|
|
|
|
}
|
2015-10-26 15:36:32 +00:00
|
|
|
}
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int QQuickSpinBoxPrivate::effectiveStepSize() const
|
|
|
|
{
|
|
|
|
return from > to ? -1 * stepSize : stepSize;
|
|
|
|
}
|
|
|
|
|
2016-05-23 14:23:16 +00:00
|
|
|
bool QQuickSpinBoxPrivate::upEnabled() const
|
|
|
|
{
|
|
|
|
const QQuickItem *upIndicator = up->indicator();
|
|
|
|
return upIndicator && upIndicator->isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBoxPrivate::updateUpEnabled()
|
|
|
|
{
|
|
|
|
QQuickItem *upIndicator = up->indicator();
|
|
|
|
if (!upIndicator)
|
|
|
|
return;
|
|
|
|
|
|
|
|
upIndicator->setEnabled(from < to ? value < to : value > to);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QQuickSpinBoxPrivate::downEnabled() const
|
|
|
|
{
|
|
|
|
const QQuickItem *downIndicator = down->indicator();
|
|
|
|
return downIndicator && downIndicator->isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBoxPrivate::updateDownEnabled()
|
|
|
|
{
|
|
|
|
QQuickItem *downIndicator = down->indicator();
|
|
|
|
if (!downIndicator)
|
|
|
|
return;
|
|
|
|
|
|
|
|
downIndicator->setEnabled(from < to ? value > from : value < from);
|
|
|
|
}
|
|
|
|
|
2015-10-10 14:07:28 +00:00
|
|
|
void QQuickSpinBoxPrivate::startRepeatDelay()
|
|
|
|
{
|
|
|
|
Q_Q(QQuickSpinBox);
|
|
|
|
stopPressRepeat();
|
|
|
|
delayTimer = q->startTimer(AUTO_REPEAT_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBoxPrivate::startPressRepeat()
|
|
|
|
{
|
|
|
|
Q_Q(QQuickSpinBox);
|
|
|
|
stopPressRepeat();
|
|
|
|
repeatTimer = q->startTimer(AUTO_REPEAT_INTERVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBoxPrivate::stopPressRepeat()
|
|
|
|
{
|
|
|
|
Q_Q(QQuickSpinBox);
|
|
|
|
if (delayTimer > 0) {
|
|
|
|
q->killTimer(delayTimer);
|
|
|
|
delayTimer = 0;
|
|
|
|
}
|
|
|
|
if (repeatTimer > 0) {
|
|
|
|
q->killTimer(repeatTimer);
|
|
|
|
repeatTimer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 14:22:07 +00:00
|
|
|
bool QQuickSpinBoxPrivate::handleMousePressEvent(QQuickItem *child, QMouseEvent *event)
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
|
|
|
Q_Q(QQuickSpinBox);
|
|
|
|
QQuickItem *ui = up->indicator();
|
|
|
|
QQuickItem *di = down->indicator();
|
2016-05-23 14:23:16 +00:00
|
|
|
up->setPressed(ui && ui->isEnabled() && ui->contains(ui->mapFromItem(child, event->pos())));
|
|
|
|
down->setPressed(di && di->isEnabled() && di->contains(di->mapFromItem(child, event->pos())));
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
bool pressed = up->isPressed() || down->isPressed();
|
|
|
|
q->setAccessibleProperty("pressed", pressed);
|
|
|
|
if (pressed)
|
|
|
|
startRepeatDelay();
|
2016-01-20 22:07:05 +00:00
|
|
|
return pressed;
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QQuickSpinBoxPrivate::handleMouseMoveEvent(QQuickItem *child, QMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_Q(QQuickSpinBox);
|
|
|
|
QQuickItem *ui = up->indicator();
|
|
|
|
QQuickItem *di = down->indicator();
|
2016-05-23 14:23:16 +00:00
|
|
|
up->setPressed(ui && ui->isEnabled() && ui->contains(ui->mapFromItem(child, event->pos())));
|
|
|
|
down->setPressed(di && di->isEnabled() && di->contains(di->mapFromItem(child, event->pos())));
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
bool pressed = up->isPressed() || down->isPressed();
|
|
|
|
q->setAccessibleProperty("pressed", pressed);
|
|
|
|
stopPressRepeat();
|
2016-01-20 22:07:05 +00:00
|
|
|
return pressed;
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QQuickSpinBoxPrivate::handleMouseReleaseEvent(QQuickItem *child, QMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_Q(QQuickSpinBox);
|
|
|
|
QQuickItem *ui = up->indicator();
|
|
|
|
QQuickItem *di = down->indicator();
|
2015-12-18 14:22:07 +00:00
|
|
|
bool wasPressed = up->isPressed() || down->isPressed();
|
|
|
|
if (up->isPressed()) {
|
2015-10-10 14:07:28 +00:00
|
|
|
up->setPressed(false);
|
2015-12-18 14:22:07 +00:00
|
|
|
if (repeatTimer <= 0 && ui && ui->contains(ui->mapFromItem(child, event->pos())))
|
2015-10-10 14:07:28 +00:00
|
|
|
q->increase();
|
2015-12-18 14:22:07 +00:00
|
|
|
} else if (down->isPressed()) {
|
2015-10-10 14:07:28 +00:00
|
|
|
down->setPressed(false);
|
2015-12-18 14:22:07 +00:00
|
|
|
if (repeatTimer <= 0 && di && di->contains(di->mapFromItem(child, event->pos())))
|
2015-10-10 14:07:28 +00:00
|
|
|
q->decrease();
|
|
|
|
}
|
|
|
|
|
|
|
|
q->setAccessibleProperty("pressed", false);
|
|
|
|
stopPressRepeat();
|
2015-12-18 14:22:07 +00:00
|
|
|
return wasPressed;
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 14:22:07 +00:00
|
|
|
bool QQuickSpinBoxPrivate::handleMouseUngrabEvent(QQuickItem *)
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
|
|
|
Q_Q(QQuickSpinBox);
|
|
|
|
up->setPressed(false);
|
|
|
|
down->setPressed(false);
|
|
|
|
|
|
|
|
q->setAccessibleProperty("pressed", false);
|
|
|
|
stopPressRepeat();
|
2015-12-18 14:22:07 +00:00
|
|
|
return false;
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QQuickSpinBox::QQuickSpinBox(QQuickItem *parent) :
|
|
|
|
QQuickControl(*(new QQuickSpinBoxPrivate), parent)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
2015-10-30 15:56:51 +00:00
|
|
|
d->up = new QQuickSpinButton(this);
|
|
|
|
d->down = new QQuickSpinButton(this);
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
setFlag(ItemIsFocusScope);
|
|
|
|
setFiltersChildMouseEvents(true);
|
2015-12-18 14:22:07 +00:00
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlproperty int QtQuick.Controls::SpinBox::from
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
This property holds the starting value for the range. The default value is \c 0.
|
|
|
|
|
|
|
|
\sa to, value
|
|
|
|
*/
|
|
|
|
int QQuickSpinBox::from() const
|
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
|
|
|
return d->from;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::setFrom(int from)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
2016-02-11 17:24:18 +00:00
|
|
|
if (d->from == from)
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->from = from;
|
|
|
|
emit fromChanged();
|
|
|
|
if (isComponentComplete())
|
|
|
|
setValue(d->value);
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlproperty int QtQuick.Controls::SpinBox::to
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
This property holds the end value for the range. The default value is \c 99.
|
|
|
|
|
|
|
|
\sa from, value
|
|
|
|
*/
|
|
|
|
int QQuickSpinBox::to() const
|
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
|
|
|
return d->to;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::setTo(int to)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
2016-02-11 17:24:18 +00:00
|
|
|
if (d->to == to)
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->to = to;
|
|
|
|
emit toChanged();
|
|
|
|
if (isComponentComplete())
|
|
|
|
setValue(d->value);
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlproperty int QtQuick.Controls::SpinBox::value
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
This property holds the value in the range \c from - \c to. The default value is \c 0.
|
|
|
|
*/
|
|
|
|
int QQuickSpinBox::value() const
|
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
|
|
|
return d->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::setValue(int value)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
if (isComponentComplete())
|
|
|
|
value = d->boundValue(value);
|
|
|
|
|
2016-02-11 17:24:18 +00:00
|
|
|
if (d->value == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->value = value;
|
2016-05-23 14:23:16 +00:00
|
|
|
|
|
|
|
d->updateUpEnabled();
|
|
|
|
d->updateDownEnabled();
|
|
|
|
|
2016-02-11 17:24:18 +00:00
|
|
|
emit valueChanged();
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlproperty int QtQuick.Controls::SpinBox::stepSize
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
This property holds the step size. The default value is \c 1.
|
|
|
|
|
|
|
|
\sa increase(), decrease()
|
|
|
|
*/
|
|
|
|
int QQuickSpinBox::stepSize() const
|
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
|
|
|
return d->stepSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::setStepSize(int step)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
2016-02-11 17:24:18 +00:00
|
|
|
if (d->stepSize == step)
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->stepSize = step;
|
|
|
|
emit stepSizeChanged();
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 13:13:39 +00:00
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlproperty bool QtQuick.Controls::SpinBox::editable
|
2016-02-22 13:13:39 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:36:32 +00:00
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlproperty Validator QtQuick.Controls::SpinBox::validator
|
2015-10-26 15:36:32 +00:00
|
|
|
|
2016-02-22 13:13:39 +00:00
|
|
|
This property holds the input text validator for editable spinboxes. By
|
|
|
|
default, SpinBox uses \l IntValidator to accept input of integer numbers.
|
2015-10-26 15:36:32 +00:00
|
|
|
|
|
|
|
\snippet SpinBox.qml validator
|
|
|
|
|
2016-02-22 13:13:39 +00:00
|
|
|
\sa editable, textFromValue, valueFromText, {Control::locale}{locale}
|
2015-10-26 15:36:32 +00:00
|
|
|
*/
|
|
|
|
QValidator *QQuickSpinBox::validator() const
|
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
|
|
|
return d->validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::setValidator(QValidator *validator)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
2016-02-11 17:24:18 +00:00
|
|
|
if (d->validator == validator)
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->validator = validator;
|
|
|
|
emit validatorChanged();
|
2015-10-26 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlproperty function QtQuick.Controls::SpinBox::textFromValue
|
2015-10-26 15:36:32 +00:00
|
|
|
|
|
|
|
This property holds a callback function that is called whenever
|
|
|
|
an integer value needs to be converted to display text.
|
|
|
|
|
|
|
|
The callback function signature is \c {string function(value, locale)}.
|
|
|
|
The function can have one or two arguments, where the first argument
|
|
|
|
is the value to be converted, and the optional second argument is the
|
|
|
|
locale that should be used for the conversion, if applicable.
|
|
|
|
|
|
|
|
The default implementation does the conversion using \l {QtQml::Locale}{Number.toLocaleString()}:
|
|
|
|
|
|
|
|
\code
|
|
|
|
textFromValue: function(value, locale) { return Number(value).toLocaleString(locale, 'f', 0); }
|
|
|
|
\endcode
|
|
|
|
|
2015-12-05 12:00:56 +00:00
|
|
|
\sa valueFromText, validator, {Control::locale}{locale}
|
2015-10-26 15:36:32 +00:00
|
|
|
*/
|
|
|
|
QJSValue QQuickSpinBox::textFromValue() const
|
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
2016-01-29 08:46:25 +00:00
|
|
|
if (!d->textFromValue.isCallable()) {
|
|
|
|
QQmlEngine *engine = qmlEngine(this);
|
|
|
|
if (engine)
|
|
|
|
d->textFromValue = engine->evaluate(QStringLiteral("function(value, locale) { return Number(value).toLocaleString(locale, 'f', 0); }"));
|
|
|
|
}
|
2015-10-26 15:36:32 +00:00
|
|
|
return d->textFromValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::setTextFromValue(const QJSValue &callback)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
if (!callback.isCallable()) {
|
|
|
|
qmlInfo(this) << "textFromValue must be a callable function";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
d->textFromValue = callback;
|
|
|
|
emit textFromValueChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlproperty function QtQuick.Controls::SpinBox::valueFromText
|
2015-10-26 15:36:32 +00:00
|
|
|
|
|
|
|
This property holds a callback function that is called whenever
|
|
|
|
input text needs to be converted to an integer value.
|
|
|
|
|
|
|
|
The callback function signature is \c {int function(text, locale)}.
|
|
|
|
The function can have one or two arguments, where the first argument
|
|
|
|
is the text to be converted, and the optional second argument is the
|
|
|
|
locale that should be used for the conversion, if applicable.
|
|
|
|
|
|
|
|
The default implementation does the conversion using \l {QtQml::Locale}{Number.fromLocaleString()}:
|
|
|
|
|
|
|
|
\code
|
|
|
|
valueFromText: function(text, locale) { return Number.fromLocaleString(locale, text); }
|
|
|
|
\endcode
|
|
|
|
|
2015-12-05 12:00:56 +00:00
|
|
|
\sa textFromValue, validator, {Control::locale}{locale}
|
2015-10-26 15:36:32 +00:00
|
|
|
*/
|
|
|
|
QJSValue QQuickSpinBox::valueFromText() const
|
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
2016-01-29 08:46:25 +00:00
|
|
|
if (!d->valueFromText.isCallable()) {
|
|
|
|
QQmlEngine *engine = qmlEngine(this);
|
|
|
|
if (engine)
|
|
|
|
d->valueFromText = engine->evaluate(QStringLiteral("function(text, locale) { return Number.fromLocaleString(locale, text); }"));
|
|
|
|
}
|
2015-10-26 15:36:32 +00:00
|
|
|
return d->valueFromText;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::setValueFromText(const QJSValue &callback)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
if (!callback.isCallable()) {
|
|
|
|
qmlInfo(this) << "valueFromText must be a callable function";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
d->valueFromText = callback;
|
|
|
|
emit valueFromTextChanged();
|
|
|
|
}
|
|
|
|
|
2015-10-10 14:07:28 +00:00
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlpropertygroup QtQuick.Controls::SpinBox::up
|
|
|
|
\qmlproperty bool QtQuick.Controls::SpinBox::up.pressed
|
|
|
|
\qmlproperty Item QtQuick.Controls::SpinBox::up.indicator
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
These properties hold the up indicator item and whether it is pressed.
|
|
|
|
|
|
|
|
\sa increase()
|
|
|
|
*/
|
2015-10-30 15:56:51 +00:00
|
|
|
QQuickSpinButton *QQuickSpinBox::up() const
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
|
|
|
return d->up;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlpropertygroup QtQuick.Controls::SpinBox::down
|
|
|
|
\qmlproperty bool QtQuick.Controls::SpinBox::down.pressed
|
|
|
|
\qmlproperty Item QtQuick.Controls::SpinBox::down.indicator
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
These properties hold the down indicator item and whether it is pressed.
|
|
|
|
|
|
|
|
\sa decrease()
|
|
|
|
*/
|
2015-10-30 15:56:51 +00:00
|
|
|
QQuickSpinButton *QQuickSpinBox::down() const
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
|
|
|
Q_D(const QQuickSpinBox);
|
|
|
|
return d->down;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlmethod void QtQuick.Controls::SpinBox::increase()
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
Increases the value by \l stepSize.
|
|
|
|
|
|
|
|
\sa stepSize
|
|
|
|
*/
|
|
|
|
void QQuickSpinBox::increase()
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
setValue(d->value + d->effectiveStepSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-04-21 14:56:59 +00:00
|
|
|
\qmlmethod void QtQuick.Controls::SpinBox::decrease()
|
2015-10-10 14:07:28 +00:00
|
|
|
|
|
|
|
Decreases the value by \l stepSize.
|
|
|
|
|
|
|
|
\sa stepSize
|
|
|
|
*/
|
|
|
|
void QQuickSpinBox::decrease()
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
setValue(d->value - d->effectiveStepSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::keyPressEvent(event);
|
|
|
|
|
|
|
|
switch (event->key()) {
|
|
|
|
case Qt::Key_Up:
|
2016-05-23 14:23:16 +00:00
|
|
|
if (d->upEnabled()) {
|
|
|
|
increase();
|
|
|
|
d->up->setPressed(true);
|
|
|
|
event->accept();
|
|
|
|
}
|
2015-10-10 14:07:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::Key_Down:
|
2016-05-23 14:23:16 +00:00
|
|
|
if (d->downEnabled()) {
|
|
|
|
decrease();
|
|
|
|
d->down->setPressed(true);
|
|
|
|
event->accept();
|
|
|
|
}
|
2015-10-10 14:07:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAccessibleProperty("pressed", d->up->isPressed() || d->down->isPressed());
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::keyReleaseEvent(event);
|
|
|
|
|
2016-09-27 12:47:06 +00:00
|
|
|
if (d->editable && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return))
|
2015-10-10 14:07:28 +00:00
|
|
|
d->updateValue();
|
|
|
|
|
|
|
|
d->up->setPressed(false);
|
|
|
|
d->down->setPressed(false);
|
|
|
|
setAccessibleProperty("pressed", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QQuickSpinBox::childMouseEventFilter(QQuickItem *child, QEvent *event)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::MouseButtonPress:
|
|
|
|
return d->handleMousePressEvent(child, static_cast<QMouseEvent *>(event));
|
|
|
|
case QEvent::MouseMove:
|
|
|
|
return d->handleMouseMoveEvent(child, static_cast<QMouseEvent *>(event));
|
|
|
|
case QEvent::MouseButtonRelease:
|
|
|
|
return d->handleMouseReleaseEvent(child, static_cast<QMouseEvent *>(event));
|
|
|
|
case QEvent::UngrabMouse:
|
|
|
|
return d->handleMouseUngrabEvent(child);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 14:22:07 +00:00
|
|
|
void QQuickSpinBox::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::mousePressEvent(event);
|
|
|
|
d->handleMousePressEvent(this, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::mouseMoveEvent(event);
|
|
|
|
d->handleMouseMoveEvent(this, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::mouseReleaseEvent(event);
|
|
|
|
d->handleMouseReleaseEvent(this, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::mouseUngrabEvent()
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::mouseUngrabEvent();
|
|
|
|
d->handleMouseUngrabEvent(this);
|
|
|
|
}
|
|
|
|
|
2015-10-10 14:07:28 +00:00
|
|
|
void QQuickSpinBox::timerEvent(QTimerEvent *event)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::timerEvent(event);
|
|
|
|
if (event->timerId() == d->delayTimer) {
|
|
|
|
d->startPressRepeat();
|
|
|
|
} else if (event->timerId() == d->repeatTimer) {
|
|
|
|
if (d->up->isPressed())
|
|
|
|
increase();
|
|
|
|
else if (d->down->isPressed())
|
|
|
|
decrease();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 14:35:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 14:23:16 +00:00
|
|
|
void QQuickSpinBox::componentComplete()
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::componentComplete();
|
|
|
|
d->updateUpEnabled();
|
|
|
|
d->updateDownEnabled();
|
|
|
|
}
|
|
|
|
|
2015-10-10 14:07:28 +00:00
|
|
|
void QQuickSpinBox::itemChange(ItemChange change, const ItemChangeData &value)
|
|
|
|
{
|
|
|
|
Q_D(QQuickSpinBox);
|
|
|
|
QQuickControl::itemChange(change, value);
|
2016-09-27 12:47:06 +00:00
|
|
|
if (d->editable && change == ItemActiveFocusHasChanged && !value.boolValue)
|
2015-10-10 14:07:28 +00:00
|
|
|
d->updateValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQuickSpinBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
|
|
|
|
{
|
|
|
|
Q_UNUSED(oldItem);
|
|
|
|
if (newItem)
|
|
|
|
newItem->setActiveFocusOnTab(true);
|
|
|
|
}
|
|
|
|
|
2016-03-19 19:02:24 +00:00
|
|
|
QFont QQuickSpinBox::defaultFont() const
|
|
|
|
{
|
|
|
|
return QQuickControlPrivate::themeFont(QPlatformTheme::EditorFont);
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:20:41 +00:00
|
|
|
#ifndef QT_NO_ACCESSIBILITY
|
|
|
|
QAccessible::Role QQuickSpinBox::accessibleRole() const
|
|
|
|
{
|
|
|
|
return QAccessible::SpinBox;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-30 15:56:51 +00:00
|
|
|
class QQuickSpinButtonPrivate : public QObjectPrivate
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-01-20 16:31:18 +00:00
|
|
|
QQuickSpinButtonPrivate() : pressed(false), indicator(nullptr) { }
|
2015-10-10 14:07:28 +00:00
|
|
|
bool pressed;
|
|
|
|
QQuickItem *indicator;
|
|
|
|
};
|
|
|
|
|
2015-10-30 15:56:51 +00:00
|
|
|
QQuickSpinButton::QQuickSpinButton(QQuickSpinBox *parent) :
|
|
|
|
QObject(*(new QQuickSpinButtonPrivate), parent)
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:56:51 +00:00
|
|
|
bool QQuickSpinButton::isPressed() const
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
2015-10-30 15:56:51 +00:00
|
|
|
Q_D(const QQuickSpinButton);
|
2015-10-10 14:07:28 +00:00
|
|
|
return d->pressed;
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:56:51 +00:00
|
|
|
void QQuickSpinButton::setPressed(bool pressed)
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
2015-10-30 15:56:51 +00:00
|
|
|
Q_D(QQuickSpinButton);
|
2016-02-11 17:24:18 +00:00
|
|
|
if (d->pressed == pressed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->pressed = pressed;
|
|
|
|
emit pressedChanged();
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 15:56:51 +00:00
|
|
|
QQuickItem *QQuickSpinButton::indicator() const
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
2015-10-30 15:56:51 +00:00
|
|
|
Q_D(const QQuickSpinButton);
|
2015-10-10 14:07:28 +00:00
|
|
|
return d->indicator;
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:56:51 +00:00
|
|
|
void QQuickSpinButton::setIndicator(QQuickItem *indicator)
|
2015-10-10 14:07:28 +00:00
|
|
|
{
|
2015-10-30 15:56:51 +00:00
|
|
|
Q_D(QQuickSpinButton);
|
2016-02-11 17:24:18 +00:00
|
|
|
if (d->indicator == indicator)
|
|
|
|
return;
|
|
|
|
|
2016-09-07 13:49:37 +00:00
|
|
|
QQuickControl *control = qobject_cast<QQuickControl*>(d->parent);
|
|
|
|
if (control)
|
|
|
|
QQuickControlPrivate::get(control)->deleteDelegate(d->indicator);
|
|
|
|
else
|
|
|
|
delete d->indicator;
|
|
|
|
|
2016-02-11 17:24:18 +00:00
|
|
|
d->indicator = indicator;
|
2016-09-07 13:49:37 +00:00
|
|
|
|
2016-02-11 17:24:18 +00:00
|
|
|
if (indicator) {
|
|
|
|
if (!indicator->parentItem())
|
|
|
|
indicator->setParentItem(static_cast<QQuickItem *>(parent()));
|
|
|
|
indicator->setAcceptedMouseButtons(Qt::LeftButton);
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
2016-02-11 17:24:18 +00:00
|
|
|
emit indicatorChanged();
|
2015-10-10 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|