Skip to content

Commit a4f4f05

Browse files
committedFeb 23, 2012
Load / save functions for single band pseudocolor
1 parent 3c3b77b commit a4f4f05

File tree

3 files changed

+227
-7
lines changed

3 files changed

+227
-7
lines changed
 

‎src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
#include "qgssinglebandpseudocolorrendererwidget.h"
1919
#include "qgssinglebandpseudocolorrenderer.h"
2020
#include "qgsrasterlayer.h"
21+
#include <QColorDialog>
22+
#include <QFileDialog>
23+
#include <QMessageBox>
24+
#include <QSettings>
25+
#include <QTextStream>
2126

2227
QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer ):
2328
QgsRasterRendererWidget( layer )
@@ -199,3 +204,208 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
199204
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
200205
}
201206
}
207+
208+
void QgsSingleBandPseudoColorRendererWidget::populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems )
209+
{
210+
mColormapTreeWidget->clear();
211+
QList<QgsColorRampShader::ColorRampItem>::const_iterator it = colorRampItems.constBegin();
212+
for ( ; it != colorRampItems.constEnd(); ++it )
213+
{
214+
QTreeWidgetItem* newItem = new QTreeWidgetItem( mColormapTreeWidget );
215+
newItem->setText( 0, QString::number( it->value, 'f' ) );
216+
newItem->setBackground( 1, QBrush( it->color ) );
217+
newItem->setText( 2, it->label );
218+
}
219+
}
220+
221+
void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromBandButton_clicked()
222+
{
223+
if ( !mRasterLayer )
224+
{
225+
return;
226+
}
227+
228+
QList<QgsColorRampShader::ColorRampItem> colorRampList;
229+
int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt();
230+
231+
if ( mRasterLayer->readColorTable( bandIndex, &colorRampList ) )
232+
{
233+
populateColormapTreeWidget( colorRampList );
234+
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
235+
}
236+
else
237+
{
238+
QMessageBox::warning( this, tr( "Load Color Map" ), tr( "The color map for band %1 failed to load" ).arg( bandIndex ) );
239+
}
240+
}
241+
242+
void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromFileButton_clicked()
243+
{
244+
int lineCounter = 0;
245+
bool importError = false;
246+
QString badLines;
247+
QSettings settings;
248+
QString lastDir = settings.value( "lastRasterFileFilterDir", "" ).toString();
249+
QString fileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), lastDir, tr( "Textfile (*.txt)" ) );
250+
QFile inputFile( fileName );
251+
if ( inputFile.open( QFile::ReadOnly ) )
252+
{
253+
//clear the current tree
254+
mColormapTreeWidget->clear();
255+
256+
QTextStream inputStream( &inputFile );
257+
QString inputLine;
258+
QStringList inputStringComponents;
259+
QList<QgsColorRampShader::ColorRampItem> colorRampItems;
260+
261+
//read through the input looking for valid data
262+
while ( !inputStream.atEnd() )
263+
{
264+
lineCounter++;
265+
inputLine = inputStream.readLine();
266+
if ( !inputLine.isEmpty() )
267+
{
268+
if ( !inputLine.simplified().startsWith( "#" ) )
269+
{
270+
if ( inputLine.contains( "INTERPOLATION", Qt::CaseInsensitive ) )
271+
{
272+
inputStringComponents = inputLine.split( ":" );
273+
if ( inputStringComponents.size() == 2 )
274+
{
275+
if ( inputStringComponents[1].trimmed().toUpper().compare( "INTERPOLATED", Qt::CaseInsensitive ) == 0 )
276+
{
277+
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Linear" ) ) );
278+
}
279+
else if ( inputStringComponents[1].trimmed().toUpper().compare( "DISCRETE", Qt::CaseInsensitive ) == 0 )
280+
{
281+
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Discrete" ) ) );
282+
}
283+
else
284+
{
285+
mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findText( tr( "Exact" ) ) );
286+
}
287+
}
288+
else
289+
{
290+
importError = true;
291+
badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
292+
}
293+
}
294+
else
295+
{
296+
inputStringComponents = inputLine.split( "," );
297+
if ( inputStringComponents.size() == 6 )
298+
{
299+
QgsColorRampShader::ColorRampItem currentItem( inputStringComponents[0].toDouble(),
300+
QColor::fromRgb( inputStringComponents[1].toInt(), inputStringComponents[2].toInt(),
301+
inputStringComponents[3].toInt(), inputStringComponents[4].toInt() ),
302+
inputStringComponents[5] );
303+
colorRampItems.push_back( currentItem );
304+
}
305+
else
306+
{
307+
importError = true;
308+
badLines = badLines + QString::number( lineCounter ) + ":\t[" + inputLine + "]\n";
309+
}
310+
}
311+
}
312+
}
313+
lineCounter++;
314+
}
315+
populateColormapTreeWidget( colorRampItems );
316+
317+
if ( importError )
318+
{
319+
QMessageBox::warning( this, tr( "Import Error" ), tr( "The following lines contained errors\n\n" ) + badLines );
320+
}
321+
}
322+
else if ( !fileName.isEmpty() )
323+
{
324+
QMessageBox::warning( this, tr( "Read access denied" ), tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
325+
}
326+
}
327+
328+
void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
329+
{
330+
QSettings settings;
331+
QString lastDir = settings.value( "lastRasterFileFilterDir", "" ).toString();
332+
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), lastDir, tr( "Textfile (*.txt)" ) );
333+
if ( !fileName.isEmpty() )
334+
{
335+
if ( !fileName.endsWith( ".txt", Qt::CaseInsensitive ) )
336+
{
337+
fileName = fileName + ".txt";
338+
}
339+
340+
QFile outputFile( fileName );
341+
if ( outputFile.open( QFile::WriteOnly ) )
342+
{
343+
QTextStream outputStream( &outputFile );
344+
outputStream << "# " << tr( "QGIS Generated Color Map Export File" ) << "\n";
345+
outputStream << "INTERPOLATION:";
346+
if ( mColorInterpolationComboBox->currentText() == tr( "Linear" ) )
347+
{
348+
outputStream << "INTERPOLATED\n";
349+
}
350+
else if ( mColorInterpolationComboBox->currentText() == tr( "Discrete" ) )
351+
{
352+
outputStream << "DISCRETE\n";
353+
}
354+
else
355+
{
356+
outputStream << "EXACT\n";
357+
}
358+
359+
int topLevelItemCount = mColormapTreeWidget->topLevelItemCount();
360+
QTreeWidgetItem* currentItem;
361+
QColor color;
362+
for ( int i = 0; i < topLevelItemCount; ++i )
363+
{
364+
currentItem = mColormapTreeWidget->topLevelItem( i );
365+
if ( !currentItem )
366+
{
367+
continue;
368+
}
369+
color = currentItem->background( 1 ).color();
370+
outputStream << currentItem->text( 0 ).toDouble() << ",";
371+
outputStream << color.red() << "," << color.green() << "," << color.blue() << "," << color.alpha() << ",";
372+
if ( currentItem->text( 2 ) == "" )
373+
{
374+
outputStream << "Color entry " << i + 1 << "\n";
375+
}
376+
else
377+
{
378+
outputStream << currentItem->text( 2 ) << "\n";
379+
}
380+
}
381+
outputStream.flush();
382+
outputFile.close();
383+
}
384+
else
385+
{
386+
QMessageBox::warning( this, tr( "Write access denied" ), tr( "Write access denied. Adjust the file permissions and try again.\n\n" ) );
387+
}
388+
}
389+
}
390+
391+
void QgsSingleBandPseudoColorRendererWidget::on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column )
392+
{
393+
if ( !item )
394+
{
395+
return;
396+
}
397+
398+
if ( column == 1 ) //change item color
399+
{
400+
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
401+
QColor newColor = QColorDialog::getColor( item->background( column ).color() );
402+
if ( newColor.isValid() )
403+
{
404+
item->setBackground( 1, QBrush( newColor ) );
405+
}
406+
}
407+
else
408+
{
409+
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
410+
}
411+
}

