void QgsSingleBandPseudoColorRendererWidget::on_mSortButton_clicked() { bool inserted = false; int myCurrentIndex = 0; int myTopLevelItemCount = mColormapTreeWidget->topLevelItemCount(); QTreeWidgetItem* myCurrentItem; QList<QgsColorRampShader::ColorRampItem> myColorRampItems; for ( int i = 0; i < myTopLevelItemCount; ++i ) { myCurrentItem = mColormapTreeWidget->topLevelItem( i ); //If the item is null or does not have a pixel values set, skip if ( !myCurrentItem || myCurrentItem->text( 0 ) == "" ) { continue; } //Create a copy of the new Color ramp Item QgsColorRampShader::ColorRampItem myNewColorRampItem; myNewColorRampItem.value = myCurrentItem->text( 0 ).toDouble(); myNewColorRampItem.color = myCurrentItem->background( 1 ).color(); myNewColorRampItem.label = myCurrentItem->text( 2 ); //Simple insertion sort - speed is not a huge factor here inserted = false; myCurrentIndex = 0; while ( !inserted ) { if ( 0 == myColorRampItems.size() || myCurrentIndex == myColorRampItems.size() ) { myColorRampItems.push_back( myNewColorRampItem ); inserted = true; } else if ( myColorRampItems[myCurrentIndex].value > myNewColorRampItem.value ) { myColorRampItems.insert( myCurrentIndex, myNewColorRampItem ); inserted = true; } else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myCurrentIndex == myColorRampItems.size() - 1 ) { myColorRampItems.push_back( myNewColorRampItem ); inserted = true; } else if ( myColorRampItems[myCurrentIndex].value <= myNewColorRampItem.value && myColorRampItems[myCurrentIndex+1].value > myNewColorRampItem.value ) { myColorRampItems.insert( myCurrentIndex + 1, myNewColorRampItem ); inserted = true; } myCurrentIndex++; } } populateColormapTreeWidget( myColorRampItems ); }
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_mLoadFromBandButton_clicked() { if ( !mRasterLayer || !mRasterLayer->dataProvider() ) { return; } int bandIndex = mBandComboBox->itemData( mBandComboBox->currentIndex() ).toInt(); QList<QgsColorRampShader::ColorRampItem> colorRampList = mRasterLayer->dataProvider()->colorTable( bandIndex ); if ( !colorRampList.isEmpty() ) { populateColormapTreeWidget( colorRampList ); mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findData( QgsColorRampShader::INTERPOLATED ) ); } else { QMessageBox::warning( this, tr( "Load Color Map" ), tr( "The color map for band %1 has no entries" ).arg( bandIndex ) ); } emit widgetChanged(); }
void QgsSingleBandPseudoColorRendererWidget::on_mLoadFromFileButton_clicked() { int lineCounter = 0; bool importError = false; QString badLines; QSettings settings; QString lastDir = settings.value( "lastColorMapDir", QDir::homePath() ).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->findData( QgsColorRampShader::INTERPOLATED ) ); } else if ( inputStringComponents[1].trimmed().toUpper().compare( "DISCRETE", Qt::CaseInsensitive ) == 0 ) { mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findData( QgsColorRampShader::DISCRETE ) ); } else { mColorInterpolationComboBox->setCurrentIndex( mColorInterpolationComboBox->findData( QgsColorRampShader::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 ); QFileInfo fileInfo( fileName ); settings.setValue( "lastColorMapDir", fileInfo.absoluteDir().absolutePath() ); 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" ) ); } emit widgetChanged(); }