doc/src/snippets/qlistview-using/model.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /****************************************************************************
       
    43 **
       
    44 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
    45 ** All rights reserved.
       
    46 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
    47 **
       
    48 ** This file is part of an example program for Qt.
       
    49 ** EDITIONS: NOLIMITS
       
    50 **
       
    51 ****************************************************************************/
       
    52 
       
    53 /*
       
    54   model.cpp
       
    55 
       
    56   A simple model that uses a QStringList as its data source.
       
    57 */
       
    58 
       
    59 #include "model.h"
       
    60 
       
    61 /*!
       
    62     Returns the number of items in the string list as the number of rows
       
    63     in the model.
       
    64 */
       
    65 
       
    66 int StringListModel::rowCount(const QModelIndex &parent) const
       
    67 {
       
    68     return stringList.count();
       
    69 }
       
    70 
       
    71 /*!
       
    72     Returns an appropriate value for the requested data.
       
    73     If the view requests an invalid index, an invalid variant is returned.
       
    74     Any valid index that corresponds to a string in the list causes that
       
    75     string to be returned.
       
    76 */
       
    77 
       
    78 QVariant StringListModel::data(const QModelIndex &index, int role) const
       
    79 {
       
    80     if (!index.isValid())
       
    81         return QVariant();
       
    82 
       
    83     if (index.row() >= stringList.size())
       
    84         return QVariant();
       
    85 
       
    86     if (role == Qt::DisplayRole)
       
    87         return stringList.at(index.row());
       
    88     else
       
    89         return QVariant();
       
    90 }
       
    91 
       
    92 /*!
       
    93     Returns the appropriate header string depending on the orientation of
       
    94     the header and the section. If anything other than the display role is
       
    95     requested, we return an invalid variant.
       
    96 */
       
    97 
       
    98 QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
       
    99                                      int role) const
       
   100 {
       
   101     if (role != Qt::DisplayRole)
       
   102         return QVariant();
       
   103 
       
   104     if (orientation == Qt::Horizontal)
       
   105         return QString("Column %1").arg(section);
       
   106     else
       
   107         return QString("Row %1").arg(section);
       
   108 }
       
   109 
       
   110 /*!
       
   111     Returns an appropriate value for the item's flags. Valid items are
       
   112     enabled, selectable, and editable.
       
   113 */
       
   114 
       
   115 Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
       
   116 {
       
   117     if (!index.isValid())
       
   118         return Qt::ItemIsEnabled;
       
   119 
       
   120     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
       
   121 }
       
   122 
       
   123 /*!
       
   124     Changes an item in the string list, but only if the following conditions
       
   125     are met:
       
   126 
       
   127     * The index supplied is valid.
       
   128     * The index corresponds to an item to be shown in a view.
       
   129     * The role associated with editing text is specified.
       
   130 
       
   131     The dataChanged() signal is emitted if the item is changed.
       
   132 */
       
   133 
       
   134 bool StringListModel::setData(const QModelIndex &index,
       
   135                               const QVariant &value, int role)
       
   136 {
       
   137     if (index.isValid() && role == Qt::EditRole) {
       
   138 
       
   139         stringList.replace(index.row(), value.toString());
       
   140         emit dataChanged(index, index);
       
   141         return true;
       
   142     }
       
   143     return false;
       
   144 }
       
   145 
       
   146 /*!
       
   147     Inserts a number of rows into the model at the specified position.
       
   148 */
       
   149 
       
   150 bool StringListModel::insertRows(int position, int rows, const QModelIndex &parent)
       
   151 {
       
   152     beginInsertRows(QModelIndex(), position, position+rows-1);
       
   153 
       
   154     for (int row = 0; row < rows; ++row) {
       
   155         stringList.insert(position, "");
       
   156     }
       
   157 
       
   158     endInsertRows();
       
   159     return true;
       
   160 }
       
   161 
       
   162 /*!
       
   163     Removes a number of rows from the model at the specified position.
       
   164 */
       
   165 
       
   166 bool StringListModel::removeRows(int position, int rows, const QModelIndex &parent)
       
   167 {
       
   168     beginRemoveRows(QModelIndex(), position, position+rows-1);
       
   169 
       
   170     for (int row = 0; row < rows; ++row) {
       
   171         stringList.removeAt(position);
       
   172     }
       
   173 
       
   174     endRemoveRows();
       
   175     return true;
       
   176 }