Skip to content

Commit 576b05c

Browse files
committedJun 23, 2011
Composer legend: support point symbol sizes in map units
1 parent 3bd37ec commit 576b05c

File tree

7 files changed

+187
-14
lines changed

7 files changed

+187
-14
lines changed
 

‎src/app/composer/qgscomposerlegendwidget.cpp

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgscomposerlegenditemdialog.h"
2121
#include "qgscomposerlegendlayersdialog.h"
2222
#include "qgscomposeritemwidget.h"
23+
#include "qgscomposermap.h"
2324
#include <QFontDialog>
2425

2526
#include "qgsapplegendinterface.h"
@@ -88,6 +89,17 @@ void QgsComposerLegendWidget::setGuiElements()
8889
{
8990
mCheckBoxAutoUpdate->setChecked( mLegend->model()->autoUpdate() );
9091
}
92+
refreshMapComboBox();
93+
94+
const QgsComposerMap* map = mLegend->composerMap();
95+
if ( map )
96+
{
97+
mMapComboBox->setCurrentIndex( mMapComboBox->findData( map->id() ) );
98+
}
99+
else
100+
{
101+
mMapComboBox->setCurrentIndex( mMapComboBox->findData( -1 ) );
102+
}
91103

92104
blockAllSignals( false );
93105
}
@@ -391,6 +403,43 @@ void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged( int state )
391403
}
392404
}
393405

406+
void QgsComposerLegendWidget::on_mMapComboBox_currentIndexChanged( int index )
407+
{
408+
if ( !mLegend )
409+
{
410+
return;
411+
}
412+
413+
QVariant itemData = mMapComboBox->itemData( index );
414+
if ( itemData.type() == QVariant::Invalid )
415+
{
416+
return;
417+
}
418+
419+
const QgsComposition* comp = mLegend->composition();
420+
if ( !comp )
421+
{
422+
return;
423+
}
424+
425+
int mapNr = itemData.toInt();
426+
if ( mapNr < 0 )
427+
{
428+
mLegend->setComposerMap( 0 );
429+
}
430+
else
431+
{
432+
const QgsComposerMap* map = comp->getComposerMapById( mapNr );
433+
if ( map )
434+
{
435+
mLegend->beginCommand( tr( "Legend map changed" ) );
436+
mLegend->setComposerMap( map );
437+
mLegend->update();
438+
mLegend->endCommand();
439+
}
440+
}
441+
}
442+
394443
void QgsComposerLegendWidget::on_mAddToolButton_clicked()
395444
{
396445
if ( !mLegend )
@@ -445,13 +494,13 @@ void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
445494
mLegend->beginCommand( "Legend item removed" );
446495

447496
QItemSelectionModel* selectionModel = mItemTreeView->selectionModel();
448-
if( !selectionModel )
497+
if ( !selectionModel )
449498
{
450499
return;
451500
}
452501

453502
QModelIndexList selection = selectionModel->selectedIndexes();
454-
for( int i = selection.size() - 1; i >= 0; --i )
503+
for ( int i = selection.size() - 1; i >= 0; --i )
455504
{
456505
QModelIndex parentIndex = selection.at( i ).parent();
457506
itemModel->removeRow( selection.at( i ).row(), parentIndex );
@@ -588,4 +637,47 @@ void QgsComposerLegendWidget::blockAllSignals( bool b )
588637
{
589638
mItemTreeView->blockSignals( b );
590639
mCheckBoxAutoUpdate->blockSignals( b );
640+
mMapComboBox->blockSignals( b );
641+
}
642+
643+
void QgsComposerLegendWidget::refreshMapComboBox()
644+
{
645+
if ( !mLegend )
646+
{
647+
return;
648+
}
649+
650+
const QgsComposition* composition = mLegend->composition();
651+
if ( !composition )
652+
{
653+
return;
654+
}
655+
656+
//save current entry
657+
int currentMapId = mMapComboBox->itemData( mMapComboBox->currentIndex() ).toInt();
658+
mMapComboBox->clear();
659+
660+
QList<const QgsComposerMap*> availableMaps = composition->composerMapItems();
661+
QList<const QgsComposerMap*>::const_iterator mapItemIt = availableMaps.constBegin();
662+
for ( ; mapItemIt != availableMaps.constEnd(); ++mapItemIt )
663+
{
664+
mMapComboBox->addItem( tr( "Map %1" ).arg(( *mapItemIt )->id() ), ( *mapItemIt )->id() );
665+
}
666+
mMapComboBox->addItem( tr( "None" ), -1 );
667+
668+
//the former entry is not there anymore
669+
int entry = mMapComboBox->findData( currentMapId );
670+
if ( entry == -1 )
671+
{
672+
}
673+
else
674+
{
675+
mMapComboBox->setCurrentIndex( entry );
676+
}
677+
}
678+
679+
void QgsComposerLegendWidget::showEvent( QShowEvent * event )
680+
{
681+
refreshMapComboBox();
682+
QWidget::showEvent( event );
591683
}

‎src/app/composer/qgscomposerlegendwidget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
5151
void on_mItemFontButton_clicked();
5252
void on_mBoxSpaceSpinBox_valueChanged( double d );
5353
void on_mCheckBoxAutoUpdate_stateChanged( int state );
54+
void on_mMapComboBox_currentIndexChanged( int index );
5455

5556
//item manipulation
5657
void on_mMoveDownToolButton_clicked();
@@ -62,13 +63,17 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
6263
void on_mUpdateAllPushButton_clicked();
6364
void on_mAddGroupButton_clicked();
6465

66+
protected:
67+
void showEvent( QShowEvent * event );
68+
6569
private slots:
6670
/**Sets GUI according to state of mLegend*/
6771
void setGuiElements();
6872

6973
private:
7074
QgsComposerLegendWidget();
7175
void blockAllSignals( bool b );
76+
void refreshMapComboBox();
7277

7378

7479
QgsComposerLegend* mLegend;

‎src/core/composer/qgscomposerlegend.cpp

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgscomposerlegend.h"
1919
#include "qgscomposerlegenditem.h"
20+
#include "qgscomposermap.h"
2021
#include "qgsmaplayer.h"
2122
#include "qgsmaplayerregistry.h"
2223
#include "qgsmaprenderer.h"
@@ -33,7 +34,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
3334
, mBoxSpace( 2 )
3435
, mLayerSpace( 2 )
3536
, mSymbolSpace( 2 )
36-
, mIconLabelSpace( 2 )
37+
, mIconLabelSpace( 2 ), mComposerMap( 0 )
3738
{
3839
//QStringList idList = layerIdList();
3940
//mLegendModel.setLayerSet( idList );
@@ -50,7 +51,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
5051
connect( &mLegendModel, SIGNAL( layersChanged() ), this, SLOT( synchronizeWithModel() ) );
5152
}
5253

53-
QgsComposerLegend::QgsComposerLegend(): QgsComposerItem( 0 )
54+
QgsComposerLegend::QgsComposerLegend(): QgsComposerItem( 0 ), mComposerMap( 0 )
5455
{
5556

5657
}
@@ -394,23 +395,49 @@ void QgsComposerLegend::drawSymbolV2( QPainter* p, QgsSymbolV2* s, double curren
394395
rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
395396
}
396397

398+
//consider relation to composer map for symbol sizes in mm
399+
bool sizeInMapUnits = s->outputUnit() == QgsSymbolV2::MapUnit;
400+
double mmPerMapUnit = 1;
401+
if ( mComposerMap )
402+
{
403+
mmPerMapUnit = mComposerMap->mapUnitsToMM();
404+
}
405+
QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s );
406+
397407
//Consider symbol size for point markers
398408
double height = mSymbolHeight;
399409
double width = mSymbolWidth;
400-
if ( s->type() == QgsSymbolV2::Marker )
410+
double size = 0;
411+
412+
if ( markerSymbol )
401413
{
402-
QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s );
403-
if ( markerSymbol )
414+
size = markerSymbol->size();
415+
height = size;
416+
width = size;
417+
if ( mComposerMap && sizeInMapUnits )
404418
{
405-
height = markerSymbol->size();
406-
width = markerSymbol->size();
419+
height *= mmPerMapUnit;
420+
width *= mmPerMapUnit;
421+
markerSymbol->setSize( width );
407422
}
408423
}
409424

