📄 qitemselectionmodel.cpp
字号:
return; QItemSelection newSelection = other; // Collect intersections QItemSelection intersections; QItemSelection::iterator it = newSelection.begin(); while (it != newSelection.end()) { if (!(*it).isValid()) { it = newSelection.erase(it); continue; } for (int t = 0; t < count(); ++t) { if ((*it).intersects(at(t))) intersections.append(at(t).intersect(*it)); } ++it; } // Split the old (and new) ranges using the intersections for (int i = 0; i < intersections.count(); ++i) { // for each intersection for (int t = 0; t < count();) { // splitt each old range if (at(t).intersects(intersections.at(i))) { split(at(t), intersections.at(i), this); removeAt(t); } else { ++t; } } // only split newSelection if Toggle is specified for (int n = 0; (command & QItemSelectionModel::Toggle) && n < newSelection.count();) { if (newSelection.at(n).intersects(intersections.at(i))) { split(newSelection.at(n), intersections.at(i), &newSelection); newSelection.removeAt(n); } else { ++n; } } } // do not add newSelection for Deselect if (!(command & QItemSelectionModel::Deselect)) operator+=(newSelection);}/*! Splits the selection \a range using the selection \a other range. Removes all items in \a other from \a range and puts the result in \a result. This can be compared with the semantics of the \e subtract operation of a set. \sa merge()*/void QItemSelection::split(const QItemSelectionRange &range, const QItemSelectionRange &other, QItemSelection *result){ if (range.parent() != other.parent()) return; QModelIndex parent = other.parent(); int top = range.top(); int left = range.left(); int bottom = range.bottom(); int right = range.right(); int other_top = other.top(); int other_left = other.left(); int other_bottom = other.bottom(); int other_right = other.right(); const QAbstractItemModel *model = range.model(); Q_ASSERT(model); if (other_top > top) { QModelIndex tl = model->index(top, left, parent); QModelIndex br = model->index(other_top - 1, right, parent); result->append(QItemSelectionRange(tl, br)); top = other_top; } if (other_bottom < bottom) { QModelIndex tl = model->index(other_bottom + 1, left, parent); QModelIndex br = model->index(bottom, right, parent); result->append(QItemSelectionRange(tl, br)); bottom = other_bottom; } if (other_left > left) { QModelIndex tl = model->index(top, left, parent); QModelIndex br = model->index(bottom, other_left - 1, parent); result->append(QItemSelectionRange(tl, br)); left = other_left; } if (other_right < right) { QModelIndex tl = model->index(top, other_right + 1, parent); QModelIndex br = model->index(bottom, right, parent); result->append(QItemSelectionRange(tl, br)); right = other_right; }}/*! \internal returns a QItemSelection where all ranges have been expanded to: Rows: left: 0 and right: columnCount()-1 Columns: top: 0 and bottom: rowCount()-1*/QItemSelection QItemSelectionModelPrivate::expandSelection(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) const{ if (selection.isEmpty() && !((command & QItemSelectionModel::Rows) || (command & QItemSelectionModel::Columns))) return selection; QItemSelection expanded; if (command & QItemSelectionModel::Rows) { for (int i = 0; i < selection.count(); ++i) { QModelIndex parent = selection.at(i).parent(); int colCount = model->columnCount(parent); QModelIndex tl = model->index(selection.at(i).top(), 0, parent); QModelIndex br = model->index(selection.at(i).bottom(), colCount - 1, parent); expanded.append(QItemSelectionRange(tl, br)); } } if (command & QItemSelectionModel::Columns) { for (int i = 0; i < selection.count(); ++i) { QModelIndex parent = selection.at(i).parent(); int rowCount = model->rowCount(parent); QModelIndex tl = model->index(0, selection.at(i).left(), parent); QModelIndex br = model->index(rowCount - 1, selection.at(i).right(), parent); expanded.append(QItemSelectionRange(tl, br)); } } return expanded;}/*! \internal*/void QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end){ Q_Q(QItemSelectionModel); // update current index if (currentIndex.isValid() && parent == currentIndex.parent() && currentIndex.row() >= start && currentIndex.row() <= end) { QModelIndex old = currentIndex; if (start > 0) // there are rows left above the change currentIndex = model->index(start - 1, old.column(), parent); else if (model && end < model->rowCount(parent) - 1) // there are rows left below the change currentIndex = model->index(end + 1, old.column(), parent); else // there are no rows left in the table currentIndex = QModelIndex(); emit q->currentChanged(currentIndex, old); emit q->currentRowChanged(currentIndex, old); if (currentIndex.column() != old.column()) emit q->currentColumnChanged(currentIndex, old); } // update selectionsx QModelIndex tl = model->index(start, 0, parent); QModelIndex br = model->index(end, model->columnCount(parent) - 1, parent); q->select(QItemSelection(tl, br), QItemSelectionModel::Deselect); finalize();}/*! \internal*/void QItemSelectionModelPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end){ Q_Q(QItemSelectionModel); // update current index if (currentIndex.isValid() && parent == currentIndex.parent() && currentIndex.column() >= start && currentIndex.column() <= end) { QModelIndex old = currentIndex; if (start > 0) // there are columns to the left of the change currentIndex = model->index(old.row(), start - 1, parent); else if (model && end < model->columnCount() - 1) // there are columns to the right of the change currentIndex = model->index(old.row(), end + 1, parent); else // there are no columns left in the table currentIndex = QModelIndex(); emit q->currentChanged(currentIndex, old); if (currentIndex.row() != old.row()) emit q->currentRowChanged(currentIndex, old); emit q->currentColumnChanged(currentIndex, old); } // update selections QModelIndex tl = model->index(0, start, parent); QModelIndex br = model->index(model->rowCount(parent) - 1, end, parent); q->select(QItemSelection(tl, br), QItemSelectionModel::Deselect); finalize();}/*! \class QItemSelectionModel \brief The QItemSelectionModel class keeps track of a view's selected items. \ingroup model-view A QItemSelectionModel keeps track of the selected items in a view, or in several views onto the same model. It also keeps track of the currently selected item in a view. The QItemSelectionModel class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. The selected items are stored using ranges. Whenever you want to modify the selected items use select() and provide either a QItemSelection, or a QModelIndex and a QItemSelectionModel::SelectionFlag. The QItemSelectionModel takes a two layer approach to selection management, dealing with both selected items that have been committed and items that are part of the current selection. The current selected items are part of the current interactive selection (for example with rubber-band selection or keyboard-shift selections). To update the currently selected items, use the bitwise OR of QItemSelectionModel::Current and any of the other SelectionFlags. If you omit the QItemSelectionModel::Current command, a new current selection will be created, and the previous one added to the committed selection. All functions operate on both layers; for example, selectedItems() will return items from both layers. \sa {Model/View Programming}, QAbstractItemModel*//*! Constructs a selection model that operates on the specified item \a model.*/QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model) : QObject(*new QItemSelectionModelPrivate, model){ d_func()->model = model; if (model) { connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(const QModelIndex&,int,int))); connect(model, SIGNAL(columnsAboutToBeRemoved(const QModelIndex&,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(const QModelIndex&,int,int))); }}/*! Constructs a selection model that operates on the specified item \a model with \a parent.*/QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent) : QObject(*new QItemSelectionModelPrivate, parent){ d_func()->model = model; if (model) { connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(const QModelIndex&,int,int))); connect(model, SIGNAL(columnsAboutToBeRemoved(const QModelIndex&,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(const QModelIndex&,int,int))); }}/*! \internal*/QItemSelectionModel::QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model) : QObject(dd, model){ d_func()->model = model; if (model) { connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&,int,int)), this, SLOT(_q_rowsAboutToBeRemoved(const QModelIndex&,int,int))); connect(model, SIGNAL(columnsAboutToBeRemoved(const QModelIndex&,int,int)), this, SLOT(_q_columnsAboutToBeRemoved(const QModelIndex&,int,int))); }}/*! Destroys the selection model.*/QItemSelectionModel::~QItemSelectionModel(){}/*! Selects the model item \a index using the specified \a command, and emits selectionChanged(). \sa QItemSelectionModel::SelectionFlags*/void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command){ if (index.isValid()) { QItemSelection selection(index, index); select(selection, command); }}/*! \fn void QItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) This signal is emitted whenever the current item changes. The \a previous model item index is replaced by the \a current index as the selection's current item. \sa currentIndex() setCurrentIndex() selectionChanged()*//*! \fn void QItemSelectionModel::currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous) This signal is emitted if the \a current item changes and its column is different to the column of the \a previous current item. \sa currentChanged() currentRowChanged() currentIndex() setCurrentIndex()*//*! \fn void QItemSelectionModel::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) This signal is emitted if the \a current item changes and its row is different to the row of the \a previous current item. \sa currentChanged() currentColumnChanged() currentIndex() setCurrentIndex()*//*! \fn void QItemSelectionModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) This signal is emitted whenever the selection changes. The change in the selection is represented as an item selection of \a deselected items and an item selection of \a selected items. Note the that the current index changes independently from the selection. \sa select() currentChanged()*//*! \enum QItemSelectionModel::SelectionFlag This enum describes the way the selection model will be updated. \value NoUpdate No selection will be made. \value Clear The complete selection will be cleared. \value Select All specified indexes will be selected. \value Deselect All specified indexes will be deselected. \value Toggle All specified indexes will be selected or deselected depending on their current state. \value Current The current selection will be updated. \value Rows All indexes will be expanded to span rows. \value Columns All indexes will be expanded to span columns. \value SelectCurrent A combination of Select and Current, provided for convenience. \value ToggleCurrent A combination of Toggle and Current, provided for convenience. \value ClearAndSelect A combination of Clear and Select, provided for convenience.*//*! Selects the item \a selection using the specified \a command, and emits selectionChanged(). \sa QItemSelectionModel::SelectionFlag*/void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command){ Q_D(QItemSelectionModel); if (command == NoUpdate) return; // store old selection QItemSelection sel = selection; QItemSelection old = d->ranges; old.merge(d->currentSelection, d->currentCommand); // expand selection according to SelectionBehavior if (command & Rows || command & Columns) sel = d->expandSelection(sel, command); // clear ranges and currentSelection if (command & Clear) { d->ranges.clear(); d->currentSelection.clear(); } // merge and clear currentSelection if Current was not set (ie. start new currentSelection) if (!(command & Current)) d->finalize(); // update currentSelection if (command & Toggle || command & Select || command & Deselect) { d->currentCommand = command; d->currentSelection = sel;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -