diff options
author | Eike Ziller <eike.ziller@qt.io> | 2019-09-03 15:10:37 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2019-09-10 12:56:51 +0000 |
commit | 62d16e437a0faf2de55284c19ac6a08797c8d127 (patch) | |
tree | 8a292c91a1de2d92fddd3778eb21ab43f2b105bf /src/plugins/help | |
parent | 3bb0ad62e2974be8e0e695ca21d628a6e0b38320 (diff) |
Help: Add context menu to litehtml backend
Change-Id: I051984e360b41d17d32cff80fd1d4017fc81dae6
Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/help')
-rw-r--r-- | src/plugins/help/litehtmlhelpviewer.cpp | 69 | ||||
-rw-r--r-- | src/plugins/help/litehtmlhelpviewer.h | 1 | ||||
-rw-r--r-- | src/plugins/help/qlitehtml/container_qpainter.cpp | 14 | ||||
-rw-r--r-- | src/plugins/help/qlitehtml/container_qpainter.h | 2 | ||||
-rw-r--r-- | src/plugins/help/qlitehtml/qlitehtmlwidget.cpp | 8 | ||||
-rw-r--r-- | src/plugins/help/qlitehtml/qlitehtmlwidget.h | 2 |
6 files changed, 61 insertions, 35 deletions
diff --git a/src/plugins/help/litehtmlhelpviewer.cpp b/src/plugins/help/litehtmlhelpviewer.cpp index 469188a7de..05ffd5a1cb 100644 --- a/src/plugins/help/litehtmlhelpviewer.cpp +++ b/src/plugins/help/litehtmlhelpviewer.cpp @@ -25,6 +25,7 @@ #include "litehtmlhelpviewer.h" +#include "helpconstants.h" #include "localhelpmanager.h" #include <utils/algorithm.h> @@ -71,6 +72,10 @@ LiteHtmlHelpViewer::LiteHtmlHelpViewer(QWidget *parent) { m_viewer->setResourceHandler([](const QUrl &url) { return getData(url); }); connect(m_viewer, &QLiteHtmlWidget::linkClicked, this, &LiteHtmlHelpViewer::setSource); + connect(m_viewer, + &QLiteHtmlWidget::contextMenuRequested, + this, + &LiteHtmlHelpViewer::showContextMenu); auto layout = new QVBoxLayout; setLayout(layout); layout->setContentsMargins(0, 0, 0, 0); @@ -270,46 +275,40 @@ void LiteHtmlHelpViewer::setSourceInternal(const QUrl &url, Utils::optional<int> emit titleChanged(); } +void LiteHtmlHelpViewer::showContextMenu(const QPoint &pos, const QUrl &url) +{ + QMenu menu(nullptr); + + QAction *copyAnchorAction = nullptr; + if (!url.isEmpty() && url.isValid()) { + if (isActionVisible(HelpViewer::Action::NewPage)) { + QAction *action = menu.addAction( + QCoreApplication::translate("HelpViewer", Constants::TR_OPEN_LINK_AS_NEW_PAGE)); + connect(action, &QAction::triggered, this, [this, url]() { + emit newPageRequested(url); + }); + } + if (isActionVisible(HelpViewer::Action::ExternalWindow)) { + QAction *action = menu.addAction( + QCoreApplication::translate("HelpViewer", Constants::TR_OPEN_LINK_IN_WINDOW)); + connect(action, &QAction::triggered, this, [this, url]() { + emit externalPageRequested(url); + }); + } + copyAnchorAction = menu.addAction(tr("Copy Link")); + } else if (!m_viewer->selectedText().isEmpty()) { + connect(menu.addAction(tr("Copy")), &QAction::triggered, this, &HelpViewer::copy); + } + + if (copyAnchorAction == menu.exec(m_viewer->mapToGlobal(pos))) + QGuiApplication::clipboard()->setText(url.toString()); +} + LiteHtmlHelpViewer::HistoryItem LiteHtmlHelpViewer::currentHistoryItem() const { return {m_viewer->url(), m_viewer->title(), m_viewer->verticalScrollBar()->value()}; } -// -- private -//void TextBrowserHelpWidget::contextMenuEvent(QContextMenuEvent *event) -//{ -// QMenu menu("", nullptr); - -// QAction *copyAnchorAction = nullptr; -// const QUrl link(linkAt(event->pos())); -// if (!link.isEmpty() && link.isValid()) { -// QAction *action = menu.addAction(tr("Open Link")); -// connect(action, &QAction::triggered, this, [this, link]() { -// setSource(link); -// }); -// if (m_parent->isActionVisible(HelpViewer::Action::NewPage)) { -// action = menu.addAction(QCoreApplication::translate("HelpViewer", Constants::TR_OPEN_LINK_AS_NEW_PAGE)); -// connect(action, &QAction::triggered, this, [this, link]() { -// emit m_parent->newPageRequested(link); -// }); -// } -// if (m_parent->isActionVisible(HelpViewer::Action::ExternalWindow)) { -// action = menu.addAction(QCoreApplication::translate("HelpViewer", Constants::TR_OPEN_LINK_IN_WINDOW)); -// connect(action, &QAction::triggered, this, [this, link]() { -// emit m_parent->externalPageRequested(link); -// }); -// } -// copyAnchorAction = menu.addAction(tr("Copy Link")); -// } else if (!textCursor().selectedText().isEmpty()) { -// connect(menu.addAction(tr("Copy")), &QAction::triggered, this, &QTextEdit::copy); -// } else { -// connect(menu.addAction(tr("Reload")), &QAction::triggered, this, &QTextBrowser::reload); -// } - -// if (copyAnchorAction == menu.exec(event->globalPos())) -// QApplication::clipboard()->setText(link.toString()); -//} - //bool TextBrowserHelpWidget::eventFilter(QObject *obj, QEvent *event) //{ // if (obj == this) { diff --git a/src/plugins/help/litehtmlhelpviewer.h b/src/plugins/help/litehtmlhelpviewer.h index 39d990f469..b596bf64cf 100644 --- a/src/plugins/help/litehtmlhelpviewer.h +++ b/src/plugins/help/litehtmlhelpviewer.h @@ -81,6 +81,7 @@ private: void goForward(int count); void goBackward(int count); void setSourceInternal(const QUrl &url, Utils::optional<int> vscroll = Utils::nullopt); + void showContextMenu(const QPoint &pos, const QUrl &url); struct HistoryItem { diff --git a/src/plugins/help/qlitehtml/container_qpainter.cpp b/src/plugins/help/qlitehtml/container_qpainter.cpp index e461086f48..3249bb4c30 100644 --- a/src/plugins/help/qlitehtml/container_qpainter.cpp +++ b/src/plugins/help/qlitehtml/container_qpainter.cpp @@ -1007,6 +1007,20 @@ QVector<QRect> DocumentContainer::leaveEvent() return {}; } +QUrl DocumentContainer::linkAt(const QPoint &documentPos, const QPoint &viewportPos) +{ + if (!m_document) + return {}; + const litehtml::element::ptr element = m_document->root()->get_element_by_point(documentPos.x(), + documentPos.y(), + viewportPos.x(), + viewportPos.y()); + const char *href = element->get_attr("href"); + if (href) + return resolveUrl(QString::fromUtf8(href), m_baseUrl); + return {}; +} + QString DocumentContainer::caption() const { return m_caption; diff --git a/src/plugins/help/qlitehtml/container_qpainter.h b/src/plugins/help/qlitehtml/container_qpainter.h index 58833bf840..a1f935550f 100644 --- a/src/plugins/help/qlitehtml/container_qpainter.h +++ b/src/plugins/help/qlitehtml/container_qpainter.h @@ -138,6 +138,8 @@ public: Qt::MouseButton button); QVector<QRect> leaveEvent(); + QUrl linkAt(const QPoint &documentPos, const QPoint &viewportPos); + QString caption() const; QString selectedText() const; diff --git a/src/plugins/help/qlitehtml/qlitehtmlwidget.cpp b/src/plugins/help/qlitehtml/qlitehtmlwidget.cpp index 4b36b7bc04..63f4916c1c 100644 --- a/src/plugins/help/qlitehtml/qlitehtmlwidget.cpp +++ b/src/plugins/help/qlitehtml/qlitehtmlwidget.cpp @@ -541,6 +541,14 @@ void QLiteHtmlWidget::leaveEvent(QEvent *event) viewport()->update(r.translated(-scrollPosition())); } +void QLiteHtmlWidget::contextMenuEvent(QContextMenuEvent *event) +{ + QPoint viewportPos; + QPoint pos; + htmlPos(event->pos(), &viewportPos, &pos); + emit contextMenuRequested(event->pos(), d->documentContainer.linkAt(pos, viewportPos)); +} + void QLiteHtmlWidget::render() { if (!d->documentContainer.document()) diff --git a/src/plugins/help/qlitehtml/qlitehtmlwidget.h b/src/plugins/help/qlitehtml/qlitehtmlwidget.h index 433e65e15d..b2f1df6cf4 100644 --- a/src/plugins/help/qlitehtml/qlitehtmlwidget.h +++ b/src/plugins/help/qlitehtml/qlitehtmlwidget.h @@ -55,6 +55,7 @@ public: signals: void linkClicked(const QUrl &url); + void contextMenuRequested(const QPoint &pos, const QUrl &url); protected: void paintEvent(QPaintEvent *event) override; @@ -64,6 +65,7 @@ protected: void mouseReleaseEvent(QMouseEvent *event) override; void mouseDoubleClickEvent(QMouseEvent *event) override; void leaveEvent(QEvent *event) override; + void contextMenuEvent(QContextMenuEvent *event) override; private: void render(); |