28 , mRasterLayer( rasterLayer )
30 Q_ASSERT( mRasterLayer );
31 Q_ASSERT( mRasterLayer->canCreateRasterAttributeTable() );
39 connect( mNativeRadioButton, &QRadioButton::toggled,
this, &QgsCreateRasterAttributeTableDialog::updateButtons );
40 connect( mDbfRadioButton, &QRadioButton::toggled,
this, &QgsCreateRasterAttributeTableDialog::updateButtons );
44 QStringList existingRatsInfo;
45 if ( mRasterLayer->attributeTableCount() > 0 )
47 for (
int bandNo = 1; bandNo <= mRasterLayer->bandCount(); ++bandNo )
54 nativeRatSupported =
false;
55 existingRatsInfo.push_back( tr(
"The data provider supports attribute table storage but some drivers do not support 'thematic' types, for this reason the option is disabled." ) );
57 if ( !rat->filePath().isEmpty() )
59 existingRatsInfo.push_back( tr(
"Raster band %1 already has an associated attribute table at %2." ).arg( QString::number( bandNo ), rat->filePath() ) );
63 existingRatsInfo.push_back( tr(
"Raster band %1 already has an associated attribute table." ).arg( bandNo ) );
69 if ( !existingRatsInfo.isEmpty() )
71 mCreateInfoLabel->setText( mCreateInfoLabel->text().append( QStringLiteral(
"<br><ul><li>" ) + existingRatsInfo.join( QLatin1String(
"</li><li>" ) ) ).append( QStringLiteral(
"</ul>" ) ) );
72 mCreateInfoLabel->adjustSize();
73 mCreateInfoLabel->show();
76 if ( !nativeRatSupported )
78 mNativeRadioButton->setEnabled(
false );
79 mDbfRadioButton->setChecked(
true );
83 mDbfPathWidget->setFilter( QStringLiteral(
"VAT DBF Files (*.vat.dbf)" ) );
84 if ( QFile::exists( mRasterLayer->dataProvider()->dataSourceUri() ) )
86 mDbfPathWidget->setFilePath( mRasterLayer->dataProvider()->dataSourceUri() +
".vat.dbf" );
90 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
91 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
130 QString errorMessage;
131 int bandNumber { 0 };
133 bool success {
false };
137 notify( tr(
"Error Creating Raster Attribute Table" ), tr(
"The raster attribute table could not be created." ),
Qgis::MessageLevel::Critical );
141 mRasterLayer->dataProvider()->setAttributeTable( bandNumber, rat );
147 const QString destinationPath {
filePath() };
148 if ( !QFile::exists( destinationPath ) || QMessageBox::warning(
nullptr, tr(
"Confirm Overwrite" ), tr(
"Are you sure you want to overwrite the existing attribute table at '%1'?" ).arg( destinationPath ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
150 success = rat->
writeToFile( destinationPath, &errorMessage );
154 mRasterLayer->dataProvider()->setAttributeTable( bandNumber,
nullptr );
160 success = mRasterLayer->dataProvider()->writeNativeAttributeTable( &errorMessage );
164 mRasterLayer->dataProvider()->setAttributeTable( bandNumber,
nullptr );
171 notify( tr(
"Raster Attribute Table Saved" ), tr(
"The new Raster Attribute Table was successfully created." ),
Qgis::MessageLevel::Success );
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.