diff --git a/src/plugins/help/litehtmlhelpviewer.cpp b/src/plugins/help/litehtmlhelpviewer.cpp index 469188a7de0..05ffd5a1cbd 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 @@ -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 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 39d990f4695..b596bf64cf2 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 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 e461086f487..3249bb4c30b 100644 --- a/src/plugins/help/qlitehtml/container_qpainter.cpp +++ b/src/plugins/help/qlitehtml/container_qpainter.cpp @@ -1007,6 +1007,20 @@ QVector 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 58833bf8400..a1f935550f1 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 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 4b36b7bc04a..63f4916c1ce 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 433e65e15d4..b2f1df6cf4c 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();