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