PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/qtopiacore/qt/tools/assistant/helpwindow.cpp

https://github.com/muromec/qtopia-ezx
C++ | 232 lines | 166 code | 24 blank | 42 comment | 33 complexity | ab9fb4f32a6564d37ec5c8ef7f9749c9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
  4. **
  5. ** This file is part of the Qt Assistant of the Qt Toolkit.
  6. **
  7. ** This file may be used under the terms of the GNU General Public
  8. ** License versions 2.0 or 3.0 as published by the Free Software
  9. ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
  10. ** included in the packaging of this file. Alternatively you may (at
  11. ** your option) use any later version of the GNU General Public
  12. ** License if such license has been publicly approved by Trolltech ASA
  13. ** (or its successors, if any) and the KDE Free Qt Foundation. In
  14. ** addition, as a special exception, Trolltech gives you certain
  15. ** additional rights. These rights are described in the Trolltech GPL
  16. ** Exception version 1.2, which can be found at
  17. ** http://www.trolltech.com/products/qt/gplexception/ and in the file
  18. ** GPL_EXCEPTION.txt in this package.
  19. **
  20. ** Please review the following information to ensure GNU General
  21. ** Public Licensing requirements will be met:
  22. ** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
  23. ** you are unsure which license is appropriate for your use, please
  24. ** review the following information:
  25. ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
  26. ** or contact the sales department at sales@trolltech.com.
  27. **
  28. ** In addition, as a special exception, Trolltech, as the sole
  29. ** copyright holder for Qt Designer, grants users of the Qt/Eclipse
  30. ** Integration plug-in the right for the Qt/Eclipse Integration to
  31. ** link to functionality provided by Qt Designer and its related
  32. ** libraries.
  33. **
  34. ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
  35. ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
  36. ** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
  37. ** granted herein.
  38. **
  39. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  40. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  41. **
  42. ****************************************************************************/
  43. #include "helpwindow.h"
  44. #include "mainwindow.h"
  45. #include "tabbedbrowser.h"
  46. #include "helpdialog.h"
  47. #include "config.h"
  48. #include <QApplication>
  49. #include <QClipboard>
  50. #include <QUrl>
  51. #include <QMessageBox>
  52. #include <QDir>
  53. #include <QFile>
  54. #include <QProcess>
  55. #include <QMenu>
  56. #include <QAction>
  57. #include <QFileInfo>
  58. #include <QFont>
  59. #include <QtEvents>
  60. #include <QTextStream>
  61. #include <QTextCodec>
  62. #include <QStatusBar>
  63. #include <QTextCursor>
  64. #include <QTextObject>
  65. #include <QTextLayout>
  66. #include <QtDebug>
  67. #include <qdesktopservices.h>
  68. #if defined(Q_OS_WIN32)
  69. # include <windows.h>
  70. #endif
  71. HelpWindow::HelpWindow(MainWindow *w, QWidget *parent)
  72. : QTextBrowser(parent)
  73. , mw(w)
  74. , blockScroll(false)
  75. , shiftPressed(false)
  76. , newWindow(false)
  77. {
  78. FontSettings settings = Config::configuration()->fontSettings();
  79. setFont(settings.browserFont);
  80. }
  81. void HelpWindow::setSource(const QUrl &name)
  82. {
  83. if (name.isValid()) {
  84. if (name.scheme() == QLatin1String("http") || name.scheme() == QLatin1String("ftp")
  85. || name.scheme() == QLatin1String("mailto") || name.path().endsWith(QLatin1String("pdf"))) {
  86. bool launched = QDesktopServices::openUrl(name);
  87. if (!launched) {
  88. QMessageBox::information(mw, tr("Help"),
  89. tr("Unable to launch web browser.\n"),
  90. tr("OK"));
  91. }
  92. return;
  93. }
  94. QFileInfo fi(name.toLocalFile());
  95. if (name.scheme() == QLatin1String("file") && fi.exists()) {
  96. if (newWindow || (shiftPressed && hasFocus())) {
  97. shiftPressed = false;
  98. mw->saveSettings();
  99. MainWindow *nmw = new MainWindow;
  100. nmw->move(mw->geometry().topLeft());
  101. nmw->show();
  102. if (mw->isMaximized())
  103. nmw->showMaximized();
  104. nmw->setup();
  105. nmw->showLink(name.toString());
  106. } else {
  107. QTextBrowser::setSource(name);
  108. QTextBrowser::scrollToAnchor(name.fragment());
  109. }
  110. return;
  111. }
  112. }
  113. mw->statusBar()->showMessage(tr("Failed to open link: '%1'").arg(name.toString()), 5000);
  114. setHtml(tr("<div align=\"center\"><h1>The page could not be found</h1><br>"
  115. "<h3>'%1'</h3></div>").arg(name.toString()));
  116. mw->browsers()->updateTitle(tr("Error..."));
  117. }
  118. void HelpWindow::openLinkInNewWindow()
  119. {
  120. if (lastAnchor.isEmpty())
  121. return;
  122. newWindow = true;
  123. setSource(lastAnchor);
  124. newWindow = false;
  125. }
  126. void HelpWindow::openLinkInNewWindow(const QString &link)
  127. {
  128. lastAnchor = link;
  129. openLinkInNewWindow();
  130. }
  131. void HelpWindow::openLinkInNewPage()
  132. {
  133. if(lastAnchor.isEmpty())
  134. return;
  135. mw->browsers()->newTab(lastAnchor);
  136. lastAnchor.clear();
  137. }
  138. void HelpWindow::openLinkInNewPage(const QString &link)
  139. {
  140. lastAnchor = link;
  141. openLinkInNewPage();
  142. }
  143. bool HelpWindow::hasAnchorAt(const QPoint& pos)
  144. {
  145. lastAnchor = anchorAt(pos);
  146. if (lastAnchor.isEmpty())
  147. return false;
  148. lastAnchor = source().resolved(lastAnchor).toString();
  149. if (lastAnchor.at(0) == QLatin1Char('#')) {
  150. QString src = source().toString();
  151. int hsh = src.indexOf(QLatin1Char('#'));
  152. lastAnchor = (hsh>=0 ? src.left(hsh) : src) + lastAnchor;
  153. }
  154. return true;
  155. }
  156. void HelpWindow::contextMenuEvent(QContextMenuEvent *e)
  157. {
  158. QMenu *m = new QMenu(0);
  159. if (hasAnchorAt(e->pos())) {
  160. m->addAction(tr("Open Link in New Window\tShift+LMB"),
  161. this, SLOT(openLinkInNewWindow()));
  162. m->addAction(tr("Open Link in New Tab"),
  163. this, SLOT(openLinkInNewPage()));
  164. }
  165. mw->setupPopupMenu(m);
  166. m->exec(e->globalPos());
  167. delete m;
  168. }
  169. void HelpWindow::mouseReleaseEvent(QMouseEvent *e)
  170. {
  171. if (e->button() == Qt::XButton1) {
  172. QTextBrowser::backward();
  173. return;
  174. }
  175. if (e->button() == Qt::XButton2) {
  176. QTextBrowser::forward();
  177. return;
  178. }
  179. if (e->button() == Qt::MidButton && hasAnchorAt(e->pos())) {
  180. openLinkInNewPage();
  181. return;
  182. }
  183. QTextBrowser::mouseReleaseEvent(e);
  184. }
  185. void HelpWindow::blockScrolling(bool b)
  186. {
  187. blockScroll = b;
  188. }
  189. void HelpWindow::ensureCursorVisible()
  190. {
  191. if (!blockScroll)
  192. QTextBrowser::ensureCursorVisible();
  193. }
  194. void HelpWindow::mousePressEvent(QMouseEvent *e)
  195. {
  196. shiftPressed = e->modifiers() & Qt::ShiftModifier;
  197. if (!(shiftPressed && hasAnchorAt(e->pos())))
  198. QTextBrowser::mousePressEvent(e);
  199. }
  200. void HelpWindow::keyPressEvent(QKeyEvent *e)
  201. {
  202. shiftPressed = e->modifiers() & Qt::ShiftModifier;
  203. QTextBrowser::keyPressEvent(e);
  204. }
  205. bool HelpWindow::isKDERunning() const
  206. {
  207. return !qgetenv("KDE_FULL_SESSION").isEmpty();
  208. }