Files
qt-creator/src/plugins/help/textbrowserhelpviewer.cpp
Eike Ziller ea27143239 Merge remote-tracking branch 'origin/3.2'
Conflicts:
	src/libs/utils/ipaddresslineedit.cpp
	src/libs/utils/logging.h
	src/plugins/analyzerbase/AnalyzerBase.pluginspec.in
	src/plugins/android/Android.pluginspec.in
	src/plugins/android/androiddeploystep.cpp
	src/plugins/android/androiddeploystep.h
	src/plugins/android/androiddeploystepfactory.cpp
	src/plugins/android/androiddeploystepwidget.cpp
	src/plugins/android/androidpackagecreationfactory.cpp
	src/plugins/android/androidpackagecreationstep.cpp
	src/plugins/android/androidpackagecreationstep.h
	src/plugins/android/androidpackagecreationwidget.cpp
	src/plugins/android/androidpackagecreationwidget.h
	src/plugins/android/javafilewizard.cpp
	src/plugins/autotoolsprojectmanager/AutotoolsProjectManager.pluginspec.in
	src/plugins/baremetal/BareMetal.pluginspec.in
	src/plugins/bazaar/Bazaar.pluginspec.in
	src/plugins/beautifier/Beautifier.pluginspec.in
	src/plugins/bineditor/BinEditor.pluginspec.in
	src/plugins/bookmarks/Bookmarks.pluginspec.in
	src/plugins/clangcodemodel/ClangCodeModel.pluginspec.in
	src/plugins/clangcodemodel/clanghighlightingsupport.cpp
	src/plugins/clangcodemodel/clangsymbolsearcher.cpp
	src/plugins/classview/ClassView.pluginspec.in
	src/plugins/clearcase/ClearCase.pluginspec.in
	src/plugins/cmakeprojectmanager/CMakeProjectManager.pluginspec.in
	src/plugins/cmakeprojectmanager/cmakeeditorfactory.cpp
	src/plugins/cmakeprojectmanager/cmakehighlighter.cpp
	src/plugins/coreplugin/Core.pluginspec.in
	src/plugins/cpaster/CodePaster.pluginspec.in
	src/plugins/cppeditor/CppEditor.pluginspec.in
	src/plugins/cppeditor/cppfilewizard.cpp
	src/plugins/cpptools/CppTools.pluginspec.in
	src/plugins/cpptools/cpphighlightingsupportinternal.cpp
	src/plugins/cpptools/cppmodelmanagerinterface.cpp
	src/plugins/cpptools/cppmodelmanagerinterface.h
	src/plugins/cvs/CVS.pluginspec.in
	src/plugins/debugger/Debugger.pluginspec.in
	src/plugins/designer/Designer.pluginspec.in
	src/plugins/diffeditor/DiffEditor.pluginspec.in
	src/plugins/emacskeys/EmacsKeys.pluginspec.in
	src/plugins/fakevim/FakeVim.pluginspec.in
	src/plugins/genericprojectmanager/GenericProjectManager.pluginspec.in
	src/plugins/git/Git.pluginspec.in
	src/plugins/git/gitorious/gitorious.cpp
	src/plugins/git/gitorious/gitorious.h
	src/plugins/git/gitorious/gitoriousclonewizard.cpp
	src/plugins/git/gitorious/gitorioushostwidget.cpp
	src/plugins/git/gitorious/gitorioushostwidget.h
	src/plugins/git/gitorious/gitorioushostwizardpage.cpp
	src/plugins/git/gitorious/gitoriousprojectwidget.cpp
	src/plugins/git/gitorious/gitoriousprojectwidget.h
	src/plugins/git/gitorious/gitoriousprojectwizardpage.cpp
	src/plugins/git/gitorious/gitoriousprojectwizardpage.h
	src/plugins/git/gitorious/gitoriousrepositorywizardpage.cpp
	src/plugins/git/gitorious/gitoriousrepositorywizardpage.h
	src/plugins/glsleditor/GLSLEditor.pluginspec.in
	src/plugins/glsleditor/glsleditorfactory.cpp
	src/plugins/glsleditor/glslfilewizard.cpp
	src/plugins/helloworld/HelloWorld.pluginspec.in
	src/plugins/help/Help.pluginspec.in
	src/plugins/imageviewer/ImageViewer.pluginspec.in
	src/plugins/ios/Ios.pluginspec.in
	src/plugins/macros/Macros.pluginspec.in
	src/plugins/mercurial/Mercurial.pluginspec.in
	src/plugins/perforce/Perforce.pluginspec.in
	src/plugins/projectexplorer/ProjectExplorer.pluginspec.in
	src/plugins/pythoneditor/PythonEditor.pluginspec.in
	src/plugins/pythoneditor/pythoneditorwidget.cpp
	src/plugins/pythoneditor/wizard/pythonfilewizard.cpp
	src/plugins/qbsprojectmanager/QbsProjectManager.pluginspec.in
	src/plugins/qbsprojectmanager/defaultpropertyprovider.cpp
	src/plugins/qmakeprojectmanager/QmakeProjectManager.pluginspec.in
	src/plugins/qmakeprojectmanager/profileeditorfactory.cpp
	src/plugins/qmldesigner/QmlDesigner.pluginspec.in
	src/plugins/qmljseditor/QmlJSEditor.pluginspec.in
	src/plugins/qmljseditor/qmljseditorfactory.cpp
	src/plugins/qmljstools/QmlJSTools.pluginspec.in
	src/plugins/qmlprofiler/QmlProfiler.pluginspec.in
	src/plugins/qmlprojectmanager/QmlProjectManager.pluginspec.in
	src/plugins/qnx/Qnx.pluginspec.in
	src/plugins/qtsupport/QtSupport.pluginspec.in
	src/plugins/remotelinux/RemoteLinux.pluginspec.in
	src/plugins/resourceeditor/ResourceEditor.pluginspec.in
	src/plugins/resourceeditor/resourcewizard.h
	src/plugins/subversion/Subversion.pluginspec.in
	src/plugins/tasklist/TaskList.pluginspec.in
	src/plugins/texteditor/TextEditor.pluginspec.in
	src/plugins/texteditor/basetexteditor_p.h
	src/plugins/texteditor/basetextmark.cpp
	src/plugins/texteditor/codeassist/basicproposalitemlistmodel.h
	src/plugins/texteditor/codeassist/defaultassistinterface.h
	src/plugins/texteditor/codeassist/iassistproposalitem.cpp
	src/plugins/texteditor/itexteditor.cpp
	src/plugins/texteditor/itexteditor.h
	src/plugins/texteditor/itextmark.cpp
	src/plugins/texteditor/plaintexteditor.cpp
	src/plugins/texteditor/plaintexteditor.h
	src/plugins/texteditor/texteditoractionhandler.cpp
	src/plugins/todo/Todo.pluginspec.in
	src/plugins/updateinfo/UpdateInfo.pluginspec.in
	src/plugins/valgrind/Valgrind.pluginspec.in
	src/plugins/vcsbase/VcsBase.pluginspec.in
	src/plugins/welcome/Welcome.pluginspec.in
	src/plugins/winrt/WinRt.pluginspec.in
	tests/auto/debugger/temporarydir.h

