Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix loading composition triggers render of all composer maps
Also cleanup some more unused API methods
  • Loading branch information
nyalldawson committed Mar 21, 2017
1 parent 8d01573 commit 041129d
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 61 deletions.
3 changes: 2 additions & 1 deletion doc/api_break.dox
Expand Up @@ -732,7 +732,8 @@ of QgsDataDefined objects.
composerLabelAdded, composerMapAdded, composerScaleBarAdded, composerLegendAdded, composerPictureAdded,
composerShapeAdded, and composerTableFrameAdded were removed. Use the general itemAdded signal instead to catch
all these item added events.

- addComposerMap no longer takes a setDefaultPreviewStyle argument.
- the mapsToRestore parameter has been removed from addItemsFromXml

QgsCoordinateReferenceSystem {#qgis_api_break_3_0_QgsCoordinateReferenceSystem}
----------------------------
Expand Down
30 changes: 7 additions & 23 deletions python/core/composer/qgscomposition.sip
Expand Up @@ -253,32 +253,16 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
/** Reads settings from xml file*/
bool readXml( const QDomElement& compositionElem, const QDomDocument& doc );

/** Load a template document
* @param doc template document
* @param substitutionMap map with text to replace. Text needs to be enclosed by brackets (e.g. '[text]' )
* @param addUndoCommands whether or not to add undo commands
* @param clearComposition set to true to clear the existing composition and read all composition and
* atlas properties from the template. Set to false to only add new items from the template, without
* overwriting the existing items or composition settings.
*/
bool loadFromTemplate( const QDomDocument& doc, QMap<QString, QString>* substitutionMap = 0,
bool addUndoCommands = false, const bool clearComposition = true );
void addItemsFromXml( const QDomElement &elem, const QDomDocument &doc,
bool addUndoCommands = false, QPointF *pos = 0, bool pasteInPlace = false );

/** 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
* @param pasteInPlace whether the position should be kept but mapped to the page origin. (the page is the page under to the mouse cursor)
* @note parameters mapsToRestore, addUndoCommands pos and pasteInPlace not available in python bindings
*/
void addItemsFromXml( const QDomElement& elem, const QDomDocument& doc );

/** Adds item to z list. Usually called from constructor of QgsComposerItem*/
void addItemToZList( QgsComposerItem* item );
/** Removes item from z list. Usually called from destructor of QgsComposerItem*/
void removeItemFromZList( QgsComposerItem* item );
void addItemToZList( QgsComposerItem *item );

void removeItemFromZList( QgsComposerItem *item );


//functions to move selected items in hierarchy
void raiseSelectedItems();
Expand Down Expand Up @@ -382,7 +366,7 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
/** Adds label to the graphics scene and advises composer to create a widget for it (through signal)*/
void addComposerLabel( QgsComposerLabel* label );
/** Adds map to the graphics scene and advises composer to create a widget for it (through signal)*/
void addComposerMap( QgsComposerMap* map, const bool setDefaultPreviewStyle = true );
void addComposerMap( QgsComposerMap* map );
/** Adds scale bar to the graphics scene and advises composer to create a widget for it (through signal)*/
void addComposerScaleBar( QgsComposerScaleBar* scaleBar );
/** Adds legend to the graphics scene and advises composer to create a widget for it (through signal)*/
Expand Down
30 changes: 4 additions & 26 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -1097,7 +1097,7 @@ bool QgsComposition::loadFromTemplate( const QDomDocument &doc, QMap<QString, QS
}

//addItemsFromXML
addItemsFromXml( importDoc.documentElement(), importDoc, nullptr, addUndoCommands, nullptr );
addItemsFromXml( importDoc.documentElement(), importDoc, addUndoCommands, nullptr );

return true;
}
Expand Down Expand Up @@ -1131,7 +1131,7 @@ QPointF QgsComposition::minPointFromXml( const QDomElement &elem ) const
}
}

