src/hbinput/inputwidgets/hbinputcheckboxlist.cpp
changeset 1 f7ac710697a9
child 5 627c4a0fd0e7
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
       
     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 HbInput 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 <hblistwidget.h>
       
    27 #include <hblistwidgetitem.h>
       
    28 #include <hbdataformmodelitem.h>
       
    29 #include <hbdataformmodel.h>
       
    30 
       
    31 #include "hbinputcheckboxlist_p.h"
       
    32 
       
    33 /// @cond
       
    34 
       
    35 class HbInputCheckBoxListPrivate
       
    36 {
       
    37 public:
       
    38     HbListWidget *mListWidget;
       
    39 };
       
    40 
       
    41 /// @endcond
       
    42 
       
    43 /*!
       
    44 Constructs checkbox list
       
    45 */
       
    46 HbInputCheckBoxList::HbInputCheckBoxList(QGraphicsItem *parent)
       
    47  : HbDataFormViewItem(parent), d_ptr(new HbInputCheckBoxListPrivate())
       
    48 {
       
    49 }
       
    50 
       
    51 /*!
       
    52 Destructs checkbox list
       
    53 */
       
    54 HbInputCheckBoxList::~HbInputCheckBoxList()
       
    55 {
       
    56     delete d_ptr;
       
    57 }
       
    58 
       
    59 /*!
       
    60 Returns a new copy of this object
       
    61 */
       
    62 HbAbstractViewItem* HbInputCheckBoxList::createItem()
       
    63 {
       
    64     return new HbInputCheckBoxList(*this);
       
    65 }
       
    66 
       
    67 /*!
       
    68 \reimp
       
    69 Returns true if \a model index is supported, otherwise returns false.
       
    70  */
       
    71 bool HbInputCheckBoxList::canSetModelIndex(const QModelIndex &index) const
       
    72 {
       
    73     HbDataFormModelItem::DataItemType itemType = 
       
    74         static_cast<HbDataFormModelItem::DataItemType>(index.data(HbDataFormModelItem::ItemTypeRole).toInt());
       
    75 
       
    76     if(itemType == HbDataFormModelItem::CustomItemBase) {
       
    77         return true;
       
    78     } else {
       
    79         return false;
       
    80     }
       
    81 }
       
    82 
       
    83 /*!
       
    84 Updates the selected items to the model
       
    85 */
       
    86 void HbInputCheckBoxList::itemActivated(const QModelIndex &index)
       
    87 {
       
    88     Q_D(HbInputCheckBoxList);
       
    89     QModelIndex itemIndex = modelIndex();
       
    90     HbDataFormModelItem *modelItem = static_cast<HbDataFormModelItem*>(
       
    91         static_cast<HbDataFormModel*>(itemView()->model())->itemFromIndex(itemIndex));
       
    92 
       
    93     QList<QVariant> selectedValues = modelItem->contentWidgetData(QString("selectedItems")).toList();
       
    94     selectedValues.replace(index.row(), !selectedValues.at(index.row()).toBool());
       
    95     modelItem->setContentWidgetData(QString("selectedItems"), selectedValues);
       
    96 
       
    97     HbAbstractViewItem *viewItem = d->mListWidget->viewItem(index.row());
       
    98     if (selectedValues.at(index.row()).toBool()) {
       
    99         d->mListWidget->setCurrentIndex(viewItem->modelIndex(), QItemSelectionModel::Select);
       
   100     } else {
       
   101         d->mListWidget->setCurrentIndex(viewItem->modelIndex(), QItemSelectionModel::Deselect);
       
   102     }
       
   103  }
       
   104 
       
   105 /*!
       
   106 Creates a widget for showing checkbox list
       
   107 */
       
   108 HbWidget* HbInputCheckBoxList::createCustomWidget()
       
   109 {
       
   110     Q_D(HbInputCheckBoxList);
       
   111 
       
   112     QModelIndex itemIndex = modelIndex();
       
   113     HbDataFormModelItem *modelItem = static_cast<HbDataFormModelItem*>(
       
   114         static_cast<HbDataFormModel*>(itemView()->model())->itemFromIndex(itemIndex));
       
   115 
       
   116     d->mListWidget = new HbListWidget();
       
   117     d->mListWidget->setSelectionMode(HbAbstractItemView::MultiSelection);
       
   118     d->mListWidget->contentWidget()->setContentsMargins(10, 10, 10, 10);
       
   119     d->mListWidget->setBackgroundItem(HbStyle::P_DataItem_background);
       
   120     
       
   121     QStringList items = modelItem->contentWidgetData(QString("items")).toStringList();
       
   122     foreach(QString itemName, items) {
       
   123         HbListWidgetItem *item = new HbListWidgetItem();
       
   124         item->setData(QVariant(itemName), Qt::DisplayRole);
       
   125         d->mListWidget->addItem(item);
       
   126     }
       
   127 
       
   128     QList<QVariant> selectedValues = modelItem->contentWidgetData(QString("selectedItems")).toList();
       
   129     for (int i = 0; i < d->mListWidget->count(); ++i) {
       
   130         if (selectedValues.at(i).toBool()) {
       
   131             HbAbstractViewItem *viewItem = d->mListWidget->viewItem(i);
       
   132             d->mListWidget->setCurrentIndex(viewItem->modelIndex(), QItemSelectionModel::Select);
       
   133         }
       
   134     }
       
   135 
       
   136     QString objectName = modelItem->contentWidgetData(QString("objectName")).toString();
       
   137     d->mListWidget->setObjectName(objectName);
       
   138 
       
   139     connect(d->mListWidget, SIGNAL(activated(const QModelIndex &)), this, SLOT(itemActivated(const QModelIndex &)));
       
   140 
       
   141     return d->mListWidget;    
       
   142 }
       
   143 
       
   144 /*!
       
   145 Copy constructor for private use.
       
   146 */
       
   147 HbInputCheckBoxList::HbInputCheckBoxList(const HbInputCheckBoxList &other)
       
   148  : HbDataFormViewItem(other), d_ptr(new HbInputCheckBoxListPrivate())
       
   149 {
       
   150 }
       
   151 
       
   152 // End of file