|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include "hbcombobox_p.h" |
|
27 |
|
28 #include <hblistview.h> |
|
29 #include <hbabstractviewitem.h> |
|
30 #include <hbdeviceprofile.h> |
|
31 #include <hbtoucharea.h> |
|
32 #include <hbmainwindow.h> |
|
33 #include <hbview.h> |
|
34 #include <hbtextitem.h> |
|
35 #include <hbstyleoptioncombobox.h> |
|
36 |
|
37 #include <QSortFilterProxyModel> |
|
38 #include <QGraphicsScene> |
|
39 #include <QCompleter> |
|
40 #include <QItemSelectionModel> |
|
41 |
|
42 #ifdef HB_EFFECTS |
|
43 #include <hbeffect.h> |
|
44 #include "hbeffectinternal_p.h" |
|
45 #define HB_DROPD0WN_ITEM_TYPE "HB_DROPDOWN" |
|
46 #endif |
|
47 |
|
48 HbComboBoxPrivate::HbComboBoxPrivate( ): |
|
49 HbWidgetPrivate ( ), |
|
50 mLineEdit ( 0 ), |
|
51 mTextItem ( 0 ), |
|
52 mButton ( 0 ), |
|
53 mDropDown ( 0 ), |
|
54 mModel ( 0 ), |
|
55 mProxyModel ( 0 ), |
|
56 mCompleter ( 0 ), |
|
57 insertPolicy ( HbComboBox::InsertAtBottom ), |
|
58 mBackgroundItem ( 0 ), |
|
59 mButtonTouchAreaItem ( 0 ), |
|
60 mIsDown ( false ), |
|
61 mEditable ( false ), |
|
62 mIsDorpdownCreated(false), |
|
63 mIsDropwnToSceneAdded(false), |
|
64 mHasDownEffect ( false ), |
|
65 mHasUpEffect (false ), |
|
66 mListItemHeight(-1) |
|
67 { |
|
68 } |
|
69 |
|
70 HbComboBoxPrivate::~HbComboBoxPrivate( ) |
|
71 { |
|
72 Q_Q(HbComboBox); |
|
73 if( mButtonTouchAreaItem ) { |
|
74 static_cast<HbTouchArea*>(mButtonTouchAreaItem)->removeEventFilter( q ); |
|
75 } |
|
76 if (!q->scene() || !q->scene()->property("destructed").isValid()) { |
|
77 if( mDropDown ) { |
|
78 delete mDropDown; |
|
79 mDropDown = 0; |
|
80 } |
|
81 } |
|
82 } |
|
83 |
|
84 void HbComboBoxPrivate::init( ) |
|
85 { |
|
86 createPrimitives( ); |
|
87 } |
|
88 |
|
89 void HbComboBoxPrivate::createPrimitives( ) |
|
90 { |
|
91 Q_Q( HbComboBox ); |
|
92 |
|
93 mTextItem = q->style()->createPrimitive( HbStyle::P_ComboBox_text, q ); |
|
94 HbStyle::setItemName( mTextItem, "combobox_labelfield" ); |
|
95 |
|
96 mBackgroundItem = q->style( )->createPrimitive( HbStyle::P_ComboBox_background, q ); |
|
97 HbStyle::setItemName( mBackgroundItem, "text_background" ); |
|
98 |
|
99 mButton = q->style( )->createPrimitive( HbStyle::P_ComboBox_button, q ); |
|
100 HbStyle::setItemName( mButton, "combobox_button" ); |
|
101 |
|
102 mButtonTouchAreaItem = q->style( )->createPrimitive( |
|
103 HbStyle::P_ComboBoxButton_toucharea, q ); |
|
104 static_cast<HbTouchArea*>(mButtonTouchAreaItem)->installEventFilter( q ); |
|
105 q->setHandlesChildEvents(true); |
|
106 } |
|
107 |
|
108 void HbComboBoxPrivate::touchAreaPressEvent( ) |
|
109 { |
|
110 Q_Q( HbComboBox ); |
|
111 if (q->count() > 0) { |
|
112 HbWidgetFeedback::triggered(q, Hb::InstantPressed); |
|
113 } |
|
114 mIsDown = true; |
|
115 q->updatePrimitives( ); |
|
116 q->setProperty("state", "pressed"); |
|
117 } |
|
118 |
|
119 void HbComboBoxPrivate::touchAreaReleaseEvent( ) |
|
120 { |
|
121 Q_Q( HbComboBox ); |
|
122 mIsDown = false; |
|
123 touchAreaClicked(); |
|
124 q->updatePrimitives( ); |
|
125 if (q->count() > 0) { |
|
126 HbWidgetFeedback::triggered(q, Hb::InstantReleased); |
|
127 } |
|
128 |
|
129 q->setProperty("state", "normal"); |
|
130 } |
|
131 |
|
132 void HbComboBoxPrivate::touchAreaClicked( ) |
|
133 { |
|
134 Q_Q( HbComboBox ); |
|
135 if ( mModel && mModel->rowCount( ) ) { |
|
136 addDropDownToScene(); |
|
137 if( !mDropDown->mList ) { |
|
138 mDropDown->createList( ); |
|
139 mDropDown->mList->setModel( mModel ); |
|
140 q->connect( mDropDown->mList, SIGNAL( activated( QModelIndex ) ), q, |
|
141 SLOT( _q_textChanged( QModelIndex ) ) ); |
|
142 } |
|
143 if ( mCurrentIndex.isValid( ) ) { |
|
144 if( mDropDown->mList->model( ) != mModel ) { |
|
145 mDropDown->mList->setModel( mModel ); |
|
146 } |
|
147 mDropDown->mList->scrollTo( mCurrentIndex, HbAbstractItemView::PositionAtTop ); |
|
148 mDropDown->mList->setCurrentIndex( mCurrentIndex, QItemSelectionModel::Select ); |
|
149 } else { |
|
150 if( mDropDown->mList->model( ) != mModel ) { |
|
151 mDropDown->mList->setModel( mModel ); |
|
152 } |
|
153 mDropDown->mList->scrollTo( mModel->index( 0, 0 ) ); |
|
154 mDropDown->mList->setCurrentIndex(mModel->index( 0, 0 ), QItemSelectionModel::Select); |
|
155 } |
|
156 #ifdef HB_EFFECTS |
|
157 HbEffect::start(mDropDown, HB_DROPD0WN_ITEM_TYPE, "appear"); |
|
158 #endif |
|
159 positionDropDown( ); |
|
160 mDropDown->setVisible( true ); |
|
161 } |
|
162 } |
|
163 |
|
164 void HbComboBoxPrivate::vkbOpened( ) |
|
165 { |
|
166 |
|
167 } |
|
168 |
|
169 void HbComboBoxPrivate::vkbClosed() |
|
170 { |
|
171 if( mDropDown->isVisible()) { |
|
172 positionDropDown(); |
|
173 } |
|
174 } |
|
175 |
|
176 void HbComboBoxPrivate::showPopup( QAbstractItemModel* aModel, QModelIndex aIndex ) |
|
177 { |
|
178 Q_UNUSED( aModel ); |
|
179 Q_UNUSED( aIndex ); |
|
180 Q_Q( HbComboBox ); |
|
181 if ( aModel && aModel->rowCount( ) ) { |
|
182 addDropDownToScene(); |
|
183 if( !mDropDown->mList ) { |
|
184 mDropDown->createList(); |
|
185 q->connect( mDropDown->mList, SIGNAL( activated( QModelIndex ) ), q, |
|
186 SLOT( _q_textChanged( QModelIndex ) ) ); |
|
187 } |
|
188 mDropDown->mList->setModel( aModel ); |
|
189 if ( aIndex.isValid( ) ) { |
|
190 mDropDown->mList->scrollTo( aIndex, HbAbstractItemView::PositionAtTop ); |
|
191 mDropDown->mList->setCurrentIndex(mCurrentIndex, QItemSelectionModel::Select); |
|
192 } else { |
|
193 mDropDown->mList->scrollTo( aModel->index( 0, 0 ) ); |
|
194 } |
|
195 positionDropDown( ); |
|
196 mDropDown->setVisible( true ); |
|
197 } |
|
198 } |
|
199 |
|
200 void HbComboBoxPrivate::createDropDown() |
|
201 { |
|
202 if( !mIsDorpdownCreated ) { |
|
203 mDropDown = new HbComboDropDown( this ); |
|
204 mIsDorpdownCreated = true; |
|
205 mDropDown->setVisible(false); |
|
206 } |
|
207 } |
|
208 |
|
209 void HbComboBoxPrivate::calculateListItemHeight() |
|
210 { |
|
211 if( mListItemHeight == -1 ) { |
|
212 QAbstractItemModel *model = mDropDown->mList->model( ); |
|
213 if( mCurrentIndex.isValid( ) && mDropDown->mList->itemByIndex( mCurrentIndex ) ) { |
|
214 mListItemHeight = mDropDown->mList->itemByIndex( mCurrentIndex )->geometry( ).height( ); |
|
215 } else if( model->index( 0, 0 ).isValid() && mDropDown->mList->itemByIndex( model->index( 0, 0 ) ) ) { |
|
216 mListItemHeight = mDropDown->mList->itemByIndex( model->index( 0, 0 ) )->geometry( ).height( ); |
|
217 } else { |
|
218 HbListViewItem *proto = mDropDown->mList->listItemPrototype(); |
|
219 HbListViewItem *temp = static_cast<HbListViewItem*>(proto->createItem()); |
|
220 mListItemHeight = temp->effectiveSizeHint(Qt::PreferredSize).height(); |
|
221 delete temp; |
|
222 temp = 0; |
|
223 } |
|
224 } |
|
225 } |
|
226 |
|
227 void HbComboBoxPrivate::positionDropDown( ) |
|
228 { |
|
229 Q_Q( HbComboBox ); |
|
230 QRectF popupRect; |
|
231 QRectF sceneRect( QPointF( ), HbDeviceProfile::profile( q ).logicalSize( ) ); |
|
232 QPointF widgetPos = q->scenePos( ); |
|
233 QAbstractItemModel *model = mDropDown->mList->model( ); |
|
234 calculateListItemHeight(); |
|
235 qreal totalHeightRequd = model->rowCount( ) * mListItemHeight; |
|
236 qreal maxPopupHeight = 0.0; |
|
237 if(q->mainWindow()->orientation() == Qt::Horizontal ) { |
|
238 maxPopupHeight = 5 * mListItemHeight; |
|
239 } else if(q->mainWindow()->orientation() == Qt::Vertical ) { |
|
240 maxPopupHeight = 8 * mListItemHeight; |
|
241 } |
|
242 if ( totalHeightRequd < maxPopupHeight ) { |
|
243 maxPopupHeight = totalHeightRequd; |
|
244 } |
|
245 QSizeF popupSize = QSizeF( q->rect( ).width( ), maxPopupHeight ); |
|
246 QPointF popupPos; |
|
247 if( !mDropDown->vkbOpened ) { |
|
248 //position of drop down in both editable and non-editable combobox depends upon |
|
249 //the available space above and below combobox |
|
250 if( (widgetPos.y( ) + q->rect( ).height( ) + maxPopupHeight) < sceneRect.height( ) ) { |
|
251 popupPos = QPointF( widgetPos.x(), widgetPos.y( )+ q->rect( ).height( ) ); |
|
252 #ifdef HB_EFFECTS |
|
253 if ( !mHasDownEffect ) { |
|
254 mHasDownEffect = true; |
|
255 mHasUpEffect = false; |
|
256 // this is temporary until proper effect theming comes. |
|
257 //this Effect will be shown when there is space in the view bottom. |
|
258 HbEffectInternal::add( mDropDown, "combo_appear_down", "appear" ); |
|
259 HbEffectInternal::add( mDropDown, "combo_disappear_downl", "disappear" ); |
|
260 } |
|
261 #endif |
|
262 } else if( widgetPos.y() - maxPopupHeight > 0.0 ) { |
|
263 popupPos = QPointF( widgetPos.x(), widgetPos.y()-maxPopupHeight ); |
|
264 #ifdef HB_EFFECTS |
|
265 if ( !mHasUpEffect ) { |
|
266 // this is temporary until proper effect theming comes. |
|
267 //this Effect will be shown when there is no space in the view bottom |
|
268 mHasUpEffect = true; |
|
269 mHasDownEffect = false; |
|
270 HbEffectInternal::add( mDropDown, "combo_appear_up", "appear" ); |
|
271 HbEffectInternal::add( mDropDown, "combo_disappear_up", "disappear" ); |
|
272 } |
|
273 #endif |
|
274 } else { |
|
275 qreal topScreenHeight = sceneRect.height( ) - maxPopupHeight; |
|
276 if( topScreenHeight > sceneRect.height( ) - topScreenHeight ) { |
|
277 popupPos = QPointF( widgetPos.x( ), 0.0 ); |
|
278 #ifdef HB_EFFECTS |
|
279 if ( !mHasDownEffect ) { |
|
280 mHasDownEffect = true; |
|
281 mHasUpEffect = false; |
|
282 // this is temporary until proper effect theming comes. |
|
283 //this Effect will be shown when there is more space in the view bottom. |
|
284 HbEffectInternal::add( mDropDown, "combo_appear_down", "appear" ); |
|
285 HbEffectInternal::add( mDropDown, "combo_disappear_down", "disappear" ); |
|
286 } |
|
287 #endif |
|
288 } else { |
|
289 popupPos = QPointF( widgetPos.x( ), sceneRect.height( ) - maxPopupHeight ); |
|
290 #ifdef HB_EFFECTS |
|
291 if ( !mHasUpEffect ) { |
|
292 mHasUpEffect = true; |
|
293 mHasDownEffect = false; |
|
294 // this is temporary until proper effect theming comes. |
|
295 //this Effect will be shown when there is more space in the view bottom. |
|
296 HbEffectInternal::add( mDropDown, "combo_appear_up", "appear" ); |
|
297 HbEffectInternal::add( mDropDown, "combo_disappear_up", "disappear" ); |
|
298 } |
|
299 #endif |
|
300 } |
|
301 } |
|
302 } else { |
|
303 // positioning drop down when vkb is positioned |
|
304 // drop down will come on top/below of combo based upon which side has more space |
|
305 // available |
|
306 |
|
307 HbEditorInterface editorInterface(q); |
|
308 HbVkbHost *host = editorInterface.vkbHost(); |
|
309 if ( host ) { |
|
310 QSizeF keyBoardArea = host->keyboardArea(); |
|
311 QSize screenSize = HbDeviceProfile::profile(q).logicalSize(); |
|
312 |
|
313 qreal heightDifference = screenSize.height() - keyBoardArea.height(); |
|
314 qreal topSpace = widgetPos.y(); |
|
315 qreal bottomSpace = heightDifference - topSpace - q->boundingRect().height(); |
|
316 |
|
317 if( topSpace > bottomSpace ) { |
|
318 //display drop down at top |
|
319 if( widgetPos.y() - maxPopupHeight > 0.0 ) { |
|
320 popupPos = QPointF( widgetPos.x(), widgetPos.y() - maxPopupHeight ); |
|
321 } else { |
|
322 popupPos = QPointF( widgetPos.x(), 0.0 ); |
|
323 popupSize.setHeight( topSpace ); |
|
324 } |
|
325 #ifdef HB_EFFECTS |
|
326 if ( !mHasUpEffect ) { |
|
327 mHasUpEffect = true; |
|
328 mHasDownEffect = false; |
|
329 // this is temporary until proper effect theming comes. |
|
330 //this Effect will be shown when there is more space in the view bottom. |
|
331 HbEffectInternal::add( mDropDown, "combo_appear_up", "appear" ); |
|
332 HbEffectInternal::add( mDropDown, "combo_disappear_up", "disappear" ); |
|
333 } |
|
334 #endif |
|
335 |
|
336 } else { |
|
337 //display drop down at bottom |
|
338 popupPos = QPointF( widgetPos.x(), widgetPos.y( ) + q->rect( ).height( ) ); |
|
339 if( bottomSpace < maxPopupHeight ) { |
|
340 popupSize.setHeight( bottomSpace ); |
|
341 } |
|
342 #ifdef HB_EFFECTS |
|
343 if ( !mHasDownEffect ) { |
|
344 mHasDownEffect = true; |
|
345 mHasUpEffect = false; |
|
346 // this is temporary until proper effect theming comes. |
|
347 //this Effect will be shown when there is more space in the view bottom. |
|
348 HbEffectInternal::add( mDropDown, "combo_appear_down", "appear" ); |
|
349 HbEffectInternal::add( mDropDown, "combo_disappear_down", "disappear" ); |
|
350 } |
|
351 #endif |
|
352 } |
|
353 } |
|
354 } |
|
355 mDropDown->setPreferredSize( popupSize ); |
|
356 mDropDown->setMinimumSize( popupSize ); |
|
357 mDropDown->setMaximumSize( popupSize ); |
|
358 mDropDown->setPos(popupPos); |
|
359 QGraphicsWidget* p = q; |
|
360 while(p->parentWidget()) { |
|
361 p = p->parentWidget(); |
|
362 } |
|
363 mDropDown->setZValue( p->zValue( ) + 1 ); |
|
364 } |
|
365 |
|
366 void HbComboBoxPrivate::_q_textChanged( const QModelIndex & aIndex ) |
|
367 { |
|
368 Q_Q( HbComboBox ); |
|
369 QVariant data = mDropDown->mList->model( )->data( aIndex ); |
|
370 mText = data.toString( ); |
|
371 if( !mEditable ) { |
|
372 if( mLineEdit ) { |
|
373 mLineEdit->setText( mText ); |
|
374 } else { |
|
375 HbStyleOptionComboBox comboBoxOption; |
|
376 q->initStyleOption(&comboBoxOption); |
|
377 q->style()->updatePrimitive( mTextItem, HbStyle::P_ComboBox_text, &comboBoxOption); |
|
378 } |
|
379 mCurrentIndex = aIndex; |
|
380 } else { |
|
381 q->disconnect( mLineEdit, SIGNAL( textChanged ( QString ) ), q, |
|
382 SLOT( _q_textChanged( QString ) ) ); |
|
383 mLineEdit->setText( mText ); |
|
384 mCurrentIndex = findData( mText ); |
|
385 q->connect( mLineEdit, SIGNAL( textChanged ( QString ) ), q, |
|
386 SLOT( _q_textChanged( QString ) ) ); |
|
387 } |
|
388 if ( mDropDown->isVisible( ) ) { |
|
389 mDropDown->setVisible( false ); |
|
390 } |
|
391 currentIndexChanged( mCurrentIndex ); |
|
392 } |
|
393 |
|
394 void HbComboBoxPrivate::_q_textCompleted( const QModelIndex & aIndex ) |
|
395 { |
|
396 if( aIndex.isValid( ) ) { |
|
397 showPopup( mCompleter->completionModel( ) ); |
|
398 } |
|
399 } |
|
400 |
|
401 void HbComboBoxPrivate::_q_textChanged( const QString & aString ) |
|
402 { |
|
403 Q_Q(HbComboBox); |
|
404 |
|
405 if( !aString.isEmpty( ) ) { |
|
406 if ( mCompleter ) { |
|
407 mCompleter->setCompletionPrefix( aString ); |
|
408 mCompleter->complete( ); |
|
409 if( mCompleter->currentRow() == -1 ) |
|
410 { |
|
411 if (( mDropDown ) && ( mDropDown->isVisible() )) { |
|
412 mDropDown->setVisible(false); |
|
413 } |
|
414 } |
|
415 } |
|
416 } else { |
|
417 showPopup( mModel, mCurrentIndex); |
|
418 } |
|
419 emit q->editTextChanged( aString ); |
|
420 } |
|
421 |
|
422 void HbComboBoxPrivate::setModel( QAbstractItemModel * model ) |
|
423 { |
|
424 Q_Q ( HbComboBox ); |
|
425 createDropDown( ); |
|
426 if ( mDropDown->isVisible( ) ) { |
|
427 mDropDown->setVisible( false ); |
|
428 } |
|
429 q->clear( ); |
|
430 delete mModel; |
|
431 mModel = model; |
|
432 mModel->setParent( q ); |
|
433 setCompletion( mEditable ); |
|
434 if( mModel->rowCount( ) ) { |
|
435 q->setCurrentIndex( 0 ); |
|
436 } |
|
437 } |
|
438 |
|
439 void HbComboBoxPrivate::setCompletion( bool completion ) |
|
440 { |
|
441 Q_Q( HbComboBox ); |
|
442 if ( completion ) { |
|
443 if ( mCompleter ) { |
|
444 mProxyModel->setSourceModel( mModel ); |
|
445 mProxyModel->sort( Qt::AscendingOrder ); |
|
446 mCompleter->setModel( mProxyModel ); |
|
447 } else { |
|
448 mProxyModel = new QSortFilterProxyModel( q ); |
|
449 mProxyModel->setDynamicSortFilter( true ); |
|
450 mProxyModel->setSortCaseSensitivity( Qt::CaseInsensitive ); |
|
451 mProxyModel->setSourceModel( mModel ); |
|
452 mProxyModel->sort( Qt::AscendingOrder ); |
|
453 mCompleter = new QCompleter( mProxyModel, q ); |
|
454 mCompleter->setCaseSensitivity( Qt::CaseInsensitive ); |
|
455 mCompleter->setCompletionRole( Qt::DisplayRole ); |
|
456 mCompleter->setCompletionMode( QCompleter::InlineCompletion ); |
|
457 q->connect( mCompleter, SIGNAL( highlighted( QModelIndex ) ), q, |
|
458 SLOT( _q_textCompleted( QModelIndex ) ) ); |
|
459 } |
|
460 } else { |
|
461 if ( mCompleter ) { |
|
462 delete mCompleter; |
|
463 mCompleter = 0; |
|
464 } |
|
465 if ( mProxyModel ) { |
|
466 delete mProxyModel; |
|
467 mProxyModel = 0; |
|
468 } |
|
469 } |
|
470 } |
|
471 |
|
472 void HbComboBoxPrivate::setEditable( bool editable ) |
|
473 { |
|
474 Q_Q(HbComboBox); |
|
475 if( mEditable == editable ) { |
|
476 return; |
|
477 } |
|
478 mEditable = editable; |
|
479 if( editable ) |
|
480 { |
|
481 if( mTextItem ) { |
|
482 HbStyle::setItemName( mTextItem, "" ); |
|
483 delete mTextItem; |
|
484 mTextItem = 0; |
|
485 mLineEdit = new HbCustomLineEdit( q, this ); |
|
486 HbStyle::setItemName( mLineEdit, "combobox_labelfield" ); |
|
487 mLineEdit->backgroundItem()->setVisible(false); |
|
488 } |
|
489 q->setHandlesChildEvents( false ); |
|
490 mLineEdit->setReadOnly( false ); |
|
491 mLineEdit->setCursorVisibility( Hb::TextCursorVisible ); |
|
492 mLineEdit->setLongPressEnabled( ); |
|
493 q->repolish( ); |
|
494 q->connect( mLineEdit, SIGNAL( textChanged ( QString ) ), |
|
495 q, SLOT( _q_textChanged( QString ) ) ); |
|
496 setCompletion( true ); |
|
497 } else { |
|
498 q->disconnect( mLineEdit, SIGNAL( textChanged ( QString ) ), |
|
499 q, SLOT( _q_textChanged( QString ) ) ); |
|
500 q->setHandlesChildEvents( true ); |
|
501 mLineEdit->setReadOnly( true ); |
|
502 mLineEdit->setLongPressEnabled( false ); |
|
503 setCompletion( false ); |
|
504 mLineEdit->setCursorVisibility( Hb::TextCursorHidden ); |
|
505 if( mModel && mModel->rowCount( ) ) { |
|
506 QModelIndex mi = mModel->index( 0, 0 ); |
|
507 if( mi.isValid( ) ) { |
|
508 mCurrentIndex = QModelIndex( mi ); |
|
509 mText = q->itemText( mCurrentIndex.row( ) ); |
|
510 mLineEdit->setText( mText ); |
|
511 } |
|
512 } |
|
513 q->repolish( ); |
|
514 } |
|
515 } |
|
516 |
|
517 QString HbComboBoxPrivate::itemText( const QModelIndex &index ) const |
|
518 { |
|
519 return mModel->data( index, itemRole( ) ).toString( ); |
|
520 } |
|
521 |
|
522 QIcon HbComboBoxPrivate::itemIcon( const QModelIndex &index ) const |
|
523 { |
|
524 QVariant decoration = mModel->data( index, Qt::DecorationRole ); |
|
525 if ( decoration.type() == QVariant::Icon ) { |
|
526 return QIcon( qvariant_cast<QIcon>( decoration ) ); |
|
527 } |
|
528 return qvariant_cast<QIcon>( decoration ); |
|
529 } |
|
530 |
|
531 int HbComboBoxPrivate::itemRole( ) const |
|
532 { |
|
533 return q_func( )->isEditable( ) ? Qt::EditRole : Qt::DisplayRole; |
|
534 } |
|
535 |
|
536 void HbComboBoxPrivate::addDropDownToScene( ) |
|
537 { |
|
538 Q_Q( HbComboBox ); |
|
539 if( !mIsDropwnToSceneAdded ) { |
|
540 if ( q->scene( ) ) { |
|
541 q->scene( )->addItem( mDropDown ); |
|
542 } |
|
543 QGraphicsScene *scene1 = mDropDown->scene( ); |
|
544 if( scene1 ) |
|
545 { |
|
546 scene1->installEventFilter( mDropDown ); |
|
547 } |
|
548 mIsDropwnToSceneAdded = true; |
|
549 } |
|
550 } |
|
551 void HbComboBoxPrivate::setCurrentIndex( const QModelIndex &mi ) |
|
552 { |
|
553 Q_Q( HbComboBox ); |
|
554 bool indexChanged = ( mi != mCurrentIndex ); |
|
555 if ( indexChanged ) { |
|
556 mCurrentIndex = QModelIndex( mi ); |
|
557 mText = q->itemText( mCurrentIndex.row( ) ); |
|
558 if( mEditable ) { |
|
559 q->disconnect( mLineEdit, SIGNAL( textChanged ( QString ) ), q, |
|
560 SLOT( _q_textChanged( QString ) ) ); |
|
561 mLineEdit->setText( mText ); |
|
562 q->connect( mLineEdit, SIGNAL( textChanged ( QString ) ), |
|
563 q, SLOT( _q_textChanged( QString ) ) ); |
|
564 } else { |
|
565 if( mLineEdit ) { |
|
566 mLineEdit->setText( mText ); |
|
567 } else { |
|
568 HbStyleOptionComboBox comboBoxOption; |
|
569 q->initStyleOption(&comboBoxOption); |
|
570 q->style()->updatePrimitive( mTextItem, HbStyle::P_ComboBox_text, &comboBoxOption); |
|
571 } |
|
572 } |
|
573 currentIndexChanged( mCurrentIndex ); |
|
574 } |
|
575 } |
|
576 |
|
577 void HbComboBoxPrivate::currentIndexChanged( const QModelIndex &index ) |
|
578 { |
|
579 Q_Q( HbComboBox ); |
|
580 emit q->currentIndexChanged( index.row( ) ); |
|
581 emit q->currentIndexChanged( q->itemText ( mCurrentIndex.row( ) ) ); |
|
582 } |
|
583 |
|
584 /* |
|
585 Returns the index of the item containing the given \a data for the |
|
586 given \a role; otherwise returns QModelIndex. |
|
587 |
|
588 The \a flags specify how the items in the combobox are searched. |
|
589 */ |
|
590 QModelIndex HbComboBoxPrivate::findData( const QVariant &data ) const |
|
591 { |
|
592 QModelIndexList result; |
|
593 if ( mModel ) { |
|
594 QModelIndex start = mModel->index( 0, 0 ); |
|
595 result = mModel->match( |
|
596 start, Qt::DisplayRole, data, 1, Qt::MatchExactly|Qt::MatchCaseSensitive ); |
|
597 } |
|
598 if ( result.isEmpty( ) ) { |
|
599 return QModelIndex( ); |
|
600 } else { |
|
601 return result.first( ); |
|
602 } |
|
603 } |
|
604 |
|
605 #include "moc_hbcombobox.cpp" |
|
606 |
|
607 |