410425
p->save();
411426
p->translate( currentXPosition, currentYCoord );
412427
p->scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );
428+
429+
if ( markerSymbol && sizeInMapUnits )
430+
{
431+
s->setOutputUnit( QgsSymbolV2::MM );
432+
}
413433
s->drawPreviewIcon( p, QSize( width * rasterScaleFactor, height * rasterScaleFactor ) );
434+
435+
if ( markerSymbol && sizeInMapUnits )
436+
{
437+
s->setOutputUnit( QgsSymbolV2::MapUnit );
438+
markerSymbol->setSize( size );
439+
}
440+
414441
p->restore();
415442
currentXPosition += width;
416443
symbolHeight = height;
@@ -609,6 +636,11 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
609636
composerLegendElem.setAttribute( "symbolWidth", mSymbolWidth );
610637
composerLegendElem.setAttribute( "symbolHeight", mSymbolHeight );
611638

639+
if ( mComposerMap )
640+
{
641+
composerLegendElem.setAttribute( "map", mComposerMap->id() );
642+
}
643+
612644
//write model properties
613645
mLegendModel.writeXML( composerLegendElem, doc );
614646

@@ -659,6 +691,12 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
659691
mSymbolWidth = itemElem.attribute( "symbolWidth", "7.0" ).toDouble();
660692
mSymbolHeight = itemElem.attribute( "symbolHeight", "14.0" ).toDouble();
661693

