45
|
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 demonstration applications 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 module toolbarsearch;
|
|
42
|
|
43 import searchlineedit;
|
|
44
|
|
45 import autosaver;
|
|
46
|
|
47 import QtCore.QSettings;
|
|
48 import QtCore.QUrl;
|
|
49
|
|
50 import QtGui.QCompleter;
|
|
51 import QtGui.QMenu;
|
|
52 import QtGui.QStringListModel;
|
|
53
|
|
54 import QtWebKit/QWebSettings;
|
|
55
|
|
56 /*
|
|
57 QT_BEGIN_NAMESPACE
|
|
58 class QUrl;
|
|
59 class QAction;
|
|
60 class QStringListModel;
|
|
61 QT_END_NAMESPACE
|
|
62
|
|
63 class AutoSaver;
|
|
64 */
|
|
65
|
|
66 class ToolbarSearch : public SearchLineEdit
|
|
67 {
|
|
68 Q_OBJECT
|
|
69
|
|
70 signals:
|
|
71 void search(const QUrl &url);
|
|
72
|
|
73 public:
|
|
74
|
|
75 /*
|
|
76 ToolbarSearch is a very basic search widget that also contains a small history.
|
|
77 Searches are turned into urls that use Google to perform search
|
|
78 */
|
|
79 this(QWidget *parent = null)
|
|
80
|
|
81 {
|
|
82 super(parent)
|
|
83 m_autosaver = new AutoSaver(this);
|
|
84 m_maxSavedSearches = 10;
|
|
85 m_stringListModel = new QStringListModel(this);
|
|
86
|
|
87 QMenu *m = menu();
|
|
88 connect(m, SIGNAL(aboutToShow()), this, SLOT(aboutToShowMenu()));
|
|
89 connect(m, SIGNAL(triggered(QAction*)), this, SLOT(triggeredMenuAction(QAction*)));
|
|
90
|
|
91 QCompleter *completer = new QCompleter(m_stringListModel, this);
|
|
92 completer.setCompletionMode(QCompleter::InlineCompletion);
|
|
93 lineEdit().setCompleter(completer);
|
|
94
|
|
95 connect(lineEdit(), SIGNAL(returnPressed()), SLOT(searchNow()));
|
|
96 setInactiveText(tr("Google"));
|
|
97 load();
|
|
98 }
|
|
99 ~this()
|
|
100 {
|
|
101 m_autosaver.saveIfNeccessary();
|
|
102 }
|
|
103
|
|
104 public slots:
|
|
105 void clear()
|
|
106 {
|
|
107 m_stringListModel.setStringList(QStringList());
|
|
108 m_autosaver.changeOccurred();;
|
|
109 }
|
|
110
|
|
111 void searchNow()
|
|
112 {
|
|
113 QString searchText = lineEdit().text();
|
|
114 QStringList newList = m_stringListModel.stringList();
|
|
115 if (newList.contains(searchText))
|
|
116 newList.removeAt(newList.indexOf(searchText));
|
|
117 newList.prepend(searchText);
|
|
118 if (newList.size() >= m_maxSavedSearches)
|
|
119 newList.removeLast();
|
|
120
|
|
121 QWebSettings *globalSettings = QWebSettings::globalSettings();
|
|
122 if (!globalSettings.testAttribute(QWebSettings::PrivateBrowsingEnabled)) {
|
|
123 m_stringListModel.setStringList(newList);
|
|
124 m_autosaver.changeOccurred();
|
|
125 }
|
|
126
|
|
127 QUrl url(QLatin1String("http://www.google.com/search"));
|
|
128 url.addQueryItem(QLatin1String("q"), searchText);
|
|
129 url.addQueryItem(QLatin1String("ie"), QLatin1String("UTF-8"));
|
|
130 url.addQueryItem(QLatin1String("oe"), QLatin1String("UTF-8"));
|
|
131 url.addQueryItem(QLatin1String("client"), QLatin1String("qtdemobrowser"));
|
|
132 emit search(url);
|
|
133 }
|
|
134
|
|
135 private slots:
|
|
136 void save()
|
|
137 {
|
|
138 QSettings settings;
|
|
139 settings.beginGroup(QLatin1String("toolbarsearch"));
|
|
140 settings.setValue(QLatin1String("recentSearches"), m_stringListModel.stringList());
|
|
141 settings.setValue(QLatin1String("maximumSaved"), m_maxSavedSearches);
|
|
142 settings.endGroup();
|
|
143 }
|
|
144
|
|
145
|
|
146 void aboutToShowMenu()
|
|
147 {
|
|
148 lineEdit().selectAll();
|
|
149 QMenu *m = menu();
|
|
150 m.clear();
|
|
151 QStringList list = m_stringListModel.stringList();
|
|
152 if (list.isEmpty()) {
|
|
153 m.addAction(tr("No Recent Searches"));
|
|
154 return;
|
|
155 }
|
|
156
|
|
157 QAction *recent = m.addAction(tr("Recent Searches"));
|
|
158 recent.setEnabled(false);
|
|
159 for (int i = 0; i < list.count(); ++i) {
|
|
160 QString text = list.at(i);
|
|
161 m.addAction(text).setData(text);
|
|
162 }
|
|
163 m.addSeparator();
|
|
164 m.addAction(tr("Clear Recent Searches"), this, SLOT(clear()));
|
|
165 }
|
|
166
|
|
167 void triggeredMenuAction(QAction *action)
|
|
168 {
|
|
169 QVariant v = action.data();
|
|
170 if (v.canConvert<QString>()) {
|
|
171 QString text = v.toString();
|
|
172 lineEdit().setText(text);
|
|
173 searchNow();
|
|
174 }
|
|
175 }
|
|
176
|
|
177 private:
|
|
178 void load()
|
|
179 {
|
|
180 QSettings settings;
|
|
181 settings.beginGroup(QLatin1String("toolbarsearch"));
|
|
182 QStringList list = settings.value(QLatin1String("recentSearches")).toStringList();
|
|
183 m_maxSavedSearches = settings.value(QLatin1String("maximumSaved"), m_maxSavedSearches).toInt();
|
|
184 m_stringListModel.setStringList(list);
|
|
185 settings.endGroup();
|
|
186 }
|
|
187
|
|
188 AutoSaver *m_autosaver;
|
|
189 int m_maxSavedSearches;
|
|
190 QStringListModel *m_stringListModel;
|
|
191 }
|