summaryrefslogtreecommitdiffstats
path: root/src/corelib/itemmodels
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2020-05-03 19:25:38 +0200
committerMarc Mutz <marc.mutz@kdab.com>2020-05-05 07:24:26 +0200
commita6a412dbb6c82f6a4885e54fbef0cfe14fbcd4ab (patch)
tree497a195f3ef7d1789f7b8544f0d836879d201c31 /src/corelib/itemmodels
parent2eee9e6fcf9b70681c5d9202d1509d176e48fc31 (diff)
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>
Diffstat (limited to 'src/corelib/itemmodels')
-rw-r--r--src/corelib/itemmodels/qitemselectionmodel.cpp31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp
index 27442fb2fa..7327b5deee 100644
--- a/src/corelib/itemmodels/qitemselectionmodel.cpp
+++ b/src/corelib/itemmodels/qitemselectionmodel.cpp
@@ -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)) {