43 #include <private/qitemselectionmodel_p.h> 46 #ifndef QT_NO_ITEMVIEWS 350 for (
int column = range.
left(); column <= range.
right(); ++column) {
351 for (
int row = range.
top(); row <= range.
bottom(); ++row) {
353 Qt::ItemFlags flags = range.
model()->
flags(index);
371 for (
int column =
left(); column <=
right(); ++column) {
372 for (
int row =
top(); row <=
bottom(); ++row) {
453 select(topLeft, bottomRight);
470 qWarning(
"Can't select indexes from different model or with different parents");
495 for (; it !=
end(); ++
it)
496 if ((*it).contains(index))
510 for (; it !=
end(); ++
it)
537 while (it != newSelection.
end()) {
538 if (!(*it).isValid()) {
539 it = newSelection.
erase(it);
542 for (
int t = 0; t < count(); ++t) {
543 if ((*it).intersects(
at(t)))
550 for (
int i = 0; i < intersections.
count(); ++i) {
551 for (
int t = 0; t < count();) {
552 if (
at(t).intersects(intersections.
at(i))) {
562 split(newSelection.
at(n), intersections.
at(i), &newSelection);
592 int other_top = other.
top();
593 int other_left = other.
left();
594 int other_bottom = other.
bottom();
595 int other_right = other.
right();
598 if (other_top > top) {
604 if (other_bottom < bottom) {
608 bottom = other_bottom;
610 if (other_left > left) {
616 if (other_right < right) {
639 q,
SLOT(_q_layoutAboutToBeChanged()));
641 q,
SLOT(_q_layoutChanged()));
654 QItemSelectionModel::SelectionFlags command)
const 662 for (
int i = 0; i < selection.
count(); ++i) {
671 if (command & QItemSelectionModel::Columns) {
672 for (
int i = 0; i < selection.
count(); ++i) {
694 if (currentIndex.isValid() && parent == currentIndex.
parent()
695 && currentIndex.
row() >= start && currentIndex.row() <=
end) {
699 else if (
model && end < model->rowCount(parent) - 1)
703 emit q->currentChanged(currentIndex, old);
704 emit q->currentRowChanged(currentIndex, old);
705 if (currentIndex.column() != old.
column())
706 emit q->currentColumnChanged(currentIndex, old);
712 while (it != ranges.end()) {
713 if (it->topLeft().parent() !=
parent) {
716 itParent = itParent.
parent();
718 if (itParent.
isValid() && start <= itParent.
row() && itParent.
row() <=
end) {
720 it = ranges.erase(it);
724 }
else if (start <= it->
bottom() && it->bottom() <= end
725 && start <= it->top() && it->top() <=
end) {
727 it = ranges.erase(it);
728 }
else if (start <= it->
top() && it->top() <=
end) {
732 }
else if (start <= it->
bottom() && it->bottom() <=
end) {
736 }
else if (it->top() < start && end < it->bottom()) {
743 deselected.
append(removedRange);
745 it = ranges.erase(it);
749 ranges.append(newParts);
764 if (currentIndex.isValid() && parent == currentIndex.
parent()
765 && currentIndex.
column() >= start && currentIndex.column() <=
end) {
769 else if (
model && end < model->columnCount() - 1)
773 emit q->currentChanged(currentIndex, old);
774 if (currentIndex.row() != old.
row())
775 emit q->currentRowChanged(currentIndex, old);
776 emit q->currentColumnChanged(currentIndex, old);
801 for (; it != ranges.
end(); ) {
802 if ((*it).isValid() && (*it).parent() == parent
803 && (*it).left() < start && (*it).right() >= start) {
808 it = ranges.
erase(it);
833 for (; it != ranges.
end(); ) {
834 if ((*it).isValid() && (*it).parent() == parent
835 && (*it).top() < start && (*it).bottom() >= start) {
840 it = ranges.
erase(it);
862 savedPersistentIndexes.clear();
863 savedPersistentCurrentIndexes.clear();
867 if (ranges.isEmpty() && currentSelection.count() == 1) {
872 if (tableRowCount * tableColCount > 1000
875 && range.
bottom() == tableRowCount - 1
876 && range.
right() == tableColCount - 1) {
877 tableSelected =
true;
882 tableSelected =
false;
888 indexes = currentSelection.indexes();
907 while (i < indexes.
count()) {
910 while (++i < indexes.
count()) {
913 && (next.
row() == br.
row())
924 while (i < colSpans.
count()) {
928 while (++i < colSpans.
count()) {
936 && (nextTl.
row() == prevTl.
row() + 1) && (nextBr.
row() == br.
row() + 1)) {
962 currentSelection.clear();
963 int bottom = tableRowCount - 1;
964 int right = tableColCount - 1;
969 tableSelected =
false;
973 if (savedPersistentCurrentIndexes.isEmpty() && savedPersistentIndexes.isEmpty()) {
980 currentSelection.clear();
983 qStableSort(savedPersistentIndexes.begin(), savedPersistentIndexes.end());
984 qStableSort(savedPersistentCurrentIndexes.begin(), savedPersistentCurrentIndexes.end());
988 currentSelection =
mergeIndexes(savedPersistentCurrentIndexes);
991 savedPersistentIndexes.clear();
992 savedPersistentCurrentIndexes.clear();
1038 d_func()->initModel(model);
1047 d_func()->initModel(model);
1075 select(selection, command);
1182 while (it !=
d->ranges.end()) {
1184 it =
d->ranges.erase(it);
1190 old.
merge(
d->currentSelection,
d->currentCommand);
1194 sel =
d->expandSelection(sel, command);
1197 if (command &
Clear) {
1199 d->currentSelection.clear();
1208 d->currentCommand = command;
1209 d->currentSelection = sel;
1214 newSelection.
merge(
d->currentSelection,
d->currentCommand);
1254 if (
d->ranges.count() == 0 &&
d->currentSelection.count() == 0)
1274 if (index ==
d->currentIndex) {
1282 select(
d->currentIndex, command);
1284 if (
d->currentIndex.row() != previous.
row() ||
1285 d->currentIndex.parent() != previous.
parent())
1287 if (
d->currentIndex.column() != previous.
column() ||
1288 d->currentIndex.parent() != previous.
parent())
1298 return static_cast<QModelIndex>(d_func()->currentIndex);
1310 bool selected =
false;
1313 for (; it !=
d->ranges.end(); ++
it) {
1314 if ((*it).isValid() && (*it).contains(index)) {
1321 if (
d->currentSelection.count()) {
1322 if ((
d->currentCommand &
Deselect) && selected)
1323 selected = !
d->currentSelection.contains(index);
1324 else if (
d->currentCommand &
Toggle)
1325 selected ^=
d->currentSelection.contains(index);
1326 else if ((
d->currentCommand &
Select) && !selected)
1327 selected =
d->currentSelection.contains(index);
1331 Qt::ItemFlags flags =
d->model->flags(index);
1353 if (
d->currentCommand &
Deselect &&
d->currentSelection.count()) {
1354 for (
int i=0; i<
d->currentSelection.count(); ++i) {
1355 if (
d->currentSelection.at(i).parent() == parent &&
1356 row >=
d->currentSelection.at(i).top() &&
1357 row <=
d->currentSelection.at(i).bottom())
1363 if (
d->currentCommand &
Toggle &&
d->currentSelection.count()) {
1364 for (
int i=0; i<
d->currentSelection.count(); ++i)
1365 if (
d->currentSelection.at(i).top() <= row &&
1366 d->currentSelection.at(i).bottom() >= row)
1367 for (
int j=0; j<
d->ranges.count(); ++j)
1368 if (
d->ranges.at(j).top() <= row &&
d->ranges.at(j).bottom() >= row
1369 &&
d->currentSelection.at(i).intersected(
d->ranges.at(j)).isValid())
1375 if (
d->currentSelection.count())
1376 joined +=
d->currentSelection;
1377 int colCount =
d->model->columnCount(parent);
1378 for (
int column = 0; column < colCount; ++column) {
1380 if ((*it).contains(row, column, parent)) {
1381 bool selectable =
false;
1382 for (
int i = column; !selectable && i <= (*it).right(); ++i) {
1383 Qt::ItemFlags flags =
d->model->index(row, i, parent).flags();
1387 column =
qMax(column, (*it).right());
1395 return colCount > 0;
1413 if (
d->currentCommand &
Deselect &&
d->currentSelection.count()) {
1414 for (
int i = 0; i <
d->currentSelection.count(); ++i) {
1415 if (
d->currentSelection.at(i).parent() == parent &&
1416 column >=
d->currentSelection.at(i).left() &&
1417 column <=
d->currentSelection.at(i).right())
1423 if (
d->currentCommand &
Toggle &&
d->currentSelection.count()) {
1424 for (
int i = 0; i <
d->currentSelection.count(); ++i) {
1425 if (
d->currentSelection.at(i).left() <= column &&
1426 d->currentSelection.at(i).right() >= column) {
1427 for (
int j = 0; j <
d->ranges.count(); ++j) {
1428 if (
d->ranges.at(j).left() <= column &&
d->ranges.at(j).right() >= column
1429 &&
d->currentSelection.at(i).intersected(
d->ranges.at(j)).isValid()) {
1439 if (
d->currentSelection.count())
1440 joined +=
d->currentSelection;
1441 int rowCount =
d->model->rowCount(parent);
1442 for (
int row = 0; row < rowCount; ++row) {
1444 if ((*it).contains(row, column, parent)) {
1445 Qt::ItemFlags flags =
d->model->index(row, column, parent).flags();
1447 row =
qMax(row, (*it).bottom());
1455 return rowCount > 0;
1469 sel.
merge(
d->currentSelection,
d->currentCommand);
1470 for (
int i = 0; i < sel.
count(); ++i) {
1471 int top = sel.
at(i).
top();
1475 if (top <= row && bottom >= row) {
1476 for (
int j = left; j <=
right; j++) {
1477 const Qt::ItemFlags flags =
d->model->index(row, j, parent).flags();
1498 sel.
merge(
d->currentSelection,
d->currentCommand);
1499 for (
int i = 0; i < sel.
count(); ++i) {
1502 int top = sel.
at(i).
top();
1504 if (left <= column && right >= column) {
1505 for (
int j = top; j <= bottom; j++) {
1506 const Qt::ItemFlags flags =
d->model->index(j, column, parent).flags();
1530 sel.
merge(
d->currentSelection,
d->currentCommand);
1533 return !(
d->ranges.isEmpty() &&
d->currentSelection.isEmpty());
1545 selected.
merge(
d->currentSelection,
d->currentCommand);
1567 for (
int i = 0; i < ranges.
count(); ++i) {
1570 for (
int row = range.
top(); row <= range.
bottom(); row++) {
1602 for (
int i = 0; i < ranges.
count(); ++i) {
1605 for (
int column = range.
left(); column <= range.
right(); column++) {
1607 if (!columnsSeen.
contains(columnDef)) {
1608 columnsSeen << columnDef;
1626 selected.
merge(
d->currentSelection,
d->currentCommand);
1630 while (i<selected.
count()) {
1644 return d_func()->model;
1656 oldSelection == newSelection)
1670 for (
int o = 0; o < deselected.
count(); ++o) {
1672 for (
int s = 0; s < selected.
count() && o < deselected.
count();) {
1673 if (deselected.
at(o) == selected.
at(s)) {
1687 for (
int o = 0; o < deselected.
count(); ++o) {
1688 for (
int s = 0; s < selected.
count(); ++s) {
1695 for (
int i = 0; i < intersections.
count(); ++i) {
1697 for (
int o = 0; o < deselected.
count();) {
1706 for (
int s = 0; s < selected.
count();) {
1720 #ifndef QT_NO_DEBUG_STREAM 1723 #ifndef Q_BROKEN_DEBUG_STREAM 1728 qWarning(
"This compiler doesn't support streaming QItemSelectionRange to QDebug");
1737 #include "moc_qitemselectionmodel.cpp" 1739 #endif // QT_NO_ITEMVIEWS QModelIndexList selectedRows(int column=0) const
Returns the indexes in the given column for the rows where all columns are selected.
The QDebug class provides an output stream for debugging information.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of columns for the children of the given parent.
int row() const
Returns the row this persistent model index refers to.
bool blockSignals(bool b)
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke...
The QItemSelectionModel class keeps track of a view's selected items.
void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command)
Merges the other selection with this QItemSelection using the command given.
int left() const
Returns the column index corresponding to the leftmost selected column in the selection range...
virtual void clear()
Clears the selection model.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
#define QT_END_NAMESPACE
This macro expands to.
QModelIndex bottomRight() const
Returns the index for the item located at the bottom-right corner of the selection range...
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column.
#define it(className, varName)
void _q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
bool isColumnSelected(int column, const QModelIndex &parent) const
Returns true if all items are selected in the column with the given parent.
QDebug & nospace()
Clears the stream's internal flag that records whether the last character was a space and returns a r...
bool isValid() const
Returns true if the selection range is valid; otherwise returns false.
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.
virtual void reset()
Clears the selection model.
#define at(className, varName)
QModelIndexList selectedIndexes() const
Returns a list of all selected model item indexes.
static QItemSelection mergeIndexes(const QList< QPersistentModelIndex > &indexes)
Merges indexes into an item selection made up of ranges.
void clearSelection()
Clears the selection in the selection model.
QDebug operator<<(QDebug dbg, const QItemSelectionRange &range)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *)
The QItemSelectionRange class manages information about a range of selected items in a model...
const QItemSelection selection() const
Returns the selection ranges stored in the selection model.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
int bottom() const
Returns the row index corresponding to the lowermost selected row in the selection range...
int count(const T &t) const
Returns the number of occurrences of value in the list.
int right() const
Returns the column index corresponding to the rightmost selected column in the selection range...
void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection)
Compares the two selections newSelection and oldSelection and emits selectionChanged() with the desel...
bool isEmpty() const
Returns true if the selection range contains no selectable item.
The QObject class is the base class of all Qt objects.
void _q_columnsAboutToBeInserted(const QModelIndex &parent, int start, int end)
Split selection ranges if columns are about to be inserted in the middle.
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.
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
bool rowIntersectsSelection(int row, const QModelIndex &parent) const
Returns true if there are any items selected in the row with the given parent.
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Sets the model item index to be the current item, and emits currentChanged().
QModelIndex topLeft() const
Returns the index for the item located at the top-left corner of 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...
friend class const_iterator
void _q_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
Split selection ranges if rows are about to be inserted in the middle.
void append(const T &t)
Inserts value at the end of the list.
#define QT_BEGIN_NAMESPACE
This macro expands to.
void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
This signal is emitted if the current item changes and its row is different to the row of the previou...
bool contains(const T &value) const
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
QModelIndexList indexes() const
Returns the list of model index items stored in the selection.
bool isSelected(const QModelIndex &index) const
Returns true if the given model item index is selected.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
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.
const QAbstractItemModel * model() const
Returns a pointer to the model containing the item that this index refers to.
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 void indexesFromRange(const QItemSelectionRange &range, QModelIndexList &result)
static void split(QT_FT_Vector *b)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
This signal is emitted whenever the selection changes.
QModelIndex currentIndex() const
Returns the model item index for the current item, or an invalid index if there is no current item...
void _q_layoutChanged()
Merge the selected indexes into selection ranges again.
void select(const QModelIndex &topLeft, const QModelIndex &bottomRight)
Adds the items in the range that extends from the top-left model item, specified by the topLeft index...
QItemSelection()
Constructs an empty selection.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QAbstractItemModel class provides the abstract interface for item model classes.
Qt::ItemFlags flags() const
Returns the flags for the item referred to by the index.
void qStableSort(RandomAccessIterator start, RandomAccessIterator end)
QItemSelection expandSelection(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) const
bool hasSelection() const
Returns true if the selection model contains any selection ranges; otherwise returns false...
virtual ~QItemSelectionModel()
Destroys the selection model.
iterator erase(iterator pos)
Removes the item associated with the iterator pos from the list, and returns an iterator to the next ...
bool columnIntersectsSelection(int column, const QModelIndex &parent) const
Returns true if there are any items selected in the column with the given parent. ...
The QItemSelection class manages information about selected items in a model.
QModelIndexList selectedColumns(int row=0) const
Returns the indexes in the given row for columns where all rows are selected.
QItemSelectionRange()
Constructs an empty selection range.
The QPersistentModelIndex class is used to locate data in a data model.
virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Selects the model item index using the specified command, and emits selectionChanged().
QObject * parent() const
Returns a pointer to the parent object.
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
The QModelIndex class is used to locate data in a data model.
void currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
This signal is emitted whenever the current item changes.
void currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous)
This signal is emitted if the current item changes and its column is different to the column of the p...
const QAbstractItemModel * model() const
Returns the item model operated on by the selection model.
QItemSelectionRange intersect(const QItemSelectionRange &other) const
Use intersected(other) instead.
QItemSelectionRange intersected(const QItemSelectionRange &other) const
Returns a new selection range containing only the items that are found in both the selection range an...
void _q_layoutAboutToBeChanged()
Split selection into individual (persistent) indexes.
int top() const
Returns the row index corresponding to the uppermost selected row in the selection range...
bool intersects(const QItemSelectionRange &other) const
Returns true if this selection range intersects (overlaps with) the other range given; otherwise retu...
static void split(const QItemSelectionRange &range, const QItemSelectionRange &other, QItemSelection *result)
Splits the selection range using the selection other range.
void initModel(QAbstractItemModel *model)
QModelIndexList indexes() const
Returns a list of model indexes that correspond to the selected items.
bool contains(const QModelIndex &index) const
Returns true if the selection contains the given index; otherwise returns false.
timeval & operator+=(timeval &t1, const timeval &t2)
QItemSelectionModel(QAbstractItemModel *model)
Constructs a selection model that operates on the specified item model.
static const KeyPair *const end
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
bool isRowSelected(int row, const QModelIndex &parent) const
Returns true if all items are selected in the row with the given parent.
int column() const
Returns the column this model index refers to.
void _q_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
const QAbstractItemModel * model() const
Returns the model that the items in the selection range belong to.
void removeAt(int i)
Removes the item at index position i.