Skip to content
Permalink
Browse files

Added managing of WMS legends (getLegendGraphic) in the addLegend of …

…QgsComposer.

Added parameters to interactively modify wms legends scale.
Add WMS getLegendGraphic draw in composer and add it's size parameters in composer window.

Developed with funding from Regione Toscana-SITA
  • Loading branch information
luipir committed Oct 18, 2013
1 parent 32da13b commit f6a6a0c017db6306691dca9c9008ef0100574d98
@@ -168,6 +168,8 @@ void QgsComposerLegendWidget::setGuiElements()
mEqualColumnWidthCheckBox->setChecked( mLegend->equalColumnWidth() );
mSymbolWidthSpinBox->setValue( mLegend->symbolWidth() );
mSymbolHeightSpinBox->setValue( mLegend->symbolHeight() );
mWmsLegendWidthSpinBox->setValue( mLegend->wmsLegendWidth() );
mWmsLegendHeightSpinBox->setValue( mLegend->wmsLegendHeight() );
mTitleSpaceBottomSpinBox->setValue( mLegend->style( QgsComposerLegendStyle::Title ).margin( QgsComposerLegendStyle::Bottom ) );
mGroupSpaceSpinBox->setValue( mLegend->style( QgsComposerLegendStyle::Group ).margin( QgsComposerLegendStyle::Top ) );
mLayerSpaceSpinBox->setValue( mLegend->style( QgsComposerLegendStyle::Subgroup ).margin( QgsComposerLegendStyle::Top ) );
@@ -280,6 +282,30 @@ void QgsComposerLegendWidget::on_mSymbolHeightSpinBox_valueChanged( double d )
}
}

void QgsComposerLegendWidget::on_mWmsLegendWidthSpinBox_valueChanged( double d )
{
if ( mLegend )
{
mLegend->beginCommand( tr( "Wms Legend width" ), QgsComposerMergeCommand::LegendWmsLegendWidth );
mLegend->setWmsLegendWidth( d );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
}
}

void QgsComposerLegendWidget::on_mWmsLegendHeightSpinBox_valueChanged( double d )
{
if ( mLegend )
{
mLegend->beginCommand( tr( "Wms Legend height" ), QgsComposerMergeCommand::LegendWmsLegendHeight );
mLegend->setWmsLegendHeight( d );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
}
}

void QgsComposerLegendWidget::on_mTitleSpaceBottomSpinBox_valueChanged( double d )
{
if ( mLegend )
@@ -59,6 +59,8 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
void on_mEqualColumnWidthCheckBox_toggled( bool checked );
void on_mSymbolWidthSpinBox_valueChanged( double d );
void on_mSymbolHeightSpinBox_valueChanged( double d );
void on_mWmsLegendWidthSpinBox_valueChanged( double d );
void on_mWmsLegendHeightSpinBox_valueChanged( double d );
void on_mTitleSpaceBottomSpinBox_valueChanged( double d );
void on_mGroupSpaceSpinBox_valueChanged( double d );
void on_mLayerSpaceSpinBox_valueChanged( double d );
@@ -224,7 +224,7 @@ void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
#if QT_VERSION >= 0x40700
if ( rasterItemList.size() == 0) itemList.reserve( 1 );
#endif
itemList.append( qMakePair( QString(""), legendGraphic ) );
itemList.append( qMakePair( QString(""), QPixmap::fromImage(legendGraphic) ) );
}
}

@@ -85,6 +85,8 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
LegendEqualColumnWidth,
LegendSymbolWidth,
LegendSymbolHeight,
LegendWmsLegendWidth,
LegendWmsLegendHeight,
LegendTitleSpaceBottom,
LegendGroupSpace,
LegendLayerSpace,
@@ -54,6 +54,8 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )

