From 9eda3882ca7368a808848676ddbdffb548d8408d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCgler?= Date: Tue, 9 Apr 2013 00:08:23 +0200 Subject: [PATCH] Make tooltip positioning work --- src/declarativeimports/core/ToolTip.qml | 8 +-- src/declarativeimports/core/dialog.cpp | 19 +++--- src/declarativeimports/core/dialog.h | 2 +- src/declarativeimports/core/tooltip.cpp | 20 +++++-- src/declarativeimports/core/tooltipdialog.cpp | 1 - src/declarativeimports/core/tooltipwindow.cpp | 59 +------------------ src/declarativeimports/core/tooltipwindow.h | 1 - 7 files changed, 29 insertions(+), 81 deletions(-) diff --git a/src/declarativeimports/core/ToolTip.qml b/src/declarativeimports/core/ToolTip.qml index a70c5bb6c..8a2c63a9c 100644 --- a/src/declarativeimports/core/ToolTip.qml +++ b/src/declarativeimports/core/ToolTip.qml @@ -40,10 +40,10 @@ import org.kde.plasma.extras 2.0 as PlasmaExtras MouseArea { id: tooltip - 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 string mainText // string + property string subText // string + property string iconSource // icon name + property string image // string / url to the image property Item target: parent property alias mainItem: tooltipWindow.mainItem diff --git a/src/declarativeimports/core/dialog.cpp b/src/declarativeimports/core/dialog.cpp index 2a681c9e0..6fd287142 100644 --- a/src/declarativeimports/core/dialog.cpp +++ b/src/declarativeimports/core/dialog.cpp @@ -132,12 +132,12 @@ void DialogProxy::setVisualParent(QQuickItem *visualParent) return; } - if (visualParent) { - setPosition(popupPosition(visualParent, Qt::AlignCenter)); - } m_visualParent = visualParent; emit visualParentChanged(); + if (visualParent && isVisible()) { + setPosition(popupPosition(visualParent, Qt::AlignCenter)); + } } bool DialogProxy::isVisible() const @@ -147,9 +147,8 @@ bool DialogProxy::isVisible() const void DialogProxy::setVisible(const bool visible) { - //qDebug() << visible; - QRect avail = QRect(400, 300, 1200, 800); // FIXME - + //QRect avail = QRect(400, 300, 1200, 800); // FIXME + QRect avail; if (visible) { syncToMainItemSize(); if (!m_visualParent.isNull()) { @@ -194,15 +193,13 @@ QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment) //If no item was specified try to align at the center of the parent view QQuickItem *parentItem = qobject_cast(parent()); if (parentItem && parentItem->window()) { - //qDebug() << "NO visual parent ... Centering at " << (parentItem->window()->geometry().center() - QPoint(width()/2, height()/2)); return parentItem->window()->geometry().center() - QPoint(width()/2, height()/2); } else { - //qDebug() << "No QQuickItem as parent found"; return QPoint(); } } QPointF pos = item->mapToScene(QPointF(0, 0)); - //qDebug() << "I've an Item at " << pos; + if (item->window() && item->window()->screen()) { pos = item->window()->mapToGlobal(pos.toPoint()); } else { @@ -282,7 +279,6 @@ QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment) } else { menuPos.setY(avail.height() - item->boundingRect().height() + bottomMargin); } - //qDebug() << menuPos; } //qDebug() << "Popup position" << menuPos << " Location: Plasma::" <width(), m_mainItem.data()->height()) + QSize(m_frameSvgItem->margins()->left() + m_frameSvgItem->margins()->right(), diff --git a/src/declarativeimports/core/dialog.h b/src/declarativeimports/core/dialog.h index 11ff9282f..5908ab6bb 100644 --- a/src/declarativeimports/core/dialog.h +++ b/src/declarativeimports/core/dialog.h @@ -150,7 +150,7 @@ Q_SIGNALS: void locationChanged(); void visualParentChanged(); -private Q_SLOTS: +public Q_SLOTS: void syncMainItemToSize(); void syncToMainItemSize(); diff --git a/src/declarativeimports/core/tooltip.cpp b/src/declarativeimports/core/tooltip.cpp index 23d061848..1e4203330 100644 --- a/src/declarativeimports/core/tooltip.cpp +++ b/src/declarativeimports/core/tooltip.cpp @@ -102,7 +102,14 @@ void ToolTip::setMainItem(QQuickItem *mainItem) QQuickItem *ToolTip::visualParent() const { - return m_visualParent.data(); + if (m_visualParent.data()) { + qDebug() << "returning real visualParent"; + return m_visualParent.data(); + } else { + qDebug() << "returning parent as visualParent"; + QQuickItem *qqi = qobject_cast(parent()); + return qqi; + } } void ToolTip::setVisualParent(QQuickItem *visualParent) @@ -126,19 +133,22 @@ bool ToolTip::isVisible() const void ToolTip::setVisible(const bool visible) { ToolTipDialog *dlg = ToolTipDialog::instance(); - qDebug() << visible; + qDebug() << "creating tooltipdialog" << visible; if (visible) { //dlg->mainItem()->deleteLater(); // QObject *myObject = m_mainComponent.data()->create(); // QQuickItem *item = qobject_cast(myObject); - qDebug() << "XXX Setting visible: " << mainItem(); + qDebug() << "---- XXX Setting visible: " << mainItem(); setMainItem(mainItem()); + qDebug() << "XXX showing tooltip: " << ToolTipDialog::instance(); + qDebug() << "XXX positioning near: " << visualParent()->objectName(); dlg->setMainItem(mainItem()); - dlg->setVisualParent(m_visualParent.data()); - dlg->setPosition(dlg->popupPosition(visualParent())); + dlg->setVisualParent(visualParent()); + dlg->syncToMainItemSize(); + //dlg->setPosition(dlg->popupPosition(visualParent())); dlg->setVisible(true); // syncGeometry(); diff --git a/src/declarativeimports/core/tooltipdialog.cpp b/src/declarativeimports/core/tooltipdialog.cpp index 973fbe376..55913a45a 100644 --- a/src/declarativeimports/core/tooltipdialog.cpp +++ b/src/declarativeimports/core/tooltipdialog.cpp @@ -31,7 +31,6 @@ ToolTipDialog::ToolTipDialog(QQuickItem *parent) { setFlags(Qt::ToolTip); m_frameSvgItem->setImagePath("widgets/tooltip"); - qDebug() << "XXX TOolTip ctor'ed"; } ToolTipDialog::~ToolTipDialog() diff --git a/src/declarativeimports/core/tooltipwindow.cpp b/src/declarativeimports/core/tooltipwindow.cpp index 31b617a1a..87cd5fb65 100644 --- a/src/declarativeimports/core/tooltipwindow.cpp +++ b/src/declarativeimports/core/tooltipwindow.cpp @@ -42,11 +42,6 @@ ToolTipWindow::ToolTipWindow(QWindow *parent) m_syncTimer->setSingleShot(true); m_syncTimer->setInterval(250); connect(m_syncTimer, &QTimer::timeout, this, &ToolTipWindow::syncGeometry); - - connect(this, SIGNAL(targetChanged()), this, SLOT(updateToolTip())); - connect(this, SIGNAL(mainTextChanged()), this, SLOT(updateToolTip())); - connect(this, SIGNAL(subTextChanged()), this, SLOT(updateToolTip())); - connect(this, SIGNAL(imageChanged()), this, SLOT(updateToolTip())); } ToolTipWindow::~ToolTipWindow() @@ -76,8 +71,6 @@ void ToolTipWindow::setTarget(QQuickItem *target) void ToolTipWindow::syncGeometry() { - qDebug() << " XXX synching geometry"; - qDebug() << "XXXX mainitem : " << mainItem()->width() << mainItem()->height(); resize(mainItem()->width(), mainItem()->height()); setPosition(popupPosition(visualParent())); @@ -132,47 +125,6 @@ void ToolTipWindow::setImage(QVariant name) emit imageChanged(); } -void ToolTipWindow::updateToolTip() -{ - if (!m_widget) { - return; - } - -// Plasma::ToolTipContent data; -// data.setMainText(m_mainText); -// data.setSubText(m_subText); - - // set image - switch (m_image.type()) { - case QVariant::String: { - QString name = m_image.toString(); - if (!name.isEmpty()) { - QIcon icon = QIcon::fromTheme(name); - if (!icon.isNull()) { -// data.setImage(icon.pixmap(IconSize(KIconLoader::Desktop))); - } - } - break; - } - - case QVariant::Icon: { - QIcon icon = m_image.value(); -// data.setImage(icon); - break; - } - - case QVariant::Pixmap: { - QPixmap pixmap = m_image.value(); -// data.setImage(pixmap); - break; - } - - default: - break; - } - //Plasma::ToolTipManager::self()->setContent(m_widget, data); -} - QQuickItem *ToolTipWindow::mainItem() const { return m_mainItem.data(); @@ -180,11 +132,7 @@ QQuickItem *ToolTipWindow::mainItem() const void ToolTipWindow::setMainItem(QQuickItem *mainItem) { - qDebug() << "XXXX mainitem changed: " << mainItem->width() << mainItem->height(); - -// resize(400, 200); if (m_mainItem.data() != mainItem) { - qDebug() << " XXX new mainItem"; disconnect(m_mainItem.data(), &QQuickItem::widthChanged, this, &ToolTipWindow::syncGeometry); disconnect(m_mainItem.data(), &QQuickItem::heightChanged, this, &ToolTipWindow::syncGeometry); if (m_mainItem) { @@ -194,7 +142,6 @@ void ToolTipWindow::setMainItem(QQuickItem *mainItem) if (mainItem) { - //mainItem->setParentItem(0); connect(m_mainItem.data(), &QQuickItem::widthChanged, this, &ToolTipWindow::syncGeometry); connect(m_mainItem.data(), &QQuickItem::heightChanged, this, &ToolTipWindow::syncGeometry); qDebug() << "XXX new mainITem connected"; @@ -209,7 +156,7 @@ void ToolTipWindow::setMainItem(QQuickItem *mainItem) } } - //if this is called in Compenent.onCompleted we have to wait a loop the item is added to a scene + //if this is called in Component.onCompleted we have to wait a loop the item is added to a scene emit mainItemChanged(); } } @@ -230,7 +177,6 @@ void ToolTipWindow::setVisualParent(QQuickItem *visualParent) } disconnect(m_visualParent.data(), &QQuickItem::xChanged, this, &ToolTipWindow::syncGeometry); disconnect(m_visualParent.data(), &QQuickItem::yChanged, this, &ToolTipWindow::syncGeometry); - //disconnect(visualParent, &QQuickItem::yChanged, this, &ToolTipWindow::syncGeometry); m_visualParent = visualParent; connect(m_visualParent.data(), &QQuickItem::xChanged, this, &ToolTipWindow::syncGeometry); @@ -242,14 +188,12 @@ void ToolTipWindow::setVisualParent(QQuickItem *visualParent) bool ToolTipWindow::isVisible() const { return QQuickWindow::isVisible(); - } void ToolTipWindow::setVisible(const bool visible) { qDebug() << visible; if (visible) { - //setPosition(popupPosition()); syncGeometry(); raise(); } @@ -266,7 +210,6 @@ QPoint ToolTipWindow::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignmen QPointF itemScreenPos; QPointF pos = item->mapToScene(QPointF(0, 0)); - //qDebug() << "I've an Item at " << pos; if (item->window() && item->window()->screen()) { pos = item->window()->mapToGlobal(pos.toPoint()); } else { diff --git a/src/declarativeimports/core/tooltipwindow.h b/src/declarativeimports/core/tooltipwindow.h index c55cbf7e9..cb28bea57 100644 --- a/src/declarativeimports/core/tooltipwindow.h +++ b/src/declarativeimports/core/tooltipwindow.h @@ -113,7 +113,6 @@ Q_SIGNALS: protected Q_SLOTS: void syncGeometry(); - void updateToolTip(); private: QTimer *m_syncTimer;