OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / help / searchwidget.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "searchwidget.h"
34 #include "localhelpmanager.h"
35 #include "openpagesmanager.h"
36
37 #include <coreplugin/icore.h>
38 #include <coreplugin/progressmanager/progressmanager.h>
39 #include <utils/styledbar.h>
40
41 #include <QtCore/QMap>
42 #include <QtCore/QString>
43 #include <QtCore/QStringList>
44
45 #include <QtGui/QMenu>
46 #include <QtGui/QLayout>
47 #include <QtGui/QKeyEvent>
48 #include <QtGui/QClipboard>
49 #include <QtGui/QApplication>
50 #include <QtGui/QTextBrowser>
51
52 #include <QtHelp/QHelpEngine>
53 #include <QtHelp/QHelpSearchEngine>
54 #include <QtHelp/QHelpSearchQueryWidget>
55 #include <QtHelp/QHelpSearchResultWidget>
56
57 using namespace Help::Internal;
58
59 SearchWidget::SearchWidget()
60     : zoomCount(0)
61     , m_progress(0)
62     , searchEngine(0)
63     , resultWidget(0)
64 {
65 }
66
67 SearchWidget::~SearchWidget()
68 {
69 }
70
71 void SearchWidget::zoomIn()
72 {
73     QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
74     if (browser && zoomCount != 10) {
75         zoomCount++;
76         browser->zoomIn();
77     }
78 }
79
80 void SearchWidget::zoomOut()
81 {
82     QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
83     if (browser && zoomCount != -5) {
84         zoomCount--;
85         browser->zoomOut();
86     }
87 }
88
89 void SearchWidget::resetZoom()
90 {
91     if (zoomCount == 0)
92         return;
93
94     QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
95     if (browser) {
96         browser->zoomOut(zoomCount);
97         zoomCount = 0;
98     }
99 }
100
101 void SearchWidget::showEvent(QShowEvent *event)
102 {
103     if (!event->spontaneous() && !searchEngine) {
104         QVBoxLayout *vLayout = new QVBoxLayout(this);
105         vLayout->setMargin(0);
106         vLayout->setSpacing(0);
107
108         searchEngine = (&LocalHelpManager::helpEngine())->searchEngine();
109
110         Utils::StyledBar *toolbar = new Utils::StyledBar(this);
111         toolbar->setSingleRow(false);
112         QHelpSearchQueryWidget *queryWidget = searchEngine->queryWidget();
113         QLayout *tbLayout = new QVBoxLayout();
114         tbLayout->setSpacing(6);
115         tbLayout->setMargin(4);
116         tbLayout->addWidget(queryWidget);
117         toolbar->setLayout(tbLayout);
118
119         Utils::StyledBar *toolbar2 = new Utils::StyledBar(this);
120         toolbar2->setSingleRow(false);
121         tbLayout = new QVBoxLayout();
122         tbLayout->setSpacing(0);
123         tbLayout->setMargin(0);
124         tbLayout->addWidget(resultWidget = searchEngine->resultWidget());
125         toolbar2->setLayout(tbLayout);
126
127         vLayout->addWidget(toolbar);
128         vLayout->addWidget(toolbar2);
129
130         setFocusProxy(queryWidget);
131
132         connect(queryWidget, SIGNAL(search()), this, SLOT(search()));
133         connect(resultWidget, SIGNAL(requestShowLink(QUrl)), this,
134             SIGNAL(linkActivated(QUrl)));
135
136         connect(searchEngine, SIGNAL(searchingStarted()), this,
137             SLOT(searchingStarted()));
138         connect(searchEngine, SIGNAL(searchingFinished(int)), this,
139             SLOT(searchingFinished(int)));
140
141         QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
142         browser->viewport()->installEventFilter(this);
143
144         connect(searchEngine, SIGNAL(indexingStarted()), this,
145             SLOT(indexingStarted()));
146         connect(searchEngine, SIGNAL(indexingFinished()), this,
147             SLOT(indexingFinished()));
148
149         QMetaObject::invokeMethod(&LocalHelpManager::helpEngine(), "setupFinished",
150             Qt::QueuedConnection);
151     }
152 }
153
154 void SearchWidget::search() const
155 {
156     QList<QHelpSearchQuery> query = searchEngine->queryWidget()->query();
157     searchEngine->search(query);
158 }
159
160 void SearchWidget::searchingStarted()
161 {
162     qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
163 }
164
165 void SearchWidget::searchingFinished(int hits)
166 {
167     Q_UNUSED(hits)
168     qApp->restoreOverrideCursor();
169 }
170
171 void SearchWidget::indexingStarted()
172 {
173     Q_ASSERT(!m_progress);
174     m_progress = new QFutureInterface<void>();
175     Core::ICore::instance()->progressManager() ->addTask(m_progress->future(),
176         tr("Indexing"), QLatin1String("Help.Indexer"));
177     m_progress->setProgressRange(0, 2);
178     m_progress->setProgressValueAndText(1, tr("Indexing Documentation..."));
179     m_progress->reportStarted();
180
181     m_watcher.setFuture(m_progress->future());
182     connect(&m_watcher, SIGNAL(canceled()), searchEngine, SLOT(cancelIndexing()));
183 }
184
185 void SearchWidget::indexingFinished()
186 {
187     m_progress->reportFinished();
188
189     delete m_progress;
190     m_progress = NULL;
191 }
192
193 bool SearchWidget::eventFilter(QObject* o, QEvent *e)
194 {
195     QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
196     if (browser && o == browser->viewport()
197         && e->type() == QEvent::MouseButtonRelease){
198         QMouseEvent *me = static_cast<QMouseEvent*>(e);
199         QUrl link = resultWidget->linkAt(me->pos());
200         if (!link.isEmpty() || link.isValid()) {
201             bool controlPressed = me->modifiers() & Qt::ControlModifier;
202             if((me->button() == Qt::LeftButton && controlPressed)
203                 || (me->button() == Qt::MidButton)) {
204                     OpenPagesManager::instance().createPageFromSearch(link);
205             }
206         }
207     }
208     return QWidget::eventFilter(o,e);
209 }
210
211 void SearchWidget::contextMenuEvent(QContextMenuEvent *contextMenuEvent)
212 {
213     QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
214     if (!browser)
215         return;
216
217     QPoint point = browser->mapFromGlobal(contextMenuEvent->globalPos());
218     if (!browser->rect().contains(point, true))
219         return;
220
221     QAction *openLink = 0;
222     QAction *openLinkInNewTab = 0;
223     QAction *copyAnchorAction = 0;
224
225     QMenu menu;
226     QUrl link = browser->anchorAt(point);
227     if (!link.isEmpty() && link.isValid()) {
228         if (link.isRelative())
229             link = browser->source().resolved(link);
230         openLink = menu.addAction(tr("Open Link"));
231         openLinkInNewTab = menu.addAction(tr("Open Link as New Page"));
232         copyAnchorAction = menu.addAction(tr("Copy Link"));
233     } else if (browser->textCursor().hasSelection()) {
234         menu.addAction(tr("Copy"), browser, SLOT(copy()));
235     } else {
236         menu.addAction(tr("Reload"), browser, SLOT(reload()));
237     }
238
239     QAction *usedAction = menu.exec(mapToGlobal(contextMenuEvent->pos()));
240     if (usedAction == openLink) {
241         browser->selectAll();
242     } else if (usedAction == openLinkInNewTab) {
243         OpenPagesManager::instance().createPageFromSearch(link);
244     } else if (usedAction == copyAnchorAction) {
245         QApplication::clipboard()->setText(link.toString());
246     }
247 }