source: trunk/demos/browser/toolbarsearch.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
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 */
58ToolbarSearch::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
77ToolbarSearch::~ToolbarSearch()
78{
79 m_autosaver->saveIfNeccessary();
80}
81
82void 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
91void 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
101void 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
125void 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
146void 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
156void ToolbarSearch::clear()
157{
158 m_stringListModel->setStringList(QStringList());
159 m_autosaver->changeOccurred();;
160}
161
Note: See TracBrowser for help on using the repository browser.