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