Add method to insert child nodes

This commit is contained in:
Nyall Dawson 2023-04-19 12:34:51 +10:00
parent 0774c80f3e
commit dee7605b90
4 changed files with 79 additions and 3 deletions

View File

@ -8,7 +8,6 @@
class QgsHistoryEntryNode
{
%Docstring(signature="appended")
@ -23,6 +22,9 @@ Base class for nodes representing a :py:class:`QgsHistoryEntry`.
public:
QgsHistoryEntryNode();
%Docstring
Constructor for QgsHistoryEntryNode.
%End
virtual ~QgsHistoryEntryNode();
@ -30,7 +32,7 @@ Base class for nodes representing a :py:class:`QgsHistoryEntry`.
%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;
@ -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
@ -99,6 +112,8 @@ Clears the group, removing all its children.
virtual int childCount() const ${SIP_FINAL};
protected:
private:
QgsHistoryEntryGroup( const QgsHistoryEntryGroup &other );
};

View File

@ -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 )

View File

@ -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.
@ -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.
*
@ -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;
};

View File

@ -282,6 +282,46 @@ class TestQgsHistoryProviderRegistry(unittest.TestCase):
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)