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