QmlDesigner: Use std::ranges in ListModelEditorModel

Change-Id: Icd0bfb1bf468169ecd90c17ab9946d6ceab88f8a
Reviewed-by: Ali Kianian <ali.kianian@qt.io>
This commit is contained in:
Marco Bubke
2025-04-12 01:15:46 +02:00
parent 95d2bc5c41
commit a8541126c4

View File

@@ -15,9 +15,12 @@
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <memory> #include <memory>
#include <ranges>
namespace QmlDesigner { namespace QmlDesigner {
namespace {
class ListModelItem : public QStandardItem class ListModelItem : public QStandardItem
{ {
public: public:
@@ -92,7 +95,6 @@ public:
bool hasInvalidValue = false; bool hasInvalidValue = false;
}; };
namespace {
QList<PropertyName> getPropertyNames(const ModelNode &listElementNode) QList<PropertyName> getPropertyNames(const ModelNode &listElementNode)
{ {
auto properties = listElementNode.variantProperties(); auto properties = listElementNode.variantProperties();
@@ -103,7 +105,7 @@ QList<PropertyName> getPropertyNames(const ModelNode &listElementNode)
for (const auto &property : properties) for (const auto &property : properties)
names.push_back(property.name().toByteArray()); names.push_back(property.name().toByteArray());
std::sort(names.begin(), names.end()); std::ranges::sort(names);
return names; return names;
} }
@@ -114,11 +116,7 @@ QList<PropertyName> mergeProperyNames(const QList<PropertyName> &first,
QList<PropertyName> merged; QList<PropertyName> merged;
merged.reserve(first.size() + second.size()); merged.reserve(first.size() + second.size());
std::set_union(first.begin(), std::ranges::set_union(first, second, std::back_inserter(merged));
first.end(),
second.begin(),
second.end(),
std::back_inserter(merged));
return merged; return merged;
} }
@@ -262,7 +260,7 @@ void ListModelEditorModel::addColumn(const QString &columnName)
{ {
PropertyName propertyName = columnName.toUtf8(); PropertyName propertyName = columnName.toUtf8();
auto found = std::lower_bound(m_propertyNames.begin(), m_propertyNames.end(), propertyName); auto found = std::ranges::lower_bound(m_propertyNames, propertyName);
if (found != m_propertyNames.end() && *found == propertyName) if (found != m_propertyNames.end() && *found == propertyName)
return; return;
@@ -298,21 +296,21 @@ void ListModelEditorModel::removeColumn(int column)
void ListModelEditorModel::removeColumns(const QList<QModelIndex> &indices) void ListModelEditorModel::removeColumns(const QList<QModelIndex> &indices)
{ {
using std::ranges::views::reverse;
std::vector<int> columns = filterColumns(indices); std::vector<int> columns = filterColumns(indices);
std::reverse(columns.begin(), columns.end()); for (int column : columns | reverse)
for (int column : columns)
removeColumn(column); removeColumn(column);
} }
void ListModelEditorModel::removeRows(const QList<QModelIndex> &indices) void ListModelEditorModel::removeRows(const QList<QModelIndex> &indices)
{ {
using std::ranges::views::reverse;
std::vector<int> rows = filterRows(indices); std::vector<int> rows = filterRows(indices);
std::reverse(rows.begin(), rows.end()); for (int row : rows | reverse)
for (int row : rows)
removeRow(row); removeRow(row);
} }
@@ -330,7 +328,7 @@ void ListModelEditorModel::renameColumn(int oldColumn, const QString &newColumnN
{ {
const PropertyName newPropertyName = newColumnName.toUtf8(); const PropertyName newPropertyName = newColumnName.toUtf8();
auto found = std::lower_bound(m_propertyNames.begin(), m_propertyNames.end(), newPropertyName); auto found = std::ranges::lower_bound(m_propertyNames, newPropertyName);
if (found != m_propertyNames.end() && *found == newPropertyName) if (found != m_propertyNames.end() && *found == newPropertyName)
return; return;
@@ -374,6 +372,8 @@ QItemSelection ListModelEditorModel::moveRowsUp(const QList<QModelIndex> &indice
QItemSelection ListModelEditorModel::moveRowsDown(const QList<QModelIndex> &indices) QItemSelection ListModelEditorModel::moveRowsDown(const QList<QModelIndex> &indices)
{ {
using std::ranges::views::reverse;
std::vector<int> rows = filterRows(indices); std::vector<int> rows = filterRows(indices);
if (rows.empty() || rows.back() >= (rowCount() - 1)) if (rows.empty() || rows.back() >= (rowCount() - 1))
@@ -381,29 +381,35 @@ QItemSelection ListModelEditorModel::moveRowsDown(const QList<QModelIndex> &indi
auto nodeListProperty = m_listModelNode.defaultNodeListProperty(); auto nodeListProperty = m_listModelNode.defaultNodeListProperty();
std::reverse(rows.begin(), rows.end()); for (int row : rows | reverse) {
for (int row : rows) {
insertRow(row + 1, takeRow(row)); insertRow(row + 1, takeRow(row));
nodeListProperty.slide(row, row + 1); nodeListProperty.slide(row, row + 1);
} }
return {index(rows.front() + 1, 0), index(rows.back() + 1, columnCount() - 1)}; return {index(rows.back() + 1, 0), index(rows.front() + 1, columnCount() - 1)};
} }
namespace {
void removeDuplicates(std::vector<int> &container)
{
std::ranges::sort(container);
auto removed = std::ranges::unique(container);
container.erase(removed.begin(), removed.end());
}
} // namespace
std::vector<int> ListModelEditorModel::filterColumns(const QList<QModelIndex> &indices) std::vector<int> ListModelEditorModel::filterColumns(const QList<QModelIndex> &indices)
{ {
std::vector<int> columns; std::vector<int> columns;
columns.reserve(indices.size()); columns.reserve(Utils::usize(indices));
for (QModelIndex index : indices) { for (QModelIndex index : indices) {
if (index.column() >= 0) if (index.column() >= 0)
columns.push_back(index.column()); columns.push_back(index.column());
} }
std::sort(columns.begin(), columns.end()); removeDuplicates(columns);
columns.erase(std::unique(columns.begin(), columns.end()), columns.end());
return columns; return columns;
} }
@@ -411,16 +417,14 @@ std::vector<int> ListModelEditorModel::filterColumns(const QList<QModelIndex> &i
std::vector<int> ListModelEditorModel::filterRows(const QList<QModelIndex> &indices) std::vector<int> ListModelEditorModel::filterRows(const QList<QModelIndex> &indices)
{ {
std::vector<int> rows; std::vector<int> rows;
rows.reserve(indices.size()); rows.reserve(Utils::usize(indices));
for (QModelIndex index : indices) { for (QModelIndex index : indices) {
if (index.row() >= 0) if (index.row() >= 0)
rows.push_back(index.row()); rows.push_back(index.row());
} }
std::sort(rows.begin(), rows.end()); removeDuplicates(rows);
rows.erase(std::unique(rows.begin(), rows.end()), rows.end());
return rows; return rows;
} }