14#include <QtCore/QFileInfo>
15#include <QtCore/QString>
16#include <QtCore/QTimer>
18#include <QtWidgets/QApplication>
19#include <QtGui/QWheelEvent>
21#include <QtNetwork/QNetworkReply>
37 const QNetworkRequest &request, NavigationType type);
40 bool closeNewTabIfNeeded;
42 friend class HelpViewerImpl;
44 Qt::MouseButtons m_pressedButtons;
45 Qt::KeyboardModifiers m_keyboardModifiers;
50 , closeNewTabIfNeeded(
false)
62 newPage->closeNewTabIfNeeded = closeNewTabIfNeeded;
63 closeNewTabIfNeeded =
false;
71 case OpenLinkInNewWindow:
72 closeNewTabIfNeeded =
true;
75 QWebPage::triggerAction(action, checked);
79#if QT_CONFIG(clipboard)
80 if (action == CopyLinkToClipboard || action == CopyImageUrlToClipboard) {
81 const QString link = QApplication::clipboard()->text();
82 QApplication::clipboard()->setText(HelpEngineWrapper::instance().findFile(link).toString());
88 const QNetworkRequest &request, QWebPage::NavigationType type)
91 const bool closeNewTab = closeNewTabIfNeeded;
92 closeNewTabIfNeeded =
false;
94 const QUrl &url = request.url();
95 if (HelpViewer::launchWithExternalApp(url)) {
97 QMetaObject::invokeMethod(OpenPagesManager::instance(),
"closeCurrentPage");
101 if (type == QWebPage::NavigationTypeLinkClicked
102 && (m_keyboardModifiers & Qt::ControlModifier
103 || m_pressedButtons == Qt::MiddleButton)) {
104 m_pressedButtons = Qt::NoButton;
105 m_keyboardModifiers = Qt::NoModifier;
120HelpViewerImpl::HelpViewerImpl(qreal zoom, QWidget *parent)
122 , d(
new HelpViewerImplPrivate)
125 setAcceptDrops(
false);
126 settings()->setAttribute(QWebSettings::JavaEnabled,
false);
127 settings()->setAttribute(QWebSettings::PluginsEnabled,
false);
129 setPage(
new HelpPage(
this));
130 page()->setNetworkAccessManager(HelpBrowserSupport::createNetworkAccessManager(
this));
132 QAction* action = pageAction(QWebPage::OpenLinkInNewWindow);
133 action->setText(tr(
"Open Link in New Page"));
135 pageAction(QWebPage::DownloadLinkToDisk)->setVisible(
false);
136 pageAction(QWebPage::DownloadImageToDisk)->setVisible(
false);
137 pageAction(QWebPage::OpenImageInNewWindow)->setVisible(
false);
139 connect(pageAction(QWebPage::Copy), SIGNAL(changed()),
this,
140 SLOT(actionChanged()));
141 connect(pageAction(QWebPage::Back), SIGNAL(changed()),
this,
142 SLOT(actionChanged()));
143 connect(pageAction(QWebPage::Forward), SIGNAL(changed()),
this,
144 SLOT(actionChanged()));
145 connect(page(), &QWebPage::linkHovered,
this,
146 [
this] (
const QString &link,
const QString &,
const QString &) {
147 emit
this->highlighted(QUrl(link));
149 connect(
this, SIGNAL(urlChanged(QUrl)),
this, SIGNAL(sourceChanged(QUrl)));
150 connect(
this, SIGNAL(loadStarted()),
this, SLOT(setLoadStarted()));
151 connect(
this, SIGNAL(loadFinished(
bool)),
this, SLOT(setLoadFinished(
bool)));
152 connect(
this, SIGNAL(titleChanged(QString)),
this, SIGNAL(titleChanged()));
153 connect(page(), SIGNAL(printRequested(QWebFrame*)),
this, SIGNAL(printRequested()));
155 setFont(viewerFont());
156 setZoomFactor(d->webDpiRatio * (zoom == 0.0 ? 1.0 : zoom));
159QFont HelpViewerImpl::viewerFont()
const
162 if (HelpEngineWrapper::instance().usesBrowserFont())
163 return HelpEngineWrapper::instance().browserFont();
165 QWebSettings *webSettings = QWebSettings::globalSettings();
166 return QFont(webSettings->fontFamily(QWebSettings::StandardFont),
167 webSettings->fontSize(QWebSettings::DefaultFontSize));
170void HelpViewerImpl::setViewerFont(
const QFont &font)
173 QWebSettings *webSettings = settings();
174 webSettings->setFontFamily(QWebSettings::StandardFont, font.family());
175 webSettings->setFontSize(QWebSettings::DefaultFontSize, font.pointSize());
178void HelpViewerImpl::scaleUp()
181 setZoomFactor(zoomFactor() + 0.1);
184void HelpViewerImpl::scaleDown()
187 setZoomFactor(qMax(0.0, zoomFactor() - 0.1));
190void HelpViewerImpl::resetScale()
193 setZoomFactor(d->webDpiRatio);
196qreal HelpViewerImpl::scale()
const
199 return zoomFactor() / d->webDpiRatio;
202QString HelpViewerImpl::title()
const
205 return QWebView::title();
208void HelpViewerImpl::setTitle(
const QString &title)
214QUrl HelpViewerImpl::source()
const
217 HelpPage *currentPage =
static_cast<HelpPage*> (page());
218 if (currentPage && !d->m_loadFinished) {
220 return currentPage->m_loadingUrl;
225void HelpViewerImpl::setSource(
const QUrl &url)
228 load(url.toString() == QLatin1String(
"help") ? LocalHelpFile : url);
231QString HelpViewerImpl::selectedText()
const
234 return QWebView::selectedText();
237bool HelpViewerImpl::isForwardAvailable()
const
240 return pageAction(QWebPage::Forward)->isEnabled();
243bool HelpViewerImpl::isBackwardAvailable()
const
246 return pageAction(QWebPage::Back)->isEnabled();
249bool HelpViewerImpl::findText(
const QString &text, HelpViewer::FindFlags flags,
bool incremental,
253 Q_UNUSED(incremental); Q_UNUSED(fromSearch);
254 QWebPage::FindFlags options = QWebPage::FindWrapsAroundDocument;
255 if (flags & HelpViewer::FindBackward)
256 options |= QWebPage::FindBackward;
257 if (flags & HelpViewer::FindCaseSensitively)
258 options |= QWebPage::FindCaseSensitively;
260 bool found = QWebView::findText(text, options);
261 options = QWebPage::HighlightAllOccurrences;
262 QWebView::findText(QString(), options);
263 QWebView::findText(text, options);
269#if QT_CONFIG(clipboard)
270void HelpViewerImpl::copy()
273 triggerPageAction(QWebPage::Copy);
277void HelpViewerImpl::forward()
283void HelpViewerImpl::backward()
291void HelpViewerImpl::keyPressEvent(QKeyEvent *e)
295#if QT_CONFIG(clipboard)
296 if (e->key() == Qt::Key_Insert && e->modifiers() == Qt::CTRL) {
297 if (!selectedText().isEmpty())
301 QWebView::keyPressEvent(e);
304void HelpViewerImpl::wheelEvent(QWheelEvent *event)
307 if (event->modifiers()& Qt::ControlModifier) {
309 event->delta() > 0 ? scaleUp() : scaleDown();
311 QWebView::wheelEvent(event);
315void HelpViewerImpl::mousePressEvent(QMouseEvent *event)
319 if (handleForwardBackwardMouseButtons(event))
323 if (HelpPage *currentPage =
static_cast<HelpPage*> (page())) {
324 currentPage->m_pressedButtons = event->buttons();
325 currentPage->m_keyboardModifiers = event->modifiers();
328 QWebView::mousePressEvent(event);
331void HelpViewerImpl::mouseReleaseEvent(QMouseEvent *event)
335 if (handleForwardBackwardMouseButtons(event))
339 QWebView::mouseReleaseEvent(event);
344void HelpViewerImpl::actionChanged()
347 QAction *a = qobject_cast<QAction *>(sender());
348 if (a == pageAction(QWebPage::Copy))
349 emit copyAvailable(a->isEnabled());
350 else if (a == pageAction(QWebPage::Back))
351 emit backwardAvailable(a->isEnabled());
352 else if (a == pageAction(QWebPage::Forward))
353 emit forwardAvailable(a->isEnabled());
358bool HelpViewerImpl::eventFilter(QObject *obj, QEvent *event)
361 return QWebView::eventFilter(obj, event);
364void HelpViewerImpl::contextMenuEvent(QContextMenuEvent *event)
367 QWebView::contextMenuEvent(event);
virtual void triggerAction(WebAction action, bool checked=false)
HelpPage(QObject *parent)
virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type)
virtual QWebPage * createWindow(QWebPage::WebWindowType)
HelpViewer * createBlankPage()
static OpenPagesManager * instance()
Combined button and popup list for selecting options.