31 , mRasterLayer( rasterLayer )
33 Q_ASSERT( mRasterLayer );
34 Q_ASSERT( mRasterLayer->canCreateRasterAttributeTable() );
42 connect( mNativeRadioButton, &QRadioButton::toggled,
this, &QgsCreateRasterAttributeTableDialog::updateButtons );
43 connect( mDbfRadioButton, &QRadioButton::toggled,
this, &QgsCreateRasterAttributeTableDialog::updateButtons );
47 QStringList existingRatsInfo;
48 if ( mRasterLayer->attributeTableCount() > 0 )
50 for (
int bandNo = 1; bandNo <= mRasterLayer->bandCount(); ++bandNo )
57 nativeRatSupported =
false;
58 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." ) );
60 if ( !rat->filePath().isEmpty() )
62 existingRatsInfo.push_back( tr(
"Raster band %1 already has an associated attribute table at %2." ).arg( QString::number( bandNo ), rat->filePath() ) );
66 existingRatsInfo.push_back( tr(
"Raster band %1 already has an associated attribute table." ).arg( bandNo ) );
72 if ( !existingRatsInfo.isEmpty() )
74 mCreateInfoLabel->setText( mCreateInfoLabel->text().append( u
"<br><ul><li>"_s + existingRatsInfo.join(
"</li><li>"_L1 ) ).append( u
"</ul>"_s ) );
75 mCreateInfoLabel->adjustSize();
76 mCreateInfoLabel->show();
79 if ( !nativeRatSupported )
81 mNativeRadioButton->setEnabled(
false );
82 mDbfRadioButton->setChecked(
true );
86 mDbfPathWidget->setFilter( u
"VAT DBF Files (*.vat.dbf)"_s );
87 if ( QFile::exists( mRasterLayer->dataProvider()->dataSourceUri() ) )
89 mDbfPathWidget->setFilePath( mRasterLayer->dataProvider()->dataSourceUri() +
".vat.dbf" );
93 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
94 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
133 QString errorMessage;
134 int bandNumber { 0 };
136 bool success {
false };
140 notify( tr(
"Error Creating Raster Attribute Table" ), tr(
"The raster attribute table could not be created." ),
Qgis::MessageLevel::Critical );
144 mRasterLayer->dataProvider()->setAttributeTable( bandNumber, rat );
150 const QString destinationPath {
filePath() };
151 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 )
153 success = rat->
writeToFile( destinationPath, &errorMessage );
157 mRasterLayer->dataProvider()->setAttributeTable( bandNumber,
nullptr );
163 success = mRasterLayer->dataProvider()->writeNativeAttributeTable( &errorMessage );
167 mRasterLayer->dataProvider()->setAttributeTable( bandNumber,
nullptr );
174 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.