diff options
author | Christian Strømme <christian.stromme@theqtcompany.com> | 2015-04-15 12:06:49 +0200 |
---|---|---|
committer | Christian Stromme <christian.stromme@theqtcompany.com> | 2015-04-15 10:50:41 +0000 |
commit | 7e86a65cb03c3a35f1c8fb3e16a55193e42930ab (patch) | |
tree | 4cc0bfc6c7925107896bd78ead3cac87a119a713 /src | |
parent | ded73ee2ae8aa333894ab11ce2297e755ec03f3e (diff) | |
download | qtquickcontrols-7e86a65cb03c3a35f1c8fb3e16a55193e42930ab.tar.gz |
Add internal ScenePosListener
Makes it possible to implement floating text selection handle popups
that retain correct position whilst the scene position of the attached
text input control changes. The text input may be eg. in a Flickable
and thus does not receive geometry change notifications since its own
position in relation to the direct parent does not change.
Task-number: QTBUG-42538
Change-Id: I63cb15ddb1daa7d39cbe95d6421b171cd1c01596
Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/controls/Private/private.pri | 7 | ||||
-rw-r--r-- | src/controls/Private/qquicksceneposlistener.cpp | 207 | ||||
-rw-r--r-- | src/controls/Private/qquicksceneposlistener_p.h | 93 | ||||
-rw-r--r-- | src/controls/plugin.cpp | 2 |
4 files changed, 306 insertions, 3 deletions
diff --git a/src/controls/Private/private.pri b/src/controls/Private/private.pri index 420647ff..1e66f9ba 100644 --- a/src/controls/Private/private.pri +++ b/src/controls/Private/private.pri @@ -10,7 +10,8 @@ HEADERS += \ $$PWD/qquickabstractstyle_p.h \ $$PWD/qquickpadding_p.h \ $$PWD/qquickcontrolsprivate_p.h \ - $$PWD/qquicktreemodeladaptor_p.h + $$PWD/qquicktreemodeladaptor_p.h \ + $$PWD/qquicksceneposlistener_p.h SOURCES += \ $$PWD/qquickcalendarmodel.cpp \ @@ -22,8 +23,8 @@ SOURCES += \ $$PWD/qquickwheelarea.cpp \ $$PWD/qquickabstractstyle.cpp \ $$PWD/qquicktreemodeladaptor.cpp \ - $$PWD/qquickcontrolsprivate.cpp - + $$PWD/qquickcontrolsprivate.cpp \ + $$PWD/qquicksceneposlistener.cpp !no_desktop { QT += widgets diff --git a/src/controls/Private/qquicksceneposlistener.cpp b/src/controls/Private/qquicksceneposlistener.cpp new file mode 100644 index 00000000..d6155fe1 --- /dev/null +++ b/src/controls/Private/qquicksceneposlistener.cpp @@ -0,0 +1,207 @@ +/**************************************************************************** +** +** 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 "qquicksceneposlistener_p.h" +#include <QtQuick/private/qquickitem_p.h> + +QT_BEGIN_NAMESPACE + +static const QQuickItemPrivate::ChangeTypes AncestorChangeTypes = QQuickItemPrivate::Geometry + | QQuickItemPrivate::Parent + | QQuickItemPrivate::Children; + +static const QQuickItemPrivate::ChangeTypes ItemChangeTypes = QQuickItemPrivate::Geometry + | QQuickItemPrivate::Parent + | QQuickItemPrivate::Destroyed; + +QQuickScenePosListener::QQuickScenePosListener(QObject *parent) + : QObject(parent) + , m_enabled(false) + , m_item(0) +{ +} + +QQuickScenePosListener::~QQuickScenePosListener() +{ + if (m_item == 0) + return; + + QQuickItemPrivate::get(m_item)->removeItemChangeListener(this, ItemChangeTypes); + removeAncestorListeners(m_item->parentItem()); +} + +QQuickItem *QQuickScenePosListener::item() const +{ + return m_item; +} + +void QQuickScenePosListener::setItem(QQuickItem *item) +{ + if (m_item == item) + return; + + if (m_item != 0) { + QQuickItemPrivate::get(m_item)->removeItemChangeListener(this, ItemChangeTypes); + removeAncestorListeners(m_item->parentItem()); + } + + m_item = item; + + if (m_item == 0) + return; + + if (m_enabled) { + QQuickItemPrivate::get(m_item)->addItemChangeListener(this, ItemChangeTypes); + addAncestorListeners(m_item->parentItem()); + } + + updateScenePos(); +} + +QPointF QQuickScenePosListener::scenePos() const +{ + return m_scenePos; +} + +bool QQuickScenePosListener::isEnabled() const +{ + return m_enabled; +} + +void QQuickScenePosListener::setEnabled(bool enabled) +{ + if (m_enabled == enabled) + return; + + m_enabled = enabled; + + if (m_item != 0) { + if (enabled) { + QQuickItemPrivate::get(m_item)->addItemChangeListener(this, ItemChangeTypes); + addAncestorListeners(m_item->parentItem()); + } else { + QQuickItemPrivate::get(m_item)->removeItemChangeListener(this, ItemChangeTypes); + removeAncestorListeners(m_item->parentItem()); + } + } + + emit enabledChanged(); +} + +void QQuickScenePosListener::itemGeometryChanged(QQuickItem *, const QRectF &, const QRectF &) +{ + updateScenePos(); +} + +void QQuickScenePosListener::itemParentChanged(QQuickItem *, QQuickItem *parent) +{ + Q_ASSERT(m_item == parent); + + addAncestorListeners(parent); +} + +void QQuickScenePosListener::itemChildRemoved(QQuickItem *, QQuickItem *child) +{ + if (isAncestor(child)) + removeAncestorListeners(child); +} + +void QQuickScenePosListener::itemDestroyed(QQuickItem *item) +{ + Q_ASSERT(m_item == item); + + // Remove all injected listeners. + m_item = 0; + QQuickItemPrivate::get(item)->removeItemChangeListener(this, ItemChangeTypes); + removeAncestorListeners(item->parentItem()); +} + +void QQuickScenePosListener::updateScenePos() +{ + const QPointF &scenePos = m_item->mapToScene(QPointF(0, 0)); + if (m_scenePos != scenePos) { + m_scenePos = scenePos; + emit scenePosChanged(); + } +} + +/*! + \internal + + Remove this listener from \a item and all its ancestors. + */ +void QQuickScenePosListener::removeAncestorListeners(QQuickItem *item) +{ + Q_ASSERT(item != m_item); + + QQuickItem *p = item; + while (p != 0) { + QQuickItemPrivate::get(p)->removeItemChangeListener(this, AncestorChangeTypes); + p = p->parentItem(); + } +} + +/*! + \internal + + Injects this as a listener to \a item and all its ancestors. + */ +void QQuickScenePosListener::addAncestorListeners(QQuickItem *item) +{ + Q_ASSERT(item != m_item); + + QQuickItem *p = item; + while (p != 0) { + QQuickItemPrivate::get(p)->addItemChangeListener(this, AncestorChangeTypes); + p = p->parentItem(); + } +} + +bool QQuickScenePosListener::isAncestor(QQuickItem *item) const +{ + if (!m_item) + return false; + + QQuickItem *parent = m_item->parentItem(); + while (parent) { + if (parent == item) + return true; + parent = parent->parentItem(); + } + return false; +} + +QT_END_NAMESPACE diff --git a/src/controls/Private/qquicksceneposlistener_p.h b/src/controls/Private/qquicksceneposlistener_p.h new file mode 100644 index 00000000..7f0b6f37 --- /dev/null +++ b/src/controls/Private/qquicksceneposlistener_p.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** 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 QQUICKSCENEPOSLISTENER_P_H +#define QQUICKSCENEPOSLISTENER_P_H + +#include <QtCore/qobject.h> +#include <QtCore/qpoint.h> +#include <QtCore/qset.h> +#include <QtQuick/private/qquickitemchangelistener_p.h> + +QT_BEGIN_NAMESPACE + +class QQuickItem; + +class QQuickScenePosListener : public QObject, public QQuickItemChangeListener +{ + Q_OBJECT + Q_PROPERTY(QQuickItem *item READ item WRITE setItem FINAL) + Q_PROPERTY(QPointF scenePos READ scenePos NOTIFY scenePosChanged FINAL) + Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged FINAL) + +public: + explicit QQuickScenePosListener(QObject *parent = 0); + ~QQuickScenePosListener(); + + QQuickItem *item() const; + void setItem(QQuickItem *item); + + QPointF scenePos() const; + + bool isEnabled() const; + void setEnabled(bool enabled); + +Q_SIGNALS: + void scenePosChanged(); + void enabledChanged(); + +protected: + void itemGeometryChanged(QQuickItem *, const QRectF &, const QRectF &); + void itemParentChanged(QQuickItem *, QQuickItem *parent); + void itemChildRemoved(QQuickItem *, QQuickItem *child); + void itemDestroyed(QQuickItem *item); + +private: + void updateScenePos(); + + void removeAncestorListeners(QQuickItem *item); + void addAncestorListeners(QQuickItem *item); + + bool isAncestor(QQuickItem *item) const; + + bool m_enabled; + QPointF m_scenePos; + QQuickItem *m_item; +}; + +QT_END_NAMESPACE + +#endif // QQUICKSCENEPOSLISTENER_P_H diff --git a/src/controls/plugin.cpp b/src/controls/plugin.cpp index a402df68..7e98509d 100644 --- a/src/controls/plugin.cpp +++ b/src/controls/plugin.cpp @@ -55,6 +55,7 @@ #include "Private/qquickabstractstyle_p.h" #include "Private/qquickcontrolsprivate_p.h" #include "Private/qquicktreemodeladaptor_p.h" +#include "Private/qquicksceneposlistener_p.h" #ifdef QT_WIDGETS_LIB #include <QtQuick/qquickimageprovider.h> @@ -154,6 +155,7 @@ void QtQuickControlsPlugin::initializeEngine(QQmlEngine *engine, const char *uri qmlRegisterType<QQuickControlsPrivateAttached>(); qmlRegisterType<QQuickTreeModelAdaptor>(private_uri, 1, 0, "TreeModelAdaptor"); + qmlRegisterType<QQuickScenePosListener>(private_uri, 1, 0, "ScenePosListener"); qmlRegisterType<QQuickMenu>(private_uri, 1, 0, "MenuPrivate"); qmlRegisterType<QQuickMenuBar>(private_uri, 1, 0, "MenuBarPrivate"); |