From e0c7eaafad5cae1052bec95a0d8603f3b88105c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCgler?= Date: Fri, 5 Apr 2013 04:27:52 +0200 Subject: [PATCH] dynamic object creation inside tooltip The mainItem is now a Component, which gets created on demand and passed to the tooltip dialog. Positioning is a bit off now, but it basically seems to work as we'd like it to. --- src/declarativeimports/core/ToolTip.qml | 58 +++++++++++++++++++------ src/declarativeimports/core/tooltip.cpp | 32 +++++++++++--- src/declarativeimports/core/tooltip.h | 7 +++ 3 files changed, 76 insertions(+), 21 deletions(-) diff --git a/src/declarativeimports/core/ToolTip.qml b/src/declarativeimports/core/ToolTip.qml index f4ee4dc67..a70c5bb6c 100644 --- a/src/declarativeimports/core/ToolTip.qml +++ b/src/declarativeimports/core/ToolTip.qml @@ -40,11 +40,12 @@ import org.kde.plasma.extras 2.0 as PlasmaExtras MouseArea { id: tooltip - property alias mainText: tooltipMaintext.text // string - property alias subText: tooltipSubtext.text // string - property alias iconSource: tooltipIcon.source // icon name - property alias image: tooltipImage.source // string / url to the image + property string mainText: tooltipMaintext.text // string + property string subText: tooltipSubtext.text // string + property string iconSource: tooltipIcon.source // icon name + property string image: tooltipImage.source // string / url to the image property Item target: parent + property alias mainItem: tooltipWindow.mainItem // private props property int _s: theme.iconSizes.small / 2 @@ -52,11 +53,28 @@ MouseArea { hoverEnabled: true onEntered: { print("entered"); - tooltipWindow.visible = true; - tooltipHideTimer.running = false; + show(); } onExited: { print("exit"); + hide(); + } + + function show() { + var mi = tooltip.mainItem; + if (mi == null) { + mi = tooltipWindow.mainComponent.createObject( tooltip.target, { "mainText": tooltip.mainText, "subText": tooltip.subText, "iconSource": tooltip.iconSource, "image": tooltip.image }); + } else { + // TODO: update properties + } + //return; + tooltipWindow.visualParent = tooltip.target; + tooltipWindow.mainItem = mi; + tooltipWindow.visible = true; + tooltipHideTimer.running = false; + } + + function hide() { tooltipHideTimer.running = true } @@ -64,10 +82,11 @@ MouseArea { id: tooltipHideTimer running: false repeat: false - interval: 50 + interval: 0 onTriggered: { //print("Hiding tooltip ..."); tooltipWindow.visible = false; + tooltipWindow.mainItem.destroy(); } } @@ -75,31 +94,40 @@ MouseArea { id: tooltipWindow visualParent: tooltip.target - mainItem: PlasmaCore.FrameSvgItem { + mainComponent: Component { id: tooltipSvg //imagePath: "widgets/tooltip" - width: childrenRect.width + margins.left + margins.right + 2*_s - height: childrenRect.height + margins.top + margins.bottom + 2*_s +// width: childrenRect.width + margins.left + margins.right + 2*_s +// height: childrenRect.height + margins.top + margins.bottom + 2*_s Item { id: tooltipContentItem - x: tooltipSvg.margins.left + _s - y: tooltipSvg.margins.top + _s + x: _s + y: _s width: childrenRect.width + _s height: childrenRect.height + property string mainText: "Default mainText" // string + property string subText: "Default subText" // string + property string iconSource: "klipper" // icon name + property string image: "" // string / url to the image + property int maxTextSize: Math.max(tooltipMaintext.paintedWidth, tooltipSubtext.paintedWidth) property int maxSize: theme.iconSizes.desktop * 6 property int preferredTextWidth: Math.min(maxTextSize, maxSize) + property int _s: theme.iconSizes.small / 2 + Image { id: tooltipImage + source: image } PlasmaCore.IconItem { id: tooltipIcon width: theme.iconSizes.desktop height: width + source: iconSource anchors { leftMargin: _s } @@ -109,7 +137,7 @@ MouseArea { level: 3 width: parent.preferredTextWidth wrapMode: Text.WordWrap - text: tooltipWindow.mainText + text: mainText anchors { left: (tooltipImage.source != "") ? tooltipImage.right : tooltipIcon.right leftMargin: _s*2 @@ -120,7 +148,7 @@ MouseArea { id: tooltipSubtext width: parent.preferredTextWidth wrapMode: Text.WordWrap - text: tooltipWindow.subText + text: subText opacity: 0.5 anchors { left: tooltipMaintext.left @@ -129,6 +157,8 @@ MouseArea { top: tooltipMaintext.bottom } } + Component.onCompleted: print("XXX Tooltip mainItem created.") + Component.onDestruction: print("XXX Tooltip mainItem destroyed.") } } } diff --git a/src/declarativeimports/core/tooltip.cpp b/src/declarativeimports/core/tooltip.cpp index f34f9d7d9..23d061848 100644 --- a/src/declarativeimports/core/tooltip.cpp +++ b/src/declarativeimports/core/tooltip.cpp @@ -51,6 +51,16 @@ void ToolTip::setTarget(QQuickItem *target) } } +QQmlComponent* ToolTip::mainComponent() const +{ + return m_mainComponent.data(); +} + +void ToolTip::setMainComponent(QQmlComponent* mainComponent) +{ + m_mainComponent = mainComponent; +} + QQuickItem *ToolTip::mainItem() const { return m_mainItem.data(); @@ -77,12 +87,12 @@ void ToolTip::setMainItem(QQuickItem *mainItem) //mainItem->setParent(contentItem()); //mainItem->setProperty("parent", QVariant::fromValue(contentItem())); - if (mainItem->metaObject()->indexOfSignal("widthChanged")) { - connect(mainItem, SIGNAL(widthChanged()), m_syncTimer, SIGNAL(start())); - } - if (mainItem->metaObject()->indexOfSignal("heightChanged")) { - connect(mainItem, SIGNAL(heightChanged()), m_syncTimer, SIGNAL(start())); - } +// if (mainItem->metaObject()->indexOfSignal("widthChanged")) { +// connect(mainItem, SIGNAL(widthChanged()), m_syncTimer, SIGNAL(start())); +// } +// if (mainItem->metaObject()->indexOfSignal("heightChanged")) { +// connect(mainItem, SIGNAL(heightChanged()), m_syncTimer, SIGNAL(start())); +// } } //if this is called in Compenent.onCompleted we have to wait a loop the item is added to a scene @@ -118,7 +128,14 @@ void ToolTip::setVisible(const bool visible) ToolTipDialog *dlg = ToolTipDialog::instance(); qDebug() << visible; if (visible) { - qDebug() << " showing tooltip: " << ToolTipDialog::instance(); + //dlg->mainItem()->deleteLater(); + +// QObject *myObject = m_mainComponent.data()->create(); +// QQuickItem *item = qobject_cast(myObject); + qDebug() << "XXX Setting visible: " << mainItem(); + setMainItem(mainItem()); + + qDebug() << "XXX showing tooltip: " << ToolTipDialog::instance(); dlg->setMainItem(mainItem()); dlg->setVisualParent(m_visualParent.data()); dlg->setPosition(dlg->popupPosition(visualParent())); @@ -128,6 +145,7 @@ void ToolTip::setVisible(const bool visible) // raise(); } else { dlg->setVisible(false); + //dlg->mainItem()->deleteLater(); } //QQuickWindow::setVisible(visible); } diff --git a/src/declarativeimports/core/tooltip.h b/src/declarativeimports/core/tooltip.h index 6da01aa65..d8f5f3d9d 100644 --- a/src/declarativeimports/core/tooltip.h +++ b/src/declarativeimports/core/tooltip.h @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -48,6 +49,7 @@ class ToolTip : public QObject * The main QML item that will be displayed in the Dialog */ Q_PROPERTY(QQuickItem *mainItem READ mainItem WRITE setMainItem NOTIFY mainItemChanged) + Q_PROPERTY(QQmlComponent *mainComponent READ mainComponent WRITE setMainComponent NOTIFY mainComponentChanged) /** * The main QML item that will be displayed in the Dialog @@ -67,6 +69,9 @@ public: QQuickItem *target() const; void setTarget(QQuickItem *target); + QQmlComponent* mainComponent() const; + void setMainComponent(QQmlComponent *mainComponent); + QQuickItem *mainItem() const; void setMainItem(QQuickItem *mainItem); @@ -80,12 +85,14 @@ public: Q_SIGNALS: void targetChanged(); + void mainComponentChanged(); void mainItemChanged(); void visualParentChanged(); void visibleChanged(); private: QTimer *m_syncTimer; + QWeakPointer m_mainComponent; QWeakPointer m_mainItem; QWeakPointer m_visualParent; QWeakPointer m_declarativeItemContainer;