Skip to content

Commit 48002db

Browse files
committed
added wms getLegendGraphic management in case of rasterLayerSymbology - 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)
1 parent d770843 commit 48002db

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/app/legend/qgslegend.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
113113

114114
connect( mMapCanvas, SIGNAL( layersChanged() ),
115115
this, SLOT( refreshCheckStates() ) );
116+
connect( mMapCanvas, SIGNAL( extentsChanged() ),
117+
this, SLOT( updateLegendItemSymbologies() ) );
116118

117119
// Initialise the line indicator widget.
118120
mInsertionLine = new QWidget( viewport() );
@@ -141,6 +143,7 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
141143
QgsLegend::~QgsLegend()
142144
{
143145
delete mInsertionLine;
146+
delete mGetLegendGraphicPopup;
144147
}
145148

146149
#ifdef QGISDEBUG
@@ -395,6 +398,24 @@ void QgsLegend::mousePressEvent( QMouseEvent * e )
395398
{
396399
if ( e->button() == Qt::LeftButton )
397400
{
401+
// show WMS legend in case itemAt( e->pos() ) is a wms legend
402+
// if it's not a legend later it return a null pixmap
403+
QImage legend = getWmsLegendPixmap( itemAt( e->pos() ) );
404+
if ( !legend.isNull() )
405+
{
406+
mGetLegendGraphicPopup = new QFrame();
407+
mGetLegendGraphicPopup->setFrameStyle(QFrame::Box | QFrame::Raised);
408+
mGetLegendGraphicPopup->setLineWidth(2);
409+
mGetLegendGraphicPopup->setAutoFillBackground(true);
410+
QVBoxLayout *layout = new QVBoxLayout;
411+
QLabel *label = new QLabel(mGetLegendGraphicPopup);
412+
label->setPixmap( QPixmap::fromImage(legend) );
413+
layout->addWidget(label);
414+
mGetLegendGraphicPopup->setLayout(layout);
415+
mGetLegendGraphicPopup->move(e->globalX(), e->globalY());
416+
mGetLegendGraphicPopup->show();
417+
}
418+
398419
mMousePressedFlag = true;
399420
mDropTarget = itemAt( e->pos() );
400421
if ( !mDropTarget )
@@ -624,6 +645,11 @@ void QgsLegend::updateGroupCheckStates( QTreeWidgetItem *item )
624645

625646
void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
626647
{
648+
if (mGetLegendGraphicPopup) {
649+
delete mGetLegendGraphicPopup;
650+
mGetLegendGraphicPopup = 0;
651+
}
652+
627653
QStringList layersPriorToEvent = layerIDs();
628654
QTreeWidget::mouseReleaseEvent( e );
629655
mMousePressedFlag = false;
@@ -3208,3 +3234,43 @@ void QgsLegend::updateLegendItemSymbologies()
32083234
ll->refreshSymbology( ll->layer()->id() );
32093235
}
32103236
}
3237+
3238+
QImage QgsLegend::getWmsLegendPixmap( QTreeWidgetItem *item )
3239+
{
3240+
if ( !item )
3241+
{
3242+
return QImage();
3243+
}
3244+
3245+
QTreeWidgetItem *parent = item->parent();
3246+
if ( !parent )
3247+
{
3248+
return QImage();
3249+
}
3250+
3251+
QgsLegendItem* li = dynamic_cast<QgsLegendItem *>( parent );
3252+
if ( !li )
3253+
{
3254+
return QImage();
3255+
}
3256+
3257+
if ( li->type() != QgsLegendItem::LEGEND_LAYER )
3258+
{
3259+
return QImage();
3260+
}
3261+
3262+
QgsLegendLayer *lyr = qobject_cast<QgsLegendLayer*>( li );
3263+
QgsRasterLayer *rasterLayer = dynamic_cast<QgsRasterLayer*>( lyr->layer() );
3264+
if ( !rasterLayer )
3265+
{
3266+
return QImage();
3267+
}
3268+
3269+
if ( rasterLayer->providerType() != "wms" )
3270+
{
3271+
return QImage();
3272+
}
3273+
3274+
return rasterLayer->dataProvider()->getLegendGraphic( canvas()->scale() );
3275+
}
3276+

src/app/legend/qgslegend.h

+10
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,16 @@ class QgsLegend : public QTreeWidget
590590

591591
bool verifyDrawingOrder();
592592

593+
/*!
594+
* Check if current LegendItem belogs to a WMS layer
595+
* @param item LegendItem to check if belongs to a WMS layer
596+
* @return QImage A valid Legend image if belogs to WMS otherwise QImage()
597+
*/
598+
QImage getWmsLegendPixmap( QTreeWidgetItem *item );
599+
600+
//! popup QFrame containing WMS getLegendGraphic pixmap
601+
QFrame *mGetLegendGraphicPopup;
602+
593603
signals:
594604
void itemAdded( QModelIndex index );
595605
void itemMoved( QModelIndex oldIndex, QModelIndex newIndex );

src/app/legend/qgslegendlayer.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,26 @@ void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
208208
#if QT_VERSION >= 0x40700
209209
itemList.reserve( rasterItemList.size() );
210210
#endif
211+
212+
// GetLegendGraphics in case of WMS service... pixmap can return null if GetLegendGraphics
213+
// is not supported by server
214+
QgsDebugMsg( QString( "layer providertype:: %1" ).arg( layer->providerType() ) );
215+
if ( layer->providerType() == "wms" )
216+
{
217+
double currentScale = legend()->canvas()->scale();
218+
219+
QImage legendGraphic = layer->dataProvider()->getLegendGraphic( currentScale );
220+
if ( !legendGraphic.isNull() )
221+
{
222+
QgsDebugMsg( QString( "downloaded legend with dimension Width:" )+QString::number(legendGraphic.width())+QString(" and Height:")+QString::number(legendGraphic.height()) );
223+
224+
#if QT_VERSION >= 0x40700
225+
if ( rasterItemList.size() == 0) itemList.reserve( 1 );
226+
#endif
227+
itemList.append( qMakePair( QString(""), legendGraphic ) );
228+
}
229+
}
230+
211231
// Paletted raster may have many colors, for example UInt16 may have 65536 colors
212232
// and it is very slow, so we limit max count
213233
QSize iconSize = treeWidget()->iconSize();

0 commit comments

Comments
 (0)