diff options
author | Adam Kallai <kadam@inf.u-szeged.hu> | 2016-06-20 16:19:49 +0200 |
---|---|---|
committer | Adam Kallai <kadam@inf.u-szeged.hu> | 2016-07-20 08:12:28 +0000 |
commit | 4578283f988abb9d738113953ce43340f15cf1d7 (patch) | |
tree | f1206c7872ae791ea1d9403068793e44d686511f /src/webengine/ui_delegates_manager.cpp | |
parent | 59c603b801b37ced083698bec813c1dbaf2c58e8 (diff) |
Add ToolTip support for Qt WebEngine (QML)
[ChangeLog][QtWebEngineQML][QQuickWebEngineView] ToolTip
(HTML global title attribute) are now handled.
Task-numner: QTBUG-54108
Change-Id: I78cc3c1e934a822d6399ebd35c8c7895fbdf5749
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/webengine/ui_delegates_manager.cpp')
-rw-r--r-- | src/webengine/ui_delegates_manager.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/webengine/ui_delegates_manager.cpp b/src/webengine/ui_delegates_manager.cpp index 6ff12b53f..769a30016 100644 --- a/src/webengine/ui_delegates_manager.cpp +++ b/src/webengine/ui_delegates_manager.cpp @@ -50,6 +50,10 @@ #include <QQmlContext> #include <QQmlEngine> #include <QQmlProperty> +#include <QCursor> +#include <QList> +#include <QScreen> +#include <QGuiApplication> // Uncomment for QML debugging //#define UI_DELEGATES_DEBUG @@ -93,6 +97,31 @@ static QString getUIDelegatesImportDir(QQmlEngine *engine) { return importDir; } +static QPoint calculateToolTipPosition(QPoint &position, QSize &toolTip) { + QRect screen; + QList<QScreen *> screens = QGuiApplication::screens(); + Q_FOREACH (const QScreen *src, screens) + if (src->availableGeometry().contains(position)) + screen = src->availableGeometry(); + + position += QPoint(2, 16); + + if (position.x() + toolTip.width() > screen.x() + screen.width()) + position.rx() -= 4 + toolTip.width(); + if (position.y() + toolTip.height() > screen.y() + screen.height()) + position.ry() -= 24 + toolTip.height(); + if (position.y() < screen.y()) + position.setY(screen.y()); + if (position.x() + toolTip.width() > screen.x() + screen.width()) + position.setX(screen.x() + screen.width() - toolTip.width()); + if (position.x() < screen.x()) + position.setX(screen.x()); + if (position.y() + toolTip.height() > screen.y() + screen.height()) + position.setY(screen.y() + screen.height() - toolTip.height()); + + return position; +} + const char *defaultPropertyName(QObject *obj) { const QMetaObject *metaObject = obj->metaObject(); @@ -116,6 +145,7 @@ MenuItemHandler::MenuItemHandler(QObject *parent) UIDelegatesManager::UIDelegatesManager(QQuickWebEngineView *view) : m_view(view) , m_messageBubbleItem(0) + , m_toolTip(nullptr) FOR_EACH_COMPONENT_TYPE(COMPONENT_MEMBER_INIT, NO_SEPARATOR) { } @@ -493,4 +523,38 @@ void UIDelegatesManager::moveMessageBubble(const QRect &anchor) QQmlProperty(m_messageBubbleItem.data(), QStringLiteral("y")).write(anchor.y() + anchor.size().height()); } +void UIDelegatesManager::showToolTip(const QString &text) +{ + if (!ensureComponentLoaded(ToolTip)) + return; + + if (text.isEmpty()) { + m_toolTip.reset(); + return; + } + + if (!m_toolTip.isNull()) + return; + + QQmlContext *context = qmlContext(m_view); + m_toolTip.reset(toolTipComponent->beginCreate(context)); + if (QQuickItem *item = qobject_cast<QQuickItem *>(m_toolTip.data())) + item->setParentItem(m_view); + m_toolTip->setParent(m_view); + toolTipComponent->completeCreate(); + + QQmlProperty(m_toolTip.data(), QStringLiteral("text")).write(text); + + int height = QQmlProperty(m_toolTip.data(), QStringLiteral("height")).read().toInt(); + int width = QQmlProperty(m_toolTip.data(), QStringLiteral("width")).read().toInt(); + QSize toolTipSize(width, height); + QPoint position = m_view->cursor().pos(); + position = m_view->mapFromGlobal(calculateToolTipPosition(position, toolTipSize)).toPoint(); + + QQmlProperty(m_toolTip.data(), QStringLiteral("x")).write(position.x()); + QQmlProperty(m_toolTip.data(), QStringLiteral("y")).write(position.y()); + + QMetaObject::invokeMethod(m_toolTip.data(), "open"); +} + } // namespace QtWebEngineCore |