QmlDesigner: Add node list iterators

Task-number: QDS-4159
Change-Id: Ie6635d87beb66f766b9995f68972b80baa100ed6
Reviewed-by: Henning Gründl <henning.gruendl@qt.io>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2021-04-14 15:30:43 +02:00
parent 2e8e47622c
commit 040854c6f1
9 changed files with 676 additions and 15 deletions

View File

@@ -236,7 +236,7 @@ public:
bool isSubclassOf(const TypeName &typeName, int majorVersion = -1, int minorVersion = -1) const;
QIcon typeIcon() const;
friend void swap(ModelNode &first, ModelNode &second)
friend void swap(ModelNode &first, ModelNode &second) noexcept
{
using std::swap;

View File

@@ -25,20 +25,150 @@
#pragma once
#include "qmldesignercorelib_global.h"
#include "modelnode.h"
#include "nodeabstractproperty.h"
#include "qmldesignercorelib_global.h"
#include <QList>
#include <iterator>
namespace QmlDesigner {
namespace Internal {
class ModelPrivate;
class InternalNodeListProperty;
using InternalNodeListPropertyPointer = QSharedPointer<InternalNodeListProperty>;
}
class ModelPrivate;
class InternalNodeListProperty;
using InternalNodeListPropertyPointer = QSharedPointer<InternalNodeListProperty>;
class NodeListPropertyIterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = int;
using value_type = ModelNode;
using pointer = InternalNodeListPropertyPointer;
using reference = ModelNode;
NodeListPropertyIterator(int currentIndex,
class InternalNodeListProperty *nodeListProperty,
Model *model,
AbstractView *view)
: m_nodeListProperty{nodeListProperty}
, m_model{model}
, m_view{view}
, m_currentIndex{currentIndex}
{}
NodeListPropertyIterator &operator++()
{
++m_currentIndex;
return *this;
}
NodeListPropertyIterator operator++(difference_type)
{
auto tmp = *this;
++m_currentIndex;
return tmp;
}
NodeListPropertyIterator &operator--()
{
--m_currentIndex;
return *this;
}
NodeListPropertyIterator operator--(difference_type)
{
auto tmp = *this;
--m_currentIndex;
return tmp;
}
NodeListPropertyIterator &operator+=(difference_type index)
{
m_currentIndex += index;
return *this;
}
NodeListPropertyIterator &operator-=(difference_type index)
{
m_currentIndex -= index;
return *this;
}
NodeListPropertyIterator operator[](difference_type index) const
{
return {index, m_nodeListProperty, m_model, m_view};
}
friend NodeListPropertyIterator operator+(const NodeListPropertyIterator &first,
difference_type index)
{
return {first.m_currentIndex + index, first.m_nodeListProperty, first.m_model, first.m_view};
}
friend NodeListPropertyIterator operator+(difference_type index,
const NodeListPropertyIterator &second)
{
return second + index;
}
friend NodeListPropertyIterator operator-(const NodeListPropertyIterator &first,
difference_type index)
{
return first + (-index);
}
friend difference_type operator-(const NodeListPropertyIterator &first,
const NodeListPropertyIterator &second)
{
return first.m_currentIndex - second.m_currentIndex;
}
friend bool operator==(const NodeListPropertyIterator &first,
const NodeListPropertyIterator &second)
{
return first.m_currentIndex == second.m_currentIndex;
}
friend bool operator!=(const NodeListPropertyIterator &first,
const NodeListPropertyIterator &second)
{
return !(first == second);
}
friend bool operator<(const NodeListPropertyIterator &first, const NodeListPropertyIterator &second)
{
return first.m_currentIndex < second.m_currentIndex;
}
friend bool operator<=(const NodeListPropertyIterator &first,
const NodeListPropertyIterator &second)
{
return first.m_currentIndex <= second.m_currentIndex;
}
friend bool operator>(const NodeListPropertyIterator &first, const NodeListPropertyIterator &second)
{
return first.m_currentIndex > second.m_currentIndex;
}
friend bool operator>=(const NodeListPropertyIterator &first,
const NodeListPropertyIterator &second)
{
return first.m_currentIndex >= second.m_currentIndex;
}
value_type operator*() const;
private:
InternalNodeListProperty *m_nodeListProperty{};
Model *m_model{};
AbstractView *m_view{};
int m_currentIndex = -1;
};
} // namespace Internal
class QMLDESIGNERCORE_EXPORT NodeListProperty : public NodeAbstractProperty
{
@@ -46,6 +176,13 @@ class QMLDESIGNERCORE_EXPORT NodeListProperty : public NodeAbstractProperty
friend class QmlDesigner::AbstractProperty;
friend class QmlDesigner::Internal::ModelPrivate;
public:
using value_type = ModelNode;
using iterator = Internal::NodeListPropertyIterator;
using const_iterator = iterator;
using size_type = int;
using difference_type = int;
using reference = ModelNode;
NodeListProperty();
NodeListProperty(const NodeListProperty &nodeListProperty, AbstractView *view);
QList<ModelNode> toModelNodeList() const;
@@ -57,6 +194,12 @@ public:
static void reverseModelNodes(const QList<ModelNode> &nodes);
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
protected:
NodeListProperty(const PropertyName &propertyName, const Internal::InternalNodePointer &internalNode, Model* model, AbstractView *view);
NodeListProperty(const Internal::InternalNodeListPropertyPointer &internalNodeListProperty, Model* model, AbstractView *view);

View File

@@ -68,12 +68,6 @@ int InternalNodeListProperty::indexOf(const InternalNode::Pointer &node) const
return m_nodeList.indexOf(node);
}
InternalNode::Pointer InternalNodeListProperty::at(int index) const
{
Q_ASSERT(index >=0 || index < m_nodeList.count());
return InternalNode::Pointer(m_nodeList.at(index));
}
bool InternalNodeListProperty::isNodeListProperty() const
{
return true;

View File

@@ -33,7 +33,7 @@ namespace QmlDesigner {
namespace Internal {
class InternalNodeListProperty : public InternalNodeAbstractProperty
class InternalNodeListProperty final : public InternalNodeAbstractProperty
{
public:
using Pointer = QSharedPointer<InternalNodeListProperty>;
@@ -43,9 +43,27 @@ public:
bool isValid() const override;
bool isEmpty() const override;
int size() const { return m_nodeList.size(); }
int count() const override;
int indexOf(const InternalNodePointer &node) const override;
InternalNodePointer at(int index) const;
const InternalNodePointer &at(int index) const
{
Q_ASSERT(index >= 0 && index < m_nodeList.count());
return m_nodeList[index];
}
InternalNodePointer &at(int index)
{
Q_ASSERT(index >= 0 && index < m_nodeList.count());
return m_nodeList[index];
}
InternalNodePointer &find(InternalNodePointer node)
{
auto found = std::find(m_nodeList.begin(), m_nodeList.end(), node);
return *found;
}
bool isNodeListProperty() const override;

View File

@@ -36,6 +36,11 @@
namespace QmlDesigner {
Internal::NodeListPropertyIterator::value_type Internal::NodeListPropertyIterator::operator*() const
{
return {m_nodeListProperty->at(m_currentIndex), m_model, m_view};
}
NodeListProperty::NodeListProperty() = default;
NodeListProperty::NodeListProperty(const NodeListProperty &property, AbstractView *view)
@@ -157,4 +162,27 @@ void NodeListProperty::reverseModelNodes(const QList<ModelNode> &nodes)
parentProperty.swap(selectedNodeIndices[i], selectedNodeIndices[selectedNodeIndices.size() - 1 - i]);
}
Internal::NodeListPropertyIterator NodeListProperty::begin()
{
return const_cast<const NodeListProperty *>(this)->begin();
}
Internal::NodeListPropertyIterator NodeListProperty::end()
{
return const_cast<const NodeListProperty *>(this)->end();
}
Internal::NodeListPropertyIterator NodeListProperty::begin() const
{
return {0, internalNode()->nodeListProperty(name()).data(), model(), view()};
}
Internal::NodeListPropertyIterator NodeListProperty::end() const
{
auto nodeListProperty = internalNode()->nodeListProperty(name());
auto size = nodeListProperty ? nodeListProperty->size() : 0;
return {size, nodeListProperty.data(), model(), view()};
}
} // namespace QmlDesigner

View File

@@ -36,6 +36,7 @@ add_qtc_test(unittest GTEST
ECHOSERVER="$<TARGET_FILE_DIR:echo>/echo"
CPPTOOLS_JSON="${CMAKE_CURRENT_BINARY_DIR}/CppTools.json"
SOURCES
abstractviewmock.h
builddependenciesprovider-test.cpp
builddependenciesstorage-test.cpp
clangindexingsettingsmanager-test.cpp
@@ -123,6 +124,7 @@ add_qtc_test(unittest GTEST
modifiedtimechecker-test.cpp
nativefilepath-test.cpp
nativefilepathview-test.cpp
nodelistproperty-test.cpp
pchmanagerclientserverinprocess-test.cpp
pchmanagerclient-test.cpp
pchmanagerserver-test.cpp

View File

@@ -0,0 +1,35 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <googletest.h>
#include <qmldesigner/designercore/include/abstractview.h>
class AbstractViewMock : public QmlDesigner::AbstractView
{
public:
};

View File

@@ -0,0 +1,439 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "abstractviewmock.h"
#include "googletest.h"
#include <model.h>
#include <modelnode.h>
#include <nodelistproperty.h>
namespace {
using QmlDesigner::ModelNode;
class NodeListProperty : public testing::Test
{
protected:
using iterator = QmlDesigner::NodeListProperty::iterator;
NodeListProperty()
{
model->attachView(&abstractViewMock);
nodeListProperty = abstractViewMock.rootModelNode().nodeListProperty("foo");
node1 = abstractViewMock.createModelNode("QtQick.Item1", -1, -1);
node2 = abstractViewMock.createModelNode("QtQick.Item2", -1, -1);
node3 = abstractViewMock.createModelNode("QtQick.Item3", -1, -1);
node4 = abstractViewMock.createModelNode("QtQick.Item4", -1, -1);
node5 = abstractViewMock.createModelNode("QtQick.Item5", -1, -1);
nodeListProperty.reparentHere(node1);
nodeListProperty.reparentHere(node2);
nodeListProperty.reparentHere(node3);
nodeListProperty.reparentHere(node4);
nodeListProperty.reparentHere(node5);
}
~NodeListProperty() { model->detachView(&abstractViewMock); }
std::vector<ModelNode> nodes() const
{
return std::vector<ModelNode>{nodeListProperty.begin(), nodeListProperty.end()};
}
static std::vector<ModelNode> nodes(iterator begin, iterator end)
{
return std::vector<ModelNode>{begin, end};
}
protected:
std::unique_ptr<QmlDesigner::Model> model{QmlDesigner::Model::create("QtQuick.Item")};
AbstractViewMock abstractViewMock;
QmlDesigner::NodeListProperty nodeListProperty;
ModelNode node1;
ModelNode node2;
ModelNode node3;
ModelNode node4;
ModelNode node5;
};
TEST_F(NodeListProperty, BeginAndEndItertors)
{
std::vector<ModelNode> nodes{nodeListProperty.begin(), nodeListProperty.end()};
ASSERT_THAT(nodes, ElementsAre(node1, node2, node3, node4, node5));
}
TEST_F(NodeListProperty, LoopOverRange)
{
std::vector<ModelNode> nodes;
for (const ModelNode &node : nodeListProperty)
nodes.push_back(node);
ASSERT_THAT(nodes, ElementsAre(node1, node2, node3, node4, node5));
}
TEST_F(NodeListProperty, NextIterator)
{
auto begin = nodeListProperty.begin();
auto nextIterator = std::next(begin);
ASSERT_THAT(nodes(nextIterator, nodeListProperty.end()), ElementsAre(node2, node3, node4, node5));
}
TEST_F(NodeListProperty, PreviousIterator)
{
auto end = nodeListProperty.end();
auto previousIterator = std::prev(end);
ASSERT_THAT(nodes(nodeListProperty.begin(), previousIterator),
ElementsAre(node1, node2, node3, node4));
}
TEST_F(NodeListProperty, IncrementIterator)
{
auto incrementIterator = nodeListProperty.begin();
++incrementIterator;
ASSERT_THAT(nodes(incrementIterator, nodeListProperty.end()),
ElementsAre(node2, node3, node4, node5));
}
TEST_F(NodeListProperty, IncrementIteratorReturnsIterator)
{
auto begin = nodeListProperty.begin();
auto incrementIterator = ++begin;
ASSERT_THAT(nodes(incrementIterator, nodeListProperty.end()),
ElementsAre(node2, node3, node4, node5));
}
TEST_F(NodeListProperty, PostIncrementIterator)
{
auto postIncrementIterator = nodeListProperty.begin();
postIncrementIterator++;
ASSERT_THAT(nodes(postIncrementIterator, nodeListProperty.end()),
ElementsAre(node2, node3, node4, node5));
}
TEST_F(NodeListProperty, PostIncrementIteratorReturnsPreincrementIterator)
{
auto begin = nodeListProperty.begin();
auto postIncrementIterator = begin++;
ASSERT_THAT(nodes(postIncrementIterator, nodeListProperty.end()),
ElementsAre(node1, node2, node3, node4, node5));
}
TEST_F(NodeListProperty, DecrementIterator)
{
auto decrementIterator = nodeListProperty.end();
--decrementIterator;
ASSERT_THAT(nodes(nodeListProperty.begin(), decrementIterator),
ElementsAre(node1, node2, node3, node4));
}
TEST_F(NodeListProperty, DerementIteratorReturnsIterator)
{
auto end = nodeListProperty.end();
auto decrementIterator = --end;
ASSERT_THAT(nodes(nodeListProperty.begin(), decrementIterator),
ElementsAre(node1, node2, node3, node4));
}
TEST_F(NodeListProperty, PostDecrementIterator)
{
auto postDecrementIterator = nodeListProperty.end();
postDecrementIterator--;
ASSERT_THAT(nodes(nodeListProperty.begin(), postDecrementIterator),
ElementsAre(node1, node2, node3, node4));
}
TEST_F(NodeListProperty, PostDecrementIteratorReturnsPredecrementIterator)
{
auto end = nodeListProperty.end();
auto postDecrementIterator = end--;
ASSERT_THAT(nodes(nodeListProperty.begin(), postDecrementIterator),
ElementsAre(node1, node2, node3, node4, node5));
}
TEST_F(NodeListProperty, IncrementIteratorByTwo)
{
auto incrementIterator = nodeListProperty.begin();
incrementIterator += 2;
ASSERT_THAT(nodes(incrementIterator, nodeListProperty.end()), ElementsAre(node3, node4, node5));
}
TEST_F(NodeListProperty, IncrementIteratorByTwoReturnsIterator)
{
auto begin = nodeListProperty.begin();
auto incrementIterator = begin += 2;
ASSERT_THAT(nodes(incrementIterator, nodeListProperty.end()), ElementsAre(node3, node4, node5));
}
TEST_F(NodeListProperty, DecrementIteratorByTwo)
{
auto decrementIterator = nodeListProperty.end();
decrementIterator -= 2;
ASSERT_THAT(nodes(nodeListProperty.begin(), decrementIterator), ElementsAre(node1, node2, node3));
}
TEST_F(NodeListProperty, DecrementIteratorByTwoReturnsIterator)
{
auto end = nodeListProperty.end();
auto decrementIterator = end -= 2;
ASSERT_THAT(nodes(nodeListProperty.begin(), decrementIterator), ElementsAre(node1, node2, node3));
}
TEST_F(NodeListProperty, AccessIterator)
{
auto iterator = std::next(nodeListProperty.begin(), 3);
auto accessIterator = iterator[2];
ASSERT_THAT(nodes(accessIterator, nodeListProperty.end()), ElementsAre(node3, node4, node5));
}
TEST_F(NodeListProperty, AddIteratorByIndexSecondOperand)
{
auto begin = nodeListProperty.begin();
auto addedIterator = begin + 2;
ASSERT_THAT(nodes(addedIterator, nodeListProperty.end()), ElementsAre(node3, node4, node5));
}
TEST_F(NodeListProperty, AddIteratorByIndexFirstOperand)
{
auto begin = nodeListProperty.begin();
auto addedIterator = 2 + begin;
ASSERT_THAT(nodes(addedIterator, nodeListProperty.end()), ElementsAre(node3, node4, node5));
}
TEST_F(NodeListProperty, SubtractIterator)
{
auto end = nodeListProperty.end();
auto subtractedIterator = end - 3;
ASSERT_THAT(nodes(subtractedIterator, nodeListProperty.end()), ElementsAre(node3, node4, node5));
}
TEST_F(NodeListProperty, CompareEqualIteratorAreEqual)
{
auto first = nodeListProperty.begin();
auto second = nodeListProperty.begin();
auto isEqual = first == second;
ASSERT_TRUE(isEqual);
}
TEST_F(NodeListProperty, CompareEqualIteratorAreNotEqual)
{
auto first = nodeListProperty.begin();
auto second = std::next(nodeListProperty.begin());
auto isEqual = first == second;
ASSERT_FALSE(isEqual);
}
TEST_F(NodeListProperty, CompareUnqualIteratorAreEqual)
{
auto first = nodeListProperty.begin();
auto second = nodeListProperty.begin();
auto isUnequal = first != second;
ASSERT_FALSE(isUnequal);
}
TEST_F(NodeListProperty, CompareUnequalIteratorAreNotEqual)
{
auto first = nodeListProperty.begin();
auto second = std::next(nodeListProperty.begin());
auto isUnequal = first != second;
ASSERT_TRUE(isUnequal);
}
TEST_F(NodeListProperty, CompareLessIteratorAreNotLessIfEqual)
{
auto first = nodeListProperty.begin();
auto second = nodeListProperty.begin();
auto isLess = first < second;
ASSERT_FALSE(isLess);
}
TEST_F(NodeListProperty, CompareLessIteratorAreNotLessIfGreater)
{
auto first = std::next(nodeListProperty.begin());
auto second = nodeListProperty.begin();
auto isLess = first < second;
ASSERT_FALSE(isLess);
}
TEST_F(NodeListProperty, CompareLessIteratorAreLessIfLess)
{
auto first = nodeListProperty.begin();
auto second = std::next(nodeListProperty.begin());
auto isLess = first < second;
ASSERT_TRUE(isLess);
}
TEST_F(NodeListProperty, CompareLessEqualIteratorAreLessEqualIfEqual)
{
auto first = nodeListProperty.begin();
auto second = nodeListProperty.begin();
auto isLessEqual = first <= second;
ASSERT_TRUE(isLessEqual);
}
TEST_F(NodeListProperty, CompareLessEqualIteratorAreNotLessEqualIfGreater)
{
auto first = std::next(nodeListProperty.begin());
auto second = nodeListProperty.begin();
auto isLessEqual = first <= second;
ASSERT_FALSE(isLessEqual);
}
TEST_F(NodeListProperty, CompareLessEqualIteratorAreLessEqualIfLess)
{
auto first = nodeListProperty.begin();
auto second = std::next(nodeListProperty.begin());
auto isLessEqual = first <= second;
ASSERT_TRUE(isLessEqual);
}
TEST_F(NodeListProperty, CompareGreaterIteratorAreGreaterIfEqual)
{
auto first = nodeListProperty.begin();
auto second = nodeListProperty.begin();
auto isGreater = first > second;
ASSERT_FALSE(isGreater);
}
TEST_F(NodeListProperty, CompareGreaterIteratorAreGreaterIfGreater)
{
auto first = std::next(nodeListProperty.begin());
auto second = nodeListProperty.begin();
auto isGreater = first > second;
ASSERT_TRUE(isGreater);
}
TEST_F(NodeListProperty, CompareGreaterIteratorAreNotGreaterIfLess)
{
auto first = nodeListProperty.begin();
auto second = std::next(nodeListProperty.begin());
auto isGreater = first > second;
ASSERT_FALSE(isGreater);
}
TEST_F(NodeListProperty, CompareGreaterEqualIteratorAreGreaterEqualIfEqual)
{
auto first = nodeListProperty.begin();
auto second = nodeListProperty.begin();
auto isGreaterEqual = first >= second;
ASSERT_TRUE(isGreaterEqual);
}
TEST_F(NodeListProperty, CompareGreaterEqualIteratorAreGreaterEqualIfGreater)
{
auto first = std::next(nodeListProperty.begin());
auto second = nodeListProperty.begin();
auto isGreaterEqual = first >= second;
ASSERT_TRUE(isGreaterEqual);
}
TEST_F(NodeListProperty, CompareGreaterEqualIteratorAreNotGreaterEqualIfLess)
{
auto first = nodeListProperty.begin();
auto second = std::next(nodeListProperty.begin());
auto isGreaterEqual = first >= second;
ASSERT_FALSE(isGreaterEqual);
}
TEST_F(NodeListProperty, DereferenceIterator)
{
auto iterator = std::next(nodeListProperty.begin());
auto node = *iterator;
ASSERT_THAT(node, Eq(node2));
}
} // namespace

View File

@@ -67,6 +67,7 @@ SOURCES += \
gtest-creator-printing.cpp \
gtest-qt-printing.cpp \
asynchronousimagecache-test.cpp \
nodelistproperty-test.cpp \
synchronousimagecache-test.cpp \
imagecachegenerator-test.cpp \
imagecachestorage-test.cpp \
@@ -233,6 +234,7 @@ SOURCES += \
}
HEADERS += \
abstractviewmock.h \
compare-operators.h \
conditionally-disabled-tests.h \
dummyclangipcclient.h \