Skip to content

Commit

Permalink
Start porting group handling from composer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 9, 2017
1 parent 955742a commit 5aa9a15
Show file tree
Hide file tree
Showing 18 changed files with 999 additions and 2 deletions.
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -403,6 +403,7 @@
%Include layout/qgslayout.sip
%Include layout/qgslayoutguidecollection.sip
%Include layout/qgslayoutitem.sip
%Include layout/qgslayoutitemgroup.sip
%Include layout/qgslayoutitemmap.sip
%Include layout/qgslayoutitempage.sip
%Include layout/qgslayoutitemregistry.sip
Expand Down
19 changes: 19 additions & 0 deletions python/core/layout/qgslayout.sip
Expand Up @@ -397,6 +397,25 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
virtual QgsAbstractLayoutUndoCommand *createCommand( const QString &text, int id = 0, QUndoCommand *parent = 0 ) /Factory/;


QgsLayoutItemGroup *groupItems( const QList<QgsLayoutItem *> &items );
%Docstring
Creates a new group from a list of layout ``items`` and adds the group to the layout.
If grouping was not possible, a None will be returned.
.. seealso:: ungroupItems()
:rtype: QgsLayoutItemGroup
%End

QList<QgsLayoutItem *> ungroupItems( QgsLayoutItemGroup *group );
%Docstring
Ungroups items by removing them from an item ``group`` and removing the group from the
layout. Child items will remain in the layout and will not be deleted.

Returns a list of the items removed from the group, or an empty list if ungrouping
was not successful.

.. seealso:: groupItems()
:rtype: list of QgsLayoutItem
%End

public slots:

Expand Down
13 changes: 13 additions & 0 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -150,6 +150,19 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
:rtype: bool
%End


bool isGroupMember() const;
%Docstring
:rtype: bool
%End

QgsLayoutItemGroup *parentGroup() const;
%Docstring
:rtype: QgsLayoutItemGroup
%End

void setParentGroup( QgsLayoutItemGroup *group );

virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget );

%Docstring
Expand Down
67 changes: 67 additions & 0 deletions python/core/layout/qgslayoutitemgroup.sip
@@ -0,0 +1,67 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/layout/qgslayoutitemgroup.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsLayoutItemGroup: QgsLayoutItem
{
%Docstring
A container for grouping several QgsLayoutItems.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutitemgroup.h"
%End
public:

explicit QgsLayoutItemGroup( QgsLayout *layout );
~QgsLayoutItemGroup();

virtual int type() const;

virtual QString stringType() const;


void addItem( QgsLayoutItem *item /Transfer/ );
%Docstring
Adds an ``item`` to the group. Ownership of the item
is transferred to the group.
%End

void removeItems();
%Docstring
Removes all items from the group (but does not delete them).
Items remain in the scene but are no longer grouped together
%End

QList<QgsLayoutItem *> items() const;
%Docstring
Returns a list of items contained by the group.
:rtype: list of QgsLayoutItem
%End

virtual void setVisibility( const bool visible );

protected:

virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );


};




