Skip to content

Commit

Permalink
Add method to insert child nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 22, 2023
1 parent 0774c80 commit dee7605
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
19 changes: 17 additions & 2 deletions python/gui/auto_generated/history/qgshistoryentrynode.sip.in
Expand Up @@ -8,7 +8,6 @@




class QgsHistoryEntryNode
{
%Docstring(signature="appended")
Expand All @@ -23,14 +22,17 @@ Base class for nodes representing a :py:class:`QgsHistoryEntry`.
public:

QgsHistoryEntryNode();
%Docstring
Constructor for QgsHistoryEntryNode.
%End
virtual ~QgsHistoryEntryNode();


QgsHistoryEntryGroup *parent();
%Docstring
Returns the node's parent node.

If parent is ``None``, the node is a root node
If parent is ``None``, the node is a root node.
%End

virtual QVariant data( int role = Qt::DisplayRole ) const = 0;
Expand Down Expand Up @@ -63,12 +65,23 @@ Base class for history entry "group" nodes, which contain children of their own.
public:

QgsHistoryEntryGroup();
%Docstring
Constructor for QgsHistoryEntryGroup
%End
~QgsHistoryEntryGroup();


void addChild( QgsHistoryEntryNode *child /Transfer/ );
%Docstring
Adds a ``child`` node to this node.

Ownership is transferred to the group.
%End

void insertChild( int index, QgsHistoryEntryNode *child /Transfer/ );
%Docstring
Inserts a ``child`` node at the specified index.

Ownership is transferred to the group.
%End

Expand Down Expand Up @@ -99,6 +112,8 @@ Clears the group, removing all its children.
virtual int childCount() const ${SIP_FINAL};


protected:

private:
QgsHistoryEntryGroup( const QgsHistoryEntryGroup &other );
};
Expand Down
11 changes: 11 additions & 0 deletions src/gui/history/qgshistoryentrynode.cpp
Expand Up @@ -65,6 +65,17 @@ void QgsHistoryEntryGroup::addChild( QgsHistoryEntryNode *child )
mChildren.emplace_back( child );
}

void QgsHistoryEntryGroup::insertChild( int index, QgsHistoryEntryNode *child )
{
if ( !child )
return;

Q_ASSERT( !child->mParent );
child->mParent = this;

mChildren.insert( mChildren.begin() + index, std::unique_ptr< QgsHistoryEntryNode >( child ) );
}

int QgsHistoryEntryGroup::indexOf( QgsHistoryEntryNode *child ) const
{
if ( child->mParent != this )
Expand Down
12 changes: 11 additions & 1 deletion src/gui/history/qgshistoryentrynode.h
Expand Up @@ -67,6 +67,7 @@ class GUI_EXPORT QgsHistoryEntryNode
virtual int childCount() const;

#if 0 // currently unused

/**
* Returns a HTML formatted text string which should be shown to a user when
* selecting the node.
Expand Down Expand Up @@ -137,6 +138,13 @@ class GUI_EXPORT QgsHistoryEntryGroup : public QgsHistoryEntryNode
*/
void addChild( QgsHistoryEntryNode *child SIP_TRANSFER );

/**
* Inserts a \a child node at the specified index.
*
* Ownership is transferred to the group.
*/
void insertChild( int index, QgsHistoryEntryNode *child SIP_TRANSFER );

/**
* Returns the index of the specified \a child node.
*
Expand All @@ -161,12 +169,14 @@ class GUI_EXPORT QgsHistoryEntryGroup : public QgsHistoryEntryNode

int childCount() const FINAL;

protected:
std::deque< std::unique_ptr< QgsHistoryEntryNode > > mChildren SIP_SKIP;

private:
#ifdef SIP_RUN
QgsHistoryEntryGroup( const QgsHistoryEntryGroup &other );
#endif

std::deque< std::unique_ptr< QgsHistoryEntryNode > > mChildren SIP_SKIP;

};

Expand Down
40 changes: 40 additions & 0 deletions tests/src/python/test_qgshistoryproviderregistry.py
Expand Up @@ -282,6 +282,46 @@ def test_nodes(self):
self.assertEqual(group.indexOf(node), 0)
self.assertEqual(group.indexOf(node2), 1)

# insert
node3 = TestNode()
group.insertChild(1, node3)
self.assertEqual(group.childCount(), 3)
self.assertEqual(node3.parent(), group)
self.assertEqual(group.childAt(0), node)
self.assertEqual(group.childAt(1), node3)
self.assertEqual(group.childAt(2), node2)
self.assertEqual(group.indexOf(node), 0)
self.assertEqual(group.indexOf(node3), 1)
self.assertEqual(group.indexOf(node2), 2)

node4 = TestNode()
group.insertChild(0, node4)
self.assertEqual(group.childCount(), 4)
self.assertEqual(node4.parent(), group)
self.assertEqual(group.childAt(0), node4)
self.assertEqual(group.childAt(1), node)
self.assertEqual(group.childAt(2), node3)
self.assertEqual(group.childAt(3), node2)
self.assertEqual(group.indexOf(node4), 0)
self.assertEqual(group.indexOf(node), 1)
self.assertEqual(group.indexOf(node3), 2)
self.assertEqual(group.indexOf(node2), 3)

node5 = TestNode()
group.insertChild(4, node5)
self.assertEqual(group.childCount(), 5)
self.assertEqual(node5.parent(), group)
self.assertEqual(group.childAt(0), node4)
self.assertEqual(group.childAt(1), node)
self.assertEqual(group.childAt(2), node3)
self.assertEqual(group.childAt(3), node2)
self.assertEqual(group.childAt(4), node5)
self.assertEqual(group.indexOf(node4), 0)
self.assertEqual(group.indexOf(node), 1)
self.assertEqual(group.indexOf(node3), 2)
self.assertEqual(group.indexOf(node2), 3)
self.assertEqual(group.indexOf(node5), 4)

group.clear()
self.assertEqual(group.childCount(), 0)

Expand Down

0 comments on commit dee7605

Please sign in to comment.