diff options
author | Gabriel de Dietrich <gabriel.dedietrich@theqtcompany.com> | 2015-07-31 11:15:49 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@theqtcompany.com> | 2015-08-03 11:52:17 +0000 |
commit | 6a5abae266f20aed0bc5961cc0b4447d69599981 (patch) | |
tree | 5631ec08d2fa4e3fbc913e33e38a4ad794ed8bba /src/controls | |
parent | bf7ea19f533fd7258bffb756080618168660ac8e (diff) |
TextArea: Add pressAndHold signal
We factorize the feature code identical to TextField's
into a helper class.
Change-Id: I4496f2d192a023f6a6c03ed81d81bca4c08a59d0
Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src/controls')
-rw-r--r-- | src/controls/controls.pri | 2 | ||||
-rw-r--r-- | src/controls/qquickpressandholdhelper.cpp | 95 | ||||
-rw-r--r-- | src/controls/qquickpressandholdhelper_p.h | 67 | ||||
-rw-r--r-- | src/controls/qquicktextarea.cpp | 36 | ||||
-rw-r--r-- | src/controls/qquicktextarea_p.h | 6 | ||||
-rw-r--r-- | src/controls/qquicktextfield.cpp | 41 |
6 files changed, 216 insertions, 31 deletions
diff --git a/src/controls/controls.pri b/src/controls/controls.pri index a86adb02..af65c841 100644 --- a/src/controls/controls.pri +++ b/src/controls/controls.pri @@ -19,6 +19,7 @@ HEADERS += \ $$PWD/qquickgroupbox_p.h \ $$PWD/qquicklabel_p.h \ $$PWD/qquickpageindicator_p.h \ + $$PWD/qquickpressandholdhelper_p.h \ $$PWD/qquickprogressbar_p.h \ $$PWD/qquickradiobutton_p.h \ $$PWD/qquickscrollbar_p.h \ @@ -49,6 +50,7 @@ SOURCES += \ $$PWD/qquickgroupbox.cpp \ $$PWD/qquicklabel.cpp \ $$PWD/qquickpageindicator.cpp \ + $$PWD/qquickpressandholdhelper.cpp \ $$PWD/qquickprogressbar.cpp \ $$PWD/qquickradiobutton.cpp \ $$PWD/qquickscrollbar.cpp \ diff --git a/src/controls/qquickpressandholdhelper.cpp b/src/controls/qquickpressandholdhelper.cpp new file mode 100644 index 00000000..088a402b --- /dev/null +++ b/src/controls/qquickpressandholdhelper.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls module of the Qt Toolkit. +** +** $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 "qquickpressandholdhelper_p.h" + +#include <QtCore/private/qobject_p.h> +#include <QtGui/qguiapplication.h> +#include <QtGui/qstylehints.h> +#include <QtQuick/qquickitem.h> +#include <QtQuick/private/qquickevents_p_p.h> + +QT_BEGIN_NAMESPACE + +QQuickPressAndHoldHelper::QQuickPressAndHoldHelper() + : control(Q_NULLPTR), longPress(false), pressAndHoldSignalIndex(-1) +{ } + +void QQuickPressAndHoldHelper::mousePressEvent(QMouseEvent *event) +{ + longPress = false; + pressPos = event->localPos(); + if (Qt::LeftButton == (event->buttons() & Qt::LeftButton)) + timer.start(QGuiApplication::styleHints()->mousePressAndHoldInterval(), control); + else + timer.stop(); +} + +void QQuickPressAndHoldHelper::mouseMoveEvent(QMouseEvent *event) +{ + if (qAbs(int(event->localPos().x() - pressPos.x())) > QGuiApplication::styleHints()->startDragDistance()) + timer.stop(); +} + +void QQuickPressAndHoldHelper::mouseReleaseEvent(QMouseEvent *) +{ + if (!longPress) + timer.stop(); +} + +void QQuickPressAndHoldHelper::timerEvent(QTimerEvent *) +{ + timer.stop(); + + if (pressAndHoldSignalIndex == -1) + pressAndHoldSignalIndex = control->metaObject()->indexOfSignal("pressAndHold(QQuickMouseEvent*)"); + Q_ASSERT(pressAndHoldSignalIndex != -1); + + longPress = QObjectPrivate::get(control)->isSignalConnected(pressAndHoldSignalIndex); + if (longPress) { + QQuickMouseEvent mev(pressPos.x(), pressPos.y(), Qt::LeftButton, Qt::LeftButton, + QGuiApplication::keyboardModifiers(), false/*isClick*/, true/*wasHeld*/); + mev.setAccepted(true); + // Use fast signal invocation since we already got its index + QQuickMouseEvent *mevPtr = &mev; + void *args[] = { Q_NULLPTR, &mevPtr }; + QMetaObject::metacall(control, QMetaObject::InvokeMetaMethod, pressAndHoldSignalIndex, args); + if (!mev.isAccepted()) + longPress = false; + } +} + +QT_END_NAMESPACE diff --git a/src/controls/qquickpressandholdhelper_p.h b/src/controls/qquickpressandholdhelper_p.h new file mode 100644 index 00000000..6c0dc5ae --- /dev/null +++ b/src/controls/qquickpressandholdhelper_p.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls module of the Qt Toolkit. +** +** $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$ +** +****************************************************************************/ + +#ifndef QQUICKPRESSANDHOLDHELPER_H +#define QQUICKPRESSANDHOLDHELPER_H + +#include <QtCore/qpoint.h> +#include <QtCore/qbasictimer.h> + +QT_BEGIN_NAMESPACE + +class QQuickItem; +class QMouseEvent; +class QTimerEvent; + +struct QQuickPressAndHoldHelper +{ + QQuickPressAndHoldHelper(); + + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void timerEvent(QTimerEvent *event); + + QQuickItem *control; + QBasicTimer timer; + QPointF pressPos; + bool longPress; + int pressAndHoldSignalIndex; +}; + +QT_END_NAMESPACE + +#endif // QQUICKPRESSANDHOLDHELPER_H diff --git a/src/controls/qquicktextarea.cpp b/src/controls/qquicktextarea.cpp index 7e2f0e68..c8025069 100644 --- a/src/controls/qquicktextarea.cpp +++ b/src/controls/qquicktextarea.cpp @@ -35,6 +35,7 @@ ****************************************************************************/ #include "qquicktextarea_p.h" +#include "qquickpressandholdhelper_p.h" #include <QtQuick/private/qquickitem_p.h> #include <QtQuick/private/qquicktext_p.h> @@ -74,6 +75,7 @@ public: QQuickItem *background; QQuickText *placeholder; + QQuickPressAndHoldHelper pressAndHoldHelper; }; void QQuickTextAreaPrivate::resizeBackground() @@ -96,6 +98,7 @@ QQuickTextArea::QQuickTextArea(QQuickItem *parent) : QQuickTextEdit(*(new QQuickTextAreaPrivate), parent) { setActiveFocusOnTab(true); + d_func()->pressAndHoldHelper.control = this; } QQuickTextArea::~QQuickTextArea() @@ -184,4 +187,37 @@ QSGNode *QQuickTextArea::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData * return clipNode; } +void QQuickTextArea::mousePressEvent(QMouseEvent *event) +{ + Q_D(QQuickTextArea); + d->pressAndHoldHelper.mousePressEvent(event); + QQuickTextEdit::mousePressEvent(event); +} + +void QQuickTextArea::mouseMoveEvent(QMouseEvent *event) +{ + Q_D(QQuickTextArea); + d->pressAndHoldHelper.mouseMoveEvent(event); + if (!d->pressAndHoldHelper.timer.isActive()) + QQuickTextEdit::mouseMoveEvent(event); +} + +void QQuickTextArea::mouseReleaseEvent(QMouseEvent *event) +{ + Q_D(QQuickTextArea); + d->pressAndHoldHelper.mouseReleaseEvent(event); + if (!d->pressAndHoldHelper.longPress) + QQuickTextEdit::mouseReleaseEvent(event); +} + +void QQuickTextArea::timerEvent(QTimerEvent *event) +{ + Q_D(QQuickTextArea); + if (event->timerId() == d->pressAndHoldHelper.timer.timerId()) { + d->pressAndHoldHelper.timerEvent(event); + } else { + QQuickTextEdit::timerEvent(event); + } +} + QT_END_NAMESPACE diff --git a/src/controls/qquicktextarea_p.h b/src/controls/qquicktextarea_p.h index edf81942..f952f304 100644 --- a/src/controls/qquicktextarea_p.h +++ b/src/controls/qquicktextarea_p.h @@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE class QQuickText; class QQuickTextAreaPrivate; +class QQuickMouseEvent; class Q_QUICKCONTROLS_EXPORT QQuickTextArea : public QQuickTextEdit { @@ -75,10 +76,15 @@ public: Q_SIGNALS: void backgroundChanged(); void placeholderChanged(); + void pressAndHold(QQuickMouseEvent *event); protected: void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) Q_DECL_OVERRIDE; QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) Q_DECL_OVERRIDE; + void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE; private: Q_DISABLE_COPY(QQuickTextArea) diff --git a/src/controls/qquicktextfield.cpp b/src/controls/qquicktextfield.cpp index de6840bd..e8b4086c 100644 --- a/src/controls/qquicktextfield.cpp +++ b/src/controls/qquicktextfield.cpp @@ -35,13 +35,13 @@ ****************************************************************************/ #include "qquicktextfield_p.h" +#include "qquickpressandholdhelper_p.h" #include <QtCore/qbasictimer.h> #include <QtQuick/private/qquickitem_p.h> #include <QtQuick/private/qquicktext_p.h> #include <QtQuick/private/qquickclipnode_p.h> #include <QtQuick/private/qquicktextinput_p_p.h> -#include <QtQuick/private/qquickevents_p_p.h> QT_BEGIN_NAMESPACE @@ -90,16 +90,13 @@ public: QQuickTextFieldPrivate() : background(Q_NULLPTR) , placeholder(Q_NULLPTR) - , longPress(false) { } void resizeBackground(); - bool isPressAndHoldConnected(); QQuickItem *background; QQuickText *placeholder; - QBasicTimer pressAndHoldTimer; - bool longPress; + QQuickPressAndHoldHelper pressAndHoldHelper; }; void QQuickTextFieldPrivate::resizeBackground() @@ -118,15 +115,10 @@ void QQuickTextFieldPrivate::resizeBackground() } } -bool QQuickTextFieldPrivate::isPressAndHoldConnected() -{ - Q_Q(QQuickTextField); - IS_SIGNAL_CONNECTED(q, QQuickTextField, pressAndHold, (QQuickMouseEvent *)); -} - QQuickTextField::QQuickTextField(QQuickItem *parent) : QQuickTextInput(*(new QQuickTextFieldPrivate), parent) { + d_func()->pressAndHoldHelper.control = this; setActiveFocusOnTab(true); } @@ -219,44 +211,31 @@ QSGNode *QQuickTextField::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData void QQuickTextField::mousePressEvent(QMouseEvent *event) { Q_D(QQuickTextField); - d->longPress = false; - if (Qt::LeftButton == (event->buttons() & Qt::LeftButton)) - d->pressAndHoldTimer.start(QGuiApplication::styleHints()->mousePressAndHoldInterval(), this); - else - d->pressAndHoldTimer.stop(); + d->pressAndHoldHelper.mousePressEvent(event); QQuickTextInput::mousePressEvent(event); } void QQuickTextField::mouseMoveEvent(QMouseEvent *event) { Q_D(QQuickTextField); - if (qAbs(int(event->localPos().x() - d->pressPos.x())) > QGuiApplication::styleHints()->startDragDistance()) - d->pressAndHoldTimer.stop(); - if (!d->pressAndHoldTimer.isActive()) + d->pressAndHoldHelper.mouseMoveEvent(event); + if (!d->pressAndHoldHelper.timer.isActive()) QQuickTextInput::mouseMoveEvent(event); } void QQuickTextField::mouseReleaseEvent(QMouseEvent *event) { Q_D(QQuickTextField); - if (!d->longPress) { - d->pressAndHoldTimer.stop(); + d->pressAndHoldHelper.mouseReleaseEvent(event); + if (!d->pressAndHoldHelper.longPress) QQuickTextInput::mouseReleaseEvent(event); - } } void QQuickTextField::timerEvent(QTimerEvent *event) { Q_D(QQuickTextField); - if (event->timerId() == d->pressAndHoldTimer.timerId()) { - d->pressAndHoldTimer.stop(); - d->longPress = true; - QQuickMouseEvent me(d->pressPos.x(), d->pressPos.y(), Qt::LeftButton, Qt::LeftButton, - QGuiApplication::keyboardModifiers(), false/*isClick*/, true/*wasHeld*/); - me.setAccepted(d->isPressAndHoldConnected()); - emit pressAndHold(&me); - if (!me.isAccepted()) - d->longPress = false; + if (event->timerId() == d->pressAndHoldHelper.timer.timerId()) { + d->pressAndHoldHelper.timerEvent(event); } else { QQuickTextInput::timerEvent(event); } |