1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "contentwindow.h"
36 #include "centralwidget.h"
37 #include "helpviewer.h"
38 #include "localhelpmanager.h"
39 #include "openpagesmanager.h"
41 #include <QtGui/QLayout>
42 #include <QtGui/QFocusEvent>
43 #include <QtGui/QMenu>
45 #include <QtHelp/QHelpEngine>
46 #include <QtHelp/QHelpContentWidget>
48 using namespace Help::Internal;
50 ContentWindow::ContentWindow()
54 m_contentWidget = (&LocalHelpManager::helpEngine())->contentWidget();
55 m_contentWidget->installEventFilter(this);
56 m_contentWidget->viewport()->installEventFilter(this);
57 m_contentWidget->setContextMenuPolicy(Qt::CustomContextMenu);
58 setFocusProxy(m_contentWidget);
60 QVBoxLayout *layout = new QVBoxLayout(this);
62 layout->addWidget(m_contentWidget);
64 connect(m_contentWidget, SIGNAL(customContextMenuRequested(QPoint)), this,
65 SLOT(showContextMenu(QPoint)));
66 connect(m_contentWidget, SIGNAL(linkActivated(QUrl)), this,
67 SIGNAL(linkActivated(QUrl)));
69 QHelpContentModel *contentModel =
70 qobject_cast<QHelpContentModel*>(m_contentWidget->model());
71 connect(contentModel, SIGNAL(contentsCreated()), this, SLOT(expandTOC()));
73 m_contentWidget->setFrameStyle(QFrame::NoFrame);
76 ContentWindow::~ContentWindow()
80 bool ContentWindow::syncToContent(const QUrl& url)
82 QModelIndex idx = m_contentWidget->indexOf(url);
85 m_contentWidget->setCurrentIndex(idx);
89 void ContentWindow::expandTOC()
91 if (m_expandDepth > -2) {
92 expandToDepth(m_expandDepth);
97 void ContentWindow::expandToDepth(int depth)
99 m_expandDepth = depth;
101 m_contentWidget->expandAll();
103 m_contentWidget->expandToDepth(depth);
106 bool ContentWindow::eventFilter(QObject *o, QEvent *e)
108 if (m_contentWidget && o == m_contentWidget->viewport()
109 && e->type() == QEvent::MouseButtonRelease) {
110 QMouseEvent *me = static_cast<QMouseEvent*>(e);
111 QItemSelectionModel *sm = m_contentWidget->selectionModel();
113 return QWidget::eventFilter(o, e);
115 Qt::MouseButtons button = me->button();
116 const QModelIndex &index = m_contentWidget->indexAt(me->pos());
118 if (index.isValid() && sm->isSelected(index)) {
119 if ((button == Qt::LeftButton && (me->modifiers() & Qt::ControlModifier))
120 || (button == Qt::MidButton)) {
121 QHelpContentModel *contentModel =
122 qobject_cast<QHelpContentModel*>(m_contentWidget->model());
124 QHelpContentItem *itm = contentModel->contentItemAt(index);
125 if (itm && HelpViewer::canOpenPage(itm->url().path()))
126 OpenPagesManager::instance().createPage(itm->url());
128 } else if (button == Qt::LeftButton) {
133 return QWidget::eventFilter(o, e);
136 void ContentWindow::showContextMenu(const QPoint &pos)
138 if (!m_contentWidget->indexAt(pos).isValid())
141 QHelpContentModel *contentModel =
142 qobject_cast<QHelpContentModel*>(m_contentWidget->model());
143 QHelpContentItem *itm =
144 contentModel->contentItemAt(m_contentWidget->currentIndex());
147 QAction *curTab = menu.addAction(tr("Open Link"));
148 QAction *newTab = menu.addAction(tr("Open Link as New Page"));
149 if (!HelpViewer::canOpenPage(itm->url().path()))
150 newTab->setEnabled(false);
152 menu.move(m_contentWidget->mapToGlobal(pos));
154 QAction *action = menu.exec();
155 if (curTab == action)
156 emit linkActivated(itm->url());
157 else if (newTab == action)
158 OpenPagesManager::instance().createPage(itm->url());
161 void ContentWindow::itemClicked(const QModelIndex &index)
163 QHelpContentModel *contentModel =
164 qobject_cast<QHelpContentModel*>(m_contentWidget->model());
167 if (QHelpContentItem *itm = contentModel->contentItemAt(index)) {
168 const QUrl &url = itm->url();
169 if (url != CentralWidget::instance()->currentHelpViewer()->source())
170 emit linkActivated(itm->url());