Skip to content
Permalink
Browse files

show symbols from rendererV2 in legend

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11115 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Jul 20, 2009
1 parent 2b082ed commit dc046b5271a72aa92c7e811fcc30da325d3ffe3c
Showing with 88 additions and 1 deletion.
  1. +84 −1 src/app/legend/qgslegendlayer.cpp
  2. +2 −0 src/app/legend/qgslegendlayer.h
  3. +2 −0 src/app/qgisapp.cpp
@@ -36,6 +36,9 @@
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"

#include "qgsrendererv2.h"
#include "qgssymbolv2.h"

#include <iostream>
#include <QAction>
#include <QCoreApplication>
@@ -271,7 +274,10 @@ void QgsLegendLayer::refreshSymbology( const QString& key, double widthScale )
if ( theMapLayer->type() == QgsMapLayer::VectorLayer ) // VECTOR
{
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( theMapLayer );
vectorLayerSymbology( vlayer, widthScale ); // get and change symbology
if ( vlayer->isUsingRendererV2() )
vectorLayerSymbologyV2( vlayer );
else
vectorLayerSymbology( vlayer, widthScale ); // get and change symbology
}
else // RASTER
{
@@ -388,6 +394,83 @@ void QgsLegendLayer::vectorLayerSymbology( const QgsVectorLayer* layer, double w
changeSymbologySettings( layer, itemList );
}

static QPixmap _symbolPreviewPixmap(QgsSymbolV2* sym, QSize iconSize)
{
QPainter p;
QPixmap pix(iconSize);
pix.fill(Qt::white);
p.begin(&pix);
sym->drawPreviewIcon(&p, iconSize);
p.end();
return pix;
}

void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
{
SymbologyList itemList;

QSize iconSize(16,16);

QSettings settings;
bool showClassifiers = settings.value( "/qgis/showLegendClassifiers", false ).toBool();

QgsFeatureRendererV2* renderer = layer->rendererV2();
switch (renderer->type())
{
case QgsFeatureRendererV2::RendererSingleSymbol:
{
QgsSingleSymbolRendererV2* r = static_cast<QgsSingleSymbolRendererV2*>(renderer);
QPixmap pix = _symbolPreviewPixmap(r->symbol(), iconSize);

itemList.push_back( std::make_pair( "", pix ) );
}
break;
case QgsFeatureRendererV2::RendererCategorizedSymbol:
{
QgsCategorizedSymbolRendererV2* r = static_cast<QgsCategorizedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
const QgsFieldMap& fields = layer->dataProvider()->fields();
QString fieldName = fields[r->attributeIndex()].name();
itemList.push_back( std::make_pair( fieldName, QPixmap() ) );
}

int count = r->categories().count();
for (int i = 0; i < count; i++)
{
const QgsRendererCategoryV2& cat = r->categories()[i];
QPixmap pix = _symbolPreviewPixmap( cat.symbol(), iconSize );
itemList.push_back( std::make_pair( cat.label(), pix ) );
}
}
break;
case QgsFeatureRendererV2::RendererGraduatedSymbol:
{
QgsGraduatedSymbolRendererV2* r = static_cast<QgsGraduatedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
const QgsFieldMap& fields = layer->dataProvider()->fields();
QString fieldName = fields[r->attributeIndex()].name();
itemList.push_back( std::make_pair( fieldName, QPixmap() ) );
}

int count = r->ranges().count();
for (int i = 0; i < count; i++)
{
const QgsRendererRangeV2& range = r->ranges()[i];
QPixmap pix = _symbolPreviewPixmap( range.symbol(), iconSize );
itemList.push_back( std::make_pair( range.label(), pix ) );
}
}
break;
default:
// nothing for unknown renderers
break;
}

changeSymbologySettings( layer, itemList );
}

void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
{
SymbologyList itemList;
@@ -96,6 +96,8 @@ class QgsLegendLayer : public QgsLegendItem
/** Prepare and change symbology for vector layer */
void vectorLayerSymbology( const QgsVectorLayer* mapLayer, double widthScale = 1.0 );

void vectorLayerSymbologyV2( QgsVectorLayer* vlayer );

/** Prepare and change symbology for raster layer */
void rasterLayerSymbology( QgsRasterLayer* mapLayer );

@@ -1045,6 +1045,8 @@ void QgisApp::toggleRendererV2()
if (!dlg.exec())
return;

mMapLegend->refreshLayerSymbology( vlayer->getLayerID(), false );

refreshMapCanvas();
}

0 comments on commit dc046b5

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