mSymbolWidth = 7;
mSymbolHeight = 4;
mWmsLegendWidth = 50;
mWmsLegendHeight = 25;
mWrapChar = "";
mlineSpacing = 1.5;
adjustBoxSize();
@@ -347,13 +349,41 @@ QgsComposerLegend::Nucleon QgsComposerLegend::drawSymbolItem( QgsComposerLegendI
}
else if ( rasterItem )
{
if ( painter )
// manage WMS lengendGraphic
// actual code recognise if it's a legend because it has an icon and it's text is empty => this is not good MV pattern implementation :(
QIcon symbolIcon = symbolItem->icon();
if ( !symbolIcon.isNull() && symbolItem->text().isEmpty() )
{
painter->setBrush( rasterItem->color() );
painter->drawRect( QRectF( point.x(), point.y() + ( itemHeight - mSymbolHeight ) / 2, mSymbolWidth, mSymbolHeight ) );
// find max size
QList<QSize> sizes = symbolIcon.availableSizes();
double maxWidth = 0;
double maxHeight = 0;
foreach ( QSize size, sizes )
{
if ( maxWidth < size.width() ) maxWidth = size.width();
if ( maxHeight < size.height() ) maxHeight = size.height();
}
QSize maxSize(maxWidth, maxHeight);

// get and print legend
QImage legend = symbolIcon.pixmap(maxWidth, maxHeight).toImage();
if ( painter )
{
painter->drawImage( QRectF( point.x(), point.y(), mWmsLegendWidth, mWmsLegendHeight ), legend, QRectF( 0, 0, maxWidth, maxHeight ) );
}
symbolSize.rwidth() = mWmsLegendWidth;
symbolSize.rheight() = mWmsLegendHeight;
}
else
{
if ( painter )
{
painter->setBrush( rasterItem->color() );
painter->drawRect( QRectF( point.x(), point.y() + ( itemHeight - mSymbolHeight ) / 2, mSymbolWidth, mSymbolHeight ) );
}
symbolSize.rwidth() = mSymbolWidth;
symbolSize.rheight() = mSymbolHeight;
}
symbolSize.rwidth() = mSymbolWidth;
symbolSize.rheight() = mSymbolHeight;
}
else //item with icon?
{
@@ -555,6 +585,8 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const

composerLegendElem.setAttribute( "symbolWidth", QString::number( mSymbolWidth ) );
composerLegendElem.setAttribute( "symbolHeight", QString::number( mSymbolHeight ) );
composerLegendElem.setAttribute( "wmsLegendWidth", QString::number( mWmsLegendWidth ) );
composerLegendElem.setAttribute( "wmsLegendHeight", QString::number( mWmsLegendHeight ) );
composerLegendElem.setAttribute( "wrapChar", mWrapChar );
composerLegendElem.setAttribute( "fontColor", mFontColor.name() );

@@ -622,6 +654,8 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument

mSymbolWidth = itemElem.attribute( "symbolWidth", "7.0" ).toDouble();
mSymbolHeight = itemElem.attribute( "symbolHeight", "14.0" ).toDouble();
mWmsLegendWidth = itemElem.attribute( "wmsLegendWidth", "50" ).toDouble();
mWmsLegendHeight = itemElem.attribute( "wmsLegendHeight", "25" ).toDouble();

mWrapChar = itemElem.attribute( "wrapChar" );

@@ -87,6 +87,12 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
double symbolHeight() const {return mSymbolHeight;}
void setSymbolHeight( double h ) {mSymbolHeight = h;}

double wmsLegendWidth() const {return mWmsLegendWidth;}
void setWmsLegendWidth( double w ) {mWmsLegendWidth = w;}

double wmsLegendHeight() const {return mWmsLegendHeight;}
void setWmsLegendHeight( double h ) {mWmsLegendHeight = h;}

void setWrapChar( const QString& t ) {mWrapChar = t;}
QString wrapChar() const {return mWrapChar;}

@@ -139,6 +145,11 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
/**Height of symbol icon*/
double mSymbolHeight;

/**Width of WMS legendGraphic pixmap*/
double mWmsLegendWidth;
/**Height of WMS legendGraphic pixmap*/
double mWmsLegendHeight;

/** Spacing between lines when wrapped */
double mlineSpacing;

@@ -225,37 +225,69 @@ int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer*
return 2;
}

