1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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();;