| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the Qt Assistant of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "helpwindow.h"
|
|---|
| 43 | #include "mainwindow.h"
|
|---|
| 44 | #include "tabbedbrowser.h"
|
|---|
| 45 | #include "helpdialog.h"
|
|---|
| 46 | #include "config.h"
|
|---|
| 47 |
|
|---|
| 48 | #include <QApplication>
|
|---|
| 49 | #include <QClipboard>
|
|---|
| 50 | #include <QUrl>
|
|---|
| 51 | #include <QMessageBox>
|
|---|
| 52 | #include <QDir>
|
|---|
| 53 | #include <QFile>
|
|---|
| 54 | #include <QProcess>
|
|---|
| 55 | #include <QAction>
|
|---|
| 56 | #include <QFileInfo>
|
|---|
| 57 | #include <QFont>
|
|---|
| 58 | #include <QtEvents>
|
|---|
| 59 | #include <QTextStream>
|
|---|
| 60 | #include <QTextCodec>
|
|---|
| 61 | #include <QStatusBar>
|
|---|
| 62 | #include <QTextCursor>
|
|---|
| 63 | #include <QTextObject>
|
|---|
| 64 | #include <QTextLayout>
|
|---|
| 65 | #include <QtDebug>
|
|---|
| 66 | #include <qdesktopservices.h>
|
|---|
| 67 |
|
|---|
| 68 | #include <QtGui/QClipboard>
|
|---|
| 69 | #include <QtGui/QApplication>
|
|---|
| 70 |
|
|---|
| 71 | #if defined(Q_OS_WIN32)
|
|---|
| 72 | # include <windows.h>
|
|---|
| 73 | #endif
|
|---|
| 74 |
|
|---|
| 75 | QT_BEGIN_NAMESPACE
|
|---|
| 76 |
|
|---|
| 77 | HelpWindow::HelpWindow(MainWindow *w, QWidget *parent)
|
|---|
| 78 | : QTextBrowser(parent)
|
|---|
| 79 | , mw(w)
|
|---|
| 80 | , blockScroll(false)
|
|---|
| 81 | , shiftPressed(false)
|
|---|
| 82 | , newWindow(false)
|
|---|
| 83 | {
|
|---|
| 84 | FontSettings settings = Config::configuration()->fontSettings();
|
|---|
| 85 | setFont(settings.browserFont);
|
|---|
| 86 |
|
|---|
| 87 | connect(this, SIGNAL(copyAvailable(bool)), w, SLOT(copyAvailable(bool)));
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void HelpWindow::setSource(const QUrl &name)
|
|---|
| 91 | {
|
|---|
| 92 | if (name.isValid()) {
|
|---|
| 93 | if (name.scheme() == QLatin1String("http") || name.scheme() == QLatin1String("ftp")
|
|---|
| 94 | || name.scheme() == QLatin1String("mailto") || name.path().endsWith(QLatin1String("pdf"))) {
|
|---|
| 95 | bool launched = QDesktopServices::openUrl(name);
|
|---|
| 96 | if (!launched) {
|
|---|
| 97 | QMessageBox::information(mw, tr("Help"),
|
|---|
| 98 | tr("Unable to launch web browser.\n"),
|
|---|
| 99 | tr("OK"));
|
|---|
| 100 | }
|
|---|
| 101 | return;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | QFileInfo fi(name.toLocalFile());
|
|---|
| 105 | if (name.scheme() == QLatin1String("file") && fi.exists()) {
|
|---|
| 106 | if (newWindow || (shiftPressed && hasFocus())) {
|
|---|
| 107 | shiftPressed = false;
|
|---|
| 108 | mw->saveSettings();
|
|---|
| 109 | MainWindow *nmw = new MainWindow;
|
|---|
| 110 | nmw->move(mw->geometry().topLeft());
|
|---|
| 111 | nmw->show();
|
|---|
| 112 |
|
|---|
| 113 | if (mw->isMaximized())
|
|---|
| 114 | nmw->showMaximized();
|
|---|
| 115 |
|
|---|
| 116 | nmw->setup();
|
|---|
| 117 | nmw->showLink(name.toString());
|
|---|
| 118 | } else {
|
|---|
| 119 | QTextBrowser::setSource(name);
|
|---|
| 120 | QTextBrowser::scrollToAnchor(name.fragment());
|
|---|
| 121 | }
|
|---|
| 122 | return;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | mw->statusBar()->showMessage(tr("Failed to open link: '%1'").arg(name.toString()), 5000);
|
|---|
| 127 | setHtml(tr("<div align=\"center\"><h1>The page could not be found</h1><br>"
|
|---|
| 128 | "<h3>'%1'</h3></div>").arg(name.toString()));
|
|---|
| 129 | mw->browsers()->updateTitle(tr("Error..."));
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void HelpWindow::openLinkInNewWindow()
|
|---|
| 133 | {
|
|---|
| 134 | if (lastAnchor.isEmpty())
|
|---|
| 135 | return;
|
|---|
| 136 | newWindow = true;
|
|---|
| 137 | setSource(lastAnchor);
|
|---|
| 138 | newWindow = false;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void HelpWindow::openLinkInNewWindow(const QString &link)
|
|---|
| 142 | {
|
|---|
| 143 | lastAnchor = link;
|
|---|
| 144 | openLinkInNewWindow();
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | void HelpWindow::openLinkInNewPage()
|
|---|
| 148 | {
|
|---|
| 149 | if(lastAnchor.isEmpty())
|
|---|
| 150 | return;
|
|---|
| 151 | mw->browsers()->newTab(lastAnchor);
|
|---|
| 152 | lastAnchor.clear();
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | void HelpWindow::openLinkInNewPage(const QString &link)
|
|---|
| 156 | {
|
|---|
| 157 | lastAnchor = link;
|
|---|
| 158 | openLinkInNewPage();
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | bool HelpWindow::hasAnchorAt(const QPoint& pos)
|
|---|
| 162 | {
|
|---|
| 163 | lastAnchor = anchorAt(pos);
|
|---|
| 164 | if (lastAnchor.isEmpty())
|
|---|
| 165 | return false;
|
|---|
| 166 | lastAnchor = source().resolved(lastAnchor).toString();
|
|---|
| 167 | if (lastAnchor.at(0) == QLatin1Char('#')) {
|
|---|
| 168 | QString src = source().toString();
|
|---|
| 169 | int hsh = src.indexOf(QLatin1Char('#'));
|
|---|
| 170 | lastAnchor = (hsh>=0 ? src.left(hsh) : src) + lastAnchor;
|
|---|
| 171 | }
|
|---|
| 172 | return true;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | void HelpWindow::contextMenuEvent(QContextMenuEvent *e)
|
|---|
| 176 | {
|
|---|
| 177 | QMenu menu(QLatin1String(""), 0);
|
|---|
| 178 |
|
|---|
| 179 | QUrl link;
|
|---|
| 180 | QAction *copyAnchorAction = 0;
|
|---|
| 181 | if (hasAnchorAt(e->pos())) {
|
|---|
| 182 | link = anchorAt(e->pos());
|
|---|
| 183 | if (link.isRelative())
|
|---|
| 184 | link = source().resolved(link);
|
|---|
| 185 | copyAnchorAction = menu.addAction(tr("Copy &Link Location"));
|
|---|
| 186 | copyAnchorAction->setEnabled(!link.isEmpty() && link.isValid());
|
|---|
| 187 |
|
|---|
| 188 | menu.addAction(tr("Open Link in New Tab"),
|
|---|
| 189 | this, SLOT(openLinkInNewPage()));
|
|---|
| 190 | menu.addAction(tr("Open Link in New Window\tShift+LMB"),
|
|---|
| 191 | this, SLOT(openLinkInNewWindow()));
|
|---|
| 192 | }
|
|---|
| 193 | mw->setupPopupMenu(&menu);
|
|---|
| 194 | QAction *action = menu.exec(e->globalPos());
|
|---|
| 195 | if (action == copyAnchorAction)
|
|---|
| 196 | QApplication::clipboard()->setText(link.toString());
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | void HelpWindow::mouseReleaseEvent(QMouseEvent *e)
|
|---|
| 200 | {
|
|---|
| 201 | if (e->button() == Qt::XButton1) {
|
|---|
| 202 | QTextBrowser::backward();
|
|---|
| 203 | return;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | if (e->button() == Qt::XButton2) {
|
|---|
| 207 | QTextBrowser::forward();
|
|---|
| 208 | return;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | if (e->button() == Qt::MidButton && hasAnchorAt(e->pos())) {
|
|---|
| 212 | openLinkInNewPage();
|
|---|
| 213 | return;
|
|---|
| 214 | }
|
|---|
| 215 | QTextBrowser::mouseReleaseEvent(e);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | void HelpWindow::blockScrolling(bool b)
|
|---|
| 219 | {
|
|---|
| 220 | blockScroll = b;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | void HelpWindow::ensureCursorVisible()
|
|---|
| 224 | {
|
|---|
| 225 | if (!blockScroll)
|
|---|
| 226 | QTextBrowser::ensureCursorVisible();
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void HelpWindow::mousePressEvent(QMouseEvent *e)
|
|---|
| 230 | {
|
|---|
| 231 | shiftPressed = e->modifiers() & Qt::ShiftModifier;
|
|---|
| 232 | if (!(shiftPressed && hasAnchorAt(e->pos())))
|
|---|
| 233 | QTextBrowser::mousePressEvent(e);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | void HelpWindow::keyPressEvent(QKeyEvent *e)
|
|---|
| 237 | {
|
|---|
| 238 | shiftPressed = e->modifiers() & Qt::ShiftModifier;
|
|---|
| 239 | QTextBrowser::keyPressEvent(e);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | bool HelpWindow::isKDERunning() const
|
|---|
| 243 | {
|
|---|
| 244 | return !qgetenv("KDE_FULL_SESSION").isEmpty();
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | QT_END_NAMESPACE
|
|---|