From a6a412dbb6c82f6a4885e54fbef0cfe14fbcd4ab Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 3 May 2020 19:25:38 +0200 Subject: QItemSelectionModel: replace a QPair with a dedicated struct Pairs are easy to use, but they have no semantics attached: Two QPair 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 --- src/corelib/itemmodels/qitemselectionmodel.cpp | 31 +++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/corelib/itemmodels') 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 > rowsSeen; + + QSet 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 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 > columnsSeen; + + QSet 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 columnDef = qMakePair(parent, column); + RowOrColumnDefinition columnDef = {parent, column}; if (!columnsSeen.contains(columnDef)) { columnsSeen << columnDef; if (isColumnSelected(column, parent)) { -- cgit v1.2.3