Skip to content
Permalink
Browse files

[FEATURE] labels support in QgsPalettedRasterRenderer, fixes #8886

  • Loading branch information
blazek committed Nov 2, 2013
1 parent 47b055f commit 24bcec43d5faf7bc8578577a31de66d071997fa4
@@ -5,7 +5,7 @@ class QgsPalettedRasterRenderer : QgsRasterRenderer
%End
public:
/**Renderer owns color array*/
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QColor* colorArray /Array,Transfer/, int nColors /ArraySize/ );
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QColor* colorArray /Array,Transfer/, int nColors /ArraySize/, const QVector<QString> labels = QVector<QString>() );
~QgsPalettedRasterRenderer();
QgsRasterInterface * clone() const /Factory/;
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterInterface* input ) /Factory/;
@@ -774,6 +774,11 @@ void QgsProjectFileTransform::convertRasterProperties( QDomDocument& doc, QDomNo
green = colorRampEntryElem.attribute( "green" ).toInt();
blue = colorRampEntryElem.attribute( "blue" ).toInt();
newPaletteElem.setAttribute( "color", QColor( red, green, blue ).name() );
QString label = colorRampEntryElem.attribute( "label" );
if ( !label.isEmpty() )
{
newPaletteElem.setAttribute( "label", label );
}
newColorPaletteElem.appendChild( newPaletteElem );
}
rasterRendererElem.appendChild( newColorPaletteElem );
@@ -22,10 +22,11 @@
#include <QDomDocument>
#include <QDomElement>
#include <QImage>
#include <QVector>

QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber,
QColor* colorArray, int nColors ):
QgsRasterRenderer( input, "paletted" ), mBand( bandNumber ), mNColors( nColors )
QColor* colorArray, int nColors, const QVector<QString> labels ):
QgsRasterRenderer( input, "paletted" ), mBand( bandNumber ), mNColors( nColors ), mLabels( labels )
{
mColors = new QRgb[nColors];
for ( int i = 0; i < nColors; ++i )
@@ -35,8 +36,8 @@ QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterInterface* input,
delete[] colorArray;
}

QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QRgb* colorArray, int nColors ):
QgsRasterRenderer( input, "paletted" ), mBand( bandNumber ), mColors( colorArray ), mNColors( nColors )
QgsPalettedRasterRenderer::QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QRgb* colorArray, int nColors, const QVector<QString> labels ):
QgsRasterRenderer( input, "paletted" ), mBand( bandNumber ), mColors( colorArray ), mNColors( nColors ), mLabels( labels )
{
}

@@ -51,6 +52,7 @@ QgsRasterInterface * QgsPalettedRasterRenderer::clone() const
renderer->setOpacity( mOpacity );
renderer->setAlphaBand( mAlphaBand );
renderer->setRasterTransparency( mRasterTransparency );
renderer->mLabels = mLabels;
return renderer;
}

@@ -64,6 +66,7 @@ QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, Q
int bandNumber = elem.attribute( "band", "-1" ).toInt();
int nColors = 0;
QRgb* colors = 0;
QVector<QString> labels;

QDomElement paletteElem = elem.firstChildElement( "colorPalette" );
if ( !paletteElem.isNull() )
@@ -94,14 +97,20 @@ QgsRasterRenderer* QgsPalettedRasterRenderer::create( const QDomElement& elem, Q
if ( value >= 0 && value < nColors )
{
colors[value] = QColor( entryElem.attribute( "color", "#000000" ) ).rgba();
QString label = entryElem.attribute( "label" );
if ( !label.isEmpty() )
{
if ( value >= labels.size() ) labels.resize( value + 1 );
labels[value] = label;
}
}
else
{
QgsDebugMsg( QString( "value %1 out of range" ).arg( value ) );
}
}
}
QgsRasterRenderer* r = new QgsPalettedRasterRenderer( input, bandNumber, colors, nColors );
QgsPalettedRasterRenderer* r = new QgsPalettedRasterRenderer( input, bandNumber, colors, nColors, labels );
r->readXML( elem );
return r;
}
@@ -134,6 +143,15 @@ QRgb* QgsPalettedRasterRenderer::rgbArray() const
return rgbValues;
}

void QgsPalettedRasterRenderer::setLabel( int idx, QString label )
{
if ( idx >= mLabels.size() )
{
mLabels.resize( idx + 1 );
}
mLabels[idx] = label;
}

QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height )
{
QgsRasterBlock *outputBlock = new QgsRasterBlock();
@@ -240,6 +258,10 @@ void QgsPalettedRasterRenderer::writeXML( QDomDocument& doc, QDomElement& parent
QDomElement colorElem = doc.createElement( "paletteEntry" );
colorElem.setAttribute( "value", i );
colorElem.setAttribute( "color", QColor( mColors[i] ).name() );
if ( !label( i ).isEmpty() )
{
colorElem.setAttribute( "label", label( i ) );
}
colorPaletteElem.appendChild( colorElem );
}
rasterRendererElem.appendChild( colorPaletteElem );
@@ -251,7 +273,8 @@ void QgsPalettedRasterRenderer::legendSymbologyItems( QList< QPair< QString, QCo
{
for ( int i = 0; i < mNColors; ++i )
{
symbolItems.push_back( qMakePair( QString::number( i ), QColor( mColors[i] ) ) );
QString lab = label( i ).isEmpty() ? QString::number( i ) : label( i );
symbolItems.push_back( qMakePair( lab, QColor( mColors[i] ) ) );
}
}

@@ -18,6 +18,8 @@
#ifndef QGSPALETTEDRASTERRENDERER_H
#define QGSPALETTEDRASTERRENDERER_H

#include <QVector>

#include "qgsrasterrenderer.h"

class QColor;
@@ -30,8 +32,8 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
{
public:
/**Renderer owns color array*/
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QColor* colorArray, int nColors );
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QRgb* colorArray, int nColors );
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QColor* colorArray, int nColors, const QVector<QString> labels = QVector<QString>() );
QgsPalettedRasterRenderer( QgsRasterInterface* input, int bandNumber, QRgb* colorArray, int nColors, const QVector<QString> labels = QVector<QString>() );
~QgsPalettedRasterRenderer();
QgsRasterInterface * clone() const;
static QgsRasterRenderer* create( const QDomElement& elem, QgsRasterInterface* input );
@@ -48,6 +50,14 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
*/
QRgb* rgbArray() const;

/** Return optional category label
* @note added in 2.1 */
QString label( int idx ) const { return mLabels.value( idx ); }

/** Set category label
* @note added in 2.1 */
void setLabel( int idx, QString label );

void writeXML( QDomDocument& doc, QDomElement& parentElem ) const;

void legendSymbologyItems( QList< QPair< QString, QColor > >& symbolItems ) const;
@@ -60,6 +70,8 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
QRgb* mColors;
/**Number of colors*/
int mNColors;
/** Optional category labels, size of vector may be < mNColors */
QVector<QString> mLabels;
};

#endif // QGSPALETTEDRASTERRENDERER_H
@@ -52,24 +52,36 @@ QgsRasterRenderer* QgsPalettedRendererWidget::renderer()
{
int nColors = mTreeWidget->topLevelItemCount();
QColor* colorArray = new QColor[nColors];
QVector<QString> labels;
for ( int i = 0; i < nColors; ++i )
{
colorArray[i] = mTreeWidget->topLevelItem( i )->background( 1 ).color();
QString label = mTreeWidget->topLevelItem( i )->text( 2 );
if ( !label.isEmpty() )
{
if ( i >= labels.size() ) labels.resize( i + 1 );
labels[i] = label;
}
}
int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
return new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, colorArray, nColors );
return new QgsPalettedRasterRenderer( mRasterLayer->dataProvider(), bandNumber, colorArray, nColors, labels );
}

void QgsPalettedRendererWidget::on_mTreeWidget_itemDoubleClicked( QTreeWidgetItem * item, int column )
{
if ( column == 1 && item ) //change item color
{
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
QColor c = QColorDialog::getColor( item->background( column ).color() );
if ( c.isValid() )
{
item->setBackground( column, QBrush( c ) );
}
}
else if ( column == 2 && item )
{
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
}
}

void QgsPalettedRendererWidget::setFromRenderer( const QgsRasterRenderer* r )
@@ -85,6 +97,7 @@ void QgsPalettedRendererWidget::setFromRenderer( const QgsRasterRenderer* r )
QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
item->setText( 0, QString::number( i ) );
item->setBackground( 1, QBrush( colors[i] ) );
item->setText( 2, pr->label( i ) );
}
delete[] colors;
}
@@ -102,6 +115,7 @@ void QgsPalettedRendererWidget::setFromRenderer( const QgsRasterRenderer* r )
QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
item->setText( 0, QString::number( index ) );
item->setBackground( 1, QBrush( itemIt->color ) );
item->setText( 2, itemIt->label );
++index;
}
}
@@ -42,6 +42,9 @@
<height>280</height>
</size>
</property>
<property name="columnCount">
<number>3</number>
</property>
<column>
<property name="text">
<string>Value</string>
@@ -52,6 +55,11 @@
<string>Color</string>
</property>
</column>
<column>
<property name="text">
<string>Label</string>
</property>
</column>
</widget>
</item>
</layout>

0 comments on commit 24bcec4

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