src/hbwidgets/itemviews/hblistmodel_p.cpp
changeset 0 16d8024aca5e
child 34 ed14f46c0e55
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 "hblistmodel_p.h"
       
    27 #include "hblistwidgetitem_p.h"
       
    28 
       
    29 #include <hblistwidgetitem.h>
       
    30 
       
    31 HbListModel::HbListModel(QObject *parent) : 
       
    32     QAbstractListModel(parent)
       
    33 {
       
    34 }
       
    35 
       
    36 HbListModel::~HbListModel()
       
    37 {
       
    38     qDeleteAll(items);
       
    39 }
       
    40 
       
    41 void HbListModel::clear()
       
    42 {
       
    43     for (int i = 0; i < items.count(); ++i) {
       
    44         if (items.at(i)) {
       
    45             items.at(i)->d->id = -1;
       
    46             delete items.at(i);
       
    47         }
       
    48     }
       
    49     items.clear();
       
    50     reset();
       
    51 }
       
    52 
       
    53 HbListWidgetItem *HbListModel::item(int row) const
       
    54 {
       
    55     return items.value(row);
       
    56 }
       
    57 
       
    58 void HbListModel::insert(int row, HbListWidgetItem *item)
       
    59 {
       
    60     item->d->mModel = this;
       
    61     //Do not accept duplicate inserts.
       
    62     //Occurs e.g if user of HbListWidgetitem creates HbListWidgetItem and gives a parent
       
    63     //(HbListWidget) but still calls HbListWidget::addItem()
       
    64     if (!item || items.contains(item) )
       
    65         return;
       
    66 
       
    67     if (row < 0)
       
    68         row = 0;
       
    69     else if (row > items.count())
       
    70         row = items.count();
       
    71     beginInsertRows(QModelIndex(), row, row);
       
    72     items.insert(row, item);
       
    73     item->d->id = row;
       
    74     endInsertRows();
       
    75 }
       
    76 
       
    77 void HbListModel::appendRow(HbListWidgetItem *item)
       
    78 {
       
    79     item->d->mModel = this;
       
    80     insert(rowCount(),item);
       
    81 }
       
    82 
       
    83 HbListWidgetItem *HbListModel::take(int row)
       
    84 {
       
    85     if (row < 0 || row >= items.count())
       
    86         return 0;
       
    87 
       
    88     beginRemoveRows(QModelIndex(), row, row);
       
    89     items.at(row)->d->id = -1;
       
    90     HbListWidgetItem *item = items.takeAt(row);
       
    91     endRemoveRows();
       
    92     return item;
       
    93 }
       
    94 
       
    95 int HbListModel::rowCount(const QModelIndex &parent) const
       
    96 {
       
    97      return parent.isValid() ? 0 : items.count();
       
    98 }
       
    99 
       
   100 QModelIndex HbListModel::index(HbListWidgetItem *item) const
       
   101 {
       
   102     if (!item || items.isEmpty())
       
   103         return QModelIndex();
       
   104 
       
   105     int row;
       
   106     const int id = item->d->id;
       
   107 
       
   108     if (id >= 0 && id < items.count() && items.at(id) == item)
       
   109     {
       
   110         row = id;
       
   111     }
       
   112     else
       
   113     {
       
   114         // we need to search for the item
       
   115         row = items.lastIndexOf(item);  // lastIndexOf is an optimization in favor of indexOf
       
   116         if (row == -1) // not found
       
   117             return QModelIndex();
       
   118         item->d->id = row;
       
   119     }
       
   120     return createIndex(row, 0, item);
       
   121 }
       
   122 
       
   123 QModelIndex HbListModel::index(int row, int column, const QModelIndex &parent) const
       
   124 {
       
   125     if (hasIndex(row, column, parent))
       
   126         return createIndex(row, column, items.at(row));
       
   127 
       
   128      return QModelIndex();
       
   129 }
       
   130 
       
   131 QVariant HbListModel::data(const QModelIndex &index, int role) const
       
   132 {
       
   133     if (!index.isValid() || index.row() >= items.count())
       
   134         return QVariant();
       
   135     return items.at(index.row())->data(role);
       
   136 
       
   137 }
       
   138 
       
   139 bool HbListModel::setData(const QModelIndex &index, const QVariant &value, int role)
       
   140 {
       
   141     if (!index.isValid() || index.row() >= items.count())
       
   142         return false;
       
   143     items.at(index.row())->setData(value,role);
       
   144     return true;
       
   145 }
       
   146 
       
   147 bool HbListModel::removeRows(int row, int count, const QModelIndex &parent)
       
   148 {
       
   149     if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid())
       
   150         return false;
       
   151 
       
   152     beginRemoveRows(QModelIndex(), row, row + count - 1);
       
   153     HbListWidgetItem *itm = 0;
       
   154     for (int r = row; r < row + count; ++r) {
       
   155         itm = items.takeAt(row);
       
   156         itm->d->id = -1;
       
   157         delete itm;
       
   158     }
       
   159     endRemoveRows();
       
   160     return true;
       
   161 }
       
   162 
       
   163 void HbListModel::itemChanged(HbListWidgetItem *item)
       
   164 {
       
   165     QModelIndex idx = index(item);
       
   166     emit dataChanged(idx, idx);
       
   167 }
       
   168 
       
   169 Qt::ItemFlags HbListModel::flags(const QModelIndex &index) const
       
   170 {
       
   171     if (index.isValid()) {
       
   172         const HbListWidgetItem *item = items.at(index.row());
       
   173         if (item) {
       
   174             return item->d->flags;
       
   175         } 
       
   176     }
       
   177     
       
   178     return 0;
       
   179 }
       
   180 
       
   181 #include "moc_hblistmodel_p.cpp"
       
   182 
       
   183