Skip to content
Permalink
Browse files

generic solution for legend symbology items

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11934 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Nov 6, 2009
1 parent 0bda836 commit 2bd06b45d90e2be2dd4836ef5df86c0c560f2feb
@@ -1,6 +1,8 @@

typedef QList<QgsSymbolV2*> QgsSymbolV2List;

typedef QList< QPair<QString, QPixmap> > QgsLegendSymbologyList;

///////////////

/*
@@ -60,6 +62,15 @@ public:
bool usingSymbolLevels() const;
void setUsingSymbolLevels(bool usingSymbolLevels);

//! create a renderer from XML element
static QgsFeatureRendererV2* load(QDomElement& symbologyElem);

//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize);

protected:
QgsFeatureRendererV2(QString type);

@@ -93,6 +104,16 @@ public:
virtual QString dump();

virtual QgsFeatureRendererV2* clone() /Factory/;

//! create a renderer from XML element
static QgsFeatureRendererV2* load(QDomElement& symbologyElem) /Factory/;

//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize);

};

//////////
@@ -159,9 +180,24 @@ public:
bool deleteCategory(int catIndex);
void deleteAllCategories();

//! create a renderer from XML element
static QgsFeatureRendererV2* load(QDomElement& symbologyElem) /Factory/;

//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize);

QString classAttribute() const;
void setClassAttribute(QString attr);

QgsSymbolV2* sourceSymbol();
void setSourceSymbol(QgsSymbolV2* sym /Transfer/);

QgsVectorColorRampV2* sourceColorRamp();
void setSourceColorRamp(QgsVectorColorRampV2* ramp /Transfer/);

protected:

QgsSymbolV2* symbolForValue(QVariant value);
@@ -244,6 +280,21 @@ public:
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp);

//! create a renderer from XML element
static QgsFeatureRendererV2* load(QDomElement& symbologyElem) /Factory/;

//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize);

QgsSymbolV2* sourceSymbol();
void setSourceSymbol(QgsSymbolV2* sym /Transfer/);

QgsVectorColorRampV2* sourceColorRamp();
void setSourceColorRamp(QgsVectorColorRampV2* ramp /Transfer/);

protected:
QgsSymbolV2* symbolForValue(double value);
};
@@ -369,7 +369,7 @@ void QgsLegendLayer::vectorLayerSymbology( const QgsVectorLayer* layer, double w
}

QPixmap pix = QPixmap::fromImage( img ); // convert to pixmap
itemList.push_back( std::make_pair( values, pix ) );
itemList.append( qMakePair( values, pix ) );
}


@@ -388,80 +388,23 @@ void QgsLegendLayer::vectorLayerSymbology( const QgsVectorLayer* layer, double w
{
classfieldname = fields[*it].name();
}
itemList.push_front( std::make_pair( classfieldname, QPixmap() ) );
itemList.append( qMakePair( classfieldname, QPixmap() ) );
}
}
}

changeSymbologySettings( layer, itemList );
}

static QPixmap _symbolPreviewPixmap(QgsSymbolV2* sym, QSize iconSize)
{
QPainter p;
QPixmap pix(iconSize);
pix.fill(Qt::white);
p.begin(&pix);
p.setRenderHint(QPainter::Antialiasing);
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();
QString rendererType = renderer->type();
if (rendererType == "singleSymbol")
{
QgsSingleSymbolRendererV2* r = static_cast<QgsSingleSymbolRendererV2*>(renderer);
QPixmap pix = _symbolPreviewPixmap(r->symbol(), iconSize);

itemList.push_back( std::make_pair( "", pix ) );
}
else if (rendererType == "categorizedSymbol")
{
QgsCategorizedSymbolRendererV2* r = static_cast<QgsCategorizedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
itemList.push_back( std::make_pair( r->classAttribute(), 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 ) );
}
}
else if (rendererType == "graduatedSymbol")
{
QgsGraduatedSymbolRendererV2* r = static_cast<QgsGraduatedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
itemList.push_back( std::make_pair( r->classAttribute(), 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 ) );
}
}
else
{
// nothing for unknown renderers
}
SymbologyList itemList = layer->rendererV2()->legendSymbologyItems(iconSize);

changeSymbologySettings( layer, itemList );
}
@@ -470,7 +413,7 @@ void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
{
SymbologyList itemList;
QPixmap legendpixmap = layer->legendAsPixmap( true ).scaled( 20, 20, Qt::KeepAspectRatio );
itemList.push_back( std::make_pair( "", legendpixmap ) );
itemList.append( qMakePair( QString(), legendpixmap ) );

changeSymbologySettings( layer, itemList );

@@ -33,7 +33,7 @@ class QgsVectorLayer;

class QTreeWidget;

typedef std::list< std::pair<QString, QPixmap> > SymbologyList;
typedef QList< QPair<QString, QPixmap> > SymbologyList;

/**
Container for layer, including layer file(s), symbology class breaks and properties
@@ -10,6 +10,7 @@

#include <QDomDocument>
#include <QDomElement>
#include <QSettings> // for legend

QgsRendererCategoryV2::QgsRendererCategoryV2(QVariant value, QgsSymbolV2* symbol, QString label)
: mValue(value), mSymbol(symbol), mLabel(label)
@@ -335,6 +336,28 @@ QDomElement QgsCategorizedSymbolRendererV2::save(QDomDocument& doc)
return rendererElem;
}

QgsLegendSymbologyList QgsCategorizedSymbolRendererV2::legendSymbologyItems(QSize iconSize)
{
QSettings settings;
bool showClassifiers = settings.value( "/qgis/showLegendClassifiers", false ).toBool();

QgsLegendSymbologyList lst;
if (showClassifiers)
{
lst << qMakePair( classAttribute(), QPixmap() );
}

int count = categories().count();
for (int i = 0; i < count; i++)
{
const QgsRendererCategoryV2& cat = categories()[i];
QPixmap pix = QgsSymbolLayerV2Utils::symbolPreviewPixmap( cat.symbol(), iconSize );
lst << qMakePair( cat.label(), pix );
}
return lst;
}


QgsSymbolV2* QgsCategorizedSymbolRendererV2::sourceSymbol()
{
return mSourceSymbol;
@@ -79,6 +79,9 @@ class QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize);

QgsSymbolV2* sourceSymbol();
void setSourceSymbol(QgsSymbolV2* sym);

@@ -10,6 +10,7 @@

#include <QDomDocument>
#include <QDomElement>
#include <QSettings> // for legend

QgsRendererRangeV2::QgsRendererRangeV2(double lowerValue, double upperValue, QgsSymbolV2* symbol, QString label)
: mLowerValue(lowerValue), mUpperValue(upperValue), mSymbol(symbol), mLabel(label)
@@ -432,6 +433,27 @@ QDomElement QgsGraduatedSymbolRendererV2::save(QDomDocument& doc)
return rendererElem;
}

QgsLegendSymbologyList QgsGraduatedSymbolRendererV2::legendSymbologyItems(QSize iconSize)
{
QSettings settings;
bool showClassifiers = settings.value( "/qgis/showLegendClassifiers", false ).toBool();

QgsLegendSymbologyList lst;
if (showClassifiers)
{
lst << qMakePair( classAttribute(), QPixmap() );
}

int count = ranges().count();
for (int i = 0; i < count; i++)
{
const QgsRendererRangeV2& range = ranges()[i];
QPixmap pix = QgsSymbolLayerV2Utils::symbolPreviewPixmap( range.symbol(), iconSize );
lst << qMakePair( range.label(), pix );
}
return lst;
}

QgsSymbolV2* QgsGraduatedSymbolRendererV2::sourceSymbol()
{
return mSourceSymbol;
@@ -87,6 +87,9 @@ class QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize);

QgsSymbolV2* sourceSymbol();
void setSourceSymbol(QgsSymbolV2* sym);

@@ -281,6 +281,13 @@ QDomElement QgsFeatureRendererV2::save(QDomDocument& doc)
return doc.createElement(RENDERER_TAG_NAME);
}

QgsLegendSymbologyList QgsFeatureRendererV2::legendSymbologyItems(QSize iconSize)
{
// empty list by default
return QgsLegendSymbologyList();
}


int QgsFeatureRendererV2::fieldNameIndex( const QgsFieldMap& fields, const QString& fieldName )
{
for ( QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); ++it )
@@ -8,6 +8,8 @@
#include <QList>
#include <QString>
#include <QVariant>
#include <QPair>
#include <QPixmap>

class QDomDocument;
class QDomElement;
@@ -19,6 +21,8 @@ class QgsFeature;
typedef QList<QgsSymbolV2*> QgsSymbolV2List;
typedef QMap<QString, QgsSymbolV2* > QgsSymbolV2Map;

typedef QList< QPair<QString, QPixmap> > QgsLegendSymbologyList;

#define RENDERER_TAG_NAME "renderer-v2"

////////
@@ -84,6 +88,9 @@ class QgsFeatureRendererV2

//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize);

/** Returns the index of a field name or -1 if the field does not exist
* copied from QgsVectorDataProvider... d'oh... probably should be elsewhere
@@ -100,3 +100,12 @@ QDomElement QgsSingleSymbolRendererV2::save(QDomDocument& doc)

return rendererElem;
}

QgsLegendSymbologyList QgsSingleSymbolRendererV2::legendSymbologyItems(QSize iconSize)
{
QPixmap pix = QgsSymbolLayerV2Utils::symbolPreviewPixmap(mSymbol, iconSize);

QgsLegendSymbologyList lst;
lst << qMakePair( QString(), pix );
return lst;
}
@@ -34,6 +34,9 @@ class QgsSingleSymbolRendererV2 : public QgsFeatureRendererV2
//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

//! return a list of symbology items for the legend
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize);

protected:
QgsSymbolV2* mSymbol;
};
@@ -145,6 +145,11 @@ QPointF QgsSymbolLayerV2Utils::decodePoint(QString str)
}

QIcon QgsSymbolLayerV2Utils::symbolPreviewIcon(QgsSymbolV2* symbol, QSize size)
{
return QIcon(symbolPreviewPixmap(symbol, size));
}

QPixmap QgsSymbolLayerV2Utils::symbolPreviewPixmap(QgsSymbolV2* symbol, QSize size)
{
QPixmap pixmap(size);
QPainter painter;
@@ -153,9 +158,10 @@ QIcon QgsSymbolLayerV2Utils::symbolPreviewIcon(QgsSymbolV2* symbol, QSize size)
painter.eraseRect(QRect(QPoint(0,0),size));
symbol->drawPreviewIcon(&painter, size);
painter.end();
return QIcon(pixmap);
return pixmap;
}


QIcon QgsSymbolLayerV2Utils::symbolLayerPreviewIcon(QgsSymbolLayerV2* layer, QSize size)
{
QPixmap pixmap(size);
@@ -47,6 +47,7 @@ class QgsSymbolLayerV2Utils
static QIcon symbolLayerPreviewIcon(QgsSymbolLayerV2* layer, QSize size);
static QIcon colorRampPreviewIcon(QgsVectorColorRampV2* ramp, QSize size);

static QPixmap symbolPreviewPixmap(QgsSymbolV2* symbol, QSize size);
static QPixmap colorRampPreviewPixmap(QgsVectorColorRampV2* ramp, QSize size);

static QgsSymbolV2* loadSymbol(QDomElement& element);

0 comments on commit 2bd06b4

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