Skip to content
Permalink
Browse files
composer: save/restore item groups (fixes #7675)
  • Loading branch information
jef-n committed Jul 20, 2013
1 parent 41a7f10 commit 745fcbfe70922798b1355e934d5bebbb392449fb
@@ -232,6 +232,9 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
//uuid
mUuid = itemElem.attribute( "uuid", QUuid::createUuid().toString() );

// temporary for groups imported from templates
mTemplateUuid = itemElem.attribute( "templateUuid" );

//id
QString id = itemElem.attribute( "id", "" );
setId( id );
@@ -28,6 +28,7 @@ class QWidget;
class QDomDocument;
class QDomElement;
class QGraphicsLineItem;
class QgsComposerItemGroup;

/** \ingroup MapComposer
* A item that forms part of a map composition.
@@ -442,8 +443,12 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
QString mId;
// name (unique)
QString mUuid;
// name (temporary when loaded from template)
QString mTemplateUuid;

void init( bool manageZValue );

friend QgsComposerItemGroup; // to access mTemplateUuid
};

#endif
@@ -17,6 +17,7 @@

#include "qgscomposeritemgroup.h"
#include "qgscomposition.h"

#include <QPen>
#include <QPainter>

@@ -89,7 +90,6 @@ void QgsComposerItemGroup::addItem( QgsComposerItem* item )
}

QgsComposerItem::setSceneRect( mSceneBoundingRectangle ); //call method of superclass to avoid repositioning of items

}

void QgsComposerItemGroup::removeItems()
@@ -168,3 +168,55 @@ void QgsComposerItemGroup::drawFrame( QPainter* p )
p->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
}
}

bool QgsComposerItemGroup::writeXML( QDomElement& elem, QDomDocument & doc ) const
{
QDomElement group = doc.createElement( "ComposerItemGroup" );

QSet<QgsComposerItem*>::const_iterator itemIt = mItems.begin();
for ( ; itemIt != mItems.end(); ++itemIt )
{
QDomElement item = doc.createElement( "ComposerItemGroupElement" );
item.setAttribute( "uuid", ( *itemIt )->uuid() );
group.appendChild( item );
}

elem.appendChild( group );

return _writeXML( group, doc );
}

bool QgsComposerItemGroup::readXML( const QDomElement& itemElem, const QDomDocument& doc )
{
//restore general composer item properties
QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
if ( composerItemList.size() > 0 )
{
QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
_readXML( composerItemElem, doc );
}

QList<QGraphicsItem *> items = mComposition->items();

QDomNodeList elementNodes = itemElem.elementsByTagName( "ComposerItemGroupElement" );
for ( int i = 0; i < elementNodes.count(); ++i )
{
QDomNode elementNode = elementNodes.at( i );
if ( !elementNode.isElement() )
continue;

QString uuid = elementNode.toElement().attribute( "uuid" );

for ( QList<QGraphicsItem *>::iterator it = items.begin(); it != items.end(); ++it )
{
QgsComposerItem *item = dynamic_cast<QgsComposerItem *>( *it );
if ( item && ( item->mUuid == uuid || item->mTemplateUuid == uuid ) )
{
addItem( item );
break;
}
}
}

return true;
}
@@ -46,15 +46,13 @@ class CORE_EXPORT QgsComposerItemGroup: public QgsComposerItem
* @param elem is Dom element corresponding to 'Composer' tag
* @param doc is the Dom document
*/
bool writeXML( QDomElement& elem, QDomDocument & doc ) const
{ Q_UNUSED( elem ); Q_UNUSED( doc ); return true; }
bool writeXML( QDomElement& elem, QDomDocument & doc ) const;

/** sets state from Dom document
* @param itemElem is Dom node corresponding to item tag
* @param doc is the Dom document
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc )
{ Q_UNUSED( itemElem ); Q_UNUSED( doc ); return true; }
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

QSet<QgsComposerItem*> items() { return mItems; }

@@ -68,5 +66,3 @@ class CORE_EXPORT QgsComposerItemGroup: public QgsComposerItem
QSet<QgsComposerItem*> mItems;
QRectF mSceneBoundingRectangle;
};


@@ -14,8 +14,6 @@
* *
***************************************************************************/

#include <stdexcept>

#include "qgscomposition.h"
#include "qgscomposerarrow.h"
#include "qgscomposerframe.h"
@@ -31,14 +29,14 @@
#include "qgscomposerattributetable.h"
#include "qgsaddremovemultiframecommand.h"
#include "qgscomposermultiframecommand.h"
#include "qgslogger.h"
#include "qgspaintenginehack.h"
#include "qgspaperitem.h"
#include "qgsproject.h"
#include "qgsgeometry.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsexpression.h"

#include <QDomDocument>
#include <QDomElement>
#include <QGraphicsRectItem>
@@ -49,24 +47,24 @@


QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
: QGraphicsScene( 0 ),
mMapRenderer( mapRenderer ),
mPlotStyle( QgsComposition::Preview ),
mPageWidth( 297 ),
mPageHeight( 210 ),
mSpaceBetweenPages( 10 ),
mPrintAsRaster( false ),
mUseAdvancedEffects( true ),
mSelectionTolerance( 0.0 ),
mSnapToGrid( false ),
mSnapGridResolution( 10.0 ),
mSnapGridOffsetX( 0.0 ),
mSnapGridOffsetY( 0.0 ),
mAlignmentSnap( true ),
mAlignmentSnapTolerance( 2 ),
mActiveItemCommand( 0 ),
mActiveMultiFrameCommand( 0 ),
mAtlasComposition( this )
: QGraphicsScene( 0 )
, mMapRenderer( mapRenderer )
, mPlotStyle( QgsComposition::Preview )
, mPageWidth( 297 )
, mPageHeight( 210 )
, mSpaceBetweenPages( 10 )
, mPrintAsRaster( false )
, mUseAdvancedEffects( true )
, mSelectionTolerance( 0.0 )
, mSnapToGrid( false )
, mSnapGridResolution( 10.0 )
, mSnapGridOffsetX( 0.0 )
, mSnapGridOffsetY( 0.0 )
, mAlignmentSnap( true )
, mAlignmentSnapTolerance( 2 )
, mActiveItemCommand( 0 )
, mActiveMultiFrameCommand( 0 )
, mAtlasComposition( this )
{
setBackgroundBrush( Qt::gray );
addPaperItem();
@@ -293,13 +291,14 @@ const QgsComposerItem* QgsComposition::getComposerItemById( QString theId ) cons
}
return 0;
}
/*

#if 0
const QgsComposerItem* QgsComposition::getComposerItemByUuid( QString theUuid, bool inAllComposers ) const
{
//This does not work since it seems impossible to get the QgisApp::instance() from here... Is there a workaround ?
QSet<QgsComposer*> composers = QSet<QgsComposer*>();

if( inAllComposers )
if ( inAllComposers )
{
composers = QgisApp::instance()->printComposers();
}
@@ -328,7 +327,7 @@ const QgsComposerItem* QgsComposition::getComposerItemByUuid( QString theUuid, b

return 0;
}
*/
#endif

