1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "searchwidget.h"
34 #include "localhelpmanager.h"
35 #include "openpagesmanager.h"
37 #include <coreplugin/icore.h>
38 #include <coreplugin/progressmanager/progressmanager.h>
39 #include <utils/styledbar.h>
41 #include <QtCore/QMap>
42 #include <QtCore/QString>
43 #include <QtCore/QStringList>
45 #include <QtGui/QMenu>
46 #include <QtGui/QLayout>
47 #include <QtGui/QKeyEvent>
48 #include <QtGui/QClipboard>
49 #include <QtGui/QApplication>
50 #include <QtGui/QTextBrowser>
52 #include <QtHelp/QHelpEngine>
53 #include <QtHelp/QHelpSearchEngine>
54 #include <QtHelp/QHelpSearchQueryWidget>
55 #include <QtHelp/QHelpSearchResultWidget>
57 using namespace Help::Internal;
59 SearchWidget::SearchWidget()
67 SearchWidget::~SearchWidget()
71 void SearchWidget::zoomIn()
73 QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
74 if (browser && zoomCount != 10) {
80 void SearchWidget::zoomOut()
82 QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
83 if (browser && zoomCount != -5) {
89 void SearchWidget::resetZoom()
94 QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
96 browser->zoomOut(zoomCount);
101 void SearchWidget::showEvent(QShowEvent *event)
103 if (!event->spontaneous() && !searchEngine) {
104 QVBoxLayout *vLayout = new QVBoxLayout(this);
105 vLayout->setMargin(0);
106 vLayout->setSpacing(0);
108 searchEngine = (&LocalHelpManager::helpEngine())->searchEngine();
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);
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);
127 vLayout->addWidget(toolbar);
128 vLayout->addWidget(toolbar2);
130 setFocusProxy(queryWidget);
132 connect(queryWidget, SIGNAL(search()), this, SLOT(search()));
133 connect(resultWidget, SIGNAL(requestShowLink(QUrl)), this,
134 SIGNAL(linkActivated(QUrl)));
136 connect(searchEngine, SIGNAL(searchingStarted()), this,
137 SLOT(searchingStarted()));
138 connect(searchEngine, SIGNAL(searchingFinished(int)), this,
139 SLOT(searchingFinished(int)));
141 QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
142 browser->viewport()->installEventFilter(this);
144 connect(searchEngine, SIGNAL(indexingStarted()), this,
145 SLOT(indexingStarted()));
146 connect(searchEngine, SIGNAL(indexingFinished()), this,
147 SLOT(indexingFinished()));
149 QMetaObject::invokeMethod(&LocalHelpManager::helpEngine(), "setupFinished",
150 Qt::QueuedConnection);
154 void SearchWidget::search() const
156 QList<QHelpSearchQuery> query = searchEngine->queryWidget()->query();
157 searchEngine->search(query);
160 void SearchWidget::searchingStarted()
162 qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
165 void SearchWidget::searchingFinished(int hits)
168 qApp->restoreOverrideCursor();
171 void SearchWidget::indexingStarted()
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();
181 m_watcher.setFuture(m_progress->future());
182 connect(&m_watcher, SIGNAL(canceled()), searchEngine, SLOT(cancelIndexing()));
185 void SearchWidget::indexingFinished()
187 m_progress->reportFinished();
193 bool SearchWidget::eventFilter(QObject* o, QEvent *e)
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);
208 return QWidget::eventFilter(o,e);
211 void SearchWidget::contextMenuEvent(QContextMenuEvent *contextMenuEvent)
213 QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
217 QPoint point = browser->mapFromGlobal(contextMenuEvent->globalPos());
218 if (!browser->rect().contains(point, true))
221 QAction *openLink = 0;
222 QAction *openLinkInNewTab = 0;
223 QAction *copyAnchorAction = 0;
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()));
236 menu.addAction(tr("Reload"), browser, SLOT(reload()));
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());