Skip to content

Commit

Permalink
QItemSelectionModel: cleanup lambdas
Browse files Browse the repository at this point in the history
Cleanup lambdas so they match the Qt coding style.

Task-number: QTBUG-113137
Change-Id: I0cf3f1781349c09e1b53a47bef9fe4f8e3632175
Reviewed-by: David Faure <david.faure@kdab.com>
  • Loading branch information
chehrlic committed Oct 8, 2024
1 parent c9306ba commit 1b3a7d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
59 changes: 32 additions & 27 deletions src/corelib/itemmodels/qitemselectionmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1475,9 +1475,10 @@ bool QItemSelectionModel::isSelected(const QModelIndex &index) const
return false;

// search model ranges
const auto containsIndex = [&](const auto &range)
{ return range.isValid() && range.contains(index); };
bool selected = std::any_of(d->ranges.begin(), d->ranges.end(), containsIndex);
auto contains = [](const auto &index) {
return [&index](const auto &range) { return range.contains(index); };
};
bool selected = std::any_of(d->ranges.begin(), d->ranges.end(), contains(index));

// check currentSelection
if (d->currentSelection.size()) {
Expand Down Expand Up @@ -1516,27 +1517,29 @@ bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) cons

// return false if row exist in currentSelection (Deselect)
if (d->currentCommand & Deselect) {
const auto matchesRow = [&](const auto &selection)
{
return row >= selection.top() &&
row <= selection.bottom() &&
parent == selection.parent();
const auto matches = [](auto row, const auto &parent) {
return [row, &parent](const auto &selection) {
return row >= selection.top() &&
row <= selection.bottom() &&
parent == selection.parent();
};
};
if (std::any_of(d->currentSelection.cbegin(), d->currentSelection.cend(), matchesRow))
if (std::any_of(d->currentSelection.cbegin(), d->currentSelection.cend(), matches(row, parent)))
return false;
}
// return false if ranges in both currentSelection and ranges
// intersect and have the same row contained
if (d->currentCommand & Toggle) {
for (const auto &selection : d->currentSelection) {
if (row >= selection.top() && row <= selection.bottom()) {
const auto selectionAndRangeIntersect = [&](const auto &range)
{
return row >= range.top() &&
row <= range.bottom() &&
selection.intersected(range).isValid();
const auto intersects = [](auto row, const auto &selection) {
return [row, &selection](const auto &range) {
return row >= range.top() &&
row <= range.bottom() &&
selection.intersected(range).isValid();
};
};
if (std::any_of(d->ranges.cbegin(), d->ranges.cend(), selectionAndRangeIntersect))
if (std::any_of(d->ranges.cbegin(), d->ranges.cend(), intersects(row, selection)))
return false;
}
}
Expand Down Expand Up @@ -1600,27 +1603,29 @@ bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent

// return false if column exist in currentSelection (Deselect)
if (d->currentCommand & Deselect) {
const auto matchesColumn = [&](const auto &selection)
{
return column >= selection.left() &&
column <= selection.right() &&
parent == selection.parent();
const auto matches = [](auto column, const auto &parent) {
return [column, &parent](const auto &selection) {
return column >= selection.left() &&
column <= selection.right() &&
parent == selection.parent();
};
};
if (std::any_of(d->currentSelection.cbegin(), d->currentSelection.cend(), matchesColumn))
if (std::any_of(d->currentSelection.cbegin(), d->currentSelection.cend(), matches(column, parent)))
return false;
}
// return false if ranges in both currentSelection and the selection model
// intersect and have the same column contained
if (d->currentCommand & Toggle) {
for (const auto &selection : d->currentSelection) {
if (column >= selection.left() && column <= selection.right()) {
const auto selectionAndRangeIntersect = [&](const auto &range)
{
return column >= range.left() &&
column <= range.right() &&
selection.intersected(range).isValid();
const auto intersects = [](auto column, const auto &selection) {
return [column, &selection](const auto &range) {
return column >= range.left() &&
column <= range.right() &&
selection.intersected(range).isValid();
};
};
if (std::any_of(d->ranges.cbegin(), d->ranges.cend(), selectionAndRangeIntersect))
if (std::any_of(d->ranges.cbegin(), d->ranges.cend(), intersects(column, selection)))
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/corelib/itemmodels/qitemselectionmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Q_CORE_EXPORT QItemSelectionRange

inline bool contains(int row, int column, const QModelIndex &parentIndex) const
{
return (tl.row() <= row && tl.column() <= column &&
br.row() >= row && br.column() >= column &&
return (br.row() >= row && br.column() >= column &&
tl.row() <= row && tl.column() <= column &&
parent() == parentIndex);
}

Expand Down

0 comments on commit 1b3a7d5

Please sign in to comment.