forked from qt-creator/qt-creator
210 lines
7.5 KiB
C++
210 lines
7.5 KiB
C++
/***************************************************************************
|
|
**
|
|
** This file is part of Qt Creator
|
|
**
|
|
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
**
|
|
** Contact: Qt Software Information (qt-info@nokia.com)
|
|
**
|
|
**
|
|
** Non-Open Source Usage
|
|
**
|
|
** Licensees may use this file in accordance with the Qt Beta Version
|
|
** License Agreement, Agreement version 2.2 provided with the Software or,
|
|
** alternatively, in accordance with the terms contained in a written
|
|
** agreement between you and Nokia.
|
|
**
|
|
** GNU General Public License Usage
|
|
**
|
|
** Alternatively, this file may be used under the terms of the GNU General
|
|
** Public License versions 2.0 or 3.0 as published by the Free Software
|
|
** Foundation and appearing in the file LICENSE.GPL included in the packaging
|
|
** of this file. Please review the following information to ensure GNU
|
|
** General Public Licensing requirements will be met:
|
|
**
|
|
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
|
** http://www.gnu.org/copyleft/gpl.html.
|
|
**
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
** rights. These rights are described in the Nokia Qt GPL Exception version
|
|
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
|
|
**
|
|
***************************************************************************/
|
|
|
|
#include "indexwindow.h"
|
|
#include "centralwidget.h"
|
|
#include "topicchooser.h"
|
|
|
|
#include <QtGui/QLayout>
|
|
#include <QtGui/QLabel>
|
|
#include <QtGui/QLineEdit>
|
|
#include <QtGui/QKeyEvent>
|
|
#include <QtGui/QMenu>
|
|
#include <QtGui/QContextMenuEvent>
|
|
#include <QtGui/QListWidgetItem>
|
|
|
|
#include <QtHelp/QHelpEngine>
|
|
#include <QtHelp/QHelpIndexWidget>
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
IndexWindow::IndexWindow(QHelpEngine *helpEngine, QWidget *parent)
|
|
: QWidget(parent)
|
|
, m_searchLineEdit(0)
|
|
, m_indexWidget(0)
|
|
, m_helpEngine(helpEngine)
|
|
{
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
QLabel *l = new QLabel(tr("&Look for:"));
|
|
layout->addWidget(l);
|
|
|
|
m_searchLineEdit = new QLineEdit();
|
|
l->setBuddy(m_searchLineEdit);
|
|
connect(m_searchLineEdit, SIGNAL(textChanged(const QString&)),
|
|
this, SLOT(filterIndices(const QString&)));
|
|
m_searchLineEdit->installEventFilter(this);
|
|
layout->setMargin(4);
|
|
layout->addWidget(m_searchLineEdit);
|
|
|
|
m_indexWidget = m_helpEngine->indexWidget();
|
|
m_indexWidget->installEventFilter(this);
|
|
connect(m_helpEngine->indexModel(), SIGNAL(indexCreationStarted()),
|
|
this, SLOT(disableSearchLineEdit()));
|
|
connect(m_helpEngine->indexModel(), SIGNAL(indexCreated()),
|
|
this, SLOT(enableSearchLineEdit()));
|
|
connect(m_indexWidget, SIGNAL(linkActivated(const QUrl&, const QString&)),
|
|
this, SIGNAL(linkActivated(const QUrl&)));
|
|
connect(m_indexWidget, SIGNAL(linksActivated(const QMap<QString, QUrl>&,
|
|
const QString&)), this, SIGNAL(linksActivated(const QMap<QString, QUrl>&,
|
|
const QString&)));
|
|
connect(m_searchLineEdit, SIGNAL(returnPressed()),
|
|
m_indexWidget, SLOT(activateCurrentItem()));
|
|
layout->addWidget(m_indexWidget);
|
|
|
|
m_indexWidget->viewport()->installEventFilter(this);
|
|
}
|
|
|
|
IndexWindow::~IndexWindow()
|
|
{
|
|
}
|
|
|
|
void IndexWindow::filterIndices(const QString &filter)
|
|
{
|
|
if (filter.contains(QLatin1Char('*')))
|
|
m_indexWidget->filterIndices(filter, filter);
|
|
else
|
|
m_indexWidget->filterIndices(filter, QString());
|
|
}
|
|
|
|
bool IndexWindow::eventFilter(QObject *obj, QEvent *e)
|
|
{
|
|
if (obj == m_searchLineEdit && e->type() == QEvent::KeyPress) {
|
|
QKeyEvent *ke = static_cast<QKeyEvent*>(e);
|
|
QModelIndex idx = m_indexWidget->currentIndex();
|
|
switch (ke->key()) {
|
|
case Qt::Key_Up:
|
|
idx = m_indexWidget->model()->index(idx.row()-1,
|
|
idx.column(), idx.parent());
|
|
if (idx.isValid())
|
|
m_indexWidget->setCurrentIndex(idx);
|
|
break;
|
|
case Qt::Key_Down:
|
|
idx = m_indexWidget->model()->index(idx.row()+1,
|
|
idx.column(), idx.parent());
|
|
if (idx.isValid())
|
|
m_indexWidget->setCurrentIndex(idx);
|
|
break;
|
|
case Qt::Key_Escape:
|
|
emit escapePressed();
|
|
break;
|
|
default:
|
|
;
|
|
}
|
|
} else if (obj == m_indexWidget && e->type() == QEvent::ContextMenu) {
|
|
QContextMenuEvent *ctxtEvent = static_cast<QContextMenuEvent*>(e);
|
|
QModelIndex idx = m_indexWidget->indexAt(ctxtEvent->pos());
|
|
if (idx.isValid()) {
|
|
QMenu menu;
|
|
QAction *curTab = menu.addAction(tr("Open Link"));
|
|
QAction *newTab = menu.addAction(tr("Open Link in New Tab"));
|
|
menu.move(m_indexWidget->mapToGlobal(ctxtEvent->pos()));
|
|
|
|
QAction *action = menu.exec();
|
|
if (curTab == action)
|
|
m_indexWidget->activateCurrentItem();
|
|
else if (newTab == action) {
|
|
QHelpIndexModel *model =
|
|
qobject_cast<QHelpIndexModel*>(m_indexWidget->model());
|
|
QString keyword = model->data(idx, Qt::DisplayRole).toString();
|
|
if (model) {
|
|
QMap<QString, QUrl> links = model->linksForKeyword(keyword);
|
|
if (links.count() == 1) {
|
|
CentralWidget::instance()->
|
|
setSourceInNewTab(links.constBegin().value());
|
|
} else {
|
|
TopicChooser tc(this, keyword, links);
|
|
if (tc.exec() == QDialog::Accepted) {
|
|
CentralWidget::instance()->setSourceInNewTab(tc.link());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else if (m_indexWidget && obj == m_indexWidget->viewport()
|
|
&& e->type() == QEvent::MouseButtonRelease) {
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
|
|
QModelIndex idx = m_indexWidget->indexAt(mouseEvent->pos());
|
|
if (idx.isValid() && mouseEvent->button()==Qt::MidButton) {
|
|
QHelpIndexModel *model =
|
|
qobject_cast<QHelpIndexModel*>(m_indexWidget->model());
|
|
QString keyword = model->data(idx, Qt::DisplayRole).toString();
|
|
if (model) {
|
|
QMap<QString, QUrl> links = model->linksForKeyword(keyword);
|
|
if (links.count() > 1) {
|
|
TopicChooser tc(this, keyword, links);
|
|
if (tc.exec() == QDialog::Accepted) {
|
|
CentralWidget::instance()->setSourceInNewTab(tc.link());
|
|
}
|
|
} else {
|
|
CentralWidget::instance()->
|
|
setSourceInNewTab(links.constBegin().value());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#ifdef Q_OS_MAC
|
|
else if (obj == m_indexWidget && e->type() == QEvent::KeyPress) {
|
|
QKeyEvent *ke = static_cast<QKeyEvent*>(e);
|
|
if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter)
|
|
m_indexWidget->activateCurrentItem();
|
|
}
|
|
#endif
|
|
return QWidget::eventFilter(obj, e);
|
|
}
|
|
|
|
void IndexWindow::enableSearchLineEdit()
|
|
{
|
|
m_searchLineEdit->setDisabled(false);
|
|
filterIndices(m_searchLineEdit->text());
|
|
}
|
|
|
|
void IndexWindow::disableSearchLineEdit()
|
|
{
|
|
m_searchLineEdit->setDisabled(true);
|
|
}
|
|
|
|
void IndexWindow::setSearchLineEditText(const QString &text)
|
|
{
|
|
m_searchLineEdit->setText(text);
|
|
}
|
|
|
|
void IndexWindow::focusInEvent(QFocusEvent *e)
|
|
{
|
|
if (e->reason() != Qt::MouseFocusReason) {
|
|
m_searchLineEdit->selectAll();
|
|
m_searchLineEdit->setFocus();
|
|
}
|
|
}
|
|
|
|
QT_END_NAMESPACE
|