Skip to content

Commit 0ce4e02

Browse files
author
mhugent
committed
[FEATURE] Added possibility to keep the current layers in a composer map even if further layers are added to the main map
git-svn-id: http://svn.osgeo.org/qgis/trunk@10963 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 5996d17 commit 0ce4e02

File tree

5 files changed

+239
-95
lines changed

5 files changed

+239
-95
lines changed

src/app/composer/qgscomposermapwidget.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,18 @@ void QgsComposerMapWidget::updateGuiElements()
252252
mXMaxLineEdit->setText( QString::number( composerMapExtent.xMaximum(), 'f', 3 ) );
253253
mYMinLineEdit->setText( QString::number( composerMapExtent.yMinimum(), 'f', 3 ) );
254254
mYMaxLineEdit->setText( QString::number( composerMapExtent.yMaximum(), 'f', 3 ) );
255+
256+
//keep layer list check box
257+
mKeepLayerListCheckBox->blockSignals(true);
258+
if(mComposerMap->keepLayerSet())
259+
{
260+
mKeepLayerListCheckBox->setCheckState(Qt::Checked);
261+
}
262+
else
263+
{
264+
mKeepLayerListCheckBox->setCheckState(Qt::Unchecked);
265+
}
266+
mKeepLayerListCheckBox->blockSignals(false);
255267
}
256268
}
257269

@@ -298,3 +310,23 @@ void QgsComposerMapWidget::on_mUpdatePreviewButton_clicked()
298310

299311
mUpdatePreviewButton->setEnabled( true );
300312
}
313+
314+
void QgsComposerMapWidget::on_mKeepLayerListCheckBox_stateChanged(int state)
315+
{
316+
if(!mComposerMap)
317+
{
318+
return;
319+
}
320+
321+
if(state == Qt::Checked)
322+
{
323+
mComposerMap->storeCurrentLayerSet();
324+
mComposerMap->setKeepLayerSet(true);
325+
}
326+
else
327+
{
328+
QStringList emptyLayerSet;
329+
mComposerMap->setLayerSet(emptyLayerSet);
330+
mComposerMap->setKeepLayerSet(false);
331+
}
332+
}

src/app/composer/qgscomposermapwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
4141
void on_mScaleLineEdit_editingFinished();
4242
void on_mSetToMapCanvasExtentButton_clicked();
4343
void on_mUpdatePreviewButton_clicked();
44+
void on_mKeepLayerListCheckBox_stateChanged(int state);
4445

4546
void on_mXMinLineEdit_editingFinished();
4647
void on_mXMaxLineEdit_editingFinished();

src/core/composer/qgscomposermap.cpp

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
int QgsComposerMap::mCurrentComposerId = 0;
4343

4444
QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height )
45-
: QgsComposerItem( x, y, width, height, composition )
45+
: QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet(false)
4646
{
4747
mComposition = composition;
4848
mMapRenderer = mComposition->mapRenderer();
@@ -69,7 +69,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
6969
}
7070

7171
QgsComposerMap::QgsComposerMap( QgsComposition *composition )
72-
: QgsComposerItem( 0, 0, 10, 10, composition )
72+
: QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet(false)
7373
{
7474
//Offset
7575
mXOffset = 0.0;
@@ -113,7 +113,16 @@ void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const
113113
QgsMapRenderer theMapRenderer;
114114
theMapRenderer.setExtent( extent );
115115
theMapRenderer.setOutputSize( size, dpi );
116-
theMapRenderer.setLayerSet( mMapRenderer->layerSet() );
116+
117+
//use stored layer set or read current set from main canvas
118+
if(mKeepLayerSet)
119+
{
120+
theMapRenderer.setLayerSet(mLayerSet);
121+
}
122+
else
123+
{
124+
theMapRenderer.setLayerSet( mMapRenderer->layerSet() );
125+
}
117126
theMapRenderer.setProjectionsEnabled( mMapRenderer->hasCrsTransformEnabled() );
118127
theMapRenderer.setDestinationSrs( mMapRenderer->destinationSrs() );
119128

@@ -240,6 +249,7 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
240249

241250
void QgsComposerMap::updateCachedImage( void )
242251
{
252+
syncLayerSet(); //layer list may have changed
243253
mCacheUpdated = false;
244254
cache();
245255
QGraphicsRectItem::update();
@@ -489,6 +499,15 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
489499
composerMapElem.setAttribute( "previewMode", "Rectangle" );
490500
}
491501

502+
if(mKeepLayerSet)
503+
{
504+
composerMapElem.setAttribute( "keepLayerSet", "true");
505+
}
506+
else
507+
{
508+
composerMapElem.setAttribute( "keepLayerSet", "false");
509+
}
510+
492511
//extent
493512
QDomElement extentElem = doc.createElement( "Extent" );
494513
extentElem.setAttribute( "xmin", QString::number( mExtent.xMinimum() ) );
@@ -497,6 +516,18 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
497516
extentElem.setAttribute( "ymax", QString::number( mExtent.yMaximum() ) );
498517
composerMapElem.appendChild( extentElem );
499518

519+
//layer set
520+
QDomElement layerSetElem = doc.createElement( "LayerSet" );
521+
QStringList::const_iterator layerIt = mLayerSet.constBegin();
522+
for(; layerIt != mLayerSet.constEnd(); ++layerIt)
523+
{
524+
QDomElement layerElem = doc.createElement( "Layer" );
525+
QDomText layerIdText = doc.createTextNode(*layerIt);
526+
layerElem.appendChild(layerIdText);
527+
layerSetElem.appendChild(layerElem);
528+
}
529+
composerMapElem.appendChild(layerSetElem);
530+
500531
#if 0
501532
// why is saving the map changing anything about the cache?
502533
mCacheUpdated = false;
@@ -545,6 +576,31 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
545576
mExtent = QgsRectangle( xmin, ymin, xmax, ymax );
546577
}
547578

