OSDN Git Service

8d752751981abac1f04a77d9416484db0241d656
[qt-creator-jp/qt-creator-jp.git] / src / shared / help / indexwindow.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "centralwidget.h"
35
36 #include "helpviewer.h"
37 #include "indexwindow.h"
38 #include "localhelpmanager.h"
39 #include "openpagesmanager.h"
40 #include "topicchooser.h"
41
42 #include <utils/filterlineedit.h>
43 #include <utils/styledbar.h>
44
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>
52
53 #include <QtHelp/QHelpEngine>
54 #include <QtHelp/QHelpIndexWidget>
55
56 using namespace Help::Internal;
57
58 IndexWindow::IndexWindow()
59     : m_searchLineEdit(0)
60     , m_indexWidget(0)
61 {
62     QVBoxLayout *layout = new QVBoxLayout(this);
63
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);
70
71     QLabel *l = new QLabel(tr("&Look for:"));
72     l->setBuddy(m_searchLineEdit);
73     layout->addWidget(l);
74     layout->setMargin(0);
75     layout->setSpacing(0);
76
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);
86
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);
102
103     m_indexWidget->viewport()->installEventFilter(this);
104 }
105
106 IndexWindow::~IndexWindow()
107 {
108 }
109
110 void IndexWindow::filterIndices(const QString &filter)
111 {
112     if (filter.contains(QLatin1Char('*')))
113         m_indexWidget->filterIndices(filter, filter);
114     else
115         m_indexWidget->filterIndices(filter, QString());
116 }
117
118 bool IndexWindow::eventFilter(QObject *obj, QEvent *e)
119 {
120     if (obj == m_searchLineEdit && e->type() == QEvent::KeyPress) {
121         QKeyEvent *ke = static_cast<QKeyEvent*>(e);
122         QModelIndex idx = m_indexWidget->currentIndex();
123         switch (ke->key()) {
124         case Qt::Key_Up:
125             idx = m_indexWidget->model()->index(idx.row()-1,
126                 idx.column(), idx.parent());
127             if (idx.isValid())
128                 m_indexWidget->setCurrentIndex(idx);
129             break;
130         case Qt::Key_Down:
131             idx = m_indexWidget->model()->index(idx.row()+1,
132                 idx.column(), idx.parent());
133             if (idx.isValid())
134                 m_indexWidget->setCurrentIndex(idx);
135             break;
136         default: ; // stop complaining
137         }
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());
146         if (idx.isValid()) {
147             QMenu menu;
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()));
151
152             QAction *action = menu.exec();
153             if (curTab == action)
154                 m_indexWidget->activateCurrentItem();
155             else if (newTab == action) {
156                 open(m_indexWidget, idx);
157             }
158         }
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());
163         if (idx.isValid()) {
164             Qt::MouseButtons button = mouseEvent->button();
165             if (((button == Qt::LeftButton) && (mouseEvent->modifiers() & Qt::ControlModifier))
166                 || (button == Qt::MidButton)) {
167                 open(m_indexWidget, idx);
168             }
169         }
170     }
171 #ifdef Q_WS_MAC
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();
176     }
177 #endif
178
179     return QWidget::eventFilter(obj, e);
180 }
181
182 void IndexWindow::enableSearchLineEdit()
183 {
184     m_searchLineEdit->setDisabled(false);
185     filterIndices(m_searchLineEdit->text());
186 }
187
188 void IndexWindow::disableSearchLineEdit()
189 {
190     m_searchLineEdit->setDisabled(true);
191 }
192
193 void IndexWindow::setSearchLineEditText(const QString &text)
194 {
195     m_searchLineEdit->setText(text);
196 }
197
198 void IndexWindow::open(QHelpIndexWidget* indexWidget, const QModelIndex &index)
199 {
200     QHelpIndexModel *model = qobject_cast<QHelpIndexModel*>(indexWidget->model());
201     if (model) {
202         QString keyword = model->data(index, Qt::DisplayRole).toString();
203         QMap<QString, QUrl> links = model->linksForKeyword(keyword);
204
205         QUrl url;
206         if (links.count() > 1) {
207             TopicChooser tc(this, keyword, links);
208             if (tc.exec() == QDialog::Accepted)
209                 url = tc.link();
210         } else if (links.count() == 1) {
211             url = links.constBegin().value();
212         } else {
213             return;
214         }
215
216         if (!HelpViewer::canOpenPage(url.path()))
217             CentralWidget::instance()->setSource(url);
218         else
219             OpenPagesManager::instance().createPage(url);
220     }
221 }