QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
gui
raster
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
***************************************************************************/
16
#include "
qgsloadrasterattributetabledialog.h
"
17
18
#include "
qgsgui.h
"
19
#include "
qgshelp.h
"
20
#include "
qgsmessagebar.h
"
21
#include "
qgsrasterattributetable.h
"
22
23
#include <QMessageBox>
24
#include <QPushButton>
25
#include <QString>
26
27
#include "moc_qgsloadrasterattributetabledialog.cpp"
28
29
using namespace
Qt::StringLiterals;
30
31
QgsLoadRasterAttributeTableDialog::QgsLoadRasterAttributeTableDialog
(
QgsRasterLayer
*rasterLayer, QWidget *parent )
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
, [] {
QgsHelp::openHelp
( u
"working_with_raster/raster_properties.html#raster-attribute-tables"
_s ); } );
40
41
connect( mDbfPathWidget, &
QgsFileWidget::fileChanged
,
this
, [
this
](
const
QString & ) { updateButtons(); } );
42
43
mRasterBand->setLayer( mRasterLayer );
44
45
mDbfPathWidget->setFilter( u
"VAT DBF Files (*.vat.dbf)"
_s );
46
47
updateButtons();
48
49
QgsGui::enableAutoGeometryRestore
(
this
);
50
}
51
52
QString
QgsLoadRasterAttributeTableDialog::filePath
()
const
53
{
54
return
mDbfPathWidget->filePath();
55
}
56
57
int
QgsLoadRasterAttributeTableDialog::rasterBand
()
58
{
59
return
mRasterBand->currentBand();
60
}
61
62
void
QgsLoadRasterAttributeTableDialog::setMessageBar
(
QgsMessageBar
*bar )
63
{
64
mMessageBar = bar;
65
}
66
67
bool
QgsLoadRasterAttributeTableDialog::openWhenDone
()
const
68
{
69
return
mOpenRat->isChecked();
70
}
71
72
void
QgsLoadRasterAttributeTableDialog::setOpenWhenDoneVisible
(
bool
visible )
73
{
74
if
( !visible )
75
{
76
mOpenRat->setChecked(
false
);
77
}
78
mOpenRat->setVisible( visible );
79
}
80
81
void
QgsLoadRasterAttributeTableDialog::accept
()
82
{
83
bool
success {
false
};
84
85
if
(
rasterBand
() < 1 )
86
{
87
notify( tr(
"Invalid Raster Band"
), tr(
"The selected raster band %1 is not valid."
).arg(
rasterBand
() ),
Qgis::MessageLevel::Critical
);
88
}
89
else
90
{
91
auto
rat = std::make_unique<QgsRasterAttributeTable>();
92
93
QString errorMessage;
94
success = rat->readFromFile(
filePath
(), &errorMessage );
95
96
if
( !success )
97
{
98
notify( tr(
"Error Loading Raster Attribute Table "
), tr(
"The raster attribute table could not be loaded.\n%1"
).arg( errorMessage ),
Qgis::MessageLevel::Critical
);
99
}
100
else
101
{
102
if
( !rat->isValid( &errorMessage ) )
103
{
104
switch
(
105
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 )
106
)
107
{
108
case
QMessageBox::Cancel:
109
return
;
110
case
QMessageBox::Yes:
111
success =
true
;
112
break
;
113
case
QMessageBox::No:
114
default
:
115
success =
false
;
116
break
;
117
}
118
}
119
120
if
( mRasterLayer->attributeTable(
rasterBand
() ) && !mRasterLayer->attributeTable(
rasterBand
() )->
filePath
().isEmpty() )
121
{
122
switch
( QMessageBox::warning(
123
nullptr
,
124
tr(
"Confirm Attribute Table Replacement"
),
125
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?"
)
126
.arg( QString::number(
rasterBand
() ), mRasterLayer->attributeTable(
rasterBand
() )->
filePath
() ),
127
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel
128
) )
129
{
130
case
QMessageBox::Cancel:
131
return
;
132
case
QMessageBox::Yes:
133
success =
true
;
134
break
;
135
case
QMessageBox::No:
136
default
:
137
success =
false
;
138
break
;
139
}
140
}
141
142
if
( success )
143
{
144
mRasterLayer->dataProvider()->setAttributeTable(
rasterBand
(), rat.release() );
145
notify( tr(
"Raster Attribute Table Loaded"
), tr(
"The new raster attribute table was successfully loaded."
),
Qgis::MessageLevel::Success
);
146
}
147
}
148
}
149
150
QDialog::accept();
151
}
152
153
void
QgsLoadRasterAttributeTableDialog::notify(
const
QString &title,
const
QString &message,
Qgis::MessageLevel
level )
154
{
155
if
( mMessageBar )
156
{
157
mMessageBar->
pushMessage
( message, level );
158
}
159
else
160
{
161
switch
( level )
162
{
163
case
Qgis::MessageLevel::Info
:
164
case
Qgis::MessageLevel::Success
:
165
case
Qgis::MessageLevel::NoLevel
:
166
{
167
QMessageBox::information(
nullptr
, title, message );
168
break
;
169
}
170
case
Qgis::MessageLevel::Warning
:
171
{
172
QMessageBox::warning(
nullptr
, title, message );
173
break
;
174
}
175
case
Qgis::MessageLevel::Critical
:
176
{
177
QMessageBox::critical(
nullptr
, title, message );
178
break
;
179
}
180
}
181
}
182
}
183
184
void
QgsLoadRasterAttributeTableDialog::updateButtons()
185
{
186
const
bool
isValidPath { !mDbfPathWidget->filePath().isEmpty() && QFile::exists( mDbfPathWidget->filePath() ) };
187
mButtonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isValidPath );
188
}
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition
qgis.h:160
Qgis::NoLevel
@ NoLevel
No level.
Definition
qgis.h:165
Qgis::Warning
@ Warning
Warning message.
Definition
qgis.h:162
Qgis::Critical
@ Critical
Critical/error message.
Definition
qgis.h:163
Qgis::Info
@ Info
Information message.
Definition
qgis.h:161
Qgis::Success
@ Success
Used for reporting a successful operation.
Definition
qgis.h:164
QgsFileWidget::fileChanged
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
QgsGui::enableAutoGeometryRestore
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:225
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition
qgshelp.cpp:41
QgsLoadRasterAttributeTableDialog::openWhenDone
bool openWhenDone() const
Returns true if the option to open the newly created attribute table is checked.
Definition
qgsloadrasterattributetabledialog.cpp:67
QgsLoadRasterAttributeTableDialog::rasterBand
int rasterBand()
Returns the raster band associated to the raster attribute table.
Definition
qgsloadrasterattributetabledialog.cpp:57
QgsLoadRasterAttributeTableDialog::accept
void accept() override
Definition
qgsloadrasterattributetabledialog.cpp:81
QgsLoadRasterAttributeTableDialog::QgsLoadRasterAttributeTableDialog
QgsLoadRasterAttributeTableDialog(QgsRasterLayer *rasterLayer, QWidget *parent=nullptr)
Creates a new QgsCreateRasterAttributeTableDialog.
Definition
qgsloadrasterattributetabledialog.cpp:31
QgsLoadRasterAttributeTableDialog::filePath
QString filePath() const
Returns the file path to VAT.DBF.
Definition
qgsloadrasterattributetabledialog.cpp:52
QgsLoadRasterAttributeTableDialog::setMessageBar
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
Definition
qgsloadrasterattributetabledialog.cpp:62
QgsLoadRasterAttributeTableDialog::setOpenWhenDoneVisible
void setOpenWhenDoneVisible(bool visible)
Sets the visibility of the "Open newly created raster attribute table" option to visible,...
Definition
qgsloadrasterattributetabledialog.cpp:72
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition
qgsmessagebar.h:61
QgsMessageBar::pushMessage
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.
Definition
qgsmessagebar.cpp:409
QgsRasterLayer
Represents a raster layer.
Definition
qgsrasterlayer.h:82
qgsgui.h
qgshelp.h
qgsloadrasterattributetabledialog.h
qgsmessagebar.h
qgsrasterattributetable.h
Generated on
for QGIS API Documentation by
1.17.0