579+
//mKeepLayerSet flag
580+
QString keepLayerSetFlag = itemElem.attribute( "keepLayerSet" );
581+
if(keepLayerSetFlag.compare("true", Qt::CaseInsensitive) == 0)
582+
{
583+
mKeepLayerSet = true;
584+
}
585+
else
586+
{
587+
mKeepLayerSet = false;
588+
}
589+
590+
//mLayerSet
591+
QDomNodeList layerSetNodeList = itemElem.elementsByTagName("LayerSet");
592+
QStringList layerSet;
593+
if(layerSetNodeList.size() > 0)
594+
{
595+
QDomElement layerSetElem = layerSetNodeList.at(0).toElement();
596+
QDomNodeList layerIdNodeList = layerSetElem.elementsByTagName("Layer");
597+
for(int i = 0; i < layerIdNodeList.size(); ++i)
598+
{
599+
layerSet << layerIdNodeList.at(i).toElement().text();
600+
}
601+
}
602+
mLayerSet = layerSet;
603+
548604
mDrawing = false;
549605
mNumCachedLayers = 0;
550606
mCacheUpdated = false;
@@ -565,3 +621,28 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
565621

566622
return true;
567623
}
624+
625+
void QgsComposerMap::storeCurrentLayerSet()
626+
{
627+
if(mMapRenderer)
628+
{
629+
mLayerSet = mMapRenderer->layerSet();
630+
}
631+
}
632+
633+
void QgsComposerMap::syncLayerSet()
634+
{
635+
if(mLayerSet.size() < 1 && !mMapRenderer)
636+
{
637+
return;
638+
}
639+
640+
QStringList currentLayerSet = mMapRenderer->layerSet();
641+
for(int i = mLayerSet.size() - 1; i >= 0; --i)
642+
{
643+
if(!currentLayerSet.contains(mLayerSet.at(i)))
644+
{
645+
mLayerSet.removeAt(i);
646+
}
647+
}
648+
}

src/core/composer/qgscomposermap.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
101101
PreviewMode previewMode() {return mPreviewMode;}
102102
void setPreviewMode( PreviewMode m ) {mPreviewMode = m;}
103103

104+
/**Getter for flag that determines if the stored layer set should be used or the current layer set of the qgis mapcanvas
105+
@note this function was added in version 1.2*/
106+
bool keepLayerSet() const {return mKeepLayerSet;}
107+
/**Setter for flag that determines if the stored layer set should be used or the current layer set of the qgis mapcanvas
108+
@note this function was added in version 1.2*/
109+
void setKeepLayerSet(bool enabled) {mKeepLayerSet = enabled;}
110+
111+
/**Getter for stored layer set that is used if mKeepLayerSet is true
112+
@note this function was added in version 1.2*/
113+
QStringList layerSet() const {return mLayerSet;}
114+
/**Setter for stored layer set that is used if mKeepLayerSet is true
115+
@note this function was added in version 1.2*/
116+
void setLayerSet(const QStringList& layerSet) {mLayerSet = layerSet;}
117+
/**Stores the current layer set of the qgis mapcanvas in mLayerSet*/
118+
void storeCurrentLayerSet();
119+
104120
// Set cache outdated
105121
void setCacheUpdated( bool u = false );
106122

@@ -176,11 +192,20 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
176192
/**Offset in y direction for showing map cache image*/
177193
double mYOffset;
178194

195+
/**Flag if layers to be displayed should be read from qgis canvas (true) or from stored list in mLayerSet (false)*/
196+
bool mKeepLayerSet;
197+
198+
/**Stored layer list (used if layer live-link mKeepLayerSet is disabled)*/
199+
QStringList mLayerSet;
200+
179201
/**For the generation of new unique ids*/
180202
static int mCurrentComposerId;
181203

182204
/**Establishes signal/slot connection for update in case of layer change*/
183205
void connectUpdateSlot();
206+
207+
/**Removes layer ids from mLayerSet that are no longer present in the qgis main map*/
208+
void syncLayerSet();
184209
};
185210

186211
#endif

0 commit comments

Comments
 (0)