Skip to content
Permalink
Browse files
Load / save functions for single band pseudocolor
  • Loading branch information
mhugent committed Feb 23, 2012
1 parent 3c3b77b commit a4f4f05f01ca412bf39f0b8a50537e674f827eb9
@@ -18,6 +18,11 @@
#include "qgssinglebandpseudocolorrendererwidget.h"
#include "qgssinglebandpseudocolorrenderer.h"
#include "qgsrasterlayer.h"
#include <QColorDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QTextStream>

QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer ):
QgsRasterRendererWidget( layer )
@@ -199,3 +204,208 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
}
}

void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems )
{
mColormapTreeWidget->clear();
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItems.constBegin();
for ( ; it != colorRampItems.constEnd(); ++it )
{
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
newItem->setText( 0, QString::number( it->value, 'f' ) );
newItem->setBackground( 1, QBrush( it->color ) );
newItem->setText( 2, it->label );
}
}

void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
{
if ( !mRasterLayer )
{
return;
}

QList<QgsColorRampShader::ColorRampItem> colorRampList;
int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();

if ( mRasterLayer->readColorTable( bandIndex, &colorRampList ) )
{
populateColormapTreeWidget( colorRampList );
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
}
else
{
QMessageBox::warning( this, tr( "Load Color Map" ), tr( "The color map for band %1 failed to load" ).arg( bandIndex ) );
}
}

void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromFileButton_clicked()
{
int lineCounter = 0;
bool importError = false;
QString badLines;
QSettings settings;
QString lastDir = settings.value( "lastRasterFileFilterDir", "" ).toString();
QString fileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), lastDir, tr( "Textfile (*.txt)" ) );
QFile inputFile( fileName );
if ( inputFile.open( QFile::ReadOnly ) )
{
//clear the current tree
mColormapTreeWidget->clear();

QTextStream inputStream( &inputFile );
QString inputLine;
QStringList inputStringComponents;
QList<QgsColorRampShader::ColorRampItem> colorRampItems;

//read through the input looking for valid data
while ( !inputStream.atEnd() )
{
lineCounter++;
inputLine = inputStream.readLine();
if ( !inputLine.isEmpty() )
{
if ( !inputLine.simplified().startsWith( "#" ) )
{
if ( inputLine.contains( "INTERPOLATION", Qt::CaseInsensitive ) )
{
inputStringComponents = inputLine.split( ":" );
if ( inputStringComponents.size() == 2 )
{
if ( inputStringComponents[1].trimmed().toUpper().compare( "INTERPOLATED", Qt::CaseInsensitive ) == 0 )
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
}
else if ( inputStringComponents[1].trimmed().toUpper().compare( "DISCRETE", Qt::CaseInsensitive ) == 0 )
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
}
else
{
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
}
}
else
{
importError = true;
badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
}
}
else
{
inputStringComponents = inputLine.split( "," );
if ( inputStringComponents.size() == 6 )
{
QgsColorRampShader::ColorRampItem currentItem( inputStringComponents[0].toDouble(),
QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(),
inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ),
inputStringComponents[5] );
colorRampItems.push_back( currentItem );
}
else
{
importError = true;
badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
}
}
}
}
lineCounter++;
}
populateColormapTreeWidget( colorRampItems );

if ( importError )
{
QMessageBox::warning( this, tr( "Import Error" ), tr( "The following lines contained errors\n\n" ) + badLines );
}
}
else if ( !fileName.isEmpty() )
{
QMessageBox::warning( this, tr( "Read access denied" ), tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
}
}

void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
{
QSettings settings;
QString lastDir = settings.value( "lastRasterFileFilterDir", "" ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), lastDir, tr( "Textfile (*.txt)" ) );
if ( !fileName.isEmpty() )
{
if ( !fileName.endsWith( ".txt", Qt::CaseInsensitive ) )
{
fileName = fileName + ".txt";
}

QFile outputFile( fileName );
if ( outputFile.open( QFile::WriteOnly ) )
{
QTextStream outputStream( &outputFile );
outputStream << "# " << tr( "QGIS Generated Color Map Export File" ) << "\n";
outputStream << "INTERPOLATION:";
if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
{
outputStream << "INTERPOLATED\n";
}
else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
{
outputStream << "DISCRETE\n";
}
else
{
outputStream << "EXACT\n";
}

int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
QTreeWidgetItem* currentItem;
QColor color;
for ( int i = 0; i < topLevelItemCount; ++i )
{
currentItem = mColormapTreeWidget->topLevelItem( i );
if ( !currentItem )
{
continue;
}
color = currentItem->background( 1 ).color();
outputStream << currentItem->text( 0 ).toDouble() << ",";
outputStream << color.red() << "," << color.green() << "," << color.blue() << "," << color.alpha() << ",";
if ( currentItem->text( 2 ) == "" )
{
outputStream << "Color entry " << i + 1 << "\n";
}
else
{
outputStream << currentItem->text( 2 ) << "\n";
}
}
outputStream.flush();
outputFile.close();
}
else
{
QMessageBox::warning( this, tr( "Write access denied" ), tr( "Write access denied. Adjust the file permissions and try again.\n\n" ) );
}
}
}

void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column )
{
if ( !item )
{
return;
}

if ( column == 1 ) //change item color
{
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
QColor newColor = QColorDialog::getColor( item->background( column ).color() );
if ( newColor.isValid() )
{
item->setBackground( 1, QBrush( newColor ) );
}
}
else
{
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
}
}
@@ -19,6 +19,7 @@
#define QGSSINGLEBANDCOLORRENDERERWIDGET_H

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

class QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendererWidget,
@@ -32,8 +33,15 @@ class QgsSingleBandPseudoColorRendererWidget: public QgsRasterRendererWidget,
static QgsRasterRendererWidget* create( QgsRasterLayer* layer ) { return new QgsSingleBandPseudoColorRendererWidget( layer ); }
QgsRasterRenderer* renderer();

private:
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );

private slots:
void on_mClassifyButton_clicked();
void on_mLoadFromBandButton_clicked();
void on_mLoadFromFileButton_clicked();
void on_mExportToFileButton_clicked();
void on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column );
};

#endif // QGSSINGLEBANDCOLORRENDERERWIDGET_H
@@ -83,8 +83,8 @@
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>../../images/themes/default/mActionNewAttribute.png</normaloff>../../images/themes/default/mActionNewAttribute.png</iconset>
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionNewAttribute.png</normaloff>:/images/themes/default/mActionNewAttribute.png</iconset>
</property>
</widget>
</item>
@@ -97,8 +97,8 @@
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>../../images/themes/default/mActionFolder.png</normaloff>../../images/themes/default/mActionFolder.png</iconset>
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionFileOpen.png</normaloff>:/images/themes/default/mActionFileOpen.png</iconset>
</property>
</widget>
</item>
@@ -111,8 +111,8 @@
<string>...</string>
</property>
<property name="icon">
<iconset>
<normaloff>../../images/themes/default/mActionFileSave.png</normaloff>../../images/themes/default/mActionFileSave.png</iconset>
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionFileSaveAs.png</normaloff>:/images/themes/default/mActionFileSaveAs.png</iconset>
</property>
</widget>
</item>
@@ -185,6 +185,8 @@
</item>
</layout>
</widget>
<resources/>
<resources>
<include location="../../images/images.qrc"/>
</resources>
<connections/>
</ui>

0 comments on commit a4f4f05

Please sign in to comment.