📄 qitemselectionmodel.cpp
字号:
for (; it != d->ranges.end(); ++it) { if ((*it).contains(index)) { selected = true; break; } } // check currentSelection if (d->currentSelection.count()) { if ((d->currentCommand & Deselect) && selected) selected = !d->currentSelection.contains(index); else if (d->currentCommand & Toggle) selected ^= d->currentSelection.contains(index); else if ((d->currentCommand & Select) && !selected) selected = d->currentSelection.contains(index); } if (selected && (d->model->flags(index) & Qt::ItemIsSelectable)) return true; return false;}/*! Returns true if all items are selected in the \a row with the given \a parent. Note that this function is usually faster than calling isSelected() on all items in the same row and that unselectable items are ignored.*/bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) const{ Q_D(const QItemSelectionModel); if (parent.isValid() && d->model != parent.model()) return false; // return false if row exist in currentSelection (Deselect) if (d->currentCommand & Deselect && d->currentSelection.count()) { for (int i=0; i<d->currentSelection.count(); ++i) { if (d->currentSelection.at(i).parent() == parent && row >= d->currentSelection.at(i).top() && row <= d->currentSelection.at(i).bottom()) return false; } } // return false if ranges in both currentSelection and ranges // intersect and have the same row contained if (d->currentCommand & Toggle && d->currentSelection.count()) { for (int i=0; i<d->currentSelection.count(); ++i) if (d->currentSelection.at(i).top() <= row && d->currentSelection.at(i).bottom() >= row) for (int j=0; j<d->ranges.count(); ++j) if (d->ranges.at(j).top() <= row && d->ranges.at(j).bottom() >= row && d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid()) return false; } // add ranges and currentSelection and check through them all QList<QItemSelectionRange>::const_iterator it; QList<QItemSelectionRange> joined = d->ranges; if (d->currentSelection.count()) joined += d->currentSelection; int colCount = d->model->columnCount(parent); for (int column = 0; column < colCount; ++column) { for (it = joined.constBegin(); it != joined.constEnd(); ++it) if ((*it).contains(row, column, parent)) { column = qMax(column, (*it).right()); break; } if (it == joined.constEnd()) return false; } return colCount > 0; // no columns means no selected items}/*! Returns true if all items are selected in the \a column with the given \a parent. Note that this function is usually faster than calling isSelected() on all items in the same column and that unselectable items are ignored.*/bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent) const{ Q_D(const QItemSelectionModel); if (parent.isValid() && d->model != parent.model()) return false; // return false if column exist in currentSelection (Deselect) if (d->currentCommand & Deselect && d->currentSelection.count()) { for (int i = 0; i < d->currentSelection.count(); ++i) { if (d->currentSelection.at(i).parent() == parent && column >= d->currentSelection.at(i).left() && column <= d->currentSelection.at(i).right()) return false; } } // return false if ranges in both currentSelection and the selection model // intersect and have the same column contained if (d->currentCommand & Toggle && d->currentSelection.count()) { for (int i = 0; i < d->currentSelection.count(); ++i) { if (d->currentSelection.at(i).left() <= column && d->currentSelection.at(i).right() >= column) { for (int j = 0; j < d->ranges.count(); ++j) { if (d->ranges.at(j).left() <= column && d->ranges.at(j).right() >= column && d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid()) { return false; } } } } } // add ranges and currentSelection and check through them all QList<QItemSelectionRange>::const_iterator it; QList<QItemSelectionRange> joined = d->ranges; if (d->currentSelection.count()) joined += d->currentSelection; int rowCount = d->model->rowCount(parent); for (int row = 0; row < rowCount; ++row) { for (it = joined.constBegin(); it != joined.constEnd(); ++it) { if ((*it).contains(row, column, parent)) { row = qMax(row, (*it).bottom()); break; } } if (it == joined.constEnd()) return false; } return rowCount > 0; // no rows means no selected items}/*! Returns true if there are any items selected in the \a row with the given \a parent.*/bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &parent) const{ Q_D(const QItemSelectionModel); if (parent.isValid() && d->model != parent.model()) return false; // check current selection for (int i = 0; i < d->currentSelection.count(); ++i) if (d->currentSelection.at(i).top() <= row && d->currentSelection.at(i).bottom() >= row) return true; // check the ranges for (int i = 0; i < d->ranges.count(); ++i) if (d->ranges.at(i).top() <= row && d->ranges.at(i).bottom() >= row) return true; return false;}/*! Returns true if there are any items selected in the \a column with the given \a parent.*/bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelIndex &parent) const{ Q_D(const QItemSelectionModel); if (parent.isValid() && d->model != parent.model()) return false; // check current selection for (int i = 0; i < d->currentSelection.count(); ++i) if (d->currentSelection.at(i).left() <= column && d->currentSelection.at(i).right() >= column) return true; // check the ranges for (int i = 0; i < d->ranges.count(); ++i) if (d->ranges.at(i).left() <= column && d->ranges.at(i).right() >= column) return true; return false;}/*! \since 4.2 Returns true if the selection model contains any selection ranges; otherwise returns false.*/bool QItemSelectionModel::hasSelection() const{ Q_D(const QItemSelectionModel); if (d->currentCommand == Toggle || d->currentCommand == Deselect) { QItemSelection sel = d->ranges; sel.merge(d->currentSelection, d->currentCommand); return !sel.isEmpty(); } else { return !(d->ranges.isEmpty() && d->currentSelection.isEmpty()); }}/*! Returns a list of all selected model item indexes. The list contains no duplicates, and is not sorted.*/QModelIndexList QItemSelectionModel::selectedIndexes() const{ Q_D(const QItemSelectionModel); QItemSelection selected = d->ranges; selected.merge(d->currentSelection, d->currentCommand); return selected.indexes();}/*! \since 4.2 Returns the indexes in the given \a column for the rows where all columns are selected. \sa selectedIndexes(), selectedColumns()*/QModelIndexList QItemSelectionModel::selectedRows(int column) const{ QModelIndexList indexes; QItemSelection ranges = selection(); for (int i = 0; i < ranges.count(); ++i) { QModelIndex parent = ranges.at(i).parent(); int right = ranges.at(i).model()->columnCount(parent) - 1; if (ranges.at(i).left() == 0 && ranges.at(i).right() == right) for (int r = ranges.at(i).top(); r <= ranges.at(i).bottom(); ++r) indexes.append(ranges.at(i).model()->index(r, column, parent)); } return indexes;}/*! \since 4.2 Returns the indexes in the given \a row for columns where all rows are selected. \sa selectedIndexes(), selectedRows()*/QModelIndexList QItemSelectionModel::selectedColumns(int row) const{ QModelIndexList indexes; QItemSelection ranges = selection(); for (int i = 0; i < ranges.count(); ++i) { QModelIndex parent = ranges.at(i).parent(); int bottom = ranges.at(i).model()->rowCount(parent) - 1; if (ranges.at(i).top() == 0 && ranges.at(i).bottom() == bottom) for (int c = ranges.at(i).left(); c <= ranges.at(i).right(); ++c) indexes.append(ranges.at(i).model()->index(row, c, parent)); } return indexes;}/*! Returns the selection ranges stored in the selection model.*/const QItemSelection QItemSelectionModel::selection() const{ Q_D(const QItemSelectionModel); QItemSelection selected = d->ranges; selected.merge(d->currentSelection, d->currentCommand); int i = 0; // make sure we have no invalid ranges // ### should probably be handled more generic somewhere else while (i<selected.count()) { if (selected.at(i).isValid()) ++i; else (selected.removeAt(i)); } return selected;}/*! Returns the item model operated on by the selection model.*/const QAbstractItemModel *QItemSelectionModel::model() const{ return d_func()->model;}/*! Compares the two selections \a newSelection and \a oldSelection and emits selectionChanged() with the deselected and selected items.*/void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection){ // if both selections are empty or equal we return if ((oldSelection.isEmpty() && newSelection.isEmpty()) || oldSelection == newSelection) return; // if either selection is empty we do not need to compare if (oldSelection.isEmpty() || newSelection.isEmpty()) { emit selectionChanged(newSelection, oldSelection); return; } QItemSelection deselected = oldSelection; QItemSelection selected = newSelection; // remove equal ranges bool advance; for (int o = 0; o < deselected.count(); ++o) { advance = true; for (int s = 0; s < selected.count() && o < deselected.count();) { if (deselected.at(o) == selected.at(s)) { deselected.removeAt(o); selected.removeAt(s); advance = false; } else { ++s; } } if (advance) ++o; } // find intersections QItemSelection intersections; for (int o = 0; o < deselected.count(); ++o) { for (int s = 0; s < selected.count(); ++s) { if (deselected.at(o).intersects(selected.at(s))) intersections.append(deselected.at(o).intersected(selected.at(s))); } } // compare remaining ranges with intersections and split them to find deselected and selected for (int i = 0; i < intersections.count(); ++i) { // split deselected for (int o = 0; o < deselected.count();) { if (deselected.at(o).intersects(intersections.at(i))) { QItemSelection::split(deselected.at(o), intersections.at(i), &deselected); deselected.removeAt(o); } else { ++o; } } // split selected for (int s = 0; s < selected.count();) { if (selected.at(s).intersects(intersections.at(i))) { QItemSelection::split(selected.at(s), intersections.at(i), &selected); selected.removeAt(s); } else { ++s; } } } emit selectionChanged(selected, deselected);}#ifndef QT_NO_DEBUG_STREAMQDebug operator<<(QDebug dbg, const QItemSelectionRange &range){#ifndef Q_BROKEN_DEBUG_STREAM dbg.nospace() << "QItemSelectionRange(" << range.topLeft() << "," << range.bottomRight() << ")"; return dbg.space();#else qWarning("This compiler doesn't support streaming QItemSelectionRange to QDebug"); return dbg; Q_UNUSED(range);#endif}#endif#include "moc_qitemselectionmodel.cpp"#endif // QT_NO_ITEMVIEWS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -