Qt 4.8
Public Functions | Properties | List of all members
QItemSelectionRange Class Reference

The QItemSelectionRange class manages information about a range of selected items in a model. More...

#include <qitemselectionmodel.h>

Public Functions

int bottom () const
 Returns the row index corresponding to the lowermost selected row in the selection range. More...
 
QModelIndex bottomRight () const
 Returns the index for the item located at the bottom-right corner of the selection range. More...
 
bool contains (const QModelIndex &index) const
 Returns true if the model item specified by the index lies within the range of selected items; otherwise returns false. More...
 
bool contains (int row, int column, const QModelIndex &parentIndex) const
 Returns true if the model item specified by (row, column) and with parentIndex as the parent item lies within the range of selected items; otherwise returns false. More...
 
int height () const
 Returns the number of selected rows in the selection range. More...
 
QModelIndexList indexes () const
 Returns the list of model index items stored in the selection. More...
 
QItemSelectionRange intersect (const QItemSelectionRange &other) const
 Use intersected(other) instead. More...
 
QItemSelectionRange intersected (const QItemSelectionRange &other) const
 Returns a new selection range containing only the items that are found in both the selection range and the other selection range. More...
 
bool intersects (const QItemSelectionRange &other) const
 Returns true if this selection range intersects (overlaps with) the other range given; otherwise returns false. More...
 
bool isEmpty () const
 Returns true if the selection range contains no selectable item. More...
 
bool isValid () const
 Returns true if the selection range is valid; otherwise returns false. More...
 
int left () const
 Returns the column index corresponding to the leftmost selected column in the selection range. More...
 
const QAbstractItemModelmodel () const
 Returns the model that the items in the selection range belong to. More...
 
bool operator!= (const QItemSelectionRange &other) const
 Returns true if the selection range differs from the other range given; otherwise returns false. More...
 
bool operator< (const QItemSelectionRange &other) const
 
bool operator== (const QItemSelectionRange &other) const
 Returns true if the selection range is exactly the same as the other range given; otherwise returns false. More...
 
QModelIndex parent () const
 Returns the parent model item index of the items in the selection range. More...
 
 QItemSelectionRange ()
 Constructs an empty selection range. More...
 
 QItemSelectionRange (const QItemSelectionRange &other)
 Copy constructor. More...
 
 QItemSelectionRange (const QModelIndex &topLeft, const QModelIndex &bottomRight)
 Constructs a new selection range containing only the index specified by the topLeft and the index bottomRight. More...
 
 QItemSelectionRange (const QModelIndex &index)
 Constructs a new selection range containing only the model item specified by the model index index. More...
 
int right () const
 Returns the column index corresponding to the rightmost selected column in the selection range. More...
 
int top () const
 Returns the row index corresponding to the uppermost selected row in the selection range. More...
 
QModelIndex topLeft () const
 Returns the index for the item located at the top-left corner of the selection range. More...
 
int width () const
 Returns the number of selected columns in the selection range. More...
 

Properties

QPersistentModelIndex br
 
QPersistentModelIndex tl
 

Detailed Description

The QItemSelectionRange class manages information about a range of selected items in a model.

A QItemSelectionRange contains information about a range of selected items in a model. A range of items is a contiguous array of model items, extending to cover a number of adjacent rows and columns with a common parent item; this can be visualized as a two-dimensional block of cells in a table. A selection range has a top(), left() a bottom(), right() and a parent().

The QItemSelectionRange class is one of the Model/View Classes and is part of Qt's Model/View Programming{model/view framework}.

The model items contained in the selection range can be obtained using the indexes() function. Use QItemSelectionModel::selectedIndexes() to get a list of all selected items for a view.

You can determine whether a given model item lies within a particular range by using the contains() function. Ranges can also be compared using the overloaded operators for equality and inequality, and the intersects() function allows you to determine whether two ranges overlap.

See also
{Model/View Programming}, QAbstractItemModel, QItemSelection, QItemSelectionModel

Definition at line 58 of file qitemselectionmodel.h.

Constructors and Destructors

◆ QItemSelectionRange() [1/4]

QItemSelectionRange::QItemSelectionRange ( )
inline

◆ QItemSelectionRange() [2/4]

QItemSelectionRange::QItemSelectionRange ( const QItemSelectionRange other)
inline

Copy constructor.

Constructs a new selection range with the same contents as the other range given.

Definition at line 63 of file qitemselectionmodel.h.

64  : tl(other.tl), br(other.br) {}
QPersistentModelIndex br
QPersistentModelIndex tl

◆ QItemSelectionRange() [3/4]

QItemSelectionRange::QItemSelectionRange ( const QModelIndex topLeft,
const QModelIndex bottomRight 
)
inline

Constructs a new selection range containing only the index specified by the topLeft and the index bottomRight.

Definition at line 144 of file qitemselectionmodel.h.

146 { tl = atopLeft; br = abottomRight; }
QPersistentModelIndex br
QPersistentModelIndex tl

◆ QItemSelectionRange() [4/4]

QItemSelectionRange::QItemSelectionRange ( const QModelIndex index)
inlineexplicit

Constructs a new selection range containing only the model item specified by the model index index.

Definition at line 66 of file qitemselectionmodel.h.

67  { tl = index; br = tl; }
QPersistentModelIndex br
QPersistentModelIndex tl
quint16 index

Functions

◆ bottom()

int QItemSelectionRange::bottom ( ) const
inline

◆ bottomRight()

QModelIndex QItemSelectionRange::bottomRight ( ) const
inline

Returns the index for the item located at the bottom-right corner of the selection range.

See also
bottom(), right(), topLeft()

Definition at line 77 of file qitemselectionmodel.h.

Referenced by intersect(), mergeIndexes(), QTreeView::moveCursor(), operator<<(), QTreeViewPrivate::select(), QListView::visualRegionForSelection(), and QTreeView::visualRegionForSelection().

77 { return QModelIndex(br); }
QPersistentModelIndex br
The QModelIndex class is used to locate data in a data model.

◆ contains() [1/2]

bool QItemSelectionRange::contains ( const QModelIndex index) const
inline

Returns true if the model item specified by the index lies within the range of selected items; otherwise returns false.

Definition at line 81 of file qitemselectionmodel.h.

Referenced by QTableView::visualRegionForSelection().

82  {
83  return (parent() == index.parent()
84  && tl.row() <= index.row() && tl.column() <= index.column()
85  && br.row() >= index.row() && br.column() >= index.column());
86  }
int row() const
Returns the row this persistent model index refers to.
int column() const
Returns the column this persistent model index refers to.
QPersistentModelIndex br
QModelIndex parent() const
Returns the parent model item index of the items in the selection range.
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
QPersistentModelIndex tl
int row() const
Returns the row this model index refers to.
int column() const
Returns the column this model index refers to.

◆ contains() [2/2]

bool QItemSelectionRange::contains ( int  row,
int  column,
const QModelIndex parentIndex 
) const
inline

Returns true if the model item specified by (row, column) and with parentIndex as the parent item lies within the range of selected items; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 88 of file qitemselectionmodel.h.

89  {
90  return (parent() == parentIndex
91  && tl.row() <= row && tl.column() <= column
92  && br.row() >= row && br.column() >= column);
93  }
int row() const
Returns the row this persistent model index refers to.
int column() const
Returns the column this persistent model index refers to.
QPersistentModelIndex br
QModelIndex parent() const
Returns the parent model item index of the items in the selection range.
QPersistentModelIndex tl

◆ height()

int QItemSelectionRange::height ( ) const
inline

Returns the number of selected rows in the selection range.

Definition at line 74 of file qitemselectionmodel.h.

Referenced by QTableView::visualRegionForSelection().

74 { return br.row() - tl.row() + 1; }
int row() const
Returns the row this persistent model index refers to.
QPersistentModelIndex br
QPersistentModelIndex tl

◆ indexes()

QModelIndexList QItemSelectionRange::indexes ( ) const

Returns the list of model index items stored in the selection.

