Qt 4.8
Functions
qitemselectionmodel.cpp File Reference
#include "qitemselectionmodel.h"
#include <private/qitemselectionmodel_p.h>
#include <qdebug.h>
#include "moc_qitemselectionmodel.cpp"

Go to the source code of this file.

Functions

static void indexesFromRange (const QItemSelectionRange &range, QModelIndexList &result)
 
static QItemSelection mergeIndexes (const QList< QPersistentModelIndex > &indexes)
 Merges indexes into an item selection made up of ranges. More...
 
QDebug operator<< (QDebug dbg, const QItemSelectionRange &range)
 

Function Documentation

◆ indexesFromRange()

static void indexesFromRange ( const QItemSelectionRange range,
QModelIndexList result 
)
static

Definition at line 347 of file qitemselectionmodel.cpp.

Referenced by QItemSelectionRange::indexes(), and QItemSelection::indexes().

348 {
349  if (range.isValid() && range.model()) {
350  for (int column = range.left(); column <= range.right(); ++column) {
351  for (int row = range.top(); row <= range.bottom(); ++row) {
352  QModelIndex index = range.model()->index(row, column, range.parent());
353  Qt::ItemFlags flags = range.model()->flags(index);
354  if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled))
355  result.append(index);
356  }
357  }
358  }
359 }
int left() const
Returns the column index corresponding to the leftmost selected column in the selection range...
bool isValid() const
Returns true if the selection range is valid; otherwise returns false.
int bottom() const
Returns the row index corresponding to the lowermost selected row in the selection range...
int right() const
Returns the column index corresponding to the rightmost selected column in the selection range...
QModelIndex parent() const
Returns the parent model item index of the items in the selection range.
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...
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QModelIndex class is used to locate data in a data model.
quint16 index
int top() const
Returns the row index corresponding to the uppermost selected row in the selection range...
const QAbstractItemModel * model() const
Returns the model that the items in the selection range belong to.

◆ mergeIndexes()

static QItemSelection mergeIndexes ( const QList< QPersistentModelIndex > &  indexes)
static

Merges indexes into an item selection made up of ranges.

Warning
This function is not part of the public interface.

Assumes that the indexes are sorted.

Definition at line 902 of file qitemselectionmodel.cpp.

Referenced by QItemSelectionModelPrivate::_q_layoutChanged().

903 {
904  QItemSelection colSpans;
905  // merge columns
906  int i = 0;
907  while (i < indexes.count()) {
908  QModelIndex tl = indexes.at(i);
909  QModelIndex br = tl;
910  while (++i < indexes.count()) {
911  QModelIndex next = indexes.at(i);
912  if ((next.parent() == br.parent())
913  && (next.row() == br.row())
914  && (next.column() == br.column() + 1))
915  br = next;
916  else
917  break;
918  }
919  colSpans.append(QItemSelectionRange(tl, br));
920  }
921  // merge rows
922  QItemSelection rowSpans;
923  i = 0;
924  while (i < colSpans.count()) {
925  QModelIndex tl = colSpans.at(i).topLeft();
926  QModelIndex br = colSpans.at(i).bottomRight();
927  QModelIndex prevTl = tl;
928  while (++i < colSpans.count()) {
929  QModelIndex nextTl = colSpans.at(i).topLeft();
930  QModelIndex nextBr = colSpans.at(i).bottomRight();
931 
932  if (nextTl.parent() != tl.parent())
933  break; // we can't merge selection ranges from different parents
934 
935  if ((nextTl.column() == prevTl.column()) && (nextBr.column() == br.column())
936  && (nextTl.row() == prevTl.row() + 1) && (nextBr.row() == br.row() + 1)) {
937  br = nextBr;
938  prevTl = nextTl;
939  } else {
940  break;
941  }
942  }
943  rowSpans.append(QItemSelectionRange(tl, br));
944  }
945  return rowSpans;
946 }
QModelIndex bottomRight() const
Returns the index for the item located at the bottom-right corner of the selection range...
The QItemSelectionRange class manages information about a range of selected items in a model...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
QModelIndex topLeft() const
Returns the index for the item located at the top-left corner of the selection range.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
int row() const
Returns the row this model index refers to.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QItemSelection class manages information about selected items in a model.
The QModelIndex class is used to locate data in a data model.
int column() const
Returns the column this model index refers to.

◆ operator<<()

QDebug operator<< ( QDebug  dbg,
const QItemSelectionRange range 
)

Definition at line 1721 of file qitemselectionmodel.cpp.

1722 {
1723 #ifndef Q_BROKEN_DEBUG_STREAM
1724  dbg.nospace() << "QItemSelectionRange(" << range.topLeft()
1725  << ',' << range.bottomRight() << ')';
1726  return dbg.space();
1727 #else
1728  qWarning("This compiler doesn't support streaming QItemSelectionRange to QDebug");
1729  return dbg;
1730  Q_UNUSED(range);
1731 #endif
1732 }
QModelIndex bottomRight() const
Returns the index for the item located at the bottom-right corner of the selection range...
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
QModelIndex topLeft() const
Returns the index for the item located at the top-left corner of the selection range.
Q_CORE_EXPORT void qWarning(const char *,...)
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
#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