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