QItemSelectionModel: replace a QPair with a dedicated struct
Pairs are easy to use, but they have no semantics attached: Two QPair<int, int> compare equal, e.g., even though one is used as a coordinate and the other as, say, a fraction. It also carries no information for the reader of the code, as exemplified by the urge to comment on the content of the pairs in both functions that use them. So, write a minimal struct with equality and qHash() instead. The comments are now no longer needed. Change-Id: I51f6ff049a5f8fa61c51856376ac2fcbfb8dd506 Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
parent
2eee9e6fcf
commit
a6a412dbb6
@ -1721,6 +1721,23 @@ QModelIndexList QItemSelectionModel::selectedIndexes() const
|
||||
return selected.indexes();
|
||||
}
|
||||
|
||||
struct RowOrColumnDefinition {
|
||||
QModelIndex parent;
|
||||
int rowOrColumn;
|
||||
|
||||
friend bool operator==(const RowOrColumnDefinition &lhs, const RowOrColumnDefinition &rhs) noexcept
|
||||
{ return lhs.parent == rhs.parent && lhs.rowOrColumn == rhs.rowOrColumn; }
|
||||
friend bool operator!=(const RowOrColumnDefinition &lhs, const RowOrColumnDefinition &rhs) noexcept
|
||||
{ return !operator==(lhs, rhs); }
|
||||
};
|
||||
size_t qHash(const RowOrColumnDefinition &key, size_t seed = 0) noexcept
|
||||
{
|
||||
QtPrivate::QHashCombine hash;
|
||||
seed = hash(seed, key.parent);
|
||||
seed = hash(seed, key.rowOrColumn);
|
||||
return seed;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 4.2
|
||||
Returns the indexes in the given \a column for the rows where all columns are selected.
|
||||
@ -1731,16 +1748,15 @@ QModelIndexList QItemSelectionModel::selectedIndexes() const
|
||||
QModelIndexList QItemSelectionModel::selectedRows(int column) const
|
||||
{
|
||||
QModelIndexList indexes;
|
||||
//the QSet contains pairs of parent modelIndex
|
||||
//and row number
|
||||
QSet< QPair<QModelIndex, int> > rowsSeen;
|
||||
|
||||
QSet<RowOrColumnDefinition> rowsSeen;
|
||||
|
||||
const QItemSelection ranges = selection();
|
||||
for (int i = 0; i < ranges.count(); ++i) {
|
||||
const QItemSelectionRange &range = ranges.at(i);
|
||||
QModelIndex parent = range.parent();
|
||||
for (int row = range.top(); row <= range.bottom(); row++) {
|
||||
QPair<QModelIndex, int> rowDef = qMakePair(parent, row);
|
||||
RowOrColumnDefinition rowDef = {parent, row};
|
||||
if (!rowsSeen.contains(rowDef)) {
|
||||
rowsSeen << rowDef;
|
||||
if (isRowSelected(row, parent)) {
|
||||
@ -1763,16 +1779,15 @@ QModelIndexList QItemSelectionModel::selectedRows(int column) const
|
||||
QModelIndexList QItemSelectionModel::selectedColumns(int row) const
|
||||
{
|
||||
QModelIndexList indexes;
|
||||
//the QSet contains pairs of parent modelIndex
|
||||
//and column number
|
||||
QSet< QPair<QModelIndex, int> > columnsSeen;
|
||||
|
||||
QSet<RowOrColumnDefinition> columnsSeen;
|
||||
|
||||
const QItemSelection ranges = selection();
|
||||
for (int i = 0; i < ranges.count(); ++i) {
|
||||
const QItemSelectionRange &range = ranges.at(i);
|
||||
QModelIndex parent = range.parent();
|
||||
for (int column = range.left(); column <= range.right(); column++) {
|
||||
QPair<QModelIndex, int> columnDef = qMakePair(parent, column);
|
||||
RowOrColumnDefinition columnDef = {parent, column};
|
||||
if (!columnsSeen.contains(columnDef)) {
|
||||
columnsSeen << columnDef;
|
||||
if (isColumnSelected(column, parent)) {
|
||||
|
Loading…
Reference in New Issue
Block a user