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 "centralwidget.h"
36 #include "helpviewer.h"
37 #include "indexwindow.h"
38 #include "localhelpmanager.h"
39 #include "openpagesmanager.h"
40 #include "topicchooser.h"
42 #include <utils/filterlineedit.h>
43 #include <utils/styledbar.h>
45 #include <QtGui/QLayout>
46 #include <QtGui/QLabel>
47 #include <QtGui/QLineEdit>
48 #include <QtGui/QKeyEvent>
49 #include <QtGui/QMenu>
50 #include <QtGui/QContextMenuEvent>
51 #include <QtGui/QListWidgetItem>
53 #include <QtHelp/QHelpEngine>
54 #include <QtHelp/QHelpIndexWidget>
56 using namespace Help::Internal;
58 IndexWindow::IndexWindow()
62 QVBoxLayout *layout = new QVBoxLayout(this);
64 m_searchLineEdit = new Utils::FilterLineEdit();
65 m_searchLineEdit->setPlaceholderText(QString());
66 setFocusProxy(m_searchLineEdit);
67 connect(m_searchLineEdit, SIGNAL(textChanged(QString)), this,
68 SLOT(filterIndices(QString)));
69 m_searchLineEdit->installEventFilter(this);
71 QLabel *l = new QLabel(tr("&Look for:"));
72 l->setBuddy(m_searchLineEdit);
75 layout->setSpacing(0);
77 Utils::StyledBar *toolbar = new Utils::StyledBar(this);
78 toolbar->setSingleRow(false);
79 QLayout *tbLayout = new QHBoxLayout();
80 tbLayout->setSpacing(6);
81 tbLayout->setMargin(4);
82 tbLayout->addWidget(l);
83 tbLayout->addWidget(m_searchLineEdit);
84 toolbar->setLayout(tbLayout);
85 layout->addWidget(toolbar);
87 QHelpEngine *engine = &LocalHelpManager::helpEngine();
88 m_indexWidget = engine->indexWidget();
89 m_indexWidget->installEventFilter(this);
90 connect(engine->indexModel(), SIGNAL(indexCreationStarted()), this,
91 SLOT(disableSearchLineEdit()));
92 connect(engine->indexModel(), SIGNAL(indexCreated()), this,
93 SLOT(enableSearchLineEdit()));
94 connect(m_indexWidget, SIGNAL(linkActivated(QUrl, QString)), this,
95 SIGNAL(linkActivated(QUrl)));
96 connect(m_indexWidget, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)),
97 this, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)));
98 connect(m_searchLineEdit, SIGNAL(returnPressed()), m_indexWidget,
99 SLOT(activateCurrentItem()));
100 m_indexWidget->setFrameStyle(QFrame::NoFrame);
101 layout->addWidget(m_indexWidget);
103 m_indexWidget->viewport()->installEventFilter(this);
106 IndexWindow::~IndexWindow()
110 void IndexWindow::filterIndices(const QString &filter)
112 if (filter.contains(QLatin1Char('*')))
113 m_indexWidget->filterIndices(filter, filter);
115 m_indexWidget->filterIndices(filter, QString());
118 bool IndexWindow::eventFilter(QObject *obj, QEvent *e)
120 if (obj == m_searchLineEdit && e->type() == QEvent::KeyPress) {
121 QKeyEvent *ke = static_cast<QKeyEvent*>(e);
122 QModelIndex idx = m_indexWidget->currentIndex();
125 idx = m_indexWidget->model()->index(idx.row()-1,
126 idx.column(), idx.parent());
128 m_indexWidget->setCurrentIndex(idx);
131 idx = m_indexWidget->model()->index(idx.row()+1,
132 idx.column(), idx.parent());
134 m_indexWidget->setCurrentIndex(idx);
136 default: ; // stop complaining
138 } else if (obj == m_searchLineEdit
139 && e->type() == QEvent::FocusIn
140 && static_cast<QFocusEvent *>(e)->reason() != Qt::MouseFocusReason) {
141 m_searchLineEdit->selectAll();
142 m_searchLineEdit->setFocus();
143 } else if (obj == m_indexWidget && e->type() == QEvent::ContextMenu) {
144 QContextMenuEvent *ctxtEvent = static_cast<QContextMenuEvent*>(e);
145 QModelIndex idx = m_indexWidget->indexAt(ctxtEvent->pos());
148 QAction *curTab = menu.addAction(tr("Open Link"));
149 QAction *newTab = menu.addAction(tr("Open Link as New Page"));
150 menu.move(m_indexWidget->mapToGlobal(ctxtEvent->pos()));
152 QAction *action = menu.exec();
153 if (curTab == action)
154 m_indexWidget->activateCurrentItem();
155 else if (newTab == action) {
156 open(m_indexWidget, idx);
159 } else if (m_indexWidget && obj == m_indexWidget->viewport()
160 && e->type() == QEvent::MouseButtonRelease) {
161 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
162 QModelIndex idx = m_indexWidget->indexAt(mouseEvent->pos());
164 Qt::MouseButtons button = mouseEvent->button();
165 if (((button == Qt::LeftButton) && (mouseEvent->modifiers() & Qt::ControlModifier))
166 || (button == Qt::MidButton)) {
167 open(m_indexWidget, idx);
172 else if (obj == m_indexWidget && e->type() == QEvent::KeyPress) {
173 QKeyEvent *ke = static_cast<QKeyEvent*>(e);
174 if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter)
175 m_indexWidget->activateCurrentItem();
179 return QWidget::eventFilter(obj, e);
182 void IndexWindow::enableSearchLineEdit()
184 m_searchLineEdit->setDisabled(false);
185 filterIndices(m_searchLineEdit->text());
188 void IndexWindow::disableSearchLineEdit()
190 m_searchLineEdit->setDisabled(true);
193 void IndexWindow::setSearchLineEditText(const QString &text)
195 m_searchLineEdit->setText(text);
198 void IndexWindow::open(QHelpIndexWidget* indexWidget, const QModelIndex &index)
200 QHelpIndexModel *model = qobject_cast<QHelpIndexModel*>(indexWidget->model());
202 QString keyword = model->data(index, Qt::DisplayRole).toString();
203 QMap<QString, QUrl> links = model->linksForKeyword(keyword);
206 if (links.count() > 1) {
207 TopicChooser tc(this, keyword, links);
208 if (tc.exec() == QDialog::Accepted)
210 } else if (links.count() == 1) {
211 url = links.constBegin().value();
216 if (!HelpViewer::canOpenPage(url.path()))
217 CentralWidget::instance()->setSource(url);
219 OpenPagesManager::instance().createPage(url);