| 1 |
/* Copyright (C) 2014-2016 Dan Chapman <[email protected]> |
| 2 |
|
| 3 |
This file is part of Dekko email client for Ubuntu Devices/ |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License or (at your option) version 3 |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "QmlObjectListModel.h" |
| 20 |
|
| 21 |
/*! |
| 22 |
\class QQmlObjectListModel |
| 23 |
|
| 24 |
\ingroup QT_QML_MODELS |
| 25 |
|
| 26 |
\brief Provides a generic way to generate a list model from QObject derived class, suitable for QML |
| 27 |
|
| 28 |
QQmlObjectListModel is a convenience subclass \c QAbstractListModel that makes use of C++ templates |
| 29 |
and Qt Meta Object to extract properties from a \c QObject derived class and create according roles |
| 30 |
inside the model. |
| 31 |
|
| 32 |
This is a far better way than to expose directly a \c QList<QObject *> inside a \c QVariant. |
| 33 |
|
| 34 |
And this is far simpler than doing all Qt model stuff manually : no subclassing or reimplementing need. |
| 35 |
|
| 36 |
The class was designed so that most of the added API is really common with \c QList one. |
| 37 |
|
| 38 |
\b Note : Simply needs that the class used for items inherits \c QObject and has Qt Meta Properties. |
| 39 |
|
| 40 |
\sa QQmlVariantListModel |
| 41 |
*/ |
| 42 |
|
| 43 |
/*! |
| 44 |
\fn static QQmlObjectListModel * QQmlObjectListModel::create (QObject * parent = Q_NULLPTR) |
| 45 |
|
| 46 |
\details A factory to create a new model from a class that will be used as item type. |
| 47 |
|
| 48 |
\tparam ItemType The class to use as item |
| 49 |
\param parent The owner object for the model |
| 50 |
\return The newly created and configurerd model |
| 51 |
|
| 52 |
This is a template method, meant to be used like this : |
| 53 |
\code |
| 54 |
QQmlObjectListModel * myModel = QQmlObjectListModel::create<MyItemClass>(this); |
| 55 |
\endcode |
| 56 |
|
| 57 |
No other customization in needed after that. |
| 58 |
*/ |
| 59 |
|
| 60 |
/*! |
| 61 |
\fn ItemType * QQmlObjectListModel::getAs () const |
| 62 |
|
| 63 |
\details A template method to retreive a given item as a precise \c T* Qt object pointer. |
| 64 |
|
| 65 |
\tparam ItemType The class to use as return pointer type |
| 66 |
\param idx The position of the item in the model |
| 67 |
\return The typed pointer to the object, or \c Q_NULLPTR if the type doesn't match |
| 68 |
|
| 69 |
\sa get(int) const, getByUid(QString) const |
| 70 |
*/ |
| 71 |
|
| 72 |
/*! |
| 73 |
\fn QList<ItemType *> QQmlObjectListModel::listAs () const |
| 74 |
|
| 75 |
\details A template method to retreive all the items as \c QList<T*> typed Qt object pointer list. |
| 76 |
|
| 77 |
\tparam ItemType The class as object type to use in the returned pointer list |
| 78 |
\return A strongly typed \c QList of items Qt object pointers |
| 79 |
|
| 80 |
\sa list() const |
| 81 |
*/ |
| 82 |
|
| 83 |
|
| 84 |
/*! |
| 85 |
\details Returns the data in a specific index for a given role. |
| 86 |
|
| 87 |
Reimplemented for QAbstractItemModel. |
| 88 |
|
| 89 |
\param index The item index (row, column and parent) |
| 90 |
\param role The role for property |
| 91 |
\return The data in the role |
| 92 |
|
| 93 |
\b Note : the \c 0 role is a pointer to item object itself. |
| 94 |
*/ |
| 95 |
|
| 96 |
/*! |
| 97 |
\details Returns the roles available in the model. |
| 98 |
|
| 99 |
Reimplemented for QAbstractItemModel. |
| 100 |
|
| 101 |
\return The hash table of role to name matching |
| 102 |
|
| 103 |
\b Note : an additional \c 'qtObject' role is added for convenience. |
| 104 |
*/ |
| 105 |
|
| 106 |
/*! |
| 107 |
\details Modifies the data in a specific index for a given role. |
| 108 |
|
| 109 |
Reimplemented for QAbstractItemModel. |
| 110 |
|
| 111 |
\param index The item index (row, column and parent) |
| 112 |
\param value The data to write |
| 113 |
\param role The role for property |
| 114 |
\return Weither the modification was done |
| 115 |
*/ |
| 116 |
|
| 117 |
/*! |
| 118 |
\details Returns the role associated to the given property name. |
| 119 |
|
| 120 |
\param name The property name inside the item class |
| 121 |
\return The matching role, \c -1 if not found |
| 122 |
*/ |
| 123 |
|
| 124 |
|
| 125 |
/*! |
| 126 |
\details Counts the items in the model. |
| 127 |
|
| 128 |
\return The count of items in the model |
| 129 |
*/ |
| 130 |
|
| 131 |
/*! |
| 132 |
\details Counts the items in the model. |
| 133 |
|
| 134 |
\return The count of items in the model |
| 135 |
*/ |
| 136 |
/*! |
| 137 |
\details Tests the content of the model. |
| 138 |
|
| 139 |
\return Whether the model contains no item |
| 140 |
*/ |
| 141 |
|
| 142 |
/*! |
| 143 |
\details Tests the presence of a given item in the model. |
| 144 |
|
| 145 |
\param item The pointer to the item |
| 146 |
\return Whether the item was found |
| 147 |
*/ |
| 148 |
|
| 149 |
/*! |
| 150 |
\details Finds the position of given item in the model. |
| 151 |
|
| 152 |
\param item The pointer to the item |
| 153 |
\return The row index of the item, \c -1 if not found |
| 154 |
*/ |
| 155 |
|
| 156 |
/*! |
| 157 |
\details Delete all the items in the model. |
| 158 |
|
| 159 |
\b Note : The items objects will be removed from the model but they will be destructed |
| 160 |
only if they have no parent (because the model took the ownership). |
| 161 |
*/ |
| 162 |
|
| 163 |
|
| 164 |
/*! |
| 165 |
\details Adds the given item at the end of the model. |
| 166 |
|
| 167 |
\param item The pointer to the item |
| 168 |
|
| 169 |
\sa prepend(QObject*), insert(int,QObject*) |
| 170 |
*/ |
| 171 |
|
| 172 |
|
| 173 |
/*! |
| 174 |
\details Adds the given item at the beginning of the model. |
| 175 |
|
| 176 |
\param item The pointer to the item |
| 177 |
|
| 178 |
\sa append(QObject*), insert(int,QObject*) |
| 179 |
*/ |
| 180 |
|
| 181 |
|
| 182 |
/*! |
| 183 |
\details Adds the given item at a certain position in the model. |
| 184 |
|
| 185 |
\param idx The position where the item must be added |
| 186 |
\param item The pointer to the item |
| 187 |
|
| 188 |
\sa append(QObject*), prepend(QObject*) |
| 189 |
*/ |
| 190 |
|
| 191 |
|
| 192 |
/*! |
| 193 |
\details Adds the given list of items at the end of the model. |
| 194 |
|
| 195 |
\param itemList The list of items |
| 196 |
|
| 197 |
\sa prepend(QObjectList), insert(int, QObjectList) |
| 198 |
*/ |
| 199 |
|
| 200 |
|
| 201 |
/*! |
| 202 |
\details Adds the given list of items at the beginning of the model. |
| 203 |
|
| 204 |
\param itemList The list of items |
| 205 |
|
| 206 |
\sa append(QObjectList), insert(int, QObjectList) |
| 207 |
*/ |
| 208 |
|
| 209 |
|
| 210 |
/*! |
| 211 |
\details Adds the given list of items at a certain position in the model. |
| 212 |
|
| 213 |
\param idx The position where the items must be added |
| 214 |
\param itemList The list of items |
| 215 |
|
| 216 |
\sa append(QObjectList), prepend(QObjectList) |
| 217 |
*/ |
| 218 |
|
| 219 |
/*! |
| 220 |
\details Moves an item from the model to another position. |
| 221 |
|
| 222 |
\param idx The current position of the item |
| 223 |
\param pos The position where it willl be after the move |
| 224 |
*/ |
| 225 |
|
| 226 |
|
| 227 |
/*! |
| 228 |
\details Remove an item from the model. |
| 229 |
|
| 230 |
\param item The pointer to the item object |
| 231 |
*/ |
| 232 |
|
| 233 |
|
| 234 |
/*! |
| 235 |
\details Remove an item from the model. |
| 236 |
|
| 237 |
\param idx The position of the item in the model |
| 238 |
*/ |
| 239 |
|
| 240 |
|
| 241 |
/*! |
| 242 |
\details Retreives a model item as standard Qt object pointer. |
| 243 |
|
| 244 |
\param idx The position of the item in the model |
| 245 |
\return A pointer to the \c QObject |
| 246 |
|
| 247 |
\sa getAs(int) const, getByUid(QString) const |
| 248 |
*/ |
| 249 |
|
| 250 |
/*! |
| 251 |
\details Retreives the first item of the model as standard Qt object pointer. |
| 252 |
|
| 253 |
\return A pointer to the \c QObject |
| 254 |
|
| 255 |
\sa last() |
| 256 |
*/ |
| 257 |
|
| 258 |
/*! |
| 259 |
\details Retreives the last item of the model as standard Qt object pointer. |
| 260 |
|
| 261 |
\return A pointer to the \c QObject |
| 262 |
|
| 263 |
\sa first() |
| 264 |
*/ |
| 265 |
|
| 266 |
/*! |
| 267 |
\details Retreives all the items of the model as a standard Qt object pointer list. |
| 268 |
|
| 269 |
\return A \c QObjectList containing all the pointers |
| 270 |
|
| 271 |
\sa listAs() const |
| 272 |
*/ |
| 273 |
|
| 274 |
/*! |
| 275 |
\details Retreives a model item as standard Qt object pointer using its indexed property. |
| 276 |
Works only if setRoleNameForUid() was used correctly at start. |
| 277 |
|
| 278 |
\param uid The identifier value that points to the item in the index |
| 279 |
\return A pointer to the \c QObject |
| 280 |
|
| 281 |
\sa getAs(int) const, get(int) const |
| 282 |
*/ |
| 283 |
|
| 284 |
/*! |
| 285 |
\details Sets which property of the items will be used as an index key. |
| 286 |
This can be used or not, but if not, getByUid() won't work. |
| 287 |
|
| 288 |
Ideally, the property used for UID should not change after items are added |
| 289 |
to the model, because it could have some side-effects. |
| 290 |
|
| 291 |
\param name The name of the property / role that is used as the index key |
| 292 |
*/ |