source: trunk/examples/network/googlesuggest/googlesuggest.cpp@ 689

Last change on this file since 689 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42
43//! [1]
44#include "googlesuggest.h"
45
46#define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1"
47//! [1]
48
49//! [2]
50GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent)
51{
52 popup = new QTreeWidget;
53 popup->setWindowFlags(Qt::Popup);
54 popup->setFocusPolicy(Qt::NoFocus);
55 popup->setFocusProxy(parent);
56 popup->setMouseTracking(true);
57
58 popup->setColumnCount(2);
59 popup->setUniformRowHeights(true);
60 popup->setRootIsDecorated(false);
61 popup->setEditTriggers(QTreeWidget::NoEditTriggers);
62 popup->setSelectionBehavior(QTreeWidget::SelectRows);
63 popup->setFrameStyle(QFrame::Box | QFrame::Plain);
64 popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65 popup->header()->hide();
66
67 popup->installEventFilter(this);
68
69 connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
70 SLOT(doneCompletion()));
71
72 timer = new QTimer(this);
73 timer->setSingleShot(true);
74 timer->setInterval(500);
75 connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
76 connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
77
78 connect(&networkManager, SIGNAL(finished(QNetworkReply*)),
79 this, SLOT(handleNetworkData(QNetworkReply*)));
80
81}
82//! [2]
83
84//! [3]
85GSuggestCompletion::~GSuggestCompletion()
86{
87 delete popup;
88}
89//! [3]
90
91//! [4]
92bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
93{
94 if (obj != popup)
95 return false;
96
97 if (ev->type() == QEvent::MouseButtonPress) {
98 popup->hide();
99 editor->setFocus();
100 return true;
101 }
102
103 if (ev->type() == QEvent::KeyPress) {
104
105 bool consumed = false;
106 int key = static_cast<QKeyEvent*>(ev)->key();
107 switch (key) {
108 case Qt::Key_Enter:
109 case Qt::Key_Return:
110 doneCompletion();
111 consumed = true;
112
113 case Qt::Key_Escape:
114 editor->setFocus();
115 popup->hide();
116 consumed = true;
117
118 case Qt::Key_Up:
119 case Qt::Key_Down:
120 case Qt::Key_Home:
121 case Qt::Key_End:
122 case Qt::Key_PageUp:
123 case Qt::Key_PageDown:
124 break;
125
126 default:
127 editor->setFocus();
128 editor->event(ev);
129 popup->hide();
130 break;
131 }
132
133 return consumed;
134 }
135
136 return false;
137}
138//! [4]
139
140//! [5]