2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-03-29 19:16:23 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-03-29 19:16:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-03-29 19:16:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-03-29 19:16:23 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
#include "qtwebkithelpviewer.h"
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
#if !defined(QT_NO_WEBKIT)
|
|
|
|
|
|
|
|
|
|
#include "helpconstants.h"
|
2010-11-16 14:15:23 +01:00
|
|
|
#include "localhelpmanager.h"
|
2010-03-29 19:16:23 +02:00
|
|
|
#include "openpagesmanager.h"
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
#include <coreplugin/find/findplugin.h>
|
2012-08-23 15:53:58 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2014-05-02 16:54:28 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2012-08-23 15:53:58 +02:00
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
#include <QAction>
|
2012-05-22 14:36:18 +02:00
|
|
|
#include <QDebug>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QString>
|
|
|
|
|
#include <QStringBuilder>
|
|
|
|
|
#include <QTimer>
|
2014-05-02 16:54:28 +02:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QWebElement>
|
2012-05-22 14:36:18 +02:00
|
|
|
#include <QWebFrame>
|
2014-05-02 16:54:28 +02:00
|
|
|
#include <QWebHistory>
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QApplication>
|
2012-05-22 14:36:18 +02:00
|
|
|
#include <QDesktopServices>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QWheelEvent>
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QHelpEngine>
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QNetworkReply>
|
|
|
|
|
#include <QNetworkRequest>
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2011-07-15 15:24:43 +02:00
|
|
|
#include <utils/networkaccessmanager.h>
|
2010-11-11 16:49:17 +01:00
|
|
|
|
2012-09-20 14:25:01 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
using namespace Core;
|
2010-03-29 19:16:23 +02:00
|
|
|
using namespace Help;
|
|
|
|
|
using namespace Help::Internal;
|
|
|
|
|
|
|
|
|
|
// -- HelpNetworkReply
|
|
|
|
|
|
|
|
|
|
class HelpNetworkReply : public QNetworkReply
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
HelpNetworkReply(const QNetworkRequest &request, const QByteArray &fileData,
|
|
|
|
|
const QString &mimeType);
|
|
|
|
|
|
2011-01-11 16:10:09 +01:00
|
|
|
virtual void abort() {}
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
virtual qint64 bytesAvailable() const
|
|
|
|
|
{ return data.length() + QNetworkReply::bytesAvailable(); }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual qint64 readData(char *data, qint64 maxlen);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QByteArray data;
|
2011-01-11 16:10:09 +01:00
|
|
|
qint64 dataLength;
|
2010-03-29 19:16:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
HelpNetworkReply::HelpNetworkReply(const QNetworkRequest &request,
|
|
|
|
|
const QByteArray &fileData, const QString& mimeType)
|
2011-01-11 16:10:09 +01:00
|
|
|
: data(fileData)
|
|
|
|
|
, dataLength(fileData.length())
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
|
|
|
|
setRequest(request);
|
2012-11-08 12:17:39 +01:00
|
|
|
setUrl(request.url());
|
2010-03-29 19:16:23 +02:00
|
|
|
setOpenMode(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
|
|
setHeader(QNetworkRequest::ContentTypeHeader, mimeType);
|
2011-01-11 16:10:09 +01:00
|
|
|
setHeader(QNetworkRequest::ContentLengthHeader, QByteArray::number(dataLength));
|
2010-03-29 19:16:23 +02:00
|
|
|
QTimer::singleShot(0, this, SIGNAL(metaDataChanged()));
|
|
|
|
|
QTimer::singleShot(0, this, SIGNAL(readyRead()));
|
2012-03-28 16:29:23 +02:00
|
|
|
QTimer::singleShot(0, this, SIGNAL(finished()));
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 HelpNetworkReply::readData(char *buffer, qint64 maxlen)
|
|
|
|
|
{
|
|
|
|
|
qint64 len = qMin(qint64(data.length()), maxlen);
|
|
|
|
|
if (len) {
|
2012-09-20 14:25:01 +02:00
|
|
|
std::memcpy(buffer, data.constData(), len);
|
2010-03-29 19:16:23 +02:00
|
|
|
data.remove(0, len);
|
|
|
|
|
}
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- HelpNetworkAccessManager
|
|
|
|
|
|
2011-07-15 15:24:43 +02:00
|
|
|
class HelpNetworkAccessManager : public Utils::NetworkAccessManager
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
HelpNetworkAccessManager(QObject *parent);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual QNetworkReply *createRequest(Operation op,
|
|
|
|
|
const QNetworkRequest &request, QIODevice *outgoingData = 0);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
HelpNetworkAccessManager::HelpNetworkAccessManager(QObject *parent)
|
2011-07-15 15:24:43 +02:00
|
|
|
: Utils::NetworkAccessManager(parent)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-14 14:31:34 +02:00
|
|
|
QNetworkReply *HelpNetworkAccessManager::createRequest(Operation op,
|
|
|
|
|
const QNetworkRequest &request, QIODevice* outgoingData)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2010-08-16 15:20:09 +02:00
|
|
|
if (!HelpViewer::isLocalUrl(request.url()))
|
2011-07-15 15:24:43 +02:00
|
|
|
return Utils::NetworkAccessManager::createRequest(op, request, outgoingData);
|
2010-06-14 14:31:34 +02:00
|
|
|
|
2014-06-17 15:37:40 +02:00
|
|
|
LocalHelpManager::HelpData data = LocalHelpManager::helpData(request.url());
|
|
|
|
|
return new HelpNetworkReply(request, data.data, data.mimeType);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-22 14:36:18 +02:00
|
|
|
// - HelpPage
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
HelpPage::HelpPage(QObject *parent)
|
|
|
|
|
: QWebPage(parent)
|
|
|
|
|
, closeNewTabIfNeeded(false)
|
|
|
|
|
, m_pressedButtons(Qt::NoButton)
|
|
|
|
|
, m_keyboardModifiers(Qt::NoModifier)
|
|
|
|
|
{
|
2012-05-22 14:36:18 +02:00
|
|
|
setForwardUnsupportedContent(true);
|
|
|
|
|
connect(this, SIGNAL(unsupportedContent(QNetworkReply*)), this,
|
|
|
|
|
SLOT(onHandleUnsupportedContent(QNetworkReply*)));
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWebPage *HelpPage::createWindow(QWebPage::WebWindowType)
|
|
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
// TODO: ensure that we'll get a QtWebKitHelpViewer here
|
|
|
|
|
QtWebKitHelpViewer* viewer = static_cast<QtWebKitHelpViewer *>(OpenPagesManager::instance()
|
|
|
|
|
.createPage());
|
|
|
|
|
HelpPage *newPage = viewer->page();
|
2010-03-29 19:16:23 +02:00
|
|
|
newPage->closeNewTabIfNeeded = closeNewTabIfNeeded;
|
|
|
|
|
closeNewTabIfNeeded = false;
|
|
|
|
|
return newPage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpPage::triggerAction(WebAction action, bool checked)
|
|
|
|
|
{
|
|
|
|
|
switch (action) {
|
|
|
|
|
case OpenLinkInNewWindow:
|
|
|
|
|
closeNewTabIfNeeded = true;
|
|
|
|
|
default: // fall through
|
|
|
|
|
QWebPage::triggerAction(action, checked);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-22 14:36:18 +02:00
|
|
|
bool HelpPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request,
|
|
|
|
|
QWebPage::NavigationType type)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
|
|
|
|
const bool closeNewTab = closeNewTabIfNeeded;
|
|
|
|
|
closeNewTabIfNeeded = false;
|
|
|
|
|
|
|
|
|
|
const QUrl &url = request.url();
|
|
|
|
|
if (HelpViewer::launchWithExternalApp(url)) {
|
|
|
|
|
if (closeNewTab)
|
|
|
|
|
QMetaObject::invokeMethod(&OpenPagesManager::instance(), "closeCurrentPage");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == QWebPage::NavigationTypeLinkClicked
|
|
|
|
|
&& (m_keyboardModifiers & Qt::ControlModifier || m_pressedButtons == Qt::MidButton)) {
|
|
|
|
|
m_pressedButtons = Qt::NoButton;
|
|
|
|
|
m_keyboardModifiers = Qt::NoModifier;
|
|
|
|
|
OpenPagesManager::instance().createPage(url);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-22 14:36:18 +02:00
|
|
|
if (frame == mainFrame())
|
|
|
|
|
m_loadingUrl = request.url();
|
|
|
|
|
|
2010-03-29 19:16:23 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-22 14:36:18 +02:00
|
|
|
void HelpPage::onHandleUnsupportedContent(QNetworkReply *reply)
|
|
|
|
|
{
|
|
|
|
|
// sub resource of this page
|
|
|
|
|
if (m_loadingUrl != reply->url()) {
|
|
|
|
|
qWarning() << "Resource" << reply->url().toEncoded() << "has unknown Content-Type, will be ignored.";
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set a default error string we are going to display
|
2014-06-17 15:37:40 +02:00
|
|
|
QString errorString = HelpViewer::tr("Unknown or unsupported content!");
|
2012-05-22 14:36:18 +02:00
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
|
// try to open the url using using the desktop service
|
|
|
|
|
if (QDesktopServices::openUrl(reply->url())) {
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// seems we failed, now we show the error page inside creator
|
|
|
|
|
} else {
|
|
|
|
|
errorString = reply->errorString();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-17 15:37:40 +02:00
|
|
|
const QString html = QString::fromUtf8(LocalHelpManager::loadErrorMessage(reply->url(),
|
|
|
|
|
errorString));
|
2012-05-22 14:36:18 +02:00
|
|
|
|
|
|
|
|
// update the current layout
|
|
|
|
|
QList<QWebFrame*> frames;
|
|
|
|
|
frames.append(mainFrame());
|
|
|
|
|
while (!frames.isEmpty()) {
|
|
|
|
|
QWebFrame *frame = frames.takeFirst();
|
|
|
|
|
if (frame->url() == reply->url()) {
|
|
|
|
|
frame->setHtml(html, reply->url());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QWebFrame *> children = frame->childFrames();
|
|
|
|
|
foreach (QWebFrame *frame, children)
|
|
|
|
|
frames.append(frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_loadingUrl == reply->url())
|
|
|
|
|
mainFrame()->setHtml(html, reply->url());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-03-29 19:16:23 +02:00
|
|
|
// -- HelpViewer
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
QtWebKitHelpWidget::QtWebKitHelpWidget(qreal zoom, QtWebKitHelpViewer *parent)
|
|
|
|
|
: QWebView(parent),
|
|
|
|
|
m_parent(parent)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
|
|
|
|
setAcceptDrops(false);
|
2010-09-22 15:01:33 +02:00
|
|
|
installEventFilter(this);
|
|
|
|
|
|
2012-05-22 14:36:18 +02:00
|
|
|
QWebSettings::globalSettings()->setAttribute(QWebSettings::DnsPrefetchEnabled, true);
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
setPage(new HelpPage(this));
|
2011-01-11 16:10:22 +01:00
|
|
|
HelpNetworkAccessManager *manager = new HelpNetworkAccessManager(this);
|
|
|
|
|
page()->setNetworkAccessManager(manager);
|
|
|
|
|
connect(manager, SIGNAL(finished(QNetworkReply*)), this,
|
|
|
|
|
SLOT(slotNetworkReplyFinished(QNetworkReply*)));
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
QAction* action = pageAction(QWebPage::OpenLinkInNewWindow);
|
|
|
|
|
action->setText(tr("Open Link as New Page"));
|
|
|
|
|
|
|
|
|
|
pageAction(QWebPage::DownloadLinkToDisk)->setVisible(false);
|
|
|
|
|
pageAction(QWebPage::DownloadImageToDisk)->setVisible(false);
|
|
|
|
|
pageAction(QWebPage::OpenImageInNewWindow)->setVisible(false);
|
|
|
|
|
|
|
|
|
|
connect(pageAction(QWebPage::Copy), SIGNAL(changed()), this,
|
|
|
|
|
SLOT(actionChanged()));
|
|
|
|
|
connect(pageAction(QWebPage::Back), SIGNAL(changed()), this,
|
|
|
|
|
SLOT(actionChanged()));
|
|
|
|
|
connect(pageAction(QWebPage::Forward), SIGNAL(changed()), this,
|
|
|
|
|
SLOT(actionChanged()));
|
|
|
|
|
|
2012-11-07 16:53:00 +01:00
|
|
|
setZoomFactor(zoom == 0.0 ? 1.0 : zoom);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
QtWebKitHelpWidget::~QtWebKitHelpWidget()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::scaleUp()
|
|
|
|
|
{
|
|
|
|
|
setZoomFactor(zoomFactor() + 0.1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::scaleDown()
|
|
|
|
|
{
|
|
|
|
|
setZoomFactor(qMax(qreal(0.0), zoomFactor() - qreal(0.1)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- public slots
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::copy()
|
|
|
|
|
{
|
|
|
|
|
triggerPageAction(QWebPage::Copy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- protected
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::keyPressEvent(QKeyEvent *e)
|
|
|
|
|
{
|
|
|
|
|
// TODO: remove this once we support multiple keysequences per command
|
|
|
|
|
if (e->key() == Qt::Key_Insert && e->modifiers() == Qt::CTRL) {
|
|
|
|
|
if (!selectedText().isEmpty())
|
|
|
|
|
copy();
|
|
|
|
|
}
|
|
|
|
|
QWebView::keyPressEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::wheelEvent(QWheelEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->modifiers()& Qt::ControlModifier) {
|
|
|
|
|
event->accept();
|
|
|
|
|
event->delta() > 0 ? scaleUp() : scaleDown();
|
|
|
|
|
} else {
|
|
|
|
|
QWebView::wheelEvent(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (Utils::HostOsInfo::isLinuxHost() && m_parent->handleForwardBackwardMouseButtons(event))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (HelpPage *currentPage = static_cast<HelpPage*>(page())) {
|
|
|
|
|
currentPage->m_pressedButtons = event->buttons();
|
|
|
|
|
currentPage->m_keyboardModifiers = event->modifiers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWebView::mousePressEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (!Utils::HostOsInfo::isLinuxHost() && m_parent->handleForwardBackwardMouseButtons(event))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QWebView::mouseReleaseEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- private slots
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::actionChanged()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
QAction *a = qobject_cast<QAction *>(sender());
|
|
|
|
|
if (a == pageAction(QWebPage::Back))
|
|
|
|
|
emit backwardAvailable(a->isEnabled());
|
|
|
|
|
else if (a == pageAction(QWebPage::Forward))
|
|
|
|
|
emit forwardAvailable(a->isEnabled());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpWidget::slotNetworkReplyFinished(QNetworkReply *reply)
|
|
|
|
|
{
|
|
|
|
|
if (reply && reply->error() != QNetworkReply::NoError) {
|
|
|
|
|
load(QUrl(Help::Constants::AboutBlank));
|
2014-06-17 15:37:40 +02:00
|
|
|
setHtml(QString::fromUtf8(LocalHelpManager::loadErrorMessage(reply->url(),
|
|
|
|
|
reply->errorString())));
|
2014-05-02 16:54:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- private
|
|
|
|
|
|
|
|
|
|
bool QtWebKitHelpWidget::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::KeyPress) {
|
|
|
|
|
if (QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event)) {
|
|
|
|
|
if (keyEvent->key() == Qt::Key_Slash)
|
|
|
|
|
Core::FindPlugin::instance()->openFindToolBar(Core::FindPlugin::FindForwardDirection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QWebView::eventFilter(obj, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QtWebKitHelpViewer::QtWebKitHelpViewer(qreal zoom, QWidget *parent)
|
|
|
|
|
: HelpViewer(parent),
|
|
|
|
|
m_webView(new QtWebKitHelpWidget(zoom, this))
|
|
|
|
|
{
|
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout;
|
|
|
|
|
setLayout(layout);
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
layout->addWidget(m_webView, 10);
|
|
|
|
|
|
|
|
|
|
connect(m_webView, SIGNAL(urlChanged(QUrl)), this, SIGNAL(sourceChanged(QUrl)));
|
|
|
|
|
connect(m_webView, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted()));
|
|
|
|
|
connect(m_webView, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished()));
|
|
|
|
|
connect(m_webView, SIGNAL(titleChanged(QString)), this, SIGNAL(titleChanged()));
|
|
|
|
|
connect(m_webView->page(), SIGNAL(printRequested(QWebFrame*)), this, SIGNAL(printRequested()));
|
|
|
|
|
connect(m_webView, SIGNAL(backwardAvailable(bool)), this, SIGNAL(backwardAvailable(bool)));
|
|
|
|
|
connect(m_webView, SIGNAL(forwardAvailable(bool)), this, SIGNAL(forwardAvailable(bool)));
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
QFont QtWebKitHelpViewer::viewerFont() const
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-14 13:15:01 +02:00
|
|
|
QWebSettings* webSettings = m_webView->settings();
|
|
|
|
|
return QFont(webSettings->fontFamily(QWebSettings::StandardFont),
|
2010-03-29 19:16:23 +02:00
|
|
|
webSettings->fontSize(QWebSettings::DefaultFontSize));
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::setViewerFont(const QFont &font)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
QWebSettings *webSettings = m_webView->settings();
|
2010-03-29 19:16:23 +02:00
|
|
|
webSettings->setFontFamily(QWebSettings::StandardFont, font.family());
|
|
|
|
|
webSettings->setFontSize(QWebSettings::DefaultFontSize, font.pointSize());
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::scaleUp()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->scaleUp();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::scaleDown()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->scaleDown();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::resetScale()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->setZoomFactor(1.0);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
qreal QtWebKitHelpViewer::scale() const
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
return m_webView->zoomFactor();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
QString QtWebKitHelpViewer::title() const
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
return m_webView->title();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
QUrl QtWebKitHelpViewer::source() const
|
|
|
|
|
{
|
|
|
|
|
return m_webView->url();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpViewer::setSource(const QUrl &url)
|
|
|
|
|
{
|
2014-06-04 13:41:32 +02:00
|
|
|
QUrl oldWithoutFragment = source();
|
|
|
|
|
oldWithoutFragment.setFragment(QString());
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->load(url);
|
2014-06-04 13:41:32 +02:00
|
|
|
|
|
|
|
|
// if the new url only changes the anchor,
|
|
|
|
|
// then webkit does not send loadStarted nor loadFinished,
|
|
|
|
|
// so we should do that manually in that case
|
|
|
|
|
QUrl newWithoutFragment = url;
|
|
|
|
|
newWithoutFragment.setFragment(QString());
|
|
|
|
|
if (oldWithoutFragment == newWithoutFragment) {
|
|
|
|
|
slotLoadStarted();
|
|
|
|
|
slotLoadFinished();
|
|
|
|
|
}
|
2014-05-02 16:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpViewer::scrollToAnchor(const QString &anchor)
|
|
|
|
|
{
|
|
|
|
|
m_webView->page()->mainFrame()->scrollToAnchor(anchor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpViewer::highlightId(const QString &id)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-06-04 13:41:32 +02:00
|
|
|
if (m_oldHighlightId == id)
|
|
|
|
|
return;
|
2014-05-02 16:54:28 +02:00
|
|
|
const QWebElement &document = m_webView->page()->mainFrame()->documentElement();
|
|
|
|
|
const QWebElementCollection &collection = document.findAll(QLatin1String("h3.fn a"));
|
|
|
|
|
|
|
|
|
|
const QLatin1String property("background-color");
|
|
|
|
|
foreach (const QWebElement &element, collection) {
|
|
|
|
|
const QString &name = element.attribute(QLatin1String("name"));
|
|
|
|
|
if (name.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (m_oldHighlightId == name
|
|
|
|
|
|| name.startsWith(m_oldHighlightId + QLatin1Char('-'))) {
|
|
|
|
|
QWebElement parent = element.parent();
|
|
|
|
|
parent.setStyleProperty(property, m_oldHighlightStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id == name
|
|
|
|
|
|| name.startsWith(id + QLatin1Char('-'))) {
|
|
|
|
|
QWebElement parent = element.parent();
|
|
|
|
|
m_oldHighlightStyle = parent.styleProperty(property,
|
|
|
|
|
QWebElement::ComputedStyle);
|
|
|
|
|
parent.setStyleProperty(property, QLatin1String("yellow"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_oldHighlightId = id;
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::setHtml(const QString &html)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->setHtml(html);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
QString QtWebKitHelpViewer::selectedText() const
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
return m_webView->selectedText();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
bool QtWebKitHelpViewer::isForwardAvailable() const
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
return m_webView->pageAction(QWebPage::Forward)->isEnabled();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
bool QtWebKitHelpViewer::isBackwardAvailable() const
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
return m_webView->pageAction(QWebPage::Back)->isEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpViewer::addBackHistoryItems(QMenu *backMenu)
|
|
|
|
|
{
|
|
|
|
|
if (QWebHistory *history = m_webView->history()) {
|
|
|
|
|
QList<QWebHistoryItem> items = history->backItems(history->count());
|
|
|
|
|
for (int i = items.count() - 1; i >= 0; --i) {
|
|
|
|
|
QAction *action = new QAction(backMenu);
|
|
|
|
|
action->setText(items.at(i).title());
|
|
|
|
|
action->setData(i);
|
|
|
|
|
connect(action, SIGNAL(triggered()), this, SLOT(goToBackHistoryItem()));
|
|
|
|
|
backMenu->addAction(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::addForwardHistoryItems(QMenu *forwardMenu)
|
|
|
|
|
{
|
|
|
|
|
if (QWebHistory *history = m_webView->history()) {
|
|
|
|
|
QList<QWebHistoryItem> items = history->forwardItems(history->count());
|
|
|
|
|
for (int i = 0; i < items.count(); ++i) {
|
|
|
|
|
QAction *action = new QAction(forwardMenu);
|
|
|
|
|
action->setText(items.at(i).title());
|
|
|
|
|
action->setData(i);
|
|
|
|
|
connect(action, SIGNAL(triggered()), this, SLOT(goToForwardHistoryItem()));
|
|
|
|
|
forwardMenu->addAction(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtWebKitHelpViewer::setOpenInNewWindowActionVisible(bool visible)
|
|
|
|
|
{
|
|
|
|
|
m_webView->pageAction(QWebPage::OpenLinkInNewWindow)->setVisible(visible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QtWebKitHelpViewer::findText(const QString &text, Core::FindFlags flags,
|
2011-01-14 11:19:57 +01:00
|
|
|
bool incremental, bool fromSearch, bool *wrapped)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2010-08-11 21:13:44 +02:00
|
|
|
Q_UNUSED(incremental);
|
|
|
|
|
Q_UNUSED(fromSearch);
|
2011-01-14 11:19:57 +01:00
|
|
|
if (wrapped)
|
|
|
|
|
*wrapped = false;
|
|
|
|
|
QWebPage::FindFlags options;
|
2014-01-13 16:17:34 +01:00
|
|
|
if (flags & Core::FindBackward)
|
2010-03-29 19:16:23 +02:00
|
|
|
options |= QWebPage::FindBackward;
|
2014-01-13 16:17:34 +01:00
|
|
|
if (flags & Core::FindCaseSensitively)
|
2010-03-29 19:16:23 +02:00
|
|
|
options |= QWebPage::FindCaseSensitively;
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
bool found = m_webView->findText(text, options);
|
2011-01-14 11:19:57 +01:00
|
|
|
if (!found) {
|
|
|
|
|
options |= QWebPage::FindWrapsAroundDocument;
|
2014-05-02 16:54:28 +02:00
|
|
|
found = m_webView->findText(text, options);
|
2011-01-14 11:19:57 +01:00
|
|
|
if (found && wrapped)
|
|
|
|
|
*wrapped = true;
|
|
|
|
|
}
|
2010-03-29 19:16:23 +02:00
|
|
|
options = QWebPage::HighlightAllOccurrences;
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->findText(QLatin1String(""), options); // clear first
|
|
|
|
|
m_webView->findText(text, options); // force highlighting of all other matches
|
2010-03-29 19:16:23 +02:00
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
HelpPage *QtWebKitHelpViewer::page() const
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
return static_cast<HelpPage *>(m_webView->page());
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::copy()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->copy();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::stop()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->triggerPageAction(QWebPage::Stop);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::forward()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->forward();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::backward()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->back();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::print(QPrinter *printer)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
m_webView->print(printer);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::goToBackHistoryItem()
|
2011-01-11 16:10:22 +01:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
goToHistoryItem(/*forward=*/false);
|
2011-01-11 16:10:22 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::goToForwardHistoryItem()
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
goToHistoryItem(/*forward=*/true);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void QtWebKitHelpViewer::goToHistoryItem(bool forward)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
2014-05-02 16:54:28 +02:00
|
|
|
QAction *action = qobject_cast<QAction *>(sender());
|
|
|
|
|
QTC_ASSERT(action, return);
|
|
|
|
|
QWebHistory *history = m_webView->history();
|
|
|
|
|
QTC_ASSERT(history, return);
|
|
|
|
|
bool ok = false;
|
|
|
|
|
int index = action->data().toInt(&ok);
|
|
|
|
|
QTC_ASSERT(ok, return);
|
|
|
|
|
if (forward)
|
|
|
|
|
history->goToItem(history->forwardItems(history->count()).at(index));
|
|
|
|
|
else
|
|
|
|
|
history->goToItem(history->backItems(history->count()).at(index));
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // !QT_NO_WEBKIT
|