[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | /*
|
---|
| 42 | model.cpp
|
---|
| 43 |
|
---|
| 44 | A simple model that uses a QVector as its data source.
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | #include "model.h"
|
---|
| 48 |
|
---|
| 49 | /*!
|
---|
| 50 | Returns the number of items in the string list as the number of rows
|
---|
| 51 | in the model.
|
---|
| 52 | */
|
---|
| 53 |
|
---|
| 54 | int LinearModel::rowCount(const QModelIndex &parent) const
|
---|
| 55 | {
|
---|
| 56 | Q_USING(parent);
|
---|
| 57 |
|
---|
| 58 | return values.count();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /*
|
---|
| 62 | Returns an appropriate value for the requested data.
|
---|
| 63 | If the view requests an invalid index, an invalid variant is returned.
|
---|
| 64 | If a header is requested then we just return the column or row number,
|
---|
| 65 | depending on the orientation of the header.
|
---|
| 66 | Any valid index that corresponds to a string in the list causes that
|
---|
| 67 | string to be returned.
|
---|
| 68 | */
|
---|
| 69 |
|
---|
| 70 | /*!
|
---|
| 71 | Returns a model index for other component to use when referencing the
|
---|
| 72 | item specified by the given row, column, and type. The parent index
|
---|
| 73 | is ignored.
|
---|
| 74 | */
|
---|
| 75 |
|
---|
| 76 | QModelIndex LinearModel::index(int row, int column, const QModelIndex &parent) const
|
---|
| 77 | {
|
---|
| 78 | if (parent == QModelIndex() && row >= 0 && row < rowCount()
|
---|
| 79 | && column == 0)
|
---|
| 80 | return createIndex(row, column, 0);
|
---|
| 81 | else
|
---|
| 82 | return QModelIndex();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | QVariant LinearModel::data(const QModelIndex &index, int role) const
|
---|
| 86 | {
|
---|
| 87 | Q_UNUSED(role);
|
---|
| 88 |
|
---|
| 89 | if (!index.isValid())
|
---|
| 90 | return QVariant();
|
---|
| 91 |
|
---|
| 92 | return values.at(index.row());
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /*!
|
---|
| 96 | Returns Qt::ItemIsEditable so that all items in the vector can be edited.
|
---|
| 97 | */
|
---|
| 98 |
|
---|
| 99 | Qt::ItemFlags LinearModel::flags(const QModelIndex &index) const
|
---|
| 100 | {
|
---|
| 101 | // all items in the model are editable
|
---|
| 102 | return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /*!
|
---|
| 106 | Changes an item in the string list, but only if the following conditions
|
---|
| 107 | are met:
|
---|
| 108 |
|
---|
| 109 | * The index supplied is valid.
|
---|
| 110 | * The index corresponds to an item to be shown in a view.
|
---|
| 111 | * The role associated with editing text is specified.
|
---|
| 112 |
|
---|
| 113 | The dataChanged() signal is emitted if the item is changed.
|
---|
| 114 | */
|
---|
| 115 |
|
---|
| 116 | bool LinearModel::setData(const QModelIndex &index,
|
---|
| 117 | const QVariant &value, int role)
|
---|
| 118 | {
|
---|
| 119 | if (!index.isValid() || role != Qt::EditRole)
|
---|
| 120 | return false;
|
---|
| 121 | values.replace(index.row(), value.toInt());
|
---|
| 122 | emit dataChanged(index, index);
|
---|
| 123 | return true;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /*!
|
---|
| 127 | Inserts a number of rows into the model at the specified position.
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | bool LinearModel::insertRows(int position, int rows, const QModelIndex &parent)
|
---|
| 131 | {
|
---|
| 132 | beginInsertRows(parent, position, position + rows - 1);
|
---|
| 133 |
|
---|
| 134 | values.insert(position, rows, 0);
|
---|
| 135 |
|
---|
| 136 | endInsertRows();
|
---|
| 137 | return true;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /*!
|
---|
| 141 | Removes a number of rows from the model at the specified position.
|
---|
| 142 | */
|
---|
| 143 |
|
---|
| 144 | bool LinearModel::removeRows(int position, int rows, const QModelIndex &parent)
|
---|
| 145 | {
|
---|
| 146 | beginRemoveRows(QModelIndex(), position, position+rows-1);
|
---|
| 147 |
|
---|
| 148 | values.remove(position, rows);
|
---|
| 149 |
|
---|
| 150 | endRemoveRows();
|
---|
| 151 | return true;
|
---|
| 152 | }
|
---|