‎src/gui/raster/qgssinglebandpseudocolorrendererwidget.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define QGSSINGLEBANDCOLORRENDERERWIDGET_H
2020

2121
#include "qgsrasterrendererwidget.h"
22+
#include "qgscolorrampshader.h"
2223
#include "ui_qgssinglebandpseudocolorrendererwidgetbase.h"
2324

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

36+
private:
37+
void populateColormapTreeWidget( const QList<QgsColorRampShader::ColorRampItem>& colorRampItems );
38+
3539
private slots:
3640
void on_mClassifyButton_clicked();
41+
void on_mLoadFromBandButton_clicked();
42+
void on_mLoadFromFileButton_clicked();
43+
void on_mExportToFileButton_clicked();
44+
void on_mColormapTreeWidget_itemDoubleClicked( QTreeWidgetItem* item, int column );
3745
};
3846

3947
#endif // QGSSINGLEBANDCOLORRENDERERWIDGET_H

‎src/ui/qgssinglebandpseudocolorrendererwidgetbase.ui

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@
8383
<string>...</string>
8484
</property>
8585
<property name="icon">
86-
<iconset>
87-
<normaloff>../../images/themes/default/mActionNewAttribute.png</normaloff>../../images/themes/default/mActionNewAttribute.png</iconset>
86+
<iconset resource="../../images/images.qrc">
87+
<normaloff>:/images/themes/default/mActionNewAttribute.png</normaloff>:/images/themes/default/mActionNewAttribute.png</iconset>
8888
</property>
8989
</widget>
9090
</item>
@@ -97,8 +97,8 @@
9797
<string>...</string>
9898
</property>
9999
<property name="icon">
100-
<iconset>
101-
<normaloff>../../images/themes/default/mActionFolder.png</normaloff>../../images/themes/default/mActionFolder.png</iconset>
100+
<iconset resource="../../images/images.qrc">
101+
<normaloff>:/images/themes/default/mActionFileOpen.png</normaloff>:/images/themes/default/mActionFileOpen.png</iconset>
102102
</property>
103103
</widget>
104104
</item>
@@ -111,8 +111,8 @@
111111
<string>...</string>
112112
</property>
113113
<property name="icon">
114-
<iconset>
115-
<normaloff>../../images/themes/default/mActionFileSave.png</normaloff>../../images/themes/default/mActionFileSave.png</iconset>
114+
<iconset resource="../../images/images.qrc">
115+
<normaloff>:/images/themes/default/mActionFileSaveAs.png</normaloff>:/images/themes/default/mActionFileSaveAs.png</iconset>
116116
</property>
117117
</widget>
118118
</item>
@@ -185,6 +185,8 @@
185185
</item>
186186
</layout>
187187
</widget>
188-
<resources/>
188+
<resources>
189+
<include location="../../images/images.qrc"/>
190+
</resources>
189191
<connections/>
190192
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.