Fix regression introduced in 98a05681851db9d88b1364af52be543715fbe306
[qt-netbsd.git] / demos / browser / toolbarsearch.cpp
blobd346418181bd91235ee98927abcca705404dcb48
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.
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.
38 ** $QT_END_LICENSE$
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))
64 QMenu *m = menu();
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"));
74 load();
77 ToolbarSearch::~ToolbarSearch()
79 m_autosaver->saveIfNeccessary();
82 void ToolbarSearch::save()
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();
91 void ToolbarSearch::load()
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();
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"));
122 emit search(url);
125 void ToolbarSearch::aboutToShowMenu()
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;
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);
142 m->addSeparator();
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);
152 searchNow();
156 void ToolbarSearch::clear()
158 m_stringListModel->setStringList(QStringList());
159 m_autosaver->changeOccurred();;