1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the demonstration applications of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "toolbarsearch.h"
43 #include "autosaver.h"
45 #include <QtCore/QSettings>
46 #include <QtCore/QUrl>
48 #include <QtGui/QCompleter>
49 #include <QtGui/QMenu>
50 #include <QtGui/QStringListModel>
52 #include <QtWebKit/QWebSettings>
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
58 ToolbarSearch::ToolbarSearch(QWidget
*parent
)
59 : SearchLineEdit(parent
)
60 , m_autosaver(new AutoSaver(this))
61 , m_maxSavedSearches(10)
62 , m_stringListModel(new QStringListModel(this))
65 connect(m
, SIGNAL(aboutToShow()), this, SLOT(aboutToShowMenu()));
66 connect(m
, SIGNAL(triggered(QAction
*)), this, SLOT(triggeredMenuAction(QAction
*)));
68 QCompleter
*completer
= new QCompleter(m_stringListModel
, this);
69 completer
->setCompletionMode(QCompleter::InlineCompletion
);
70 lineEdit()->setCompleter(completer
);
72 connect(lineEdit(), SIGNAL(returnPressed()), SLOT(searchNow()));
73 setInactiveText(tr("Google"));
77 ToolbarSearch::~ToolbarSearch()
79 m_autosaver
->saveIfNeccessary();
82 void ToolbarSearch::save()
85 settings
.beginGroup(QLatin1String("toolbarsearch"));
86 settings
.setValue(QLatin1String("recentSearches"), m_stringListModel
->stringList());
87 settings
.setValue(QLatin1String("maximumSaved"), m_maxSavedSearches
);
91 void ToolbarSearch::load()
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
);
101 void ToolbarSearch::searchNow()
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();
111 QWebSettings
*globalSettings
= QWebSettings::globalSettings();
112 if (!globalSettings
->testAttribute(QWebSettings::PrivateBrowsingEnabled
)) {
113 m_stringListModel
->setStringList(newList
);
114 m_autosaver
->changeOccurred();
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"));
125 void ToolbarSearch::aboutToShowMenu()
127 lineEdit()->selectAll();
130 QStringList list
= m_stringListModel
->stringList();
131 if (list
.isEmpty()) {
132 m
->addAction(tr("No Recent Searches"));
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
);
143 m
->addAction(tr("Clear Recent Searches"), this, SLOT(clear()));
146 void ToolbarSearch::triggeredMenuAction(QAction
*action
)
148 QVariant v
= action
->data();
149 if (v
.canConvert
<QString
>()) {
150 QString text
= v
.toString();
151 lineEdit()->setText(text
);
156 void ToolbarSearch::clear()
158 m_stringListModel
->setStringList(QStringList());
159 m_autosaver
->changeOccurred();;