void QgsComposition::addItemsFromXml( const QDomElement &elem, const QDomDocument &doc, QMap< QgsComposerMap *, int > *mapsToRestore,
void QgsComposition::addItemsFromXml( const QDomElement &elem, const QDomDocument &doc,
bool addUndoCommands, QPointF *pos, bool pasteInPlace )
{
QPointF *pasteInPlacePt = nullptr;
Expand Down Expand Up @@ -1195,21 +1195,10 @@ void QgsComposition::addItemsFromXml( const QDomElement &elem, const QDomDocumen
QDomElement currentComposerMapElem = composerMapList.at( i ).toElement();
QgsComposerMap *newMap = new QgsComposerMap( this );

if ( mapsToRestore )
{
newMap->setUpdatesEnabled( false );
}

newMap->readXml( currentComposerMapElem, doc );
newMap->assignFreeId();

if ( mapsToRestore )
{
mapsToRestore->insert( newMap, static_cast< int >( newMap->previewMode() ) );
newMap->setPreviewMode( QgsComposerMap::Rectangle );
newMap->setUpdatesEnabled( true );
}
addComposerMap( newMap, false );
addComposerMap( newMap );
newMap->setZValue( newMap->zValue() + zOrderOffset );
if ( pos )
{
Expand Down Expand Up @@ -2486,20 +2475,9 @@ void QgsComposition::addComposerLabel( QgsComposerLabel *label )
emit itemAdded( label );
}

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

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

updateBounds();
connect( map, SIGNAL( sizeChanged() ), this, SLOT( updateBounds() ) );

Expand Down
5 changes: 2 additions & 3 deletions src/core/composer/qgscomposition.h
Expand Up @@ -484,13 +484,12 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
/** 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
* @param pasteInPlace whether the position should be kept but mapped to the page origin. (the page is the page under to the mouse cursor)
* @note parameters mapsToRestore, addUndoCommands pos and pasteInPlace not available in python bindings
*/
void addItemsFromXml( const QDomElement &elem, const QDomDocument &doc, QMap< QgsComposerMap *, int > *mapsToRestore = nullptr,
void addItemsFromXml( const QDomElement &elem, const QDomDocument &doc,
bool addUndoCommands = false, QPointF *pos = nullptr, bool pasteInPlace = false );

//! Adds item to z list. Usually called from constructor of QgsComposerItem
Expand Down Expand Up @@ -602,7 +601,7 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
//! Adds label to the graphics scene and advises composer to create a widget for it (through signal)
void addComposerLabel( QgsComposerLabel *label );
//! Adds map to the graphics scene and advises composer to create a widget for it (through signal)
void addComposerMap( QgsComposerMap *map, const bool setDefaultPreviewStyle = true );
void addComposerMap( QgsComposerMap *map );
//! Adds scale bar to the graphics scene and advises composer to create a widget for it (through signal)
void addComposerScaleBar( QgsComposerScaleBar *scaleBar );
//! Adds legend to the graphics scene and advises composer to create a widget for it (through signal)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgscomposerview.cpp
Expand Up @@ -1543,7 +1543,7 @@ void QgsComposerView::pasteItems( PasteMode mode )
pt = mapToScene( viewport()->rect().center() );
}
bool pasteInPlace = ( mode == PasteModeInPlace );
composition()->addItemsFromXml( docElem, doc, nullptr, true, &pt, pasteInPlace );
composition()->addItemsFromXml( docElem, doc, true, &pt, pasteInPlace );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgsatlascomposition.cpp
Expand Up @@ -153,7 +153,7 @@ void TestQgsAtlasComposition::init()
mAtlasMap->setFrameEnabled( true );
// Make sure it doesn't try to render a map for caching onto a still 0-sized image
mAtlasMap->setPreviewMode( QgsComposerMap::Rectangle );
mComposition->addComposerMap( mAtlasMap, false );
mComposition->addComposerMap( mAtlasMap );
mAtlasMap->setLayers( QList<QgsMapLayer *>() << mVectorLayer );

mAtlas = &mComposition->atlasComposition();
Expand All @@ -167,7 +167,7 @@ void TestQgsAtlasComposition::init()
mOverview->overview()->setFrameMap( mAtlasMap->id() );
mOverview->setPreviewMode( QgsComposerMap::Rectangle );
mOverview->setLayers( QList<QgsMapLayer *>() << mVectorLayer );
mComposition->addComposerMap( mOverview, false );
mComposition->addComposerMap( mOverview );
mOverview->setNewExtent( QgsRectangle( 49670.718, 6415139.086, 699672.519, 7065140.887 ) );

// set the fill symbol of the overview map
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgscomposermap.cpp
Expand Up @@ -140,7 +140,7 @@ void TestQgsComposerMap::uniqueId()
QDomDocument doc;
QDomElement documentElement = doc.createElement( QStringLiteral( "ComposerItemClipboard" ) );
mComposerMap->writeXml( documentElement, doc );
mComposition->addItemsFromXml( documentElement, doc, 0, false );
mComposition->addItemsFromXml( documentElement, doc, false );

//test if both composer maps have different ids
const QgsComposerMap *newMap = 0;
Expand Down
6 changes: 2 additions & 4 deletions tests/src/python/test_qgscomposermap.py
Expand Up @@ -186,16 +186,14 @@ def testMapCrs(self):
self.assertEqual(map.crs().authid(), 'EPSG:4326')
self.assertFalse(map.presetCrs().isValid())

# Fails because addItemsFromXml has been commented out in sip
@unittest.expectedFailure
def testuniqueId(self):
doc = QDomDocument()
documentElement = doc.createElement('ComposerItemClipboard')
self.mComposition.writeXml(documentElement, doc)
self.mComposition.addItemsFromXml(documentElement, doc, 0, False)
self.mComposition.addItemsFromXml(documentElement, doc)

# test if both composer maps have different ids
newMap = QgsComposerMap()
newMap = QgsComposerMap(self.mComposition, 0, 0, 10, 10)
mapList = self.mComposition.composerMapItems()

for mapIt in mapList:
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 041129d

Please sign in to comment.