Definition at line 386 of file qitemselectionmodel.cpp.

Referenced by QItemSelectionModelPrivate::_q_layoutAboutToBeChanged().

387 {
388  QModelIndexList result;
389  indexesFromRange(*this, result);
390  return result;
391 }
static void indexesFromRange(const QItemSelectionRange &range, QModelIndexList &result)

◆ intersect()

QItemSelectionRange QItemSelectionRange::intersect ( const QItemSelectionRange other) const

Use intersected(other) instead.

Definition at line 294 of file qitemselectionmodel.cpp.

295 {
296  if (model() == other.model() && parent() == other.parent()) {
297  QModelIndex topLeft = model()->index(qMax(top(), other.top()),
298  qMax(left(), other.left()),
299  other.parent());
301  qMin(right(), other.right()),
302  other.parent());
303  return QItemSelectionRange(topLeft, bottomRight);
304  }
305  return QItemSelectionRange();
306 }
int left() const
Returns the column index corresponding to the leftmost selected column in the selection range...
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QModelIndex bottomRight() const
Returns the index for the item located at the bottom-right corner of the selection range...
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.
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QModelIndex topLeft() const
Returns the index for the item located at the top-left corner of the selection range.
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...
QItemSelectionRange()
Constructs an empty selection range.
The QModelIndex class is used to locate data in a data model.
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.

◆ intersected()

QItemSelectionRange QItemSelectionRange::intersected ( const QItemSelectionRange other) const
inline

Returns a new selection range containing only the items that are found in both the selection range and the other selection range.

Since
4.2

Definition at line 97 of file qitemselectionmodel.h.

Referenced by QItemSelectionModel::emitSelectionChanged(), and QItemSelection::merge().

98  { return intersect(other); }
QItemSelectionRange intersect(const QItemSelectionRange &other) const
Use intersected(other) instead.

◆ intersects()

bool QItemSelectionRange::intersects ( const QItemSelectionRange other) const

Returns true if this selection range intersects (overlaps with) the other range given; otherwise returns false.

Definition at line 262 of file qitemselectionmodel.cpp.

Referenced by QItemSelectionModel::emitSelectionChanged(), and QItemSelection::merge().

263 {
264  return (isValid() && other.isValid()
265  && parent() == other.parent()
266  && model() == other.model()
267  && ((top() <= other.top() && bottom() >= other.top())
268  || (top() >= other.top() && top() <= other.bottom()))
269  && ((left() <= other.left() && right() >= other.left())
270  || (left() >= other.left() && left() <= other.right())));
271 }
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.
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.

◆ isEmpty()

bool QItemSelectionRange::isEmpty ( ) const

Returns true if the selection range contains no selectable item.

Since
4.7

Definition at line 366 of file qitemselectionmodel.cpp.

Referenced by QTableView::setSelection().

367 {
368  if (!isValid() || !model())
369  return true;
370 
371  for (int column = left(); column <= right(); ++column) {
372  for (int row = top(); row <= bottom(); ++row) {
373  QModelIndex index = model()->index(row, column, parent());
374  Qt::ItemFlags flags = model()->flags(index);
375  if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled))
376  return false;
377  }
378  }
379  return true;
380 }
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...
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.

◆ isValid()

bool QItemSelectionRange::isValid ( ) const
inline

Returns true if the selection range is valid; otherwise returns false.

Definition at line 129 of file qitemselectionmodel.h.

Referenced by indexesFromRange(), intersects(), isEmpty(), QTreeViewPrivate::select(), QItemSelectionModel::selection(), QTableViewPrivate::trimHiddenSelections(), QTableView::visualRegionForSelection(), QListView::visualRegionForSelection(), QTreeView::visualRegionForSelection(), and QHeaderView::visualRegionForSelection().