QList< QPair< QString, QColor > > rasterItemList = rasterLayer->legendSymbologyItems();
QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
int row = 0;
for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
{
QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( itemIt->first );

QgsComposerRasterSymbolItem* oldSymbolItem = dynamic_cast<QgsComposerRasterSymbolItem*>( layerItem->child( row, 0 ) );
if ( oldSymbolItem )
{
currentSymbolItem->setUserText( oldSymbolItem->userText() );
currentSymbolItem->setText( currentSymbolItem->userText() );
QgsDebugMsg( QString( "layer providertype:: %1" ).arg( rasterLayer->providerType() ) );
if ( rasterLayer->providerType() == "wms" )
{
QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( "" );
// GetLegendGraphics in case of WMS service... image can return null if GetLegendGraphics
// is not supported by server
// double currentScale = legend()->canvas()->scale();
// BEAWARE getLegendGraphic() COULD BE USED WITHOUT SCALE PARAMETER IF IT WAS ALREADY CALLED WITH
// THIS PARAMER FROM A COMPONENT THAT CAN RECOVER CURRENT SCALE => LEGEND IN THE DESKTOP
// OTHERWISE IT RETURN A INVALID PIXMAP (QPixmap().isNull() == False)
QImage legendGraphic = rasterLayer->dataProvider()->getLegendGraphic();
if ( !legendGraphic.isNull() )
{
QgsDebugMsg( QString( "downloaded legend with dimension Width:" )+QString::number(legendGraphic.width())+QString(" and Height:")+QString::number(legendGraphic.height()) );
if ( mHasTopLevelWindow )
{
currentSymbolItem->setIcon( QIcon( QPixmap::fromImage(legendGraphic) ) );
}
}

if ( mHasTopLevelWindow )
else
{
QPixmap itemPixmap( 20, 20 );
itemPixmap.fill( itemIt->second );
currentSymbolItem->setIcon( QIcon( itemPixmap ) );
currentSymbolItem->setText(tr("No Legend Available"));
}

currentSymbolItem->setLayerID( rasterLayer->id() );
currentSymbolItem->setColor( itemIt->second );
int currentRowCount = layerItem->rowCount();
layerItem->setChild( currentRowCount, 0, currentSymbolItem );
row++;
currentSymbolItem->setColor( QColor() );
layerItem->removeRows(0, layerItem->rowCount());
layerItem->setChild( layerItem->rowCount(), 0, currentSymbolItem );
}

// Delete following old items (if current number of items decreased)
for ( int i = layerItem->rowCount() - 1; i >= row; --i )
else
{
layerItem->removeRow( i );
QList< QPair< QString, QColor > > rasterItemList = rasterLayer->legendSymbologyItems();
QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
int row = 0;
for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
{
QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( itemIt->first );

QgsComposerRasterSymbolItem* oldSymbolItem = dynamic_cast<QgsComposerRasterSymbolItem*>( layerItem->child( row, 0 ) );
if ( oldSymbolItem )
{
currentSymbolItem->setUserText( oldSymbolItem->userText() );
currentSymbolItem->setText( currentSymbolItem->userText() );
}

if ( mHasTopLevelWindow )
{
QPixmap itemPixmap( 20, 20 );
itemPixmap.fill( itemIt->second );
currentSymbolItem->setIcon( QIcon( itemPixmap ) );
}
currentSymbolItem->setLayerID( rasterLayer->id() );
currentSymbolItem->setColor( itemIt->second );
int currentRowCount = layerItem->rowCount();
layerItem->setChild( currentRowCount, 0, currentSymbolItem );
row++;
}

// Delete following old items (if current number of items decreased)
for ( int i = layerItem->rowCount() - 1; i >= row; --i )
{
layerItem->removeRow( i );
}
}

return 0;
@@ -54,9 +54,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-141</y>
<y>-309</y>
<width>373</width>
<height>1141</height>
<height>1387</height>
</rect>
</property>
<layout class="QVBoxLayout" name="mainLayout">
@@ -196,9 +196,6 @@
<attribute name="headerVisible">
<bool>true</bool>
</attribute>
<attribute name="headerVisible">
<bool>true</bool>
</attribute>
</widget>
</item>
<item>
@@ -540,6 +537,73 @@
</layout>
</widget>
</item>
<item>
<widget class="QgsCollapsibleGroupBoxBasic" name="mSymbolsColGroupBox_2">
<property name="title">
<string>WMS LegendGraphic</string>
</property>
<property name="syncGroup" stdset="0">
<string notr="true">composeritem</string>
</property>
<property name="collapsed" stdset="0">
<bool>true</bool>
</property>
<layout class="QFormLayout" name="formLayout_5">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_13">
<property name="text">
<string>Legend width</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="mWmsLegendWidthSpinBox">
<property name="prefix">
<string/>
</property>
<property name="suffix">
<string> mm</string>
</property>
<property name="maximum">
<double>9999.000000000000000</double>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_14">
<property name="text">
<string>Legend height</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="mWmsLegendHeightSpinBox">
<property name="prefix">
<string/>
</property>
<property name="suffix">
<string> mm</string>
</property>
<property name="maximum">
<double>9999.000000000000000</double>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QgsCollapsibleGroupBoxBasic" name="mSpacingColGroupBox">
<property name="title">

0 comments on commit f6a6a0c

Please sign in to comment.
You can’t perform that action at this time.