src/hbwidgets/widgets/hbcombobox.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     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 <hbcombobox.h>
       
    29 #include <hblistview.h>
       
    30 #include <hbtoucharea.h>
       
    31 #include <hbtextitem.h>
       
    32 #include <hbstyleoptioncombobox.h>
       
    33 
       
    34 #include <QGraphicsSceneMouseEvent>
       
    35 #include <QStandardItemModel>
       
    36 #include <QDebug>
       
    37 
       
    38 /*!
       
    39     @beta
       
    40     @hbwidgets
       
    41     \class HbComboBox
       
    42 
       
    43     \brief The HbComboBox provides a drop-down list that permits selecting an item.
       
    44 
       
    45     A HbComboBox provides a means of presenting a list of options to the user
       
    46     in a way that takes up the minimum amount of screen space.
       
    47 
       
    48     A combobox is a selection widget that displays the currently selected item,
       
    49     and can provides a drop-down list that permits selecting an item.
       
    50     
       
    51     A HbComboBox with currently Selected item
       
    52     \image html noneditablecombobox.png.
       
    53 
       
    54 
       
    55     A HbComboBox with drop-down list to select an item from list of options
       
    56     \image html comboboxdropdownlist.png.
       
    57 
       
    58 
       
    59     HbComboBox are of two types. 
       
    60 
       
    61     Editable:
       
    62 
       
    63     If the comboBox is set to editable, touching the ComboBox label field invokes the VKB in
       
    64     touch environments or accepts input from an attached keyboard.
       
    65     choosing an element from the ComboBox dropdown list replaces the label 
       
    66     area with the chosen item.
       
    67     on touching button area of the comboBox launches the drop-down list to select
       
    68     the list of options 
       
    69     ( button area in comboBox behaves same in both editable and Non Editable comboBox ).
       
    70 
       
    71     Non Editable:
       
    72     
       
    73     If the comboBox is set to Non Editable widget that displays the currently selected item
       
    74     in the label field of ComboBox.
       
    75     Touching the comboBox label field or button area of the comboBox will opens
       
    76     the drop-down list to select an item from list of options.
       
    77 
       
    78     By Default comboBox is Non Editable. 
       
    79 
       
    80     Application is responsible for setting the model to the HbComboBox,
       
    81     If no model is set, the drop down list cannot be displayed at all.
       
    82 
       
    83     HbComboBox owns the model set by the Application.
       
    84 
       
    85     HbComboBox provides three signals:
       
    86 
       
    87     \li currentIndexChanged( index ) is emitted when the current selection in the combobox is
       
    88     changed by user interaction. If application is setting a index/text then this signal is
       
    89     not emmitted. In case of an editable combobox on combobox loosing focus if the current
       
    90     selection has changed then this signal is emitted with the new model index.
       
    91 
       
    92     \li currentIndexChanged( const QString& ) is emitted when the curret selection in the combobox
       
    93     is changed by user interaction. If application is setting a differnet index/text
       
    94     then this signal is not emmitted. 
       
    95     In case of an editable combobox on combobox loosing focus if the current selection
       
    96     has changed then this signal is emitted with the new string.
       
    97 
       
    98     \li editTextChanged( QString& ) is emitted when combobox looses focus and user has typed a text
       
    99     for which there is no matching item found in the model with the text typed by the user.
       
   100 
       
   101     The following is an example of how to create a model and adding item to the created model.
       
   102     How to set the model on the HbcomboBox.
       
   103     Here the model is ownership transfer to the widget.
       
   104     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,37}
       
   105 
       
   106     An example how to add strings into HbComboBox and setting currentIndex.
       
   107     HbComboBox will creates the model internally if model is not created.
       
   108     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,38}
       
   109 
       
   110     An example how to add stringsList into HbComboBox and setting currentIndex.
       
   111     HbComboBox will creates the model internally if model is not created.
       
   112     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,39}
       
   113 
       
   114     An example how to insert String at index into HbComboBox.
       
   115     HbComboBox will creates the model internally if model is not created.
       
   116     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,40}
       
   117 
       
   118     An example how to insert StringList at index into HbComboBox.
       
   119     HbComboBox will creates the model internally if model is not created.
       
   120     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,41}
       
   121 
       
   122     An example how to set the items into HbComboBox.
       
   123     HbComboBox will replces the existing model.
       
   124     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,42}
       
   125 
       
   126 */
       
   127 
       
   128 
       
   129 /*!
       
   130     Constructs a new HbComboBox with \a parent.
       
   131  */
       
   132 HbComboBox::HbComboBox( QGraphicsItem *parent ):
       
   133     HbWidget( *new HbComboBoxPrivate, parent )
       
   134 {
       
   135     Q_D( HbComboBox );
       
   136     d->init( );
       
   137     updatePrimitives( );
       
   138     setProperty("state", "normal");
       
   139 }
       
   140 
       
   141 /*!
       
   142     Destructor.
       
   143  */
       
   144 HbComboBox::~HbComboBox( )
       
   145 {
       
   146 }
       
   147 
       
   148 /*!
       
   149     \internal[protected][Access Privilage]
       
   150  */
       
   151 HbComboBox::HbComboBox( HbComboBoxPrivate &dd, QGraphicsItem *parent ) :
       
   152     HbWidget( dd, parent )
       
   153 {
       
   154     Q_D( HbComboBox );
       
   155     d->init( );
       
   156     updatePrimitives( );
       
   157     setProperty("state", "normal");
       
   158 }
       
   159 
       
   160 /*!
       
   161     @beta
       
   162     \property HbComboBox::items
       
   163     \brief list of comboBox items
       
   164     It replaces the existing list.
       
   165 */
       
   166 void HbComboBox::setItems( const QStringList &texts )
       
   167 {  
       
   168     if ( texts.isEmpty( ) ) {
       
   169         return;
       
   170     }
       
   171     QStandardItemModel* model = new QStandardItemModel( this );
       
   172     int textCount = texts.count( );
       
   173     for( int i = 0; i < textCount; i++ )
       
   174     {
       
   175         QStandardItem* standardItem = new QStandardItem( );
       
   176         standardItem->setData( texts.at( i ),Qt::DisplayRole );
       
   177         model->appendRow( standardItem );
       
   178     }
       
   179    setModel( model );
       
   180 }
       
   181 
       
   182 /*!
       
   183     @beta
       
   184     This property holds the list added in the combobox.
       
   185     By default, for an empty combo box, this property has a empty StringList. 
       
   186  */
       
   187 QStringList HbComboBox::items( ) const
       
   188 {
       
   189     Q_D( const HbComboBox );
       
   190     if( d->mModel && d->mModel->rowCount( )) {
       
   191         QStringList list;
       
   192         int rowCount = d->mModel->rowCount();
       
   193         for(int i = 0; i < rowCount; i++)
       
   194         {
       
   195             list<<( d->mModel->data( d->mModel->index( i, 0 ) ) ).toString();
       
   196         }
       
   197         return list;
       
   198     } else {
       
   199       return QStringList();
       
   200     }
       
   201 }
       
   202 
       
   203 /*!
       
   204     @alpha
       
   205     Sets the \a icon for the item on the given \a index in the combobox.
       
   206     this API will not work if applcation sets the model as QStringlistModel.
       
   207 */
       
   208 void HbComboBox::setItemIcon( int index, const HbIcon &icon )
       
   209 {
       
   210     Q_D(const HbComboBox);
       
   211     if(d->mModel) {
       
   212         QModelIndex item = d->mModel->index( index, 0 );
       
   213         if ( item.isValid( ) ) {        
       
   214             d->mModel->setData( item, icon.qicon(), Qt::DecorationRole );
       
   215         }
       
   216     }
       
   217 }
       
   218 
       
   219 /*!
       
   220     @alpha
       
   221     returns the HbIcon for the given \a index in the combobox.
       
   222 */
       
   223 HbIcon HbComboBox::itemIcon( int index ) const
       
   224 {
       
   225     Q_D( const HbComboBox );
       
   226     if( d->mModel ) {
       
   227         QModelIndex mi = d->mModel->index( index, 0 );
       
   228         if( mi.isValid ( ) ) {
       
   229             return d->itemIcon( mi );
       
   230         } 
       
   231     }
       
   232     return HbIcon( );
       
   233 }
       
   234 
       
   235 /*!
       
   236     @beta
       
   237     Returns the data for the given \a role in the given \a index in the
       
   238     combobox, or QVariant::Invalid if there is no data for this role.
       
   239 */
       
   240 QVariant HbComboBox::itemData( int index, int role ) const
       
   241 {
       
   242     Q_D( const HbComboBox );
       
   243     if( d->mModel ) {
       
   244         QModelIndex mi = d->mModel->index( index, 0 );
       
   245         if( mi.isValid ( ) ) {
       
   246             return d->mModel->data( mi, role );
       
   247         } 
       
   248     }
       
   249     return QVariant( );
       
   250 }
       
   251 
       
   252 /*!
       
   253     @beta
       
   254     Sets the data \a role for the item on the given \a index in the combobox
       
   255     to the specified \a value.
       
   256 */
       
   257 void HbComboBox::setItemData( int index, const QVariant &value, int role )
       
   258 {
       
   259     Q_D( const HbComboBox );
       
   260     if( d->mModel ) {
       
   261         QModelIndex item = d->mModel->index( index, 0 );
       
   262         if ( item.isValid( ) ) {
       
   263             d->mModel->setData( item, value, role );
       
   264         }
       
   265     }
       
   266 }
       
   267 
       
   268 /*!
       
   269     @proto
       
   270     This case is valid only for Editable comboBox
       
   271     Returns the validator that is used to constraint text input to the
       
   272     combobox and returns NULL if it is invalid.
       
   273 
       
   274     \sa editable
       
   275 */
       
   276 const HbValidator *HbComboBox::validator() const
       
   277 {
       
   278     Q_D( const HbComboBox );
       
   279     if( d->mEditable) {
       
   280         return d->mLineEdit->validator( );
       
   281     }
       
   282     return NULL;
       
   283 }
       
   284 
       
   285 /*!
       
   286     @proto
       
   287     This case is only valid for the Editable comboBox.
       
   288     Sets the \a validator to use instead of the current validator.
       
   289 */
       
   290 void HbComboBox::setValidator( HbValidator *validator )
       
   291 {
       
   292     Q_D( HbComboBox );
       
   293     if( d->mEditable ) {
       
   294         disconnect( d->mLineEdit, SIGNAL( textChanged ( QString ) ), 
       
   295             this, SLOT( _q_textChanged( QString ) ) );
       
   296         d->mLineEdit->setValidator( validator );
       
   297         connect( d->mLineEdit, SIGNAL( textChanged ( QString ) ), 
       
   298             this, SLOT( _q_textChanged( QString ) ) );
       
   299     }
       
   300 }
       
   301 
       
   302 /*!
       
   303     This property holds the number of items in the combobox.
       
   304     By default, for an empty combo box, this property has a value of 0.
       
   305  */
       
   306 int HbComboBox::count( ) const
       
   307 {
       
   308     Q_D( const HbComboBox );
       
   309     if( d->mModel ) {
       
   310         return d->mModel->rowCount( );
       
   311     }
       
   312     return 0;
       
   313 }
       
   314 
       
   315 /*!
       
   316     \deprecated HbComboBox::insertPolicy() const
       
   317     is deprecated.
       
   318     \sa setInsertPolicy
       
   319 */
       
   320 HbComboBox::InsertPolicy HbComboBox::insertPolicy( ) const
       
   321 {
       
   322     Q_D( const HbComboBox );
       
   323     qDebug() << "this is deprecated and will cease to exist in the near future.";
       
   324     return d->insertPolicy;
       
   325 }
       
   326 
       
   327 /*!
       
   328     \deprecated HbComboBox::setInsertPolicy(HbComboBox::InsertPolicy)
       
   329     is deprecated.
       
   330     \sa insertPolicy
       
   331 */
       
   332 void HbComboBox::setInsertPolicy( InsertPolicy policy )
       
   333 {
       
   334     Q_D( HbComboBox );
       
   335     qDebug() << "this is deprecated and will cease to exist in the near future.";
       
   336     d->insertPolicy = policy;
       
   337 }
       
   338 
       
   339 /*!
       
   340     @beta
       
   341     Sets the model to \a model 
       
   342     comboBox Owns the model set by the Application.
       
   343     It replaces the old model if exists.
       
   344     please do not pass 0(NULL) as \a model instead call clear() to clear the contens of the model.
       
   345  */
       
   346 void HbComboBox::setModel( QAbstractItemModel * model )
       
   347 {
       
   348     Q_D( HbComboBox );
       
   349     if ( d->mModel != model ) {
       
   350         if( model ) {
       
   351             d->setModel( model );
       
   352         } else {
       
   353             clear( );
       
   354         }
       
   355     }
       
   356 }
       
   357 
       
   358 /*!
       
   359     @beta
       
   360     Returns model that view is currently presenting.
       
   361  */
       
   362 QAbstractItemModel* HbComboBox::model( ) const
       
   363 {
       
   364     Q_D( const HbComboBox );
       
   365     return d->mModel;
       
   366 }
       
   367 
       
   368 /*!
       
   369     @beta
       
   370     Sets current index to \a index.
       
   371     By default no item is selected.
       
   372  */
       
   373 void HbComboBox::setCurrentIndex( int index )
       
   374 {
       
   375     Q_D( HbComboBox );
       
   376     if( d->mModel ) {
       
   377         QModelIndex mi = d->mModel->index( index, 0 );
       
   378         if( mi.isValid( ) ) {
       
   379             d->setCurrentIndex( mi );
       
   380         }
       
   381     }
       
   382 }
       
   383 
       
   384 /*!
       
   385     @beta
       
   386     Returns index of current item and returns -1 for invalid current index.
       
   387  */
       
   388 int HbComboBox::currentIndex( ) const
       
   389 {
       
   390     Q_D( const HbComboBox );
       
   391     return d->mCurrentIndex.row( );
       
   392 }
       
   393 
       
   394 /*!
       
   395     @beta
       
   396     \fn int HbComboBox::findText(const QString &text, Qt::MatchFlags 
       
   397                                       flags = Qt::MatchExactly|Qt::MatchCaseSensitive) const
       
   398 
       
   399     Returns the index of the item containing the given \a text; otherwise
       
   400     returns -1.
       
   401 
       
   402     The \a flags specify how the items in the combobox are searched.
       
   403 */
       
   404 
       
   405 /*!
       
   406     Returns the index of the item containing the given \a data for the
       
   407     given \a role; otherwise returns -1.
       
   408 
       
   409     The \a flags specify how the items in the combobox are searched.
       
   410 */
       
   411 int HbComboBox::findData( const QVariant &data, int role, Qt::MatchFlags flags ) const
       
   412 {
       
   413     Q_D( const HbComboBox );
       
   414     if(d->mModel) {
       
   415         QModelIndexList result;
       
   416         QModelIndex start = d->mModel->index( 0, 0 );
       
   417         result = d->mModel->match( start, role, data, 1, flags );
       
   418         if ( result.isEmpty( ) )
       
   419             return -1;
       
   420         return result.first( ).row( );
       
   421     }
       
   422     return -1;
       
   423 }
       
   424 
       
   425 /*!
       
   426     @beta
       
   427     This is specific to the Editable comboBox only.
       
   428     Sets the text in the combobox's text edit 
       
   429  */
       
   430 void HbComboBox::setEditText( const QString &text )
       
   431 {
       
   432     Q_D( HbComboBox );
       
   433     if( d->mEditable ) {
       
   434         disconnect( d->mLineEdit, SIGNAL( textChanged ( QString ) ), 
       
   435             this, SLOT( _q_textChanged( QString ) ) );
       
   436         d->mLineEdit->setText( text );
       
   437         connect( d->mLineEdit, SIGNAL( textChanged ( QString ) ), 
       
   438             this, SLOT( _q_textChanged( QString ) ) );
       
   439     }
       
   440 }
       
   441 
       
   442 /*!
       
   443     @beta
       
   444     \property HbComboBox::currentText
       
   445     \brief the text of the current item
       
   446      combo box with model count \0 returns an empty string.
       
   447 */
       
   448 QString HbComboBox::currentText( ) const
       
   449 {
       
   450     Q_D( const HbComboBox );
       
   451     if( d->mEditable ) {
       
   452         return d->mLineEdit->text( );
       
   453     }else if( d->mCurrentIndex.isValid( ) ) {
       
   454         return itemText ( d->mCurrentIndex.row( ) );
       
   455     } else {
       
   456         return QString( );
       
   457     }
       
   458 }
       
   459 
       
   460 /*!
       
   461     @beta
       
   462     Removes the item at the given index from the combobox.
       
   463     This will update the current index if the index is removed. 
       
   464  */
       
   465 void HbComboBox::removeItem( int index )
       
   466 {
       
   467     Q_D( HbComboBox );
       
   468     if( d->mModel ) {
       
   469         int rowCount = d->mModel->rowCount( );
       
   470         if( index >=0 && index < rowCount ) {
       
   471             bool currentText = false;
       
   472             if ( d->mModel->index(index, 0) == d->mCurrentIndex ) {
       
   473                 currentText = true;
       
   474                 d->mModel->removeRow( index );
       
   475             } else if( d->mCurrentIndex.row() > index ) {
       
   476                  int row = d->mCurrentIndex.row();
       
   477                  d->mModel->removeRow( index );
       
   478                  d->mCurrentIndex = d->mModel->index( --row, 0 );
       
   479                  d->currentIndexChanged( d->mCurrentIndex );
       
   480             } else {
       
   481                 d->mModel->removeRow( index );
       
   482             }
       
   483             if( d->mModel->rowCount( ) == 0 ) {
       
   484                 if( d->mEditable ) {
       
   485                     clearEditText();
       
   486                 } else {
       
   487                     if( d->mLineEdit ) {
       
   488                         d->mLineEdit->setText( QString() );
       
   489                     } else {
       
   490                         d->mText = QString( );
       
   491                         HbStyleOptionComboBox comboBoxOption;
       
   492                         initStyleOption(&comboBoxOption);
       
   493                         style()->updatePrimitive( d->mTextItem, HbStyle::P_ComboBox_text, &comboBoxOption);
       
   494                     }
       
   495                 }
       
   496                 return;
       
   497             }
       
   498             if( currentText ) {
       
   499                 d->mCurrentIndex = d->mModel->index( 0, 0 );
       
   500                 if( d->mEditable ) {
       
   501                     disconnect(d->mLineEdit, SIGNAL( textChanged ( QString ) ),
       
   502                                  this, SLOT( _q_textChanged( QString ) ) );
       
   503                     d->mLineEdit->setText( d->mModel->data( d->mCurrentIndex ).toString( ) );
       
   504                     connect(d->mLineEdit, SIGNAL( textChanged ( QString ) ), 
       
   505                                  this, SLOT( _q_textChanged( QString ) ) );
       
   506                 } else {
       
   507                     if( d->mLineEdit ) {
       
   508                         d->mLineEdit->setText( QString() );
       
   509                     } else {
       
   510                         d->mText = d->mModel->data( d->mCurrentIndex ).toString( );
       
   511                         HbStyleOptionComboBox comboBoxOption;
       
   512                         initStyleOption(&comboBoxOption);
       
   513                         style()->updatePrimitive( d->mTextItem, HbStyle::P_ComboBox_text, &comboBoxOption);
       
   514                     }
       
   515                 }
       
   516                 d->currentIndexChanged( d->mCurrentIndex );
       
   517             }
       
   518         }
       
   519     }
       
   520 }
       
   521 
       
   522 /*!
       
   523     \reimp
       
   524  */
       
   525 void HbComboBox::resizeEvent( QGraphicsSceneResizeEvent *event )
       
   526 {
       
   527     Q_UNUSED( event )
       
   528     Q_D( HbComboBox );
       
   529     if ( d->mDropDown && d->mDropDown->isVisible() ){
       
   530         d->positionDropDown( );
       
   531     }
       
   532 }
       
   533 
       
   534 /*!
       
   535     \reimp
       
   536 */
       
   537 void HbComboBox::keyPressEvent( QKeyEvent *event )
       
   538 {
       
   539     Q_D( HbComboBox );
       
   540     if ( !event->isAutoRepeat( ) ) {
       
   541         if ( event->key( ) == Qt::Key_Enter || event->key( ) == Qt::Key_Return ) {
       
   542             d->mIsDown = true;
       
   543             updatePrimitives( );
       
   544             event->accept( );
       
   545         }
       
   546     } else {
       
   547         event->accept( );
       
   548     }
       
   549 }
       
   550 
       
   551 /*!
       
   552     \reimp
       
   553 */
       
   554 void HbComboBox::keyReleaseEvent( QKeyEvent *event )
       
   555 {
       
   556     Q_D( HbComboBox );
       
   557     if ( !event->isAutoRepeat( ) ){
       
   558         if ( event->key( ) == Qt::Key_Enter || event->key( ) == Qt::Key_Return ){
       
   559             d->mIsDown = false;
       
   560             updatePrimitives( );
       
   561             event->accept( );
       
   562             d->touchAreaClicked( );
       
   563             d->mDropDown->mList->setFocus( );
       
   564         }
       
   565     } else {
       
   566         event->accept( );
       
   567     }
       
   568 }
       
   569 
       
   570 /*!
       
   571     \reimp
       
   572  */
       
   573 QGraphicsItem* HbComboBox::primitive( HbStyle::Primitive primitive ) const
       
   574 {
       
   575     Q_D( const HbComboBox );
       
   576 
       
   577     switch( primitive ){
       
   578         case HbStyle::P_ComboBox_background:
       
   579             return d->mBackgroundItem;
       
   580         case HbStyle::P_ComboBoxButton_toucharea:
       
   581             return d->mButtonTouchAreaItem;
       
   582         default:
       
   583             return 0;
       
   584     }
       
   585 }
       
   586 
       
   587 void HbComboBox::initStyleOption( HbStyleOptionComboBox *option )const
       
   588 {
       
   589     Q_D( const HbComboBox );    
       
   590     option->text = d->mText;
       
   591     HbWidget::initStyleOption( option );  
       
   592 }
       
   593 
       
   594 /*!
       
   595     \reimp
       
   596  */
       
   597 void HbComboBox::updatePrimitives( )
       
   598 {
       
   599     Q_D( HbComboBox );    
       
   600     HbStyleOption styleOption;
       
   601     HbWidget::initStyleOption( &styleOption );
       
   602     if ( d->mIsDown ) {
       
   603         styleOption.state |= QStyle::State_Sunken;
       
   604     }
       
   605     if ( d->mBackgroundItem ) {
       
   606         style( )->updatePrimitive( 
       
   607             d->mBackgroundItem, HbStyle::P_ComboBox_background, &styleOption );
       
   608         style( )->updatePrimitive( 
       
   609             d->mButton, HbStyle::P_ComboBox_button, &styleOption );
       
   610     }
       
   611 }
       
   612 
       
   613 /*!
       
   614     @beta
       
   615     Clears the combobox, removes all items from model. 
       
   616  */
       
   617 void HbComboBox::clear( )
       
   618 {
       
   619     Q_D( HbComboBox );
       
   620     if( d->mModel ) {
       
   621         d->mModel->removeRows(0, d->mModel->rowCount());
       
   622         d->mCurrentIndex = QModelIndex( );
       
   623         if( d->mEditable ) {
       
   624             clearEditText();
       
   625         } else {
       
   626             if( d->mLineEdit ) {
       
   627                 d->mLineEdit->setText( QString() );
       
   628             } else {
       
   629                 d->mText = QString( );
       
   630                 HbStyleOptionComboBox comboBoxOption;
       
   631                 initStyleOption(&comboBoxOption);
       
   632                 style()->updatePrimitive( d->mTextItem, HbStyle::P_ComboBox_text, &comboBoxOption);
       
   633             }
       
   634         }
       
   635     }
       
   636 }
       
   637 
       
   638 /*!
       
   639     @beta
       
   640     Clears the contents of the line edit used for editing in the combobox.
       
   641  */
       
   642 void HbComboBox::clearEditText( )
       
   643 {
       
   644     Q_D( HbComboBox );
       
   645     if( d->mEditable ) {
       
   646         disconnect( d->mLineEdit, SIGNAL( textChanged ( QString ) ),
       
   647             this, SLOT( _q_textChanged( QString ) ) );
       
   648         d->mLineEdit->setText( QString( ) );
       
   649         connect( d->mLineEdit, SIGNAL( textChanged ( QString ) ),
       
   650             this, SLOT( _q_textChanged( QString ) ) );
       
   651     }
       
   652 }
       
   653 
       
   654 /*!
       
   655     @beta
       
   656     \property HbComboBox::editable
       
   657     \brief Set editable the property of the combobox.
       
   658     True, Editable, user can type in the combobox to search for items from 
       
   659     the list of items. Shows the prediction. If user types a text which does not match
       
   660     to any items in the model then slection is not changed.
       
   661     False, Non editable user canot enter text using keyboard.
       
   662  */
       
   663 void HbComboBox::setEditable( bool editable )
       
   664 {
       
   665     Q_D( HbComboBox ); 
       
   666     d->setEditable( editable );
       
   667     setProperty("state", "normal");
       
   668 }
       
   669 
       
   670 /*!
       
   671     @beta
       
   672     Returns whether or not the combobox is editable
       
   673     True if editable, else false
       
   674  */
       
   675 bool HbComboBox::isEditable( ) const
       
   676 {
       
   677     Q_D( const HbComboBox );
       
   678     return d->mEditable;
       
   679 }
       
   680 
       
   681 /*!
       
   682     @beta
       
   683     Adds an item to the combobox with the given text,
       
   684     and containing the specified userData (stored in the Qt::UserRole).
       
   685     The item is appended to the list of existing items.
       
   686  */
       
   687 void HbComboBox::addItem( const QString &text, const QVariant &userData)
       
   688 {
       
   689     insertItem( count( ), text, userData );
       
   690 }
       
   691 
       
   692 /*!
       
   693     @beta
       
   694     Adds an item to the combobox with the given icon and text,
       
   695     and containing the specified userData (stored in the Qt::UserRole). 
       
   696     The item is appended to the list of existing items.
       
   697  */
       
   698 void HbComboBox::addItem( const HbIcon &icon, const QString &text, const QVariant &userData )
       
   699 {
       
   700     insertItem( count( ), icon, text, userData );
       
   701 }
       
   702 
       
   703 /*!
       
   704     @beta
       
   705     Adds each of the strings in the given texts to the combobox.
       
   706     Each item is appended to the list of existing items in turn.
       
   707  */
       
   708 void HbComboBox::addItems( const QStringList &texts )
       
   709 {
       
   710     insertItems( count(), texts );
       
   711 }
       
   712 
       
   713 
       
   714 /*!
       
   715     @beta
       
   716     Inserts the text into the combobox at the given index.
       
   717     If the index is equal to or higher than the total number of items, 
       
   718     the new item is appended to the list of existing items.
       
   719     If the index is zero or negative, the new item is prepended to the list of existing items.
       
   720  */
       
   721 void HbComboBox::insertItem( int index, const QString &text, const QVariant &userData )
       
   722 {
       
   723     Q_D( HbComboBox );
       
   724     if( text.isNull( ) ){
       
   725         return;
       
   726     }
       
   727     if( !d->mModel ) {
       
   728         QStandardItemModel* model = new QStandardItemModel(this );
       
   729         setModel( model );
       
   730     }
       
   731     if (  !d->mModel->rowCount( ) ) {
       
   732         d->mModel->insertRow( 0 );
       
   733         d->mModel->insertColumn( 0 );
       
   734         if( d->mModel->index( 0, 0 ).isValid( ) ) {
       
   735             d->mModel->setData( d->mModel->index( 0, 0 ), text, Qt::DisplayRole );
       
   736             if( userData.isValid( ) ) {
       
   737                 d->mModel->setData( d->mModel->index( 0, 0 ), userData, Qt::UserRole );
       
   738             }
       
   739             setCurrentIndex( 0 );
       
   740         }
       
   741     } else {
       
   742         int rowCount = d->mModel->rowCount( );
       
   743         if( index >= rowCount ) {
       
   744             d->mModel->insertRow( rowCount );
       
   745             d->mModel->setData( d->mModel->index( rowCount, 0 ), text, Qt::DisplayRole );
       
   746             if( userData.isValid( ) ) {
       
   747                 d->mModel->setData( d->mModel->index( rowCount, 0 ), userData, Qt::UserRole );
       
   748             }
       
   749         } else if(index <= 0) {
       
   750             d->mModel->insertRow( 0 );
       
   751             d->mModel->setData( d->mModel->index(0,0), text, Qt::DisplayRole );
       
   752             if( userData.isValid( ) ) {
       
   753                 d->mModel->setData( d->mModel->index( 0, 0 ), userData, Qt::UserRole );
       
   754             }
       
   755             if( d->mCurrentIndex.row() >= 0 ) {
       
   756                 d->mCurrentIndex = d->mModel->index(d->mCurrentIndex.row()+1, 0);
       
   757                 d->currentIndexChanged(d->mCurrentIndex);
       
   758             }
       
   759         } else {
       
   760            d->mModel->insertRow( index );
       
   761            d->mModel->setData( d->mModel->index( index, 0 ), text, Qt::DisplayRole );
       
   762            if( userData.isValid( ) ) {
       
   763                 d->mModel->setData( d->mModel->index( index, 0 ), userData, Qt::UserRole );                
       
   764             }
       
   765            if( d->mCurrentIndex.row() <= index ) {
       
   766                 d->mCurrentIndex = d->mModel->index(d->mCurrentIndex.row()+1, 0);
       
   767                 d->currentIndexChanged(d->mCurrentIndex);
       
   768             }
       
   769         }
       
   770     }
       
   771 }
       
   772 
       
   773 /*!
       
   774     @beta
       
   775     Inserts the text and icon into the combobox at the given index.
       
   776     If the index is equal to or higher than the total number of items, 
       
   777     the new item is appended to the list of existing items.
       
   778     If the index is zero or negative, the new item is prepended to the list of existing items.
       
   779  */
       
   780 void HbComboBox::insertItem( int index, const HbIcon &icon, const QString &text, const QVariant &userData)
       
   781 {
       
   782     Q_D( HbComboBox );
       
   783     if( text.isEmpty( ) ){
       
   784         return;
       
   785     }
       
   786 
       
   787     if( !d->mModel ) {
       
   788         QStandardItemModel* model = new QStandardItemModel( this );
       
   789         setModel( model );
       
   790     }
       
   791     if (  !d->mModel->rowCount( ) ) {
       
   792         d->mModel->insertRow( 0 );
       
   793         d->mModel->insertColumn( 0 );
       
   794         if( d->mModel->index( 0, 0 ).isValid( ) ) {
       
   795             d->mModel->setData( d->mModel->index(0,0), text, Qt::DisplayRole );
       
   796             setCurrentIndex( 0 );
       
   797         }
       
   798         if(!icon.isNull() && d->mModel->index( 0, 0 ).isValid( ) ) {
       
   799             d->mModel->setData(d->mModel->index( 0, 0 ), icon.qicon( ), Qt::DecorationRole);
       
   800         }
       
   801         if( userData.isValid( ) && d->mModel->index( 0, 0 ).isValid( ) ) {
       
   802             d->mModel->setData( d->mModel->index( 0, 0 ), userData, Qt::UserRole );                
       
   803         }
       
   804     } else {
       
   805         int rowCount = d->mModel->rowCount( );
       
   806         if( index >= rowCount ) {
       
   807             d->mModel->insertRow( rowCount );
       
   808             d->mModel->setData( d->mModel->index( rowCount, 0 ), text, Qt::DisplayRole );
       
   809             if(!icon.isNull()) {
       
   810                 d->mModel->setData( d->mModel->index( rowCount, 0 ), icon, Qt::DecorationRole );
       
   811             }
       
   812             if( userData.isValid( ) ) {
       
   813                 d->mModel->setData( d->mModel->index( rowCount, 0 ), userData, Qt::UserRole );
       
   814             }
       
   815             if( d->mCurrentIndex.row() == index ) {
       
   816                 d->mCurrentIndex = d->mModel->index( d->mCurrentIndex.row( ) + 1, 0 );
       
   817                 d->currentIndexChanged( d->mCurrentIndex );
       
   818             }
       
   819         } else if(index <= 0) {
       
   820             d->mModel->insertRow( 0 );
       
   821             d->mModel->setData( d->mModel->index( 0, 0 ), text, Qt::DisplayRole );
       
   822             if(!icon.isNull()) {
       
   823                 d->mModel->setData(d->mModel->index( 0, 0 ), icon, Qt::DecorationRole);
       
   824             }
       
   825             if( userData.isValid( ) ) {
       
   826                 d->mModel->setData( d->mModel->index( 0, 0 ), userData, Qt::UserRole );
       
   827             }
       
   828             if( d->mCurrentIndex.row() >= 0 ) {
       
   829                 d->mCurrentIndex = d->mModel->index( d->mCurrentIndex.row( ) + 1, 0);
       
   830                 d->currentIndexChanged( d->mCurrentIndex );
       
   831             }
       
   832         } else {
       
   833             d->mModel->insertRow( index );
       
   834             d->mModel->setData( d->mModel->index( index, 0 ), text, Qt::DisplayRole );
       
   835             if(!icon.isNull( ) ) {
       
   836                 d->mModel->setData( d->mModel->index( index, 0 ), icon, Qt::DecorationRole );
       
   837             }
       
   838             if( userData.isValid( ) ) {
       
   839                 d->mModel->setData( d->mModel->index( index, 0 ), userData, Qt::UserRole );
       
   840             }
       
   841             if( d->mCurrentIndex.row() <= index ) {
       
   842                 d->mCurrentIndex = d->mModel->index( d->mCurrentIndex.row( ) + 1, 0);
       
   843                 d->currentIndexChanged( d->mCurrentIndex );
       
   844             }
       
   845         }
       
   846     }
       
   847 }
       
   848 
       
   849 /*!
       
   850     @beta
       
   851     Inserts the strings from the list into the combobox as separate items, starting at the index.
       
   852     If the index is equal to or higher than the total number of items, 
       
   853     the new item is appended to the list of existing items.
       
   854     If the index is zero or negative, the new item is prepended to the list of existing items.
       
   855  */
       
   856 void HbComboBox::insertItems( int index, const QStringList &texts )
       
   857 {
       
   858     Q_D( HbComboBox );
       
   859     if( !d->mModel ) {
       
   860         QStandardItemModel* model = new QStandardItemModel( this );
       
   861         setModel( model );
       
   862     }
       
   863     if ( !d->mModel->rowCount( ) ) {
       
   864         int textCount = texts.count( );
       
   865         for( int i = 0; i < textCount; i++)
       
   866         {
       
   867             d->mModel->insertRow( i );
       
   868             if( i == 0)
       
   869                 d->mModel->insertColumn( 0 );
       
   870             if( d->mModel->index( i, 0 ).isValid( ) ) {
       
   871                 d->mModel->setData( d->mModel->index( i, 0 ),  texts.at( i ), Qt::DisplayRole );
       
   872                 if( i == 0) {
       
   873                     setCurrentIndex( 0 );
       
   874                 }
       
   875             }
       
   876         }
       
   877     }  else {
       
   878         int rowCount = -1;
       
   879         rowCount = d->mModel->rowCount( );
       
   880         int textCount = texts.count( );
       
   881         if ( index >= rowCount ) {
       
   882             d->mModel->insertRows( rowCount, textCount );
       
   883             int temp = 0;
       
   884             for ( int i = rowCount; i < ( rowCount + textCount ); i++ ) {
       
   885                 d->mModel->setData ( d->mModel->index( i, 0 ), texts.at( temp++ ) );
       
   886             }
       
   887             if( d->mCurrentIndex.row() == index ) {
       
   888                 d->mCurrentIndex = d->mModel->index( d->mCurrentIndex.row( ) + textCount, 0);
       
   889                 d->currentIndexChanged( d->mCurrentIndex );
       
   890             }
       
   891         } else if( index <= 0 ) {
       
   892             d->mModel->insertRows( 0, textCount );
       
   893             for ( int i = 0; i < textCount; i++ ) {
       
   894                 d->mModel->setData( d->mModel->index( i, 0 ), texts.at( i ) );
       
   895             }
       
   896             if( d->mCurrentIndex.row() >= 0 ) {
       
   897                 d->mCurrentIndex = d->mModel->index(d->mCurrentIndex.row() + textCount, 0);
       
   898                 d->currentIndexChanged( d->mCurrentIndex );
       
   899             }
       
   900         } else {
       
   901             d->mModel->insertRows( index, texts.count( ) );
       
   902             int temp = 0;
       
   903             for ( int i = index; i < ( textCount + index ); i++ ) {
       
   904                 d->mModel->setData( d->mModel->index( i, 0 ), texts.at( temp++ ) );
       
   905             }
       
   906             if( d->mCurrentIndex.row() <= index ) {
       
   907                 d->mCurrentIndex = d->mModel->index(d->mCurrentIndex.row() + textCount, 0);
       
   908                 d->currentIndexChanged( d->mCurrentIndex );
       
   909             }
       
   910         }
       
   911     }
       
   912 }
       
   913 
       
   914 /*!
       
   915     @beta
       
   916     Returns the text for the given index in the combobox.
       
   917  */
       
   918 QString HbComboBox::itemText( int index ) const
       
   919 {
       
   920     Q_D( const HbComboBox );
       
   921     if(d->mModel) {
       
   922         QModelIndex mi = d->mModel->index( index, 0 );
       
   923         if( mi.isValid() ) {
       
   924             return d->itemText( mi );
       
   925         }
       
   926      }
       
   927      return QString( );
       
   928 }
       
   929 
       
   930 /*!
       
   931     @beta
       
   932     Sets the text for the item on the given index in the combobox.
       
   933  */
       
   934 void HbComboBox::setItemText( int index, const QString &text )
       
   935 {
       
   936     Q_D( HbComboBox );
       
   937     if (d->mModel) {
       
   938         QModelIndex item = d->mModel->index( index, 0 );
       
   939         if ( item.isValid( ) ) {
       
   940             if(d->mModel->setData( item, text, Qt::EditRole )) {
       
   941                 if(d->mCurrentIndex.row() == index) {
       
   942                     if( d->mLineEdit ) {
       
   943                         d->mLineEdit->setText( text );
       
   944                     } else {                    
       
   945                         d->mText = text ;
       
   946                         HbStyleOptionComboBox comboBoxOption;
       
   947                         initStyleOption(&comboBoxOption);
       
   948                         style()->updatePrimitive( d->mTextItem, HbStyle::P_ComboBox_text, &comboBoxOption);
       
   949                     }
       
   950                 }
       
   951             }
       
   952             
       
   953         }
       
   954     }
       
   955 }
       
   956 
       
   957 /*!
       
   958     reimplementation. 
       
   959 */
       
   960 bool HbComboBox::eventFilter( QObject *obj, QEvent *event )
       
   961 {
       
   962     Q_D( HbComboBox );
       
   963     bool accepted = false;  
       
   964     if(obj == static_cast<HbTouchArea*>(d->mButtonTouchAreaItem)) {
       
   965         if( (event->type() == QEvent::GraphicsSceneMousePress ) || 
       
   966                 (event->type() == QEvent::GraphicsSceneMouseDoubleClick ) ) {
       
   967             if( static_cast<HbTouchArea*>(d->mButtonTouchAreaItem)->rect( ).contains( 
       
   968                         static_cast<QGraphicsSceneMouseEvent *>( event )->pos( ) ) ){
       
   969                 d->touchAreaPressEvent( );
       
   970                 accepted = true;
       
   971             }
       
   972         }else if( event->type() == QEvent::GraphicsSceneMouseRelease ) {
       
   973              if( static_cast<HbTouchArea*>(d->mButtonTouchAreaItem)->rect( ).contains(
       
   974                         static_cast<QGraphicsSceneMouseEvent *>( event )->pos( ) ) ){
       
   975                     d->touchAreaReleaseEvent( );
       
   976                     accepted = true;
       
   977              } else if (d->mIsDown) {
       
   978                  d->mIsDown = false;
       
   979                  updatePrimitives( );
       
   980              }
       
   981         }
       
   982     }
       
   983     return accepted;
       
   984 }
       
   985 
       
   986 /*!
       
   987     \reimp
       
   988  */
       
   989 void HbComboBox::changeEvent( QEvent *event )
       
   990 {
       
   991     switch ( event->type( ) ){
       
   992         case QEvent::EnabledChange:
       
   993             updatePrimitives( );
       
   994             break;
       
   995         default:
       
   996             break;
       
   997     }
       
   998     HbWidget::changeEvent( event );
       
   999 }
       
  1000 
       
  1001 // End of file
       
  1002 
       
  1003