694+
//composer map
695+
if ( !itemElem.attribute( "map" ).isEmpty() )
696+
{
697+
mComposerMap = mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() );
698+
}
699+
662700
//read model properties
663701
QDomNodeList modelNodeList = itemElem.elementsByTagName( "Model" );
664702
if ( modelNodeList.size() > 0 )
@@ -678,3 +716,15 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
678716
emit itemChanged();
679717
return true;
680718
}
719+
720+
void QgsComposerLegend::setComposerMap( const QgsComposerMap* map )
721+
{
722+
mComposerMap = map;
723+
QObject::connect( map, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
724+
}
725+
726+
void QgsComposerLegend::invalidateCurrentMap()
727+
{
728+
disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
729+
mComposerMap = 0;
730+
}

‎src/core/composer/qgscomposerlegend.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class QgsSymbol;
2525
class QgsSymbolV2;
2626
class QgsComposerGroupItem;
2727
class QgsComposerLayerItem;
28+
class QgsComposerMap;
2829

2930
/** \ingroup MapComposer
3031
* A legend that can be placed onto a map composition
@@ -86,6 +87,9 @@ class CORE_EXPORT QgsComposerLegend: public QgsComposerItem
8687
double symbolHeight() const {return mSymbolHeight;}
8788
void setSymbolHeight( double h ) {mSymbolHeight = h;}
8889

90+
void setComposerMap( const QgsComposerMap* map );
91+
const QgsComposerMap* composerMap() const { return mComposerMap; }
92+
8993
/**Updates the model and all legend entries*/
9094
void updateLegend();
9195

@@ -104,6 +108,8 @@ class CORE_EXPORT QgsComposerLegend: public QgsComposerItem
104108
public slots:
105109
/**Data changed*/
106110
void synchronizeWithModel();
111+
/**Sets mCompositionMap to 0 if the map is deleted*/
112+
void invalidateCurrentMap();
107113

108114
protected:
109115
QString mTitle;
@@ -129,6 +135,9 @@ class CORE_EXPORT QgsComposerLegend: public QgsComposerItem
129135

130136
QgsLegendModel mLegendModel;
131137

138+
/**Reference to map (because symbols are sometimes in map units)*/
139+
const QgsComposerMap* mComposerMap;
140+
132141

133142
private:
134143
QgsComposerLegend(); //forbidden

‎src/core/composer/qgscomposermap.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
262262
void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
263263
bool drawCanvasItems() const { return mDrawCanvasItems; }
264264

265+
/**Returns the conversion factor map units -> mm*/
266+
double mapUnitsToMM() const;
267+
265268
signals:
266269
void extentChanged();
267270

@@ -392,8 +395,6 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
392395
void mapPolygon( QPolygonF& poly ) const;
393396
/**Calculates the extent to request and the yShift of the top-left point in case of rotation.*/
394397
void requestedExtent( QgsRectangle& extent ) const;
395-
/**Returns the conversion factor map units -> mm*/
396-
double mapUnitsToMM() const;
397398
/**Scales a composer map shift (in MM) and rotates it by mRotation
398399
@param xShift in: shift in x direction (in item units), out: xShift in map units
399400
@param yShift in: shift in y direction (in item units), out: yShift in map units*/

‎src/gui/qgscomposerview.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ void QgsComposerView::addComposerMap( QgsComposerMap* map )
552552

553553
void QgsComposerView::addComposerScaleBar( QgsComposerScaleBar* scaleBar )
554554
{
555-
//take first available map...
555+
//take first available map
556556
QList<const QgsComposerMap*> mapItemList = composition()->composerMapItems();
557557
if ( mapItemList.size() > 0 )
558558
{
@@ -569,6 +569,12 @@ void QgsComposerView::addComposerScaleBar( QgsComposerScaleBar* scaleBar )
569569

570570
void QgsComposerView::addComposerLegend( QgsComposerLegend* legend )
571571
{
572+
//take first available map
573+
QList<const QgsComposerMap*> mapItemList = composition()->composerMapItems();
574+
if ( mapItemList.size() > 0 )
575+
{
576+
legend->setComposerMap( mapItemList.at( 0 ) );
577+
}
572578
scene()->addItem( legend );
573579
emit composerLegendAdded( legend );
574580
scene()->clearSelection();

‎src/ui/qgscomposerlegendwidgetbase.ui

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<item row="0" column="0">
4242
<widget class="QToolBox" name="toolBox">
4343
<property name="currentIndex">
44-
<number>1</number>
44+
<number>0</number>
4545
</property>
4646
<widget class="QWidget" name="page">
4747
<property name="geometry">
@@ -163,7 +163,7 @@
163163
</property>
164164
</widget>
165165
</item>
166-
<item row="12" column="0">
166+
<item row="14" column="0">
167167
<spacer name="verticalSpacer">
168168
<property name="orientation">
169169
<enum>Qt::Vertical</enum>
@@ -176,6 +176,16 @@
176176
</property>
177177
</spacer>
178178
</item>
179+
<item row="13" column="0">
180+
<widget class="QComboBox" name="mMapComboBox"/>
181+
</item>
182+
<item row="12" column="0">
183+
<widget class="QLabel" name="mMapLabel">
184+
<property name="text">
185+
<string>Map</string>
186+
</property>
187+
</widget>
188+
</item>
179189
</layout>
180190
</widget>
181191
<widget class="QWidget" name="page_2">

0 commit comments

Comments
 (0)
Please sign in to comment.