Skip to content
Permalink
Browse files
Fix lazy restore of composer maps from xml
  • Loading branch information
mhugent committed Feb 24, 2012
1 parent 0ed1df8 commit c5e527d636e371c4f5f2a9801f82ace031980974
@@ -107,7 +107,7 @@ class QgsComposition: QGraphicsScene
@param doc xml document
@param addUndoCommands insert AddItem commands if true (e.g. for copy/paste)
@param pos item position. Optional, take position from xml if 0*/
void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, bool addUndoCommands = false, QPointF* pos = 0 );
//void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore, bool addUndoCommands = false, QPointF* pos = 0 );

/**Adds item to z list. Usually called from constructor of QgsComposerItem*/
void addItemToZList( QgsComposerItem* item );
@@ -1126,10 +1126,10 @@ void QgsComposer::showEvent( QShowEvent* event )
if ( event->spontaneous() ) //event from the window system
{
//go through maps and restore original preview modes (show on demand after loading from project file)
QMap< QgsComposerMap*, QgsComposerMap::PreviewMode >::iterator mapIt = mMapsToRestore.begin();
QMap< QgsComposerMap*, int >::iterator mapIt = mMapsToRestore.begin();
for ( ; mapIt != mMapsToRestore.end(); ++mapIt )
{
mapIt.key()->setPreviewMode( mapIt.value() );
mapIt.key()->setPreviewMode(( QgsComposerMap::PreviewMode )( mapIt.value() ) );
mapIt.key()->cache();
mapIt.key()->update();
}
@@ -1186,10 +1186,10 @@ void QgsComposer::writeXML( QDomNode& parentNode, QDomDocument& doc )
composerElem.setAttribute( "title", mTitle );

//change preview mode of minimised / hidden maps before saving XML (show contents only on demand)
QMap< QgsComposerMap*, QgsComposerMap::PreviewMode >::iterator mapIt = mMapsToRestore.begin();
QMap< QgsComposerMap*, int >::iterator mapIt = mMapsToRestore.begin();
for ( ; mapIt != mMapsToRestore.end(); ++mapIt )
{
mapIt.key()->setPreviewMode( mapIt.value() );
mapIt.key()->setPreviewMode(( QgsComposerMap::PreviewMode )( mapIt.value() ) );
}
mMapsToRestore.clear();

@@ -1281,7 +1281,7 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
//read and restore all the items
if ( mComposition )
{
mComposition->addItemsFromXML( composerElem, doc );
mComposition->addItemsFromXML( composerElem, doc, &mMapsToRestore );
}

mComposition->sortZList();
@@ -321,7 +321,7 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
QUndoView* mUndoView;

//! We load composer map content from project xml only on demand. Therefore we need to store the real preview mode type
QMap< QgsComposerMap*, QgsComposerMap::PreviewMode > mMapsToRestore;
QMap< QgsComposerMap*, int > mMapsToRestore;

QDockWidget* mItemDock;
QDockWidget* mUndoDock;
@@ -16,7 +16,6 @@

#include "qgscomposition.h"
#include "qgscomposeritem.h"
#include "qgscomposermap.h"
#include "qgspaperitem.h"
#include "qgscomposerarrow.h"
#include "qgscomposerlabel.h"
@@ -261,7 +260,8 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
return true;
}

void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, bool addUndoCommands, QPointF* pos )
void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore,
bool addUndoCommands, QPointF* pos )
{
QDomNodeList composerLabelList = elem.elementsByTagName( "ComposerLabel" );
for ( int i = 0; i < composerLabelList.size(); ++i )
@@ -286,11 +286,19 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
QDomElement currentComposerMapElem = composerMapList.at( i ).toElement();
QgsComposerMap* newMap = new QgsComposerMap( this );
newMap->readXML( currentComposerMapElem, doc );

if ( mapsToRestore )
{
mapsToRestore->insert( newMap, ( int )( newMap->previewMode() ) );
newMap->setPreviewMode( QgsComposerMap::Rectangle );
}
addComposerMap( newMap, false );

if ( pos )
{
newMap->setItemPosition( pos->x(), pos->y() );
}
addComposerMap( newMap );

if ( addUndoCommands )
{
pushAddRemoveCommand( newMap, tr( "Map added" ) );
@@ -1000,12 +1008,20 @@ void QgsComposition::addComposerLabel( QgsComposerLabel* label )
emit selectedItemChanged( label );
}

void QgsComposition::addComposerMap( QgsComposerMap* map )
void QgsComposition::addComposerMap( QgsComposerMap* map, bool setDefaultPreviewStyle )
{
addItem( map );
//set default preview mode to cache. Must be done here between adding composer map to scene and emiting signal
map->setPreviewMode( QgsComposerMap::Cache );
map->cache();
if ( setDefaultPreviewStyle )
{
//set default preview mode to cache. Must be done here between adding composer map to scene and emiting signal
map->setPreviewMode( QgsComposerMap::Cache );
}

if ( map->previewMode() != QgsComposerMap::Rectangle )
{
map->cache();
}

emit composerMapAdded( map );
clearSelection();
map->setSelected( true );
@@ -144,9 +144,11 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
/**Add items from XML representation to the graphics scene (for project file reading, pasting items from clipboard)
@param elem items parent element, e.g. \verbatim <Composer> \endverbatim or \verbatim <ComposerItemClipboard> \endverbatim
@param doc xml document
@param mapsToRestore for reading from project file: set preview move 'rectangle' to all maps and save the preview states to show composer maps on demand
@param addUndoCommands insert AddItem commands if true (e.g. for copy/paste)
@param pos item position. Optional, take position from xml if 0*/
void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, bool addUndoCommands = false, QPointF* pos = 0 );
void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore = 0,
bool addUndoCommands = false, QPointF* pos = 0 );

/**Adds item to z list. Usually called from constructor of QgsComposerItem*/
void addItemToZList( QgsComposerItem* item );
@@ -194,7 +196,7 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
/**Adds label to the graphics scene and advices composer to create a widget for it (through signal)*/
void addComposerLabel( QgsComposerLabel* label );
/**Adds map to the graphics scene and advices composer to create a widget for it (through signal)*/
void addComposerMap( QgsComposerMap* map );
void addComposerMap( QgsComposerMap* map, bool setDefaultPreviewStyle = true );
/**Adds scale bar to the graphics scene and advices composer to create a widget for it (through signal)*/
void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
/**Adds legend to the graphics scene and advices composer to create a widget for it (through signal)*/
@@ -480,7 +480,7 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
if ( composition() )
{
QPointF pt = mapToScene( mapFromGlobal( QCursor::pos() ) );
composition()->addItemsFromXML( docElem, doc, true, &pt );
composition()->addItemsFromXML( docElem, doc, 0, true, &pt );
}
}
}

0 comments on commit c5e527d

Please sign in to comment.