centralwidget.cpp 10.3 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29 30

#include "centralwidget.h"
31

32
#include "helpmanager.h"
con's avatar
con committed
33 34 35 36 37 38
#include "helpviewer.h"
#include "topicchooser.h"

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

39
#include <QtGui/QKeyEvent>
con's avatar
con committed
40
#include <QtGui/QLayout>
41
#include <QtGui/QPageSetupDialog>
con's avatar
con committed
42 43 44
#include <QtGui/QPrinter>
#include <QtGui/QPrintDialog>
#include <QtGui/QPrintPreviewDialog>
45
#include <QtGui/QStackedWidget>
con's avatar
con committed
46

47 48
#include <QtHelp/QHelpEngine>
#include <QtHelp/QHelpSearchEngine>
49

con's avatar
con committed
50 51
using namespace Help::Internal;

52 53 54
CentralWidget *gStaticCentralWidget = 0;

// -- CentralWidget
con's avatar
con committed
55

56
CentralWidget::CentralWidget(QWidget *parent)
con's avatar
con committed
57 58
    : QWidget(parent)
    , printer(0)
59
    , m_stackedWidget(0)
con's avatar
con committed
60
{
61 62
    Q_ASSERT(!gStaticCentralWidget);
    gStaticCentralWidget = this;
con's avatar
con committed
63 64 65

    QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    vboxLayout->setMargin(0);
66 67
    m_stackedWidget = new QStackedWidget(this);
    vboxLayout->addWidget(m_stackedWidget);
con's avatar
con committed
68 69 70 71
}

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

76
    QString zoomFactors;
kh's avatar
kh committed
77
    QString currentPages;
78 79 80 81 82 83
    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('|');
84
        }
con's avatar
con committed
85
    }
86

kh1's avatar
kh1 committed
87
    QHelpEngineCore *engine = &LocalHelpManager::helpEngine();
88
    engine->setCustomValue(QLatin1String("LastShownPages"), currentPages);
89 90
    engine->setCustomValue(QLatin1String("LastShownPagesZoom"), zoomFactors);
    engine->setCustomValue(QLatin1String("LastTabPage"), currentIndex());
con's avatar
con committed
91 92 93 94
}

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

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

106
bool CentralWidget::isForwardAvailable() const
con's avatar
con committed
107
{
108
    const HelpViewer* viewer = currentHelpViewer();
con's avatar
con committed
109
    if (viewer)
110
        return viewer->isForwardAvailable();
con's avatar
con committed
111

112
    return false;
con's avatar
con committed
113 114
}

115
bool CentralWidget::isBackwardAvailable() const
con's avatar
con committed
116
{
117
    const HelpViewer* viewer = currentHelpViewer();
con's avatar
con committed
118
    if (viewer)
119
        return viewer->isBackwardAvailable();
con's avatar
con committed
120

121
    return false;
con's avatar
con committed
122 123
}

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

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

134
void CentralWidget::addPage(HelpViewer *page, bool fromSearch)
con's avatar
con committed
135
{
136 137 138 139 140 141 142 143
    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
144 145
}

146
void CentralWidget::removePage(int index)
con's avatar
con committed
147
{
148 149 150 151
    const bool currentChanged = (index == currentIndex());
    m_stackedWidget->removeWidget(m_stackedWidget->widget(index));
    if (currentChanged)
        emit currentViewerChanged();
con's avatar
con committed
152 153
}

154
int CentralWidget::currentIndex() const
con's avatar
con committed
155
{
156
    return  m_stackedWidget->currentIndex();
con's avatar
con committed
157 158
}

159
void CentralWidget::setCurrentPage(HelpViewer *page)
con's avatar
con committed
160
{
161 162
    m_stackedWidget->setCurrentWidget(page);
    emit currentViewerChanged();
con's avatar
con committed
163 164
}

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

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

173
void CentralWidget::copy()
con's avatar
con committed
174
{
175 176
    if (HelpViewer* viewer = currentHelpViewer())
        viewer->copy();
con's avatar
con committed
177 178
}

179
void CentralWidget::home()
con's avatar
con committed
180
{
181 182
    if (HelpViewer* viewer = currentHelpViewer())
        viewer->home();
con's avatar
con committed
183 184
}

