Skip to content
Permalink
Browse files
Started renderer dialog for singleband pseudocolor
  • Loading branch information
mhugent committed Feb 22, 2012
1 parent a14beda commit 7c7d36a67bdacaa187da66bba84a4698f8321dcf
@@ -42,6 +42,7 @@
#include "qgsrasterrendererregistry.h"
#include "qgsmultibandcolorrendererwidget.h"
#include "qgspalettedrendererwidget.h"
#include "qgssinglebandpseudocolorrendererwidget.h"

#include <QTableWidgetItem>
#include <QHeaderView>
@@ -337,6 +338,7 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
//insert renderer widgets into registry
QgsRasterRendererRegistry::instance()->insertWidgetFunction( "paletted", QgsPalettedRendererWidget::create );
QgsRasterRendererRegistry::instance()->insertWidgetFunction( "multibandcolor", QgsMultiBandColorRendererWidget::create );
QgsRasterRendererRegistry::instance()->insertWidgetFunction( "singlebandpseudocolor", QgsSingleBandPseudoColorRendererWidget::create );

//fill available renderers into combo box
QList< QgsRasterRendererRegistryEntry > rendererEntries = QgsRasterRendererRegistry::instance()->entries();
@@ -18,6 +18,19 @@
#include "qgsrasterrendererregistry.h"
#include "qgsmultibandcolorrenderer.h"
#include "qgspalettedrasterrenderer.h"
#include "qgssinglebandpseudocolorrenderer.h"

QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry( const QString& theName, const QString& theVisibleName,
QgsRasterRendererCreateFunc rendererFunction,
QgsRasterRendererWidgetCreateFunc widgetFunction ):
name( theName ), visibleName( theVisibleName ), rendererCreateFunction( rendererFunction ),
widgetCreateFunction( widgetFunction )
{
}

QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry(): rendererCreateFunction( 0 ), widgetCreateFunction( 0 )
{
}

QgsRasterRendererRegistry* QgsRasterRendererRegistry::mInstance = 0;

@@ -32,6 +45,13 @@ QgsRasterRendererRegistry* QgsRasterRendererRegistry::instance()

QgsRasterRendererRegistry::QgsRasterRendererRegistry()
{
insert( QgsRasterRendererRegistryEntry( "paletted", QObject::tr( "Paletted" ), QgsPalettedRasterRenderer::create, 0 ) );
insert( QgsRasterRendererRegistryEntry( "multibandcolor", QObject::tr( "Multiband color" ),
QgsMultiBandColorRenderer::create, 0 ) );
insert( QgsRasterRendererRegistryEntry( "singlebandpseudocolor", QObject::tr( "Singleband pseudocolor" ),
QgsSingleBandPseudoColorRenderer::create, 0 ) );

#if 0
//add entry for palleted renderer
QgsRasterRendererRegistryEntry palettedEntry;
palettedEntry.name = "paletted";
@@ -47,6 +67,7 @@ QgsRasterRendererRegistry::QgsRasterRendererRegistry()
palettedEntry.rendererCreateFunction = QgsMultiBandColorRenderer::create;
multiBandColorEntry.widgetCreateFunction = 0;
insert( multiBandColorEntry );
#endif //0
}

