From 03faf2750e02f78c5f3fd1589da00a6bcfa20e53 Mon Sep 17 00:00:00 2001 From: kh1 Date: Thu, 11 Feb 2010 12:13:14 +0100 Subject: Some more refactoring. Move the QTextBrowser based help viewer into it's own source files and introduce a common base clase that currently only contains the statics, but more to come for common stuff. Review: ck --- tools/assistant/tools/assistant/aboutdialog.cpp | 32 +-- tools/assistant/tools/assistant/assistant.pro | 6 +- tools/assistant/tools/assistant/centralwidget.cpp | 4 +- tools/assistant/tools/assistant/contentwindow.cpp | 5 +- tools/assistant/tools/assistant/helpviewer.cpp | 271 ++----------------- tools/assistant/tools/assistant/helpviewer.h | 61 +---- tools/assistant/tools/assistant/helpviewer_qtb.cpp | 292 +++++++++++++++++++++ tools/assistant/tools/assistant/helpviewer_qtb.h | 109 ++++++++ tools/assistant/tools/assistant/helpviewer_qwv.cpp | 23 +- tools/assistant/tools/assistant/helpviewer_qwv.h | 13 +- tools/assistant/tools/assistant/indexwindow.cpp | 3 +- tools/assistant/tools/assistant/mainwindow.cpp | 1 - 12 files changed, 462 insertions(+), 358 deletions(-) create mode 100644 tools/assistant/tools/assistant/helpviewer_qtb.cpp create mode 100644 tools/assistant/tools/assistant/helpviewer_qtb.h diff --git a/tools/assistant/tools/assistant/aboutdialog.cpp b/tools/assistant/tools/assistant/aboutdialog.cpp index c4f5fd2..a9bc352 100644 --- a/tools/assistant/tools/assistant/aboutdialog.cpp +++ b/tools/assistant/tools/assistant/aboutdialog.cpp @@ -39,7 +39,6 @@ ** ****************************************************************************/ #include "helpviewer.h" -#include "helpviewer_qwv.h" #include "tracer.h" #include @@ -71,7 +70,7 @@ void AboutLabel::setText(const QString &text, const QByteArray &resources) TRACE_OBJ QDataStream in(resources); in >> m_resourceMap; - + QTextBrowser::setText(text); } @@ -97,34 +96,34 @@ QVariant AboutLabel::loadResource(int type, const QUrl &name) void AboutLabel::setSource(const QUrl &url) { TRACE_OBJ - if (url.isValid() && (!HelpViewer::isLocalUrl(url) - || !HelpViewer::canOpenPage(url.path()))) { + if (url.isValid() && (!AbstractHelpViewer::isLocalUrl(url) + || !AbstractHelpViewer::canOpenPage(url.path()))) { if (!QDesktopServices::openUrl(url)) { QMessageBox::warning(this, tr("Warning"), - tr("Unable to launch external application.\n"), - tr("OK")); + tr("Unable to launch external application.\n"), tr("OK")); } } } AboutDialog::AboutDialog(QWidget *parent) - : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint|Qt::WindowTitleHint|Qt::WindowSystemMenuHint) + : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint | + Qt::WindowTitleHint|Qt::WindowSystemMenuHint) { TRACE_OBJ m_pixmapLabel = 0; m_aboutLabel = new AboutLabel(); - + m_closeButton = new QPushButton(); m_closeButton->setText(tr("&Close")); - connect(m_closeButton, SIGNAL(clicked()), - this, SLOT(close())); + connect(m_closeButton, SIGNAL(clicked()), this, SLOT(close())); m_layout = new QGridLayout(this); m_layout->addWidget(m_aboutLabel, 1, 0, 1, -1); - m_layout->addItem(new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Fixed), 2, 1, 1, 1); + m_layout->addItem(new QSpacerItem(20, 10, QSizePolicy::Minimum, + QSizePolicy::Fixed), 2, 1, 1, 1); m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 0, 1, 1); m_layout->addWidget(m_closeButton, 3, 1, 1, 1); - m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 2, 1, 1); + m_layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding), 3, 2, 1, 1); } void AboutDialog::setText(const QString &text, const QByteArray &resources) @@ -154,7 +153,8 @@ QString AboutDialog::documentTitle() const void AboutDialog::updateSize() { TRACE_OBJ - QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()).size(); + QSize screenSize = QApplication::desktop()->availableGeometry(QCursor::pos()) + .size(); int limit = qMin(screenSize.width()/2, 500); #ifdef Q_WS_MAC @@ -166,7 +166,7 @@ void AboutDialog::updateSize() if (width > limit) width = limit; - + QFontMetrics fm(qApp->font("QWorkspaceTitleBar")); int windowTitleWidth = qMin(fm.width(windowTitle()) + 50, limit); if (windowTitleWidth > width) @@ -174,8 +174,8 @@ void AboutDialog::updateSize() layout()->activate(); int height = (layout()->hasHeightForWidth()) - ? layout()->totalHeightForWidth(width) - : layout()->totalMinimumSize().height(); + ? layout()->totalHeightForWidth(width) + : layout()->totalMinimumSize().height(); setFixedSize(width, height); QCoreApplication::removePostedEvents(this, QEvent::LayoutRequest); } diff --git a/tools/assistant/tools/assistant/assistant.pro b/tools/assistant/tools/assistant/assistant.pro index 2ae03d4..0733128 100644 --- a/tools/assistant/tools/assistant/assistant.pro +++ b/tools/assistant/tools/assistant/assistant.pro @@ -28,8 +28,9 @@ HEADERS += aboutdialog.h \ findwidget.h \ filternamedialog.h \ helpenginewrapper.h \ - helpviewer_qwv.h \ helpviewer.h \ + helpviewer_qtb.h \ + helpviewer_qwv.h \ indexwindow.h \ installdialog.h \ mainwindow.h \ @@ -55,8 +56,9 @@ SOURCES += aboutdialog.cpp \ findwidget.cpp \ filternamedialog.cpp \ helpenginewrapper.cpp \ - helpviewer_qwv.cpp \ helpviewer.cpp \ + helpviewer_qtb.cpp \ + helpviewer_qwv.cpp \ indexwindow.cpp \ installdialog.cpp \ main.cpp \ diff --git a/tools/assistant/tools/assistant/centralwidget.cpp b/tools/assistant/tools/assistant/centralwidget.cpp index 41b0df4..c780df6 100644 --- a/tools/assistant/tools/assistant/centralwidget.cpp +++ b/tools/assistant/tools/assistant/centralwidget.cpp @@ -43,7 +43,7 @@ #include "centralwidget.h" #include "findwidget.h" #include "helpenginewrapper.h" -#include "helpviewer.h" +#include "helpviewer_qtb.h" #include "helpviewer_qwv.h" #include "searchwidget.h" #include "mainwindow.h" @@ -82,10 +82,8 @@ namespace { CentralWidget *staticCentralWidget = 0; } - // -- CentralWidget - CentralWidget::CentralWidget(MainWindow *parent) : QWidget(parent) , lastTabPage(0) diff --git a/tools/assistant/tools/assistant/contentwindow.cpp b/tools/assistant/tools/assistant/contentwindow.cpp index bfb88c7..e0347c8 100644 --- a/tools/assistant/tools/assistant/contentwindow.cpp +++ b/tools/assistant/tools/assistant/contentwindow.cpp @@ -44,7 +44,6 @@ #include "centralwidget.h" #include "helpenginewrapper.h" #include "helpviewer.h" -#include "helpviewer_qwv.h" #include #include @@ -145,7 +144,7 @@ bool ContentWindow::eventFilter(QObject *o, QEvent *e) qobject_cast(m_contentWidget->model()); if (contentModel) { QHelpContentItem *itm = contentModel->contentItemAt(index); - if (itm && HelpViewer::canOpenPage(itm->url().path())) + if (itm && AbstractHelpViewer::canOpenPage(itm->url().path())) CentralWidget::instance()->setSourceInNewTab(itm->url()); } } else if (button == Qt::LeftButton) { @@ -171,7 +170,7 @@ void ContentWindow::showContextMenu(const QPoint &pos) QMenu menu; QAction *curTab = menu.addAction(tr("Open Link")); QAction *newTab = menu.addAction(tr("Open Link in New Tab")); - if (!HelpViewer::canOpenPage(itm->url().path())) + if (!AbstractHelpViewer::canOpenPage(itm->url().path())) newTab->setEnabled(false); menu.move(m_contentWidget->mapToGlobal(pos)); diff --git a/tools/assistant/tools/assistant/helpviewer.cpp b/tools/assistant/tools/assistant/helpviewer.cpp index 0f198ac..8597f6b 100644 --- a/tools/assistant/tools/assistant/helpviewer.cpp +++ b/tools/assistant/tools/assistant/helpviewer.cpp @@ -39,281 +39,44 @@ ** ****************************************************************************/ #include "helpviewer.h" -#include "helpviewer_qwv.h" - -#include "centralwidget.h" -#include "helpenginewrapper.h" #include "tracer.h" -#include -#include - -#include -#include -#include -#include -#include -#include +#include +#include QT_BEGIN_NAMESPACE -bool HelpViewer::canOpenPage(const QString &url) -{ - TRACE_OBJ - return url.endsWith(QLatin1String(".html"), Qt::CaseInsensitive) - || url.endsWith(QLatin1String(".htm"), Qt::CaseInsensitive) - || url == QLatin1String("blank"); -} - -bool HelpViewer::isLocalUrl(const QUrl &url) -{ - TRACE_OBJ - const QString scheme = url.scheme(); - return scheme.isEmpty() - || scheme == QLatin1String("file") - || scheme == QLatin1String("qrc") - || scheme == QLatin1String("data") - || scheme == QLatin1String("qthelp") - || scheme == QLatin1String("about"); -} - -#if defined(QT_NO_WEBKIT) - -namespace { -const QString PageNotFoundMessage = +QString AbstractHelpViewer::PageNotFoundMessage = QCoreApplication::translate("HelpViewer", "Error 404...