const QgsComposerItem* QgsComposition::getComposerItemByUuid( QString theUuid ) const
{
@@ -507,6 +506,7 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
mPrintResolution = compositionElem.attribute( "printResolution", "300" ).toInt();

updatePaperItems();

return true;
}

@@ -574,6 +574,7 @@ bool QgsComposition::loadFromTemplate( const QDomDocument& doc, QMap<QString, QS
QDomNode composerItemNode = composerItemsNodes.at( i );
if ( composerItemNode.isElement() )
{
composerItemNode.toElement().setAttribute( "templateUuid", composerItemNode.toElement().attribute( "uuid" ) );
composerItemNode.toElement().removeAttribute( "uuid" );
}
}
@@ -803,7 +804,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
pushAddRemoveCommand( newTable, tr( "Table added" ) );
}
}
//html
// html
QDomNodeList composerHtmlList = elem.elementsByTagName( "ComposerHtml" );
for ( int i = 0; i < composerHtmlList.size(); ++i )
{
@@ -813,6 +814,17 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
newHtml->setCreateUndoCommands( true );
this->addMultiFrame( newHtml );
}


// groups (must be last as it references uuids of above items)
QDomNodeList groupList = elem.elementsByTagName( "ComposerItemGroup" );
for ( int i = 0; i < groupList.size(); ++i )
{
QDomElement groupElem = groupList.at( i ).toElement();
QgsComposerItemGroup *newGroup = new QgsComposerItemGroup( this );
newGroup->readXML( groupElem, doc );
addItem( newGroup );
}
}

void QgsComposition::addItemToZList( QgsComposerItem* item )
@@ -822,7 +834,6 @@ void QgsComposition::addItemToZList( QgsComposerItem* item )
return;
}
mItemZList.push_back( item );
QgsDebugMsg( QString::number( mItemZList.size() ) );
item->setZValue( mItemZList.size() );
}

@@ -1386,7 +1397,7 @@ QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, d
{
snappedItems.append( qMakePair( currentItem, QgsComposerItem::Middle ) );
}
else if ( qgsDoubleNear( currentXCoord, currentItem->transform().dx() + currentItem->rect().width(), itemTolerance ) )
else if ( qgsDoubleNear( currentXCoord, currentItem->transform().dx() + currentItem->rect().width(), itemTolerance ) )
{
snappedItems.append( qMakePair( currentItem, QgsComposerItem::MiddleRight ) );
}

0 comments on commit 745fcbf

Please sign in to comment.