Files
qt-creator/src/plugins/help/centralwidget.cpp

371 lines
10 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
** 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://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/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 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, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2008-12-02 12:01:29 +01:00
#include "centralwidget.h"
2008-12-02 12:01:29 +01:00
#include "helpviewer.h"
#include "localhelpmanager.h"
#include <topicchooser.h>
2008-12-02 12:01:29 +01:00
#include <QEvent>
#include <QTimer>
2008-12-02 12:01:29 +01:00
#include <QKeyEvent>
#include <QLayout>
#include <QPageSetupDialog>
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QStackedWidget>
2008-12-02 12:01:29 +01:00
#include <QHelpEngine>
#include <QHelpSearchEngine>
2008-12-02 12:01:29 +01:00
using namespace Help::Internal;
CentralWidget *gStaticCentralWidget = 0;
// -- CentralWidget
2008-12-02 12:01:29 +01:00
CentralWidget::CentralWidget(QWidget *parent)
2008-12-02 12:01:29 +01:00
: QWidget(parent)
, printer(0)
, m_stackedWidget(0)
2008-12-02 12:01:29 +01:00
{
Q_ASSERT(!gStaticCentralWidget);
gStaticCentralWidget = this;
2008-12-02 12:01:29 +01:00
QVBoxLayout *vboxLayout = new QVBoxLayout(this);
vboxLayout->setMargin(0);
m_stackedWidget = new QStackedWidget(this);
vboxLayout->addWidget(m_stackedWidget);
2008-12-02 12:01:29 +01:00
}
CentralWidget::~CentralWidget()
{
2009-06-25 15:20:38 +02:00
#ifndef QT_NO_PRINTER
delete printer;
#endif
QString zoomFactors;
2009-06-25 15:20:38 +02:00
QString currentPages;
for (int i = 0; i < m_stackedWidget->count(); ++i) {
const HelpViewer * const viewer = viewerAt(i);
const QUrl &source = viewer->source();
if (source.isValid()) {
currentPages += source.toString() + QLatin1Char('|');
zoomFactors += QString::number(viewer->scale()) + QLatin1Char('|');
}
2008-12-02 12:01:29 +01:00
}
QHelpEngineCore *engine = &LocalHelpManager::helpEngine();
engine->setCustomValue(QLatin1String("LastShownPages"), currentPages);
engine->setCustomValue(QLatin1String("LastShownPagesZoom"), zoomFactors);
engine->setCustomValue(QLatin1String("LastTabPage"), currentIndex());
2008-12-02 12:01:29 +01:00
}
CentralWidget *CentralWidget::instance()
{
Q_ASSERT(gStaticCentralWidget);
return gStaticCentralWidget;
2008-12-02 12:01:29 +01:00
}
bool CentralWidget::isForwardAvailable() const
2008-12-02 12:01:29 +01:00
{
const HelpViewer* viewer = currentHelpViewer();
2008-12-02 12:01:29 +01:00
if (viewer)
return viewer->isForwardAvailable();
2008-12-02 12:01:29 +01:00
return false;
2008-12-02 12:01:29 +01:00
}
bool CentralWidget::isBackwardAvailable() const
2008-12-02 12:01:29 +01:00
{
const HelpViewer* viewer = currentHelpViewer();
2008-12-02 12:01:29 +01:00
if (viewer)
return viewer->isBackwardAvailable();
2008-12-02 12:01:29 +01:00
return false;
2008-12-02 12:01:29 +01:00
}
HelpViewer* CentralWidget::viewerAt(int index) const
2008-12-02 12:01:29 +01:00
{
return qobject_cast<HelpViewer*> (m_stackedWidget->widget(index));
2008-12-02 12:01:29 +01:00
}
HelpViewer* CentralWidget::currentHelpViewer() const
2008-12-02 12:01:29 +01:00
{
return qobject_cast<HelpViewer*> (m_stackedWidget->currentWidget());
2008-12-02 12:01:29 +01:00
}
void CentralWidget::addPage(HelpViewer *page, bool fromSearch)
2008-12-02 12:01:29 +01:00
{
page->installEventFilter(this);
page->setFocus(Qt::OtherFocusReason);
connectSignals(page);
m_stackedWidget->addWidget(page);
if (fromSearch) {
connect(currentHelpViewer(), SIGNAL(loadFinished(bool)), this,
SLOT(highlightSearchTerms()));
}
2008-12-02 12:01:29 +01:00
}
void CentralWidget::removePage(int index)
2008-12-02 12:01:29 +01:00
{
m_stackedWidget->removeWidget(m_stackedWidget->widget(index));
2008-12-02 12:01:29 +01:00
}
int CentralWidget::currentIndex() const
2008-12-02 12:01:29 +01:00
{
return m_stackedWidget->currentIndex();
2008-12-02 12:01:29 +01:00
}
void CentralWidget::setCurrentPage(HelpViewer *page)
2008-12-02 12:01:29 +01:00
{
m_stackedWidget->setCurrentWidget(page);
2008-12-02 12:01:29 +01:00
}
bool CentralWidget::find(const QString &txt, Core::FindFlags flags,
bool incremental, bool *wrapped)
2008-12-02 12:01:29 +01:00
{
return currentHelpViewer()->findText(txt, flags, incremental, false, wrapped);
2008-12-02 12:01:29 +01:00
}
// -- public slots
2008-12-02 12:01:29 +01:00
void CentralWidget::copy()
2008-12-02 12:01:29 +01:00
{
if (HelpViewer* viewer = currentHelpViewer())
viewer->copy();
2008-12-02 12:01:29 +01:00
}
void CentralWidget::home()
2008-12-02 12:01:29 +01:00
{
if (HelpViewer* viewer = currentHelpViewer())
viewer->home();
2008-12-02 12:01:29 +01:00
}
void CentralWidget::zoomIn()
2008-12-02 12:01:29 +01:00
{
HelpViewer* viewer = currentHelpViewer();
2008-12-02 12:01:29 +01:00
if (viewer)
viewer->scaleUp();
2008-12-02 12:01:29 +01:00
}
void CentralWidget::zoomOut()
2008-12-02 12:01:29 +01:00
{
HelpViewer* viewer = currentHelpViewer();
if (viewer)
viewer->scaleDown();
2008-12-02 12:01:29 +01:00
}
void CentralWidget::resetZoom()
2008-12-02 12:01:29 +01:00
{
HelpViewer* viewer = currentHelpViewer();
2008-12-02 12:01:29 +01:00
if (viewer)
viewer->resetScale();
2008-12-02 12:01:29 +01:00
}
void CentralWidget::forward()
{
if (HelpViewer* viewer = currentHelpViewer())
2008-12-02 12:01:29 +01:00
viewer->forward();
}
void CentralWidget::backward()
{
if (HelpViewer* viewer = currentHelpViewer())
2008-12-02 12:01:29 +01:00
viewer->backward();
}
void CentralWidget::print()
2008-12-02 12:01:29 +01:00
{
#ifndef QT_NO_PRINTER
if (HelpViewer* viewer = currentHelpViewer()) {
initPrinter();
QPrintDialog dlg(printer, this);
dlg.setWindowTitle(tr("Print Document"));
if (!viewer->selectedText().isEmpty())
dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);
dlg.addEnabledOption(QAbstractPrintDialog::PrintPageRange);
dlg.addEnabledOption(QAbstractPrintDialog::PrintCollateCopies);
if (dlg.exec() == QDialog::Accepted)
viewer->print(printer);
}
#endif
2008-12-02 12:01:29 +01:00
}
void CentralWidget::pageSetup()
2008-12-02 12:01:29 +01:00
{
#ifndef QT_NO_PRINTER
initPrinter();
QPageSetupDialog dlg(printer);
dlg.exec();
2008-12-02 12:01:29 +01:00
#endif
}
void CentralWidget::printPreview()
2008-12-02 12:01:29 +01:00
{
#ifndef QT_NO_PRINTER
initPrinter();
QPrintPreviewDialog preview(printer, this);
connect(&preview, SIGNAL(paintRequested(QPrinter*)),
SLOT(printPreview(QPrinter*)));
preview.exec();
#endif
2008-12-02 12:01:29 +01:00
}
void CentralWidget::setSource(const QUrl &url)
2008-12-02 12:01:29 +01:00
{
if (HelpViewer* viewer = currentHelpViewer()) {
viewer->setSource(url);
viewer->setFocus(Qt::OtherFocusReason);
2008-12-02 12:01:29 +01:00
}
}
void CentralWidget::setSourceFromSearch(const QUrl &url)
2008-12-02 12:01:29 +01:00
{
if (HelpViewer* viewer = currentHelpViewer()) {
connect(viewer, SIGNAL(loadFinished(bool)), this,
SLOT(highlightSearchTerms()));
viewer->setSource(url);
viewer->setFocus(Qt::OtherFocusReason);
2008-12-02 12:01:29 +01:00
}
}
void CentralWidget::showTopicChooser(const QMap<QString, QUrl> &links,
const QString &keyword)
2008-12-02 12:01:29 +01:00
{
TopicChooser tc(this, keyword, links);
if (tc.exec() == QDialog::Accepted)
setSource(tc.link());
2008-12-02 12:01:29 +01:00
}
// -- protected
2008-12-02 12:01:29 +01:00
void CentralWidget::focusInEvent(QFocusEvent * /* event */)
{
// If we have a current help viewer then this is the 'focus proxy',
// otherwise it's the central widget. This is needed, so an embedding
// program can just set the focus to the central widget and it does
// The Right Thing(TM)
QObject *receiver = m_stackedWidget;
if (HelpViewer *viewer = currentHelpViewer())
receiver = viewer;
QTimer::singleShot(1, receiver, SLOT(setFocus()));
2008-12-02 12:01:29 +01:00
}
// -- private slots
void CentralWidget::highlightSearchTerms()
2008-12-02 12:01:29 +01:00
{
if (HelpViewer *viewer = currentHelpViewer()) {
QHelpSearchEngine *searchEngine =
LocalHelpManager::helpEngine().searchEngine();
QList<QHelpSearchQuery> queryList = searchEngine->query();
2008-12-02 12:01:29 +01:00
QStringList terms;
foreach (const QHelpSearchQuery &query, queryList) {
switch (query.fieldName) {
default: break;
case QHelpSearchQuery::ALL: {
case QHelpSearchQuery::PHRASE:
case QHelpSearchQuery::DEFAULT:
case QHelpSearchQuery::ATLEAST:
foreach (QString term, query.wordList)
2011-04-19 15:42:14 +02:00
terms.append(term.remove(QLatin1Char('"')));
2009-06-25 15:20:38 +02:00
}
}
2008-12-02 12:01:29 +01:00
}
foreach (const QString& term, terms)
viewer->findText(term, 0, false, true);
disconnect(viewer, SIGNAL(loadFinished(bool)), this,
SLOT(highlightSearchTerms()));
2008-12-02 12:01:29 +01:00
}
}
void CentralWidget::printPreview(QPrinter *p)
2008-12-02 12:01:29 +01:00
{
#ifndef QT_NO_PRINTER
HelpViewer *viewer = currentHelpViewer();
if (viewer)
viewer->print(p);
2008-12-02 12:01:29 +01:00
#else
Q_UNUSED(p)
2008-12-02 12:01:29 +01:00
#endif
}
void CentralWidget::handleSourceChanged(const QUrl &url)
2008-12-02 12:01:29 +01:00
{
if (sender() == currentHelpViewer())
emit sourceChanged(url);
2008-12-02 12:01:29 +01:00
}
// -- private
void CentralWidget::initPrinter()
2008-12-02 12:01:29 +01:00
{
#ifndef QT_NO_PRINTER
if (!printer)
printer = new QPrinter(QPrinter::HighResolution);
#endif
2008-12-02 12:01:29 +01:00
}
void CentralWidget::connectSignals(HelpViewer *page)
{
connect(page, SIGNAL(sourceChanged(QUrl)), this, SLOT(handleSourceChanged(QUrl)));
connect(page, SIGNAL(forwardAvailable(bool)), this, SIGNAL(forwardAvailable(bool)));
connect(page, SIGNAL(backwardAvailable(bool)), this, SIGNAL(backwardAvailable(bool)));
connect(page, SIGNAL(printRequested()), this, SLOT(print()));
connect(page, SIGNAL(openFindToolBar()), this, SIGNAL(openFindToolBar()));
}
bool CentralWidget::eventFilter(QObject *object, QEvent *e)
2008-12-02 12:01:29 +01:00
{
if (e->type() != QEvent::KeyPress)
return QWidget::eventFilter(object, e);
HelpViewer *viewer = currentHelpViewer();
QKeyEvent *keyEvent = static_cast<QKeyEvent*> (e);
if (viewer == object && keyEvent->key() == Qt::Key_Backspace) {
if (viewer->isBackwardAvailable()) {
#if !defined(QT_NO_WEBKIT)
// this helps in case there is an html <input> field
if (!viewer->hasFocus())
#endif
viewer->backward();
}
}
return QWidget::eventFilter(object, e);
2008-12-02 12:01:29 +01:00
}