|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: hssearchview.cpp |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QSortFilterProxyModel> |
|
19 |
|
20 #include <HbGroupBox> |
|
21 #include <HbLineEdit> |
|
22 #include <HbListView> |
|
23 #include <HbAbstractViewItem> |
|
24 #include <HbView> |
|
25 #include <HbSearchPanel> |
|
26 #include <QApplication> |
|
27 #include <QInputContext> |
|
28 |
|
29 #include <caitemmodel.h> |
|
30 |
|
31 #include "hslistviewitem.h" |
|
32 #include "hsmainwindow.h" |
|
33 #include "hssearchview.h" |
|
34 |
|
35 /*! |
|
36 Set up proxy model for search. |
|
37 \param builder Retrieves UI widgets. |
|
38 \param stateContext Identifies the state where search starts from. |
|
39 \param mainWindow Enables switching between originating and search view. |
|
40 */ |
|
41 HsSearchView::HsSearchView(HsMenuViewBuilder &builder, |
|
42 HsStateContext stateContext, HsMainWindow &mainWindow) : |
|
43 mProxyModel(new QSortFilterProxyModel(this)), |
|
44 mSearchView(NULL), |
|
45 mSearchListView(NULL), mSearchPanel(NULL), mStateContext(stateContext), |
|
46 mBuilder(builder), mMainWindow(mainWindow), mListView(NULL), |
|
47 mVkbHost(NULL) |
|
48 { |
|
49 mProxyModel->setFilterRole(CaItemModel::TextRole); |
|
50 mProxyModel->setFilterKeyColumn(1); |
|
51 mProxyModel->setSortRole(CaItemModel::TextRole); |
|
52 } |
|
53 |
|
54 /*! |
|
55 Empty. |
|
56 */ |
|
57 HsSearchView::~HsSearchView() |
|
58 { |
|
59 } |
|
60 |
|
61 /*! |
|
62 Makes the UI to show or hide view search panel. |
|
63 When search panel is shown the view toolbar and status pane |
|
64 are hidden until search panel is hidden. |
|
65 \param visible When true search panel will be shown, |
|
66 otherwise it will be hidden. |
|
67 */ |
|
68 void HsSearchView::setSearchPanelVisible(bool visible) |
|
69 { |
|
70 HSMENUTEST_FUNC_ENTRY("HsMenuView::setSearchPanelVisible"); |
|
71 |
|
72 if (visible && !isActive()) { |
|
73 |
|
74 searchBegins(); |
|
75 |
|
76 connectSearchItemViewsSignals(); |
|
77 connectSearchPanelSignals(); |
|
78 |
|
79 } else if (isActive()) { |
|
80 |
|
81 disconnectSearchPanelSignals(); |
|
82 disconnectSearchItemViewsSignals(); |
|
83 |
|
84 searchFinished(); |
|
85 } |
|
86 HSMENUTEST_FUNC_EXIT("HsMenuView::setSearchPanelVisible"); |
|
87 } |
|
88 |
|
89 /*! |
|
90 \param view List view to operate on. |
|
91 \return Index of F=first visible item of \a view if any |
|
92 or default QModelIndex otherwise. |
|
93 */ |
|
94 QModelIndex HsSearchView::firstVisibleItemIndex(const HbListView *view) const |
|
95 { |
|
96 QModelIndex result; |
|
97 |
|
98 const QList<HbAbstractViewItem *> array = |
|
99 view->visibleItems(); |
|
100 |
|
101 if (array.count() >= 1) { |
|
102 result = array[0]->modelIndex(); |
|
103 } |
|
104 return result; |
|
105 } |
|
106 |
|
107 /*! |
|
108 Displays search panel with view representing search results. |
|
109 */ |
|
110 void HsSearchView::searchBegins() |
|
111 { |
|
112 HSMENUTEST_FUNC_ENTRY("HsSearchView::searchBegins"); |
|
113 |
|
114 setOriginatingContext(); |
|
115 |
|
116 mListView = mBuilder.currentListView(); |
|
117 mSearchViewInitialIndex = firstVisibleItemIndex(mListView); |
|
118 |
|
119 mProxyModel->invalidate(); |
|
120 mProxyModel->setSourceModel(mListView->model()); |
|
121 mProxyModel->setFilterRegExp( |
|
122 QRegExp(QString(".*"), Qt::CaseInsensitive, QRegExp::RegExp)); |
|
123 |
|
124 QString viewLabelHeading; |
|
125 |
|
126 if (mBuilder.currentViewLabel() != 0) { |
|
127 viewLabelHeading = mBuilder.currentViewLabel()->heading(); |
|
128 } |
|
129 |
|
130 setSearchContext(); |
|
131 |
|
132 mSearchView = mBuilder.currentView(); |
|
133 mSearchListView = mBuilder.currentListView(); |
|
134 mSearchPanel = mBuilder.currentSearchPanel(); |
|
135 initSearchPanel(*mSearchPanel); |
|
136 mVkbHost.reset(new HbShrinkingVkbHost(mSearchView)); |
|
137 |
|
138 if (mBuilder.currentViewLabel() != 0) { |
|
139 mBuilder.currentViewLabel()->setHeading(viewLabelHeading); |
|
140 } |
|
141 |
|
142 |
|
143 mSearchView->hideItems(Hb::AllItems); |
|
144 mSearchListView->setModel(mProxyModel); |
|
145 mSearchListView->setItemPrototype(new HsListViewItem()); |
|
146 |
|
147 mMainWindow.setCurrentView(mSearchView); |
|
148 |
|
149 mSearchListView->scrollTo( |
|
150 mProxyModel->mapFromSource(mSearchViewInitialIndex), |
|
151 HbAbstractItemView::PositionAtTop); |
|
152 HSMENUTEST_FUNC_EXIT("HsSearchView::searchBegins"); |
|
153 } |
|
154 |
|
155 |
|
156 /* |
|
157 Connects \a activated and \a longPressed signals coming from search list |
|
158 view to emit corresponding signal of this object with translated model index. |
|
159 */ |
|
160 void HsSearchView::connectSearchItemViewsSignals() |
|
161 { |
|
162 connect(mSearchListView, SIGNAL(activated(QModelIndex)), |
|
163 this, SLOT(activatedProxySlot(QModelIndex))); |
|
164 connect(mSearchListView, SIGNAL(longPressed(HbAbstractViewItem *, QPointF)), |
|
165 this, SLOT(longPressedProxySlot(HbAbstractViewItem *, QPointF))); |
|
166 } |
|
167 |
|
168 /*! |
|
169 Disconnects \a activated and \a longPressed signals coming from list |
|
170 view from to emit corresponding signal |
|
171 of this object with translated model index. |
|
172 */ |
|
173 void HsSearchView::disconnectSearchItemViewsSignals() |
|
174 { |
|
175 disconnect(mSearchListView, SIGNAL(activated(QModelIndex)), |
|
176 this, SLOT(activatedProxySlot(QModelIndex))); |
|
177 disconnect(mSearchListView, |
|
178 SIGNAL(longPressed(HbAbstractViewItem *, QPointF)), |
|
179 this, SLOT(longPressedProxySlot(HbAbstractViewItem *, QPointF))); |
|
180 } |
|
181 |
|
182 |
|
183 /*! |
|
184 Makes search panel invisible. |
|
185 Equivalent to \a setSearchPanelVisible(false). |
|
186 */ |
|
187 void HsSearchView::hideSearchPanel() |
|
188 { |
|
189 HSMENUTEST_FUNC_ENTRY("HsMenuView::hideSearchPanel"); |
|
190 setSearchPanelVisible(false); |
|
191 HSMENUTEST_FUNC_EXIT("HsMenuView::hideSearchPanel"); |
|
192 } |
|
193 |
|
194 /*! |
|
195 Connects signals \a exitClicked and \a criteriaChanged emitted |
|
196 by search panel with handling slots of the object or its members. |
|
197 */ |
|
198 void HsSearchView::connectSearchPanelSignals() |
|
199 { |
|
200 connect(mSearchPanel, SIGNAL(exitClicked()), |
|
201 this, SLOT(hideSearchPanel())); |
|
202 connect(mSearchPanel, SIGNAL(criteriaChanged(QString)), |
|
203 this, SLOT(findItem(QString))); |
|
204 } |
|
205 |
|
206 /*! |
|
207 Disconnects signals \a exitClicked and \a criteriaChanged emitted |
|
208 by search panel from handling slots of the object or its members |
|
209 Scrolls view to state before connections. |
|
210 */ |
|
211 void HsSearchView::disconnectSearchPanelSignals() |
|
212 { |
|
213 disconnect(mSearchPanel, SIGNAL(exitClicked()), |
|
214 this, SLOT(hideSearchPanel())); |
|
215 disconnect(mSearchPanel, SIGNAL(criteriaChanged(QString)), |
|
216 this, SLOT(findItem(QString))); |
|
217 } |
|
218 |
|
219 /*! |
|
220 Scrolls originating view to first item matching search criteria. |
|
221 */ |
|
222 void HsSearchView::searchFinished() |
|
223 { |
|
224 HSMENUTEST_FUNC_ENTRY("HsSearchView::searchFinished"); |
|
225 |
|
226 mIndexToScrollAfterSearchDone = |
|
227 firstVisibleItemIndex(mSearchListView); |
|
228 |
|
229 setOriginatingContext(); |
|
230 |
|
231 mMainWindow.setCurrentView(mBuilder.currentView()); |
|
232 |
|
233 mListView->scrollTo(mProxyModel->mapToSource(mIndexToScrollAfterSearchDone), |
|
234 HbAbstractItemView::PositionAtTop); |
|
235 |
|
236 HbVkbHost::detachHost(mSearchView); |
|
237 mVkbHost.reset(NULL); |
|
238 mSearchListView = NULL; |
|
239 mSearchPanel = NULL; |
|
240 HSMENUTEST_FUNC_EXIT("HsSearchView::searchFinished"); |
|
241 } |
|
242 |
|
243 /*! |
|
244 Looks up for item and if found scrolls to it. |
|
245 \param criteriaStr The item name to find. |
|
246 */ |
|
247 void HsSearchView::findItem(QString criteriaStr) |
|
248 { |
|
249 qDebug() << "HsSearchView::findItem: " + criteriaStr; |
|
250 HSMENUTEST_FUNC_ENTRY("HsSearchView::findItem"); |
|
251 |
|
252 mProxyModel->invalidate(); |
|
253 mProxyModel->setSourceModel(mListView->model()); |
|
254 mProxyModel->setFilterRegExp( |
|
255 QRegExp("(^|\\b)" + criteriaStr, Qt::CaseInsensitive)); |
|
256 |
|
257 mSearchListView->scrollTo( |
|
258 mProxyModel->index(0,0), HbAbstractItemView::PositionAtTop); |
|
259 |
|
260 HSMENUTEST_FUNC_EXIT("HsSearchView::findItem"); |
|
261 } |
|
262 |
|
263 /*! |
|
264 Slot used to translate activated signal from proxy model to normal model. |
|
265 \param index Represents an item activated in search list view. |
|
266 */ |
|
267 void HsSearchView::activatedProxySlot(const QModelIndex &index) |
|
268 { |
|
269 emit activated(mProxyModel->mapToSource(index)); |
|
270 } |
|
271 |
|
272 /*! |
|
273 Slot used to forward 'long pressed' signal with item description transladed |
|
274 from search view context to context of the view search was requested from. |
|
275 \param item Long pressed item. |
|
276 \param coords Coordinates of the long press action. |
|
277 */ |
|
278 void HsSearchView::longPressedProxySlot(HbAbstractViewItem *item, |
|
279 const QPointF &coords) |
|
280 { |
|
281 /* |
|
282 this is a kind of hack, introduced for reasons: |
|
283 item object should be reusable later, but orbit (or qt) prevents setting |
|
284 its index model to previous state |
|
285 */ |
|
286 mSearchViewLongPressedIndex = |
|
287 mProxyModel->mapToSource(item->modelIndex()); |
|
288 QScopedPointer<HbAbstractViewItem> itemNew(item->createItem()); |
|
289 itemNew->setModelIndex(mSearchViewLongPressedIndex); |
|
290 emit longPressed(itemNew.data(), coords); |
|
291 } |
|
292 |
|
293 /*! |
|
294 Sets up search panel. |
|
295 |
|
296 \param searchPanel Search panel to initialize. |
|
297 */ |
|
298 void HsSearchView::initSearchPanel(HbSearchPanel &searchPanel) |
|
299 { |
|
300 HbLineEdit *const lineEdit(searchPanelLineEdit(searchPanel)); |
|
301 |
|
302 lineEdit->setText(""); |
|
303 lineEdit->setFocus(); |
|
304 lineEdit->setInputMethodHints(Qt::ImhNoPredictiveText | |
|
305 Qt::ImhNoAutoUppercase); |
|
306 } |
|
307 |
|
308 /*! |
|
309 \param searchPanel Search panel to operate on. |
|
310 \return Line edit of the searchPanel on success, NULL otherwise. |
|
311 */ |
|
312 HbLineEdit *HsSearchView::searchPanelLineEdit( |
|
313 HbSearchPanel &searchPanel) const |
|
314 { |
|
315 HSMENUTEST_FUNC_ENTRY("HsSearchView::searchPanelLineEdit"); |
|
316 |
|
317 HbLineEdit *result(0); |
|
318 |
|
319 foreach(QGraphicsItem *obj, searchPanel.childItems()) { |
|
320 |
|
321 QGraphicsWidget *const widget = static_cast<QGraphicsWidget *>(obj); |
|
322 |
|
323 if (widget != NULL) { |
|
324 |
|
325 HbLineEdit *const lineEdit = qobject_cast<HbLineEdit *>(widget); |
|
326 |
|
327 if (lineEdit != NULL) { |
|
328 result = lineEdit; |
|
329 break; |
|
330 } |
|
331 } |
|
332 } |
|
333 HSMENUTEST_FUNC_EXIT("HsSearchView::searchPanelLineEdit"); |
|
334 |
|
335 return result; |
|
336 } |
|
337 |
|
338 /*! |
|
339 Sets the builder context to the one determined by \a mStateContext and |
|
340 \a HsItemViewContext. |
|
341 */ |
|
342 void HsSearchView::setOriginatingContext() |
|
343 { |
|
344 mBuilder.setStateContext(mStateContext); |
|
345 mBuilder.setOperationalContext(HsItemViewContext); |
|
346 } |
|
347 |
|
348 /*! |
|
349 Sets the builder context to the one determined by \a mStateContext and |
|
350 \a HsSearchContext. |
|
351 */ |
|
352 void HsSearchView::setSearchContext() |
|
353 { |
|
354 mBuilder.setStateContext(mStateContext); |
|
355 mBuilder.setOperationalContext(HsSearchContext); |
|
356 } |
|
357 /*! |
|
358 \retval true when search view is already invoked, \a false otherwise. |
|
359 */ |
|
360 bool HsSearchView::isActive() const |
|
361 { |
|
362 return mSearchListView != NULL; |
|
363 } |
|
364 |