src/hbwidgets/itemviews/hblistwidgetitem_p.cpp
changeset 0 16d8024aca5e
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 #include "hblistwidgetitem_p.h"
       
    26 #include "hblistmodel_p.h"
       
    27 
       
    28 #include <hblistwidgetitem.h>
       
    29 
       
    30 #include <QVariant>
       
    31 
       
    32 
       
    33 /*
       
    34     HbListWidgetItemPrivate in a internal class, which implements data container 
       
    35     for HblistWidgetItems and HblistWidget. 
       
    36 */
       
    37 HbListWidgetItemPrivate::HbListWidgetItemPrivate(
       
    38         HbListWidgetItem *itemWidget) :
       
    39     mItem(itemWidget),
       
    40     mModel(0),
       
    41     id(0),
       
    42     flags(Qt::ItemIsSelectable|Qt::ItemIsEnabled)
       
    43 {
       
    44 }
       
    45 
       
    46 HbListWidgetItemPrivate::~HbListWidgetItemPrivate()
       
    47 {
       
    48 }
       
    49 
       
    50 void HbListWidgetItemPrivate::setData(const QVariant &value, int role, int index)
       
    51 {
       
    52     QVector<HbWidgetItemData>::iterator it;
       
    53     for (it = mValues.begin(); it != mValues.end(); ++it) {
       
    54         if ((*it).role == role) {
       
    55             QVariantList list;
       
    56             if ((*it).value.canConvert<QVariantList>()) {
       
    57                 list = (*it).value.toList();
       
    58             } else {
       
    59                 list.append((*it).value);
       
    60             }
       
    61              
       
    62             if (index < list.count()) {
       
    63                 // Existing value -> replace if different
       
    64                 if (list.at(index) == value)  
       
    65                     return;
       
    66                 list.replace(index, value);
       
    67             } else {
       
    68                 // New value -> grow the list if needed
       
    69                 while (list.count() < index) {
       
    70                     list.append(QVariant());
       
    71                 }
       
    72 
       
    73                 list.append(value);
       
    74             }
       
    75 
       
    76             (*it).value = list;
       
    77             return;
       
    78         }
       
    79     }
       
    80 
       
    81     // role is not present yet
       
    82     QVariantList list;
       
    83     for (int i = 0; i < index; i++) {
       
    84         list.append(QVariant());
       
    85     }
       
    86     list.append(value);
       
    87     mValues.append(HbWidgetItemData(role, list));
       
    88 }
       
    89 
       
    90 void HbListWidgetItemPrivate::setData(const QVariant &value, int role)
       
    91 {
       
    92     //Note: no more call to itemChanged, caller is responsible for calling it
       
    93     role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
       
    94 
       
    95     QVector<HbWidgetItemData>::iterator it;
       
    96     for (it = mValues.begin(); it != mValues.end(); ++it) {
       
    97         if ((*it).role == role) {
       
    98             (*it).value = value;
       
    99             return;
       
   100         }
       
   101     }
       
   102 
       
   103     mValues.append(HbWidgetItemData(role, value));
       
   104 }
       
   105 
       
   106 
       
   107 QVariant HbListWidgetItemPrivate::data(int role, int index) const
       
   108 {
       
   109     QVariant value = data(role);
       
   110     if (value.canConvert<QVariantList>()) {
       
   111         QVariantList list = value.toList();
       
   112         return list.value(index);
       
   113     } else if (index == 0) {
       
   114         return value;
       
   115     } else {
       
   116         return QVariant();
       
   117     }
       
   118 }
       
   119 
       
   120 /*
       
   121     Returns the item's data for the given \a role, or an invalid
       
   122     QVariant if there is no data for the role.
       
   123     
       
   124     \note The default implementation treats Qt::EditRole and Qt::DisplayRole
       
   125     as referring to the same data.  
       
   126 */
       
   127 QVariant HbListWidgetItemPrivate::data(int role) const
       
   128 {
       
   129     role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
       
   130 
       
   131     QVector<HbWidgetItemData>::const_iterator it;
       
   132     for (it = mValues.begin(); it != mValues.end(); ++it) {
       
   133         if ((*it).role == role) {
       
   134             return (*it).value;
       
   135         }
       
   136     }
       
   137 
       
   138     if (role == Hb::ItemTypeRole) {
       
   139         return Hb::StandardItem;
       
   140     }
       
   141     return QVariant();
       
   142 }
       
   143