source: branches/vendor/trolltech/qt/current/tools/assistant/tools/assistant/searchwidget.cpp @ 2

Last change on this file since 2 was 2, checked in by Dmitry A. Kuminov, 13 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the Qt Assistant of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file.  Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file.  Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "mainwindow.h"
43#include "searchwidget.h"
44
45#include <QtCore/QMap>
46#include <QtCore/QString>
47#include <QtCore/QStringList>
48
49#include <QtGui/QMenu>
50#include <QtGui/QLayout>
51#include <QtGui/QKeyEvent>
52#include <QtGui/QClipboard>
53#include <QtGui/QApplication>
54#include <QtGui/QTextBrowser>
55
56#include <QtHelp/QHelpSearchEngine>
57#include <QtHelp/QHelpSearchQueryWidget>
58#include <QtHelp/QHelpSearchResultWidget>
59
60QT_BEGIN_NAMESPACE
61
62SearchWidget::SearchWidget(QHelpSearchEngine *engine, QWidget *parent)
63    : QWidget(parent)
64    , zoomCount(0)
65    , searchEngine(engine)
66{
67    QVBoxLayout *vLayout = new QVBoxLayout(this);
68
69    resultWidget = searchEngine->resultWidget();
70    QHelpSearchQueryWidget *queryWidget = searchEngine->queryWidget();
71
72    vLayout->addWidget(queryWidget);
73    vLayout->addWidget(resultWidget);       
74   
75    setFocusProxy(queryWidget);
76
77    connect(queryWidget, SIGNAL(search()), this, SLOT(search()));
78    connect(resultWidget, SIGNAL(requestShowLink(const QUrl&)), 
79        this, SIGNAL(requestShowLink(const QUrl&)));
80
81    connect(searchEngine, SIGNAL(searchingStarted()), this, SLOT(searchingStarted()));
82    connect(searchEngine, SIGNAL(searchingFinished(int)), this, SLOT(searchingFinished(int)));
83
84    QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
85    browser->viewport()->installEventFilter(this);
86}
87
88SearchWidget::~SearchWidget()
89{
90    // nothing todo
91}
92
93void SearchWidget::zoomIn()
94{
95#ifndef QT_CLUCENE_SUPPORT
96    return;
97#endif
98
99    QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
100    if (browser && zoomCount != 10) {
101        zoomCount++;
102        browser->zoomIn();
103    }
104}
105
106void SearchWidget::zoomOut()
107{
108#ifndef QT_CLUCENE_SUPPORT
109        return;
110#endif
111
112    QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
113    if (browser && zoomCount != -5) {
114        zoomCount--;
115        browser->zoomOut();
116    }
117}
118
119void SearchWidget::resetZoom()
120{
121#ifndef QT_CLUCENE_SUPPORT
122    return;
123#endif
124
125    if (zoomCount == 0)
126        return;
127
128    QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
129    if (browser) {
130        browser->zoomOut(zoomCount);
131        zoomCount = 0;
132    }
133}
134
135void SearchWidget::search() const
136{
137    QList<QHelpSearchQuery> query = searchEngine->queryWidget()->query();
138    searchEngine->search(query);
139}
140
141void SearchWidget::searchingStarted()
142{
143    qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
144}
145
146void SearchWidget::searchingFinished(int hits)
147{
148    Q_UNUSED(hits)
149    qApp->restoreOverrideCursor();
150}
151
152bool SearchWidget::eventFilter(QObject* o, QEvent *e)
153{
154    QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
155    if (browser && o == browser->viewport() && e->type() == QEvent::MouseButtonRelease){
156        QMouseEvent *me = static_cast<QMouseEvent*>(e);
157        QUrl link = resultWidget->linkAt(me->pos());
158        if (!link.isEmpty() || link.isValid()) {
159            bool controlPressed = me->modifiers() & Qt::ControlModifier;
160            if((me->button() == Qt::LeftButton && controlPressed)
161                || (me->button() == Qt::MidButton)) {
162                    emit requestShowLinkInNewTab(link);
163            }
164        }
165    }
166    return QWidget::eventFilter(o,e);
167}
168
169void SearchWidget::keyPressEvent(QKeyEvent *keyEvent)
170{
171    if (keyEvent->key() == Qt::Key_Escape)
172        MainWindow::activateCurrentBrowser();
173    else
174        keyEvent->ignore();
175}
176
177void SearchWidget::contextMenuEvent(QContextMenuEvent *contextMenuEvent)
178{
179    QMenu menu;
180    QPoint point = contextMenuEvent->globalPos();
181
182#ifdef QT_CLUCENE_SUPPORT
183    QTextBrowser* browser = qFindChild<QTextBrowser*>(resultWidget);
184    if (!browser)
185        return;
186
187    point = browser->mapFromGlobal(point);
188    if (!browser->rect().contains(point, true))
189        return;
190
191    QUrl link = browser->anchorAt(point);
192
193    QAction *copyAction = menu.addAction(tr("&Copy") + 
194        QString(QLatin1String("\t") + QString(QKeySequence(Qt::CTRL | Qt::Key_C))));
195    copyAction->setEnabled(QTextCursor(browser->textCursor()).hasSelection());
196
197    QAction *copyAnchorAction = menu.addAction(tr("Copy &Link Location"));
198    copyAnchorAction->setEnabled(!link.isEmpty() && link.isValid());
199
200    QAction *newTabAction = menu.addAction(tr("Open Link in New Tab") + 
201        QString(QLatin1String("\t") + QString(QKeySequence(Qt::CTRL))) + 
202        QLatin1String("LMB"));
203    newTabAction->setEnabled(!link.isEmpty() && link.isValid());
204
205    menu.addSeparator();
206
207    QAction *selectAllAction = menu.addAction(tr("Select All") + 
208        QString(QLatin1String("\t") + QString(QKeySequence(Qt::CTRL | Qt::Key_A))));
209
210    QAction *usedAction = menu.exec(mapToGlobal(contextMenuEvent->pos()));
211    if (usedAction == copyAction) {
212        QTextCursor cursor = browser->textCursor();
213        if (!cursor.isNull() && cursor.hasSelection()) {
214            QString selectedText = cursor.selectedText();
215            QMimeData *data = new QMimeData();
216            data->setText(selectedText);
217            QApplication::clipboard()->setMimeData(data);
218        }
219    }
220    else if (usedAction == copyAnchorAction) {
221        QApplication::clipboard()->setText(link.toString());
222    }
223    else if (usedAction == newTabAction) {
224        emit requestShowLinkInNewTab(link);
225    } 
226    else if (usedAction == selectAllAction) {
227        browser->selectAll();
228    }
229#else
230    point = resultWidget->mapFromGlobal(point);
231    QUrl link = resultWidget->linkAt(point);
232    if (link.isEmpty() || !link.isValid())
233        return;
234
235    QAction *curTab = menu.addAction(tr("Open Link"));
236    QAction *newTab = menu.addAction(tr("Open Link in New Tab"));
237
238    QAction *action = menu.exec(mapToGlobal(contextMenuEvent->pos()));
239    if (curTab == action)
240        emit requestShowLink(link);
241    else if (newTab == action)
242        emit requestShowLinkInNewTab(link);
243#endif
244}
245
246QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.