Skip to content
Snippets Groups Projects
centralwidget.cpp 10.4 KiB
Newer Older
/**************************************************************************
con's avatar
con committed
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
**
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
**
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
** 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.
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
**
**************************************************************************/
con's avatar
con committed

#include "centralwidget.h"
#include "helpmanager.h"
con's avatar
con committed
#include "helpviewer.h"
#include "topicchooser.h"

#include <QtCore/QEvent>
#include <QtCore/QTimer>

#include <QtGui/QKeyEvent>
con's avatar
con committed
#include <QtGui/QLayout>
#include <QtGui/QPageSetupDialog>
con's avatar
con committed
#include <QtGui/QPrinter>
#include <QtGui/QPrintDialog>
#include <QtGui/QPrintPreviewDialog>
#include <QtGui/QStackedWidget>
con's avatar
con committed

#include <QtHelp/QHelpEngine>
#include <QtHelp/QHelpSearchEngine>
con's avatar
con committed
using namespace Help::Internal;

CentralWidget *gStaticCentralWidget = 0;

// -- CentralWidget
con's avatar
con committed

CentralWidget::CentralWidget(QWidget *parent)
con's avatar
con committed
    : QWidget(parent)
    , printer(0)
    , m_stackedWidget(0)
con's avatar
con committed
{
    Q_ASSERT(!gStaticCentralWidget);
    gStaticCentralWidget = this;
con's avatar
con committed

    QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    vboxLayout->setMargin(0);
    m_stackedWidget = new QStackedWidget(this);
    vboxLayout->addWidget(m_stackedWidget);
con's avatar
con committed
}

CentralWidget::~CentralWidget()
{
kh's avatar
kh committed
#ifndef QT_NO_PRINTER
    delete printer;
#endif

    QString zoomFactors;
kh's avatar
kh committed
    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('|');
con's avatar
con committed
    }
kh1's avatar
kh1 committed
    QHelpEngineCore *engine = &LocalHelpManager::helpEngine();
    engine->setCustomValue(QLatin1String("LastShownPages"), currentPages);
    engine->setCustomValue(QLatin1String("LastShownPagesZoom"), zoomFactors);
    engine->setCustomValue(QLatin1String("LastTabPage"), currentIndex());
con's avatar
con committed
}

CentralWidget *CentralWidget::instance()
{
    Q_ASSERT(gStaticCentralWidget);
    return gStaticCentralWidget;
con's avatar
con committed
}

bool CentralWidget::hasSelection() const
con's avatar
con committed
{
    if (HelpViewer* viewer = currentHelpViewer())
        return !viewer->selectedText().isEmpty();
    return false;
con's avatar
con committed
}

bool CentralWidget::isForwardAvailable() const
con's avatar
con committed
{
    const HelpViewer* viewer = currentHelpViewer();
con's avatar
con committed
    if (viewer)
        return viewer->isForwardAvailable();
con's avatar
con committed

con's avatar
con committed
}

bool CentralWidget::isBackwardAvailable() const
con's avatar
con committed
{
    const HelpViewer* viewer = currentHelpViewer();
con's avatar
con committed
    if (viewer)
        return viewer->isBackwardAvailable();
con's avatar
con committed

con's avatar
con committed
}

HelpViewer* CentralWidget::viewerAt(int index) const
con's avatar
con committed
{
    return qobject_cast<HelpViewer*> (m_stackedWidget->widget(index));
con's avatar
con committed
}

HelpViewer* CentralWidget::currentHelpViewer() const
con's avatar
con committed
{
    return qobject_cast<HelpViewer*> (m_stackedWidget->currentWidget());
con's avatar
con committed
}

void CentralWidget::addPage(HelpViewer *page, bool fromSearch)
con's avatar
con committed
{
    page->installEventFilter(this);
    page->setFocus(Qt::OtherFocusReason);
    connectSignals(page);
    m_stackedWidget->addWidget(page);
    if (fromSearch) {
        connect(currentHelpViewer(), SIGNAL(loadFinished(bool)), this,
            SLOT(highlightSearchTerms()));
     }
con's avatar
con committed
}

void CentralWidget::removePage(int index)
con's avatar
con committed
{
    const bool currentChanged = (index == currentIndex());
    m_stackedWidget->removeWidget(m_stackedWidget->widget(index));
    if (currentChanged)
        emit currentViewerChanged();
con's avatar
con committed
}

int CentralWidget::currentIndex() const
con's avatar
con committed
{
    return  m_stackedWidget->currentIndex();
con's avatar
con committed
}

void CentralWidget::setCurrentPage(HelpViewer *page)
con's avatar
con committed
{
    m_stackedWidget->setCurrentWidget(page);
    emit currentViewerChanged();
con's avatar
con committed
}

bool CentralWidget::find(const QString &txt, Find::FindFlags flags,
    bool incremental)
con's avatar
con committed
{
    return currentHelpViewer()->findText(txt, flags, incremental, false);
con's avatar
con committed
}

// -- public slots
con's avatar
con committed