QgsRasterRendererRegistry::~QgsRasterRendererRegistry()
@@ -32,6 +32,9 @@ typedef QgsRasterRendererWidget*( *QgsRasterRendererWidgetCreateFunc )( QgsRaste

struct QgsRasterRendererRegistryEntry
{
QgsRasterRendererRegistryEntry( const QString& theName, const QString& theVisibleName, QgsRasterRendererCreateFunc rendererFunction,
QgsRasterRendererWidgetCreateFunc widgetFunction );
QgsRasterRendererRegistryEntry();
QString name;
QString visibleName; //visible (and translatable) name
QgsRasterRendererCreateFunc rendererCreateFunction; //pointer to create function
@@ -28,6 +28,13 @@ QgsSingleBandPseudoColorRenderer::QgsSingleBandPseudoColorRenderer( QgsRasterDat

QgsSingleBandPseudoColorRenderer::~QgsSingleBandPseudoColorRenderer()
{
delete mShader;
}

void QgsSingleBandPseudoColorRenderer::setShader( QgsRasterShader* shader )
{
delete mShader;
mShader = shader;
}

QgsRasterRenderer* QgsSingleBandPseudoColorRenderer::create( const QDomElement& elem )
@@ -26,13 +26,18 @@ class QgsRasterShader;
class QgsSingleBandPseudoColorRenderer: public QgsRasterRenderer
{
public:
/**Note: takes ownership of QgsRasterShader*/
QgsSingleBandPseudoColorRenderer( QgsRasterDataProvider* provider, int band, QgsRasterShader* shader );
~QgsSingleBandPseudoColorRenderer();

static QgsRasterRenderer* create( const QDomElement& elem );

virtual void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );

/**Takes ownership of the shader*/
void setShader( QgsRasterShader* shader );
const QgsRasterShader* shader;

private:
QgsRasterShader* mShader;
int mBand;
@@ -3,6 +3,7 @@ SET(QGIS_GUI_SRCS

raster/qgsmultibandcolorrendererwidget.cpp
raster/qgspalettedrendererwidget.cpp
raster/qgssinglebandpseudocolorrendererwidget.cpp

symbology-ng/qgsbrushstylecombobox.cpp
symbology-ng/qgscolorrampcombobox.cpp
@@ -85,6 +86,7 @@ SET(QGIS_GUI_MOC_HDRS

raster/qgspalettedrendererwidget.h
raster/qgsmultibandcolorrendererwidget.h
raster/qgssinglebandpseudocolorrendererwidget.h

symbology-ng/qgsdashspacedialog.h
symbology-ng/qgssymbollayerv2widget.h
@@ -0,0 +1,172 @@
/***************************************************************************
qgssinglebandpseudocolorrendererwidget.cpp
------------------------------------------
begin : February 2012
copyright : (C) 2012 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgssinglebandpseudocolorrendererwidget.h"
#include "qgssinglebandpseudocolorrenderer.h"
#include "qgsrasterlayer.h"

QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer ):
QgsRasterRendererWidget( layer )
{
setupUi( this );

if ( !mRasterLayer )
{
return;
}

QgsRasterDataProvider* provider = mRasterLayer->dataProvider();
if ( !provider )
{
return;
}

//fill available bands into combo box
int nBands = provider->bandCount();
for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
{
mBandComboBox->addItem( provider->colorInterpretationName( i ), i );
}

mColorInterpolationComboBox->addItem( tr( "Discrete" ), 0 );
mColorInterpolationComboBox->addItem( tr( "Linear" ), 1 );
mColorInterpolationComboBox->addItem( tr( "Exact" ), 2 );

mClassificationModeComboBox->addItem( tr( "Equal interval" ) );
//quantile would be nice as well

QgsSingleBandPseudoColorRenderer* r = dynamic_cast<QgsSingleBandPseudoColorRenderer*>( layer->renderer() );
if ( r )
{
//todo...
}
}

QgsSingleBandPseudoColorRendererWidget::~QgsSingleBandPseudoColorRendererWidget()
{
}

QgsRasterRenderer* QgsSingleBandPseudoColorRendererWidget::renderer()
{
QgsRasterShader* rasterShader = new QgsRasterShader();
QgsColorRampShader* colorRampShader = new QgsColorRampShader();

//iterate through mColormapTreeWidget and set colormap info of layer
QList<QgsColorRampShader::ColorRampItem> colorRampItems;
int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* currentItem;
for ( int i = 0; i < topLevelItemCount; ++i )
{
currentItem = mColormapTreeWidget->topLevelItem( i );
if ( !currentItem )
{
continue;
}
QgsColorRampShader::ColorRampItem newColorRampItem;
newColorRampItem.value = currentItem->text( 0 ).toDouble();
newColorRampItem.color = currentItem->background( 1 ).color();
newColorRampItem.label = currentItem->text( 2 );
colorRampItems.append( newColorRampItem );
}
// sort the shader items
qSort( colorRampItems );
colorRampShader->setColorRampItemList( colorRampItems );

if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
{
colorRampShader->setColorRampType( QgsColorRampShader::INTERPOLATED );
}
else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
{
colorRampShader->setColorRampType( QgsColorRampShader::DISCRETE );
}
else
{
colorRampShader->setColorRampType( QgsColorRampShader::EXACT );
}
rasterShader->setRasterShaderFunction( colorRampShader );

int bandNumber = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
return new QgsSingleBandPseudoColorRenderer( mRasterLayer->dataProvider(), bandNumber, rasterShader );
}

void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
{
int bandComboIndex = mBandComboBox->currentIndex();
if ( bandComboIndex == -1 )
{
return;
}

int bandNr = mBandComboBox->itemData( bandComboIndex ).toInt();
QgsRasterBandStats myRasterBandStats = mRasterLayer->bandStatistics( bandNr );
int numberOfEntries = mNumberOfEntriesSpinBox->value();

QList<double> entryValues;
QList<QColor> entryColors;

if ( mClassificationModeComboBox->currentText() == tr( "Equal interval" ) )
{
double currentValue = myRasterBandStats.minimumValue;
double intervalDiff;
if ( numberOfEntries > 1 )
{
//because the highest value is also an entry, there are (numberOfEntries - 1)
//intervals
intervalDiff = ( myRasterBandStats.maximumValue - myRasterBandStats.minimumValue ) /
( numberOfEntries - 1 );
}
else
{
intervalDiff = myRasterBandStats.maximumValue - myRasterBandStats.minimumValue;
}

for ( int i = 0; i < numberOfEntries; ++i )
{
entryValues.push_back( currentValue );
currentValue += intervalDiff;
}
}

//hard code color range from blue -> red for now. Allow choice of ramps in future
int colorDiff = 0;
if ( numberOfEntries != 0 )
{
colorDiff = ( int )( 255 / numberOfEntries );
}

for ( int i = 0; i < numberOfEntries; ++i )
{
QColor currentColor;
currentColor.setRgb( colorDiff*i, 0, 255 - colorDiff * i );
entryColors.push_back( currentColor );
}

mColormapTreeWidget->clear();

QList<double>::const_iterator value_it = entryValues.begin();
QList<QColor>::const_iterator color_it = entryColors.begin();

for ( ; value_it != entryValues.end(); ++value_it, ++color_it )
{
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( *value_it, 'f' ) );
newItem->setBackground( 1, QBrush( *color_it ) );
newItem->setText( 2, QString::number( *value_it, 'f' ) );
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
}
}
@@ -0,0 +1,39 @@
/***************************************************************************
qgssinglebandpseudocolorrendererwidget.h
----------------------------------------
begin : February 2012
copyright : (C) 2012 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSSINGLEBANDCOLORRENDERERWIDGET_H
#define QGSSINGLEBANDCOLORRENDERERWIDGET_H

#include "qgsrasterrendererwidget.h"
#include "ui_qgssinglebandpseudocolorrendererwidgetbase.h"

class QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendererWidget,
private Ui::QgsSingleBandPseudoColorRendererWidgetBase
{
Q_OBJECT
public:
QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer );
~QgsSingleBandPseudoColorRendererWidget();

static QgsRasterRendererWidget* create( QgsRasterLayer* layer ) { return new QgsSingleBandPseudoColorRendererWidget( layer ); }
QgsRasterRenderer* renderer();

private slots:
void on_mClassifyButton_clicked();
};

#endif // QGSSINGLEBANDCOLORRENDERERWIDGET_H

0 comments on commit 7c7d36a

Please sign in to comment.