The page could not be found


'%1'" "

"); -} - -HelpViewer::HelpViewer(CentralWidget *parent) - : QTextBrowser(parent) - , zoomCount(0) - , controlPressed(false) - , lastAnchor(QString()) - , parentWidget(parent) - , helpEngine(HelpEngineWrapper::instance()) -{ - TRACE_OBJ - document()->setDocumentMargin(8); -} - -void HelpViewer::setSource(const QUrl &url) -{ - TRACE_OBJ - bool help = url.toString() == QLatin1String("help"); - if (url.isValid() && !help) { - if (launchedWithExternalApp(url)) - return; - - QUrl u = helpEngine.findFile(url); - if (u.isValid()) { - QTextBrowser::setSource(u); - return; - } - } - - if (help) { - QTextBrowser::setSource(QUrl(QLatin1String("qthelp://com.trolltech.com." - "assistantinternal-1.0.0/assistant/assistant.html"))); - } else { - QTextBrowser::setSource(url); - setHtml(PageNotFoundMessage.arg(url.toString())); - emit sourceChanged(url); - } -} - -void HelpViewer::resetZoom() -{ - TRACE_OBJ - if (zoomCount == 0) - return; - - QTextBrowser::zoomOut(zoomCount); - zoomCount = 0; -} - -void HelpViewer::zoomIn(int range) -{ - TRACE_OBJ - if (zoomCount == 10) - return; - - QTextBrowser::zoomIn(range); - zoomCount++; -} - -void HelpViewer::zoomOut(int range) -{ - TRACE_OBJ - if (zoomCount == -5) - return; - - QTextBrowser::zoomOut(range); - zoomCount--; -} -bool HelpViewer::launchedWithExternalApp(const QUrl &url) +AbstractHelpViewer::AbstractHelpViewer() { - TRACE_OBJ - const bool canOpen = canOpenPage(url.path()); - if (!isLocalUrl(url) || !canOpen) { - bool launched = false; - if (!canOpen && url.scheme() == QLatin1String("qthelp")) { - const QString& path = url.path(); - const int lastDash = path.lastIndexOf(QChar('/')); - QString fileName = QDir::tempPath() + QDir::separator(); - if (lastDash < 0) - fileName += path; - else - fileName += path.mid(lastDash + 1, path.length()); - - QFile tmpFile(QDir::cleanPath(fileName)); - if (tmpFile.open(QIODevice::ReadWrite)) { - tmpFile.write(helpEngine.fileData(url)); - tmpFile.close(); - } - launched = QDesktopServices::openUrl(QUrl(tmpFile.fileName())); - } else { - launched = QDesktopServices::openUrl(url); - } - - if (!launched) { - QMessageBox::information(this, tr("Help"), - tr("Unable to launch external application.\n"), tr("OK")); - } - return true; - } - return false; -} - -QVariant HelpViewer::loadResource(int type, const QUrl &name) -{ - TRACE_OBJ - QByteArray ba; - if (type < 4) { - ba = helpEngine.fileData(name); - if (name.toString().endsWith(QLatin1String(".svg"), Qt::CaseInsensitive)) { - QImage image; - image.loadFromData(ba, "svg"); - if (!image.isNull()) - return image; - } - } - return ba; -} - -void HelpViewer::openLinkInNewTab() -{ - TRACE_OBJ - if(lastAnchor.isEmpty()) - return; - - parentWidget->setSourceInNewTab(QUrl(lastAnchor)); - lastAnchor.clear(); -} - -void HelpViewer::openLinkInNewTab(const QString &link) -{ - TRACE_OBJ - lastAnchor = link; - openLinkInNewTab(); -} - -bool HelpViewer::hasAnchorAt(const QPoint& pos) -{ - TRACE_OBJ - lastAnchor = anchorAt(pos); - if (lastAnchor.isEmpty()) - return false; - - lastAnchor = source().resolved(lastAnchor).toString(); - if (lastAnchor.at(0) == QLatin1Char('#')) { - QString src = source().toString(); - int hsh = src.indexOf(QLatin1Char('#')); - lastAnchor = (hsh>=0 ? src.left(hsh) : src) + lastAnchor; - } - - return true; -} - -void HelpViewer::contextMenuEvent(QContextMenuEvent *e) -{ - TRACE_OBJ - QMenu menu(QLatin1String(""), 0); - - QUrl link; - QAction *copyAnchorAction = 0; - if (hasAnchorAt(e->pos())) { - link = anchorAt(e->pos()); - if (link.isRelative()) - link = source().resolved(link); - copyAnchorAction = menu.addAction(tr("Copy &Link Location")); - copyAnchorAction->setEnabled(!link.isEmpty() && link.isValid()); - - menu.addAction(tr("Open Link in New Tab\tCtrl+LMB"), this, - SLOT(openLinkInNewTab())); - menu.addSeparator(); - } - menu.addActions(parentWidget->globalActions()); - QAction *action = menu.exec(e->globalPos()); - if (action == copyAnchorAction) - QApplication::clipboard()->setText(link.toString()); } -void HelpViewer::mouseReleaseEvent(QMouseEvent *e) +AbstractHelpViewer::~AbstractHelpViewer() { - TRACE_OBJ - if (e->button() == Qt::XButton1) { - QTextBrowser::backward(); - return; - } - - if (e->button() == Qt::XButton2) { - QTextBrowser::forward(); - return; - } - - controlPressed = e->modifiers() & Qt::ControlModifier; - if ((controlPressed && hasAnchorAt(e->pos())) || - (e->button() == Qt::MidButton && hasAnchorAt(e->pos()))) { - openLinkInNewTab(); - return; - } - - QTextBrowser::mouseReleaseEvent(e); } -void HelpViewer::keyPressEvent(QKeyEvent *e) +bool AbstractHelpViewer::isLocalUrl(const QUrl &url) { TRACE_OBJ - 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::home() -{ - TRACE_OBJ - setSource(helpEngine.homePage()); + const QString &scheme = url.scheme(); + return scheme.isEmpty() + || scheme == QLatin1String("file") + || scheme == QLatin1String("qrc") + || scheme == QLatin1String("data") + || scheme == QLatin1String("qthelp") + || scheme == QLatin1String("about"); } -void HelpViewer::wheelEvent(QWheelEvent *e) +bool AbstractHelpViewer::canOpenPage(const QString &url) { TRACE_OBJ - if (e->modifiers() == Qt::CTRL) { - e->accept(); - (e->delta() > 0) ? zoomIn() : zoomOut(); - } else { - e->ignore(); - QTextBrowser::wheelEvent(e); - } + return url.endsWith(QLatin1String(".html"), Qt::CaseInsensitive) + || url.endsWith(QLatin1String(".htm"), Qt::CaseInsensitive) + || url == QLatin1String("blank"); } -#endif - QT_END_NAMESPACE diff --git a/tools/assistant/tools/assistant/helpviewer.h b/tools/assistant/tools/assistant/helpviewer.h index da992fa..276cd1f 100644 --- a/tools/assistant/tools/assistant/helpviewer.h +++ b/tools/assistant/tools/assistant/helpviewer.h @@ -38,73 +38,26 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - #ifndef HELPVIEWER_H #define HELPVIEWER_H -#include -#include - -#if defined(QT_NO_WEBKIT) - -#include +#include QT_BEGIN_NAMESPACE -class CentralWidget; -class HelpEngineWrapper; -class QContextMenuEvent; -class QKeyEvent; -class QMouseEvent; +class QUrl; -class HelpViewer : public QTextBrowser +class AbstractHelpViewer { - Q_OBJECT - public: - HelpViewer(CentralWidget *parent); - void setSource(const QUrl &url); + AbstractHelpViewer(); + ~AbstractHelpViewer(); - void resetZoom(); - void zoomIn(int range = 1); - void zoomOut(int range = 1); - int zoom() const { return zoomCount; } - void setZoom(int zoom) { zoomCount = zoom; } - - inline bool hasSelection() const - { return textCursor().hasSelection(); } - - bool launchedWithExternalApp(const QUrl &url); - static bool canOpenPage(const QString &url); + static QString PageNotFoundMessage; static bool isLocalUrl(const QUrl &url); - -public Q_SLOTS: - void home(); - -protected: - void wheelEvent(QWheelEvent *e); - -private: - QVariant loadResource(int type, const QUrl &name); - void openLinkInNewTab(const QString &link); - bool hasAnchorAt(const QPoint& pos); - void contextMenuEvent(QContextMenuEvent *e); - void mouseReleaseEvent(QMouseEvent *e); - void keyPressEvent(QKeyEvent *e); - -private slots: - void openLinkInNewTab(); - -private: - int zoomCount; - bool controlPressed; - QString lastAnchor; - CentralWidget* parentWidget; - HelpEngineWrapper &helpEngine; + static bool canOpenPage(const QString &url); }; -#endif - QT_END_NAMESPACE #endif // HELPVIEWER_H diff --git a/tools/assistant/tools/assistant/helpviewer_qtb.cpp b/tools/assistant/tools/assistant/helpviewer_qtb.cpp new file mode 100644 index 0000000..dc35878 --- /dev/null +++ b/tools/assistant/tools/assistant/helpviewer_qtb.cpp @@ -0,0 +1,292 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** 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. +** +** 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. +** +** 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. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#if defined(QT_NO_WEBKIT) + +#include "helpviewer_qtb.h" +#include "helpviewer_qwv.h" + +#include "centralwidget.h" +#include "helpenginewrapper.h" +#include "tracer.h" + +#include +#include + +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +HelpViewer::HelpViewer(CentralWidget *parent) + : QTextBrowser(parent) + , zoomCount(0) + , controlPressed(false) + , lastAnchor(QString()) + , parentWidget(parent) + , helpEngine(HelpEngineWrapper::instance()) +{ + TRACE_OBJ + document()->setDocumentMargin(8); +} + +void HelpViewer::setSource(const QUrl &url) +{ + TRACE_OBJ + bool help = url.toString() == QLatin1String("help"); + if (url.isValid() && !help) { + if (launchedWithExternalApp(url)) + return; + + QUrl u = helpEngine.findFile(url); + if (u.isValid()) { + QTextBrowser::setSource(u); + return; + } + } + + if (help) { + QTextBrowser::setSource(QUrl(QLatin1String("qthelp://com.trolltech.com." + "assistantinternal-1.0.0/assistant/assistant.html"))); + } else { + QTextBrowser::setSource(url); + setHtml(PageNotFoundMessage.arg(url.toString())); + emit sourceChanged(url); + } +} + +void HelpViewer::resetZoom() +{ + TRACE_OBJ + if (zoomCount == 0) + return; + + QTextBrowser::zoomOut(zoomCount); + zoomCount = 0; +} + +void HelpViewer::zoomIn(int range) +{ + TRACE_OBJ + if (zoomCount == 10) + return; + + QTextBrowser::zoomIn(range); + zoomCount++; +} + +void HelpViewer::zoomOut(int range) +{ + TRACE_OBJ + if (zoomCount == -5) + return; + + QTextBrowser::zoomOut(range); + zoomCount--; +} + +bool HelpViewer::launchedWithExternalApp(const QUrl &url) +{ + TRACE_OBJ + const bool canOpen = canOpenPage(url.path()); + if (!isLocalUrl(url) || !canOpen) { + bool launched = false; + if (!canOpen && url.scheme() == QLatin1String("qthelp")) { + const QString& path = url.path(); + const int lastDash = path.lastIndexOf(QChar('/')); + QString fileName = QDir::tempPath() + QDir::separator(); + if (lastDash < 0) + fileName += path; + else + fileName += path.mid(lastDash + 1, path.length()); + + QFile tmpFile(QDir::cleanPath(fileName)); + if (tmpFile.open(QIODevice::ReadWrite)) { + tmpFile.write(helpEngine.fileData(url)); + tmpFile.close(); + } + launched = QDesktopServices::openUrl(QUrl(tmpFile.fileName())); + } else { + launched = QDesktopServices::openUrl(url); + } + + if (!launched) { + QMessageBox::information(this, tr("Help"), + tr("Unable to launch external application.\n"), tr("OK")); + } + return true; + } + return false; +} + +QVariant HelpViewer::loadResource(int type, const QUrl &name) +{ + TRACE_OBJ + QByteArray ba; + if (type < 4) { + ba = helpEngine.fileData(name); + if (name.toString().endsWith(QLatin1String(".svg"), Qt::CaseInsensitive)) { + QImage image; + image.loadFromData(ba, "svg"); + if (!image.isNull()) + return image; + } + } + return ba; +} + +void HelpViewer::openLinkInNewTab() +{ + TRACE_OBJ + if(lastAnchor.isEmpty()) + return; + + parentWidget->setSourceInNewTab(QUrl(lastAnchor)); + lastAnchor.clear(); +} + +void HelpViewer::openLinkInNewTab(const QString &link) +{ + TRACE_OBJ + lastAnchor = link; + openLinkInNewTab(); +} + +bool HelpViewer::hasAnchorAt(const QPoint& pos) +{ + TRACE_OBJ + lastAnchor = anchorAt(pos); + if (lastAnchor.isEmpty()) + return false; + + lastAnchor = source().resolved(lastAnchor).toString(); + if (lastAnchor.at(0) == QLatin1Char('#')) { + QString src = source().toString(); + int hsh = src.indexOf(QLatin1Char('#')); + lastAnchor = (hsh>=0 ? src.left(hsh) : src) + lastAnchor; + } + + return true; +} + +void HelpViewer::contextMenuEvent(QContextMenuEvent *e) +{ + TRACE_OBJ + QMenu menu(QLatin1String(""), 0); + + QUrl link; + QAction *copyAnchorAction = 0; + if (hasAnchorAt(e->pos())) { + link = anchorAt(e->pos()); + if (link.isRelative()) + link = source().resolved(link); + copyAnchorAction = menu.addAction(tr("Copy &Link Location")); + copyAnchorAction->setEnabled(!link.isEmpty() && link.isValid()); + + menu.addAction(tr("Open Link in New Tab\tCtrl+LMB"), this, + SLOT(openLinkInNewTab())); + menu.addSeparator(); + } + menu.addActions(parentWidget->globalActions()); + QAction *action = menu.exec(e->globalPos()); + if (action == copyAnchorAction) + QApplication::clipboard()->setText(link.toString()); +} + +void HelpViewer::mouseReleaseEvent(QMouseEvent *e) +{ + TRACE_OBJ + if (e->button() == Qt::XButton1) { + QTextBrowser::backward(); + return; + } + + if (e->button() == Qt::XButton2) { + QTextBrowser::forward(); + return; + } + + controlPressed = e->modifiers() & Qt::ControlModifier; + if ((controlPressed && hasAnchorAt(e->pos())) || + (e->button() == Qt::MidButton && hasAnchorAt(e->pos()))) { + openLinkInNewTab(); + return; + } + + QTextBrowser::mouseReleaseEvent(e); +} + +void HelpViewer::keyPressEvent(QKeyEvent *e) +{ + TRACE_OBJ + 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::home() +{ + TRACE_OBJ + setSource(helpEngine.homePage()); +} + +void HelpViewer::wheelEvent(QWheelEvent *e) +{ + TRACE_OBJ + if (e->modifiers() == Qt::CTRL) { + e->accept(); + (e->delta() > 0) ? zoomIn() : zoomOut(); + } else { + e->ignore(); + QTextBrowser::wheelEvent(e); + } +} + +QT_END_NAMESPACE + +#endif // QT_NO_WEBKIT diff --git a/tools/assistant/tools/assistant/helpviewer_qtb.h b/tools/assistant/tools/assistant/helpviewer_qtb.h new file mode 100644 index 0000000..09f5370 --- /dev/null +++ b/tools/assistant/tools/assistant/helpviewer_qtb.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** 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. +** +** 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. +** +** 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. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#if defined(QT_NO_WEBKIT) + +#ifndef HELPVIEWERQTB_H +#define HELPVIEWERQTB_H + +#include "helpviewer.h" + +#include +#include + +#include + +QT_BEGIN_NAMESPACE + +class CentralWidget; +class HelpEngineWrapper; +class QContextMenuEvent; +class QKeyEvent; +class QMouseEvent; + +class HelpViewer : public QTextBrowser, public AbstractHelpViewer +{ + Q_OBJECT + +public: + HelpViewer(CentralWidget *parent); + void setSource(const QUrl &url); + + void resetZoom(); + void zoomIn(int range = 1); + void zoomOut(int range = 1); + int zoom() const { return zoomCount; } + void setZoom(int zoom) { zoomCount = zoom; } + + inline bool hasSelection() const + { return textCursor().hasSelection(); } + + bool launchedWithExternalApp(const QUrl &url); + +public Q_SLOTS: + void home(); + +protected: + void wheelEvent(QWheelEvent *e); + +private: + QVariant loadResource(int type, const QUrl &name); + void openLinkInNewTab(const QString &link); + bool hasAnchorAt(const QPoint& pos); + void contextMenuEvent(QContextMenuEvent *e); + void mouseReleaseEvent(QMouseEvent *e); + void keyPressEvent(QKeyEvent *e); + +private slots: + void openLinkInNewTab(); + +private: + int zoomCount; + bool controlPressed; + QString lastAnchor; + CentralWidget* parentWidget; + HelpEngineWrapper &helpEngine; +}; + +QT_END_NAMESPACE + +#endif // HELPVIEWERQTB_H + +#endif // QT_NO_WEBKIT diff --git a/tools/assistant/tools/assistant/helpviewer_qwv.cpp b/tools/assistant/tools/assistant/helpviewer_qwv.cpp index cd6c89b..d2e0019 100644 --- a/tools/assistant/tools/assistant/helpviewer_qwv.cpp +++ b/tools/assistant/tools/assistant/helpviewer_qwv.cpp @@ -38,14 +38,14 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "helpviewer.h" +#if !defined(QT_NO_WEBKIT) + #include "helpviewer_qwv.h" #include "centralwidget.h" #include "helpenginewrapper.h" #include "tracer.h" -#include #include #include #include @@ -61,15 +61,6 @@ QT_BEGIN_NAMESPACE -#if !defined(QT_NO_WEBKIT) - -namespace { -const QString PageNotFoundMessage = - QCoreApplication::translate("HelpViewer", "Error 404...


