Skip to content

Commit

Permalink
Composer legend: support point symbol sizes in map units
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 23, 2011
1 parent 3bd37ec commit 576b05c
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 14 deletions.
96 changes: 94 additions & 2 deletions src/app/composer/qgscomposerlegendwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qgscomposerlegenditemdialog.h"
#include "qgscomposerlegendlayersdialog.h"
#include "qgscomposeritemwidget.h"
#include "qgscomposermap.h"
#include <QFontDialog>

#include "qgsapplegendinterface.h"
Expand Down Expand Up @@ -88,6 +89,17 @@ void QgsComposerLegendWidget::setGuiElements()
{
mCheckBoxAutoUpdate->setChecked( mLegend->model()->autoUpdate() );
}
refreshMapComboBox();

const QgsComposerMap* map = mLegend->composerMap();
if ( map )
{
mMapComboBox->setCurrentIndex( mMapComboBox->findData( map->id() ) );
}
else
{
mMapComboBox->setCurrentIndex( mMapComboBox->findData( -1 ) );
}

blockAllSignals( false );
}
Expand Down Expand Up @@ -391,6 +403,43 @@ void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged( int state )
}
}

void QgsComposerLegendWidget::on_mMapComboBox_currentIndexChanged( int index )
{
if ( !mLegend )
{
return;
}

QVariant itemData = mMapComboBox->itemData( index );
if ( itemData.type() == QVariant::Invalid )
{
return;
}

const QgsComposition* comp = mLegend->composition();
if ( !comp )
{
return;
}

int mapNr = itemData.toInt();
if ( mapNr < 0 )
{
mLegend->setComposerMap( 0 );
}
else
{
const QgsComposerMap* map = comp->getComposerMapById( mapNr );
if ( map )
{
mLegend->beginCommand( tr( "Legend map changed" ) );
mLegend->setComposerMap( map );
mLegend->update();
mLegend->endCommand();
}
}
}

void QgsComposerLegendWidget::on_mAddToolButton_clicked()
{
if ( !mLegend )
Expand Down Expand Up @@ -445,13 +494,13 @@ void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
mLegend->beginCommand( "Legend item removed" );

QItemSelectionModel* selectionModel = mItemTreeView->selectionModel();
if( !selectionModel )
if ( !selectionModel )
{
return;
}

QModelIndexList selection = selectionModel->selectedIndexes();
for( int i = selection.size() - 1; i >= 0; --i )
for ( int i = selection.size() - 1; i >= 0; --i )
{
QModelIndex parentIndex = selection.at( i ).parent();
itemModel->removeRow( selection.at( i ).row(), parentIndex );
Expand Down Expand Up @@ -588,4 +637,47 @@ void QgsComposerLegendWidget::blockAllSignals( bool b )
{
mItemTreeView->blockSignals( b );
mCheckBoxAutoUpdate->blockSignals( b );
mMapComboBox->blockSignals( b );
}

void QgsComposerLegendWidget::refreshMapComboBox()
{
if ( !mLegend )
{
return;
}

const QgsComposition* composition = mLegend->composition();
if ( !composition )
{
return;
}

//save current entry
int currentMapId = mMapComboBox->itemData( mMapComboBox->currentIndex() ).toInt();
mMapComboBox->clear();

QList<const QgsComposerMap*> availableMaps = composition->composerMapItems();
QList<const QgsComposerMap*>::const_iterator mapItemIt = availableMaps.constBegin();
for ( ; mapItemIt != availableMaps.constEnd(); ++mapItemIt )
{
mMapComboBox->addItem( tr( "Map %1" ).arg(( *mapItemIt )->id() ), ( *mapItemIt )->id() );
}
mMapComboBox->addItem( tr( "None" ), -1 );

//the former entry is not there anymore
int entry = mMapComboBox->findData( currentMapId );
if ( entry == -1 )
{
}
else
{
mMapComboBox->setCurrentIndex( entry );
}
}

void QgsComposerLegendWidget::showEvent( QShowEvent * event )
{
refreshMapComboBox();
QWidget::showEvent( event );
}
5 changes: 5 additions & 0 deletions src/app/composer/qgscomposerlegendwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
void on_mItemFontButton_clicked();
void on_mBoxSpaceSpinBox_valueChanged( double d );
void on_mCheckBoxAutoUpdate_stateChanged( int state );
void on_mMapComboBox_currentIndexChanged( int index );

//item manipulation
void on_mMoveDownToolButton_clicked();
Expand All @@ -62,13 +63,17 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
void on_mUpdateAllPushButton_clicked();
void on_mAddGroupButton_clicked();

protected:
void showEvent( QShowEvent * event );

private slots:
/**Sets GUI according to state of mLegend*/
void setGuiElements();

private:
QgsComposerLegendWidget();
void blockAllSignals( bool b );
void refreshMapComboBox();


QgsComposerLegend* mLegend;
Expand Down
64 changes: 57 additions & 7 deletions src/core/composer/qgscomposerlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "qgscomposerlegend.h"
#include "qgscomposerlegenditem.h"
#include "qgscomposermap.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaprenderer.h"
Expand All @@ -33,7 +34,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
, mBoxSpace( 2 )
, mLayerSpace( 2 )
, mSymbolSpace( 2 )
, mIconLabelSpace( 2 )
, mIconLabelSpace( 2 ), mComposerMap( 0 )
{
//QStringList idList = layerIdList();
//mLegendModel.setLayerSet( idList );
Expand All @@ -50,7 +51,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
connect( &mLegendModel, SIGNAL( layersChanged() ), this, SLOT( synchronizeWithModel() ) );
}

QgsComposerLegend::QgsComposerLegend(): QgsComposerItem( 0 )
QgsComposerLegend::QgsComposerLegend(): QgsComposerItem( 0 ), mComposerMap( 0 )
{

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

//consider relation to composer map for symbol sizes in mm
bool sizeInMapUnits = s->outputUnit() == QgsSymbolV2::MapUnit;
double mmPerMapUnit = 1;
if ( mComposerMap )
{
mmPerMapUnit = mComposerMap->mapUnitsToMM();
}
QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s );

//Consider symbol size for point markers
double height = mSymbolHeight;
double width = mSymbolWidth;
if ( s->type() == QgsSymbolV2::Marker )
double size = 0;

if ( markerSymbol )
{
QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s );
if ( markerSymbol )
size = markerSymbol->size();
height = size;
width = size;
if ( mComposerMap && sizeInMapUnits )
{
height = markerSymbol->size();
width = markerSymbol->size();
height *= mmPerMapUnit;
width *= mmPerMapUnit;
markerSymbol->setSize( width );
}
}

p->save();
p->translate( currentXPosition, currentYCoord );
p->scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );

