Qt 4.8
qabstractitemmodel.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 QtCore 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 "qabstractitemmodel.h"
43 #include <private/qabstractitemmodel_p.h>
44 #include <qdatastream.h>
45 #include <qstringlist.h>
46 #include <qsize.h>
47 #include <qmimedata.h>
48 #include <qdebug.h>
49 #include <qvector.h>
50 #include <qstack.h>
51 #include <qbitarray.h>
52 
53 #include <limits.h>
54 
56 
58 {
59  Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list
61  QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model());
62  QHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes;
64  if (it != indexes.end()) {
65  d = (*it);
66  } else {
67  d = new QPersistentModelIndexData(index);
68  indexes.insert(index, d);
69  }
70  Q_ASSERT(d);
71  return d;
72 }
73 
75 {
76  Q_ASSERT(data);
77  Q_ASSERT(data->ref == 0);
78  QAbstractItemModel *model = const_cast<QAbstractItemModel *>(data->model);
79  // a valid persistent model index with a null model pointer can only happen if the model was destroyed
80  if (model) {
81  QAbstractItemModelPrivate *p = model->d_func();
82  Q_ASSERT(p);
84  }
85  delete data;
86 }
87 
119  : d(0)
120 {
121 }
122 
134  : d(other.d)
135 {
136  if (d) d->ref.ref();
137 }
138 
144  : d(0)
145 {
146  if (index.isValid()) {
148  d->ref.ref();
149  }
150 }
151 
159 {
160  if (d && !d->ref.deref()) {
162  d = 0;
163  }
164 }
165 
175 {
176  if (d && other.d)
177  return d->index == other.d->index;
178  return d == other.d;
179 }
180 
195 {
196  if (d && other.d)
197  return d->index < other.d->index;
198 
199  return d < other.d;
200 }
201 
219 {
220  if (d == other.d)
221  return *this;
222  if (d && !d->ref.deref())
224  d = other.d;
225  if (d) d->ref.ref();
226  return *this;
227 }
228 
235 {
236  if (d && !d->ref.deref())
238  if (other.isValid()) {
240  if (d) d->ref.ref();
241  } else {
242  d = 0;
243  }
244  return *this;
245 }
246 
256 QPersistentModelIndex::operator const QModelIndex&() const
257 {
258  static const QModelIndex invalid;
259  if (d)
260  return d->index;
261  return invalid;
262 }
263 
278 {
279  if (d)
280  return d->index == other;
281  return !other.isValid();
282 }
283 
295 {
296  if (d)
297  return d->index != other;
298  return other.isValid();
299 }
300 
311 {
312  if (d)
313  return d->index.row();
314  return -1;
315 }
316 
327 {
328  if (d)
329  return d->index.column();
330  return -1;
331 }
332 
343 {
344  if (d)
345  return d->index.internalPointer();
346  return 0;
347 }
348 
359 {
360  if (d)
361  return d->index.internalId();
362  return 0;
363 }
364 
372 {
373  if (d)
374  return d->index.parent();
375  return QModelIndex();
376 }
377 
386 {
387  if (d)
388  return d->index.sibling(row, column);
389  return QModelIndex();
390 }
391 
400 {
401  if (d)
402  return d->index.child(row, column);
403  return QModelIndex();
404 }
405 
413 {
414  if (d)
415  return d->index.data(role);
416  return QVariant();
417 }
418 
427 Qt::ItemFlags QPersistentModelIndex::flags() const
428 {
429  if (d)
430  return d->index.flags();
431  return 0;
432 }
433 
438 {
439  if (d)
440  return d->index.model();
441  return 0;
442 }
443 
460 {
461  return d && d->index.isValid();
462 }
463 
464 #ifndef QT_NO_DEBUG_STREAM
466 {
467 #ifndef Q_BROKEN_DEBUG_STREAM
468  dbg.nospace() << "QModelIndex(" << idx.row() << ',' << idx.column()
469  << ',' << idx.internalPointer() << ',' << idx.model() << ')';
470  return dbg.space();
471 #else
472  qWarning("This compiler doesn't support streaming QModelIndex to QDebug");
473  return dbg;
474  Q_UNUSED(idx);
475 #endif
476 }
477 
479 {
480  if (idx.d)
481  dbg << idx.d->index;
482  else
483  dbg << QModelIndex();
484  return dbg;
485 }
486 #endif
487 
489 {
490 public:
492  QModelIndex index(int, int, const QModelIndex &) const { return QModelIndex(); }
493  QModelIndex parent(const QModelIndex &) const { return QModelIndex(); }
494  int rowCount(const QModelIndex &) const { return 0; }
495  int columnCount(const QModelIndex &) const { return 0; }
496  bool hasChildren(const QModelIndex &) const { return false; }
497  QVariant data(const QModelIndex &, int) const { return QVariant(); }
498 };
499 
500 Q_GLOBAL_STATIC(QEmptyItemModel, qEmptyModel)
501 
503 {
504  return qEmptyModel();
505 }
506 
507 namespace {
508  struct DefaultRoleNames : public QHash<int, QByteArray>
509  {
510  DefaultRoleNames() {
511  (*this)[Qt::DisplayRole] = "display";
512  (*this)[Qt::DecorationRole] = "decoration";
513  (*this)[Qt::EditRole] = "edit";
514  (*this)[Qt::ToolTipRole] = "toolTip";
515  (*this)[Qt::StatusTipRole] = "statusTip";
516  (*this)[Qt::WhatsThisRole] = "whatsThis";
517  }
518  };
519 }
520 
521 Q_GLOBAL_STATIC(DefaultRoleNames, qDefaultRoleNames)
522 
523 const QHash<int,QByteArray> &QAbstractItemModelPrivate::defaultRoleNames()
524 {
525  return *qDefaultRoleNames();
526 }
527 
528 
529 static uint typeOfVariant(const QVariant &value)
530 {
531  //return 0 for integer, 1 for floating point and 2 for other
532  switch (value.userType()) {
533  case QVariant::Bool:
534  case QVariant::Int:
535  case QVariant::UInt:
536  case QVariant::LongLong:
537  case QVariant::ULongLong:
538  case QVariant::Char:
539  case QMetaType::Short:
540  case QMetaType::UShort:
541  case QMetaType::UChar:
542  case QMetaType::ULong:
543  case QMetaType::Long:
544  return 0;
545  case QVariant::Double:
546  case QMetaType::Float:
547  return 1;
548  default:
549  return 2;
550  }
551 }
552 
563 {
564  switch(qMax(typeOfVariant(v1), typeOfVariant(v2)))
565  {
566  case 0: //integer type
567  return v1.toLongLong() < v2.toLongLong();
568  case 1: //floating point
569  return v1.toReal() < v2.toReal();
570  default:
571  return v1.toString().localeAwareCompare(v2.toString()) < 0;
572  }
573 }
574 
576 {
577  if (data->index.isValid()) {
578  int removed = persistent.indexes.remove(data->index);
579  Q_ASSERT_X(removed == 1, "QPersistentModelIndex::~QPersistentModelIndex",
580  "persistent model indexes corrupted"); //maybe the index was somewhat invalid?
581  // This assert may happen if the model use changePersistentIndex in a way that could result on two
582  // QPersistentModelIndex pointing to the same index.
583  Q_UNUSED(removed);
584  }
585  // make sure our optimization still works
586  for (int i = persistent.moved.count() - 1; i >= 0; --i) {
587  int idx = persistent.moved[i].indexOf(data);
588  if (idx >= 0)
589  persistent.moved[i].remove(idx);
590  }
591  // update the references to invalidated persistent indexes
592  for (int i = persistent.invalidated.count() - 1; i >= 0; --i) {
593  int idx = persistent.invalidated[i].indexOf(data);
594  if (idx >= 0)
595  persistent.invalidated[i].remove(idx);
596  }
597 
598 }
599 
601  int first, int last)
602 {
604  Q_UNUSED(last);
605  QVector<QPersistentModelIndexData *> persistent_moved;
606  if (first < q->rowCount(parent)) {
607  for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
608  it != persistent.indexes.constEnd(); ++it) {
610  const QModelIndex &index = data->index;
611  if (index.row() >= first && index.isValid() && index.parent() == parent) {
612  persistent_moved.append(data);
613  }
614  }
615  }
616  persistent.moved.push(persistent_moved);
617 }
618 
620  int first, int last)
621 {
622  QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop();
623  int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested
625  it != persistent_moved.constEnd(); ++it) {
627  QModelIndex old = data->index;
628  persistent.indexes.erase(persistent.indexes.find(old));
629  data->index = q_func()->index(old.row() + count, old.column(), parent);
630  if (data->index.isValid()) {
631  persistent.insertMultiAtEnd(data->index, data);
632  } else {
633  qWarning() << "QAbstractItemModel::endInsertRows: Invalid index (" << old.row() + count << ',' << old.column() << ") in model" << q_func();
634  }
635  }
636 }
637 
638 void QAbstractItemModelPrivate::itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation)
639 {
640  QVector<QPersistentModelIndexData *> persistent_moved_explicitly;
641  QVector<QPersistentModelIndexData *> persistent_moved_in_source;
642  QVector<QPersistentModelIndexData *> persistent_moved_in_destination;
643 
645  const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator begin = persistent.indexes.constBegin();
646  const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator end = persistent.indexes.constEnd();
647 
648  const bool sameParent = (srcParent == destinationParent);
649  const bool movingUp = (srcFirst > destinationChild);
650 
651  for ( it = begin; it != end; ++it) {
653  const QModelIndex &index = data->index;
654  const QModelIndex &parent = index.parent();
655  const bool isSourceIndex = (parent == srcParent);
656  const bool isDestinationIndex = (parent == destinationParent);
657 
658  int childPosition;
659  if (orientation == Qt::Vertical)
660  childPosition = index.row();
661  else
662  childPosition = index.column();
663 
664  if (!index.isValid() || !(isSourceIndex || isDestinationIndex ) )
665  continue;
666 
667  if (!sameParent && isDestinationIndex) {
668  if (childPosition >= destinationChild)
669  persistent_moved_in_destination.append(data);
670  continue;
671  }
672 
673  if (sameParent && movingUp && childPosition < destinationChild)
674  continue;
675 
676  if (sameParent && !movingUp && childPosition < srcFirst )
677  continue;
678 
679  if (!sameParent && childPosition < srcFirst)
680  continue;
681 
682  if (sameParent && (childPosition > srcLast) && (childPosition >= destinationChild ))
683  continue;
684 
685  if ((childPosition <= srcLast) && (childPosition >= srcFirst)) {
686  persistent_moved_explicitly.append(data);
687  } else {
688  persistent_moved_in_source.append(data);
689  }
690  }
691  persistent.moved.push(persistent_moved_explicitly);
692  persistent.moved.push(persistent_moved_in_source);
693  persistent.moved.push(persistent_moved_in_destination);
694 }
695 
707 {
711 
712  for (it = begin; it != end; ++it)
713  {
715 
716  int row = data->index.row();
717  int column = data->index.column();
718 
719  if (Qt::Vertical == orientation)
720  row += change;
721  else
722  column += change;
723 
724  persistent.indexes.erase(persistent.indexes.find(data->index));
725  data->index = q_func()->index(row, column, parent);
726  if (data->index.isValid()) {
727  persistent.insertMultiAtEnd(data->index, data);
728  } else {
729  qWarning() << "QAbstractItemModel::endMoveRows: Invalid index (" << row << "," << column << ") in model" << q_func();
730  }
731  }
732 }
733 
734 void QAbstractItemModelPrivate::itemsMoved(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation)
735 {
736  QVector<QPersistentModelIndexData *> moved_in_destination = persistent.moved.pop();
737  QVector<QPersistentModelIndexData *> moved_in_source = persistent.moved.pop();
738  QVector<QPersistentModelIndexData *> moved_explicitly = persistent.moved.pop();
739 
740  const bool sameParent = (sourceParent == destinationParent);
741  const bool movingUp = (sourceFirst > destinationChild);
742 
743  const int explicit_change = (!sameParent || movingUp) ? destinationChild - sourceFirst : destinationChild - sourceLast - 1 ;
744  const int source_change = (!sameParent || !movingUp) ? -1*(sourceLast - sourceFirst + 1) : sourceLast - sourceFirst + 1 ;
745  const int destination_change = sourceLast - sourceFirst + 1;
746 
747  movePersistentIndexes(moved_explicitly, explicit_change, destinationParent, orientation);
748  movePersistentIndexes(moved_in_source, source_change, sourceParent, orientation);
749  movePersistentIndexes(moved_in_destination, destination_change, destinationParent, orientation);
750 }
751 
753  int first, int last)
754 {
755  QVector<QPersistentModelIndexData *> persistent_moved;
756  QVector<QPersistentModelIndexData *> persistent_invalidated;
757  // find the persistent indexes that are affected by the change, either by being in the removed subtree
758  // or by being on the same level and below the removed rows
759  for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
760  it != persistent.indexes.constEnd(); ++it) {
762  bool level_changed = false;
763  QModelIndex current = data->index;
764  while (current.isValid()) {
765  QModelIndex current_parent = current.parent();
766  if (current_parent == parent) { // on the same level as the change
767  if (!level_changed && current.row() > last) // below the removed rows
768  persistent_moved.append(data);
769  else if (current.row() <= last && current.row() >= first) // in the removed subtree
770  persistent_invalidated.append(data);
771  break;
772  }
773  current = current_parent;
774  level_changed = true;
775  }
776  }
777 
778  persistent.moved.push(persistent_moved);
779  persistent.invalidated.push(persistent_invalidated);
780 }
781 
783  int first, int last)
784 {
785  QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop();
786  int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested
788  it != persistent_moved.constEnd(); ++it) {
790  QModelIndex old = data->index;
791  persistent.indexes.erase(persistent.indexes.find(old));
792  data->index = q_func()->index(old.row() - count, old.column(), parent);
793  if (data->index.isValid()) {
794  persistent.insertMultiAtEnd(data->index, data);
795  } else {
796  qWarning() << "QAbstractItemModel::endRemoveRows: Invalid index (" << old.row() - count << ',' << old.column() << ") in model" << q_func();
797  }
798  }
799  QVector<QPersistentModelIndexData *> persistent_invalidated = persistent.invalidated.pop();
801  it != persistent_invalidated.constEnd(); ++it) {
803  persistent.indexes.erase(persistent.indexes.find(data->index));
804  data->index = QModelIndex();
805  data->model = 0;
806  }
807 }
808 
810  int first, int last)
811 {
813  Q_UNUSED(last);
814  QVector<QPersistentModelIndexData *> persistent_moved;
815  if (first < q->columnCount(parent)) {
816  for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
817  it != persistent.indexes.constEnd(); ++it) {
819  const QModelIndex &index = data->index;
820  if (index.column() >= first && index.isValid() && index.parent() == parent)
821  persistent_moved.append(data);
822  }
823  }
824  persistent.moved.push(persistent_moved);
825 }
826 
828  int first, int last)
829 {
830  QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop();
831  int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested
833  it != persistent_moved.constEnd(); ++it) {
835  QModelIndex old = data->index;
836  persistent.indexes.erase(persistent.indexes.find(old));
837  data->index = q_func()->index(old.row(), old.column() + count, parent);
838  if (data->index.isValid()) {
839  persistent.insertMultiAtEnd(data->index, data);
840  } else {
841  qWarning() << "QAbstractItemModel::endInsertColumns: Invalid index (" << old.row() << ',' << old.column() + count << ") in model" << q_func();
842  }
843  }
844 }
845 
847  int first, int last)
848 {
849  QVector<QPersistentModelIndexData *> persistent_moved;
850  QVector<QPersistentModelIndexData *> persistent_invalidated;
851  // find the persistent indexes that are affected by the change, either by being in the removed subtree
852  // or by being on the same level and to the right of the removed columns
853  for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
854  it != persistent.indexes.constEnd(); ++it) {
856  bool level_changed = false;
857  QModelIndex current = data->index;
858  while (current.isValid()) {
859  QModelIndex current_parent = current.parent();
860  if (current_parent == parent) { // on the same level as the change
861  if (!level_changed && current.column() > last) // right of the removed columns
862  persistent_moved.append(data);
863  else if (current.column() <= last && current.column() >= first) // in the removed subtree
864  persistent_invalidated.append(data);
865  break;
866  }
867  current = current_parent;
868  level_changed = true;
869  }
870  }
871 
872  persistent.moved.push(persistent_moved);
873  persistent.invalidated.push(persistent_invalidated);
874 
875 }
876 
878  int first, int last)
879 {
880  QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop();
881  int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested
883  it != persistent_moved.constEnd(); ++it) {
885  QModelIndex old = data->index;
886  persistent.indexes.erase(persistent.indexes.find(old));
887  data->index = q_func()->index(old.row(), old.column() - count, parent);
888  if (data->index.isValid()) {
889  persistent.insertMultiAtEnd(data->index, data);
890  } else {
891  qWarning() << "QAbstractItemModel::endRemoveColumns: Invalid index (" << old.row() << ',' << old.column() - count << ") in model" << q_func();
892  }
893  }
894  QVector<QPersistentModelIndexData *> persistent_invalidated = persistent.invalidated.pop();
896  it != persistent_invalidated.constEnd(); ++it) {
898  persistent.indexes.erase(persistent.indexes.find(data->index));
899  data->index = QModelIndex();
900  data->model = 0;
901  }
902 }
903 
1478 {
1479 
1480 }
1481 
1482 
1487  : QObject(*new QAbstractItemModelPrivate, parent)
1488 {
1489 }
1490 
1495  : QObject(dd, parent)
1496 {
1497 }
1498 
1503 {
1504  d_func()->invalidatePersistentIndexes();
1505 }
1506 
1793 bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const
1794 {
1795  if (row < 0 || column < 0)
1796  return false;
1797  return row < rowCount(parent) && column < columnCount(parent);
1798 }
1799 
1800 
1809 {
1810  return (rowCount(parent) > 0) && (columnCount(parent) > 0);
1811 }
1812 
1813 
1824 {
1825  QMap<int, QVariant> roles;
1826  for (int i = 0; i < Qt::UserRole; ++i) {
1827  QVariant variantData = data(index, i);
1828  if (variantData.isValid())
1829  roles.insert(i, variantData);
1830  }
1831  return roles;
1832 }
1833 
1847 bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
1848 {
1849  Q_UNUSED(index);
1850  Q_UNUSED(value);
1851  Q_UNUSED(role);
1852  return false;
1853 }
1854 
1881 {
1882  bool b = true;
1883  for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it)
1884  b = b && setData(index, it.value(), it.key());
1885  return b;
1886 }
1887 
1895 {
1897  types << QLatin1String("application/x-qabstractitemmodeldatalist");
1898  return types;
1899 }
1900 
1912 {
1913  if (indexes.count() <= 0)
1914  return 0;
1916  if (types.isEmpty())
1917  return 0;
1918  QMimeData *data = new QMimeData();
1919  QString format = types.at(0);
1920  QByteArray encoded;
1922  encodeData(indexes, stream);
1923  data->setData(format, encoded);
1924  return data;
1925 }
1926 
1951  int row, int column, const QModelIndex &parent)
1952 {
1953  // check if the action is supported
1954  if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
1955  return false;
1956  // check if the format is supported
1958  if (types.isEmpty())
1959  return false;
1960  QString format = types.at(0);
1961  if (!data->hasFormat(format))
1962  return false;
1963  if (row > rowCount(parent))
1964  row = rowCount(parent);
1965  if (row == -1)
1966  row = rowCount(parent);
1967  if (column == -1)
1968  column = 0;
1969  // decode and insert
1970  QByteArray encoded = data->data(format);
1972  return decodeData(row, column, parent, stream);
1973 }
1974 
1992 {
1993  return Qt::CopyAction;
1994 }
1995 
2008 {
2009  // ### Qt 5: make this virtual or these properties
2010  Q_D(const QAbstractItemModel);
2011  if (d->supportedDragActions != -1)
2012  return d->supportedDragActions;
2013  return supportedDropActions();
2014 }
2015 
2026 void QAbstractItemModel::setSupportedDragActions(Qt::DropActions actions)
2027 {
2029  d->supportedDragActions = actions;
2030 }
2031 
2063 {
2064  return false;
2065 }
2066 
2092 {
2093  return false;
2094 }
2095 
2113 {
2114  return false;
2115 }
2116 
2134 {
2135  return false;
2136 }
2137 
2149 {
2150  // do nothing
2151 }
2152 
2166 {
2167  return false;
2168 }
2169 
2179 Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
2180 {
2181  Q_D(const QAbstractItemModel);
2182  if (!d->indexValid(index))
2183  return 0;
2184 
2186 }
2187 
2194 {
2195  Q_UNUSED(column);
2196  Q_UNUSED(order);
2197  // do nothing
2198 }
2199 
2210 {
2211  return index;
2212 }
2213 
2234  const QVariant &value, int hits,
2235  Qt::MatchFlags flags) const
2236 {
2237  QModelIndexList result;
2238  uint matchType = flags & 0x0F;
2240  bool recurse = flags & Qt::MatchRecursive;
2241  bool wrap = flags & Qt::MatchWrap;
2242  bool allHits = (hits == -1);
2243  QString text; // only convert to a string if it is needed
2244  QModelIndex p = parent(start);
2245  int from = start.row();
2246  int to = rowCount(p);
2247 
2248  // iterates twice if wrapping
2249  for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
2250  for (int r = from; (r < to) && (allHits || result.count() < hits); ++r) {
2251  QModelIndex idx = index(r, start.column(), p);
2252  if (!idx.isValid())
2253  continue;
2254  QVariant v = data(idx, role);
2255  // QVariant based matching
2256  if (matchType == Qt::MatchExactly) {
2257  if (value == v)
2258  result.append(idx);
2259  } else { // QString based matching
2260  if (text.isEmpty()) // lazy conversion
2261  text = value.toString();
2262  QString t = v.toString();
2263  switch (matchType) {
2264  case Qt::MatchRegExp:
2265  if (QRegExp(text, cs).exactMatch(t))
2266  result.append(idx);
2267  break;
2268  case Qt::MatchWildcard:
2269  if (QRegExp(text, cs, QRegExp::Wildcard).exactMatch(t))
2270  result.append(idx);
2271  break;
2272  case Qt::MatchStartsWith:
2273  if (t.startsWith(text, cs))
2274  result.append(idx);
2275  break;
2276  case Qt::MatchEndsWith:
2277  if (t.endsWith(text, cs))
2278  result.append(idx);
2279  break;
2280  case Qt::MatchFixedString:
2281  if (t.compare(text, cs) == 0)
2282  result.append(idx);
2283  break;
2284  case Qt::MatchContains:
2285  default:
2286  if (t.contains(text, cs))
2287  result.append(idx);
2288  }
2289  }
2290  if (recurse && hasChildren(idx)) { // search the hierarchy
2291  result += match(index(0, idx.column(), idx), role,
2292  (text.isEmpty() ? value : text),
2293  (allHits ? -1 : hits - result.count()), flags);
2294  }
2295  }
2296  // prepare for the next iteration
2297  from = 0;
2298  to = start.row();
2299  }
2300  return result;
2301 }
2302 
2310 {
2311  return QSize(1, 1);
2312 }
2313 
2330 {
2332  d->roleNames = roleNames;
2333 }
2334 
2346 {
2347  Q_D(const QAbstractItemModel);
2348  return d->roleNames;
2349 }
2350 
2361 {
2362  return true;
2363 }
2364 
2373 {
2374  // do nothing
2375 }
2376 
2388 QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const
2389 {
2390  Q_UNUSED(orientation);
2391  if (role == Qt::DisplayRole)
2392  return section + 1;
2393  return QVariant();
2394 }
2395 
2409  const QVariant &value, int role)
2410 {
2411  Q_UNUSED(section);
2412  Q_UNUSED(orientation);
2413  Q_UNUSED(value);
2414  Q_UNUSED(role);
2415  return false;
2416 }
2417 
2462 {
2464  for (; it != indexes.end(); ++it)
2465  stream << (*it).row() << (*it).column() << itemData(*it);
2466 }
2467 
2471 bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &parent,
2473 {
2474  int top = INT_MAX;
2475  int left = INT_MAX;
2476  int bottom = 0;
2477  int right = 0;
2478  QVector<int> rows, columns;
2480 
2481  while (!stream.atEnd()) {
2482  int r, c;
2484  stream >> r >> c >> v;
2485  rows.append(r);
2486  columns.append(c);
2487  data.append(v);
2488  top = qMin(r, top);
2489  left = qMin(c, left);
2490  bottom = qMax(r, bottom);
2491  right = qMax(c, right);
2492  }
2493 
2494  // insert the dragged items into the table, use a bit array to avoid overwriting items,
2495  // since items from different tables can have the same row and column
2496  int dragRowCount = 0;
2497  int dragColumnCount = right - left + 1;
2498 
2499  // Compute the number of continuous rows upon insertion and modify the rows to match
2500  QVector<int> rowsToInsert(bottom + 1);
2501  for (int i = 0; i < rows.count(); ++i)
2502  rowsToInsert[rows.at(i)] = 1;
2503  for (int i = 0; i < rowsToInsert.count(); ++i) {
2504  if (rowsToInsert[i] == 1){
2505  rowsToInsert[i] = dragRowCount;
2506  ++dragRowCount;
2507  }
2508  }
2509  for (int i = 0; i < rows.count(); ++i)
2510  rows[i] = top + rowsToInsert[rows[i]];
2511 
2512  QBitArray isWrittenTo(dragRowCount * dragColumnCount);
2513 
2514  // make space in the table for the dropped data
2515  int colCount = columnCount(parent);
2516  if (colCount == 0) {
2517  insertColumns(colCount, dragColumnCount - colCount, parent);
2518  colCount = columnCount(parent);
2519  }
2520  insertRows(row, dragRowCount, parent);
2521 
2522  row = qMax(0, row);
2523  column = qMax(0, column);
2524 
2525  QVector<QPersistentModelIndex> newIndexes(data.size());
2526  // set the data in the table
2527  for (int j = 0; j < data.size(); ++j) {
2528  int relativeRow = rows.at(j) - top;
2529  int relativeColumn = columns.at(j) - left;
2530  int destinationRow = relativeRow + row;
2531  int destinationColumn = relativeColumn + column;
2532  int flat = (relativeRow * dragColumnCount) + relativeColumn;
2533  // if the item was already written to, or we just can't fit it in the table, create a new row
2534  if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
2535  destinationColumn = qBound(column, destinationColumn, colCount - 1);
2536  destinationRow = row + dragRowCount;
2537  insertRows(row + dragRowCount, 1, parent);
2538  flat = (dragRowCount * dragColumnCount) + relativeColumn;
2539  isWrittenTo.resize(++dragRowCount * dragColumnCount);
2540  }
2541  if (!isWrittenTo.testBit(flat)) {
2542  newIndexes[j] = index(destinationRow, destinationColumn, parent);
2543  isWrittenTo.setBit(flat);
2544  }
2545  }
2546 
2547  for(int k = 0; k < newIndexes.size(); k++) {
2548  if (newIndexes.at(k).isValid())
2549  setItemData(newIndexes.at(k), data.at(k));
2550  }
2551 
2552  return true;
2553 }
2554 
2595 void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last)
2596 {
2597  Q_ASSERT(first >= 0);
2598  Q_ASSERT(last >= first);
2600  d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
2601  emit rowsAboutToBeInserted(parent, first, last);
2602  d->rowsAboutToBeInserted(parent, first, last);
2603 }
2604 
2614 {
2616  QAbstractItemModelPrivate::Change change = d->changes.pop();
2617  d->rowsInserted(change.parent, change.first, change.last);
2618  emit rowsInserted(change.parent, change.first, change.last);
2619 }
2620 
2649 void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, int last)
2650 {
2651  Q_ASSERT(first >= 0);
2652  Q_ASSERT(last >= first);
2654  d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
2655  emit rowsAboutToBeRemoved(parent, first, last);
2656  d->rowsAboutToBeRemoved(parent, first, last);
2657 }
2658 
2668 {
2670  QAbstractItemModelPrivate::Change change = d->changes.pop();
2671  d->rowsRemoved(change.parent, change.first, change.last);
2672  emit rowsRemoved(change.parent, change.first, change.last);
2673 }
2674 
2683 bool QAbstractItemModelPrivate::allowMove(const QModelIndex &srcParent, int start, int end, const QModelIndex &destinationParent, int destinationStart, Qt::Orientation orientation)
2684 {
2685  // Don't move the range within itself.
2686  if (destinationParent == srcParent)
2687  return !(destinationStart >= start && destinationStart <= end + 1);
2688 
2689  QModelIndex destinationAncestor = destinationParent;
2690  int pos = (Qt::Vertical == orientation) ? destinationAncestor.row() : destinationAncestor.column();
2691  forever {
2692  if (destinationAncestor == srcParent) {
2693  if (pos >= start && pos <= end)
2694  return false;
2695  break;
2696  }
2697 
2698  if (!destinationAncestor.isValid())
2699  break;
2700 
2701  pos = (Qt::Vertical == orientation) ? destinationAncestor.row() : destinationAncestor.column();
2702  destinationAncestor = destinationAncestor.parent();
2703  }
2704 
2705  return true;
2706 }
2707 
2803 bool QAbstractItemModel::beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
2804 {
2805  Q_ASSERT(sourceFirst >= 0);
2806  Q_ASSERT(sourceLast >= sourceFirst);
2807  Q_ASSERT(destinationChild >= 0);
2809 
2810  if (!d->allowMove(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical)) {
2811  return false;
2812  }
2813 
2814  QAbstractItemModelPrivate::Change sourceChange(sourceParent, sourceFirst, sourceLast);
2815  sourceChange.needsAdjust = sourceParent.isValid() && sourceParent.row() >= destinationChild && sourceParent.parent() == destinationParent;
2816  d->changes.push(sourceChange);
2817  int destinationLast = destinationChild + (sourceLast - sourceFirst);
2818  QAbstractItemModelPrivate::Change destinationChange(destinationParent, destinationChild, destinationLast);
2819  destinationChange.needsAdjust = destinationParent.isValid() && destinationParent.row() >= sourceLast && destinationParent.parent() == sourceParent;
2820  d->changes.push(destinationChange);
2821 
2822  emit rowsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild);
2824  d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical);
2825  return true;
2826 }
2827 
2842 {
2844 
2845  QAbstractItemModelPrivate::Change insertChange = d->changes.pop();
2846  QAbstractItemModelPrivate::Change removeChange = d->changes.pop();
2847 
2848  QModelIndex adjustedSource = removeChange.parent;
2849  QModelIndex adjustedDestination = insertChange.parent;
2850 
2851  const int numMoved = removeChange.last - removeChange.first + 1;
2852  if (insertChange.needsAdjust)
2853  adjustedDestination = createIndex(adjustedDestination.row() - numMoved, adjustedDestination.column(), adjustedDestination.internalPointer());
2854 
2855  if (removeChange.needsAdjust)
2856  adjustedSource = createIndex(adjustedSource.row() + numMoved, adjustedSource.column(), adjustedSource.internalPointer());
2857 
2858  d->itemsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, Qt::Vertical);
2859 
2860  emit rowsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first);
2861  emit layoutChanged();
2862 }
2863 
2906 {
2907  Q_ASSERT(first >= 0);
2908  Q_ASSERT(last >= first);
2910  d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
2911  emit columnsAboutToBeInserted(parent, first, last);
2912  d->columnsAboutToBeInserted(parent, first, last);
2913 }
2914 
2925 {
2927  QAbstractItemModelPrivate::Change change = d->changes.pop();
2928  d->columnsInserted(change.parent, change.first, change.last);
2929  emit columnsInserted(change.parent, change.first, change.last);
2930 }
2931 
2961 {
2962  Q_ASSERT(first >= 0);
2963  Q_ASSERT(last >= first);
2965  d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
2966  emit columnsAboutToBeRemoved(parent, first, last);
2967  d->columnsAboutToBeRemoved(parent, first, last);
2968 }
2969 
2979 {
2981  QAbstractItemModelPrivate::Change change = d->changes.pop();
2982  d->columnsRemoved(change.parent, change.first, change.last);
2983  emit columnsRemoved(change.parent, change.first, change.last);
2984 }
2985 
3026 bool QAbstractItemModel::beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
3027 {
3028  Q_ASSERT(sourceFirst >= 0);
3029  Q_ASSERT(sourceLast >= sourceFirst);
3030  Q_ASSERT(destinationChild >= 0);
3032 
3033  if (!d->allowMove(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Horizontal)) {
3034  return false;
3035  }
3036 
3037  QAbstractItemModelPrivate::Change sourceChange(sourceParent, sourceFirst, sourceLast);
3038  sourceChange.needsAdjust = sourceParent.isValid() && sourceParent.row() >= destinationChild && sourceParent.parent() == destinationParent;
3039  d->changes.push(sourceChange);
3040  int destinationLast = destinationChild + (sourceLast - sourceFirst);
3041  QAbstractItemModelPrivate::Change destinationChange(destinationParent, destinationChild, destinationLast);
3042  destinationChange.needsAdjust = destinationParent.isValid() && destinationParent.row() >= sourceLast && destinationParent.parent() == sourceParent;
3043  d->changes.push(destinationChange);
3044 
3045  d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Horizontal);
3046 
3047  emit columnsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild);
3049  return true;
3050 }
3051 
3066 {
3068 
3069  QAbstractItemModelPrivate::Change insertChange = d->changes.pop();
3070  QAbstractItemModelPrivate::Change removeChange = d->changes.pop();
3071 
3072  QModelIndex adjustedSource = removeChange.parent;
3073  QModelIndex adjustedDestination = insertChange.parent;
3074 
3075  const int numMoved = removeChange.last - removeChange.first + 1;
3076  if (insertChange.needsAdjust)
3077  adjustedDestination = createIndex(adjustedDestination.row(), adjustedDestination.column() - numMoved, adjustedDestination.internalPointer());
3078 
3079  if (removeChange.needsAdjust)
3080  adjustedSource = createIndex(adjustedSource.row(), adjustedSource.column() + numMoved, adjustedSource.internalPointer());
3081 
3082  d->itemsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, Qt::Horizontal);
3083 
3084  emit columnsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first);
3085  emit layoutChanged();
3086 }
3087 
3096 {
3099  d->invalidatePersistentIndexes();
3100  QMetaObject::invokeMethod(this, "resetInternalData");
3101  emit modelReset();
3102 }
3103 
3126 {
3128 }
3129 
3140 {
3142  d->invalidatePersistentIndexes();
3143  QMetaObject::invokeMethod(this, "resetInternalData");
3144  emit modelReset();
3145 }
3146 
3157 {
3159  if (d->persistent.indexes.isEmpty())
3160  return;
3161  // find the data and reinsert it sorted
3162  const QHash<QModelIndex, QPersistentModelIndexData *>::iterator it = d->persistent.indexes.find(from);
3163  if (it != d->persistent.indexes.end()) {
3165  d->persistent.indexes.erase(it);
3166  data->index = to;
3167  if (to.isValid())
3168  d->persistent.insertMultiAtEnd(to, data);
3169  else
3170  data->model = 0;
3171  }
3172 }
3173 
3189  const QModelIndexList &to)
3190 {
3192  if (d->persistent.indexes.isEmpty())
3193  return;
3194  QVector<QPersistentModelIndexData *> toBeReinserted;
3195  toBeReinserted.reserve(to.count());
3196  for (int i = 0; i < from.count(); ++i) {
3197  if (from.at(i) == to.at(i))
3198  continue;
3199  const QHash<QModelIndex, QPersistentModelIndexData *>::iterator it = d->persistent.indexes.find(from.at(i));
3200  if (it != d->persistent.indexes.end()) {
3202  d->persistent.indexes.erase(it);
3203  data->index = to.at(i);
3204  if (data->index.isValid())
3205  toBeReinserted << data;
3206  else
3207  data->model = 0;
3208  }
3209  }
3210 
3212  it != toBeReinserted.constEnd() ; ++it) {
3214  d->persistent.insertMultiAtEnd(data->index, data);
3215  }
3216 }
3217 
3227 {
3228  Q_D(const QAbstractItemModel);
3229  QModelIndexList result;
3230  for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = d->persistent.indexes.constBegin();
3231  it != d->persistent.indexes.constEnd(); ++it) {
3233  result.append(data->index);
3234  }
3235  return result;
3236 }
3237 
3238 
3309  : QAbstractItemModel(parent)
3310 {
3311 
3312 }
3313 
3324  : QAbstractItemModel(dd, parent)
3325 {
3326 
3327 }
3328 
3334 {
3335 
3336 }
3337 
3349 QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent) const
3350 {
3351  return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
3352 }
3353 
3366 {
3367  return QModelIndex();
3368 }
3369 
3371 {
3372  if (parent.model() == this || !parent.isValid())
3373  return rowCount(parent) > 0 && columnCount(parent) > 0;
3374  return false;
3375 }
3376 
3449  : QAbstractItemModel(parent)
3450 {
3451 
3452 }
3453 
3464  : QAbstractItemModel(dd, parent)
3465 {
3466 
3467 }
3468 
3474 {
3475 
3476 }
3477 
3489 QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent) const
3490 {
3491  return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
3492 }
3493 
3501 {
3502  return QModelIndex();
3503 }
3504 
3517 {
3518  return parent.isValid() ? 0 : 1;
3519 }
3520 
3522 {
3523  return parent.isValid() ? false : (rowCount() > 0);
3524 }
3525 
3540  int row, int column, const QModelIndex &parent)
3541 {
3542  if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
3543  return false;
3544 
3546  if (types.isEmpty())
3547  return false;
3548  QString format = types.at(0);
3549  if (!data->hasFormat(format))
3550  return false;
3551 
3552  QByteArray encoded = data->data(format);
3554 
3555  // if the drop is on an item, replace the data in the items
3556  if (parent.isValid() && row == -1 && column == -1) {
3557  int top = INT_MAX;
3558  int left = INT_MAX;
3559  QVector<int> rows, columns;
3561 
3562  while (!stream.atEnd()) {
3563  int r, c;
3565  stream >> r >> c >> v;
3566  rows.append(r);
3567  columns.append(c);
3568  data.append(v);
3569  top = qMin(r, top);
3570  left = qMin(c, left);
3571  }
3572 
3573  for (int i = 0; i < data.size(); ++i) {
3574  int r = (rows.at(i) - top) + parent.row();
3575  int c = (columns.at(i) - left) + parent.column();
3576  if (hasIndex(r, c))
3577  setItemData(index(r, c), data.at(i));
3578  }
3579 
3580  return true;
3581  }
3582 
3583  // otherwise insert new rows for the data
3584  return decodeData(row, column, parent, stream);
3585 }
3586 
3591  int row, int column, const QModelIndex &parent)
3592 {
3593  if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
3594  return false;
3595 
3597  if (types.isEmpty())
3598  return false;
3599  QString format = types.at(0);
3600  if (!data->hasFormat(format))
3601  return false;
3602 
3603  QByteArray encoded = data->data(format);
3605 
3606  // if the drop is on an item, replace the data in the items
3607  if (parent.isValid() && row == -1 && column == -1) {
3608  int top = INT_MAX;
3609  int left = INT_MAX;
3610  QVector<int> rows, columns;
3612 
3613  while (!stream.atEnd()) {
3614  int r, c;
3616  stream >> r >> c >> v;
3617  rows.append(r);
3618  columns.append(c);
3619  data.append(v);
3620  top = qMin(r, top);
3621  left = qMin(c, left);
3622  }
3623 
3624  for (int i = 0; i < data.size(); ++i) {
3625  int r = (rows.at(i) - top) + parent.row();
3626  if (columns.at(i) == left && hasIndex(r, 0))
3627  setItemData(index(r), data.at(i));
3628  }
3629 
3630  return true;
3631  }
3632 
3633  if (row == -1)
3634  row = rowCount(parent);
3635 
3636  // otherwise insert new rows for the data
3637  return decodeData(row, column, parent, stream);
3638 }
3639 
3699 {
3701  indexes.insertMulti(key, data);
3703  while (it != indexes.end() && it.key() == key) {
3704  qSwap(*newIt,*it);
3705  newIt = it;
3706  ++it;
3707  }
3708 }
3709 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last)
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
Returns a list of indexes for the items in the column of the start index where data stored under the ...
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of columns for the children of the given parent.
void * internalPointer() const
Returns a void * pointer used by the model to associate the index with the internal data structure...
int row() const
Returns the row this persistent model index refers to.
virtual QSize span(const QModelIndex &index) const
Returns the row and column span of the item represented by index.
bool hasChildren(const QModelIndex &) const
Returns true if parent has any children; otherwise returns false.
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
Definition: qmimedata.cpp:547
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
int columnCount(const QModelIndex &parent) const
Returns the number of columns in the list with the given parent.
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Handles the data supplied by a drag and drop operation that ended with the given action.
unsigned char c[8]
Definition: qnumeric_p.h:62
QModelIndexList persistentIndexList() const
Returns the list of indexes stored as persistent indexes in the model.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
void setBit(int i)
Sets the bit at index position i to 1.
Definition: qbitarray.h:128
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual QMap< int, QVariant > itemData(const QModelIndex &index) const
Returns a map with values for all predefined roles in the model for the item at the given index...
void beginInsertColumns(const QModelIndex &parent, int first, int last)
Begins a column insertion operation.
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
QModelIndex index(int, int, const QModelIndex &) const
Returns the index of the item in the model specified by the given row, column and parent index...
static void destroy(QPersistentModelIndexData *data)
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column.
QModelIndex parent(const QModelIndex &) const
Returns the parent of the model item with the given index.
#define it(className, varName)
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
void columnsRemoved(const QModelIndex &parent, int first, int last)
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
int column() const
Returns the column this persistent model index refers to.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
bool atEnd() const
Returns true if the I/O device has reached the end position (end of the stream or file) or if there i...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the vector...
Definition: qvector.h:252
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
void beginRemoveColumns(const QModelIndex &parent, int first, int last)
Begins a column removal operation.
virtual bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles)
Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole...
void columnsInserted(const QModelIndex &parent, int first, int last)
This signal is emitted after columns have been inserted into the model.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
QModelIndex createIndex(int row, int column, void *data=0) const
Creates a model index for the given row and column with the internal pointer ptr. ...
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
virtual QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of model indexes.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
virtual Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this model.
int rowCount(const QModelIndex &) const
Returns the number of rows under the given parent.
bool hasIndex(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns true if the model returns a valid QModelIndex for row and column with parent, otherwise returns false.
QByteArray data(const QString &mimetype) const
Returns the data stored in the object in the format described by the MIME type specified by mimeType...
Definition: qmimedata.cpp:524
bool ref()
Atomically increments the value of this QAtomicInt.
void endInsertRows()
Ends a row insertion operation.
void columnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
This signal is emitted just before columns are moved within the model.
static bool variantLessThan(const QVariant &v1, const QVariant &v2)
This function is used by our Q{Tree,Widget,Table}WidgetModel classes to sort.
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column or an invalid QModelIndex if there is no sibling at this positi...
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
bool allowMove(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation)
Returns whether a move operation is valid.
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void rowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
This signal is emitted just before rows are moved within the model.
void columnsInserted(const QModelIndex &parent, int first, int last)
#define Q_D(Class)
Definition: qglobal.h:2482
virtual bool submit()
Lets the model know that it should submit cached information to permanent storage.
void endMoveRows()
Ends a row move operation.
bool decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream)
void endResetModel()
Completes a model reset operation.
static uint typeOfVariant(const QVariant &value)
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
void insertMultiAtEnd(const QModelIndex &key, QPersistentModelIndexData *data)
QHash::insertMulti insert the value before the old value.
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns the data for the given role and section in the header with the specified orientation.
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QModelIndex index(int row, int column=0, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
QVariant data(const QModelIndex &, int) const
Returns the data stored under the given role for the item referred to by the index.
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list t...
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
int columnCount(const QModelIndex &) const
Returns the number of columns for the children of the given parent.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
#define Q_Q(Class)
Definition: qglobal.h:2483
static QPersistentModelIndexData * create(const QModelIndex &index)
virtual void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Sorts the model by column in the given order.
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const =0
Returns the index of the item in the model specified by the given row, column and parent index...
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:249
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last)
This signal is emitted just before columns are inserted into the model.
bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn)
Begins a column move operation.
SortOrder
Definition: qnamespace.h:189
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
const QAbstractItemModel * model
static FILE * stream
void rowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
This signal is emitted after rows have been moved within the model.
qint64 internalId() const
Returns a qint64 used by the model to associate the index with the internal data structure.
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
~QAbstractTableModel()
Destroys the abstract table model.
virtual bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Returns true if parent has any children; otherwise returns false.
void resetInternalData()
This slot is called just after the internal data of a model is cleared while it is being reset...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Reimplemented Function
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last)
This signal is emitted just before rows are inserted into the model.
const QAbstractItemModel * model() const
Returns the model that the index belongs to.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
int row() const
Returns the row this model index refers to.
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
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
Qt::DropActions supportedDragActions() const
Returns the actions supported by the data in this model.
bool deref()
Atomically decrements the value of this QAtomicInt.
const QAbstractItemModel * model() const
Returns a pointer to the model containing the item that this index refers to.
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
On models that support this, removes count rows starting with the given row under parent parent from ...
void resize(int size)
Resizes the bit array to size bits.
Definition: qbitarray.cpp:214
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
void modelAboutToBeReset()
This signal is emitted when reset() is called, before the model&#39;s internal state (e.
QModelIndex parent() const
Returns the parent QModelIndex for this persistent index, or an invalid QModelIndex if it has no pare...
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
virtual QModelIndex buddy(const QModelIndex &index) const
Returns a model index for the buddy of the item represented by index.
void encodeData(const QModelIndexList &indexes, QDataStream &stream) const
void removePersistentIndexData(QPersistentModelIndexData *data)
QPersistentModelIndexData * d
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const =0
Returns the data stored under the given role for the item referred to by the index.
DropAction
Definition: qnamespace.h:1597
void endRemoveRows()
Ends a row removal operation.
virtual bool canFetchMore(const QModelIndex &parent) const
Returns true if there is more data available for parent; otherwise returns false. ...
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
__int64 qint64
Definition: qglobal.h:942
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
virtual bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
On models that support this, inserts count rows into the model before the given row.
int localeAwareCompare(const QString &s) const
Definition: qstring.cpp:5197
QPersistentModelIndex & operator=(const QPersistentModelIndex &other)
Sets the persistent model index to refer to the same item in a model as the other persistent model in...
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QAbstractItemModel class provides the abstract interface for item model classes.
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
Sets the data for the given role and section in the header with the specified orientation to the valu...
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &)
Qt::ItemFlags flags() const
Returns the flags for the item referred to by the index.
QAbstractTableModel(QObject *parent=0)
Constructs an abstract table model for the given parent.
iterator begin()
Returns an STL-style iterator pointing to the first item in the map.
Definition: qmap.h:372
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted just before columns are removed from the model.
virtual void fetchMore(const QModelIndex &parent)
Fetches any available data for the items with the parent specified by the parent index.
QModelIndex child(int row, int column) const
Returns the child of the model index that is stored in the given row and column.
virtual bool hasFormat(const QString &mimetype) const
Returns true if the object can return data for the MIME type specified by mimeType; otherwise returns...
Definition: qmimedata.cpp:563
CaseSensitivity
Definition: qnamespace.h:1451
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
const QHash< int, QByteArray > & roleNames() const
Returns the model&#39;s role names.
QAbstractListModel(QObject *parent=0)
Constructs an abstract list model with the given parent.
int compare(const QString &s) const
Definition: qstring.cpp:5037
void changePersistentIndex(const QModelIndex &from, const QModelIndex &to)
Changes the QPersistentModelIndex that is equal to the given from model index to the given to model i...
void itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation)
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
static const struct @32 types[]
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last)
The QPersistentModelIndex class is used to locate data in a data model.
void rowsInserted(const QModelIndex &parent, int first, int last)
This signal is emitted after rows have been inserted into the model.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
bool operator<(const QPersistentModelIndex &other) const
Returns true if this persistent model index is smaller than the other persistent model index; otherwi...
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
bool operator!=(const QPersistentModelIndex &other) const
Returns true if this persistent model index is not equal to the other persistent model index; otherwi...
QEmptyItemModel(QObject *parent=0)
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
const_iterator ConstIterator
Qt-style synonym for QList::const_iterator.
Definition: qlist.h:279
int key
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
Definition: qglobal.h:1219
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
The QModelIndex class is used to locate data in a data model.
void rowsInserted(const QModelIndex &parent, int first, int last)
Qt::ItemFlags flags() const
Returns the flags for the item referred to by the index.
virtual ~QAbstractItemModel()
Destroys the abstract item model.
void modelReset()
This signal is emitted when reset() is called, after the model&#39;s internal state (e.
void setSupportedDragActions(Qt::DropActions)
Sets the supported drag actions for the items in the model.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Reimplemented Function
virtual bool insertColumns(int column, int count, const QModelIndex &parent=QModelIndex())
On models that support this, inserts count new columns into the model before the given column...
void endInsertColumns()
Ends a column insertion operation.
quint16 index
bool hasChildren(const QModelIndex &parent) const
Returns true if parent has any children; otherwise returns false.
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
~QAbstractListModel()
Destroys the abstract list model.
QAbstractItemModel(QObject *parent=0)
Constructs an abstract item model with the given parent.
bool operator==(const QPersistentModelIndex &other) const
Returns true if this persistent model index is equal to the other persistent model index; otherwise r...
const T * const_iterator
The QVector::const_iterator typedef provides an STL-style const iterator for QVector and QStack...
Definition: qvector.h:245
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
void setRoleNames(const QHash< int, QByteArray > &roleNames)
Sets the model&#39;s role names to roleNames.
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
void itemsMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation)
void endMoveColumns()
Ends a column move operation.
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
static const KeyPair *const end
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted just before rows are removed from the model.
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
Orientation
Definition: qnamespace.h:174
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
virtual void revert()
Lets the model know that it should discard cached information.
QModelIndex child(int row, int column) const
Returns the child of the model index that is stored in the given row and column.
void rowsRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted after rows have been removed from the model.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
void columnsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column)
This signal is emitted after columns have been moved within the model.
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow)
Begins a row move operation.
void columnsRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted after columns have been removed from the model.
void beginResetModel()
Begins a model reset operation.
#define INT_MAX
void rowsRemoved(const QModelIndex &parent, int first, int last)
virtual bool removeColumns(int column, int count, const QModelIndex &parent=QModelIndex())
On models that support this, removes count columns starting with the given column under parent parent...
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
qreal toReal(bool *ok=0) const
Returns the variant as a qreal if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2740
void reset()
Resets the model to its original state in any attached views.
#define text
Definition: qobjectdefs.h:80
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
int column() const
Returns the column this model index refers to.
bool isValid() const
Returns true if this persistent model index is valid; otherwise returns false.
void endRemoveColumns()
Ends a column removal operation.
bool hasChildren(const QModelIndex &parent) const
Returns true if parent has any children; otherwise returns false.
void movePersistentIndexes(QVector< QPersistentModelIndexData *> indexes, int change, const QModelIndex &parent, Qt::Orientation orientation)
Moves persistent indexes indexes by amount change.
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452