130  {
131  return (tl.isValid() && br.isValid() && tl.parent() == br.parent()
132  && top() <= bottom() && left() <= right());
133  }
int left() const
Returns the column index corresponding to the leftmost selected column in the selection range...
QPersistentModelIndex br
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...
QPersistentModelIndex tl
QModelIndex parent() const
Returns the parent QModelIndex for this persistent index, or an invalid QModelIndex if it has no pare...
int top() const
Returns the row index corresponding to the uppermost selected row in the selection range...
bool isValid() const
Returns true if this persistent model index is valid; otherwise returns false.

◆ left()

int QItemSelectionRange::left ( ) const
inline

◆ model()

const QAbstractItemModel * QItemSelectionRange::model ( ) const
inline

◆ operator!=()

bool QItemSelectionRange::operator!= ( const QItemSelectionRange other) const
inline

Returns true if the selection range differs from the other range given; otherwise returns false.

Definition at line 102 of file qitemselectionmodel.h.

103  { return !operator==(other); }
bool operator==(const QItemSelectionRange &other) const
Returns true if the selection range is exactly the same as the other range given; otherwise returns f...

◆ operator<()

bool QItemSelectionRange::operator< ( const QItemSelectionRange other) const
inline

Definition at line 104 of file qitemselectionmodel.h.

105  {
106  // Comparing parents will compare the models, but if two equivalent ranges
107  // in two different models have invalid parents, they would appear the same
108  if (other.tl.model() == tl.model()) {
109  // parent has to be calculated, so we only do so once.
110  const QModelIndex topLeftParent = tl.parent();
111  const QModelIndex otherTopLeftParent = other.tl.parent();
112  if (topLeftParent == otherTopLeftParent) {
113  if (other.tl.row() == tl.row()) {
114  if (other.tl.column() == tl.column()) {
115  if (other.br.row() == br.row()) {
116  return br.column() < other.br.column();
117  }
118  return br.row() < other.br.row();
119  }
120  return tl.column() < other.tl.column();
121  }
122  return tl.row() < other.tl.row();
123  }
124  return topLeftParent < otherTopLeftParent;
125  }
126  return tl.model() < other.tl.model();
127  }
int row() const
Returns the row this persistent model index refers to.
int column() const
Returns the column this persistent model index refers to.
QPersistentModelIndex br
QPersistentModelIndex tl
const QAbstractItemModel * model() const
Returns the model that the index belongs to.
QModelIndex parent() const
Returns the parent QModelIndex for this persistent index, or an invalid QModelIndex if it has no pare...
The QModelIndex class is used to locate data in a data model.

◆ operator==()

bool QItemSelectionRange::operator== ( const QItemSelectionRange other) const
inline

Returns true if the selection range is exactly the same as the other range given; otherwise returns false.

Definition at line 100 of file qitemselectionmodel.h.

101  { return (tl == other.tl && br == other.br); }
QPersistentModelIndex br
QPersistentModelIndex tl

◆ parent()

QModelIndex QItemSelectionRange::parent ( ) const
inline

◆ right()

int QItemSelectionRange::right ( ) const
inline

◆ top()

int QItemSelectionRange::top ( ) const
inline

◆ topLeft()

QModelIndex QItemSelectionRange::topLeft ( ) const
inline

Returns the index for the item located at the top-left corner of the selection range.

See also
top(), left(), bottomRight()

Definition at line 76 of file qitemselectionmodel.h.

Referenced by intersect(), mergeIndexes(), QTreeView::moveCursor(), operator<<(), QListView::visualRegionForSelection(), and QTreeView::visualRegionForSelection().

76 { return QModelIndex(tl); }
QPersistentModelIndex tl
The QModelIndex class is used to locate data in a data model.

◆ width()

int QItemSelectionRange::width ( ) const
inline

Returns the number of selected columns in the selection range.

Definition at line 73 of file qitemselectionmodel.h.

Referenced by QTableView::visualRegionForSelection().

73 { return br.column() - tl.column() + 1; }
int column() const
Returns the column this persistent model index refers to.
QPersistentModelIndex br
QPersistentModelIndex tl

Properties

◆ br

QPersistentModelIndex QItemSelectionRange::br
private

◆ tl

QPersistentModelIndex QItemSelectionRange::tl
private

The documentation for this class was generated from the following files: