Fix undo/redo for groups

This commit is contained in:
Nyall Dawson 2017-10-09 17:31:12 +10:00
parent e17b32c6e2
commit 02acbb4184
12 changed files with 503 additions and 174 deletions

View File

@ -30,6 +30,14 @@ class QgsLayoutItemGroup: QgsLayoutItem
virtual QString displayName() const;
static QgsLayoutItemGroup *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
%Docstring
Returns a new group item for the specified ``layout``.
The caller takes responsibility for deleting the returned object.
:rtype: QgsLayoutItemGroup
%End
void addItem( QgsLayoutItem *item /Transfer/ );
%Docstring
Adds an ``item`` to the group. Ownership of the item
@ -56,6 +64,11 @@ class QgsLayoutItemGroup: QgsLayoutItem
virtual void attemptResize( const QgsLayoutSize &size );
virtual bool writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
virtual bool readXml( const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context );
virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget );

View File

@ -366,6 +366,7 @@ SET(QGIS_CORE_SRCS
layout/qgslayoutguidecollection.cpp
layout/qgslayoutitem.cpp
layout/qgslayoutitemgroup.cpp
layout/qgslayoutitemgroupundocommand.cpp
layout/qgslayoutitemmap.cpp
layout/qgslayoutitempage.cpp
layout/qgslayoutitemregistry.cpp
@ -716,6 +717,7 @@ SET(QGIS_CORE_MOC_HDRS
layout/qgslayoutguidecollection.h
layout/qgslayoutitem.h
layout/qgslayoutitemgroup.h
layout/qgslayoutitemgroupundocommand.h
layout/qgslayoutitemmap.h
layout/qgslayoutitempage.h
layout/qgslayoutitemregistry.h

View File

@ -23,6 +23,7 @@
#include "qgsproject.h"
#include "qgslayoutitemundocommand.h"
#include "qgslayoutitemgroup.h"
#include "qgslayoutitemgroupundocommand.h"
QgsLayout::QgsLayout( QgsProject *project )
: mProject( project )
@ -382,10 +383,16 @@ void QgsLayout::removeLayoutItem( QgsLayoutItem *item )
{
std::unique_ptr< QgsLayoutItemDeleteUndoCommand > deleteCommand;
if ( !mBlockUndoCommands )
{
mUndoStack->beginMacro( tr( "Deleted item" ) );
deleteCommand.reset( new QgsLayoutItemDeleteUndoCommand( item, tr( "Deleted item" ) ) );
}
removeLayoutItemPrivate( item );
if ( deleteCommand )
{
mUndoStack->stack()->push( deleteCommand.release() );
mUndoStack->endMacro();
}
}
QgsLayoutUndoStack *QgsLayout::undoStack()
@ -456,20 +463,17 @@ QgsLayoutItemGroup *QgsLayout::groupItems( const QList<QgsLayoutItem *> &items )
}
QgsLayoutItemGroup *returnGroup = itemGroup.get();
addLayoutItem( itemGroup.release() );
mUndoStack->endMacro();
std::unique_ptr< QgsLayoutItemGroupUndoCommand > c( new QgsLayoutItemGroupUndoCommand( QgsLayoutItemGroupUndoCommand::Grouped, returnGroup, this, tr( "Items grouped" ) ) );
mUndoStack->stack()->push( c.release() );
mProject->setDirty( true );
#if 0
QgsGroupUngroupItemsCommand *c = new QgsGroupUngroupItemsCommand( QgsGroupUngroupItemsCommand::Grouped, itemGroup, this, tr( "Items grouped" ) );
connect( c, &QgsGroupUngroupItemsCommand::itemRemoved, this, &QgsComposition::itemRemoved );
connect( c, &QgsGroupUngroupItemsCommand::itemAdded, this, &QgsComposition::sendItemAddedSignal );
undoStack()->push( c );
mProject->setDirty( true );
//QgsDebugMsg( QString( "itemgroup after pushAddRemove has %1" ) .arg( itemGroup->items().size() ) );
emit composerItemGroupAdded( itemGroup );
#endif
mUndoStack->endMacro();
return returnGroup;
}
@ -481,25 +485,21 @@ QList<QgsLayoutItem *> QgsLayout::ungroupItems( QgsLayoutItemGroup *group )
return ungroupedItems;
}
#if 0 //TODO
// group ownership transferred to QgsGroupUngroupItemsCommand
mUndoStack->beginMacro( tr( "Ungrouped items" ) );
// Call this before removing group items so it can keep note
// of contents
QgsGroupUngroupItemsCommand *c = new QgsGroupUngroupItemsCommand( QgsGroupUngroupItemsCommand::Ungrouped, group, this, tr( "Items ungrouped" ) );
connect( c, &QgsGroupUngroupItemsCommand::itemRemoved, this, &QgsComposition::itemRemoved );
connect( c, &QgsGroupUngroupItemsCommand::itemAdded, this, &QgsComposition::sendItemAddedSignal );
std::unique_ptr< QgsLayoutItemGroupUndoCommand > c( new QgsLayoutItemGroupUndoCommand( QgsLayoutItemGroupUndoCommand::Ungrouped, group, this, tr( "Items ungrouped" ) ) );
mUndoStack->stack()->push( c.release() );
undoStack()->push( c );
mProject->setDirty( true );
#endif
ungroupedItems = group->items();
group->removeItems();
removeLayoutItem( group );
mUndoStack->endMacro();
#if 0 //TODO
// note: emits itemRemoved
removeComposerItem( group, false, false );
#endif
@ -554,7 +554,7 @@ void QgsLayout::removeLayoutItemPrivate( QgsLayoutItem *item )
#if 0 //TODO
emit itemRemoved( item );
#endif
item->deleteLater();
delete item;
}
void QgsLayout::updateZValues( const bool addUndoCommands )

View File

@ -523,6 +523,7 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
friend class QgsLayoutItemDeleteUndoCommand;
friend class QgsLayoutItemUndoCommand;
friend class QgsLayoutUndoCommand;
friend class QgsLayoutItemGroupUndoCommand;
friend class QgsLayoutModel;
};

View File

@ -139,13 +139,20 @@ void QgsLayoutItem::setVisibility( const bool visible )
return;
}
std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->beginCommand( this, visible ? tr( "Item shown" ) : tr( "Item hidden" ) );
{
command.reset( createCommand( visible ? tr( "Item shown" ) : tr( "Item hidden" ), 0 ) );
command->saveBeforeState();
}
QGraphicsItem::setVisible( visible );
if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->endCommand();
if ( command )
{
command->saveAfterState();
mLayout->undoStack()->stack()->push( command.release() );
}
//inform model that visibility has changed
if ( mLayout )
@ -729,6 +736,7 @@ bool QgsLayoutItem::writePropertiesToElement( QDomElement &element, QDomDocument
element.setAttribute( QStringLiteral( "position" ), mItemPosition.encodePoint() );
element.setAttribute( QStringLiteral( "size" ), mItemSize.encodeSize() );
element.setAttribute( QStringLiteral( "rotation" ), QString::number( rotation() ) );
element.setAttribute( QStringLiteral( "groupUuid" ), mParentGroupUuid );
element.setAttribute( "zValue", QString::number( zValue() ) );
element.setAttribute( "visibility", isVisible() );
@ -807,6 +815,15 @@ bool QgsLayoutItem::readPropertiesFromElement( const QDomElement &element, const
attemptResize( QgsLayoutSize::decodeSize( element.attribute( QStringLiteral( "size" ) ) ) );
setItemRotation( element.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble() );
mParentGroupUuid = element.attribute( QStringLiteral( "groupUuid" ) );
if ( !mParentGroupUuid.isEmpty() )
{
if ( QgsLayoutItemGroup *group = parentGroup() )
{
group->addItem( this );
}
}
//TODO
/*
// temporary for groups imported from templates

View File

@ -25,8 +25,6 @@ QgsLayoutItemGroup::QgsLayoutItemGroup( QgsLayout *layout )
QgsLayoutItemGroup::~QgsLayoutItemGroup()
{
if ( mLayout )
mLayout->undoStack()->beginMacro( tr( "Removed group" ) );
//loop through group members and remove them from the scene
for ( QgsLayoutItem *item : qgsAsConst( mItems ) )
{
@ -37,10 +35,8 @@ QgsLayoutItemGroup::~QgsLayoutItemGroup()
if ( mLayout )
mLayout->removeLayoutItem( item );
else
item->deleteLater();
delete item;
}
if ( mLayout )
mLayout->undoStack()->endMacro();
}
int QgsLayoutItemGroup::type() const
@ -63,6 +59,11 @@ QString QgsLayoutItemGroup::displayName() const
return tr( "<Group>" );
}
QgsLayoutItemGroup *QgsLayoutItemGroup::create( QgsLayout *layout, const QVariantMap & )
{
return new QgsLayoutItemGroup( layout );
}
void QgsLayoutItemGroup::addItem( QgsLayoutItem *item )
{
if ( !item )
@ -107,7 +108,7 @@ QList<QgsLayoutItem *> QgsLayoutItemGroup::items() const
void QgsLayoutItemGroup::setVisibility( const bool visible )
{
if ( mLayout )
if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->beginMacro( tr( "Set group visibility" ) );
//also set visibility for all items within the group
for ( QgsLayoutItem *item : qgsAsConst( mItems ) )
@ -118,7 +119,7 @@ void QgsLayoutItemGroup::setVisibility( const bool visible )
}
//lastly set visibility for group item itself
QgsLayoutItem::setVisibility( visible );
if ( mLayout )
if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->endMacro();
}
@ -127,7 +128,8 @@ void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point )
if ( !mLayout )
return;
mLayout->undoStack()->beginMacro( tr( "Moved group" ) );
if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->beginMacro( tr( "Moved group" ) );
QPointF scenePoint = mLayout->convertToLayoutUnits( point );
double deltaX = scenePoint.x() - pos().x();
@ -139,7 +141,12 @@ void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point )
if ( !item )
continue;
mLayout->undoStack()->beginCommand( item, QString() );
std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
if ( !shouldBlockUndoCommands() )
{
command.reset( createCommand( QString(), 0 ) );
command->saveBeforeState();
}
// need to convert delta from layout units -> item units
QgsLayoutPoint itemPos = item->positionWithUnits();
@ -148,11 +155,16 @@ void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point )
itemPos.setY( itemPos.y() + deltaPos.y() );
item->attemptMove( itemPos );
mLayout->undoStack()->endCommand();
if ( command )
{
command->saveAfterState();
mLayout->undoStack()->stack()->push( command.release() );
}
}
//lastly move group item itself
QgsLayoutItem::attemptMove( point );
mLayout->undoStack()->endMacro();
if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->endMacro();
resetBoundingRect();
}
@ -161,7 +173,8 @@ void QgsLayoutItemGroup::attemptResize( const QgsLayoutSize &size )
if ( !mLayout )
return;
mLayout->undoStack()->beginMacro( tr( "Resized group" ) );
if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->beginMacro( tr( "Resized group" ) );
QRectF oldRect = rect();
QSizeF newSizeLayoutUnits = mLayout->convertToLayoutUnits( size );
@ -174,6 +187,13 @@ void QgsLayoutItemGroup::attemptResize( const QgsLayoutSize &size )
if ( !item )
continue;
std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
if ( !shouldBlockUndoCommands() )
{
command.reset( createCommand( QString(), 0 ) );
command->saveBeforeState();
}
QRectF itemRect = mapRectFromItem( item, item->rect() );
QgsLayoutUtils::relativeResizeRect( itemRect, oldRect, newRect );
@ -186,13 +206,78 @@ void QgsLayoutItemGroup::attemptResize( const QgsLayoutSize &size )
QgsLayoutSize itemSize = mLayout->convertFromLayoutUnits( itemRect.size(), item->sizeWithUnits().units() );
item->attemptResize( itemSize );
if ( command )
{
command->saveAfterState();
mLayout->undoStack()->stack()->push( command.release() );
}
}
QgsLayoutItem::attemptResize( size );
mLayout->undoStack()->endMacro();
if ( !shouldBlockUndoCommands() )
mLayout->undoStack()->endMacro();
resetBoundingRect();
}
bool QgsLayoutItemGroup::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const
{
QDomElement element = document.createElement( QStringLiteral( "LayoutItem" ) );
element.setAttribute( QStringLiteral( "type" ), stringType() );
writePropertiesToElement( element, document, context );
for ( QgsLayoutItem *item : mItems )
{
if ( !item )
continue;
QDomElement childItem = document.createElement( QStringLiteral( "ComposerItemGroupElement" ) );
childItem.setAttribute( QStringLiteral( "uuid" ), item->uuid() );
element.appendChild( childItem );
}
parentElement.appendChild( element );
return true;
}
bool QgsLayoutItemGroup::readXml( const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context )
{
if ( itemElement.nodeName() != QStringLiteral( "LayoutItem" ) || itemElement.attribute( QStringLiteral( "type" ) ) != stringType() )
{
return false;
}
bool result = readPropertiesFromElement( itemElement, document, context );
QList<QgsLayoutItem *> items;
mLayout->layoutItems( items );
QDomNodeList elementNodes = itemElement.elementsByTagName( QStringLiteral( "ComposerItemGroupElement" ) );
for ( int i = 0; i < elementNodes.count(); ++i )
{
QDomNode elementNode = elementNodes.at( i );
if ( !elementNode.isElement() )
continue;
QString uuid = elementNode.toElement().attribute( QStringLiteral( "uuid" ) );
for ( QgsLayoutItem *item : qgsAsConst( items ) )
{
if ( item && ( item->mUuid == uuid /* TODO || item->mTemplateUuid == uuid */ ) )
{
addItem( item );
break;
}
}
}
resetBoundingRect();
return result;
}
void QgsLayoutItemGroup::paint( QPainter *, const QStyleOptionGraphicsItem *, QWidget * )
{
}

View File

@ -38,6 +38,13 @@ class CORE_EXPORT QgsLayoutItemGroup: public QgsLayoutItem
QString stringType() const override;
QString displayName() const override;
/**
* Returns a new group item for the specified \a layout.
*
* The caller takes responsibility for deleting the returned object.
*/
static QgsLayoutItemGroup *create( QgsLayout *layout, const QVariantMap &settings ) SIP_FACTORY;
/**
* Adds an \a item to the group. Ownership of the item
* is transferred to the group.
@ -62,6 +69,9 @@ class CORE_EXPORT QgsLayoutItemGroup: public QgsLayoutItem
void attemptMove( const QgsLayoutPoint &point ) override;
void attemptResize( const QgsLayoutSize &size ) override;
bool writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const override;
bool readXml( const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context ) override;
void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
protected:

View File

@ -0,0 +1,86 @@
/***************************************************************************
qgslayoutitemgroupundocommand.cpp
---------------------------
begin : 2016-06-09
copyright : (C) 2016 by Sandro Santilli
email : strk at kbt dot io
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslayoutitemgroupundocommand.h"
#include "qgslayoutitemgroup.h"
#include "qgslayout.h"
#include "qgsproject.h"
QgsLayoutItemGroupUndoCommand::QgsLayoutItemGroupUndoCommand( State s, QgsLayoutItemGroup *group, QgsLayout *layout, const QString &text, QUndoCommand *parent )
: QUndoCommand( text, parent )
, mGroupUuid( group->uuid() )
, mLayout( layout )
, mState( s )
, mFirstRun( true )
{
const QList< QgsLayoutItem * > items = group->items();
for ( QgsLayoutItem *i : items )
{
mItemUuids.insert( i->uuid() );
}
}
void QgsLayoutItemGroupUndoCommand::redo()
{
if ( mFirstRun )
{
mFirstRun = false;
return;
}
switchState();
}
void QgsLayoutItemGroupUndoCommand::undo()
{
if ( mFirstRun )
{
mFirstRun = false;
return;
}
switchState();
}
void QgsLayoutItemGroupUndoCommand::switchState()
{
if ( mState == Grouped )
{
// ungroup
QgsLayoutItemGroup *group = dynamic_cast< QgsLayoutItemGroup * >( mLayout->itemByUuid( mGroupUuid ) );
group->removeItems();
mLayout->removeLayoutItemPrivate( group );
mState = Ungrouped;
}
else //Ungrouped
{
// find group by uuid...
QgsLayoutItemGroup *group = dynamic_cast< QgsLayoutItemGroup * >( mLayout->itemByUuid( mGroupUuid ) );
if ( !group )
{
group = new QgsLayoutItemGroup( mLayout );
mLayout->addLayoutItemPrivate( group );
}
for ( const QString &childUuid : mItemUuids )
{
QgsLayoutItem *childItem = mLayout->itemByUuid( childUuid );
group->addItem( childItem );
}
mState = Grouped;
}
mLayout->project()->setDirty( true );
}

View File

@ -0,0 +1,71 @@
/***************************************************************************
qgslayoutitemgroupundocommand.h
-------------------------------
begin : 2016-06-09
copyright : (C) 2016 by Sandro Santilli
email : strk at kbt dot io
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSLAYOUTITEMGROUPUNDOCOMMAND_H
#define QGSLAYOUTITEMGROUPUNDOCOMMAND_H
#include "qgis_core.h"
#include <QUndoCommand>
#include "qgslayoutitem.h"
#define SIP_NO_FILE
/**
* \ingroup core
* A layout undo command class for grouping / ungrouping layout items.
*/
class CORE_EXPORT QgsLayoutItemGroupUndoCommand: public QObject, public QUndoCommand
{
Q_OBJECT
public:
//! Command kind, and state
enum State
{
Grouped = 0,
Ungrouped
};
/**
* Create a group or ungroup command
*
* \param s command kind (\see State)
* \param item the group item being created or ungrouped
* \param c the composition including this group
* \param text command label
* \param parent parent command, if any
*
*/
QgsLayoutItemGroupUndoCommand( State s, QgsLayoutItemGroup *group, QgsLayout *layout,
const QString &text, QUndoCommand *parent = nullptr );
void redo() override;
void undo() override;
private:
QString mGroupUuid;
QSet<QString> mItemUuids;
QgsLayout *mLayout = nullptr;
State mState;
bool mFirstRun = true; //flag to prevent execution when the command is pushed to the QUndoStack
//changes between added / removed state
void switchState();
};
#endif // QGSLAYOUTITEMGROUPUNDOCOMMAND_H

View File

@ -17,6 +17,7 @@
#include "qgslayoutitemregistry.h"
#include "qgslayoutitemshape.h"
#include "qgslayoutitempage.h"
#include "qgslayoutitemgroup.h"
#include "qgsgloweffect.h"
#include "qgseffectstack.h"
#include <QPainter>
@ -43,12 +44,15 @@ bool QgsLayoutItemRegistry::populate()
};
addLayoutItemType( new QgsLayoutItemMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, QStringLiteral( "temp type" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), createTemporaryItem ) );
addLayoutItemType( new QgsLayoutItemMetadata( LayoutGroup, QStringLiteral( "Group" ), QIcon(), QgsLayoutItemGroup::create ) );
addLayoutItemType( new QgsLayoutItemMetadata( LayoutPage, QStringLiteral( "Page" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileNew.svg" ) ), QgsLayoutItemPage::create ) );
addLayoutItemType( new QgsLayoutItemMetadata( LayoutRectangle, QStringLiteral( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), QgsLayoutItemRectangularShape::create ) );
addLayoutItemType( new QgsLayoutItemMetadata( LayoutEllipse, QStringLiteral( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), QgsLayoutItemEllipseShape::create ) );
addLayoutItemType( new QgsLayoutItemMetadata( LayoutTriangle, QStringLiteral( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), QgsLayoutItemTriangleShape::create ) );
return true;
}

View File

@ -627,7 +627,8 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
continue;
}
mLayout->undoStack()->beginCommand( item, QString() );
std::unique_ptr< QgsAbstractLayoutUndoCommand > command( item->createCommand( QString(), 0 ) );
command->saveBeforeState();
// need to convert delta from layout units -> item units
QgsLayoutPoint itemPos = item->positionWithUnits();
@ -636,7 +637,8 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
itemPos.setY( itemPos.y() + deltaPos.y() );
item->attemptMove( itemPos );
mLayout->undoStack()->endCommand();
command->saveAfterState();
mLayout->undoStack()->stack()->push( command.release() );
}
mLayout->undoStack()->endMacro();
}
@ -655,7 +657,8 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
continue;
}
mLayout->undoStack()->beginCommand( item, QString() );
std::unique_ptr< QgsAbstractLayoutUndoCommand > command( item->createCommand( QString(), 0 ) );
command->saveBeforeState();
QRectF itemRect;
if ( selectedItems.size() == 1 )
@ -680,7 +683,8 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
QgsLayoutSize itemSize = mLayout->convertFromLayoutUnits( itemRect.size(), item->sizeWithUnits().units() );
item->attemptResize( itemSize );
mLayout->undoStack()->endCommand();
command->saveAfterState();
mLayout->undoStack()->stack()->push( command.release() );
}
mLayout->undoStack()->endMacro();
}

View File

@ -388,103 +388,111 @@ void TestQgsLayoutItemGroup::resizeGroup()
}
Q_DECLARE_METATYPE( QgsLayoutItemGroup * )
Q_DECLARE_METATYPE( QgsComposerPolygon * )
Q_DECLARE_METATYPE( QgsLayoutItemRectangularShape * )
Q_DECLARE_METATYPE( QgsLayoutItem * )
void TestQgsLayoutItemGroup::undoRedo()
{
#if 0
QgsComposerPolygon *item1, *item2;
int polygonsAdded = 0;
int groupsAdded = 0;
int itemsRemoved = 0;
QgsProject proj;
QgsLayout l( &proj );
qRegisterMetaType<QgsComposerPolygon *>();
QSignalSpy spyPolygonAdded( mComposition, &QgsComposition::itemAdded );
QCOMPARE( spyPolygonAdded.count(), 0 );
QgsLayoutItemRectangularShape *item1 = nullptr;
QgsLayoutItemRectangularShape *item2 = nullptr;
qRegisterMetaType<QgsComposerItemGroup *>();
QSignalSpy spyGroupAdded( mComposition, &QgsComposition::composerItemGroupAdded );
QCOMPARE( spyGroupAdded.count(), 0 );
// int shapesAdded = 0;
// int groupsAdded = 0;
// int itemsRemoved = 0;
qRegisterMetaType<QgsComposerItem *>();
QSignalSpy spyItemRemoved( mComposition, &QgsComposition::itemRemoved );
QCOMPARE( spyItemRemoved.count(), 0 );
qRegisterMetaType<QgsLayoutItemRectangularShape *>();
// QSignalSpy spyPolygonAdded( &l, &QgsLayout::itemAdded );
// QCOMPARE( spyPolygonAdded.count(), 0 );
qRegisterMetaType<QgsLayoutItemGroup *>();
// QSignalSpy spyGroupAdded( &l, &QgsLayout::composerItemGroupAdded );
// QCOMPARE( spyGroupAdded.count(), 0 );
qRegisterMetaType<QgsLayoutItem *>();
// QSignalSpy spyItemRemoved( &l, &QgsLayout::itemRemoved );
// QCOMPARE( spyItemRemoved.count(), 0 );
//test for crash when undo/redoing with groups
// Set initial condition
QUndoStack *us = mComposition->undoStack();
QUndoStack *us = l.undoStack()->stack();
QgsDebugMsg( QString( "clearing" ) );
us->clear();
QgsDebugMsg( QString( "clearing completed" ) );
QList<QgsComposerItem *> items;
mComposition->composerItems( items );
QCOMPARE( items.size(), 1 ); // paper only
QList<QgsLayoutItem *> items;
l.layoutItems( items );
QCOMPARE( items.size(), 0 );
QgsDebugMsg( QString( "clear stack count:%1 index:%2" ) .arg( us->count() ) .arg( us->index() ) );
//create some items
item1 = new QgsComposerPolygon( QPolygonF( QRectF( 0, 0, 1, 1 ) ), mComposition ); //QgsComposerLabel( mComposition );
mComposition->addComposerPolygon( item1 );
QCOMPARE( spyPolygonAdded.count(), ++polygonsAdded );
item2 = new QgsComposerPolygon( QPolygonF( QRectF( -1, -2, 1, 1 ) ), mComposition ); //QgsComposerLabel( mComposition );
mComposition->addComposerPolygon( item2 );
QCOMPARE( spyPolygonAdded.count(), ++polygonsAdded );
mComposition->composerItems( items );
QCOMPARE( items.size(), 3 ); // paper, 2 shapes
item1 = new QgsLayoutItemRectangularShape( &l );
item1->attemptMove( QgsLayoutPoint( 0.05, 0.09, QgsUnitTypes::LayoutMeters ) );
QPointer< QgsLayoutItem > pItem1( item1 );
QString item1Uuid = item1->uuid();
item1->attemptResize( QgsLayoutSize( 0.1, 0.15, QgsUnitTypes::LayoutMeters ) );
l.addLayoutItem( item1 );
// QCOMPARE( spyPolygonAdded.count(), ++shapesAdded );
item2 = new QgsLayoutItemRectangularShape( &l );
QPointer< QgsLayoutItem > pItem2( item2 );
QString item2Uuid = item2->uuid();
item2->attemptMove( QgsLayoutPoint( 2, 3, QgsUnitTypes::LayoutMillimeters ) );
item2->attemptResize( QgsLayoutSize( 4, 6, QgsUnitTypes::LayoutMillimeters ) );
l.addLayoutItem( item2 );
// QCOMPARE( spyPolygonAdded.count(), ++shapesAdded );
l.layoutItems( items );
QCOMPARE( items.size(), 2 ); // 2 shapes
QgsDebugMsg( QString( "addedItems stack count:%1 index:%2" ) .arg( us->count() ) .arg( us->index() ) );
QCOMPARE( item1->pos(), QPointF( 0, 0 ) );
QCOMPARE( item2->pos(), QPointF( -1, -2 ) );
QCOMPARE( item1->pos(), QPointF( 50, 90 ) );
QCOMPARE( item2->pos(), QPointF( 2, 3 ) );
//dumpUndoStack(*us, "after initial items addition");
//group items
items.clear();
items << item1 << item2;
mGroup = mComposition->groupItems( items );
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), ++groupsAdded );
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
QCOMPARE( mGroup->items().size(), 2 );
mComposition->composerItems( items );
QCOMPARE( items.size(), 4 ); // paper, 2 shapes, 1 group
QVERIFY( ! item1->isRemoved() );
QCOMPARE( item1->pos(), QPointF( 0, 0 ) );
QVERIFY( ! item2->isRemoved() );
QCOMPARE( item2->pos(), QPointF( -1, -2 ) );
QVERIFY( ! mGroup->isRemoved() );
QCOMPARE( mGroup->pos(), QPointF( -1, -2 ) );
QgsLayoutItemGroup *group = l.groupItems( items );
QString groupUuid = group->uuid();
// QCOMPARE( spyPolygonAdded.count(), shapesAdded );
// QCOMPARE( spyGroupAdded.count(), ++groupsAdded );
// QCOMPARE( spyItemRemoved.count(), itemsRemoved );
QCOMPARE( group->items().size(), 2 );
l.layoutItems( items );
QCOMPARE( items.size(), 3 ); // 2 shapes, 1 group
QCOMPARE( item1->pos(), QPointF( 50, 90 ) );
QCOMPARE( item2->pos(), QPointF( 2, 3 ) );
QCOMPARE( group->pos(), QPointF( 2, 3 ) );
//dumpUndoStack(*us, "after initial items addition");
//move group
QgsDebugMsg( QString( "moving group" ) );
mGroup->beginCommand( QStringLiteral( "move group" ) );
mGroup->move( 10.0, 20.0 );
mGroup->endCommand();
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), groupsAdded );
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
group->attemptMove( QgsLayoutPoint( 10.0, 20.0 ) );
// QCOMPARE( spyPolygonAdded.count(), shapesAdded );
// QCOMPARE( spyGroupAdded.count(), groupsAdded );
// QCOMPARE( spyItemRemoved.count(), itemsRemoved );
QgsDebugMsg( QString( "groupItems stack count:%1 index:%2" ) .arg( us->count() ) .arg( us->index() ) );
QCOMPARE( mGroup->items().size(), 2 );
mComposition->composerItems( items );
QCOMPARE( items.size(), 4 ); // paper, 2 shapes, 1 group
QVERIFY( ! item1->isRemoved() );
QCOMPARE( item1->pos(), QPointF( 10, 20 ) );
QVERIFY( ! item2->isRemoved() );
QCOMPARE( item2->pos(), QPointF( 9, 18 ) );
QVERIFY( ! mGroup->isRemoved() );
QCOMPARE( mGroup->pos(), QPointF( 9, 18 ) );
QCOMPARE( group->items().size(), 2 );
l.layoutItems( items );
QCOMPARE( items.size(), 3 ); // 2 shapes, 1 group
QCOMPARE( item1->pos(), QPointF( 58, 107 ) );
QCOMPARE( item2->pos(), QPointF( 10, 20 ) );
QCOMPARE( group->pos(), QPointF( 10, 20 ) );
//ungroup
QPointer< QgsLayoutItemGroup > pGroup( group ); // for testing deletion
QgsDebugMsg( QString( "ungrouping" ) );
mComposition->ungroupItems( mGroup );
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), groupsAdded );
QCOMPARE( spyItemRemoved.count(), ++itemsRemoved );
mComposition->composerItems( items );
QCOMPARE( items.size(), 3 ); // paper, 2 shapes
QVERIFY( ! item1->isRemoved() );
QVERIFY( ! item2->isRemoved() );
QVERIFY( mGroup->isRemoved() );
QCOMPARE( mGroup->pos(), QPointF( 9, 18 ) ); // should not rely on this
l.ungroupItems( group );
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
// QCOMPARE( spyPolygonAdded.count(), shapesAdded );
// QCOMPARE( spyGroupAdded.count(), groupsAdded );
// QCOMPARE( spyItemRemoved.count(), ++itemsRemoved );
l.layoutItems( items );
QCOMPARE( items.size(), 2 ); // 2 shapes
QCOMPARE( item1->pos(), QPointF( 58, 107 ) );
QCOMPARE( item2->pos(), QPointF( 10, 20 ) );
QVERIFY( !pGroup );
//dumpUndoStack(*us, "after ungroup");
// US 0: Items grouped
// US 1: move group
@ -493,18 +501,23 @@ void TestQgsLayoutItemGroup::undoRedo()
//undo (groups again) -- crashed here before #11371 got fixed
QgsDebugMsg( QString( "undo ungrouping" ) );
us->undo();
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), ++groupsAdded );
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
QCOMPARE( mGroup->items().size(), 2 ); // WARNING: might not be alive anymore
mComposition->composerItems( items );
QCOMPARE( items.size(), 4 ); // paper, 2 shapes, 1 group
QVERIFY( ! item1->isRemoved() );
QCOMPARE( item1->pos(), QPointF( 10, 20 ) );
QVERIFY( ! item2->isRemoved() );
QCOMPARE( item2->pos(), QPointF( 9, 18 ) );
QVERIFY( ! mGroup->isRemoved() );
QCOMPARE( mGroup->pos(), QPointF( 9, 18 ) );
// QCOMPARE( spyPolygonAdded.count(), shapesAdded );
// QCOMPARE( spyGroupAdded.count(), ++groupsAdded );
// QCOMPARE( spyItemRemoved.count(), itemsRemoved );
QVERIFY( item1->isGroupMember() );
QVERIFY( item2->isGroupMember() );
QCOMPARE( item1->parentGroup(), item2->parentGroup() );
QCOMPARE( item1->parentGroup()->uuid(), groupUuid );
group = item1->parentGroup();
pGroup = group;
QCOMPARE( group->items().size(), 2 ); // WARNING: might not be alive anymore
l.layoutItems( items );
QCOMPARE( items.size(), 3 ); // 2 shapes, 1 group
QCOMPARE( item1->pos(), QPointF( 58, 107 ) );
QCOMPARE( item2->pos(), QPointF( 10, 20 ) );
QCOMPARE( group->pos(), QPointF( 10, 20 ) );
//dumpUndoStack(*us, "after undo ungroup");
// US 0: Items grouped
// US 1: move group
@ -512,13 +525,19 @@ void TestQgsLayoutItemGroup::undoRedo()
//remove group
QgsDebugMsg( QString( "remove group" ) );
mComposition->removeComposerItem( mGroup, true, true );
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), groupsAdded );
itemsRemoved += 3; // the group and the two items
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
mComposition->composerItems( items );
QCOMPARE( items.size(), 1 ); // paper only
l.removeLayoutItem( group );
// QCOMPARE( spyPolygonAdded.count(), shapesAdded );
//QCOMPARE( spyGroupAdded.count(), groupsAdded );
//itemsRemoved += 3; // the group and the two items
//QCOMPARE( spyItemRemoved.count(), itemsRemoved );
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
QVERIFY( !pGroup );
QVERIFY( !pItem1 );
QVERIFY( !pItem2 );
l.layoutItems( items );
QCOMPARE( items.size(), 0 ); // nothing
QgsDebugMsg( QString( "remove stack count:%1 index:%2" ) .arg( us->count() ) .arg( us->index() ) );
//dumpUndoStack(*us, "after remove group");
// US 0: Items grouped
@ -528,13 +547,20 @@ void TestQgsLayoutItemGroup::undoRedo()
//undo remove group
QgsDebugMsg( QString( "undo remove group" ) );
us->undo();
polygonsAdded += 2;
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), ++groupsAdded );
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
mComposition->composerItems( items );
QCOMPARE( mGroup->items().size(), 2 );
QCOMPARE( items.size(), 4 ); // paper, 2 shapes, 1 group
//shapesAdded += 2;
//QCOMPARE( spyPolygonAdded.count(), shapesAdded );
//QCOMPARE( spyGroupAdded.count(), ++groupsAdded );
//QCOMPARE( spyItemRemoved.count(), itemsRemoved );
l.layoutItems( items );
group = dynamic_cast< QgsLayoutItemGroup * >( l.itemByUuid( groupUuid ) );
QVERIFY( group );
QCOMPARE( group->items().size(), 2 );
QCOMPARE( items.size(), 3 ); // 2 shapes, 1 group
item1 = dynamic_cast< QgsLayoutItemRectangularShape * >( l.itemByUuid( item1Uuid ) );
QCOMPARE( item1->parentGroup(), group );
item2 = dynamic_cast< QgsLayoutItemRectangularShape * >( l.itemByUuid( item2Uuid ) );
QCOMPARE( item2->parentGroup(), group );
QgsDebugMsg( QString( "undo stack count:%1 index:%2" ) .arg( us->count() ) .arg( us->index() ) );
//dumpUndoStack(*us, "after undo remove group");
// US 0: Items grouped
@ -544,42 +570,39 @@ void TestQgsLayoutItemGroup::undoRedo()
//undo move group
QgsDebugMsg( QString( "undo move group" ) );
us->undo();
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), groupsAdded );
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
QCOMPARE( mGroup->items().size(), 2 );
mComposition->composerItems( items );
QCOMPARE( items.size(), 4 ); // paper, 2 shapes, 1 group
// QCOMPARE( spyPolygonAdded.count(), shapesAdded );
// QCOMPARE( spyGroupAdded.count(), groupsAdded );
// QCOMPARE( spyItemRemoved.count(), itemsRemoved );
QCOMPARE( group->items().size(), 2 );
l.layoutItems( items );
QCOMPARE( items.size(), 3 ); // 2 shapes, 1 group
QCOMPARE( item1->isGroupMember(), true );
QCOMPARE( item2->isGroupMember(), true );
QVERIFY( ! item1->isRemoved() );
QCOMPARE( item1->pos(), QPointF( 0, 0 ) );
QVERIFY( ! item2->isRemoved() );
QCOMPARE( item2->pos(), QPointF( -1, -2 ) );
QVERIFY( ! mGroup->isRemoved() );
QCOMPARE( mGroup->pos(), QPointF( -1, -2 ) );
QCOMPARE( item1->pos(), QPointF( 50, 90 ) );
QCOMPARE( item2->pos(), QPointF( 2, 3 ) );
QCOMPARE( group->pos(), QPointF( 2, 3 ) );
//dumpUndoStack(*us, "after undo move group");
// US 0: Items grouped
// US 1: -move group
// US 2: -Remove item group
//undo group
pGroup = group;
QgsDebugMsg( QString( "undo group" ) );
us->undo();
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), groupsAdded );
QCOMPARE( spyItemRemoved.count(), ++itemsRemoved );
//QCOMPARE( spyPolygonAdded.count(), shapesAdded );
//QCOMPARE( spyGroupAdded.count(), groupsAdded );
//QCOMPARE( spyItemRemoved.count(), ++itemsRemoved );
//QCOMPARE( mGroup->items().size(), 2 ); // not important
mComposition->composerItems( items );
QCOMPARE( items.size(), 3 ); // paper, 2 shapes
l.layoutItems( items );
QCOMPARE( items.size(), 2 ); // 2 shapes
QCOMPARE( item1->isGroupMember(), false );
QCOMPARE( item2->isGroupMember(), false );
QVERIFY( ! item1->isRemoved() );
QCOMPARE( item1->pos(), QPointF( 0, 0 ) );
QVERIFY( ! item2->isRemoved() );
QCOMPARE( item2->pos(), QPointF( -1, -2 ) );
QVERIFY( mGroup->isRemoved() );
//QCOMPARE( mGroup->pos(), QPointF( -1, -2 ) );
QCOMPARE( item1->pos(), QPointF( 50, 90 ) );
QCOMPARE( item2->pos(), QPointF( 2, 3 ) );
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
QVERIFY( !pGroup );
//dumpUndoStack(*us, "after undo group");
// US 0: -Items grouped
// US 1: -move group
@ -588,14 +611,15 @@ void TestQgsLayoutItemGroup::undoRedo()
//redo group
QgsDebugMsg( QString( "redo group" ) );
us->redo();
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), ++groupsAdded );
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
mComposition->composerItems( items );
QCOMPARE( items.size(), 4 ); // paper, 2 shapes, 1 group
//QCOMPARE( spyPolygonAdded.count(), shapesAdded );
//QCOMPARE( spyGroupAdded.count(), ++groupsAdded );
//QCOMPARE( spyItemRemoved.count(), itemsRemoved );
l.layoutItems( items );
QCOMPARE( items.size(), 3 ); // 2 shapes, 1 group
QCOMPARE( item1->isGroupMember(), true );
QCOMPARE( item2->isGroupMember(), true );
//// QCOMPARE( mGroup->pos(), QPointF( 0, 0 ) ); // getting nan,nan here
group = dynamic_cast< QgsLayoutItemGroup * >( l.itemByUuid( groupUuid ) );
QCOMPARE( group->pos(), QPointF( 2, 3 ) );
//dumpUndoStack(*us, "after redo group");
// US 0: Items grouped
// US 1: -move group
@ -604,14 +628,19 @@ void TestQgsLayoutItemGroup::undoRedo()
//redo move group
QgsDebugMsg( QString( "redo move group" ) );
us->redo();
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), groupsAdded );
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
mComposition->composerItems( items );
QCOMPARE( items.size(), 4 ); // paper, 2 shapes, 1 group
//QCOMPARE( spyPolygonAdded.count(), shapesAdded );
//QCOMPARE( spyGroupAdded.count(), groupsAdded );
//QCOMPARE( spyItemRemoved.count(), itemsRemoved );
l.layoutItems( items );
QCOMPARE( items.size(), 3 ); // 2 shapes, 1 group
QCOMPARE( item1->isGroupMember(), true );
QCOMPARE( item2->isGroupMember(), true );
QCOMPARE( mGroup->pos(), QPointF( 9, 18 ) );
//TODO - fix!!!
QCOMPARE( item1->pos(), QPointF( 50, 90 ) );
QCOMPARE( item2->pos(), QPointF( 2, 3 ) );
QCOMPARE( group->pos(), QPointF( 2, 3 ) );
//dumpUndoStack(*us, "after redo move group");
// US 0: Items grouped
// US 1: move group
@ -619,13 +648,21 @@ void TestQgsLayoutItemGroup::undoRedo()
//redo remove group
QgsDebugMsg( QString( "redo remove group" ) );
pGroup = group;
pItem1 = item1;
pItem2 = item2;
us->redo();
QCOMPARE( spyPolygonAdded.count(), polygonsAdded );
QCOMPARE( spyGroupAdded.count(), groupsAdded );
itemsRemoved += 3; // 1 group, 2 contained items
QCOMPARE( spyItemRemoved.count(), itemsRemoved );
mComposition->composerItems( items );
QCOMPARE( items.size(), 1 ); // paper only
//QCOMPARE( spyPolygonAdded.count(), shapesAdded );
//QCOMPARE( spyGroupAdded.count(), groupsAdded );
//itemsRemoved += 3; // 1 group, 2 contained items
//QCOMPARE( spyItemRemoved.count(), itemsRemoved );
l.layoutItems( items );
QCOMPARE( items.size(), 0 );
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
QVERIFY( !pGroup );
QVERIFY( !pItem1 );
QVERIFY( !pItem2 );
QgsDebugMsg( QString( "undo stack count:%1 index:%2" ) .arg( us->count() ) .arg( us->index() ) );
//dumpUndoStack(*us, "after redo remove group");
// US 0: Items grouped
@ -636,7 +673,6 @@ void TestQgsLayoutItemGroup::undoRedo()
us->clear();
QgsDebugMsg( QString( "clear stack count:%1 index:%2" ) .arg( us->count() ) .arg( us->index() ) );
#endif
}
QGSTEST_MAIN( TestQgsLayoutItemGroup )