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. Change-Id: Icc3586451f081f6166fece52675d5379160f51da Reviewed-by: Kai Köhne <kai.koehne@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> (cherry picked from commit 1c06f86d255d3d92201b1092794a4c1ea403c24d) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
e19f344460
commit
4e9051417f
@ -20,7 +20,7 @@ TreeModel::TreeModel(const QString &data, QObject *parent)
|
|||||||
: QAbstractItemModel(parent)
|
: QAbstractItemModel(parent)
|
||||||
, rootItem(std::make_unique<TreeItem>(QVariantList{tr("Title"), tr("Summary")}))
|
, 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]
|
//! [0]
|
||||||
|
|
||||||
@ -109,52 +109,44 @@ int TreeModel::rowCount(const QModelIndex &parent) const
|
|||||||
}
|
}
|
||||||
//! [8]
|
//! [8]
|
||||||
|
|
||||||
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
void TreeModel::setupModelData(const QList<QStringView> &lines, TreeItem *parent)
|
||||||
{
|
{
|
||||||
QList<TreeItem *> parents;
|
struct ParentIndentation
|
||||||
QList<int> indentations;
|
{
|
||||||
parents << parent;
|
TreeItem *parent;
|
||||||
indentations << 0;
|
qsizetype indentation;
|
||||||
|
};
|
||||||
|
|
||||||
int number = 0;
|
QList<ParentIndentation> state{{parent, 0}};
|
||||||
|
|
||||||
while (number < lines.count()) {
|
for (const auto &line : lines) {
|
||||||
int position = 0;
|
qsizetype position = 0;
|
||||||
while (position < lines[number].length()) {
|
for ( ; position < line.length() && line.at(position).isSpace(); ++position) {
|
||||||
if (lines[number].at(position) != ' '_L1)
|
|
||||||
break;
|
|
||||||
position++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString lineData = lines[number].mid(position).trimmed();
|
const QStringView lineData = line.sliced(position).trimmed();
|
||||||
|
|
||||||
if (!lineData.isEmpty()) {
|
if (!lineData.isEmpty()) {
|
||||||
// Read the column data from the rest of the line.
|
// Read the column data from the rest of the line.
|
||||||
const QStringList columnStrings =
|
const auto columnStrings = lineData.split(u'\t', Qt::SkipEmptyParts);
|
||||||
lineData.split('\t'_L1, Qt::SkipEmptyParts);
|
|
||||||
QVariantList columnData;
|
QVariantList columnData;
|
||||||
columnData.reserve(columnStrings.count());
|
columnData.reserve(columnStrings.count());
|
||||||
for (const QString &columnString : columnStrings)
|
for (const auto &columnString : columnStrings)
|
||||||
columnData << columnString;
|
columnData << columnString.toString();
|
||||||
|
|
||||||
if (position > indentations.last()) {
|
if (position > state.constLast().indentation) {
|
||||||
// The last child of the current parent is now the new parent
|
// The last child of the current parent is now the new parent
|
||||||
// unless the current parent has no children.
|
// unless the current parent has no children.
|
||||||
|
auto *lastParent = state.constLast().parent;
|
||||||
if (parents.last()->childCount() > 0) {
|
if (lastParent->childCount() > 0)
|
||||||
parents << parents.last()->child(parents.last()->childCount()-1);
|
state.append({lastParent->child(lastParent->childCount() - 1), position});
|
||||||
indentations << position;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
while (position < indentations.last() && parents.count() > 0) {
|
while (position < state.constLast().indentation && !state.isEmpty())
|
||||||
parents.pop_back();
|
state.removeLast();
|
||||||
indentations.pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append a new item to the current parent's list of children.
|
// 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ public:
|
|||||||
int columnCount(const QModelIndex &parent = {}) const override;
|
int columnCount(const QModelIndex &parent = {}) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void setupModelData(const QStringList &lines, TreeItem *parent);
|
static void setupModelData(const QList<QStringView> &lines, TreeItem *parent);
|
||||||
|
|
||||||
std::unique_ptr<TreeItem> rootItem;
|
std::unique_ptr<TreeItem> rootItem;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user