QGIS API Documentation 3.29.0-Master (694ebda27c)
qgsloadrasterattributetabledialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsloadrasterattributetabledialog.cpp - QgsLoadRasterAttributeTableDialog
3
4 ---------------------
5 begin : 21.10.2022
6 copyright : (C) 2022 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
18#include "qgsmessagebar.h"
19#include "qgsgui.h"
20#include <QMessageBox>
21#include <QPushButton>
22
24 : QDialog( parent )
25 , mRasterLayer( rasterLayer )
26{
27
28 setupUi( this );
29
30 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
31 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
32
33 connect( mDbfPathWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & )
34 {
35 updateButtons();
36 } );
37
38 mRasterBand->setLayer( mRasterLayer );
39
40 mDbfPathWidget->setFilter( QStringLiteral( "VAT DBF Files (*.vat.dbf)" ) );
41
42 updateButtons();
43
45}
46
48{
49 return mDbfPathWidget->filePath();
50}
51
53{
54 return mRasterBand->currentBand();
55}
56
58{
59 mMessageBar = bar;
60}
61
63{
64 return mOpenRat->isChecked();
65}
66
68{
69 if ( ! visible )
70 {
71 mOpenRat->setChecked( false );
72 }
73 mOpenRat->setVisible( visible );
74}
75
77{
78 bool success { false };
79
80 if ( rasterBand() < 1 )
81 {
82 notify( tr( "Invalid Raster Band" ),
83 tr( "The selected raster band %1 is not valid." ).arg( rasterBand() ),
84 Qgis::MessageLevel::Critical );
85 }
86 else
87 {
88 std::unique_ptr<QgsRasterAttributeTable> rat = std::make_unique<QgsRasterAttributeTable>( );
89
90 QString errorMessage;
91 success = rat->readFromFile( filePath(), &errorMessage );
92
93 if ( ! success )
94 {
95 notify( tr( "Error Loading Raster Attribute Table " ),
96 tr( "The raster attribute table could not be loaded.\n%1" ).arg( errorMessage ),
97 Qgis::MessageLevel::Critical );
98 }
99 else
100 {
101 if ( ! rat->isValid( &errorMessage ) )
102 {
103 switch ( QMessageBox::warning( nullptr, tr( "Invalid Raster Attribute Table" ), tr( "The raster attribute table is not valid:\n%1\nLoad anyway?" ), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel ) )
104 {
105 case QMessageBox::Cancel:
106 return;
107 case QMessageBox::Yes:
108 success = true;
109 break;
110 case QMessageBox::No:
111 default:
112 success = false;
113 break;
114 }
115 }
116
117 if ( mRasterLayer->attributeTable( rasterBand() ) && ! mRasterLayer->attributeTable( rasterBand() )->filePath().isEmpty() )
118 {
119 switch ( QMessageBox::warning( nullptr, tr( "Confirm Attribute Table Replacement" ), tr( "Raster band %1 already has an associated attribute table loaded from '%2'. Are you sure you want to replace the existing raster attribute table?" ).arg( QString::number( rasterBand() ), mRasterLayer->attributeTable( rasterBand() )->filePath() ), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel ) )
120 {
121 case QMessageBox::Cancel:
122 return;
123 case QMessageBox::Yes:
124 success = true;
125 break;
126 case QMessageBox::No:
127 default:
128 success = false;
129 break;
130 }
131 }
132
133 if ( success )
134 {
135 mRasterLayer->dataProvider()->setAttributeTable( rasterBand(), rat.release() );
136 notify( tr( "Raster Attribute Table Loaded" ),
137 tr( "The new raster attribute table was successfully loaded." ),
138 Qgis::MessageLevel::Success );
139 }
140 }
141 }
142
143 QDialog::accept();
144}
145
146void QgsLoadRasterAttributeTableDialog::notify( const QString &title, const QString &message, Qgis::MessageLevel level )
147{
148 if ( mMessageBar )
149 {
150 mMessageBar->pushMessage( message, level );
151 }
152 else
153 {
154 switch ( level )
155 {
156 case Qgis::MessageLevel::Info:
157 case Qgis::MessageLevel::Success:
158 case Qgis::MessageLevel::NoLevel:
159 {
160 QMessageBox::information( nullptr, title, message );
161 break;
162 }
163 case Qgis::MessageLevel::Warning:
164 {
165 QMessageBox::warning( nullptr, title, message );
166 break;
167 }
168 case Qgis::MessageLevel::Critical:
169 {
170 QMessageBox::critical( nullptr, title, message );
171 break;
172 }
173 }
174 }
175}
176
177void QgsLoadRasterAttributeTableDialog::updateButtons()
178{
179 const bool isValidPath { ! mDbfPathWidget->filePath().isEmpty() &&QFile::exists( mDbfPathWidget->filePath() ) };
180 mButtonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isValidPath );
181}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:115
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:178
bool openWhenDone() const
Returns true if the option to open the newly created attribute table is checked.
int rasterBand()
Returns the raster band associated to the raster attribute table.
QgsLoadRasterAttributeTableDialog(QgsRasterLayer *rasterLayer, QWidget *parent=nullptr)
Creates a new QgsCreateRasterAttributeTableDialog.
QString filePath() const
Returns the file path to VAT.DBF.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
void setOpenWhenDoneVisible(bool visible)
Sets the visibility of the "Open newly created raster attribute table" option to visible,...
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
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.
void setAttributeTable(int bandNumber, QgsRasterAttributeTable *attributeTable)
Set the attribute table to attributeTable for the specified bandNumber, if the attributeTable is NULL...
Represents a raster layer.
QgsRasterAttributeTable * attributeTable(int bandNumber) const
Returns the (possibly NULL) raster attribute table for the given band bandNumber.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.