|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "toolbarsearch.h" |
|
43 #include "autosaver.h" |
|
44 |
|
45 #include <QtCore/QSettings> |
|
46 #include <QtCore/QUrl> |
|
47 |
|
48 #include <QtGui/QCompleter> |
|
49 #include <QtGui/QMenu> |
|
50 #include <QtGui/QStringListModel> |
|
51 |
|
52 #include <QtWebKit/QWebSettings> |
|
53 |
|
54 /* |
|
55 ToolbarSearch is a very basic search widget that also contains a small history. |
|
56 Searches are turned into urls that use Google to perform search |
|
57 */ |
|
58 ToolbarSearch::ToolbarSearch(QWidget *parent) |
|
59 : SearchLineEdit(parent) |
|
60 , m_autosaver(new AutoSaver(this)) |
|
61 , m_maxSavedSearches(10) |
|
62 , m_stringListModel(new QStringListModel(this)) |
|
63 { |
|
64 QMenu *m = menu(); |
|
65 connect(m, SIGNAL(aboutToShow()), this, SLOT(aboutToShowMenu())); |
|
66 connect(m, SIGNAL(triggered(QAction*)), this, SLOT(triggeredMenuAction(QAction*))); |
|
67 |
|
68 QCompleter *completer = new QCompleter(m_stringListModel, this); |
|
69 completer->setCompletionMode(QCompleter::InlineCompletion); |
|
70 lineEdit()->setCompleter(completer); |
|
71 |
|
72 connect(lineEdit(), SIGNAL(returnPressed()), SLOT(searchNow())); |
|
73 setInactiveText(tr("Google")); |
|
74 load(); |
|
75 } |
|
76 |
|
77 ToolbarSearch::~ToolbarSearch() |
|
78 { |
|
79 m_autosaver->saveIfNeccessary(); |
|
80 } |
|
81 |
|
82 void ToolbarSearch::save() |
|
83 { |
|
84 QSettings settings; |
|
85 settings.beginGroup(QLatin1String("toolbarsearch")); |
|
86 settings.setValue(QLatin1String("recentSearches"), m_stringListModel->stringList()); |
|
87 settings.setValue(QLatin1String("maximumSaved"), m_maxSavedSearches); |
|
88 settings.endGroup(); |
|
89 } |
|
90 |
|
91 void ToolbarSearch::load() |
|
92 { |
|
93 QSettings settings; |
|
94 settings.beginGroup(QLatin1String("toolbarsearch")); |
|
95 QStringList list = settings.value(QLatin1String("recentSearches")).toStringList(); |
|
96 m_maxSavedSearches = settings.value(QLatin1String("maximumSaved"), m_maxSavedSearches).toInt(); |
|
97 m_stringListModel->setStringList(list); |
|
98 settings.endGroup(); |
|
99 } |
|
100 |
|
101 void ToolbarSearch::searchNow() |
|
102 { |
|
103 QString searchText = lineEdit()->text(); |
|
104 QStringList newList = m_stringListModel->stringList(); |
|
105 if (newList.contains(searchText)) |
|
106 newList.removeAt(newList.indexOf(searchText)); |
|
107 newList.prepend(searchText); |
|
108 if (newList.size() >= m_maxSavedSearches) |
|
109 newList.removeLast(); |
|
110 |
|
111 QWebSettings *globalSettings = QWebSettings::globalSettings(); |
|
112 if (!globalSettings->testAttribute(QWebSettings::PrivateBrowsingEnabled)) { |
|
113 m_stringListModel->setStringList(newList); |
|
114 m_autosaver->changeOccurred(); |
|
115 } |
|
116 |
|
117 QUrl url(QLatin1String("http://www.google.com/search")); |
|
118 url.addQueryItem(QLatin1String("q"), searchText); |
|
119 url.addQueryItem(QLatin1String("ie"), QLatin1String("UTF-8")); |
|
120 url.addQueryItem(QLatin1String("oe"), QLatin1String("UTF-8")); |
|
121 url.addQueryItem(QLatin1String("client"), QLatin1String("qtdemobrowser")); |
|
122 emit search(url); |
|
123 } |
|
124 |
|
125 void ToolbarSearch::aboutToShowMenu() |
|
126 { |
|
127 lineEdit()->selectAll(); |
|
128 QMenu *m = menu(); |
|
129 m->clear(); |
|
130 QStringList list = m_stringListModel->stringList(); |
|
131 if (list.isEmpty()) { |
|
132 m->addAction(tr("No Recent Searches")); |
|
133 return; |
|
134 } |
|
135 |
|
136 QAction *recent = m->addAction(tr("Recent Searches")); |
|
137 recent->setEnabled(false); |
|
138 for (int i = 0; i < list.count(); ++i) { |
|
139 QString text = list.at(i); |
|
140 m->addAction(text)->setData(text); |
|
141 } |
|
142 m->addSeparator(); |
|
143 m->addAction(tr("Clear Recent Searches"), this, SLOT(clear())); |
|
144 } |
|
145 |
|
146 void ToolbarSearch::triggeredMenuAction(QAction *action) |
|
147 { |
|
148 QVariant v = action->data(); |
|
149 if (v.canConvert<QString>()) { |
|
150 QString text = v.toString(); |
|
151 lineEdit()->setText(text); |
|
152 searchNow(); |
|
153 } |
|
154 } |
|
155 |
|
156 void ToolbarSearch::clear() |
|
157 { |
|
158 m_stringListModel->setStringList(QStringList()); |
|
159 m_autosaver->changeOccurred();; |
|
160 } |
|
161 |