From 5246dd2de211ffb81313a5c29ae7894545a28c43 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Fri, 27 Mar 2020 11:17:35 +0100 Subject: Register C++ types declaratively Adapt to the new way of registering C++ types. The types need to be seen at compile time so that code can be generated that invokes them. This patch: - Adds QML_* macros where applicable. - Adapts the build system files to the new way of registering modules. - Splits up the QtQuick.Controls[.*].impl files into their own plugins, as we can only register one QML module per .pro file. - Removes C++ type registration calls in every plugin. - Moves private types from src/quickcontrols2/quickcontrols2.pro to src/quickcontrols2/impl/quickcontrols2-impl.pro. Some of these types need to be exposed to QML, but quickcontrols2.pro is already in use to declare the QtQuick.Controls import (and also provides the public C++ QQuickStyle API), and the new QML_IMPORT_NAME/VERSION syntax only allows one module per project. As some of the types that need to be exposed to QML are also referenced by some C++ code (e.g. tests, etc.), we just move all of the private types to the new library. Follow-up patches will register the QML types declaratively. Task-number: QTBUG-82922 Change-Id: Iaf9ee106237d61701d57a8896f3822304c8151a6 Reviewed-by: Ulf Hermann --- src/quickcontrols2impl/qquickanimatednode.cpp | 167 ++++++ src/quickcontrols2impl/qquickanimatednode_p.h | 112 ++++ src/quickcontrols2impl/qquickattachedobject.cpp | 274 +++++++++ src/quickcontrols2impl/qquickattachedobject_p.h | 84 +++ src/quickcontrols2impl/qquickchecklabel.cpp | 49 ++ src/quickcontrols2impl/qquickchecklabel_p.h | 78 +++ src/quickcontrols2impl/qquickclippedtext.cpp | 116 ++++ src/quickcontrols2impl/qquickclippedtext_p.h | 98 ++++ src/quickcontrols2impl/qquickcolor.cpp | 66 +++ src/quickcontrols2impl/qquickcolor_p.h | 74 +++ src/quickcontrols2impl/qquickcolorimage.cpp | 103 ++++ src/quickcontrols2impl/qquickcolorimage_p.h | 90 +++ src/quickcontrols2impl/qquickiconimage.cpp | 213 +++++++ src/quickcontrols2impl/qquickiconimage_p.h | 96 +++ src/quickcontrols2impl/qquickiconimage_p_p.h | 77 +++ src/quickcontrols2impl/qquickiconlabel.cpp | 642 +++++++++++++++++++++ src/quickcontrols2impl/qquickiconlabel_p.h | 142 +++++ src/quickcontrols2impl/qquickiconlabel_p_p.h | 111 ++++ src/quickcontrols2impl/qquickitemgroup.cpp | 122 ++++ src/quickcontrols2impl/qquickitemgroup_p.h | 85 +++ src/quickcontrols2impl/qquickmnemoniclabel.cpp | 132 +++++ src/quickcontrols2impl/qquickmnemoniclabel_p.h | 84 +++ src/quickcontrols2impl/qquickpaddedrectangle.cpp | 211 +++++++ src/quickcontrols2impl/qquickpaddedrectangle_p.h | 121 ++++ src/quickcontrols2impl/qquickplaceholdertext.cpp | 73 +++ src/quickcontrols2impl/qquickplaceholdertext_p.h | 76 +++ src/quickcontrols2impl/qquicktumblerview.cpp | 320 ++++++++++ src/quickcontrols2impl/qquicktumblerview_p.h | 111 ++++ src/quickcontrols2impl/qtquickcontrols2foreign_p.h | 76 +++ .../qtquickcontrols2implglobal_p.h | 67 +++ src/quickcontrols2impl/quickcontrols2impl.pro | 54 ++ 31 files changed, 4124 insertions(+) create mode 100644 src/quickcontrols2impl/qquickanimatednode.cpp create mode 100644 src/quickcontrols2impl/qquickanimatednode_p.h create mode 100644 src/quickcontrols2impl/qquickattachedobject.cpp create mode 100644 src/quickcontrols2impl/qquickattachedobject_p.h create mode 100644 src/quickcontrols2impl/qquickchecklabel.cpp create mode 100644 src/quickcontrols2impl/qquickchecklabel_p.h create mode 100644 src/quickcontrols2impl/qquickclippedtext.cpp create mode 100644 src/quickcontrols2impl/qquickclippedtext_p.h create mode 100644 src/quickcontrols2impl/qquickcolor.cpp create mode 100644 src/quickcontrols2impl/qquickcolor_p.h create mode 100644 src/quickcontrols2impl/qquickcolorimage.cpp create mode 100644 src/quickcontrols2impl/qquickcolorimage_p.h create mode 100644 src/quickcontrols2impl/qquickiconimage.cpp create mode 100644 src/quickcontrols2impl/qquickiconimage_p.h create mode 100644 src/quickcontrols2impl/qquickiconimage_p_p.h create mode 100644 src/quickcontrols2impl/qquickiconlabel.cpp create mode 100644 src/quickcontrols2impl/qquickiconlabel_p.h create mode 100644 src/quickcontrols2impl/qquickiconlabel_p_p.h create mode 100644 src/quickcontrols2impl/qquickitemgroup.cpp create mode 100644 src/quickcontrols2impl/qquickitemgroup_p.h create mode 100644 src/quickcontrols2impl/qquickmnemoniclabel.cpp create mode 100644 src/quickcontrols2impl/qquickmnemoniclabel_p.h create mode 100644 src/quickcontrols2impl/qquickpaddedrectangle.cpp create mode 100644 src/quickcontrols2impl/qquickpaddedrectangle_p.h create mode 100644 src/quickcontrols2impl/qquickplaceholdertext.cpp create mode 100644 src/quickcontrols2impl/qquickplaceholdertext_p.h create mode 100644 src/quickcontrols2impl/qquicktumblerview.cpp create mode 100644 src/quickcontrols2impl/qquicktumblerview_p.h create mode 100644 src/quickcontrols2impl/qtquickcontrols2foreign_p.h create mode 100644 src/quickcontrols2impl/qtquickcontrols2implglobal_p.h create mode 100644 src/quickcontrols2impl/quickcontrols2impl.pro (limited to 'src/quickcontrols2impl') diff --git a/src/quickcontrols2impl/qquickanimatednode.cpp b/src/quickcontrols2impl/qquickanimatednode.cpp new file mode 100644 index 00000000..b4e665de --- /dev/null +++ b/src/quickcontrols2impl/qquickanimatednode.cpp @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickanimatednode_p.h" + +#include +#include + +// based on qtdeclarative/examples/quick/scenegraph/threadedanimation + +QT_BEGIN_NAMESPACE + +QQuickAnimatedNode::QQuickAnimatedNode(QQuickItem *target) + : m_window(target->window()) +{ +} + +bool QQuickAnimatedNode::isRunning() const +{ + return m_running; +} + +int QQuickAnimatedNode::currentTime() const +{ + int time = m_currentTime; + if (m_running) + time += m_timer.elapsed(); + return time; +} + +void QQuickAnimatedNode::setCurrentTime(int time) +{ + m_currentTime = time; + m_timer.restart(); +} + +int QQuickAnimatedNode::duration() const +{ + return m_duration; +} + +void QQuickAnimatedNode::setDuration(int duration) +{ + m_duration = duration; +} + +int QQuickAnimatedNode::loopCount() const +{ + return m_loopCount; +} + +void QQuickAnimatedNode::setLoopCount(int count) +{ + m_loopCount = count; +} + +void QQuickAnimatedNode::sync(QQuickItem *target) +{ + Q_UNUSED(target); +} + +QQuickWindow *QQuickAnimatedNode::window() const +{ + return m_window; +} + +void QQuickAnimatedNode::start(int duration) +{ + if (m_running) + return; + + m_running = true; + m_currentLoop = 0; + m_timer.restart(); + if (duration > 0) + m_duration = duration; + + connect(m_window, &QQuickWindow::beforeRendering, this, &QQuickAnimatedNode::advance, Qt::DirectConnection); + connect(m_window, &QQuickWindow::frameSwapped, this, &QQuickAnimatedNode::update, Qt::DirectConnection); + + // If we're inside a QQuickWidget, this call is necessary to ensure the widget + // gets updated for the first time. + m_window->update(); + + emit started(); +} + +void QQuickAnimatedNode::restart() +{ + stop(); + start(); +} + +void QQuickAnimatedNode::stop() +{ + if (!m_running) + return; + + m_running = false; + disconnect(m_window, &QQuickWindow::beforeRendering, this, &QQuickAnimatedNode::advance); + disconnect(m_window, &QQuickWindow::frameSwapped, this, &QQuickAnimatedNode::update); + emit stopped(); +} + +void QQuickAnimatedNode::updateCurrentTime(int time) +{ + Q_UNUSED(time); +} + +void QQuickAnimatedNode::advance() +{ + int time = currentTime(); + if (time > m_duration) { + time = 0; + setCurrentTime(0); + + if (m_loopCount > 0 && ++m_currentLoop >= m_loopCount) { + time = m_duration; // complete + stop(); + } + } + updateCurrentTime(time); + + // If we're inside a QQuickWidget, this call is necessary to ensure the widget gets updated. + m_window->update(); +} + +void QQuickAnimatedNode::update() +{ + if (m_running) + m_window->update(); +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickanimatednode_p.h b/src/quickcontrols2impl/qquickanimatednode_p.h new file mode 100644 index 00000000..e403f0bf --- /dev/null +++ b/src/quickcontrols2impl/qquickanimatednode_p.h @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKANIMATEDNODE_P_H +#define QQUICKANIMATEDNODE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickItem; +class QQuickWindow; + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickAnimatedNode : public QObject, public QSGTransformNode +{ + Q_OBJECT + +public: + explicit QQuickAnimatedNode(QQuickItem *target); + + bool isRunning() const; + + int currentTime() const; + void setCurrentTime(int time); + + int duration() const; + void setDuration(int duration); + + enum LoopCount { Infinite = -1 }; + + int loopCount() const; + void setLoopCount(int count); + + virtual void sync(QQuickItem *target); + + QQuickWindow *window() const; + + // must be called from sync() or updatePaintNode() + void start(int duration = 0); + void restart(); + void stop(); + +Q_SIGNALS: + void started(); + void stopped(); + +protected: + virtual void updateCurrentTime(int time); + +private Q_SLOTS: + void advance(); + void update(); + +private: + bool m_running = false; + int m_duration = 0; + int m_loopCount = 1; + int m_currentTime = 0; + int m_currentLoop = 0; + QElapsedTimer m_timer; + QQuickWindow *m_window = nullptr; +}; + +QT_END_NAMESPACE + +#endif // QQUICKANIMATEDNODE_P_H diff --git a/src/quickcontrols2impl/qquickattachedobject.cpp b/src/quickcontrols2impl/qquickattachedobject.cpp new file mode 100644 index 00000000..722f22f9 --- /dev/null +++ b/src/quickcontrols2impl/qquickattachedobject.cpp @@ -0,0 +1,274 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickattachedobject_p.h" + +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +static QQuickAttachedObject *attachedObject(const QMetaObject *type, QObject *object, bool create = false) +{ + if (!object) + return nullptr; + auto func = qmlAttachedPropertiesFunction(object, type); + return qobject_cast(qmlAttachedPropertiesObject(object, func, create)); +} + +static QQuickAttachedObject *findAttachedParent(const QMetaObject *type, QObject *object) +{ + QQuickItem *item = qobject_cast(object); + if (item) { + // lookup parent items and popups + QQuickItem *parent = item->parentItem(); + while (parent) { + QQuickAttachedObject *attached = attachedObject(type, parent); + if (attached) + return attached; + + QQuickPopup *popup = qobject_cast(parent->parent()); + if (popup) + return attachedObject(type, popup); + + parent = parent->parentItem(); + } + + // fallback to item's window + QQuickAttachedObject *attached = attachedObject(type, item->window()); + if (attached) + return attached; + } else { + // lookup popup's window + QQuickPopup *popup = qobject_cast(object); + if (popup) + return attachedObject(type, popup->popupItem()->window()); + } + + // lookup parent window + QQuickWindow *window = qobject_cast(object); + if (window) { + QQuickWindow *parentWindow = qobject_cast(window->parent()); + if (parentWindow) { + QQuickAttachedObject *attached = attachedObject(type, window); + if (attached) + return attached; + } + } + + // fallback to engine (global) + if (object) { + QQmlEngine *engine = qmlEngine(object); + if (engine) { + QByteArray name = QByteArray("_q_") + type->className(); + QQuickAttachedObject *attached = engine->property(name).value(); + if (!attached) { + attached = attachedObject(type, engine, true); + engine->setProperty(name, QVariant::fromValue(attached)); + } + return attached; + } + } + + return nullptr; +} + +static QList findAttachedChildren(const QMetaObject *type, QObject *object) +{ + QList children; + + QQuickItem *item = qobject_cast(object); + if (!item) { + QQuickWindow *window = qobject_cast(object); + if (window) { + item = window->contentItem(); + + const auto &windowChildren = window->children(); + for (QObject *child : windowChildren) { + QQuickWindow *childWindow = qobject_cast(child); + if (childWindow) { + QQuickAttachedObject *attached = attachedObject(type, childWindow); + if (attached) + children += attached; + } + } + } + } + + if (item) { + const auto childItems = item->childItems(); + for (QQuickItem *child : childItems) { + QQuickAttachedObject *attached = attachedObject(type, child); + if (attached) + children += attached; + else + children += findAttachedChildren(type, child); + } + } + + return children; +} + +static QQuickItem *findAttachedItem(QObject *parent) +{ + QQuickItem *item = qobject_cast(parent); + if (!item) { + QQuickPopup *popup = qobject_cast(parent); + if (popup) + item = popup->popupItem(); + } + return item; +} + +class QQuickAttachedObjectPrivate : public QObjectPrivate, public QQuickItemChangeListener +{ + Q_DECLARE_PUBLIC(QQuickAttachedObject) + +public: + static QQuickAttachedObjectPrivate *get(QQuickAttachedObject *attachedObject) + { + return attachedObject->d_func(); + } + + void attachTo(QObject *object); + void detachFrom(QObject *object); + + void itemWindowChanged(QQuickWindow *window); + void itemParentChanged(QQuickItem *item, QQuickItem *parent) override; + + QList attachedChildren; + QPointer attachedParent; +}; + +void QQuickAttachedObjectPrivate::attachTo(QObject *object) +{ + QQuickItem *item = findAttachedItem(object); + if (item) { + connect(item, &QQuickItem::windowChanged, this, &QQuickAttachedObjectPrivate::itemWindowChanged); + QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Parent); + } +} + +void QQuickAttachedObjectPrivate::detachFrom(QObject *object) +{ + QQuickItem *item = findAttachedItem(object); + if (item) { + disconnect(item, &QQuickItem::windowChanged, this, &QQuickAttachedObjectPrivate::itemWindowChanged); + QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Parent); + } +} + +void QQuickAttachedObjectPrivate::itemWindowChanged(QQuickWindow *window) +{ + Q_Q(QQuickAttachedObject); + QQuickAttachedObject *attachedParent = nullptr; + QQuickItem *item = qobject_cast(q->sender()); + if (item) + attachedParent = findAttachedParent(q->metaObject(), item); + if (!attachedParent) + attachedParent = attachedObject(q->metaObject(), window); + q->setAttachedParent(attachedParent); +} + +void QQuickAttachedObjectPrivate::itemParentChanged(QQuickItem *item, QQuickItem *parent) +{ + Q_Q(QQuickAttachedObject); + Q_UNUSED(parent); + q->setAttachedParent(findAttachedParent(q->metaObject(), item)); +} + +QQuickAttachedObject::QQuickAttachedObject(QObject *parent) + : QObject(*(new QQuickAttachedObjectPrivate), parent) +{ + Q_D(QQuickAttachedObject); + d->attachTo(parent); +} + +QQuickAttachedObject::~QQuickAttachedObject() +{ + Q_D(QQuickAttachedObject); + d->detachFrom(parent()); + setAttachedParent(nullptr); +} + +QList QQuickAttachedObject::attachedChildren() const +{ + Q_D(const QQuickAttachedObject); + return d->attachedChildren; +} + +QQuickAttachedObject *QQuickAttachedObject::attachedParent() const +{ + Q_D(const QQuickAttachedObject); + return d->attachedParent; +} + +void QQuickAttachedObject::setAttachedParent(QQuickAttachedObject *parent) +{ + Q_D(QQuickAttachedObject); + if (d->attachedParent == parent) + return; + + QQuickAttachedObject *oldParent = d->attachedParent; + if (d->attachedParent) + QQuickAttachedObjectPrivate::get(d->attachedParent)->attachedChildren.removeOne(this); + d->attachedParent = parent; + if (parent) + QQuickAttachedObjectPrivate::get(parent)->attachedChildren.append(this); + attachedParentChange(parent, oldParent); +} + +void QQuickAttachedObject::init() +{ + QQuickAttachedObject *attachedParent = findAttachedParent(metaObject(), parent()); + if (attachedParent) + setAttachedParent(attachedParent); + + const QList attachedChildren = findAttachedChildren(metaObject(), parent()); + for (QQuickAttachedObject *child : attachedChildren) + child->setAttachedParent(this); +} + +void QQuickAttachedObject::attachedParentChange(QQuickAttachedObject *newParent, QQuickAttachedObject *oldParent) +{ + Q_UNUSED(newParent); + Q_UNUSED(oldParent); +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickattachedobject_p.h b/src/quickcontrols2impl/qquickattachedobject_p.h new file mode 100644 index 00000000..cd2404c2 --- /dev/null +++ b/src/quickcontrols2impl/qquickattachedobject_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKATTACHEDOBJECT_P_H +#define QQUICKATTACHEDOBJECT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickAttachedObjectPrivate; + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickAttachedObject : public QObject +{ + Q_OBJECT + +public: + explicit QQuickAttachedObject(QObject *parent = nullptr); + ~QQuickAttachedObject(); + + QList attachedChildren() const; + + QQuickAttachedObject *attachedParent() const; + void setAttachedParent(QQuickAttachedObject *parent); + +protected: + void init(); + + virtual void attachedParentChange(QQuickAttachedObject *newParent, QQuickAttachedObject *oldParent); + +private: + Q_DISABLE_COPY(QQuickAttachedObject) + Q_DECLARE_PRIVATE(QQuickAttachedObject) +}; + +QT_END_NAMESPACE + +#endif // QQUICKATTACHEDOBJECT_P_H diff --git a/src/quickcontrols2impl/qquickchecklabel.cpp b/src/quickcontrols2impl/qquickchecklabel.cpp new file mode 100644 index 00000000..dccbf77f --- /dev/null +++ b/src/quickcontrols2impl/qquickchecklabel.cpp @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickchecklabel_p.h" + +QT_BEGIN_NAMESPACE + +QQuickCheckLabel::QQuickCheckLabel(QQuickItem *parent) : + QQuickText(parent) +{ + setHAlign(AlignLeft); + setVAlign(AlignVCenter); + setElideMode(ElideRight); +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickchecklabel_p.h b/src/quickcontrols2impl/qquickchecklabel_p.h new file mode 100644 index 00000000..823ce9df --- /dev/null +++ b/src/quickcontrols2impl/qquickchecklabel_p.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKCHECKLABEL_P_H +#define QQUICKCHECKLABEL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickCheckLabel : public QQuickText +{ + Q_OBJECT + QML_NAMED_ELEMENT(CheckLabel) + QML_ADDED_IN_VERSION(2, 3) + +public: + explicit QQuickCheckLabel(QQuickItem *parent = nullptr); +}; + +struct QQuickTextForeign +{ + Q_GADGET + QML_ANONYMOUS + QML_FOREIGN(QQuickText) + QML_ADDED_IN_VERSION(2, 3) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickCheckLabel) + +#endif // QQUICKCHECKLABEL_P_H diff --git a/src/quickcontrols2impl/qquickclippedtext.cpp b/src/quickcontrols2impl/qquickclippedtext.cpp new file mode 100644 index 00000000..862197f3 --- /dev/null +++ b/src/quickcontrols2impl/qquickclippedtext.cpp @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickclippedtext_p.h" + +#include + +QT_BEGIN_NAMESPACE + +QQuickClippedText::QQuickClippedText(QQuickItem *parent) + : QQuickText(parent) +{ +} + +qreal QQuickClippedText::clipX() const +{ + return m_clipX; +} + +void QQuickClippedText::setClipX(qreal x) +{ + if (qFuzzyCompare(x, m_clipX)) + return; + + m_clipX = x; + markClipDirty(); +} + +qreal QQuickClippedText::clipY() const +{ + return m_clipY; +} + +void QQuickClippedText::setClipY(qreal y) +{ + if (qFuzzyCompare(y, m_clipY)) + return; + + m_clipY = y; + markClipDirty(); +} + +qreal QQuickClippedText::clipWidth() const +{ + return m_clipWidth ? m_clipWidth : width(); +} + +void QQuickClippedText::setClipWidth(qreal width) +{ + m_hasClipWidth = true; + if (qFuzzyCompare(width, m_clipWidth)) + return; + + m_clipWidth = width; + markClipDirty(); +} + +qreal QQuickClippedText::clipHeight() const +{ + return m_clipHeight ? m_clipHeight : height(); +} + +void QQuickClippedText::setClipHeight(qreal height) +{ + m_hasClipHeight = true; + if (qFuzzyCompare(height, m_clipHeight)) + return; + + m_clipHeight = height; + markClipDirty(); +} + +QRectF QQuickClippedText::clipRect() const +{ + return QRectF(clipX(), clipY(), clipWidth(), clipHeight()); +} + +void QQuickClippedText::markClipDirty() +{ + QQuickItemPrivate::get(this)->dirty(QQuickItemPrivate::Size); +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickclippedtext_p.h b/src/quickcontrols2impl/qquickclippedtext_p.h new file mode 100644 index 00000000..f3b011fd --- /dev/null +++ b/src/quickcontrols2impl/qquickclippedtext_p.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKCLIPPEDTEXT_P_H +#define QQUICKCLIPPEDTEXT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickClippedText : public QQuickText +{ + Q_OBJECT + Q_PROPERTY(qreal clipX READ clipX WRITE setClipX FINAL) + Q_PROPERTY(qreal clipY READ clipY WRITE setClipY FINAL) + Q_PROPERTY(qreal clipWidth READ clipWidth WRITE setClipWidth FINAL) + Q_PROPERTY(qreal clipHeight READ clipHeight WRITE setClipHeight FINAL) + QML_NAMED_ELEMENT(ClippedText) + QML_ADDED_IN_VERSION(2, 2) + +public: + explicit QQuickClippedText(QQuickItem *parent = nullptr); + + qreal clipX() const; + void setClipX(qreal x); + + qreal clipY() const; + void setClipY(qreal y); + + qreal clipWidth() const; + void setClipWidth(qreal width); + + qreal clipHeight() const; + void setClipHeight(qreal height); + + QRectF clipRect() const override; + +private: + void markClipDirty(); + + bool m_hasClipWidth = false; + bool m_hasClipHeight = false; + qreal m_clipX = 0; + qreal m_clipY = 0; + qreal m_clipWidth = 0; + qreal m_clipHeight = 0; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickClippedText) + +#endif // QQUICKCLIPPEDTEXT_P_H diff --git a/src/quickcontrols2impl/qquickcolor.cpp b/src/quickcontrols2impl/qquickcolor.cpp new file mode 100644 index 00000000..8ae568fc --- /dev/null +++ b/src/quickcontrols2impl/qquickcolor.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickcolor_p.h" + +QT_BEGIN_NAMESPACE + +QQuickColor::QQuickColor(QObject *parent) : + QObject(parent) +{ +} + +QColor QQuickColor::transparent(const QColor &color, qreal opacity) const +{ + return QColor(color.red(), color.green(), color.blue(), + int(qreal(255) * qBound(qreal(0), opacity, qreal(1)))); +} + +QColor QQuickColor::blend(const QColor &a, const QColor &b, qreal factor) const +{ + if (factor <= 0.0) + return a; + if (factor >= 1.0) + return b; + + QColor color; + color.setRedF(a.redF() * (1.0 - factor) + b.redF() * factor); + color.setGreenF(a.greenF() * (1.0 - factor) + b.greenF() * factor); + color.setBlueF(a.blueF() * (1.0 - factor) + b.blueF() * factor); + return color; +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickcolor_p.h b/src/quickcontrols2impl/qquickcolor_p.h new file mode 100644 index 00000000..cd69d0d8 --- /dev/null +++ b/src/quickcontrols2impl/qquickcolor_p.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKCOLOR_P_H +#define QQUICKCOLOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickColor : public QObject +{ + Q_OBJECT + QML_NAMED_ELEMENT(Color) + QML_SINGLETON + QML_ADDED_IN_VERSION(2, 3) + +public: + explicit QQuickColor(QObject *parent = nullptr); + + Q_INVOKABLE QColor transparent(const QColor &color, qreal opacity) const; + Q_INVOKABLE QColor blend(const QColor &a, const QColor &b, qreal factor) const; +}; + +QT_END_NAMESPACE + +#endif // QQUICKCOLOR_P_H diff --git a/src/quickcontrols2impl/qquickcolorimage.cpp b/src/quickcontrols2impl/qquickcolorimage.cpp new file mode 100644 index 00000000..3afcef7e --- /dev/null +++ b/src/quickcontrols2impl/qquickcolorimage.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickcolorimage_p.h" + +#include + +QT_BEGIN_NAMESPACE + +QQuickColorImage::QQuickColorImage(QQuickItem *parent) + : QQuickImage(parent) +{ +} + +QColor QQuickColorImage::color() const +{ + return m_color; +} + +void QQuickColorImage::setColor(const QColor &color) +{ + if (m_color == color) + return; + + m_color = color; + if (isComponentComplete()) + load(); + emit colorChanged(); +} + +void QQuickColorImage::resetColor() +{ + setColor(Qt::transparent); +} + +QColor QQuickColorImage::defaultColor() const +{ + return m_defaultColor; +} + +void QQuickColorImage::setDefaultColor(const QColor &color) +{ + if (m_defaultColor == color) + return; + + m_defaultColor = color; + emit defaultColorChanged(); +} + +void QQuickColorImage::resetDefaultColor() +{ + setDefaultColor(Qt::transparent); +} + +void QQuickColorImage::pixmapChange() +{ + QQuickImage::pixmapChange(); + if (m_color.alpha() > 0 && m_color != m_defaultColor) { + QQuickImageBasePrivate *d = static_cast(QQuickItemPrivate::get(this)); + QImage image = d->pix.image(); + if (!image.isNull()) { + QPainter painter(&image); + painter.setCompositionMode(QPainter::CompositionMode_SourceIn); + painter.fillRect(image.rect(), m_color); + d->pix.setImage(image); + } + } +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickcolorimage_p.h b/src/quickcontrols2impl/qquickcolorimage_p.h new file mode 100644 index 00000000..faa2e42c --- /dev/null +++ b/src/quickcontrols2impl/qquickcolorimage_p.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKCOLORIMAGE_P_H +#define QQUICKCOLORIMAGE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickColorImage : public QQuickImage +{ + Q_OBJECT + Q_PROPERTY(QColor color READ color WRITE setColor RESET resetColor NOTIFY colorChanged FINAL) + Q_PROPERTY(QColor defaultColor READ defaultColor WRITE setDefaultColor RESET resetDefaultColor NOTIFY defaultColorChanged FINAL) + QML_NAMED_ELEMENT(ColorImage) + QML_ADDED_IN_VERSION(2, 3) + +public: + explicit QQuickColorImage(QQuickItem *parent = nullptr); + + QColor color() const; + void setColor(const QColor &color); + void resetColor(); + + QColor defaultColor() const; + void setDefaultColor(const QColor &color); + void resetDefaultColor(); + +Q_SIGNALS: + void colorChanged(); + void defaultColorChanged(); + +protected: + void pixmapChange() override; + +private: + QColor m_color = Qt::transparent; + QColor m_defaultColor = Qt::transparent; +}; + +QT_END_NAMESPACE + +#endif // QQUICKCOLORIMAGE_P_H diff --git a/src/quickcontrols2impl/qquickiconimage.cpp b/src/quickcontrols2impl/qquickiconimage.cpp new file mode 100644 index 00000000..1fc3abfc --- /dev/null +++ b/src/quickcontrols2impl/qquickiconimage.cpp @@ -0,0 +1,213 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickiconimage_p.h" +#include "qquickiconimage_p_p.h" + +#include +#include + +QT_BEGIN_NAMESPACE + +bool QQuickIconImagePrivate::updateDevicePixelRatio(qreal targetDevicePixelRatio) +{ + if (isThemeIcon) { + devicePixelRatio = calculateDevicePixelRatio(); + return true; + } + + return QQuickImagePrivate::updateDevicePixelRatio(targetDevicePixelRatio); +} + +void QQuickIconImagePrivate::updateIcon() +{ + Q_Q(QQuickIconImage); + // Both geometryChange() and QQuickImageBase::sourceSizeChanged() + // (which we connect to updateIcon() in the constructor) can be called as a result + // of updateIcon() changing the various sizes, so we must check that we're not recursing. + if (updatingIcon) + return; + + updatingIcon = true; + + QSize size = sourcesize; + // If no size is specified for theme icons, it will use the smallest available size. + if (size.width() <= 0) + size.setWidth(q->width()); + if (size.height() <= 0) + size.setHeight(q->height()); + + const qreal dpr = calculateDevicePixelRatio(); + const QIconLoaderEngineEntry *entry = QIconLoaderEngine::entryForSize(icon, size * dpr, qCeil(dpr)); + + if (entry) { + QQmlContext *context = qmlContext(q); + const QUrl entryUrl = QUrl::fromLocalFile(entry->filename); + url = context ? context->resolvedUrl(entryUrl) : entryUrl; + isThemeIcon = true; + } else { + url = source; + isThemeIcon = false; + } + q->load(); + + updatingIcon = false; +} + +void QQuickIconImagePrivate::updateFillMode() +{ + Q_Q(QQuickIconImage); + // If we start with a sourceSize of 28x28 and then set sourceSize.width to 24, the fillMode + // will change to PreserveAspectFit (because pixmapSize.width() > width()), which causes the + // pixmap to be reloaded at its original size of 28x28, which causes the fillMode to change + // to Pad (because pixmapSize.width() <= width()), and so on. + if (updatingFillMode) + return; + + updatingFillMode = true; + + const QSize pixmapSize = QSize(pix.width(), pix.height()) / calculateDevicePixelRatio(); + if (pixmapSize.width() > q->width() || pixmapSize.height() > q->height()) + q->setFillMode(QQuickImage::PreserveAspectFit); + else + q->setFillMode(QQuickImage::Pad); + + updatingFillMode = false; +} + +qreal QQuickIconImagePrivate::calculateDevicePixelRatio() const +{ + Q_Q(const QQuickIconImage); + return q->window() ? q->window()->effectiveDevicePixelRatio() : qApp->devicePixelRatio(); +} + +QQuickIconImage::QQuickIconImage(QQuickItem *parent) + : QQuickImage(*(new QQuickIconImagePrivate), parent) +{ + setFillMode(Pad); +} + +QString QQuickIconImage::name() const +{ + Q_D(const QQuickIconImage); + return d->icon.iconName; +} + +void QQuickIconImage::setName(const QString &name) +{ + Q_D(QQuickIconImage); + if (d->icon.iconName == name) + return; + + d->icon = QIconLoader::instance()->loadIcon(name); + if (isComponentComplete()) + d->updateIcon(); + emit nameChanged(); +} + +QColor QQuickIconImage::color() const +{ + Q_D(const QQuickIconImage); + return d->color; +} + +void QQuickIconImage::setColor(const QColor &color) +{ + Q_D(QQuickIconImage); + if (d->color == color) + return; + + d->color = color; + if (isComponentComplete()) + d->updateIcon(); + emit colorChanged(); +} + +void QQuickIconImage::setSource(const QUrl &source) +{ + Q_D(QQuickIconImage); + if (d->source == source) + return; + + d->source = source; + if (isComponentComplete()) + d->updateIcon(); + emit sourceChanged(source); +} + +void QQuickIconImage::componentComplete() +{ + Q_D(QQuickIconImage); + QQuickImage::componentComplete(); + d->updateIcon(); + QObjectPrivate::connect(this, &QQuickImageBase::sourceSizeChanged, d, &QQuickIconImagePrivate::updateIcon); +} + +void QQuickIconImage::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) +{ + Q_D(QQuickIconImage); + QQuickImage::geometryChange(newGeometry, oldGeometry); + if (isComponentComplete() && newGeometry.size() != oldGeometry.size()) + d->updateIcon(); +} + +void QQuickIconImage::itemChange(ItemChange change, const ItemChangeData &value) +{ + Q_D(QQuickIconImage); + if (change == ItemDevicePixelRatioHasChanged) + d->updateIcon(); + QQuickImage::itemChange(change, value); +} + +void QQuickIconImage::pixmapChange() +{ + Q_D(QQuickIconImage); + QQuickImage::pixmapChange(); + d->updateFillMode(); + + // Don't apply the color if we're recursing (updateFillMode() can cause us to recurse). + if (!d->updatingFillMode && d->color.alpha() > 0) { + QImage image = d->pix.image(); + if (!image.isNull()) { + QPainter painter(&image); + painter.setCompositionMode(QPainter::CompositionMode_SourceIn); + painter.fillRect(image.rect(), d->color); + d->pix.setImage(image); + } + } +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickiconimage_p.h b/src/quickcontrols2impl/qquickiconimage_p.h new file mode 100644 index 00000000..37891476 --- /dev/null +++ b/src/quickcontrols2impl/qquickiconimage_p.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKICONIMAGE_P_H +#define QQUICKICONIMAGE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickIconImagePrivate; + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickIconImage : public QQuickImage +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged FINAL) + QML_NAMED_ELEMENT(IconImage) + QML_ADDED_IN_VERSION(2, 3) + +public: + explicit QQuickIconImage(QQuickItem *parent = nullptr); + + QString name() const; + void setName(const QString &name); + + QColor color() const; + void setColor(const QColor &color); + + void setSource(const QUrl &url) override; + +Q_SIGNALS: + void nameChanged(); + void colorChanged(); + +protected: + void componentComplete() override; + void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; + void itemChange(ItemChange change, const ItemChangeData &value) override; + void pixmapChange() override; + +private: + Q_DISABLE_COPY(QQuickIconImage) + Q_DECLARE_PRIVATE(QQuickIconImage) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickIconImage) + +#endif // QQUICKICONIMAGE_P_H diff --git a/src/quickcontrols2impl/qquickiconimage_p_p.h b/src/quickcontrols2impl/qquickiconimage_p_p.h new file mode 100644 index 00000000..cec5956a --- /dev/null +++ b/src/quickcontrols2impl/qquickiconimage_p_p.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKICONIMAGE_P_P_H +#define QQUICKICONIMAGE_P_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickIconImagePrivate : public QQuickImagePrivate +{ + Q_DECLARE_PUBLIC(QQuickIconImage) + +public: + void updateIcon(); + void updateFillMode(); + qreal calculateDevicePixelRatio() const; + bool updateDevicePixelRatio(qreal targetDevicePixelRatio) override; + + QUrl source; + QColor color = Qt::transparent; + QThemeIconInfo icon; + bool updatingIcon = false; + bool isThemeIcon = false; + bool updatingFillMode = false; +}; + +QT_END_NAMESPACE + +#endif // QQUICKICONIMAGE_P_P_H diff --git a/src/quickcontrols2impl/qquickiconlabel.cpp b/src/quickcontrols2impl/qquickiconlabel.cpp new file mode 100644 index 00000000..d76da28f --- /dev/null +++ b/src/quickcontrols2impl/qquickiconlabel.cpp @@ -0,0 +1,642 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickiconlabel_p.h" +#include "qquickiconlabel_p_p.h" +#include "qquickiconimage_p.h" +#include "qquickmnemoniclabel_p.h" + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +static void beginClass(QQuickItem *item) +{ + if (QQmlParserStatus *parserStatus = qobject_cast(item)) + parserStatus->classBegin(); +} + +static void completeComponent(QQuickItem *item) +{ + if (QQmlParserStatus *parserStatus = qobject_cast(item)) + parserStatus->componentComplete(); +} + +bool QQuickIconLabelPrivate::hasIcon() const +{ + return display != QQuickIconLabel::TextOnly && !icon.isEmpty(); +} + +bool QQuickIconLabelPrivate::hasText() const +{ + return display != QQuickIconLabel::IconOnly && !text.isEmpty(); +} + +bool QQuickIconLabelPrivate::createImage() +{ + Q_Q(QQuickIconLabel); + if (image) + return false; + + image = new QQuickIconImage(q); + watchChanges(image); + beginClass(image); + image->setObjectName(QStringLiteral("image")); + image->setName(icon.name()); + image->setSource(icon.source()); + image->setSourceSize(QSize(icon.width(), icon.height())); + image->setColor(icon.color()); + image->setCache(icon.cache()); + QQmlEngine::setContextForObject(image, qmlContext(q)); + if (componentComplete) + completeComponent(image); + return true; +} + +bool QQuickIconLabelPrivate::destroyImage() +{ + if (!image) + return false; + + unwatchChanges(image); + delete image; + image = nullptr; + return true; +} + +bool QQuickIconLabelPrivate::updateImage() +{ + if (!hasIcon()) + return destroyImage(); + return createImage(); +} + +void QQuickIconLabelPrivate::syncImage() +{ + if (!image || icon.isEmpty()) + return; + + image->setName(icon.name()); + image->setSource(icon.source()); + image->setSourceSize(QSize(icon.width(), icon.height())); + image->setColor(icon.color()); + image->setCache(icon.cache()); + const int valign = alignment & Qt::AlignVertical_Mask; + image->setVerticalAlignment(static_cast(valign)); + const int halign = alignment & Qt::AlignHorizontal_Mask; + image->setHorizontalAlignment(static_cast(halign)); +} + +void QQuickIconLabelPrivate::updateOrSyncImage() +{ + if (updateImage()) { + if (componentComplete) { + updateImplicitSize(); + layout(); + } + } else { + syncImage(); + } +} + +bool QQuickIconLabelPrivate::createLabel() +{ + Q_Q(QQuickIconLabel); + if (label) + return false; + + label = new QQuickMnemonicLabel(q); + watchChanges(label); + beginClass(label); + label->setObjectName(QStringLiteral("label")); + label->setFont(font); + label->setColor(color); + label->setElideMode(QQuickText::ElideRight); + const int valign = alignment & Qt::AlignVertical_Mask; + label->setVAlign(static_cast(valign)); + const int halign = alignment & Qt::AlignHorizontal_Mask; + label->setHAlign(static_cast(halign)); + label->setText(text); + if (componentComplete) + completeComponent(label); + return true; +} + +bool QQuickIconLabelPrivate::destroyLabel() +{ + if (!label) + return false; + + unwatchChanges(label); + delete label; + label = nullptr; + return true; +} + +bool QQuickIconLabelPrivate::updateLabel() +{ + if (!hasText()) + return destroyLabel(); + return createLabel(); +} + +void QQuickIconLabelPrivate::syncLabel() +{ + if (!label) + return; + + label->setText(text); +} + +void QQuickIconLabelPrivate::updateOrSyncLabel() +{ + if (updateLabel()) { + if (componentComplete) { + updateImplicitSize(); + layout(); + } + } else { + syncLabel(); + } +} + +void QQuickIconLabelPrivate::updateImplicitSize() +{ + Q_Q(QQuickIconLabel); + const bool showIcon = image && hasIcon(); + const bool showText = label && hasText(); + const qreal horizontalPadding = leftPadding + rightPadding; + const qreal verticalPadding = topPadding + bottomPadding; + const qreal iconImplicitWidth = showIcon ? image->implicitWidth() : 0; + const qreal iconImplicitHeight = showIcon ? image->implicitHeight() : 0; + const qreal textImplicitWidth = showText ? label->implicitWidth() : 0; + const qreal textImplicitHeight = showText ? label->implicitHeight() : 0; + const qreal effectiveSpacing = showText && showIcon && image->implicitWidth() > 0 ? spacing : 0; + const qreal implicitWidth = display == QQuickIconLabel::TextBesideIcon ? iconImplicitWidth + textImplicitWidth + effectiveSpacing + : qMax(iconImplicitWidth, textImplicitWidth); + const qreal implicitHeight = display == QQuickIconLabel::TextUnderIcon ? iconImplicitHeight + textImplicitHeight + effectiveSpacing + : qMax(iconImplicitHeight, textImplicitHeight); + q->setImplicitSize(implicitWidth + horizontalPadding, implicitHeight + verticalPadding); +} + +// adapted from QStyle::alignedRect() +static QRectF alignedRect(bool mirrored, Qt::Alignment alignment, const QSizeF &size, const QRectF &rectangle) +{ + alignment = QGuiApplicationPrivate::visualAlignment(mirrored ? Qt::RightToLeft : Qt::LeftToRight, alignment); + qreal x = rectangle.x(); + qreal y = rectangle.y(); + const qreal w = size.width(); + const qreal h = size.height(); + if ((alignment & Qt::AlignVCenter) == Qt::AlignVCenter) + y += rectangle.height() / 2 - h / 2; + else if ((alignment & Qt::AlignBottom) == Qt::AlignBottom) + y += rectangle.height() - h; + if ((alignment & Qt::AlignRight) == Qt::AlignRight) + x += rectangle.width() - w; + else if ((alignment & Qt::AlignHCenter) == Qt::AlignHCenter) + x += rectangle.width() / 2 - w / 2; + return QRectF(x, y, w, h); +} + +void QQuickIconLabelPrivate::layout() +{ + Q_Q(QQuickIconLabel); + if (!componentComplete) + return; + + const qreal availableWidth = width - leftPadding - rightPadding; + const qreal availableHeight = height - topPadding - bottomPadding; + + switch (display) { + case QQuickIconLabel::IconOnly: + if (image) { + const QRectF iconRect = alignedRect(mirrored, alignment, + QSizeF(qMin(image->implicitWidth(), availableWidth), + qMin(image->implicitHeight(), availableHeight)), + QRectF(leftPadding, topPadding, availableWidth, availableHeight)); + image->setSize(iconRect.size()); + image->setPosition(iconRect.topLeft()); + } + break; + case QQuickIconLabel::TextOnly: + if (label) { + const QRectF textRect = alignedRect(mirrored, alignment, + QSizeF(qMin(label->implicitWidth(), availableWidth), + qMin(label->implicitHeight(), availableHeight)), + QRectF(leftPadding, topPadding, availableWidth, availableHeight)); + label->setSize(textRect.size()); + label->setPosition(textRect.topLeft()); + } + break; + + case QQuickIconLabel::TextUnderIcon: { + // Work out the sizes first, as the positions depend on them. + QSizeF iconSize; + QSizeF textSize; + if (image) { + iconSize.setWidth(qMin(image->implicitWidth(), availableWidth)); + iconSize.setHeight(qMin(image->implicitHeight(), availableHeight)); + } + qreal effectiveSpacing = 0; + if (label) { + if (!iconSize.isEmpty()) + effectiveSpacing = spacing; + textSize.setWidth(qMin(label->implicitWidth(), availableWidth)); + textSize.setHeight(qMin(label->implicitHeight(), availableHeight - iconSize.height() - effectiveSpacing)); + } + + QRectF combinedRect = alignedRect(mirrored, alignment, + QSizeF(qMax(iconSize.width(), textSize.width()), + iconSize.height() + effectiveSpacing + textSize.height()), + QRectF(leftPadding, topPadding, availableWidth, availableHeight)); + if (image) { + QRectF iconRect = alignedRect(mirrored, Qt::AlignHCenter | Qt::AlignTop, iconSize, combinedRect); + image->setSize(iconRect.size()); + image->setPosition(iconRect.topLeft()); + } + if (label) { + QRectF textRect = alignedRect(mirrored, Qt::AlignHCenter | Qt::AlignBottom, textSize, combinedRect); + label->setSize(textRect.size()); + label->setPosition(textRect.topLeft()); + } + break; + } + + case QQuickIconLabel::TextBesideIcon: + default: + // Work out the sizes first, as the positions depend on them. + QSizeF iconSize(0, 0); + QSizeF textSize(0, 0); + if (image) { + iconSize.setWidth(qMin(image->implicitWidth(), availableWidth)); + iconSize.setHeight(qMin(image->implicitHeight(), availableHeight)); + } + qreal effectiveSpacing = 0; + if (label) { + if (!iconSize.isEmpty()) + effectiveSpacing = spacing; + textSize.setWidth(qMin(label->implicitWidth(), availableWidth - iconSize.width() - effectiveSpacing)); + textSize.setHeight(qMin(label->implicitHeight(), availableHeight)); + } + + const QRectF combinedRect = alignedRect(mirrored, alignment, + QSizeF(iconSize.width() + effectiveSpacing + textSize.width(), + qMax(iconSize.height(), textSize.height())), + QRectF(leftPadding, topPadding, availableWidth, availableHeight)); + if (image) { + const QRectF iconRect = alignedRect(mirrored, Qt::AlignLeft | Qt::AlignVCenter, iconSize, combinedRect); + image->setSize(iconRect.size()); + image->setPosition(iconRect.topLeft()); + } + if (label) { + const QRectF textRect = alignedRect(mirrored, Qt::AlignRight | Qt::AlignVCenter, textSize, combinedRect); + label->setSize(textRect.size()); + label->setPosition(textRect.topLeft()); + } + break; + } + + q->setBaselineOffset(label ? label->y() + label->baselineOffset() : 0); +} + +static const QQuickItemPrivate::ChangeTypes itemChangeTypes = + QQuickItemPrivate::ImplicitWidth + | QQuickItemPrivate::ImplicitHeight + | QQuickItemPrivate::Destroyed; + +void QQuickIconLabelPrivate::watchChanges(QQuickItem *item) +{ + QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item); + itemPrivate->addItemChangeListener(this, itemChangeTypes); +} + +void QQuickIconLabelPrivate::unwatchChanges(QQuickItem* item) +{ + QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item); + itemPrivate->removeItemChangeListener(this, itemChangeTypes); +} + +void QQuickIconLabelPrivate::itemImplicitWidthChanged(QQuickItem *) +{ + updateImplicitSize(); + layout(); +} + +void QQuickIconLabelPrivate::itemImplicitHeightChanged(QQuickItem *) +{ + updateImplicitSize(); + layout(); +} + +void QQuickIconLabelPrivate::itemDestroyed(QQuickItem *item) +{ + unwatchChanges(item); + if (item == image) + image = nullptr; + else if (item == label) + label = nullptr; +} + +QQuickIconLabel::QQuickIconLabel(QQuickItem *parent) + : QQuickItem(*(new QQuickIconLabelPrivate), parent) +{ +} + +QQuickIconLabel::~QQuickIconLabel() +{ + Q_D(QQuickIconLabel); + if (d->image) + d->unwatchChanges(d->image); + if (d->label) + d->unwatchChanges(d->label); +} + +QQuickIcon QQuickIconLabel::icon() const +{ + Q_D(const QQuickIconLabel); + return d->icon; +} + +void QQuickIconLabel::setIcon(const QQuickIcon &icon) +{ + Q_D(QQuickIconLabel); + if (d->icon == icon) + return; + + d->icon = icon; + d->updateOrSyncImage(); +} + +QString QQuickIconLabel::text() const +{ + Q_D(const QQuickIconLabel); + return d->text; +} + +void QQuickIconLabel::setText(const QString &text) +{ + Q_D(QQuickIconLabel); + if (d->text == text) + return; + + d->text = text; + d->updateOrSyncLabel(); +} + +QFont QQuickIconLabel::font() const +{ + Q_D(const QQuickIconLabel); + return d->font; +} + +void QQuickIconLabel::setFont(const QFont &font) +{ + Q_D(QQuickIconLabel); + if (d->font == font) + return; + + d->font = font; + if (d->label) + d->label->setFont(font); +} + +QColor QQuickIconLabel::color() const +{ + Q_D(const QQuickIconLabel); + return d->color; +} + +void QQuickIconLabel::setColor(const QColor &color) +{ + Q_D(QQuickIconLabel); + if (d->color == color) + return; + + d->color = color; + if (d->label) + d->label->setColor(color); +} + +QQuickIconLabel::Display QQuickIconLabel::display() const +{ + Q_D(const QQuickIconLabel); + return d->display; +} + +void QQuickIconLabel::setDisplay(Display display) +{ + Q_D(QQuickIconLabel); + if (d->display == display) + return; + + d->display = display; + d->updateImage(); + d->updateLabel(); + d->updateImplicitSize(); + d->layout(); +} + +qreal QQuickIconLabel::spacing() const +{ + Q_D(const QQuickIconLabel); + return d->spacing; +} + +void QQuickIconLabel::setSpacing(qreal spacing) +{ + Q_D(QQuickIconLabel); + if (qFuzzyCompare(d->spacing, spacing)) + return; + + d->spacing = spacing; + if (d->image && d->label) { + d->updateImplicitSize(); + d->layout(); + } +} + +bool QQuickIconLabel::isMirrored() const +{ + Q_D(const QQuickIconLabel); + return d->mirrored; +} + +void QQuickIconLabel::setMirrored(bool mirrored) +{ + Q_D(QQuickIconLabel); + if (d->mirrored == mirrored) + return; + + d->mirrored = mirrored; + d->layout(); +} + +Qt::Alignment QQuickIconLabel::alignment() const +{ + Q_D(const QQuickIconLabel); + return d->alignment; +} + +void QQuickIconLabel::setAlignment(Qt::Alignment alignment) +{ + Q_D(QQuickIconLabel); + const int valign = alignment & Qt::AlignVertical_Mask; + const int halign = alignment & Qt::AlignHorizontal_Mask; + const uint align = (valign ? valign : Qt::AlignVCenter) | (halign ? halign : Qt::AlignHCenter); + if (d->alignment == align) + return; + + d->alignment = static_cast(align); + if (d->label) { + d->label->setVAlign(static_cast(valign)); + d->label->setHAlign(static_cast(halign)); + } + if (d->image) { + d->image->setVerticalAlignment(static_cast(valign)); + d->image->setHorizontalAlignment(static_cast(halign)); + } + d->layout(); +} + +qreal QQuickIconLabel::topPadding() const +{ + Q_D(const QQuickIconLabel); + return d->topPadding; +} + +void QQuickIconLabel::setTopPadding(qreal padding) +{ + Q_D(QQuickIconLabel); + if (qFuzzyCompare(d->topPadding, padding)) + return; + + d->topPadding = padding; + d->updateImplicitSize(); + d->layout(); +} + +void QQuickIconLabel::resetTopPadding() +{ + setTopPadding(0); +} + +qreal QQuickIconLabel::leftPadding() const +{ + Q_D(const QQuickIconLabel); + return d->leftPadding; +} + +void QQuickIconLabel::setLeftPadding(qreal padding) +{ + Q_D(QQuickIconLabel); + if (qFuzzyCompare(d->leftPadding, padding)) + return; + + d->leftPadding = padding; + d->updateImplicitSize(); + d->layout(); +} + +void QQuickIconLabel::resetLeftPadding() +{ + setLeftPadding(0); +} + +qreal QQuickIconLabel::rightPadding() const +{ + Q_D(const QQuickIconLabel); + return d->rightPadding; +} + +void QQuickIconLabel::setRightPadding(qreal padding) +{ + Q_D(QQuickIconLabel); + if (qFuzzyCompare(d->rightPadding, padding)) + return; + + d->rightPadding = padding; + d->updateImplicitSize(); + d->layout(); +} + +void QQuickIconLabel::resetRightPadding() +{ + setRightPadding(0); +} + +qreal QQuickIconLabel::bottomPadding() const +{ + Q_D(const QQuickIconLabel); + return d->bottomPadding; +} + +void QQuickIconLabel::setBottomPadding(qreal padding) +{ + Q_D(QQuickIconLabel); + if (qFuzzyCompare(d->bottomPadding, padding)) + return; + + d->bottomPadding = padding; + d->updateImplicitSize(); + d->layout(); +} + +void QQuickIconLabel::resetBottomPadding() +{ + setBottomPadding(0); +} + +void QQuickIconLabel::componentComplete() +{ + Q_D(QQuickIconLabel); + if (d->image) + completeComponent(d->image); + if (d->label) + completeComponent(d->label); + QQuickItem::componentComplete(); + d->layout(); +} + +void QQuickIconLabel::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) +{ + Q_D(QQuickIconLabel); + QQuickItem::geometryChange(newGeometry, oldGeometry); + d->layout(); +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickiconlabel_p.h b/src/quickcontrols2impl/qquickiconlabel_p.h new file mode 100644 index 00000000..1ea0affb --- /dev/null +++ b/src/quickcontrols2impl/qquickiconlabel_p.h @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKICONLABEL_P_H +#define QQUICKICONLABEL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickIconLabelPrivate; + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickIconLabel : public QQuickItem +{ + Q_OBJECT + Q_PROPERTY(QQuickIcon icon READ icon WRITE setIcon FINAL) + Q_PROPERTY(QString text READ text WRITE setText FINAL) + Q_PROPERTY(QFont font READ font WRITE setFont FINAL) + Q_PROPERTY(QColor color READ color WRITE setColor FINAL) + Q_PROPERTY(Display display READ display WRITE setDisplay FINAL) + Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing FINAL) + Q_PROPERTY(bool mirrored READ isMirrored WRITE setMirrored FINAL) + Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment FINAL) + Q_PROPERTY(qreal topPadding READ topPadding WRITE setTopPadding RESET resetTopPadding FINAL) + Q_PROPERTY(qreal leftPadding READ leftPadding WRITE setLeftPadding RESET resetLeftPadding FINAL) + Q_PROPERTY(qreal rightPadding READ rightPadding WRITE setRightPadding RESET resetRightPadding FINAL) + Q_PROPERTY(qreal bottomPadding READ bottomPadding WRITE setBottomPadding RESET resetBottomPadding FINAL) + QML_NAMED_ELEMENT(IconLabel) + QML_ADDED_IN_VERSION(2, 3) + +public: + enum Display { + IconOnly, + TextOnly, + TextBesideIcon, + TextUnderIcon + }; + Q_ENUM(Display) + + explicit QQuickIconLabel(QQuickItem *parent = nullptr); + ~QQuickIconLabel(); + + QQuickIcon icon() const; + void setIcon(const QQuickIcon &icon); + + QString text() const; + void setText(const QString &text); + + QFont font() const; + void setFont(const QFont &font); + + QColor color() const; + void setColor(const QColor &color); + + Display display() const; + void setDisplay(Display display); + + qreal spacing() const; + void setSpacing(qreal spacing); + + bool isMirrored() const; + void setMirrored(bool mirrored); + + Qt::Alignment alignment() const; + void setAlignment(Qt::Alignment alignment); + + qreal topPadding() const; + void setTopPadding(qreal padding); + void resetTopPadding(); + + qreal leftPadding() const; + void setLeftPadding(qreal padding); + void resetLeftPadding(); + + qreal rightPadding() const; + void setRightPadding(qreal padding); + void resetRightPadding(); + + qreal bottomPadding() const; + void setBottomPadding(qreal padding); + void resetBottomPadding(); + +protected: + void componentComplete() override; + void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; + +private: + Q_DISABLE_COPY(QQuickIconLabel) + Q_DECLARE_PRIVATE(QQuickIconLabel) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickIconLabel) + +#endif // QQUICKICONLABEL_P_H diff --git a/src/quickcontrols2impl/qquickiconlabel_p_p.h b/src/quickcontrols2impl/qquickiconlabel_p_p.h new file mode 100644 index 00000000..18cefe53 --- /dev/null +++ b/src/quickcontrols2impl/qquickiconlabel_p_p.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKICONLABEL_P_P_H +#define QQUICKICONLABEL_P_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickIconImage; +class QQuickMnemonicLabel; + +class QQuickIconLabelPrivate : public QQuickItemPrivate, public QQuickItemChangeListener +{ + Q_DECLARE_PUBLIC(QQuickIconLabel) + +public: + bool hasIcon() const; + bool hasText() const; + + bool createImage(); + bool destroyImage(); + bool updateImage(); + void syncImage(); + void updateOrSyncImage(); + + bool createLabel(); + bool destroyLabel(); + bool updateLabel(); + void syncLabel(); + void updateOrSyncLabel(); + + void updateImplicitSize(); + void layout(); + + void watchChanges(QQuickItem *item); + void unwatchChanges(QQuickItem *item); + void setPositioningDirty(); + + bool isLeftToRight() const; + + void itemImplicitWidthChanged(QQuickItem *) override; + void itemImplicitHeightChanged(QQuickItem *) override; + void itemDestroyed(QQuickItem *item) override; + + bool mirrored = false; + QQuickIconLabel::Display display = QQuickIconLabel::TextBesideIcon; + Qt::Alignment alignment = Qt::AlignCenter; + qreal spacing = 0; + qreal topPadding = 0; + qreal leftPadding = 0; + qreal rightPadding = 0; + qreal bottomPadding = 0; + QFont font; + QColor color; + QString text; + QQuickIcon icon; + QQuickIconImage *image = nullptr; + QQuickMnemonicLabel *label = nullptr; +}; + +QT_END_NAMESPACE + +#endif // QQUICKICONLABEL_P_P_H diff --git a/src/quickcontrols2impl/qquickitemgroup.cpp b/src/quickcontrols2impl/qquickitemgroup.cpp new file mode 100644 index 00000000..c4307001 --- /dev/null +++ b/src/quickcontrols2impl/qquickitemgroup.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickitemgroup_p.h" + +#include + +QT_BEGIN_NAMESPACE + +QQuickItemGroup::QQuickItemGroup(QQuickItem *parent) + : QQuickImplicitSizeItem(*(new QQuickImplicitSizeItemPrivate), parent) +{ +} + +QQuickItemGroup::~QQuickItemGroup() +{ + const auto children = childItems(); + for (QQuickItem *child : children) + unwatch(child); +} + +void QQuickItemGroup::watch(QQuickItem *item) +{ + QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight); +} + +void QQuickItemGroup::unwatch(QQuickItem *item) +{ + QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight); +} + +QSizeF QQuickItemGroup::calculateImplicitSize() const +{ + qreal width = 0; + qreal height = 0; + const auto children = childItems(); + for (QQuickItem *child : children) { + width = qMax(width, child->implicitWidth()); + height = qMax(height, child->implicitHeight()); + } + return QSizeF(width, height); +} + +void QQuickItemGroup::updateImplicitSize() +{ + QSizeF size = calculateImplicitSize(); + setImplicitSize(size.width(), size.height()); +} + +void QQuickItemGroup::itemChange(ItemChange change, const ItemChangeData &data) +{ + QQuickImplicitSizeItem::itemChange(change, data); + switch (change) { + case ItemChildAddedChange: + watch(data.item); + data.item->setSize(QSizeF(width(), height())); + updateImplicitSize(); + break; + case ItemChildRemovedChange: + unwatch(data.item); + updateImplicitSize(); + break; + default: + break; + } +} + +void QQuickItemGroup::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) +{ + QQuickImplicitSizeItem::geometryChange(newGeometry, oldGeometry); + + if (newGeometry.size() != oldGeometry.size()) { + const auto children = childItems(); + for (QQuickItem *child : children) + child->setSize(newGeometry.size()); + } +} + +void QQuickItemGroup::itemImplicitWidthChanged(QQuickItem *) +{ + setImplicitWidth(calculateImplicitSize().width()); +} + +void QQuickItemGroup::itemImplicitHeightChanged(QQuickItem *) +{ + setImplicitHeight(calculateImplicitSize().height()); +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickitemgroup_p.h b/src/quickcontrols2impl/qquickitemgroup_p.h new file mode 100644 index 00000000..9c04a990 --- /dev/null +++ b/src/quickcontrols2impl/qquickitemgroup_p.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKITEMGROUP_P_H +#define QQUICKITEMGROUP_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickItemGroup : public QQuickImplicitSizeItem, protected QQuickItemChangeListener +{ + Q_OBJECT + QML_NAMED_ELEMENT(ItemGroup) + QML_ADDED_IN_VERSION(2, 2) + +public: + explicit QQuickItemGroup(QQuickItem *parent = nullptr); + ~QQuickItemGroup(); + +protected: + void watch(QQuickItem *item); + void unwatch(QQuickItem *item); + + QSizeF calculateImplicitSize() const; + void updateImplicitSize(); + + void itemChange(ItemChange change, const ItemChangeData &data) override; + void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; + + void itemImplicitWidthChanged(QQuickItem *item) override; + void itemImplicitHeightChanged(QQuickItem *item) override; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickItemGroup) + +#endif // QQUICKITEMGROUP_P_H diff --git a/src/quickcontrols2impl/qquickmnemoniclabel.cpp b/src/quickcontrols2impl/qquickmnemoniclabel.cpp new file mode 100644 index 00000000..309d076a --- /dev/null +++ b/src/quickcontrols2impl/qquickmnemoniclabel.cpp @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickmnemoniclabel_p.h" + +#include + +QT_BEGIN_NAMESPACE + +QQuickMnemonicLabel::QQuickMnemonicLabel(QQuickItem *parent) + : QQuickText(parent) +{ +} + +QString QQuickMnemonicLabel::text() const +{ + return m_fullText; +} + +void QQuickMnemonicLabel::setText(const QString &text) +{ + if (m_fullText == text) + return; + + m_fullText = text; + updateMnemonic(); +} + +bool QQuickMnemonicLabel::isMnemonicVisible() const +{ + return m_mnemonicVisible; +} + +void QQuickMnemonicLabel::setMnemonicVisible(bool visible) +{ + if (m_mnemonicVisible == visible) + return; + + m_mnemonicVisible = visible; + updateMnemonic(); + + if (isComponentComplete()) + forceLayout(); +} + +static QTextLayout::FormatRange underlineRange(int start, int length = 1) +{ + QTextLayout::FormatRange range; + range.start = start; + range.length = length; + range.format.setFontUnderline(true); + return range; +} + +// based on QPlatformTheme::removeMnemonics() +void QQuickMnemonicLabel::updateMnemonic() +{ + QString text(m_fullText.size(), QChar::Null); + int idx = 0; + int pos = 0; + int len = m_fullText.length(); + QList formats; + while (len) { + if (m_fullText.at(pos) == QLatin1Char('&') && (len == 1 || m_fullText.at(pos + 1) != QLatin1Char('&'))) { + if (m_mnemonicVisible && (pos == 0 || m_fullText.at(pos - 1) != QLatin1Char('&'))) + formats += underlineRange(pos); + ++pos; + --len; + if (len == 0) + break; + } else if (m_fullText.at(pos) == QLatin1Char('(') && len >= 4 && + m_fullText.at(pos + 1) == QLatin1Char('&') && + m_fullText.at(pos + 2) != QLatin1Char('&') && + m_fullText.at(pos + 3) == QLatin1Char(')')) { + // a mnemonic with format "\s*(&X)" + if (m_mnemonicVisible) { + formats += underlineRange(pos + 1); + } else { + int n = 0; + while (idx > n && text.at(idx - n - 1).isSpace()) + ++n; + idx -= n; + pos += 4; + len -= 4; + continue; + } + } + text[idx] = m_fullText.at(pos); + ++pos; + ++idx; + --len; + } + text.truncate(idx); + + QQuickTextPrivate::get(this)->layout.setFormats(formats); + QQuickText::setText(text); +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickmnemoniclabel_p.h b/src/quickcontrols2impl/qquickmnemoniclabel_p.h new file mode 100644 index 00000000..69ddbfb1 --- /dev/null +++ b/src/quickcontrols2impl/qquickmnemoniclabel_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKMNEMONICLABEL_P_H +#define QQUICKMNEMONICLABEL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickMnemonicLabel : public QQuickText +{ + Q_OBJECT + Q_PROPERTY(QString text READ text WRITE setText FINAL) + Q_PROPERTY(bool mnemonicVisible READ isMnemonicVisible WRITE setMnemonicVisible FINAL) + QML_NAMED_ELEMENT(MnemonicLabel) + QML_ADDED_IN_VERSION(2, 3) + +public: + explicit QQuickMnemonicLabel(QQuickItem *parent = nullptr); + + QString text() const; + void setText(const QString &text); + + bool isMnemonicVisible() const; + void setMnemonicVisible(bool visible); + +private: + void updateMnemonic(); + + bool m_mnemonicVisible = true; + QString m_fullText; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickMnemonicLabel) + +#endif // QQUICKMNEMONICLABEL_P_H diff --git a/src/quickcontrols2impl/qquickpaddedrectangle.cpp b/src/quickcontrols2impl/qquickpaddedrectangle.cpp new file mode 100644 index 00000000..fd9e9d90 --- /dev/null +++ b/src/quickcontrols2impl/qquickpaddedrectangle.cpp @@ -0,0 +1,211 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickpaddedrectangle_p.h" + +#include + +QT_BEGIN_NAMESPACE + +QQuickPaddedRectangle::QQuickPaddedRectangle(QQuickItem *parent) : + QQuickRectangle(parent) +{ +} + +qreal QQuickPaddedRectangle::padding() const +{ + return m_padding; +} + +void QQuickPaddedRectangle::setPadding(qreal padding) +{ + if (!qFuzzyCompare(m_padding, padding)) { + m_padding = padding; + update(); + emit paddingChanged(); + if (m_hasTopPadding) + emit topPaddingChanged(); + if (!m_hasLeftPadding) + emit leftPaddingChanged(); + if (!m_hasRightPadding) + emit rightPaddingChanged(); + if (!m_hasBottomPadding) + emit bottomPaddingChanged(); + } +} + +void QQuickPaddedRectangle::resetPadding() +{ + setPadding(0); +} + +qreal QQuickPaddedRectangle::topPadding() const +{ + return m_hasTopPadding ? m_topPadding : m_padding; +} + +void QQuickPaddedRectangle::setTopPadding(qreal padding) +{ + setTopPadding(padding, true); +} + +void QQuickPaddedRectangle::resetTopPadding() +{ + setTopPadding(0, false); +} + +qreal QQuickPaddedRectangle::leftPadding() const +{ + return m_hasLeftPadding ? m_leftPadding : m_padding; +} + +void QQuickPaddedRectangle::setLeftPadding(qreal padding) +{ + setLeftPadding(padding, true); +} + +void QQuickPaddedRectangle::resetLeftPadding() +{ + setLeftPadding(0, false); +} + +qreal QQuickPaddedRectangle::rightPadding() const +{ + return m_hasRightPadding ? m_rightPadding : m_padding; +} + +void QQuickPaddedRectangle::setRightPadding(qreal padding) +{ + setRightPadding(padding, true); +} + +void QQuickPaddedRectangle::resetRightPadding() +{ + setRightPadding(0, false); +} + +qreal QQuickPaddedRectangle::bottomPadding() const +{ + return m_hasBottomPadding ? m_bottomPadding : m_padding; +} + +void QQuickPaddedRectangle::setBottomPadding(qreal padding) +{ + setBottomPadding(padding, true); +} + +void QQuickPaddedRectangle::resetBottomPadding() +{ + setBottomPadding(0, false); +} + +void QQuickPaddedRectangle::setTopPadding(qreal padding, bool has) +{ + qreal oldPadding = topPadding(); + m_hasTopPadding = has; + m_topPadding = padding; + if (!qFuzzyCompare(oldPadding, padding)) { + update(); + emit topPaddingChanged(); + } +} + +void QQuickPaddedRectangle::setLeftPadding(qreal padding, bool has) +{ + qreal oldPadding = leftPadding(); + m_hasLeftPadding = has; + m_leftPadding = padding; + if (!qFuzzyCompare(oldPadding, padding)) { + update(); + emit leftPaddingChanged(); + } +} + +void QQuickPaddedRectangle::setRightPadding(qreal padding, bool has) +{ + qreal oldPadding = rightPadding(); + m_hasRightPadding = has; + m_rightPadding = padding; + if (!qFuzzyCompare(oldPadding, padding)) { + update(); + emit rightPaddingChanged(); + } +} + +void QQuickPaddedRectangle::setBottomPadding(qreal padding, bool has) +{ + qreal oldPadding = bottomPadding(); + m_hasBottomPadding = has; + m_bottomPadding = padding; + if (!qFuzzyCompare(oldPadding, padding)) { + update(); + emit bottomPaddingChanged(); + } +} + +QSGNode *QQuickPaddedRectangle::updatePaintNode(QSGNode *node, UpdatePaintNodeData *data) +{ + QSGTransformNode *transformNode = static_cast(node); + if (!transformNode) + transformNode = new QSGTransformNode; + + QSGInternalRectangleNode *rectNode = static_cast(QQuickRectangle::updatePaintNode(transformNode->firstChild(), data)); + + if (rectNode) { + if (!transformNode->firstChild()) + transformNode->appendChildNode(rectNode); + + qreal top = topPadding(); + qreal left = leftPadding(); + qreal right = rightPadding(); + qreal bottom = bottomPadding(); + + if (!qFuzzyIsNull(top) || !qFuzzyIsNull(left) || !qFuzzyIsNull(right) || !qFuzzyIsNull(bottom)) { + QMatrix4x4 m; + m.translate(left, top); + transformNode->setMatrix(m); + + qreal w = qMax(0.0, width() -left-right); + qreal h = qMax(0.0, height() -top-bottom); + + rectNode->setRect(QRectF(0, 0, w, h)); + rectNode->update(); + } + } + return transformNode; +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickpaddedrectangle_p.h b/src/quickcontrols2impl/qquickpaddedrectangle_p.h new file mode 100644 index 00000000..27743270 --- /dev/null +++ b/src/quickcontrols2impl/qquickpaddedrectangle_p.h @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKPADDEDRECTANGLE_P_H +#define QQUICKPADDEDRECTANGLE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickPaddedRectangle : public QQuickRectangle +{ + Q_OBJECT + Q_PROPERTY(qreal padding READ padding WRITE setPadding RESET resetPadding NOTIFY paddingChanged FINAL) + Q_PROPERTY(qreal topPadding READ topPadding WRITE setTopPadding RESET resetTopPadding NOTIFY topPaddingChanged FINAL) + Q_PROPERTY(qreal leftPadding READ leftPadding WRITE setLeftPadding RESET resetLeftPadding NOTIFY leftPaddingChanged FINAL) + Q_PROPERTY(qreal rightPadding READ rightPadding WRITE setRightPadding RESET resetRightPadding NOTIFY rightPaddingChanged FINAL) + Q_PROPERTY(qreal bottomPadding READ bottomPadding WRITE setBottomPadding RESET resetBottomPadding NOTIFY bottomPaddingChanged FINAL) + QML_NAMED_ELEMENT(PaddedRectangle) + QML_ADDED_IN_VERSION(2, 0) + +public: + QQuickPaddedRectangle(QQuickItem *parent = nullptr); + + qreal padding() const; + void setPadding(qreal padding); + void resetPadding(); + + qreal topPadding() const; + void setTopPadding(qreal padding); + void resetTopPadding(); + + qreal leftPadding() const; + void setLeftPadding(qreal padding); + void resetLeftPadding(); + + qreal rightPadding() const; + void setRightPadding(qreal padding); + void resetRightPadding(); + + qreal bottomPadding() const; + void setBottomPadding(qreal padding); + void resetBottomPadding(); + +Q_SIGNALS: + void paddingChanged(); + void topPaddingChanged(); + void leftPaddingChanged(); + void rightPaddingChanged(); + void bottomPaddingChanged(); + +protected: + QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; + +private: + void setTopPadding(qreal padding, bool has); + void setLeftPadding(qreal padding, bool has); + void setRightPadding(qreal padding, bool has); + void setBottomPadding(qreal padding, bool has); + + qreal m_padding = 0; + qreal m_topPadding = 0; + qreal m_leftPadding = 0; + qreal m_rightPadding = 0; + qreal m_bottomPadding = 0; + bool m_hasTopPadding = false; + bool m_hasLeftPadding = false; + bool m_hasRightPadding = false; + bool m_hasBottomPadding = false; +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickPaddedRectangle) + +#endif // QQUICKPADDEDRECTANGLE_P_H diff --git a/src/quickcontrols2impl/qquickplaceholdertext.cpp b/src/quickcontrols2impl/qquickplaceholdertext.cpp new file mode 100644 index 00000000..2fe6be07 --- /dev/null +++ b/src/quickcontrols2impl/qquickplaceholdertext.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquickplaceholdertext_p.h" + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +QQuickPlaceholderText::QQuickPlaceholderText(QQuickItem *parent) : QQuickText(parent) +{ +} + +void QQuickPlaceholderText::componentComplete() +{ + QQuickText::componentComplete(); + connect(parentItem(), SIGNAL(effectiveHorizontalAlignmentChanged()), this, SLOT(updateAlignment())); + updateAlignment(); +} + +void QQuickPlaceholderText::updateAlignment() +{ + if (QQuickTextInput *input = qobject_cast(parentItem())) { + if (QQuickTextInputPrivate::get(input)->hAlignImplicit) + resetHAlign(); + else + setHAlign(static_cast(input->hAlign())); + } else if (QQuickTextEdit *edit = qobject_cast(parentItem())) { + if (QQuickTextEditPrivate::get(edit)->hAlignImplicit) + resetHAlign(); + else + setHAlign(static_cast(edit->hAlign())); + } else { + resetHAlign(); + } +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qquickplaceholdertext_p.h b/src/quickcontrols2impl/qquickplaceholdertext_p.h new file mode 100644 index 00000000..0beccd72 --- /dev/null +++ b/src/quickcontrols2impl/qquickplaceholdertext_p.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKPLACEHOLDERTEXT_P_H +#define QQUICKPLACEHOLDERTEXT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickPlaceholderText : public QQuickText +{ + Q_OBJECT + QML_NAMED_ELEMENT(PlaceholderText) + QML_ADDED_IN_VERSION(2, 2) + +public: + explicit QQuickPlaceholderText(QQuickItem *parent = nullptr); + +protected: + void componentComplete() override; + +private Q_SLOTS: + void updateAlignment(); +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQuickPlaceholderText) + +#endif // QQUICKPLACEHOLDERTEXT_P_H diff --git a/src/quickcontrols2impl/qquicktumblerview.cpp b/src/quickcontrols2impl/qquicktumblerview.cpp new file mode 100644 index 00000000..911e2ae8 --- /dev/null +++ b/src/quickcontrols2impl/qquicktumblerview.cpp @@ -0,0 +1,320 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 "qquicktumblerview_p.h" + +#include +#include +#include +#include + +#include +#include + +QT_BEGIN_NAMESPACE + +Q_LOGGING_CATEGORY(lcTumblerView, "qt.quick.controls.tumblerview") + +QQuickTumblerView::QQuickTumblerView(QQuickItem *parent) : + QQuickItem(parent) +{ + // We don't call createView() here because we don't know what the wrap flag is set to + // yet, and we don't want to create a view that might never get used. +} + +QVariant QQuickTumblerView::model() const +{ + return m_model; +} + +void QQuickTumblerView::setModel(const QVariant &model) +{ + qCDebug(lcTumblerView) << "setting model to:" << model << "on" + << (m_pathView ? static_cast(m_pathView) : static_cast(m_listView)); + if (model == m_model) + return; + + m_model = model; + + if (m_pathView) { + m_pathView->setModel(m_model); + } else if (m_listView) { + // QQuickItemView::setModel() resets the current index, + // but if we're still creating the Tumbler, it should be maintained. + const int oldCurrentIndex = m_listView->currentIndex(); + m_listView->setModel(m_model); + if (!isComponentComplete()) + m_listView->setCurrentIndex(oldCurrentIndex); + } + + emit modelChanged(); +} + +QQmlComponent *QQuickTumblerView::delegate() const +{ + return m_delegate; +} + +void QQuickTumblerView::setDelegate(QQmlComponent *delegate) +{ + qCDebug(lcTumblerView) << "setting delegate to:" << delegate << "on" + << (m_pathView ? static_cast(m_pathView) : static_cast(m_listView)); + if (delegate == m_delegate) + return; + + m_delegate = delegate; + + if (m_pathView) + m_pathView->setDelegate(m_delegate); + else if (m_listView) + m_listView->setDelegate(m_delegate); + + emit delegateChanged(); +} + +QQuickPath *QQuickTumblerView::path() const +{ + return m_path; +} + +void QQuickTumblerView::setPath(QQuickPath *path) +{ + if (path == m_path) + return; + + m_path = path; + emit pathChanged(); +} + +void QQuickTumblerView::createView() +{ + Q_ASSERT(m_tumbler); + + // We create a view regardless of whether or not we know + // the count yet, because we rely on the view to tell us the count. + if (m_tumbler->wrap()) { + if (m_listView) { + // It's necessary to call deleteLater() rather than delete, + // as this code is most likely being run in rensponse to a signal + // emission somewhere in the list view's internals, so we need to + // wait until that has finished. + m_listView->deleteLater(); + QQml_setParent_noEvent(m_listView, nullptr); + // The auto tests pass with unparenting the list view alone, but + // just to be sure, we unset some other things as well. + m_listView->setParentItem(nullptr); + m_listView->setVisible(false); + m_listView->setModel(QVariant()); + m_listView = nullptr; + } + + if (!m_pathView) { + qCDebug(lcTumblerView) << "creating PathView"; + + m_pathView = new QQuickPathView; + QQmlEngine::setContextForObject(m_pathView, qmlContext(this)); + QQml_setParent_noEvent(m_pathView, this); + m_pathView->setParentItem(this); + m_pathView->setPath(m_path); + m_pathView->setDelegate(m_delegate); + m_pathView->setPreferredHighlightBegin(0.5); + m_pathView->setPreferredHighlightEnd(0.5); + m_pathView->setHighlightMoveDuration(1000); + m_pathView->setClip(true); + + // Give the view a size. + updateView(); + // Set the model. + updateModel(); + + qCDebug(lcTumblerView) << "finished creating PathView"; + } + } else { + if (m_pathView) { + m_pathView->deleteLater(); + QQml_setParent_noEvent(m_pathView, nullptr); + m_pathView->setParentItem(nullptr); + m_pathView->setVisible(false); + m_pathView->setModel(QVariant()); + m_pathView = nullptr; + } + + if (!m_listView) { + qCDebug(lcTumblerView) << "creating ListView"; + + m_listView = new QQuickListView; + QQmlEngine::setContextForObject(m_listView, qmlContext(this)); + QQml_setParent_noEvent(m_listView, this); + m_listView->setParentItem(this); + m_listView->setSnapMode(QQuickListView::SnapToItem); + m_listView->setClip(true); + + // Give the view a size. + updateView(); + // Set the model. + updateModel(); + + // Set these after the model is set so that the currentItem animation + // happens instantly on startup/after switching models. If we set them too early, + // the view animates any potential currentIndex change over one second, + // which we don't want when the contentItem has just been created. + m_listView->setDelegate(m_delegate); + // Set this after setting the delegate to avoid unexpected currentIndex changes: QTBUG-79150 + m_listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange); + m_listView->setHighlightMoveDuration(1000); + + qCDebug(lcTumblerView) << "finished creating ListView"; + } + } +} + +// Called whenever the size or visibleItemCount changes. +void QQuickTumblerView::updateView() +{ + QQuickItem *theView = view(); + if (!theView) + return; + + theView->setSize(QSizeF(width(), height())); + + // Can be called in geometryChange when it might not have a parent item yet. + if (!m_tumbler) + return; + + // Set view-specific properties that have a dependency on the size, etc. + if (m_pathView) { + m_pathView->setPathItemCount(m_tumbler->visibleItemCount() + 1); + m_pathView->setDragMargin(width() / 2); + } else { + m_listView->setPreferredHighlightBegin(height() / 2 - (height() / m_tumbler->visibleItemCount() / 2)); + m_listView->setPreferredHighlightEnd(height() / 2 + (height() / m_tumbler->visibleItemCount() / 2)); + } +} + +void QQuickTumblerView::updateModel() +{ + if (m_pathView && !m_pathView->model().isValid() && m_model.isValid()) { + // QQuickPathView::setPathItemCount() resets the offset animation, + // so we just skip the animation while constructing the view. + const int oldHighlightMoveDuration = m_pathView->highlightMoveDuration(); + m_pathView->setHighlightMoveDuration(0); + + // Setting model can change the count, which can affect the wrap, which can cause + // the current view to be deleted before setModel() is finished, which causes a crash. + // Since QQuickTumbler can't know about QQuickTumblerView, we use its private API to + // inform it that it should delay setting wrap. + QQuickTumblerPrivate *tumblerPrivate = QQuickTumblerPrivate::get(m_tumbler); + tumblerPrivate->beginSetModel(); + m_pathView->setModel(m_model); + tumblerPrivate->endSetModel(); + + // The count-depends-on-wrap behavior could cause wrap to change after + // the call above, so we must check that we're still using a PathView. + if (m_pathView) + m_pathView->setHighlightMoveDuration(oldHighlightMoveDuration); + } else if (m_listView && !m_listView->model().isValid() && m_model.isValid()) { + const int currentIndex = m_tumbler->currentIndex(); + QQuickTumblerPrivate *tumblerPrivate = QQuickTumblerPrivate::get(m_tumbler); + + // setModel() causes QQuickTumblerPrivate::_q_onViewCountChanged() to + // be called, which calls QQuickTumbler::setCurrentIndex(), + // which results in QQuickItemViewPrivate::createHighlightItem() being + // called. When the highlight item is created, + // QQuickTumblerPrivate::itemChildAdded() is notified and + // QQuickTumblerPrivate::_q_updateItemHeights() is called, which causes + // a geometry change in the item and createHighlight() is called again. + // However, since the highlight item hadn't been assigned yet in the + // previous call frame, the "if (highlight) { delete highlight; }" + // check doesn't succeed, so the item is never deleted. + // + // To avoid this, we tell QQuickTumblerPrivate to ignore signals while + // setting the model, and manually call _q_onViewCountChanged() to + // ensure the correct sequence of calls happens (_q_onViewCountChanged() + // has to be within the ignoreSignals scope, because it also generates + // recursion otherwise). + tumblerPrivate->ignoreSignals = true; + m_listView->setModel(m_model); + m_listView->setCurrentIndex(currentIndex); + + tumblerPrivate->_q_onViewCountChanged(); + tumblerPrivate->ignoreSignals = false; + } +} + +void QQuickTumblerView::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) +{ + QQuickItem::geometryChange(newGeometry, oldGeometry); + updateView(); +} + +void QQuickTumblerView::componentComplete() +{ + QQuickItem::componentComplete(); + updateView(); +} + +void QQuickTumblerView::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) +{ + QQuickItem::itemChange(change, data); + + if (change == QQuickItem::ItemParentHasChanged && data.item) { + if (m_tumbler) + m_tumbler->disconnect(this); + + m_tumbler = qobject_cast(parentItem()); + + if (m_tumbler) { + // We assume that the parentChanged() signal of the tumbler will be emitted before its wrap property is set... + connect(m_tumbler, &QQuickTumbler::wrapChanged, this, &QQuickTumblerView::createView); + connect(m_tumbler, &QQuickTumbler::visibleItemCountChanged, this, &QQuickTumblerView::updateView); + } + } +} + +QQuickItem *QQuickTumblerView::view() +{ + if (!m_tumbler) + return nullptr; + + if (m_tumbler->wrap()) + return m_pathView; + + return m_listView; +} + +QT_END_NAMESPACE + +#include "moc_qquicktumblerview_p.cpp" diff --git a/src/quickcontrols2impl/qquicktumblerview_p.h b/src/quickcontrols2impl/qquicktumblerview_p.h new file mode 100644 index 00000000..44ce5fa6 --- /dev/null +++ b/src/quickcontrols2impl/qquicktumblerview_p.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QQUICKTUMBLERVIEW_P_H +#define QQUICKTUMBLERVIEW_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickListView; +class QQuickPath; +class QQuickPathView; + +class QQuickTumbler; + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickTumblerView : public QQuickItem +{ + Q_OBJECT + Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged) + Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged) + Q_PROPERTY(QQuickPath *path READ path WRITE setPath NOTIFY pathChanged) + QML_NAMED_ELEMENT(TumblerView) + QML_ADDED_IN_VERSION(2, 1) + +public: + QQuickTumblerView(QQuickItem *parent = nullptr); + + QVariant model() const; + void setModel(const QVariant &model); + + QQmlComponent *delegate() const; + void setDelegate(QQmlComponent *delegate); + + QQuickPath *path() const; + void setPath(QQuickPath *path); + +Q_SIGNALS: + void modelChanged(); + void delegateChanged(); + void pathChanged(); + +protected: + void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; + void componentComplete() override; + void itemChange(ItemChange change, const ItemChangeData &data) override; + +private: + QQuickItem *view(); + void createView(); + void updateView(); + void updateModel(); + + void wrapChange(); + + QQuickTumbler *m_tumbler = nullptr; + QVariant m_model; + QQmlComponent *m_delegate = nullptr; + QQuickPathView *m_pathView = nullptr; + QQuickListView *m_listView = nullptr; + QQuickPath *m_path = nullptr; +}; + +QT_END_NAMESPACE + +#endif // TUMBLERVIEW_H diff --git a/src/quickcontrols2impl/qtquickcontrols2foreign_p.h b/src/quickcontrols2impl/qtquickcontrols2foreign_p.h new file mode 100644 index 00000000..6285e43c --- /dev/null +++ b/src/quickcontrols2impl/qtquickcontrols2foreign_p.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 +#include +#include + +QT_BEGIN_NAMESPACE + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +// These are necessary in order to use C++ types in a file where only QtQuick.Controls has been imported. +// Control types like Button don't need this done for them, as each style module provides a Button type, +// and QtQuick.Controls is a sort of alias for the active style import. + +struct QQuickOverlayAttachedForeign +{ + Q_GADGET + QML_NAMED_ELEMENT(Overlay) + QML_FOREIGN(QQuickOverlay) + QML_UNCREATABLE("") + QML_ADDED_IN_VERSION(2, 3) +}; + +struct QQuickSplitHandleAttachedForeign +{ + Q_GADGET + QML_NAMED_ELEMENT(SplitHandle) + QML_FOREIGN(QQuickSplitHandleAttached) + QML_UNCREATABLE("") + QML_ADDED_IN_VERSION(2, 13) +}; + +QT_END_NAMESPACE diff --git a/src/quickcontrols2impl/qtquickcontrols2implglobal_p.h b/src/quickcontrols2impl/qtquickcontrols2implglobal_p.h new file mode 100644 index 00000000..9ff27a3b --- /dev/null +++ b/src/quickcontrols2impl/qtquickcontrols2implglobal_p.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 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 QTQUICKCONTROLS2IMPLGLOBAL_H +#define QTQUICKCONTROLS2IMPLGLOBAL_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include + +#ifndef QT_STATIC +# if defined(QT_BUILD_QUICKCONTROLS2IMPL_LIB) +# define Q_QUICKCONTROLS2IMPL_EXPORT Q_DECL_EXPORT +# else +# define Q_QUICKCONTROLS2IMPL_EXPORT Q_DECL_IMPORT +# endif +#else +# define Q_QUICKCONTROLS2IMPL_EXPORT +#endif + +#define Q_QUICKCONTROLS2_PRIVATE_EXPORT Q_QUICKCONTROLS2IMPL_EXPORT + +Q_QUICKCONTROLS2_PRIVATE_EXPORT void qml_register_types_QtQuick_Controls_impl(); + +#endif // QTQUICKCONTROLS2IMPLGLOBAL_H diff --git a/src/quickcontrols2impl/quickcontrols2impl.pro b/src/quickcontrols2impl/quickcontrols2impl.pro new file mode 100644 index 00000000..7a7d2862 --- /dev/null +++ b/src/quickcontrols2impl/quickcontrols2impl.pro @@ -0,0 +1,54 @@ +TARGET = QtQuickControls2Impl +MODULE = quickcontrols2impl + +QT += quick +QT_PRIVATE += core-private gui-private qml-private quick-private quicktemplates2-private + +DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII + +HEADERS += \ + $$PWD/qtquickcontrols2implglobal_p.h \ + $$PWD/qquickanimatednode_p.h \ + $$PWD/qquickattachedobject_p.h \ + $$PWD/qquickchecklabel_p.h \ + $$PWD/qquickclippedtext_p.h \ + $$PWD/qquickcolor_p.h \ + $$PWD/qquickcolorimage_p.h \ + $$PWD/qquickiconimage_p.h \ + $$PWD/qquickiconimage_p_p.h \ + $$PWD/qquickiconlabel_p.h \ + $$PWD/qquickiconlabel_p_p.h \ + $$PWD/qquickitemgroup_p.h \ + $$PWD/qquickmnemoniclabel_p.h \ + $$PWD/qquickpaddedrectangle_p.h \ + $$PWD/qquickplaceholdertext_p.h \ + $$PWD/qtquickcontrols2foreign_p.h + +SOURCES += \ + $$PWD/qquickanimatednode.cpp \ + $$PWD/qquickattachedobject.cpp \ + $$PWD/qquickchecklabel.cpp \ + $$PWD/qquickclippedtext.cpp \ + $$PWD/qquickcolor.cpp \ + $$PWD/qquickcolorimage.cpp \ + $$PWD/qquickiconimage.cpp \ + $$PWD/qquickiconlabel.cpp \ + $$PWD/qquickitemgroup.cpp \ + $$PWD/qquickmnemoniclabel.cpp \ + $$PWD/qquickpaddedrectangle.cpp \ + $$PWD/qquickplaceholdertext.cpp + +qtConfig(quick-listview):qtConfig(quick-pathview) { + HEADERS += \ + $$PWD/qquicktumblerview_p.h + SOURCES += \ + $$PWD/qquicktumblerview.cpp +} + +QMLTYPES_FILENAME = plugins.qmltypes +QMLTYPES_INSTALL_DIR = $$[QT_INSTALL_QML]/QtQuick/Controls/impl +QML_IMPORT_NAME = QtQuick.Controls.impl +QML_IMPORT_VERSION = 2.15 +CONFIG += qmltypes install_qmltypes install_metatypes + +load(qt_module) -- cgit v1.2.3