QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 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 updateButtons();
35 } );
36
37 mRasterBand->setLayer( mRasterLayer );
38
39 mDbfPathWidget->setFilter( QStringLiteral( "VAT DBF Files (*.vat.dbf)" ) );
40
41 updateButtons();
42
44}
45
47{
48 return mDbfPathWidget->filePath();
49}
50
52{
53 return mRasterBand->currentBand();
54}
55
57{
58 mMessageBar = bar;
59}
60
62{
63 return mOpenRat->isChecked();
64}
65
67{
68 if ( !visible )
69 {
70 mOpenRat->setChecked( false );
71 }
72 mOpenRat->setVisible( visible );
73}
74
76{
77 bool success { false };
78
79 if ( rasterBand() < 1 )
80 {
81 notify( tr( "Invalid Raster Band" ), tr( "The selected raster band %1 is not valid." ).arg( rasterBand() ), Qgis::MessageLevel::Critical );
82 }
83 else
84 {
85 std::unique_ptr<QgsRasterAttributeTable> rat = std::make_unique<QgsRasterAttributeTable>();
86
87 QString errorMessage;
88 success = rat->readFromFile( filePath(), &errorMessage );
89
90 if ( !success )
91 {
92 notify( tr( "Error Loading Raster Attribute Table " ), tr( "The raster attribute table could not be loaded.\n%1" ).arg( errorMessage ), Qgis::MessageLevel::Critical );
93 }
94 else
95 {
96 if ( !rat->isValid( &errorMessage ) )
97 {
98 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 ) )
99 {
100 case QMessageBox::Cancel:
101 return;
102 case QMessageBox::Yes:
103 success = true;
104 break;
105 case QMessageBox::No:
106 default:
107 success = false;
108 break;
109 }
110 }
111
112 if ( mRasterLayer->attributeTable( rasterBand() ) && !mRasterLayer->attributeTable( rasterBand() )->filePath().isEmpty() )
113 {
114 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 ) )
115 {
116 case QMessageBox::Cancel:
117 return;
118 case QMessageBox::Yes:
119 success = true;
120 break;
121 case QMessageBox::No:
122 default:
123 success = false;
124 break;
125 }
126 }
127
128 if ( success )
129 {
130 mRasterLayer->dataProvider()->setAttributeTable( rasterBand(), rat.release() );
131 notify( tr( "Raster Attribute Table Loaded" ), tr( "The new raster attribute table was successfully loaded." ), Qgis::MessageLevel::Success );
132 }
133 }
134 }
135
136 QDialog::accept();
137}
138
139void QgsLoadRasterAttributeTableDialog::notify( const QString &title, const QString &message, Qgis::MessageLevel level )
140{
141 if ( mMessageBar )
142 {
143 mMessageBar->pushMessage( message, level );
144 }
145 else
146 {
147 switch ( level )
148 {
152 {
153 QMessageBox::information( nullptr, title, message );
154 break;
155 }
157 {
158 QMessageBox::warning( nullptr, title, message );
159 break;
160 }
162 {
163 QMessageBox::critical( nullptr, title, message );
164 break;
165 }
166 }
167 }
168}
169
170void QgsLoadRasterAttributeTableDialog::updateButtons()
171{
172 const bool isValidPath { !mDbfPathWidget->filePath().isEmpty() && QFile::exists( mDbfPathWidget->filePath() ) };
173 mButtonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isValidPath );
174}
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:210
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.