diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-11-15 22:14:24 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-12-06 18:22:41 +0100 |
commit | 1c06f86d255d3d92201b1092794a4c1ea403c24d (patch) | |
tree | 7254ecac8bc7a74c1b5d206968c381ac6d8d1fa1 /examples/widgets | |
parent | a3e20df03d522bd1b07ac7a85578401f36f290b9 (diff) |
SimpleTreeModel: Polish TreeModel::setupModelData()
- Use QStringView.
- Use a list of a pair-like struct to represent the state instead
of 2 lists.
- Use qsizetype.
- Use constLast() to avoid detaching.
Pick-to: 6.6
Change-Id: Icc3586451f081f6166fece52675d5379160f51da
Reviewed-by: Kai Köhne <kai.koehne@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'examples/widgets')
-rw-r--r-- | examples/widgets/itemviews/simpletreemodel/treemodel.cpp | 58 | ||||
-rw-r--r-- | examples/widgets/itemviews/simpletreemodel/treemodel.h | 2 |
2 files changed, 26 insertions, 34 deletions
diff --git a/examples/widgets/itemviews/simpletreemodel/treemodel.cpp b/examples/widgets/itemviews/simpletreemodel/treemodel.cpp index 8f2e52f657..8538d99629 100644 --- a/examples/widgets/itemviews/simpletreemodel/treemodel.cpp +++ b/examples/widgets/itemviews/simpletreemodel/treemodel.cpp @@ -20,7 +20,7 @@ TreeModel::TreeModel(const QString &data, QObject *parent) : QAbstractItemModel(parent) , rootItem(std::make_unique<TreeItem>(QVariantList{tr("Title"), tr("Summary")})) { - setupModelData(data.split('\n'_L1), rootItem.get()); + setupModelData(QStringView{data}.split(u'\n'), rootItem.get()); } //! [0] @@ -109,52 +109,44 @@ int TreeModel::rowCount(const QModelIndex &parent) const } //! [8] -void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent) +void TreeModel::setupModelData(const QList<QStringView> &lines, TreeItem *parent) { - QList<TreeItem *> parents; - QList<int> indentations; - parents << parent; - indentations << 0; - - int number = 0; - - while (number < lines.count()) { - int position = 0; - while (position < lines[number].length()) { - if (lines[number].at(position) != ' '_L1) - break; - position++; - } + struct ParentIndentation + { + TreeItem *parent; + qsizetype indentation; + }; + + QList<ParentIndentation> state{{parent, 0}}; - const QString lineData = lines[number].mid(position).trimmed(); + for (const auto &line : lines) { + qsizetype position = 0; + for ( ; position < line.length() && line.at(position).isSpace(); ++position) { + } + const QStringView lineData = line.sliced(position).trimmed(); if (!lineData.isEmpty()) { // Read the column data from the rest of the line. - const QStringList columnStrings = - lineData.split('\t'_L1, Qt::SkipEmptyParts); + const auto columnStrings = lineData.split(u'\t', Qt::SkipEmptyParts); QVariantList columnData; columnData.reserve(columnStrings.count()); - for (const QString &columnString : columnStrings) - columnData << columnString; + for (const auto &columnString : columnStrings) + columnData << columnString.toString(); - if (position > indentations.last()) { + if (position > state.constLast().indentation) { // The last child of the current parent is now the new parent // unless the current parent has no children. - - if (parents.last()->childCount() > 0) { - parents << parents.last()->child(parents.last()->childCount()-1); - indentations << position; - } + auto *lastParent = state.constLast().parent; + if (lastParent->childCount() > 0) + state.append({lastParent->child(lastParent->childCount() - 1), position}); } else { - while (position < indentations.last() && parents.count() > 0) { - parents.pop_back(); - indentations.pop_back(); - } + while (position < state.constLast().indentation && !state.isEmpty()) + state.removeLast(); } // Append a new item to the current parent's list of children. - parents.last()->appendChild(std::make_unique<TreeItem>(columnData, parents.last())); + auto *lastParent = state.constLast().parent; + lastParent->appendChild(std::make_unique<TreeItem>(columnData, lastParent)); } - ++number; } } diff --git a/examples/widgets/itemviews/simpletreemodel/treemodel.h b/examples/widgets/itemviews/simpletreemodel/treemodel.h index 55c72bb3bd..1a42fa585f 100644 --- a/examples/widgets/itemviews/simpletreemodel/treemodel.h +++ b/examples/widgets/itemviews/simpletreemodel/treemodel.h @@ -32,7 +32,7 @@ public: int columnCount(const QModelIndex &parent = {}) const override; private: - static void setupModelData(const QStringList &lines, TreeItem *parent); + static void setupModelData(const QList<QStringView> &lines, TreeItem *parent); std::unique_ptr<TreeItem> rootItem; }; |