Skip to content

Commit

Permalink
added wms getLegendGraphic management in case of rasterLayerSymbology…
Browse files Browse the repository at this point in the history
… - Developed with funding from Regione Toscana-SITA

This commit add the getLegendGraphic pixmap as legend in the QgsLegend.
Due to architectural QgsLegend limitation pixmap is shown of the same dimension of every single legend element (too little)

managing of getLegendGraphic popup and refresh - Developed with funding from Regione Toscana-SITA
Due the fact that WMS pixmap is too little, has been added a popup to show WMS legend in the original size
Has been added feature to refresh getLegendGraphics to every scale change due to extend modification. In this way
getLagendGraphic can be updated basing on scale and in the future also basing on viewport.

Modified QPixmap to QImage to be device independent

set absolute position due to popup is not referred to legendInterface (avoid hiding of the legend)
  • Loading branch information
luipir committed Oct 18, 2013
1 parent d770843 commit 48002db
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -113,6 +113,8 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )

connect( mMapCanvas, SIGNAL( layersChanged() ),
this, SLOT( refreshCheckStates() ) );
connect( mMapCanvas, SIGNAL( extentsChanged() ),
this, SLOT( updateLegendItemSymbologies() ) );

// Initialise the line indicator widget.
mInsertionLine = new QWidget( viewport() );
Expand Down Expand Up @@ -141,6 +143,7 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
QgsLegend::~QgsLegend()
{
delete mInsertionLine;
delete mGetLegendGraphicPopup;
}

#ifdef QGISDEBUG
Expand Down Expand Up @@ -395,6 +398,24 @@ void QgsLegend::mousePressEvent( QMouseEvent * e )
{
if ( e->button() == Qt::LeftButton )
{
// show WMS legend in case itemAt( e->pos() ) is a wms legend
// if it's not a legend later it return a null pixmap
QImage legend = getWmsLegendPixmap( itemAt( e->pos() ) );
if ( !legend.isNull() )
{
mGetLegendGraphicPopup = new QFrame();
mGetLegendGraphicPopup->setFrameStyle(QFrame::Box | QFrame::Raised);
mGetLegendGraphicPopup->setLineWidth(2);
mGetLegendGraphicPopup->setAutoFillBackground(true);
QVBoxLayout *layout = new QVBoxLayout;
QLabel *label = new QLabel(mGetLegendGraphicPopup);
label->setPixmap( QPixmap::fromImage(legend) );
layout->addWidget(label);
mGetLegendGraphicPopup->setLayout(layout);
mGetLegendGraphicPopup->move(e->globalX(), e->globalY());
mGetLegendGraphicPopup->show();
}

mMousePressedFlag = true;
mDropTarget = itemAt( e->pos() );
if ( !mDropTarget )
Expand Down Expand Up @@ -624,6 +645,11 @@ void QgsLegend::updateGroupCheckStates( QTreeWidgetItem *item )

void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
{
if (mGetLegendGraphicPopup) {
delete mGetLegendGraphicPopup;
mGetLegendGraphicPopup = 0;
}

QStringList layersPriorToEvent = layerIDs();
QTreeWidget::mouseReleaseEvent( e );
mMousePressedFlag = false;
Expand Down Expand Up @@ -3208,3 +3234,43 @@ void QgsLegend::updateLegendItemSymbologies()
ll->refreshSymbology( ll->layer()->id() );
}
}

QImage QgsLegend::getWmsLegendPixmap( QTreeWidgetItem *item )
{
if ( !item )
{
return QImage();
}

QTreeWidgetItem *parent = item->parent();
if ( !parent )
{
return QImage();
}

QgsLegendItem* li = dynamic_cast<QgsLegendItem *>( parent );
if ( !li )
{
return QImage();
}

if ( li->type() != QgsLegendItem::LEGEND_LAYER )
{
return QImage();
}

QgsLegendLayer *lyr = qobject_cast<QgsLegendLayer*>( li );
QgsRasterLayer *rasterLayer = dynamic_cast<QgsRasterLayer*>( lyr->layer() );
if ( !rasterLayer )
{
return QImage();
}

if ( rasterLayer->providerType() != "wms" )
{
return QImage();
}

return rasterLayer->dataProvider()->getLegendGraphic( canvas()->scale() );
}

10 changes: 10 additions & 0 deletions src/app/legend/qgslegend.h
Expand Up @@ -590,6 +590,16 @@ class QgsLegend : public QTreeWidget

bool verifyDrawingOrder();

/*!
* Check if current LegendItem belogs to a WMS layer
* @param item LegendItem to check if belongs to a WMS layer
* @return QImage A valid Legend image if belogs to WMS otherwise QImage()
*/
QImage getWmsLegendPixmap( QTreeWidgetItem *item );

//! popup QFrame containing WMS getLegendGraphic pixmap
QFrame *mGetLegendGraphicPopup;

signals:
void itemAdded( QModelIndex index );
void itemMoved( QModelIndex oldIndex, QModelIndex newIndex );
Expand Down
20 changes: 20 additions & 0 deletions src/app/legend/qgslegendlayer.cpp
Expand Up @@ -208,6 +208,26 @@ void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
#if QT_VERSION >= 0x40700
itemList.reserve( rasterItemList.size() );
#endif

// GetLegendGraphics in case of WMS service... pixmap can return null if GetLegendGraphics
// is not supported by server
QgsDebugMsg( QString( "layer providertype:: %1" ).arg( layer->providerType() ) );
if ( layer->providerType() == "wms" )
{
double currentScale = legend()->canvas()->scale();

QImage legendGraphic = layer->dataProvider()->getLegendGraphic( currentScale );
if ( !legendGraphic.isNull() )
{
QgsDebugMsg( QString( "downloaded legend with dimension Width:" )+QString::number(legendGraphic.width())+QString(" and Height:")+QString::number(legendGraphic.height()) );

#if QT_VERSION >= 0x40700
if ( rasterItemList.size() == 0) itemList.reserve( 1 );
#endif
itemList.append( qMakePair( QString(""), legendGraphic ) );
}
}

// Paletted raster may have many colors, for example UInt16 may have 65536 colors
// and it is very slow, so we limit max count
QSize iconSize = treeWidget()->iconSize();
Expand Down

0 comments on commit 48002db

Please sign in to comment.