From 6c2a24a4f2695ae5565b9a5a42b04c9b68833866 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 9 May 2017 16:44:08 +0200 Subject: Make QQuickIcon a value type QQuickIcon no longer inherits QObject, but becomes a light-weight implicitly shared Q_GADGET-type, that is passed by value the same way fonts and colors are. Before: SUB: OS: Fedora 25 (Workstation Edition) SUB: QPA: xcb SUB: GL_VENDOR: Intel Open Source Technology Center SUB: GL_RENDERER: Mesa DRI Intel(R) Haswell Desktop SUB: GL_VERSION: 3.0 Mesa 13.0.4 SUB: running: benchmarks/auto/creation/controls/delegates_buttoncontrol2.qml SUB: 110 frames SUB: 109 frames SUB: 109 frames SUB: 109 frames SUB: 109 frames SUB: Average: SUB: 109.2 frames; using samples; MedianAll=109; StdDev=0.447214, CoV=0.00409536 After: [...] SUB: running: benchmarks/auto/creation/controls/delegates_buttoncontrol2.qml SUB: 123 frames SUB: 124 frames SUB: 124 frames SUB: 122 frames SUB: 125 frames SUB: Average: SUB: 123.6 frames; using samples; MedianAll=124; StdDev=1.14018, CoV=0.00922472 Change-Id: I604532204fb94fc0726d0c9b8b6097f9ebc265e8 Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquickabstractbutton.cpp | 81 +++++++++++-------------- src/quicktemplates2/qquickabstractbutton_p.h | 8 ++- src/quicktemplates2/qquickabstractbutton_p_p.h | 3 +- src/quicktemplates2/qquickaction.cpp | 20 +++--- src/quicktemplates2/qquickaction_p.h | 8 ++- src/quicktemplates2/qquickaction_p_p.h | 3 +- src/quicktemplates2/qquickicon.cpp | 75 ++++++++++++----------- src/quicktemplates2/qquickicon_p.h | 38 ++++++------ src/quicktemplates2/qtquicktemplates2global_p.h | 1 + 9 files changed, 118 insertions(+), 119 deletions(-) (limited to 'src/quicktemplates2') diff --git a/src/quicktemplates2/qquickabstractbutton.cpp b/src/quicktemplates2/qquickabstractbutton.cpp index 96faf2d5..225e20d4 100644 --- a/src/quicktemplates2/qquickabstractbutton.cpp +++ b/src/quicktemplates2/qquickabstractbutton.cpp @@ -37,7 +37,6 @@ #include "qquickabstractbutton_p.h" #include "qquickabstractbutton_p_p.h" #include "qquickbuttongroup_p.h" -#include "qquickicon_p.h" #include "qquickaction_p.h" #include "qquickaction_p_p.h" @@ -161,7 +160,6 @@ QQuickAbstractButtonPrivate::QQuickAbstractButtonPrivate() pressButtons(Qt::NoButton), indicator(nullptr), group(nullptr), - icon(nullptr), display(QQuickAbstractButton::TextBesideIcon), action(nullptr) { @@ -637,16 +635,22 @@ void QQuickAbstractButton::setIndicator(QQuickItem *indicator) \sa {Control::}{contentItem} */ -QQuickIcon *QQuickAbstractButton::icon() const +QQuickIcon QQuickAbstractButton::icon() const { - QQuickAbstractButtonPrivate *d = const_cast(d_func()); - if (!d->icon) { - d->icon = new QQuickIcon; - QQml_setParent_noEvent(d->icon, const_cast(this)); - } + Q_D(const QQuickAbstractButton); return d->icon; } +void QQuickAbstractButton::setIcon(const QQuickIcon &icon) +{ + Q_D(QQuickAbstractButton); + if (d->icon == icon) + return; + + d->icon = icon; + emit iconChanged(); +} + /*! \since QtQuick.Controls 2.3 \qmlproperty enumeration QtQuick.Controls::AbstractButton::display @@ -704,18 +708,10 @@ void QQuickAbstractButton::setAction(QQuickAction *action) QObjectPrivate::disconnect(oldAction, &QQuickAction::triggered, d, &QQuickAbstractButtonPrivate::click); disconnect(oldAction, &QQuickAction::textChanged, this, &QQuickAbstractButton::setText); + disconnect(oldAction, &QQuickAction::iconChanged, this, &QQuickAbstractButton::setIcon); disconnect(oldAction, &QQuickAction::checkedChanged, this, &QQuickAbstractButton::setChecked); disconnect(oldAction, &QQuickAction::checkableChanged, this, &QQuickAbstractButton::setCheckable); disconnect(oldAction, &QQuickAction::enabledChanged, this, &QQuickItem::setEnabled); - - QQuickIcon *actionIcon = QQuickActionPrivate::get(oldAction)->icon; - if (actionIcon && d->icon) { - disconnect(actionIcon, &QQuickIcon::nameChanged, d->icon, &QQuickIcon::setName); - disconnect(actionIcon, &QQuickIcon::sourceChanged, d->icon, &QQuickIcon::setSource); - disconnect(actionIcon, &QQuickIcon::widthChanged, d->icon, &QQuickIcon::setWidth); - disconnect(actionIcon, &QQuickIcon::heightChanged, d->icon, &QQuickIcon::setHeight); - disconnect(actionIcon, &QQuickIcon::colorChanged, d->icon, &QQuickIcon::setColor); - } } if (action) { @@ -723,39 +719,32 @@ void QQuickAbstractButton::setAction(QQuickAction *action) QObjectPrivate::connect(action, &QQuickAction::triggered, d, &QQuickAbstractButtonPrivate::click); connect(action, &QQuickAction::textChanged, this, &QQuickAbstractButton::setText); + connect(action, &QQuickAction::iconChanged, this, &QQuickAbstractButton::setIcon); connect(action, &QQuickAction::checkedChanged, this, &QQuickAbstractButton::setChecked); connect(action, &QQuickAction::checkableChanged, this, &QQuickAbstractButton::setCheckable); connect(action, &QQuickAction::enabledChanged, this, &QQuickItem::setEnabled); - QQuickIcon *actionIcon = QQuickActionPrivate::get(action)->icon; - if (actionIcon) { - QQuickIcon *buttonIcon = icon(); - connect(actionIcon, &QQuickIcon::nameChanged, buttonIcon, &QQuickIcon::setName); - connect(actionIcon, &QQuickIcon::sourceChanged, buttonIcon, &QQuickIcon::setSource); - connect(actionIcon, &QQuickIcon::widthChanged, buttonIcon, &QQuickIcon::setWidth); - connect(actionIcon, &QQuickIcon::heightChanged, buttonIcon, &QQuickIcon::setHeight); - connect(actionIcon, &QQuickIcon::colorChanged, buttonIcon, &QQuickIcon::setColor); - - QString name = actionIcon->name(); - if (!name.isEmpty()) - buttonIcon->setName(name); - - QUrl source = actionIcon->source(); - if (!source.isEmpty()) - buttonIcon->setSource(source); - - int width = actionIcon->width(); - if (width > 0) - buttonIcon->setWidth(width); - - int height = actionIcon->height(); - if (height) - buttonIcon->setHeight(height); - - QColor color = actionIcon->color(); - if (color != Qt::transparent) - buttonIcon->setColor(color); - } + QQuickIcon actionIcon = action->icon(); + + QString name = actionIcon.name(); + if (!name.isEmpty()) + d->icon.setName(name); + + QUrl source = actionIcon.source(); + if (!source.isEmpty()) + d->icon.setSource(source); + + int width = actionIcon.width(); + if (width > 0) + d->icon.setWidth(width); + + int height = actionIcon.height(); + if (height) + d->icon.setHeight(height); + + QColor color = actionIcon.color(); + if (color != Qt::transparent) + d->icon.setColor(color); setText(action->text()); setChecked(action->isChecked()); diff --git a/src/quicktemplates2/qquickabstractbutton_p.h b/src/quicktemplates2/qquickabstractbutton_p.h index c6a50665..9c372414 100644 --- a/src/quicktemplates2/qquickabstractbutton_p.h +++ b/src/quicktemplates2/qquickabstractbutton_p.h @@ -49,10 +49,10 @@ // #include +#include QT_BEGIN_NAMESPACE -class QQuickIcon; class QQuickAction; class QQuickAbstractButtonPrivate; @@ -66,7 +66,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickAbstractButton : public QQuickContr Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable NOTIFY checkableChanged FINAL) Q_PROPERTY(bool autoExclusive READ autoExclusive WRITE setAutoExclusive NOTIFY autoExclusiveChanged FINAL) Q_PROPERTY(QQuickItem *indicator READ indicator WRITE setIndicator NOTIFY indicatorChanged FINAL) - Q_PROPERTY(QQuickIcon *icon READ icon CONSTANT FINAL REVISION 3) + Q_PROPERTY(QQuickIcon icon READ icon WRITE setIcon NOTIFY iconChanged FINAL REVISION 3) Q_PROPERTY(Display display READ display WRITE setDisplay NOTIFY displayChanged REVISION 3) Q_PROPERTY(QQuickAction *action READ action WRITE setAction NOTIFY actionChanged FINAL REVISION 3) @@ -99,7 +99,8 @@ public: QQuickItem *indicator() const; void setIndicator(QQuickItem *indicator); - QQuickIcon *icon() const; + QQuickIcon icon() const; + void setIcon(const QQuickIcon &icon); enum Display { IconOnly, @@ -133,6 +134,7 @@ Q_SIGNALS: void checkableChanged(); void autoExclusiveChanged(); void indicatorChanged(); + Q_REVISION(3) void iconChanged(); Q_REVISION(3) void displayChanged(); Q_REVISION(3) void actionChanged(); diff --git a/src/quicktemplates2/qquickabstractbutton_p_p.h b/src/quicktemplates2/qquickabstractbutton_p_p.h index 61a546d1..f1fef5e5 100644 --- a/src/quicktemplates2/qquickabstractbutton_p_p.h +++ b/src/quicktemplates2/qquickabstractbutton_p_p.h @@ -55,7 +55,6 @@ QT_BEGIN_NAMESPACE class QQuickAction; class QQuickButtonGroup; -class QQuickIcon; class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickAbstractButtonPrivate : public QQuickControlPrivate { @@ -102,11 +101,11 @@ public: int holdTimer; int delayTimer; int repeatTimer; + QQuickIcon icon; QPointF pressPoint; Qt::MouseButtons pressButtons; QQuickItem *indicator; QQuickButtonGroup *group; - QQuickIcon *icon; QQuickAbstractButton::Display display; QPointer action; }; diff --git a/src/quicktemplates2/qquickaction.cpp b/src/quicktemplates2/qquickaction.cpp index e6b1cedb..fb6d7c3e 100644 --- a/src/quicktemplates2/qquickaction.cpp +++ b/src/quicktemplates2/qquickaction.cpp @@ -38,7 +38,6 @@ #include "qquickaction_p_p.h" #include "qquickactiongroup_p.h" #include "qquickshortcutcontext_p_p.h" -#include "qquickicon_p.h" #include #include @@ -176,7 +175,6 @@ QQuickActionPrivate::QQuickActionPrivate() enabled(true), checked(false), checkable(false), - icon(nullptr), defaultShortcutEntry(nullptr), group(nullptr) { @@ -381,16 +379,22 @@ void QQuickAction::setText(const QString &text) \include qquickicon.qdocinc grouped-properties */ -QQuickIcon *QQuickAction::icon() const +QQuickIcon QQuickAction::icon() const { - QQuickActionPrivate *d = const_cast(d_func()); - if (!d->icon) { - d->icon = new QQuickIcon; - QQml_setParent_noEvent(d->icon, const_cast(this)); - } + Q_D(const QQuickAction); return d->icon; } +void QQuickAction::setIcon(const QQuickIcon &icon) +{ + Q_D(QQuickAction); + if (d->icon == icon) + return; + + d->icon = icon; + emit iconChanged(icon); +} + /*! \qmlproperty bool QtQuick.Controls::Action::enabled diff --git a/src/quicktemplates2/qquickaction_p.h b/src/quicktemplates2/qquickaction_p.h index d47b847e..ce989bed 100644 --- a/src/quicktemplates2/qquickaction_p.h +++ b/src/quicktemplates2/qquickaction_p.h @@ -49,7 +49,7 @@ // #include - +#include #include #include @@ -62,7 +62,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickAction : public QObject { Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged FINAL) - Q_PROPERTY(QQuickIcon *icon READ icon CONSTANT FINAL) + Q_PROPERTY(QQuickIcon icon READ icon WRITE setIcon NOTIFY iconChanged FINAL) Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged RESET resetEnabled FINAL) Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY checkedChanged FINAL) Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable NOTIFY checkableChanged FINAL) @@ -75,7 +75,8 @@ public: QString text() const; void setText(const QString &text); - QQuickIcon *icon() const; + QQuickIcon icon() const; + void setIcon(const QQuickIcon &icon); bool isEnabled() const; void setEnabled(bool enabled); @@ -96,6 +97,7 @@ public Q_SLOTS: Q_SIGNALS: void textChanged(const QString &text); + void iconChanged(const QQuickIcon &icon); void enabledChanged(bool enabled); void checkedChanged(bool checked); void checkableChanged(bool checkable); diff --git a/src/quicktemplates2/qquickaction_p_p.h b/src/quicktemplates2/qquickaction_p_p.h index 85da6b03..08c442ed 100644 --- a/src/quicktemplates2/qquickaction_p_p.h +++ b/src/quicktemplates2/qquickaction_p_p.h @@ -56,7 +56,6 @@ QT_BEGIN_NAMESPACE -class QQuickIcon; class QShortcutEvent; class QQuickActionGroup; @@ -115,7 +114,7 @@ public: bool checked; bool checkable; QString text; - QQuickIcon *icon; + QQuickIcon icon; QVariant vshortcut; QKeySequence keySequence; ShortcutEntry *defaultShortcutEntry; diff --git a/src/quicktemplates2/qquickicon.cpp b/src/quicktemplates2/qquickicon.cpp index 38d0b2af..0b0127d3 100644 --- a/src/quicktemplates2/qquickicon.cpp +++ b/src/quicktemplates2/qquickicon.cpp @@ -34,15 +34,12 @@ ** ****************************************************************************/ -#include #include "qquickicon_p.h" QT_BEGIN_NAMESPACE -class QQuickIconPrivate : public QObjectPrivate +class QQuickIconPrivate : public QSharedData { - Q_DECLARE_PUBLIC(QQuickIcon) - public: QQuickIconPrivate() : width(0), @@ -58,94 +55,98 @@ public: QColor color; }; -QQuickIcon::QQuickIcon(QObject *parent) - : QObject(*(new QQuickIconPrivate), parent) +QQuickIcon::QQuickIcon() + : d(new QQuickIconPrivate) +{ +} + +QQuickIcon::QQuickIcon(const QQuickIcon &other) + : d(other.d) +{ +} + +QQuickIcon::~QQuickIcon() +{ +} + +QQuickIcon &QQuickIcon::operator=(const QQuickIcon &other) +{ + d = other.d; + return *this; +} + +bool QQuickIcon::operator==(const QQuickIcon &other) const +{ + return d == other.d || (d->name == other.d->name + && d->source == other.d->source + && d->width == other.d->width + && d->height == other.d->height + && d->color == other.d->color); +} + +bool QQuickIcon::operator!=(const QQuickIcon &other) const +{ + return !(*this == other); +} + +bool QQuickIcon::isEmpty() const { + return d->name.isEmpty() && d->source.isEmpty(); } QString QQuickIcon::name() const { - Q_D(const QQuickIcon); return d->name; } void QQuickIcon::setName(const QString &name) { - Q_D(QQuickIcon); - if (name == d->name) - return; - d->name = name; - emit nameChanged(name); } QUrl QQuickIcon::source() const { - Q_D(const QQuickIcon); return d->source; } void QQuickIcon::setSource(const QUrl &source) { - Q_D(QQuickIcon); - if (source == d->source) - return; - d->source = source; - emit sourceChanged(source); } int QQuickIcon::width() const { - Q_D(const QQuickIcon); return d->width; } void QQuickIcon::setWidth(int width) { - Q_D(QQuickIcon); - if (width == d->width) - return; - d->width = width; - emit widthChanged(width); } int QQuickIcon::height() const { - Q_D(const QQuickIcon); return d->height; } void QQuickIcon::setHeight(int height) { - Q_D(QQuickIcon); - if (height == d->height) - return; - d->height = height; - emit heightChanged(height); } QColor QQuickIcon::color() const { - Q_D(const QQuickIcon); return d->color; } void QQuickIcon::setColor(const QColor &color) { - Q_D(QQuickIcon); - if (color == d->color) - return; - d->color = color; - emit colorChanged(color); } void QQuickIcon::resetColor() { - setColor(Qt::transparent); + d->color = Qt::transparent; } QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquickicon_p.h b/src/quicktemplates2/qquickicon_p.h index 4b1557a0..6e28f2a9 100644 --- a/src/quicktemplates2/qquickicon_p.h +++ b/src/quicktemplates2/qquickicon_p.h @@ -49,7 +49,9 @@ // #include -#include +#include +#include +#include #include #include @@ -57,17 +59,25 @@ QT_BEGIN_NAMESPACE class QQuickIconPrivate; -class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickIcon : public QObject +class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickIcon { - Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) - Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged FINAL) - Q_PROPERTY(int width READ width WRITE setWidth NOTIFY widthChanged FINAL) - Q_PROPERTY(int height READ height WRITE setHeight NOTIFY heightChanged FINAL) - Q_PROPERTY(QColor color READ color WRITE setColor RESET resetColor NOTIFY colorChanged FINAL) + Q_GADGET + Q_PROPERTY(QString name READ name WRITE setName FINAL) + Q_PROPERTY(QUrl source READ source WRITE setSource FINAL) + Q_PROPERTY(int width READ width WRITE setWidth FINAL) + Q_PROPERTY(int height READ height WRITE setHeight FINAL) + Q_PROPERTY(QColor color READ color WRITE setColor RESET resetColor FINAL) public: - explicit QQuickIcon(QObject *parent = nullptr); + QQuickIcon(); + QQuickIcon(const QQuickIcon &other); + ~QQuickIcon(); + + QQuickIcon& operator=(const QQuickIcon &other); + bool operator==(const QQuickIcon &other) const; + bool operator!=(const QQuickIcon &other) const; + + bool isEmpty() const; QString name() const; void setName(const QString &name); @@ -85,16 +95,8 @@ public: void setColor(const QColor &color); void resetColor(); -Q_SIGNALS: - void nameChanged(const QString &name); - void sourceChanged(const QUrl &source); - void widthChanged(int width); - void heightChanged(int height); - void colorChanged(const QColor &color); - private: - Q_DISABLE_COPY(QQuickIcon) - Q_DECLARE_PRIVATE(QQuickIcon) + QSharedDataPointer d; }; QT_END_NAMESPACE diff --git a/src/quicktemplates2/qtquicktemplates2global_p.h b/src/quicktemplates2/qtquicktemplates2global_p.h index b7f54bb6..e5ee3f2e 100644 --- a/src/quicktemplates2/qtquicktemplates2global_p.h +++ b/src/quicktemplates2/qtquicktemplates2global_p.h @@ -49,6 +49,7 @@ // #include +#include #include QT_BEGIN_NAMESPACE -- cgit v1.2.3