/************************************************************************
* This file has been generated automatically from *
* *
* src/core/layout/qgslayoutitemgroup.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 2 additions & 0 deletions python/core/layout/qgslayoutitemmap.sip
Expand Up @@ -25,8 +25,10 @@ class QgsLayoutItemMap : QgsLayoutItem
Constructor for QgsLayoutItemMap, with the specified parent ``layout``.
%End
virtual int type() const;

virtual QString stringType() const;


protected:

virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
Expand Down
1 change: 1 addition & 0 deletions python/core/layout/qgslayoutitemregistry.sip
Expand Up @@ -98,6 +98,7 @@ class QgsLayoutItemRegistry : QObject
enum ItemType
{
LayoutItem,
LayoutGroup,

// known
LayoutPage,
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -365,6 +365,7 @@ SET(QGIS_CORE_SRCS
layout/qgslayoutgridsettings.cpp
layout/qgslayoutguidecollection.cpp
layout/qgslayoutitem.cpp
layout/qgslayoutitemgroup.cpp
layout/qgslayoutitemmap.cpp
layout/qgslayoutitempage.cpp
layout/qgslayoutitemregistry.cpp
Expand Down Expand Up @@ -714,6 +715,7 @@ SET(QGIS_CORE_MOC_HDRS
layout/qgslayout.h
layout/qgslayoutguidecollection.h
layout/qgslayoutitem.h
layout/qgslayoutitemgroup.h
layout/qgslayoutitemmap.h
layout/qgslayoutitempage.h
layout/qgslayoutitemregistry.h
Expand Down
67 changes: 67 additions & 0 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsreadwritecontext.h"
#include "qgsproject.h"
#include "qgslayoutitemundocommand.h"
#include "qgslayoutitemgroup.h"

QgsLayout::QgsLayout( QgsProject *project )
: mProject( project )
Expand Down Expand Up @@ -439,6 +440,72 @@ QgsAbstractLayoutUndoCommand *QgsLayout::createCommand( const QString &text, int
return new QgsLayoutUndoCommand( this, text, id, parent );
}

QgsLayoutItemGroup *QgsLayout::groupItems( const QList<QgsLayoutItem *> &items )
{
if ( items.size() < 2 )
{
//not enough items for a group
return nullptr;
}

mUndoStack->beginMacro( tr( "Grouped items" ) );
std::unique_ptr< QgsLayoutItemGroup > itemGroup( new QgsLayoutItemGroup( this ) );
for ( QgsLayoutItem *item : items )
{
itemGroup->addItem( item );
}
QgsLayoutItemGroup *returnGroup = itemGroup.get();
addLayoutItem( itemGroup.release() );
mUndoStack->endMacro();

#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

return returnGroup;
}

QList<QgsLayoutItem *> QgsLayout::ungroupItems( QgsLayoutItemGroup *group )
{
QList<QgsLayoutItem *> ungroupedItems;
if ( !group )
{
return ungroupedItems;
}

#if 0 //TODO
// group ownership transferred to QgsGroupUngroupItemsCommand
// 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 );

undoStack()->push( c );
mProject->setDirty( true );

#endif

ungroupedItems = group->items();
group->removeItems();

removeLayoutItem( group );
#if 0 //TODO
// note: emits itemRemoved
removeComposerItem( group, false, false );
#endif

return ungroupedItems;
}

void QgsLayout::writeXmlLayoutSettings( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
{
mCustomProperties.writeXml( element, document );
Expand Down
17 changes: 17 additions & 0 deletions src/core/layout/qgslayout.h
Expand Up @@ -447,6 +447,23 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext

QgsAbstractLayoutUndoCommand *createCommand( const QString &text, int id = 0, QUndoCommand *parent = nullptr ) SIP_FACTORY override;

/**
* Creates a new group from a list of layout \a items and adds the group to the layout.
* If grouping was not possible, a nullptr will be returned.
* \see ungroupItems()
*/
QgsLayoutItemGroup *groupItems( const QList<QgsLayoutItem *> &items );

/**
* Ungroups items by removing them from an item \a group and removing the group from the
* layout. Child items will remain in the layout and will not be deleted.
*
* Returns a list of the items removed from the group, or an empty list if ungrouping
* was not successful.
*
* \see groupItems()
*/
QList<QgsLayoutItem *> ungroupItems( QgsLayoutItemGroup *group );

public slots:

Expand Down
22 changes: 22 additions & 0 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgslayoutitemundocommand.h"
#include "qgslayoutmodel.h"
#include "qgssymbollayerutils.h"
#include "qgslayoutitemgroup.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QUuid>
Expand Down Expand Up @@ -178,6 +179,27 @@ void QgsLayoutItem::setLocked( const bool locked )
emit lockChanged();
}

bool QgsLayoutItem::isGroupMember() const
{
return !mParentGroupUuid.isEmpty() && mLayout && static_cast< bool >( mLayout->itemByUuid( mParentGroupUuid ) );
}

QgsLayoutItemGroup *QgsLayoutItem::parentGroup() const
{
if ( !mLayout || mParentGroupUuid.isEmpty() )
return nullptr;

return qobject_cast< QgsLayoutItemGroup * >( mLayout->itemByUuid( mParentGroupUuid ) );
}

void QgsLayoutItem::setParentGroup( QgsLayoutItemGroup *group )
{
if ( !group )
mParentGroupUuid.clear();
else
mParentGroupUuid = group->uuid();
}

void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget * )
{
if ( !painter || !painter->device() )
Expand Down
11 changes: 11 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -28,6 +28,7 @@

class QgsLayout;
class QPainter;
class QgsLayoutItemGroup;

/**
* \ingroup core
Expand Down Expand Up @@ -169,6 +170,13 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
*/
bool isLocked() const { return mIsLocked; }


bool isGroupMember() const;

QgsLayoutItemGroup *parentGroup() const;

void setParentGroup( QgsLayoutItemGroup *group );

/**
* Handles preparing a paint surface for the layout item and painting the item's
* content. Derived classes must not override this method, but instead implement
Expand Down Expand Up @@ -585,6 +593,9 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
//! Unique id
QString mUuid;

//! Parent group unique id
QString mParentGroupUuid;

ReferencePoint mReferencePoint = UpperLeft;
QgsLayoutSize mFixedSize;
QgsLayoutSize mMinimumSize;
Expand Down

0 comments on commit 5aa9a15

Please sign in to comment.