Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make core::TreeModel mutable #39

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion core/treemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ namespace qnc::core {
TreeModel::TreeModel(QObject *parent)
: QAbstractItemModel{parent}
, m_root{new RootNode{this}}
{}
{
connect(this, &TreeModel::modelAboutToBeReset, this, [this] {
m_flags.setFlag(Flag::CurrentlyResetting, true);
});

connect(this, &TreeModel::modelReset, this, [this] {
m_flags.setFlag(Flag::CurrentlyResetting, false);
});
}

QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
Expand Down Expand Up @@ -146,6 +154,44 @@ int TreeModel::Node::index() const
return 0;
}

void TreeModel::Node::clear()
{
if (m_children.empty())
return;

const auto model = treeModel();
Q_ASSERT(model != nullptr);

const auto lastRow = static_cast<int>(m_children.size()) - 1;

if (!model->m_flags.testFlag(Flag::CurrentlyResetting))
model->beginRemoveRows(model->indexForNode(this), 0, lastRow);

m_children.clear();

if (!model->m_flags.testFlag(Flag::CurrentlyResetting))
model->endRemoveRows();
}

TreeModel::Node *TreeModel::Node::addChild(Pointer child)
{
const auto model = treeModel();
Q_ASSERT(model != nullptr);

const auto newRow = static_cast<int>(m_children.size());

if (!model->m_flags.testFlag(Flag::CurrentlyResetting))
model->beginInsertRows(model->indexForNode(this), newRow, newRow);

const auto childPointer = child.get();
m_children.emplace_back(std::move(child));

if (!model->m_flags.testFlag(Flag::CurrentlyResetting))
model->endInsertRows();

return childPointer;
}

TreeModel::Node *TreeModel::Node::findChild(const std::function<bool(const Pointer &)> &predicate) const
{
if (const auto it = std::find_if(m_children.cbegin(), m_children.cend(), predicate); it != m_children.cend())
Expand Down
23 changes: 12 additions & 11 deletions core/treemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ class TreeModel : public QAbstractItemModel // ---------------------------------
#endif

private:
enum class Flag {
CurrentlyResetting = 1 << 0,
};

Q_DECLARE_FLAGS(Flags, Flag);

RootNode *const m_root;
Flags m_flags;
};

class TreeModel::Node // ------------------------------------------------------------------------------- TreeModel::Node
Expand Down Expand Up @@ -91,11 +98,15 @@ class TreeModel::Node // -------------------------------------------------------
template<class NodeType, auto IdField, class ContainerType>
void updateOrAddChildren(const ContainerType &container);

void clear();

protected:
template<class NodeType>
const NodeType *parent() const { return dynamic_cast<const NodeType *>(m_parent); }

private:
Node *addChild(Pointer child);

const Node *const m_parent;
std::vector<Pointer> m_children = {};
};
Expand Down Expand Up @@ -157,18 +168,8 @@ inline void core::TreeModel::ValueNode<ValueType, BaseType>::update(const ValueT
template<class NodeType, typename ...Args>
inline NodeType *TreeModel::Node::addChild(Args &&...args)
{
const auto model = treeModel();
Q_ASSERT(model != nullptr);

const auto newRow = static_cast<int>(m_children.size());
model->beginInsertRows(model->indexForNode(this), newRow, newRow);

auto node = std::make_unique<NodeType>(std::forward<Args>(args)..., this);
const auto nodePointer = node.get();
m_children.emplace_back(std::move(node));

model->endInsertRows();
return nodePointer;
return static_cast<NodeType *>(addChild(std::move(node)));
}

template<class NodeType, typename ...Args>
Expand Down
86 changes: 77 additions & 9 deletions tests/auto/tst_coremodels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class TestTreeModel : public TreeModel // --------------------------------------

QModelIndex addNode(const Data &data, const QModelIndex &parent = {});
void addNodes(const QList<Data> &dataList, const QModelIndex &parent = {});
void reset(const QList<Data> &dataList);
void removeChildren(const QModelIndex &parent);
};

