source: branches/vendor/trolltech/qt/current/tools/assistant/tools/assistant/contentwindow.cpp @ 2

Last change on this file since 2 was 2, checked in by Dmitry A. Kuminov, 13 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the Qt Assistant of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file.  Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file.  Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "contentwindow.h"
43#include "centralwidget.h"
44
45#include <QtGui/QLayout>
46#include <QtGui/QFocusEvent>
47#include <QtGui/QMenu>
48
49#include <QtHelp/QHelpEngine>
50#include <QtHelp/QHelpContentWidget>
51
52QT_BEGIN_NAMESPACE
53
54ContentWindow::ContentWindow(QHelpEngine *helpEngine)
55    : m_helpEngine(helpEngine)
56    , m_contentWidget(0)
57    , m_expandDepth(-2)
58{
59    m_contentWidget = m_helpEngine->contentWidget();
60    m_contentWidget->viewport()->installEventFilter(this);
61    m_contentWidget->setContextMenuPolicy(Qt::CustomContextMenu);
62
63    QVBoxLayout *layout = new QVBoxLayout(this);
64    layout->setMargin(4);
65    layout->addWidget(m_contentWidget);
66
67    connect(m_contentWidget, SIGNAL(customContextMenuRequested(QPoint)), this,
68        SLOT(showContextMenu(QPoint)));
69    connect(m_contentWidget, SIGNAL(linkActivated(QUrl)), this,
70        SIGNAL(linkActivated(QUrl)));
71
72    QHelpContentModel *contentModel =
73        qobject_cast<QHelpContentModel*>(m_contentWidget->model());
74    connect(contentModel, SIGNAL(contentsCreated()), this, SLOT(expandTOC()));
75}
76
77ContentWindow::~ContentWindow()
78{
79}
80
81bool ContentWindow::syncToContent(const QUrl& url)
82{
83    QModelIndex idx = m_contentWidget->indexOf(url);
84    if (!idx.isValid())
85        return false;
86    m_contentWidget->setCurrentIndex(idx);
87    return true;
88}
89
90void ContentWindow::expandTOC()
91{
92    if (m_expandDepth > -2) {
93        expandToDepth(m_expandDepth);
94        m_expandDepth = -2;
95    }
96}
97
98void ContentWindow::expandToDepth(int depth)
99{
100    m_expandDepth = depth;
101    if (depth == -1)
102        m_contentWidget->expandAll();
103    else
104        m_contentWidget->expandToDepth(depth);
105}
106
107void ContentWindow::focusInEvent(QFocusEvent *e)
108{
109    if (e->reason() != Qt::MouseFocusReason)
110        m_contentWidget->setFocus();
111}
112
113void ContentWindow::keyPressEvent(QKeyEvent *e)
114{
115    if (e->key() == Qt::Key_Escape)
116        emit escapePressed();
117}
118
119bool ContentWindow::eventFilter(QObject *o, QEvent *e)
120{
121    if (m_contentWidget && o == m_contentWidget->viewport()
122        && e->type() == QEvent::MouseButtonRelease) {
123        QMouseEvent *me = static_cast<QMouseEvent*>(e);
124        QModelIndex index = m_contentWidget->indexAt(me->pos());
125        QItemSelectionModel *sm = m_contentWidget->selectionModel();
126
127        if (index.isValid() && (sm && sm->isSelected(index))) {
128            if (me->button() == Qt::LeftButton) {
129                itemClicked(index);
130            } else if (me->button() == Qt::MidButton) {
131                QHelpContentModel *contentModel =
132                    qobject_cast<QHelpContentModel*>(m_contentWidget->model());
133                if (contentModel) {
134                    QHelpContentItem *itm = contentModel->contentItemAt(index);
135                    if (itm && !isPdfFile(itm))
136                        CentralWidget::instance()->setSourceInNewTab(itm->url());
137                }
138            }
139        }
140    }
141    return QWidget::eventFilter(o, e);
142}
143
144void ContentWindow::showContextMenu(const QPoint &pos)
145{
146    if (!m_contentWidget->indexAt(pos).isValid())
147        return;
148
149    QHelpContentModel *contentModel =
150        qobject_cast<QHelpContentModel*>(m_contentWidget->model());
151    QHelpContentItem *itm =
152        contentModel->contentItemAt(m_contentWidget->currentIndex());
153
154    QMenu menu;
155    QAction *curTab = menu.addAction(tr("Open Link"));
156    QAction *newTab = menu.addAction(tr("Open Link in New Tab"));
157    if (isPdfFile(itm))
158        newTab->setEnabled(false);
159   
160    menu.move(m_contentWidget->mapToGlobal(pos));
161
162    QAction *action = menu.exec();
163    if (curTab == action)
164        emit linkActivated(itm->url());
165    else if (newTab == action)
166        CentralWidget::instance()->setSourceInNewTab(itm->url());
167}
168
169void ContentWindow::itemClicked(const QModelIndex &index)
170{
171    QHelpContentModel *contentModel =
172        qobject_cast<QHelpContentModel*>(m_contentWidget->model());
173
174    if (contentModel) {
175        QHelpContentItem *itm = contentModel->contentItemAt(index);
176        if (itm)
177            emit linkActivated(itm->url());
178    }
179}
180
181bool ContentWindow::isPdfFile(QHelpContentItem *item) const
182{
183    const QString &path = item->url().path();
184    return path.endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive);
185}
186
187QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.