45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** 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 are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41 module toolbarsearch;
|
|
42
|
73
|
43
|
|
44 import qt.core.QSettings;
|
|
45 import qt.core.QUrl;
|
|
46
|
|
47 import qt.gui.QCompleter;
|
|
48 import qt.gui.QMenu;
|
|
49 import qt.gui.QStringListModel;
|
|
50
|
94
|
51 import qt.webkit.QWebSettings;
|
73
|
52
|
45
|
53 import searchlineedit;
|
|
54 import autosaver;
|
|
55
|
|
56
|
|
57 class ToolbarSearch : public SearchLineEdit
|
|
58 {
|
77
|
59 mixin Signal!("search", QUrl /*url*/);
|
73
|
60
|
74
|
61 public:
|
73
|
62
|
|
63 /*
|
|
64 ToolbarSearch is a very basic search widget that also contains a small history.
|
|
65 Searches are turned into urls that use Google to perform search
|
|
66 */
|
|
67 this(QWidget parent = null)
|
|
68 {
|
77
|
69 super(parent);
|
73
|
70 m_autosaver = new AutoSaver(this);
|
|
71 m_maxSavedSearches = 10;
|
|
72 m_stringListModel = new QStringListModel(this);
|
45
|
73
|
73
|
74 QMenu m = menu();
|
|
75 m.aboutToShow().connect(&this.aboutToShowMenu);
|
|
76 m.triggered.connect(&this.triggeredMenuAction);
|
|
77
|
|
78 QCompleter completer = new QCompleter(m_stringListModel, this);
|
77
|
79 completer.setCompletionMode(QCompleter.InlineCompletion);
|
73
|
80 lineEdit().setCompleter(completer);
|
|
81
|
|
82 lineEdit().returnPressed.connect(&searchNow);
|
|
83 setInactiveText(tr("Google"));
|
|
84 load();
|
|
85 }
|
|
86
|
|
87 ~this()
|
|
88 {
|
|
89 m_autosaver.saveIfNeccessary();
|
|
90 }
|
45
|
91
|
|
92 public:
|
73
|
93
|
|
94 void clear()
|
|
95 {
|
77
|
96 m_stringListModel.setStringList(string[]());
|
73
|
97 m_autosaver.changeOccurred();
|
|
98 }
|
45
|
99
|
73
|
100 void searchNow()
|
|
101 {
|
77
|
102 string searchText = lineEdit().text();
|
|
103 string[] newList = m_stringListModel.stringList();
|
73
|
104 if (newList.contains(searchText))
|
|
105 newList.removeAt(newList.indexOf(searchText));
|
|
106 newList.prepend(searchText);
|
|
107 if (newList.size() >= m_maxSavedSearches)
|
|
108 newList.removeLast();
|
45
|
109
|
77
|
110 QWebSettings globalSettings = QWebSettings.globalSettings();
|
|
111 if (!globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled)) {
|
73
|
112 m_stringListModel.setStringList(newList);
|
|
113 m_autosaver.changeOccurred();
|
|
114 }
|
45
|
115
|
80
|
116 auto url = new QUrl("http://www.google.com/search");
|
|
117 url.addQueryItem("q", searchText);
|
|
118 url.addQueryItem("ie", "UTF-8");
|
|
119 url.addQueryItem("oe", "UTF-8");
|
|
120 url.addQueryItem("client", "qtdemobrowser");
|
77
|
121 search.emit(url);
|
73
|
122 }
|
45
|
123
|
73
|
124 private:
|
45
|
125
|
73
|
126 void save()
|
|
127 {
|
|
128 QSettings settings;
|
80
|
129 settings.beginGroup("toolbarsearch");
|
|
130 settings.setValue("recentSearches", m_stringListModel.stringList());
|
|
131 settings.setValue("maximumSaved", m_maxSavedSearches);
|
73
|
132 settings.endGroup();
|
|
133 }
|
45
|
134
|
|
135
|
73
|
136 void aboutToShowMenu()
|
|
137 {
|
|
138 lineEdit().selectAll();
|
|
139 QMenu m = menu();
|
|
140 m.clear();
|
77
|
141 string[] list = m_stringListModel.stringList();
|
73
|
142 if (list.isEmpty()) {
|
|
143 m.addAction(tr("No Recent Searches"));
|
|
144 return;
|
|
145 }
|
45
|
146
|
73
|
147 QAction recent = m.addAction(tr("Recent Searches"));
|
|
148 recent.setEnabled(false);
|
77
|
149 for (int i = 0; i < list.length; ++i) {
|
|
150 string text = list[i];
|
73
|
151 m.addAction(text).setData(text);
|
|
152 }
|
|
153 m.addSeparator();
|
|
154 m.addAction(tr("Clear Recent Searches"), this, SLOT(clear()));
|
|
155 }
|
45
|
156
|
73
|
157 void triggeredMenuAction(QAction action)
|
|
158 {
|
|
159 QVariant v = action.data();
|
141
|
160 if (v.canConvert!(string)) {
|
77
|
161 string text = v.toString();
|
73
|
162 lineEdit().setText(text);
|
|
163 searchNow();
|
|
164 }
|
|
165 }
|
45
|
166
|
|
167 private:
|
73
|
168
|
|
169 void load()
|
|
170 {
|
|
171 QSettings settings;
|
80
|
172 settings.beginGroup("toolbarsearch");
|
|
173 string[] list = settings.value("recentSearches").toStringList();
|
|
174 m_maxSavedSearches = settings.value("maximumSaved", m_maxSavedSearches).toInt();
|
73
|
175 m_stringListModel.setStringList(list);
|
|
176 settings.endGroup();
|
|
177 }
|
|
178
|
|
179 AutoSaver m_autosaver;
|
|
180 int m_maxSavedSearches;
|
|
181 QStringListModel m_stringListModel;
|
45
|
182 }
|