QModelIndex TestTreeModel::addNode(const Data &data, const QModelIndex &parent)
Expand All @@ -119,6 +121,22 @@ void TestTreeModel::addNodes(const QList<Data> &dataList, const QModelIndex &par
}
}

void TestTreeModel::reset(const QList<Data> &dataList)
{
beginResetModel();

root()->clear();
addNodes(dataList);

endResetModel();
}

void TestTreeModel::removeChildren(const QModelIndex &parent)
{
if (const auto node = nodeForIndex(parent))
node->clear();
}

// ----------------------------------------------------------------------------------------------------------- utilities

template <typename T>
Expand Down Expand Up @@ -222,23 +240,73 @@ private slots:
{
const QFETCH(DetailModel::RowList, rows);

auto model = TestTreeModel{};
const auto tester = QAbstractItemModelTester{&model};
auto modelReset = QSignalSpy{&model, &DetailModel::modelReset};
auto rowsInserted = QSignalSpy{&model, &DetailModel::rowsInserted};
auto model = TestTreeModel{};
const auto tester = QAbstractItemModelTester{&model};
auto modelReset = QSignalSpy{&model, &DetailModel::modelReset};
auto rowsInserted = QSignalSpy{&model, &DetailModel::rowsInserted};
auto rowsRemoved = QSignalSpy{&model, &DetailModel::rowsRemoved};

QCOMPARE( modelReset.count(), 0);
QCOMPARE( modelReset.count(), 0); // ----------------------------------------------------- verify initial state
QCOMPARE(rowsInserted.count(), 0);
QCOMPARE( rowsRemoved.count(), 0);

model.addNodes(rows);
compareTreeModel(model, {}, {});
QVERIFY(!QTest::currentTestFailed());

if (QTest::currentTestFailed())
return;
model.addNodes(rows); // ----------------------------------------------------------------------- test insertions
QVERIFY(!QTest::currentTestFailed());

QCOMPARE( modelReset.count(), 0);
QCOMPARE( modelReset.count(), 0); // -------------------------------------------- verify state after insertions
QCOMPARE(rowsInserted.count(), flatten(rows).size());
QCOMPARE( rowsRemoved.count(), 0);

if (!rowsInserted.isEmpty()) {
QCOMPARE(rowsInserted[0][0], QModelIndex{});
QCOMPARE(rowsInserted[0][1], 0);
QCOMPARE(rowsInserted[0][2], 0);
rowsInserted.clear();
}

compareTreeModel(model, {}, rows);
QVERIFY(!QTest::currentTestFailed());

model.reset({}); // --------------------------------------------------------------- test clearing by model reset
QVERIFY(!QTest::currentTestFailed());

QCOMPARE( modelReset.count(), 1); // ---------------------------------------------- verify state after clearing
QCOMPARE(rowsInserted.count(), 0);
QCOMPARE( rowsRemoved.count(), 0);

modelReset.clear();

compareTreeModel(model, {}, {});
QVERIFY(!QTest::currentTestFailed());

model.reset(rows); // -------------------------------------------------------------- test filling by model reset
QVERIFY(!QTest::currentTestFailed());

QCOMPARE( modelReset.count(), 1); // ----------------------------------------------- verify state after filling
QCOMPARE(rowsInserted.count(), 0);
QCOMPARE( rowsRemoved.count(), 0);

modelReset.clear();

compareTreeModel(model, {}, rows);
QVERIFY(!QTest::currentTestFailed());

model.removeChildren({}); // --------------------------------------------------------- test clearing by removing
QVERIFY(!QTest::currentTestFailed());

QCOMPARE( modelReset.count(), 0); // ---------------------------------------------- verify state after clearing
QCOMPARE(rowsInserted.count(), 0);
QCOMPARE( rowsRemoved.count(), rows.isEmpty() ? 0 : 1);

if (!rowsRemoved.isEmpty()) {
QCOMPARE(rowsRemoved[0][0], QModelIndex{});
QCOMPARE(rowsRemoved[0][1], 0);
QCOMPARE(rowsRemoved[0][2], rows.size() - 1);
rowsRemoved.clear();
}
}

private:
Expand Down
Loading