diff --git a/src/scriptengines/qml/plasmoid/appletquickitem.cpp b/src/scriptengines/qml/plasmoid/appletquickitem.cpp index 1832cae1c..5791afb80 100644 --- a/src/scriptengines/qml/plasmoid/appletquickitem.cpp +++ b/src/scriptengines/qml/plasmoid/appletquickitem.cpp @@ -39,19 +39,234 @@ QHash AppletQuickItemPrivate::s_rootObjects = QHash(); -AppletQuickItemPrivate::AppletQuickItemPrivate(Plasma::Applet *a) - : switchWidth(-1), +AppletQuickItemPrivate::AppletQuickItemPrivate(Plasma::Applet *a, AppletQuickItem *item) + : q(item), + switchWidth(-1), switchHeight(-1), applet(a), expanded(false) { } +void AppletQuickItemPrivate::connectLayoutAttached(QObject *item) +{ + QObject *layout = 0; + + //Extract the representation's Layout, if any + //No Item? + if (!item) { + return; + } + + //Search a child that has the needed Layout properties + //HACK: here we are not type safe, but is the only way to access to a pointer of Layout + foreach (QObject *child, item->children()) { + //find for the needed property of Layout: minimum/maximum/preferred sizes and fillWidth/fillHeight + if (child->property("minimumWidth").isValid() && child->property("minimumHeight").isValid() && + child->property("preferredWidth").isValid() && child->property("preferredHeight").isValid() && + child->property("maximumWidth").isValid() && child->property("maximumHeight").isValid() && + child->property("fillWidth").isValid() && child->property("fillHeight").isValid() + ) { + layout = child; + } + } + + if (!layout) { + return; + } + + //propagate all the size hints + propagateSizeHint("minimumWidth"); + propagateSizeHint("minimumHeight"); + propagateSizeHint("preferredWidth"); + propagateSizeHint("preferredHeight"); + propagateSizeHint("maximumWidth"); + propagateSizeHint("maximumHeight"); + propagateSizeHint("fillWidth"); + propagateSizeHint("fillHeight"); + + //HACK: check the Layout properties we wrote + QQmlProperty p(q, "Layout.minimumWidth", QtQml::qmlContext(qmlObject->rootObject())); + + QObject *ownLayout = 0; + + foreach (QObject *child, q->children()) { + //find for the needed property of Layout: minimum/maximum/preferred sizes and fillWidth/fillHeight + if (child->property("minimumWidth").isValid() && child->property("minimumHeight").isValid() && + child->property("preferredWidth").isValid() && child->property("preferredHeight").isValid() && + child->property("maximumWidth").isValid() && child->property("maximumHeight").isValid() && + child->property("fillWidth").isValid() && child->property("fillHeight").isValid() + ) { + ownLayout = child; + } + } + + //this should never happen, since we ask to create it if doesn't exists + if (!ownLayout) { + return; + } + + //if the representation didn't change, don't do anything + if (representationLayout.data() == layout) { + return; + } + + if (representationLayout) { + QObject::disconnect(representationLayout.data(), 0, q, 0); + } + + //Here we can't use the new connect syntax because we can't link against QtQuick layouts + QObject::connect(layout, SIGNAL(minimumWidthChanged()), + q, SLOT(minimumWidthChanged())); + QObject::connect(layout, SIGNAL(minimumHeightChanged()), + q, SLOT(minimumHeightChanged())); + + QObject::connect(layout, SIGNAL(preferredWidthChanged()), + q, SLOT(preferredWidthChanged())); + QObject::connect(layout, SIGNAL(preferredHeightChanged()), + q, SLOT(preferredHeightChanged())); + + QObject::connect(layout, SIGNAL(maximumWidthChanged()), + q, SLOT(maximumWidthChanged())); + QObject::connect(layout, SIGNAL(maximumHeightChanged()), + q, SLOT(maximumHeightChanged())); + + QObject::connect(layout, SIGNAL(fillWidthChanged()), + q, SLOT(fillWidthChanged())); + QObject::connect(layout, SIGNAL(fillHeightChanged()), + q, SLOT(fillHeightChanged())); + + representationLayout = layout; + AppletQuickItemPrivate::ownLayout = ownLayout; + + propagateSizeHint("minimumWidth"); + propagateSizeHint("minimumHeight"); + propagateSizeHint("preferredWidth"); + propagateSizeHint("preferredHeight"); + propagateSizeHint("maximumWidth"); + propagateSizeHint("maximumHeight"); + propagateSizeHint("fillWidth"); + propagateSizeHint("fillHeight"); +} + +void AppletQuickItemPrivate::propagateSizeHint(const QByteArray &layoutProperty) +{ + if (ownLayout && representationLayout) { + ownLayout.data()->setProperty(layoutProperty, representationLayout.data()->property(layoutProperty)); + } +} + +QObject *AppletQuickItemPrivate::createCompactRepresentationItem() +{ + if (!compactRepresentation) { + return 0; + } + + if (compactRepresentationItem) { + return compactRepresentationItem.data(); + } + + compactRepresentationItem = qmlObject->createObjectFromComponent(compactRepresentation.data(), QtQml::qmlContext(qmlObject->rootObject())); + + emit q->compactRepresentationItemChanged(compactRepresentationItem.data()); + + return compactRepresentationItem.data(); +} + +QObject *AppletQuickItemPrivate::createFullRepresentationItem() +{ + if (fullRepresentationItem) { + return fullRepresentationItem.data(); + } + + if (fullRepresentation) { + fullRepresentationItem = qmlObject->createObjectFromComponent(fullRepresentation.data(), QtQml::qmlContext(qmlObject->rootObject())); + } else { + fullRepresentation = qmlObject->mainComponent(); + fullRepresentationItem = qmlObject->rootObject(); + emit q->fullRepresentationChanged(fullRepresentation.data()); + } + + QQuickItem *graphicsObj = qobject_cast(fullRepresentationItem.data()); + QObject::connect (graphicsObj, &QQuickItem::widthChanged, [=]() { + fullRepresentationResizeTimer.start(); + }); + QObject::connect (graphicsObj, &QQuickItem::heightChanged, [=]() { + fullRepresentationResizeTimer.start(); + }); + + emit q->fullRepresentationItemChanged(fullRepresentationItem.data()); + + return fullRepresentationItem.data(); +} + +QObject *AppletQuickItemPrivate::createCompactRepresentationExpanderItem() +{ + if (!compactRepresentationExpander) { + return 0; + } + + if (compactRepresentationExpanderItem) { + return compactRepresentationExpanderItem.data(); + } + + compactRepresentationExpanderItem = qmlObject->createObjectFromComponent(compactRepresentationExpander.data(), QtQml::qmlContext(qmlObject->rootObject())); + + + compactRepresentationExpanderItem.data()->setProperty("compactRepresentation", QVariant::fromValue(createCompactRepresentationItem())); + compactRepresentationExpanderItem.data()->setProperty("fullRepresentation", QVariant::fromValue(createFullRepresentationItem())); + + emit q->compactRepresentationExpanderItemChanged(compactRepresentationExpanderItem.data()); + + return compactRepresentationExpanderItem.data(); +} + +void AppletQuickItemPrivate::minimumWidthChanged() +{ + propagateSizeHint("minimumWidth"); +} + +void AppletQuickItemPrivate::minimumHeightChanged() +{ + propagateSizeHint("minimumHeight"); +} + +void AppletQuickItemPrivate::preferredWidthChanged() +{ + propagateSizeHint("preferredWidth"); +} + +void AppletQuickItemPrivate::preferredHeightChanged() +{ + propagateSizeHint("preferredHeight"); +} + +void AppletQuickItemPrivate::maximumWidthChanged() +{ + propagateSizeHint("maximumWidth"); +} + +void AppletQuickItemPrivate::maximumHeightChanged() +{ + propagateSizeHint("maximumHeight"); +} + +void AppletQuickItemPrivate::fillWidthChanged() +{ + propagateSizeHint("fillWidth"); +} + +void AppletQuickItemPrivate::fillHeightChanged() +{ + propagateSizeHint("fillHeight"); +} + + AppletQuickItem::AppletQuickItem(Plasma::Applet *applet, QQuickItem *parent) : QQuickItem(parent), - d(new AppletQuickItemPrivate(applet)) + d(new AppletQuickItemPrivate(applet, this)) { if (d->applet) { d->appletPackage = d->applet->package(); @@ -286,8 +501,8 @@ void AppletQuickItem::setExpanded(bool expanded) return; } - createFullRepresentationItem(); - createCompactRepresentationExpanderItem(); + d->createFullRepresentationItem(); + d->createCompactRepresentationExpanderItem(); d->expanded = expanded; emit expandedChanged(expanded); @@ -316,181 +531,6 @@ QObject *AppletQuickItem::compactRepresentationExpanderItem() return d->compactRepresentationExpanderItem.data(); } - - -QObject *AppletQuickItem::createCompactRepresentationItem() -{ - if (!d->compactRepresentation) { - return 0; - } - - if (d->compactRepresentationItem) { - return d->compactRepresentationItem.data(); - } - - d->compactRepresentationItem = d->qmlObject->createObjectFromComponent(d->compactRepresentation.data(), QtQml::qmlContext(d->qmlObject->rootObject())); - - emit compactRepresentationItemChanged(d->compactRepresentationItem.data()); - - return d->compactRepresentationItem.data(); -} - -QObject *AppletQuickItem::createFullRepresentationItem() -{ - if (d->fullRepresentationItem) { - return d->fullRepresentationItem.data(); - } - - if (d->fullRepresentation) { - d->fullRepresentationItem = d->qmlObject->createObjectFromComponent(d->fullRepresentation.data(), QtQml::qmlContext(d->qmlObject->rootObject())); - } else { - d->fullRepresentation = d->qmlObject->mainComponent(); - d->fullRepresentationItem = d->qmlObject->rootObject(); - emit fullRepresentationChanged(d->fullRepresentation.data()); - } - - QQuickItem *graphicsObj = qobject_cast(d->fullRepresentationItem.data()); - connect (graphicsObj, &QQuickItem::widthChanged, [=]() { - d->fullRepresentationResizeTimer.start(); - }); - connect (graphicsObj, &QQuickItem::heightChanged, [=]() { - d->fullRepresentationResizeTimer.start(); - }); - - emit fullRepresentationItemChanged(d->fullRepresentationItem.data()); - - return d->fullRepresentationItem.data(); -} - -QObject *AppletQuickItem::createCompactRepresentationExpanderItem() -{ - if (!d->compactRepresentationExpander) { - return 0; - } - - if (d->compactRepresentationExpanderItem) { - return d->compactRepresentationExpanderItem.data(); - } - - d->compactRepresentationExpanderItem = d->qmlObject->createObjectFromComponent(d->compactRepresentationExpander.data(), QtQml::qmlContext(d->qmlObject->rootObject())); - - - d->compactRepresentationExpanderItem.data()->setProperty("compactRepresentation", QVariant::fromValue(createCompactRepresentationItem())); - d->compactRepresentationExpanderItem.data()->setProperty("fullRepresentation", QVariant::fromValue(createFullRepresentationItem())); - - emit compactRepresentationExpanderItemChanged(d->compactRepresentationExpanderItem.data()); - - return d->compactRepresentationExpanderItem.data(); -} - -void AppletQuickItem::connectLayoutAttached(QObject *item) -{ - QObject *layout = 0; - - //Extract the representation's Layout, if any - //No Item? - if (!item) { - return; - } - - //Search a child that has the needed Layout properties - //HACK: here we are not type safe, but is the only way to access to a pointer of Layout - foreach (QObject *child, item->children()) { - //find for the needed property of Layout: minimum/maximum/preferred sizes and fillWidth/fillHeight - if (child->property("minimumWidth").isValid() && child->property("minimumHeight").isValid() && - child->property("preferredWidth").isValid() && child->property("preferredHeight").isValid() && - child->property("maximumWidth").isValid() && child->property("maximumHeight").isValid() && - child->property("fillWidth").isValid() && child->property("fillHeight").isValid() - ) { - layout = child; - } - } - - if (!layout) { - return; - } - - //propagate all the size hints - propagateSizeHint("minimumWidth"); - propagateSizeHint("minimumHeight"); - propagateSizeHint("preferredWidth"); - propagateSizeHint("preferredHeight"); - propagateSizeHint("maximumWidth"); - propagateSizeHint("maximumHeight"); - propagateSizeHint("fillWidth"); - propagateSizeHint("fillHeight"); - - //HACK: check the Layout properties we wrote - QQmlProperty p(this, "Layout.minimumWidth", QtQml::qmlContext(d->qmlObject->rootObject())); - - QObject *ownLayout = 0; - - foreach (QObject *child, children()) { - //find for the needed property of Layout: minimum/maximum/preferred sizes and fillWidth/fillHeight - if (child->property("minimumWidth").isValid() && child->property("minimumHeight").isValid() && - child->property("preferredWidth").isValid() && child->property("preferredHeight").isValid() && - child->property("maximumWidth").isValid() && child->property("maximumHeight").isValid() && - child->property("fillWidth").isValid() && child->property("fillHeight").isValid() - ) { - ownLayout = child; - } - } - - //this should never happen, since we ask to create it if doesn't exists - if (!ownLayout) { - return; - } - - //if the representation didn't change, don't do anything - if (d->representationLayout.data() == layout) { - return; - } - - if (d->representationLayout) { - disconnect(d->representationLayout.data(), 0, this, 0); - } - - //Here we can't use the new connect syntax because we can't link against QtQuick layouts - connect(layout, SIGNAL(minimumWidthChanged()), - this, SLOT(minimumWidthChanged())); - connect(layout, SIGNAL(minimumHeightChanged()), - this, SLOT(minimumHeightChanged())); - - connect(layout, SIGNAL(preferredWidthChanged()), - this, SLOT(preferredWidthChanged())); - connect(layout, SIGNAL(preferredHeightChanged()), - this, SLOT(preferredHeightChanged())); - - connect(layout, SIGNAL(maximumWidthChanged()), - this, SLOT(maximumWidthChanged())); - connect(layout, SIGNAL(maximumHeightChanged()), - this, SLOT(maximumHeightChanged())); - - connect(layout, SIGNAL(fillWidthChanged()), - this, SLOT(fillWidthChanged())); - connect(layout, SIGNAL(fillHeightChanged()), - this, SLOT(fillHeightChanged())); - - d->representationLayout = layout; - d->ownLayout = ownLayout; - - propagateSizeHint("minimumWidth"); - propagateSizeHint("minimumHeight"); - propagateSizeHint("preferredWidth"); - propagateSizeHint("preferredHeight"); - propagateSizeHint("maximumWidth"); - propagateSizeHint("maximumHeight"); - propagateSizeHint("fillWidth"); - propagateSizeHint("fillHeight"); -} - -void AppletQuickItem::propagateSizeHint(const QByteArray &layoutProperty) -{ - if (d->ownLayout && d->representationLayout) { - d->ownLayout.data()->setProperty(layoutProperty, d->representationLayout.data()->property(layoutProperty)); - } -} - void AppletQuickItem::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) { Q_UNUSED(oldGeometry) @@ -549,7 +589,7 @@ void AppletQuickItem::compactRepresentationCheck() //Expanded if (full) { - QQuickItem *item = qobject_cast(createFullRepresentationItem()); + QQuickItem *item = qobject_cast(d->createFullRepresentationItem()); if (item) { item->setParentItem(this); @@ -568,12 +608,12 @@ void AppletQuickItem::compactRepresentationCheck() } d->currentRepresentationItem = item; - connectLayoutAttached(item); + d->connectLayoutAttached(item); } //Icon } else { - QQuickItem *compactItem = qobject_cast(createCompactRepresentationItem()); + QQuickItem *compactItem = qobject_cast(d->createCompactRepresentationItem()); if (compactItem) { //set the root item as the main visible item @@ -592,55 +632,15 @@ void AppletQuickItem::compactRepresentationCheck() if (d->compactRepresentationExpanderItem) { d->compactRepresentationExpanderItem.data()->setProperty("compactRepresentation", QVariant::fromValue(compactItem)); - d->compactRepresentationExpanderItem.data()->setProperty("fullRepresentation", QVariant::fromValue(createFullRepresentationItem())); + d->compactRepresentationExpanderItem.data()->setProperty("fullRepresentation", QVariant::fromValue(d->createFullRepresentationItem())); } d->currentRepresentationItem = compactItem; - connectLayoutAttached(compactItem); + d->connectLayoutAttached(compactItem); } } } -void AppletQuickItem::minimumWidthChanged() -{ - propagateSizeHint("minimumWidth"); -} - -void AppletQuickItem::minimumHeightChanged() -{ - propagateSizeHint("minimumHeight"); -} - -void AppletQuickItem::preferredWidthChanged() -{ - propagateSizeHint("preferredWidth"); -} - -void AppletQuickItem::preferredHeightChanged() -{ - propagateSizeHint("preferredHeight"); -} - -void AppletQuickItem::maximumWidthChanged() -{ - propagateSizeHint("maximumWidth"); -} - -void AppletQuickItem::maximumHeightChanged() -{ - propagateSizeHint("maximumHeight"); -} - -void AppletQuickItem::fillWidthChanged() -{ - propagateSizeHint("fillWidth"); -} - -void AppletQuickItem::fillHeightChanged() -{ - propagateSizeHint("fillHeight"); -} - #include "moc_appletquickitem.cpp" diff --git a/src/scriptengines/qml/plasmoid/appletquickitem.h b/src/scriptengines/qml/plasmoid/appletquickitem.h index ee9857e0e..c417df20a 100644 --- a/src/scriptengines/qml/plasmoid/appletquickitem.h +++ b/src/scriptengines/qml/plasmoid/appletquickitem.h @@ -139,29 +139,14 @@ protected: void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); virtual void itemChange(ItemChange change, const ItemChangeData &value); - QObject *createCompactRepresentationItem(); - QObject *createFullRepresentationItem(); - QObject *createCompactRepresentationExpanderItem(); - - //look into item, and return the Layout attached property, if found - void connectLayoutAttached(QObject *item); - void propagateSizeHint(const QByteArray &layoutProperty); private Q_SLOTS: void compactRepresentationCheck(); - //handlers of Layout signals - void minimumWidthChanged(); - void minimumHeightChanged(); - void preferredWidthChanged(); - void preferredHeightChanged(); - void maximumWidthChanged(); - void maximumHeightChanged(); - void fillWidthChanged(); - void fillHeightChanged(); - private: AppletQuickItemPrivate *const d; + + Q_PRIVATE_SLOT(d, void minimumWidthChanged()) }; QML_DECLARE_TYPEINFO(AppletQuickItem, QML_HAS_ATTACHED_PROPERTIES) diff --git a/src/scriptengines/qml/plasmoid/appletquickitem_p.h b/src/scriptengines/qml/plasmoid/appletquickitem_p.h index ad4e080fa..9952ac78b 100644 --- a/src/scriptengines/qml/plasmoid/appletquickitem_p.h +++ b/src/scriptengines/qml/plasmoid/appletquickitem_p.h @@ -35,7 +35,28 @@ class AppletQuickItem; class AppletQuickItemPrivate { public: - AppletQuickItemPrivate(Plasma::Applet *a); + AppletQuickItemPrivate(Plasma::Applet *a, AppletQuickItem *item); + + QObject *createCompactRepresentationItem(); + QObject *createFullRepresentationItem(); + QObject *createCompactRepresentationExpanderItem(); + + //look into item, and return the Layout attached property, if found + void connectLayoutAttached(QObject *item); + void propagateSizeHint(const QByteArray &layoutProperty); + + //handlers of Layout signals, private slots + void minimumWidthChanged(); + void minimumHeightChanged(); + void preferredWidthChanged(); + void preferredHeightChanged(); + void maximumWidthChanged(); + void maximumHeightChanged(); + void fillWidthChanged(); + void fillHeightChanged(); + + + AppletQuickItem *q; int switchWidth; int switchHeight;