The page could not be found


'%1'" - "

"); -} - class HelpNetworkReply : public QNetworkReply { public: @@ -159,7 +150,7 @@ QNetworkReply *HelpNetworkAccessManager::createRequest(Operation /*op*/, HelpEngineWrapper &helpEngine = HelpEngineWrapper::instance(); const QByteArray &data = helpEngine.findFile(url).isValid() ? helpEngine.fileData(url) - : PageNotFoundMessage.arg(url.toString()).toUtf8(); + : AbstractHelpViewer::PageNotFoundMessage.arg(url.toString()).toUtf8(); return new HelpNetworkReply(request, data, mimeType); } @@ -224,9 +215,9 @@ bool HelpPage::acceptNavigationRequest(QWebFrame *, const bool closeNewTab = closeNewTabIfNeeded; closeNewTabIfNeeded = false; - if (HelpViewer::isLocalUrl(url)) { + if (AbstractHelpViewer::isLocalUrl(url)) { const QString& path = url.path(); - if (!HelpViewer::canOpenPage(path)) { + if (!AbstractHelpViewer::canOpenPage(path)) { QTemporaryFile tmpTmpFile; if (!tmpTmpFile.open()) return false; @@ -392,6 +383,6 @@ void HelpViewer::setLoadFinished(bool ok) emit sourceChanged(url()); } -#endif - QT_END_NAMESPACE + +#endif // !QT_NO_WEBKIT diff --git a/tools/assistant/tools/assistant/helpviewer_qwv.h b/tools/assistant/tools/assistant/helpviewer_qwv.h index a78bd57..4daa869 100644 --- a/tools/assistant/tools/assistant/helpviewer_qwv.h +++ b/tools/assistant/tools/assistant/helpviewer_qwv.h @@ -38,10 +38,12 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#if !defined(QT_NO_WEBKIT) + #ifndef HELPVIEWERQWV_H #define HELPVIEWERQWV_H -#if !defined(QT_NO_WEBKIT) +#include "helpviewer.h" #include #include @@ -52,7 +54,7 @@ class CentralWidget; class HelpEngineWrapper; class QMouseEvent; -class HelpViewer : public QWebView +class HelpViewer : public QWebView, public AbstractHelpViewer { Q_OBJECT @@ -85,9 +87,6 @@ public: inline qreal zoom() const { return textSizeMultiplier(); } - static bool canOpenPage(const QString &url); - static bool isLocalUrl(const QUrl &url); - public Q_SLOTS: void home(); void backward() { back(); } @@ -114,8 +113,8 @@ private: HelpEngineWrapper &helpEngine; }; -#endif - QT_END_NAMESPACE #endif // HELPVIEWERQWV_H + +#endif // !QT_NO_WEBKIT diff --git a/tools/assistant/tools/assistant/indexwindow.cpp b/tools/assistant/tools/assistant/indexwindow.cpp index 1879db7..63ddbe4 100644 --- a/tools/assistant/tools/assistant/indexwindow.cpp +++ b/tools/assistant/tools/assistant/indexwindow.cpp @@ -44,7 +44,6 @@ #include "centralwidget.h" #include "helpenginewrapper.h" #include "helpviewer.h" -#include "helpviewer_qwv.h" #include "topicchooser.h" #include @@ -220,7 +219,7 @@ void IndexWindow::open(QHelpIndexWidget* indexWidget, const QModelIndex &index) return; } - if (!HelpViewer::canOpenPage(url.path())) + if (!AbstractHelpViewer::canOpenPage(url.path())) CentralWidget::instance()->setSource(url); else CentralWidget::instance()->setSourceInNewTab(url); diff --git a/tools/assistant/tools/assistant/mainwindow.cpp b/tools/assistant/tools/assistant/mainwindow.cpp index 2ff6b5e..086f9eb 100644 --- a/tools/assistant/tools/assistant/mainwindow.cpp +++ b/tools/assistant/tools/assistant/mainwindow.cpp @@ -44,7 +44,6 @@ #include "bookmarkmanager.h" #include "centralwidget.h" -#include "helpviewer.h" #include "indexwindow.h" #include "topicchooser.h" #include "contentwindow.h" -- cgit v0.12