|
18 | 18 | #include "qgssinglebandpseudocolorrendererwidget.h"
|
19 | 19 | #include "qgssinglebandpseudocolorrenderer.h"
|
20 | 20 | #include "qgsrasterlayer.h"
|
| 21 | +#include <QColorDialog> |
| 22 | +#include <QFileDialog> |
| 23 | +#include <QMessageBox> |
| 24 | +#include <QSettings> |
| 25 | +#include <QTextStream> |
21 | 26 |
|
22 | 27 | QgsSingleBandPseudoColorRendererWidget::QgsSingleBandPseudoColorRendererWidget( QgsRasterLayer* layer ):
|
23 | 28 | QgsRasterRendererWidget( layer )
|
@@ -199,3 +204,208 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
|
199 | 204 | newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable );
|
200 | 205 | }
|
201 | 206 | }
|
| 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 | +} |
0 commit comments