32 , mRasterLayer( rasterLayer )
34 Q_ASSERT( mRasterLayer );
35 Q_ASSERT( mRasterLayer->canCreateRasterAttributeTable() );
43 connect( mNativeRadioButton, &QRadioButton::toggled,
this, &QgsCreateRasterAttributeTableDialog::updateButtons );
44 connect( mDbfRadioButton, &QRadioButton::toggled,
this, &QgsCreateRasterAttributeTableDialog::updateButtons );
48 QStringList existingRatsInfo;
49 if ( mRasterLayer->attributeTableCount() > 0 )
51 for (
int bandNo = 1; bandNo <= mRasterLayer->bandCount(); ++bandNo )
58 nativeRatSupported =
false;
59 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." ) );
61 if ( !rat->filePath().isEmpty() )
63 existingRatsInfo.push_back( tr(
"Raster band %1 already has an associated attribute table at %2." ).arg( QString::number( bandNo ), rat->filePath() ) );
67 existingRatsInfo.push_back( tr(
"Raster band %1 already has an associated attribute table." ).arg( bandNo ) );
73 if ( !existingRatsInfo.isEmpty() )
75 mCreateInfoLabel->setText( mCreateInfoLabel->text().append( u
"<br><ul><li>"_s + existingRatsInfo.join(
"</li><li>"_L1 ) ).append( u
"</ul>"_s ) );
76 mCreateInfoLabel->adjustSize();
77 mCreateInfoLabel->show();
80 if ( !nativeRatSupported )
82 mNativeRadioButton->setEnabled(
false );
83 mDbfRadioButton->setChecked(
true );
87 mDbfPathWidget->setFilter( u
"VAT DBF Files (*.vat.dbf)"_s );
88 if ( QFile::exists( mRasterLayer->dataProvider()->dataSourceUri() ) )
90 mDbfPathWidget->setFilePath( mRasterLayer->dataProvider()->dataSourceUri() +
".vat.dbf" );
94 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
95 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
96 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, [] {
97 QgsHelp::openHelp( u
"working_with_raster/raster_properties.html#raster-attribute-tables"_s );
137 QString errorMessage;
138 int bandNumber { 0 };
140 bool success {
false };
144 notify( tr(
"Error Creating Raster Attribute Table" ), tr(
"The raster attribute table could not be created." ),
Qgis::MessageLevel::Critical );
148 mRasterLayer->dataProvider()->setAttributeTable( bandNumber, rat );
154 const QString destinationPath {
filePath() };
155 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 )
157 success = rat->
writeToFile( destinationPath, &errorMessage );
161 mRasterLayer->dataProvider()->setAttributeTable( bandNumber,
nullptr );
167 success = mRasterLayer->dataProvider()->writeNativeAttributeTable( &errorMessage );
171 mRasterLayer->dataProvider()->setAttributeTable( bandNumber,
nullptr );
178 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.