forked from qt-creator/qt-creator
Help: Add context menu to litehtml backend
Change-Id: I051984e360b41d17d32cff80fd1d4017fc81dae6 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
#include "litehtmlhelpviewer.h"
|
#include "litehtmlhelpviewer.h"
|
||||||
|
|
||||||
|
#include "helpconstants.h"
|
||||||
#include "localhelpmanager.h"
|
#include "localhelpmanager.h"
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
@@ -71,6 +72,10 @@ LiteHtmlHelpViewer::LiteHtmlHelpViewer(QWidget *parent)
|
|||||||
{
|
{
|
||||||
m_viewer->setResourceHandler([](const QUrl &url) { return getData(url); });
|
m_viewer->setResourceHandler([](const QUrl &url) { return getData(url); });
|
||||||
connect(m_viewer, &QLiteHtmlWidget::linkClicked, this, &LiteHtmlHelpViewer::setSource);
|
connect(m_viewer, &QLiteHtmlWidget::linkClicked, this, &LiteHtmlHelpViewer::setSource);
|
||||||
|
connect(m_viewer,
|
||||||
|
&QLiteHtmlWidget::contextMenuRequested,
|
||||||
|
this,
|
||||||
|
&LiteHtmlHelpViewer::showContextMenu);
|
||||||
auto layout = new QVBoxLayout;
|
auto layout = new QVBoxLayout;
|
||||||
setLayout(layout);
|
setLayout(layout);
|
||||||
layout->setContentsMargins(0, 0, 0, 0);
|
layout->setContentsMargins(0, 0, 0, 0);
|
||||||
@@ -270,46 +275,40 @@ void LiteHtmlHelpViewer::setSourceInternal(const QUrl &url, Utils::optional<int>
|
|||||||
emit titleChanged();
|
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
|
LiteHtmlHelpViewer::HistoryItem LiteHtmlHelpViewer::currentHistoryItem() const
|
||||||
{
|
{
|
||||||
return {m_viewer->url(), m_viewer->title(), m_viewer->verticalScrollBar()->value()};
|
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)
|
//bool TextBrowserHelpWidget::eventFilter(QObject *obj, QEvent *event)
|
||||||
//{
|
//{
|
||||||
// if (obj == this) {
|
// if (obj == this) {
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ private:
|
|||||||
void goForward(int count);
|
void goForward(int count);
|
||||||
void goBackward(int count);
|
void goBackward(int count);
|
||||||
void setSourceInternal(const QUrl &url, Utils::optional<int> vscroll = Utils::nullopt);
|
void setSourceInternal(const QUrl &url, Utils::optional<int> vscroll = Utils::nullopt);
|
||||||
|
void showContextMenu(const QPoint &pos, const QUrl &url);
|
||||||
|
|
||||||
struct HistoryItem
|
struct HistoryItem
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1007,6 +1007,20 @@ QVector<QRect> DocumentContainer::leaveEvent()
|
|||||||
return {};
|
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
|
QString DocumentContainer::caption() const
|
||||||
{
|
{
|
||||||
return m_caption;
|
return m_caption;
|
||||||
|
|||||||
@@ -138,6 +138,8 @@ public:
|
|||||||
Qt::MouseButton button);
|
Qt::MouseButton button);
|
||||||
QVector<QRect> leaveEvent();
|
QVector<QRect> leaveEvent();
|
||||||
|
|
||||||
|
QUrl linkAt(const QPoint &documentPos, const QPoint &viewportPos);
|
||||||
|
|
||||||
QString caption() const;
|
QString caption() const;
|
||||||
QString selectedText() const;
|
QString selectedText() const;
|
||||||
|
|
||||||
|
|||||||
@@ -541,6 +541,14 @@ void QLiteHtmlWidget::leaveEvent(QEvent *event)
|
|||||||
viewport()->update(r.translated(-scrollPosition()));
|
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()
|
void QLiteHtmlWidget::render()
|
||||||
{
|
{
|
||||||
if (!d->documentContainer.document())
|
if (!d->documentContainer.document())
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ public:
|
|||||||
|
|
||||||
signals:
|
signals:
|
||||||
void linkClicked(const QUrl &url);
|
void linkClicked(const QUrl &url);
|
||||||
|
void contextMenuRequested(const QPoint &pos, const QUrl &url);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *event) override;
|
void paintEvent(QPaintEvent *event) override;
|
||||||
@@ -64,6 +65,7 @@ protected:
|
|||||||
void mouseReleaseEvent(QMouseEvent *event) override;
|
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||||
void mouseDoubleClickEvent(QMouseEvent *event) override;
|
void mouseDoubleClickEvent(QMouseEvent *event) override;
|
||||||
void leaveEvent(QEvent *event) override;
|
void leaveEvent(QEvent *event) override;
|
||||||
|
void contextMenuEvent(QContextMenuEvent *event) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void render();
|
void render();
|
||||||
|
|||||||
Reference in New Issue
Block a user