From fd1a34045b09d9c7e927d814b87617c2424e68a8 Mon Sep 17 00:00:00 2001 From: Szabolcs David Date: Tue, 5 Feb 2019 15:07:24 +0100 Subject: Add a way to customize tooltips in WebEngineView Implement a tooltipRequested signal and a corresponding request class to expose tooltip information to the users. Task-number: QTBUG-59290 Change-Id: I4e31773c62a65d6f340aaa74237cb0076252cd5b Reviewed-by: Michal Klocek --- .../api/qquickwebenginedialogrequests.cpp | 110 +++++++++++++++++++++ .../api/qquickwebenginedialogrequests_p.h | 33 +++++++ src/webengine/api/qquickwebengineview.cpp | 8 +- src/webengine/api/qquickwebengineview_p.h | 4 +- src/webengine/doc/src/webengineview_lgpl.qdoc | 12 +++ src/webengine/plugin/plugin.cpp | 3 + src/webengine/ui_delegates_manager.cpp | 6 +- 7 files changed, 171 insertions(+), 5 deletions(-) (limited to 'src/webengine') diff --git a/src/webengine/api/qquickwebenginedialogrequests.cpp b/src/webengine/api/qquickwebenginedialogrequests.cpp index b1f52a6b1..01e757375 100644 --- a/src/webengine/api/qquickwebenginedialogrequests.cpp +++ b/src/webengine/api/qquickwebenginedialogrequests.cpp @@ -44,6 +44,9 @@ #include "file_picker_controller.h" #include "web_contents_adapter_client.h" +#include +#include + QT_BEGIN_NAMESPACE using namespace QtWebEngineCore; @@ -823,4 +826,111 @@ void QQuickWebEngineFormValidationMessageRequest::setAccepted(bool accepted) m_accepted = accepted; } +/////////////////////////////////////////////////////////////////////////////// + +/*! + \qmltype TooltipRequest + \instantiates QQuickWebEngineTooltipRequest + \inqmlmodule QtWebEngine + \since QtWebEngine 1.10 + + \brief A request for showing a tooltip to the user. +*/ + +QQuickWebEngineTooltipRequest::QQuickWebEngineTooltipRequest( + const QString &text, QObject *parent): + QObject(parent) + , m_text(text) + , m_type(text.isEmpty() ? RequestType::Hide : RequestType::Show) + , m_accepted(false) +{ + Q_ASSERT(parent); + if (QQuickItem *view = qobject_cast(parent)) + m_position = view->mapFromGlobal(view->cursor().pos()).toPoint(); +} + +QQuickWebEngineTooltipRequest::~QQuickWebEngineTooltipRequest() +{ + +} + +/*! + \qmlproperty int TooltipRequest::x + \readonly + + The x coordinate of the top-left corner of the requested tooltip. +*/ + +int QQuickWebEngineTooltipRequest::x() const +{ + return m_position.x(); +} + +/*! + \qmlproperty int TooltipRequest::y + \readonly + + The y coordinate of the top-left corner of the requested tooltip. +*/ + +int QQuickWebEngineTooltipRequest::y() const +{ + return m_position.y(); +} + +/*! + \qmlproperty bool TooltipRequest::text + \readonly + + The text of the tooltip. It contains an empty string when the + tooltip should be hidden. +*/ + + +QString QQuickWebEngineTooltipRequest::text() const +{ + return m_text; +} + +/*! + \qmlproperty enumeration TooltipRequest::type + \readonly + + The type of the tooltip request. + + \value TooltipRequest.Show + The tooltip should be shown. + \value TooltipRequest.Hide + The tooltip should be hidden. +*/ + +QQuickWebEngineTooltipRequest::RequestType QQuickWebEngineTooltipRequest::type() const +{ + return m_type; +} + +/*! + \qmlproperty bool TooltipRequest::accepted + + Indicates whether the tooltip request has been accepted + by the signal handler. + + If the property is \c false after any signal handlers + for WebEngineView::tooltipRequested have been executed, + a default tooltip will be shown. + To prevent this, set \c {request.accepted} to \c true. + + The default is \c false. +*/ + +bool QQuickWebEngineTooltipRequest::isAccepted() const +{ + return m_accepted; +} + +void QQuickWebEngineTooltipRequest::setAccepted(bool accepted) +{ + m_accepted = accepted; +} + QT_END_NAMESPACE diff --git a/src/webengine/api/qquickwebenginedialogrequests_p.h b/src/webengine/api/qquickwebenginedialogrequests_p.h index cdb10c26b..5e3f7c547 100644 --- a/src/webengine/api/qquickwebenginedialogrequests_p.h +++ b/src/webengine/api/qquickwebenginedialogrequests_p.h @@ -260,6 +260,39 @@ private: friend class QQuickWebEngineViewPrivate; }; +class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineTooltipRequest : public QObject { + Q_OBJECT +public: + enum RequestType { + Show, + Hide, + }; + Q_ENUM(RequestType) + Q_PROPERTY(int x READ x CONSTANT FINAL) + Q_PROPERTY(int y READ y CONSTANT FINAL) + Q_PROPERTY(QString text READ text CONSTANT FINAL) + Q_PROPERTY(RequestType type READ type CONSTANT FINAL) + Q_PROPERTY(bool accepted READ isAccepted WRITE setAccepted FINAL) + + ~QQuickWebEngineTooltipRequest(); + int x() const; + int y() const; + QString text() const; + RequestType type() const; + bool isAccepted() const; + void setAccepted(bool accepted); + +private: + QQuickWebEngineTooltipRequest(const QString &text = QString(), + QObject *parent = nullptr); + QPoint m_position; + QString m_text; + RequestType m_type; + bool m_accepted; + friend class QQuickWebEngineViewPrivate; + Q_DISABLE_COPY(QQuickWebEngineTooltipRequest) +}; + QT_END_NAMESPACE #endif // QQUICKWEBENGINDIALOGREQUESTS_H diff --git a/src/webengine/api/qquickwebengineview.cpp b/src/webengine/api/qquickwebengineview.cpp index e7b2d8c9c..b6b542f15 100644 --- a/src/webengine/api/qquickwebengineview.cpp +++ b/src/webengine/api/qquickwebengineview.cpp @@ -1210,7 +1210,13 @@ bool QQuickWebEngineViewPrivate::isEnabled() const void QQuickWebEngineViewPrivate::setToolTip(const QString &toolTipText) { - ui()->showToolTip(toolTipText); + Q_Q(QQuickWebEngineView); + QQuickWebEngineTooltipRequest *request = new QQuickWebEngineTooltipRequest(toolTipText, q); + // mark the object for gc by creating temporary jsvalue + qmlEngine(q)->newQObject(request); + Q_EMIT q->tooltipRequested(request); + if (!request->isAccepted()) + ui()->showToolTip(toolTipText); } QtWebEngineCore::TouchHandleDrawableClient *QQuickWebEngineViewPrivate::createTouchHandle(const QMap &images) diff --git a/src/webengine/api/qquickwebengineview_p.h b/src/webengine/api/qquickwebengineview_p.h index c851dcb8d..abbe40f53 100644 --- a/src/webengine/api/qquickwebengineview_p.h +++ b/src/webengine/api/qquickwebengineview_p.h @@ -76,6 +76,7 @@ class QQuickWebEngineNavigationRequest; class QQuickWebEngineNewViewRequest; class QQuickWebEngineProfile; class QQuickWebEngineSettings; +class QQuickWebEngineTooltipRequest; class QQuickWebEngineFormValidationMessageRequest; class QQuickWebEngineViewPrivate; class QWebEngineQuotaRequest; @@ -104,7 +105,7 @@ private: const bool m_toggleOn; }; -#define LATEST_WEBENGINEVIEW_REVISION 9 +#define LATEST_WEBENGINEVIEW_REVISION 10 class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineView : public QQuickItem { Q_OBJECT @@ -552,6 +553,7 @@ Q_SIGNALS: Q_REVISION(7) void registerProtocolHandlerRequested(const QWebEngineRegisterProtocolHandlerRequest &request); Q_REVISION(8) void printRequested(); Q_REVISION(9) void selectClientCertificate(QQuickWebEngineClientCertificateSelection *clientCertSelection); + Q_REVISION(10) void tooltipRequested(QQuickWebEngineTooltipRequest *request); #if QT_CONFIG(webengine_testsupport) void testSupportChanged(); diff --git a/src/webengine/doc/src/webengineview_lgpl.qdoc b/src/webengine/doc/src/webengineview_lgpl.qdoc index a6f2af39e..3b375c532 100644 --- a/src/webengine/doc/src/webengineview_lgpl.qdoc +++ b/src/webengine/doc/src/webengineview_lgpl.qdoc @@ -1493,3 +1493,15 @@ \sa WebEngineClientCertificateSelection */ + +/*! + \qmlsignal WebEngineView::tooltipRequested(TooltipRequest request) + \since QtWebEngine 1.10 + + This signal is emitted when the web page wants to show a tooltip at + a specified position. + + \note Signal handlers need to call \c{request.accepted = true} to prevent a default tooltip from showing up. + + \sa TooltipRequest +*/ diff --git a/src/webengine/plugin/plugin.cpp b/src/webengine/plugin/plugin.cpp index 82a76760d..a4f84fe4d 100644 --- a/src/webengine/plugin/plugin.cpp +++ b/src/webengine/plugin/plugin.cpp @@ -95,6 +95,7 @@ public: qmlRegisterType(uri, 1, 7, "WebEngineView"); qmlRegisterType(uri, 1, 8, "WebEngineView"); qmlRegisterType(uri, 1, 9, "WebEngineView"); + qmlRegisterType(uri, 1, 10, "WebEngineView"); qmlRegisterType(uri, 1, 1, "WebEngineProfile"); qmlRegisterType(uri, 1, 2, "WebEngineProfile"); qmlRegisterType(uri, 1, 3, "WebEngineProfile"); @@ -163,6 +164,8 @@ public: qmlRegisterUncreatableType(uri, 1, 9, "WebEngineClientCertificateOption", tr("Cannot create a separate instance of WebEngineClientCertificateOption")); qmlRegisterUncreatableType(uri, 1, 9, "WebEngineNotification", msgUncreatableType("WebEngineNotification")); + qmlRegisterUncreatableType(uri, 1, 10, "TooltipRequest", + msgUncreatableType("TooltipRequest")); } private: diff --git a/src/webengine/ui_delegates_manager.cpp b/src/webengine/ui_delegates_manager.cpp index da120ab69..d324fe75d 100644 --- a/src/webengine/ui_delegates_manager.cpp +++ b/src/webengine/ui_delegates_manager.cpp @@ -539,14 +539,14 @@ void UIDelegatesManager::showMenu(QObject *menu) void UIDelegatesManager::showToolTip(const QString &text) { - if (!ensureComponentLoaded(ToolTip)) - return; - if (text.isEmpty()) { m_toolTip.reset(); return; } + if (!ensureComponentLoaded(ToolTip)) + return; + if (!m_toolTip.isNull()) return; -- cgit v1.2.3