185
void CentralWidget::zoomIn()
con's avatar
con committed
186
{
187
    HelpViewer* viewer = currentHelpViewer();
con's avatar
con committed
188
    if (viewer)
189
        viewer->scaleUp();
con's avatar
con committed
190 191
}

192
void CentralWidget::zoomOut()
con's avatar
con committed
193 194 195
{
    HelpViewer* viewer = currentHelpViewer();
    if (viewer)
196
        viewer->scaleDown();
con's avatar
con committed
197 198
}

199
void CentralWidget::resetZoom()
con's avatar
con committed
200
{
201
    HelpViewer* viewer = currentHelpViewer();
con's avatar
con committed
202
    if (viewer)
203
        viewer->resetScale();
con's avatar
con committed
204 205 206 207
}

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

212
void CentralWidget::nextPage()
con's avatar
con committed
213
{
214 215
    m_stackedWidget->setCurrentIndex((m_stackedWidget->currentIndex() + 1)
        % m_stackedWidget->count());
con's avatar
con committed
216 217 218 219
}

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

224
void CentralWidget::previousPage()
con's avatar
con committed
225
{
226 227
    m_stackedWidget->setCurrentIndex((m_stackedWidget->currentIndex() - 1)
        % m_stackedWidget->count());
con's avatar
con committed
228
}
229

230
void CentralWidget::print()
con's avatar
con committed
231
{
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
#ifndef QT_NO_PRINTER
    if (HelpViewer* viewer = currentHelpViewer()) {
        initPrinter();

        QPrintDialog *dlg = new QPrintDialog(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);
        delete dlg;
    }
247
#endif
con's avatar
con committed
248 249
}

250
void CentralWidget::pageSetup()
con's avatar
con committed
251
{
252 253 254 255
#ifndef QT_NO_PRINTER
    initPrinter();
    QPageSetupDialog dlg(printer);
    dlg.exec();
con's avatar
con committed
256 257 258
#endif
}

259
void CentralWidget::printPreview()
con's avatar
con committed
260
{
261 262 263 264 265 266 267
#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
268 269
}

270
void CentralWidget::setSource(const QUrl &url)
con's avatar
con committed
271
{
272 273 274
    if (HelpViewer* viewer = currentHelpViewer()) {
        viewer->setSource(url);
        viewer->setFocus(Qt::OtherFocusReason);
con's avatar
con committed
275 276 277
    }
}

278
void CentralWidget::setSourceFromSearch(const QUrl &url)
con's avatar
con committed
279
{
280
    if (HelpViewer* viewer = currentHelpViewer()) {
281 282 283 284
        connect(viewer, SIGNAL(loadFinished(bool)), this,
            SLOT(highlightSearchTerms()));
        viewer->setSource(url);
        viewer->setFocus(Qt::OtherFocusReason);
con's avatar
con committed
285 286 287
    }
}

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

296
// -- protected
297

con's avatar
con committed
298 299
void CentralWidget::focusInEvent(QFocusEvent * /* event */)
{
300 301 302 303 304 305 306
    // 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;
307
    QTimer::singleShot(1, receiver, SLOT(setFocus()));
con's avatar
con committed
308 309
}

310 311 312
// -- private slots

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

319 320 321 322 323 324 325 326 327 328
        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("\"")));
kh's avatar
kh committed
329 330
                }
            }
con's avatar
con committed
331
        }
332 333 334 335 336

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

340
void CentralWidget::printPreview(QPrinter *p)
con's avatar
con committed
341
{
342 343 344 345
#ifndef QT_NO_PRINTER
    HelpViewer *viewer = currentHelpViewer();
    if (viewer)
        viewer->print(p);
con's avatar
con committed
346
#else
347
    Q_UNUSED(p)
con's avatar
con committed
348 349 350
#endif
}

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

357 358 359
// -- private

void CentralWidget::initPrinter()
con's avatar
con committed
360
{
361 362 363 364
#ifndef QT_NO_PRINTER
    if (!printer)
        printer = new QPrinter(QPrinter::HighResolution);
#endif
con's avatar
con committed
365 366
}

367
void CentralWidget::connectSignals(HelpViewer *page)
368
{
369 370 371
    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)));
372 373
}

374
bool CentralWidget::eventFilter(QObject *object, QEvent *e)
con's avatar
con committed
375
{
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    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
391
}