Qt 4.8
qdeclarativerepeater.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativerepeater_p.h"
43 #include "private/qdeclarativerepeater_p_p.h"
44 
45 #include "private/qdeclarativevisualitemmodel_p.h"
46 #include <private/qdeclarativeglobal_p.h>
48 
49 #include <qlistmodelinterface_p.h>
50 
53 : model(0), ownModel(false)
54 {
55 }
56 
58 {
59  if (ownModel)
60  delete model;
61 }
62 
161 {
162 }
163 
165 {
166 }
167 
190 {
191  Q_D(const QDeclarativeRepeater);
192  return d->dataSource;
193 }
194 
196 {
198  if (d->dataSource == model)
199  return;
200 
201  clear();
202  if (d->model) {
203  disconnect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
204  disconnect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
205  disconnect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
206  disconnect(d->model, SIGNAL(modelReset()), this, SLOT(modelReset()));
207  /*
208  disconnect(d->model, SIGNAL(createdItem(int,QDeclarativeItem*)), this, SLOT(createdItem(int,QDeclarativeItem*)));
209  disconnect(d->model, SIGNAL(destroyingItem(QDeclarativeItem*)), this, SLOT(destroyingItem(QDeclarativeItem*)));
210  */
211  }
212  d->dataSource = model;
213  QObject *object = qvariant_cast<QObject*>(model);
214  QDeclarativeVisualModel *vim = 0;
215  if (object && (vim = qobject_cast<QDeclarativeVisualModel *>(object))) {
216  if (d->ownModel) {
217  delete d->model;
218  d->ownModel = false;
219  }
220  d->model = vim;
221  } else {
222  if (!d->ownModel) {
223  d->model = new QDeclarativeVisualDataModel(qmlContext(this), this);
224  d->ownModel = true;
225  }
226  if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model))
227  dataModel->setModel(model);
228  }
229  if (d->model) {
230  connect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
231  connect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
232  connect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
233  connect(d->model, SIGNAL(modelReset()), this, SLOT(modelReset()));
234  /*
235  connect(d->model, SIGNAL(createdItem(int,QDeclarativeItem*)), this, SLOT(createdItem(int,QDeclarativeItem*)));
236  connect(d->model, SIGNAL(destroyingItem(QDeclarativeItem*)), this, SLOT(destroyingItem(QDeclarativeItem*)));
237  */
238  regenerate();
239  }
240  emit modelChanged();
241  emit countChanged();
242 }
243 
281 {
282  Q_D(const QDeclarativeRepeater);
283  if (d->model) {
284  if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model))
285  return dataModel->delegate();
286  }
287 
288  return 0;
289 }
290 
292 {
294  if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model))
295  if (delegate == dataModel->delegate())
296  return;
297 
298  if (!d->ownModel) {
299  d->model = new QDeclarativeVisualDataModel(qmlContext(this));
300  d->ownModel = true;
301  }
302  if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model)) {
303  dataModel->setDelegate(delegate);
304  regenerate();
306  }
307 }
308 
317 int QDeclarativeRepeater::count() const
318 {
319  Q_D(const QDeclarativeRepeater);
320  if (d->model)
321  return d->model->count();
322  return 0;
323 }
324 
336 {
337  Q_D(const QDeclarativeRepeater);
338  if (index >= 0 && index < d->deletables.count())
339  return d->deletables[index];
340  return 0;
341 
342 }
343 
345 {
347  regenerate();
348 }
349 
351  const QVariant &value)
352 {
353  QVariant rv = QDeclarativeItem::itemChange(change, value);
354  if (change == ItemParentHasChanged) {
355  regenerate();
356  }
357 
358  return rv;
359 }
360 
362 {
364  bool complete = isComponentComplete();
365 
366  if (d->model) {
367  while (d->deletables.count() > 0) {
368  QDeclarativeItem *item = d->deletables.takeLast();
369  if (complete)
370  emit itemRemoved(d->deletables.count()-1, item);
371  d->model->release(item);
372  }
373  }
374  d->deletables.clear();
375 }
376 
378 {
380  if (!isComponentComplete())
381  return;
382 
383  clear();
384 
385  if (!d->model || !d->model->count() || !d->model->isValid() || !parentItem() || !isComponentComplete())
386  return;
387 
388  for (int ii = 0; ii < count(); ++ii) {
389  QDeclarativeItem *item = d->model->item(ii);
390  if (item) {
392  item->setParentItem(parentItem());
393  item->stackBefore(this);
394  d->deletables << item;
395  emit itemAdded(ii, item);
396  }
397  }
398 }
399 
401 {
403  if (!isComponentComplete())
404  return;
405  for (int i = 0; i < count; ++i) {
406  int modelIndex = index + i;
407  QDeclarativeItem *item = d->model->item(modelIndex);
408  if (item) {
410  item->setParentItem(parentItem());
411  if (modelIndex < d->deletables.count())
412  item->stackBefore(d->deletables.at(modelIndex));
413  else
414  item->stackBefore(this);
415  d->deletables.insert(modelIndex, item);
416  emit itemAdded(modelIndex, item);
417  }
418  }
419  emit countChanged();
420 }
421 
423 {
425  if (!isComponentComplete() || count <= 0)
426  return;
427  while (count--) {
428  QDeclarativeItem *item = d->deletables.takeAt(index);
429  emit itemRemoved(index, item);
430  if (item)
431  d->model->release(item);
432  else
433  break;
434  }
435  emit countChanged();
436 }
437 
438 void QDeclarativeRepeater::itemsMoved(int from, int to, int count)
439 {
441  if (!isComponentComplete() || count <= 0)
442  return;
443  if (from + count > d->deletables.count()) {
444  regenerate();
445  return;
446  }
447  QList<QDeclarativeItem*> removed;
448  int removedCount = count;
449  while (removedCount--)
450  removed << d->deletables.takeAt(from);
451  for (int i = 0; i < count; ++i)
452  d->deletables.insert(to + i, removed.at(i));
453  d->deletables.last()->stackBefore(this);
454  for (int i = d->model->count()-1; i > 0; --i) {
455  QDeclarativeItem *item = d->deletables.at(i-1);
456  item->stackBefore(d->deletables.at(i));
457  }
458 }
459 
461 {
462  if (!isComponentComplete())
463  return;
464  regenerate();
465  emit countChanged();
466 }
467 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QGraphicsItem * parent
QDeclarativeRepeater(QDeclarativeItem *parent=0)
This handler is called when an item is added to the repeater.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDeclarativeParserStatus ** d
void itemAdded(int index, QDeclarativeItem *item)
bool isComponentComplete() const
Returns true if construction of the QML component is complete; otherwise returns false.
void setDelegate(QDeclarativeComponent *)
#define SLOT(a)
Definition: qobjectdefs.h:226
void setModel(const QVariant &)
GraphicsItemChange
This enum describes the state changes that are notified by QGraphicsItem::itemChange().
void itemsMoved(int, int, int)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
Q_DECLARATIVE_EXPORT QDeclarativeContext * qmlContext(const QObject *)
#define Q_D(Class)
Definition: qglobal.h:2482
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Note that unlike QGraphicsItems, QDeclarativeItem::itemChange() is not called during initial widget p...
void itemRemoved(int index, QDeclarativeItem *item)
void setParentItem(QDeclarativeItem *parent)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QDeclarativeComponent class encapsulates a QML component definition.
The QDeclarativeItem class provides the most basic of all visual items in QML.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
QDeclarativeVisualModel * model
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
QDeclarativeComponent * delegate() const
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
QDeclarativeItem * parentItem() const
Returns the QDeclarativeItem parent of this item.
virtual QVariant itemChange(GraphicsItemChange, const QVariant &)
Note that unlike QGraphicsItems, QDeclarativeItem::itemChange() is not called during initial widget p...
void stackBefore(const QGraphicsItem *sibling)
Stacks this item before sibling, which must be a sibling item (i.
void QDeclarative_setParent_noEvent(QObject *object, QObject *parent)
Makes the object a child of parent.
virtual void componentComplete()
QVariant model() const
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
quint16 index
Q_INVOKABLE QDeclarativeItem * itemAt(int index) const
The QList class is a template class that provides lists.
Definition: qdatastream.h:62