QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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 ***************************************************************************/
17#include "moc_qgsloadrasterattributetabledialog.cpp"
19#include "qgsmessagebar.h"
20#include "qgsgui.h"
21#include <QMessageBox>
22#include <QPushButton>
23
25 : QDialog( parent )
26 , mRasterLayer( rasterLayer )
27{
28
29 setupUi( this );
30
31 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
32 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
33
34 connect( mDbfPathWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & )
35 {
36 updateButtons();
37 } );
38
39 mRasterBand->setLayer( mRasterLayer );
40
41 mDbfPathWidget->setFilter( QStringLiteral( "VAT DBF Files (*.vat.dbf)" ) );
42
43 updateButtons();
44
46}
47
49{
50 return mDbfPathWidget->filePath();
51}
52
54{
55 return mRasterBand->currentBand();
56}
57
59{
60 mMessageBar = bar;
61}
62
64{
65 return mOpenRat->isChecked();
66}
67
69{
70 if ( ! visible )
71 {
72 mOpenRat->setChecked( false );
73 }
74 mOpenRat->setVisible( visible );
75}
76
78{
79 bool success { false };
80
81 if ( rasterBand() < 1 )
82 {
83 notify( tr( "Invalid Raster Band" ),
84 tr( "The selected raster band %1 is not valid." ).arg( rasterBand() ),
86 }
87 else
88 {
89 std::unique_ptr<QgsRasterAttributeTable> rat = std::make_unique<QgsRasterAttributeTable>( );
90
91 QString errorMessage;
92 success = rat->readFromFile( filePath(), &errorMessage );
93
94 if ( ! success )
95 {
96 notify( tr( "Error Loading Raster Attribute Table " ),
97 tr( "The raster attribute table could not be loaded.\n%1" ).arg( errorMessage ),
99 }
100 else
101 {
102 if ( ! rat->isValid( &errorMessage ) )
103 {
104 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 ) )
105 {
106 case QMessageBox::Cancel:
107 return;
108 case QMessageBox::Yes:
109 success = true;
110 break;
111 case QMessageBox::No:
112 default:
113 success = false;
114 break;
115 }
116 }
117
118 if ( mRasterLayer->attributeTable( rasterBand() ) && ! mRasterLayer->attributeTable( rasterBand() )->filePath().isEmpty() )
119 {
120 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 ) )
121 {
122 case QMessageBox::Cancel:
123 return;
124 case QMessageBox::Yes:
125 success = true;
126 break;
127 case QMessageBox::No:
128 default:
129 success = false;
130 break;
131 }
132 }
133
134 if ( success )
135 {
136 mRasterLayer->dataProvider()->setAttributeTable( rasterBand(), rat.release() );
137 notify( tr( "Raster Attribute Table Loaded" ),
138 tr( "The new raster attribute table was successfully loaded." ),
140 }
141 }
142 }
143
144 QDialog::accept();
145}
146
147void QgsLoadRasterAttributeTableDialog::notify( const QString &title, const QString &message, Qgis::MessageLevel level )
148{
149 if ( mMessageBar )
150 {
151 mMessageBar->pushMessage( message, level );
152 }
153 else
154 {
155 switch ( level )
156 {
160 {
161 QMessageBox::information( nullptr, title, message );
162 break;
163 }
165 {
166 QMessageBox::warning( nullptr, title, message );
167 break;
168 }
170 {
171 QMessageBox::critical( nullptr, title, message );
172 break;
173 }
174 }
175 }
176}
177
178void QgsLoadRasterAttributeTableDialog::updateButtons()
179{
180 const bool isValidPath { ! mDbfPathWidget->filePath().isEmpty() &&QFile::exists( mDbfPathWidget->filePath() ) };
181 mButtonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isValidPath );
182}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ NoLevel
No level.
Definition qgis.h:159
@ Warning
Warning message.
Definition qgis.h:156
@ Critical
Critical/error message.
Definition qgis.h:157
@ Info
Information message.
Definition qgis.h:155
@ Success
Used for reporting a successful operation.
Definition qgis.h:158
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:209
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.
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.