2010-03-29 19:16:23 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-03-29 19:16:23 +02:00
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2010-03-29 19:16:23 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2010-03-29 19:16:23 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-03-29 19:16:23 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "helpviewer.h"
|
|
|
|
|
|
|
|
|
|
#if defined(QT_NO_WEBKIT)
|
|
|
|
|
|
|
|
|
|
#include "helpconstants.h"
|
|
|
|
|
#include "helpviewer_p.h"
|
2010-11-16 14:15:23 +01:00
|
|
|
#include "localhelpmanager.h"
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
|
#include <QtGui/QClipboard>
|
|
|
|
|
#include <QtGui/QContextMenuEvent>
|
|
|
|
|
#include <QtGui/QKeyEvent>
|
|
|
|
|
#include <QtGui/QMenu>
|
|
|
|
|
|
2010-06-14 16:57:26 +02:00
|
|
|
#include <QtHelp/QHelpEngine>
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
using namespace Find;
|
|
|
|
|
using namespace Help;
|
|
|
|
|
using namespace Help::Internal;
|
|
|
|
|
|
|
|
|
|
// -- HelpViewer
|
|
|
|
|
|
|
|
|
|
HelpViewer::HelpViewer(qreal zoom, QWidget *parent)
|
|
|
|
|
: QTextBrowser(parent)
|
|
|
|
|
, d(new HelpViewerPrivate(zoom))
|
|
|
|
|
{
|
2010-03-30 16:10:05 +02:00
|
|
|
QPalette p = palette();
|
|
|
|
|
p.setColor(QPalette::Inactive, QPalette::Highlight,
|
|
|
|
|
p.color(QPalette::Active, QPalette::Highlight));
|
|
|
|
|
p.setColor(QPalette::Inactive, QPalette::HighlightedText,
|
|
|
|
|
p.color(QPalette::Active, QPalette::HighlightedText));
|
|
|
|
|
setPalette(p);
|
|
|
|
|
|
2010-03-29 19:16:23 +02:00
|
|
|
installEventFilter(this);
|
|
|
|
|
document()->setDocumentMargin(8);
|
|
|
|
|
|
|
|
|
|
QFont font = viewerFont();
|
|
|
|
|
font.setPointSize(int(font.pointSize() + zoom));
|
|
|
|
|
setViewerFont(font);
|
|
|
|
|
|
|
|
|
|
connect(this, SIGNAL(sourceChanged(QUrl)), this, SIGNAL(titleChanged()));
|
2011-01-11 13:16:12 +01:00
|
|
|
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HelpViewer::~HelpViewer()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFont HelpViewer::viewerFont() const
|
|
|
|
|
{
|
2010-06-14 16:57:26 +02:00
|
|
|
const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
|
2010-03-29 19:16:23 +02:00
|
|
|
return qVariantValue<QFont>(engine.customValue(QLatin1String("font"),
|
|
|
|
|
qApp->font()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::setViewerFont(const QFont &newFont)
|
|
|
|
|
{
|
|
|
|
|
if (font() != newFont) {
|
|
|
|
|
d->forceFont = true;
|
|
|
|
|
setFont(newFont);
|
|
|
|
|
d->forceFont = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::scaleUp()
|
|
|
|
|
{
|
|
|
|
|
if (d->zoomCount < 10) {
|
|
|
|
|
d->zoomCount++;
|
|
|
|
|
d->forceFont = true;
|
|
|
|
|
zoomIn();
|
|
|
|
|
d->forceFont = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::scaleDown()
|
|
|
|
|
{
|
|
|
|
|
if (d->zoomCount > -5) {
|
|
|
|
|
d->zoomCount--;
|
|
|
|
|
d->forceFont = true;
|
|
|
|
|
zoomOut();
|
|
|
|
|
d->forceFont = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::resetScale()
|
|
|
|
|
{
|
|
|
|
|
if (d->zoomCount != 0) {
|
|
|
|
|
d->forceFont = true;
|
|
|
|
|
zoomOut(d->zoomCount);
|
|
|
|
|
d->forceFont = false;
|
|
|
|
|
}
|
|
|
|
|
d->zoomCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qreal HelpViewer::scale() const
|
|
|
|
|
{
|
|
|
|
|
return d->zoomCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString HelpViewer::title() const
|
|
|
|
|
{
|
|
|
|
|
return documentTitle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::setTitle(const QString &title)
|
|
|
|
|
{
|
|
|
|
|
setDocumentTitle(title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QUrl HelpViewer::source() const
|
|
|
|
|
{
|
|
|
|
|
return QTextBrowser::source();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::setSource(const QUrl &url)
|
|
|
|
|
{
|
|
|
|
|
const QString &string = url.toString();
|
|
|
|
|
if (url.isValid() && string != QLatin1String("help")) {
|
|
|
|
|
if (launchWithExternalApp(url))
|
|
|
|
|
return;
|
|
|
|
|
|
2010-06-14 16:55:58 +02:00
|
|
|
QUrl resolvedUrl;
|
|
|
|
|
if (url.scheme() == QLatin1String("http"))
|
|
|
|
|
resolvedUrl = url;
|
|
|
|
|
|
|
|
|
|
if (!resolvedUrl.isValid()) {
|
|
|
|
|
const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
|
|
|
|
|
resolvedUrl = engine.findFile(url);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-29 19:16:23 +02:00
|
|
|
if (resolvedUrl.isValid()) {
|
|
|
|
|
QTextBrowser::setSource(resolvedUrl);
|
|
|
|
|
emit loadFinished(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextBrowser::setSource(url);
|
|
|
|
|
setHtml(string == Help::Constants::AboutBlank ? AboutBlankPage
|
|
|
|
|
: PageNotFoundMessage.arg(url.toString()));
|
|
|
|
|
emit loadFinished(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString HelpViewer::selectedText() const
|
|
|
|
|
{
|
|
|
|
|
return textCursor().selectedText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HelpViewer::isForwardAvailable() const
|
|
|
|
|
{
|
|
|
|
|
return QTextBrowser::isForwardAvailable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HelpViewer::isBackwardAvailable() const
|
|
|
|
|
{
|
|
|
|
|
return QTextBrowser::isBackwardAvailable();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-15 13:30:04 +02:00
|
|
|
bool HelpViewer::findText(const QString &text, Find::FindFlags flags,
|
2010-03-30 16:10:05 +02:00
|
|
|
bool incremental, bool fromSearch)
|
2010-03-29 19:16:23 +02:00
|
|
|
{
|
|
|
|
|
QTextDocument *doc = document();
|
|
|
|
|
QTextCursor cursor = textCursor();
|
|
|
|
|
if (!doc || cursor.isNull())
|
|
|
|
|
return false;
|
|
|
|
|
|
2010-03-30 16:10:05 +02:00
|
|
|
const int position = cursor.selectionStart();
|
2010-03-29 19:16:23 +02:00
|
|
|
if (incremental)
|
2010-03-30 16:10:05 +02:00
|
|
|
cursor.setPosition(position);
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2010-07-15 13:30:04 +02:00
|
|
|
QTextDocument::FindFlags f = Find::textDocumentFlagsForFindFlags(flags);
|
2010-03-29 19:16:23 +02:00
|
|
|
QTextCursor found = doc->find(text, cursor, f);
|
|
|
|
|
if (found.isNull()) {
|
2010-07-15 13:30:04 +02:00
|
|
|
if ((flags & Find::FindBackward) == 0)
|
2010-03-29 19:16:23 +02:00
|
|
|
cursor.movePosition(QTextCursor::Start);
|
|
|
|
|
else
|
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
|
found = doc->find(text, cursor, f);
|
2010-03-30 16:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fromSearch) {
|
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
|
viewport()->setUpdatesEnabled(false);
|
|
|
|
|
|
|
|
|
|
QTextCharFormat marker;
|
|
|
|
|
marker.setForeground(Qt::red);
|
|
|
|
|
cursor.movePosition(QTextCursor::Start);
|
|
|
|
|
setTextCursor(cursor);
|
|
|
|
|
|
|
|
|
|
while (find(text)) {
|
|
|
|
|
QTextCursor hit = textCursor();
|
|
|
|
|
hit.mergeCharFormat(marker);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
2010-03-30 16:10:05 +02:00
|
|
|
|
|
|
|
|
viewport()->setUpdatesEnabled(true);
|
|
|
|
|
cursor.endEditBlock();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
2010-03-30 16:10:05 +02:00
|
|
|
|
|
|
|
|
bool cursorIsNull = found.isNull();
|
|
|
|
|
if (cursorIsNull) {
|
|
|
|
|
found = textCursor();
|
|
|
|
|
found.setPosition(position);
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
2010-03-30 16:10:05 +02:00
|
|
|
setTextCursor(found);
|
2010-04-27 16:27:39 +02:00
|
|
|
return !cursorIsNull;
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- public slots
|
|
|
|
|
|
|
|
|
|
void HelpViewer::copy()
|
|
|
|
|
{
|
|
|
|
|
QTextBrowser::copy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::forward()
|
|
|
|
|
{
|
|
|
|
|
QTextBrowser::forward();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::backward()
|
|
|
|
|
{
|
|
|
|
|
QTextBrowser::backward();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- protected
|
|
|
|
|
|
|
|
|
|
void HelpViewer::keyPressEvent(QKeyEvent *e)
|
|
|
|
|
{
|
|
|
|
|
if ((e->key() == Qt::Key_Home && e->modifiers() != Qt::NoModifier)
|
|
|
|
|
|| (e->key() == Qt::Key_End && e->modifiers() != Qt::NoModifier)) {
|
|
|
|
|
QKeyEvent* event = new QKeyEvent(e->type(), e->key(), Qt::NoModifier,
|
|
|
|
|
e->text(), e->isAutoRepeat(), e->count());
|
|
|
|
|
e = event;
|
|
|
|
|
}
|
|
|
|
|
QTextBrowser::keyPressEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::wheelEvent(QWheelEvent *e)
|
|
|
|
|
{
|
|
|
|
|
if (e->modifiers() == Qt::ControlModifier) {
|
|
|
|
|
e->accept();
|
|
|
|
|
e->delta() > 0 ? scaleUp() : scaleDown();
|
|
|
|
|
} else {
|
|
|
|
|
QTextBrowser::wheelEvent(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::mousePressEvent(QMouseEvent *e)
|
|
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
if (handleForwardBackwardMouseButtons(e))
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
QTextBrowser::mousePressEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
|
{
|
|
|
|
|
#ifndef Q_OS_LINUX
|
|
|
|
|
if (handleForwardBackwardMouseButtons(e))
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
bool controlPressed = e->modifiers() & Qt::ControlModifier;
|
|
|
|
|
if ((controlPressed && d->hasAnchorAt(this, e->pos())) ||
|
|
|
|
|
(e->button() == Qt::MidButton && d->hasAnchorAt(this, e->pos()))) {
|
|
|
|
|
d->openLinkInNewPage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextBrowser::mouseReleaseEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- private slots
|
|
|
|
|
|
|
|
|
|
void HelpViewer::actionChanged()
|
|
|
|
|
{
|
|
|
|
|
// stub
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- private
|
|
|
|
|
|
|
|
|
|
bool HelpViewer::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::FontChange && !d->forceFont)
|
|
|
|
|
return true;
|
2010-09-22 15:01:33 +02:00
|
|
|
|
|
|
|
|
if (event->type() == QEvent::KeyPress) {
|
|
|
|
|
if (QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event)) {
|
|
|
|
|
if (keyEvent->key() == Qt::Key_Slash)
|
|
|
|
|
emit openFindToolBar();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-29 19:16:23 +02:00
|
|
|
return QTextBrowser::eventFilter(obj, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QMenu menu(QLatin1String(""), 0);
|
|
|
|
|
|
|
|
|
|
QUrl link;
|
|
|
|
|
QAction *copyAnchorAction = 0;
|
|
|
|
|
if (d->hasAnchorAt(this, event->pos())) {
|
|
|
|
|
link = anchorAt(event->pos());
|
|
|
|
|
if (link.isRelative())
|
|
|
|
|
link = source().resolved(link);
|
|
|
|
|
menu.addAction(tr("Open Link"), d, SLOT(openLink()));
|
|
|
|
|
menu.addAction(tr("Open Link as New Page"), d, SLOT(openLinkInNewPage()));
|
|
|
|
|
|
|
|
|
|
if (!link.isEmpty() && link.isValid())
|
|
|
|
|
copyAnchorAction = menu.addAction(tr("Copy Link"));
|
|
|
|
|
} else if (!selectedText().isEmpty()) {
|
|
|
|
|
menu.addAction(tr("Copy"), this, SLOT(copy()));
|
|
|
|
|
} else {
|
|
|
|
|
menu.addAction(tr("Reload"), this, SLOT(reload()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (copyAnchorAction == menu.exec(event->globalPos()))
|
|
|
|
|
QApplication::clipboard()->setText(link.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant HelpViewer::loadResource(int type, const QUrl &name)
|
|
|
|
|
{
|
|
|
|
|
QByteArray ba;
|
|
|
|
|
if (type < 4) {
|
2010-06-14 16:57:26 +02:00
|
|
|
const QHelpEngineCore &engine = LocalHelpManager::helpEngine();
|
2010-03-29 19:16:23 +02:00
|
|
|
ba = engine.fileData(name);
|
|
|
|
|
if (name.toString().endsWith(QLatin1String(".svg"), Qt::CaseInsensitive)) {
|
|
|
|
|
QImage image;
|
|
|
|
|
image.loadFromData(ba, "svg");
|
|
|
|
|
if (!image.isNull())
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ba;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // QT_NO_WEBKIT
|