Change-Id: I254af8be8119fe9855287909e17d4b8ca9d2fc2f
2014-10-14 15:36:16 +02:00

437 lines
12 KiB
C++

/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "textbrowserhelpviewer.h"
#include "helpconstants.h"
#include "localhelpmanager.h"
#include <coreplugin/find/findplugin.h>
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <QApplication>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QHelpEngine>
#include <QKeyEvent>
#include <QMenu>
#include <QVBoxLayout>
using namespace Help;
using namespace Help::Internal;
// -- HelpViewer
TextBrowserHelpViewer::TextBrowserHelpViewer(qreal zoom, QWidget *parent)
: HelpViewer(parent)
, m_textBrowser(new TextBrowserHelpWidget(zoom, this))
{
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_textBrowser, 10);
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);
connect(m_textBrowser, SIGNAL(sourceChanged(QUrl)), this, SIGNAL(titleChanged()));
connect(m_textBrowser, SIGNAL(forwardAvailable(bool)), this, SIGNAL(forwardAvailable(bool)));
connect(m_textBrowser, SIGNAL(backwardAvailable(bool)), this, SIGNAL(backwardAvailable(bool)));
}
TextBrowserHelpViewer::~TextBrowserHelpViewer()
{
}
QFont TextBrowserHelpViewer::viewerFont() const
{
return m_textBrowser->font();
}
void TextBrowserHelpViewer::setViewerFont(const QFont &newFont)
{
m_textBrowser->forceFont = true;
m_textBrowser->setFont(newFont);
m_textBrowser->forceFont = false;
}
void TextBrowserHelpViewer::scaleUp()
{
m_textBrowser->scaleUp();
}
void TextBrowserHelpViewer::scaleDown()
{
m_textBrowser->scaleDown();
}
void TextBrowserHelpViewer::resetScale()
{
if (m_textBrowser->zoomCount != 0) {
m_textBrowser->forceFont = true;
m_textBrowser->zoomOut(m_textBrowser->zoomCount);
m_textBrowser->forceFont = false;
}
m_textBrowser->zoomCount = 0;
}
qreal TextBrowserHelpViewer::scale() const
{
return m_textBrowser->zoomCount;
}
QString TextBrowserHelpViewer::title() const
{
return m_textBrowser->documentTitle();
}
QUrl TextBrowserHelpViewer::source() const
{
return m_textBrowser->source();
}
void TextBrowserHelpViewer::setSource(const QUrl &url)
{
if (launchWithExternalApp(url))
return;
slotLoadStarted();
m_textBrowser->setSource(url);
slotLoadFinished();
}
void TextBrowserHelpViewer::scrollToAnchor(const QString &anchor)
{
m_textBrowser->scrollToAnchor(anchor);
}
void TextBrowserHelpViewer::setHtml(const QString &html)
{
m_textBrowser->setHtml(html);
}
QString TextBrowserHelpViewer::selectedText() const
{
return m_textBrowser->textCursor().selectedText();
}
bool TextBrowserHelpViewer::isForwardAvailable() const
{
return m_textBrowser->isForwardAvailable();
}
bool TextBrowserHelpViewer::isBackwardAvailable() const
{
return m_textBrowser->isBackwardAvailable();
}
void TextBrowserHelpViewer::addBackHistoryItems(QMenu *backMenu)
{
for (int i = 1; i <= m_textBrowser->backwardHistoryCount(); ++i) {
QAction *action = new QAction(backMenu);
action->setText(m_textBrowser->historyTitle(-i));
action->setData(-i);
connect(action, SIGNAL(triggered()), this, SLOT(goToHistoryItem()));
backMenu->addAction(action);
}
}
void TextBrowserHelpViewer::addForwardHistoryItems(QMenu *forwardMenu)
{
for (int i = 1; i <= m_textBrowser->forwardHistoryCount(); ++i) {
QAction *action = new QAction(forwardMenu);
action->setText(m_textBrowser->historyTitle(i));
action->setData(i);
connect(action, SIGNAL(triggered()), this, SLOT(goToHistoryItem()));
forwardMenu->addAction(action);
}
}
void TextBrowserHelpViewer::setOpenInNewPageActionVisible(bool visible)
{
m_textBrowser->m_openInNewPageActionVisible = visible;
}
bool TextBrowserHelpViewer::findText(const QString &text, Core::FindFlags flags,
bool incremental, bool fromSearch, bool *wrapped)
{
if (wrapped)
*wrapped = false;
QTextDocument *doc = m_textBrowser->document();
QTextCursor cursor = m_textBrowser->textCursor();
if (!doc || cursor.isNull())
return false;
const int position = cursor.selectionStart();
if (incremental)
cursor.setPosition(position);
QTextDocument::FindFlags f = Core::textDocumentFlagsForFindFlags(flags);
QTextCursor found = doc->find(text, cursor, f);
if (found.isNull()) {
if ((flags & Core::FindBackward) == 0)
cursor.movePosition(QTextCursor::Start);
else
cursor.movePosition(QTextCursor::End);
found = doc->find(text, cursor, f);
if (!found.isNull() && wrapped)
*wrapped = true;
}
if (fromSearch) {
cursor.beginEditBlock();
m_textBrowser->viewport()->setUpdatesEnabled(false);
QTextCharFormat marker;
marker.setForeground(Qt::red);
cursor.movePosition(QTextCursor::Start);
m_textBrowser->setTextCursor(cursor);
while (m_textBrowser->find(text)) {
QTextCursor hit = m_textBrowser->textCursor();
hit.mergeCharFormat(marker);
}
m_textBrowser->viewport()->setUpdatesEnabled(true);
cursor.endEditBlock();
}
bool cursorIsNull = found.isNull();
if (cursorIsNull) {
found = m_textBrowser->textCursor();
found.setPosition(position);
}
m_textBrowser->setTextCursor(found);
return !cursorIsNull;
}
// -- public slots
void TextBrowserHelpViewer::copy()
{
m_textBrowser->copy();
}
void TextBrowserHelpViewer::stop()
{
}
void TextBrowserHelpViewer::forward()
{
m_textBrowser->forward();
}
void TextBrowserHelpViewer::backward()
{
m_textBrowser->backward();
}
void TextBrowserHelpViewer::print(QPrinter *printer)
{
m_textBrowser->print(printer);
}
void TextBrowserHelpViewer::goToHistoryItem()
{
QAction *action = qobject_cast<QAction *>(sender());
QTC_ASSERT(action, return);
bool ok = false;
int index = action->data().toInt(&ok);
QTC_ASSERT(ok, return);
// go back?
while (index < 0) {
m_textBrowser->backward();
++index;
}
// go forward?
while (index > 0) {
m_textBrowser->forward();
--index;
}
}
// -- private
TextBrowserHelpWidget::TextBrowserHelpWidget(int zoom, TextBrowserHelpViewer *parent)
: QTextBrowser(parent)
, zoomCount(zoom)
, forceFont(false)
, lastAnchor(QString())
, m_openInNewPageActionVisible(true)
, m_parent(parent)
{
installEventFilter(this);
document()->setDocumentMargin(8);
}
QVariant TextBrowserHelpWidget::loadResource(int type, const QUrl &name)
{
if (type < 4)
return LocalHelpManager::helpData(name).data;
return QByteArray();
}
bool TextBrowserHelpWidget::hasAnchorAt(const QPoint &pos)
{
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 TextBrowserHelpWidget::openLink(bool newPage)
{
if (lastAnchor.isEmpty())
return;
if (newPage)
OpenPagesManager::instance().createPage(lastAnchor);
else
CentralWidget::instance()->setSource(lastAnchor);
lastAnchor.clear();
}
void TextBrowserHelpWidget::scaleUp()
{
if (zoomCount < 10) {
zoomCount++;
forceFont = true;
zoomIn();
forceFont = false;
}
}
void TextBrowserHelpWidget::scaleDown()
{
if (zoomCount > -5) {
zoomCount--;
forceFont = true;
zoomOut();
forceFont = false;
}
}
void TextBrowserHelpWidget::openLink()
{
openLink(false);
}
void TextBrowserHelpWidget::openLinkInNewPage()
{
openLink(true);
}
void TextBrowserHelpWidget::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(QLatin1String(""), 0);
QUrl link;
QAction *copyAnchorAction = 0;
if (hasAnchorAt(event->pos())) {
link = anchorAt(event->pos());
if (link.isRelative())
link = source().resolved(link);
menu.addAction(tr("Open Link"), this, SLOT(openLink()));
if (m_openInNewPageActionVisible)
menu.addAction(QCoreApplication::translate("HelpViewer", "Open Link as New Page"),
this, SLOT(openLinkInNewPage()));
if (!link.isEmpty() && link.isValid())
copyAnchorAction = menu.addAction(tr("Copy Link"));
} else if (!textCursor().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());
}
bool TextBrowserHelpWidget::eventFilter(QObject *obj, QEvent *event)
{
if (obj == this) {
if (event->type() == QEvent::FontChange) {
if (!forceFont)
return true;
} else if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Slash) {
keyEvent->accept();
Core::FindPlugin::instance()->openFindToolBar(Core::FindPlugin::FindForwardDirection);
return true;
}
}
}
return QTextBrowser::eventFilter(obj, event);
}
void TextBrowserHelpWidget::wheelEvent(QWheelEvent *e)
{
if (e->modifiers() == Qt::ControlModifier) {
e->accept();
e->delta() > 0 ? scaleUp() : scaleDown();
} else {
QTextBrowser::wheelEvent(e);
}
}
void TextBrowserHelpWidget::mousePressEvent(QMouseEvent *e)
{
if (Utils::HostOsInfo::isLinuxHost() && m_parent->handleForwardBackwardMouseButtons(e))
return;
QTextBrowser::mousePressEvent(e);
}
void TextBrowserHelpWidget::mouseReleaseEvent(QMouseEvent *e)
{
if (!Utils::HostOsInfo::isLinuxHost() && m_parent->handleForwardBackwardMouseButtons(e))
return;
bool controlPressed = e->modifiers() & Qt::ControlModifier;
if ((controlPressed && hasAnchorAt(e->pos())) ||
(e->button() == Qt::MidButton && hasAnchorAt(e->pos()))) {
openLinkInNewPage();
return;
}
QTextBrowser::mouseReleaseEvent(e);
}