if ( markerSymbol && sizeInMapUnits )
{
s->setOutputUnit( QgsSymbolV2::MM );
}
s->drawPreviewIcon( p, QSize( width * rasterScaleFactor, height * rasterScaleFactor ) );

if ( markerSymbol && sizeInMapUnits )
{
s->setOutputUnit( QgsSymbolV2::MapUnit );
markerSymbol->setSize( size );
}

p->restore();
currentXPosition += width;
symbolHeight = height;
Expand Down Expand Up @@ -609,6 +636,11 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
composerLegendElem.setAttribute( "symbolWidth", mSymbolWidth );
composerLegendElem.setAttribute( "symbolHeight", mSymbolHeight );

if ( mComposerMap )
{
composerLegendElem.setAttribute( "map", mComposerMap->id() );
}

//write model properties
mLegendModel.writeXML( composerLegendElem, doc );

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

//composer map
if ( !itemElem.attribute( "map" ).isEmpty() )
{
mComposerMap = mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() );
}

//read model properties
QDomNodeList modelNodeList = itemElem.elementsByTagName( "Model" );
if ( modelNodeList.size() > 0 )
Expand All @@ -678,3 +716,15 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
emit itemChanged();
return true;
}

void QgsComposerLegend::setComposerMap( const QgsComposerMap* map )
{
mComposerMap = map;
QObject::connect( map, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
}

void QgsComposerLegend::invalidateCurrentMap()
{
disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
mComposerMap = 0;
}
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposerlegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class QgsSymbol;
class QgsSymbolV2;
class QgsComposerGroupItem;
class QgsComposerLayerItem;
class QgsComposerMap;

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

void setComposerMap( const QgsComposerMap* map );
const QgsComposerMap* composerMap() const { return mComposerMap; }

/**Updates the model and all legend entries*/
void updateLegend();

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

protected:
QString mTitle;
Expand All @@ -129,6 +135,9 @@ class CORE_EXPORT QgsComposerLegend: public QgsComposerItem

QgsLegendModel mLegendModel;

/**Reference to map (because symbols are sometimes in map units)*/
const QgsComposerMap* mComposerMap;


private:
QgsComposerLegend(); //forbidden
Expand Down
5 changes: 3 additions & 2 deletions src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
bool drawCanvasItems() const { return mDrawCanvasItems; }

/**Returns the conversion factor map units -> mm*/
double mapUnitsToMM() const;

signals:
void extentChanged();

Expand Down Expand Up @@ -392,8 +395,6 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void mapPolygon( QPolygonF& poly ) const;
/**Calculates the extent to request and the yShift of the top-left point in case of rotation.*/
void requestedExtent( QgsRectangle& extent ) const;
/**Returns the conversion factor map units -> mm*/
double mapUnitsToMM() const;
/**Scales a composer map shift (in MM) and rotates it by mRotation
@param xShift in: shift in x direction (in item units), out: xShift in map units
@param yShift in: shift in y direction (in item units), out: yShift in map units*/
Expand Down
8 changes: 7 additions & 1 deletion src/gui/qgscomposerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ void QgsComposerView::addComposerMap( QgsComposerMap* map )

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

void QgsComposerView::addComposerLegend( QgsComposerLegend* legend )
{
//take first available map
QList<const QgsComposerMap*> mapItemList = composition()->composerMapItems();
if ( mapItemList.size() > 0 )
{
legend->setComposerMap( mapItemList.at( 0 ) );
}
scene()->addItem( legend );
emit composerLegendAdded( legend );
scene()->clearSelection();
Expand Down
14 changes: 12 additions & 2 deletions src/ui/qgscomposerlegendwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<item row="0" column="0">
<widget class="QToolBox" name="toolBox">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="page">
<property name="geometry">
Expand Down Expand Up @@ -163,7 +163,7 @@
</property>
</widget>
</item>
<item row="12" column="0">
<item row="14" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -176,6 +176,16 @@
</property>
</spacer>
</item>
<item row="13" column="0">
<widget class="QComboBox" name="mMapComboBox"/>
</item>
<item row="12" column="0">
<widget class="QLabel" name="mMapLabel">
<property name="text">
<string>Map</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
Expand Down

0 comments on commit 576b05c

Please sign in to comment.