void CentralWidget::copy()
con's avatar
con committed
{
    if (HelpViewer* viewer = currentHelpViewer())
        viewer->copy();
con's avatar
con committed
}

void CentralWidget::home()
con's avatar
con committed
{
    if (HelpViewer* viewer = currentHelpViewer())
        viewer->home();
con's avatar
con committed
}

void CentralWidget::zoomIn()
con's avatar
con committed
{
    HelpViewer* viewer = currentHelpViewer();
con's avatar
con committed
    if (viewer)
        viewer->scaleUp();
con's avatar
con committed
}

void CentralWidget::zoomOut()
con's avatar
con committed
{
    HelpViewer* viewer = currentHelpViewer();
    if (viewer)
        viewer->scaleDown();
con's avatar
con committed
}

void CentralWidget::resetZoom()
con's avatar
con committed
{
    HelpViewer* viewer = currentHelpViewer();
con's avatar
con committed
    if (viewer)
        viewer->resetScale();
con's avatar
con committed
}

void CentralWidget::forward()
{
    if (HelpViewer* viewer = currentHelpViewer())
con's avatar
con committed
        viewer->forward();
}

void CentralWidget::nextPage()
con's avatar
con committed
{
    m_stackedWidget->setCurrentIndex((m_stackedWidget->currentIndex() + 1)
        % m_stackedWidget->count());
con's avatar
con committed
}

void CentralWidget::backward()
{
    if (HelpViewer* viewer = currentHelpViewer())
con's avatar
con committed
        viewer->backward();
}

void CentralWidget::previousPage()
con's avatar
con committed
{
    m_stackedWidget->setCurrentIndex((m_stackedWidget->currentIndex() - 1)
        % m_stackedWidget->count());
con's avatar
con committed
}
void CentralWidget::print()
con's avatar
con committed
{
#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);
    }
con's avatar
con committed
}

void CentralWidget::pageSetup()
con's avatar
con committed
{
#ifndef QT_NO_PRINTER
    initPrinter();
    QPageSetupDialog dlg(printer);
    dlg.exec();
con's avatar
con committed
#endif
}

void CentralWidget::printPreview()
con's avatar
con committed
{
#ifndef QT_NO_PRINTER
    initPrinter();
    QPrintPreviewDialog preview(printer, this);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)),
        SLOT(printPreview(QPrinter*)));
    preview.exec();
#endif
con's avatar
con committed
}

void CentralWidget::setSource(const QUrl &url)
con's avatar
con committed
{
    if (HelpViewer* viewer = currentHelpViewer()) {
        viewer->setSource(url);
        viewer->setFocus(Qt::OtherFocusReason);
con's avatar
con committed
    }
}

void CentralWidget::setSourceFromSearch(const QUrl &url)
con's avatar
con committed
{
    if (HelpViewer* viewer = currentHelpViewer()) {
        connect(viewer, SIGNAL(loadFinished(bool)), this,
            SLOT(highlightSearchTerms()));
        viewer->setSource(url);
        viewer->setFocus(Qt::OtherFocusReason);
con's avatar
con committed
    }
}

void CentralWidget::showTopicChooser(const QMap<QString, QUrl> &links,
    const QString &keyword)
con's avatar
con committed
{
    TopicChooser tc(this, keyword, links);
    if (tc.exec() == QDialog::Accepted)
        setSource(tc.link());
con's avatar
con committed
}

con's avatar
con committed
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()));
con's avatar
con committed
}

// -- private slots

void CentralWidget::highlightSearchTerms()
con's avatar
con committed
{
    if (HelpViewer *viewer = currentHelpViewer()) {
        QHelpSearchEngine *searchEngine = 
kh1's avatar
kh1 committed
            LocalHelpManager::helpEngine().searchEngine();
        QList<QHelpSearchQuery> queryList = searchEngine->query();
con's avatar
con committed

        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)
                        terms.append(term.remove(QLatin1String("\"")));
con's avatar
con committed
        }

        foreach (const QString& term, terms)
            viewer->findText(term, 0, false, true);
        disconnect(viewer, SIGNAL(loadFinished(bool)), this,
            SLOT(highlightSearchTerms()));
con's avatar
con committed
    }
}

void CentralWidget::printPreview(QPrinter *p)
con's avatar
con committed
{
#ifndef QT_NO_PRINTER
    HelpViewer *viewer = currentHelpViewer();
    if (viewer)
        viewer->print(p);
con's avatar
con committed
#else
con's avatar
con committed
#endif
}

void CentralWidget::handleSourceChanged(const QUrl &url)
con's avatar
con committed
{
    if (sender() == currentHelpViewer())
        emit sourceChanged(url);
con's avatar
con committed
}

// -- private

void CentralWidget::initPrinter()
con's avatar
con committed
{
#ifndef QT_NO_PRINTER
    if (!printer)
        printer = new QPrinter(QPrinter::HighResolution);
#endif
con's avatar
con committed
}

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)
con's avatar
con committed
{
    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);
con's avatar
con committed
}