QGIS API Documentation 3.99.0-Master (0c964c3d988)
Loading...
Searching...
No Matches
qgsrasterattributetableaddcolumndialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterattributetableaddcolumndialog.cpp - QgsRasterAttributeTableAddColumnDialog
3
4 ---------------------
5 begin : 10.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"
21
22#include <QPushButton>
23#include <QString>
24
25#include "moc_qgsrasterattributetableaddcolumndialog.cpp"
26
27using namespace Qt::StringLiterals;
28
30 : QDialog( parent )
31 , mAttributeTable( attributeTable )
32{
33 // Precondition
34 Q_ASSERT( mAttributeTable );
35
36 setupUi( this );
37
38 connect( mName, &QLineEdit::textChanged, this, [this]( const QString & ) { updateDialog(); } );
39 connect( mStandardColumn, &QRadioButton::toggled, this, [this]( bool ) { updateDialog(); } );
40 connect( mColor, &QRadioButton::toggled, this, [this]( bool ) { updateDialog(); } );
41 connect( mUsage, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int ) { updateDialog(); } );
42 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, [] {
43 QgsHelp::openHelp( u"working_with_raster/raster_properties.html#raster-attribute-tables"_s );
44 } );
45
46 mDataType->addItem( QgsFields::iconForFieldType( QMetaType::Type::QString ), tr( "String" ), static_cast<int>( QMetaType::Type::QString ) );
47 mDataType->addItem( QgsFields::iconForFieldType( QMetaType::Type::Int ), tr( "Integer" ), static_cast<int>( QMetaType::Type::Int ) );
48 mDataType->addItem( QgsFields::iconForFieldType( QMetaType::Type::LongLong ), tr( "Long Integer" ), static_cast<int>( QMetaType::Type::LongLong ) );
49 mDataType->addItem( QgsFields::iconForFieldType( QMetaType::Type::Double ), tr( "Double" ), static_cast<int>( QMetaType::Type::Double ) );
50 mStandardColumn->setChecked( true );
51
52 updateDialog();
53
55}
56
58{
59 if ( mAfter->isChecked() )
60 {
61 return mColumn->currentIndex() + 1;
62 }
63 else
64 {
65 return mColumn->currentIndex();
66 }
67}
68
70{
71 return mColor->isChecked();
72}
73
75{
76 return mRamp->isChecked();
77}
78
80{
81 return mName->text();
82}
83
88
90{
91 return static_cast<QMetaType::Type>( mDataType->currentData().toInt() );
92}
93
94void QgsRasterAttributeTableAddColumnDialog::updateDialog()
95{
96 mDefinition->setEnabled( mStandardColumn->isChecked() );
97 mError->hide();
98 mError->clear();
99
100 QList<Qgis::RasterAttributeTableFieldUsage> usages;
101 usages = mAttributeTable->usages();
102 const bool hasMinMax { usages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) };
103 const bool hasMinAndMax { usages.contains( Qgis::RasterAttributeTableFieldUsage::Min ) && usages.contains( Qgis::RasterAttributeTableFieldUsage::Max ) };
104 const bool canAddMinMax { !hasMinMax && mAttributeTable->type() == Qgis::RasterAttributeTableType::Thematic };
105 const bool canAddMinAndMax { !hasMinAndMax && mAttributeTable->type() == Qgis::RasterAttributeTableType::Athematic };
106
107 if ( mAttributeTable->hasColor() || mAttributeTable->hasRamp() )
108 {
109 mColor->setChecked( false );
110 mColor->setEnabled( false );
111 mRamp->setChecked( false );
112 mRamp->setEnabled( false );
113 mStandardColumn->setChecked( true );
114 }
115 else if ( mAttributeTable->type() == Qgis::RasterAttributeTableType::Thematic )
116 {
117 mColor->setEnabled( true );
118 mRamp->setChecked( false );
119 mRamp->setEnabled( false );
120 }
121 else
122 {
123 mColor->setEnabled( true );
124 mRamp->setEnabled( true );
125 }
126
127 bool isValid { true };
128 if ( mStandardColumn->isChecked() )
129 {
130 const QString upperName { mName->text().trimmed().toUpper() };
131 if ( upperName.isEmpty() )
132 {
133 mError->setText( tr( "A field name cannot be blank." ) );
134 isValid = false;
135 }
136
137 const QList<QgsRasterAttributeTable::Field> fields { mAttributeTable->fields() };
138 for ( const QgsRasterAttributeTable::Field &f : std::as_const( fields ) )
139 {
140 if ( f.name.toUpper() == upperName )
141 {
142 mError->setText( tr( "A field with this name already exists." ) );
143 isValid = false;
144 break;
145 }
146 }
147 }
148
149 const QHash<Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation> usageInfo { QgsRasterAttributeTable::usageInformation() };
150
151 const int currentUsageIndex { mUsage->currentIndex() };
152 const QSignalBlocker usageBlocker( mUsage );
153 mUsage->clear();
154
155
156 for ( auto it = usageInfo.cbegin(); it != usageInfo.cend(); ++it )
157 {
158 // We don't want duplicated columns or columns that are not suitable for color or ramps
159 // if they are already there, it could be a single if condition but it is more readable
160 // this way
161 if ( !it.value().unique || !usages.contains( it.key() ) )
162 {
163 if ( ( it.key() == Qgis::RasterAttributeTableFieldUsage::MinMax && !canAddMinMax ) || ( it.key() == Qgis::RasterAttributeTableFieldUsage::Min && !canAddMinAndMax ) || ( it.key() == Qgis::RasterAttributeTableFieldUsage::Max && !canAddMinAndMax ) || ( it.value().isColor ) || ( it.value().isRamp ) )
164 {
165 continue;
166 }
167 mUsage->addItem( QgsRasterAttributeTable::usageName( it.key() ), static_cast<int>( it.key() ) );
168 }
169 }
170 mUsage->setCurrentIndex( std::clamp( currentUsageIndex, 0, static_cast<int>( mUsage->count() - 1 ) ) );
171
172 const QList<QgsRasterAttributeTable::Field> fields { mAttributeTable->fields() };
173
174 int currentIndex { mColumn->currentIndex() };
175 if ( mColumn->currentIndex() < 0 )
176 {
177 currentIndex = fields.count() - 1;
178 }
179
180 const QSignalBlocker columnBlocker( mColumn );
181 mColumn->clear();
182 for ( const QgsRasterAttributeTable::Field &field : std::as_const( fields ) )
183 {
184 mColumn->addItem( field.name );
185 }
186 mColumn->setCurrentIndex( std::clamp( currentIndex, 0, static_cast<int>( fields.count() - 1 ) ) );
187
188 if ( !isValid )
189 {
190 mError->show();
191 }
192
193 mButtonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isValid );
194}
RasterAttributeTableFieldUsage
The RasterAttributeTableFieldUsage enum represents the usage of a Raster Attribute Table field.
Definition qgis.h:1660
@ MinMax
Field usage MinMax.
Definition qgis.h:1666
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
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
QMetaType::Type type() const
Returns the new column type.
bool isRamp() const
Returns true if the add color ramp column option was checked.
bool isColor() const
Returns true if the add color column option was checked.
int position() const
Returns the position where the new column (before) will be inserted.
QgsRasterAttributeTableAddColumnDialog(QgsRasterAttributeTable *attributeTable, QWidget *parent=nullptr)
Creates a new QgsRasterAttributeTableAddColumnDialog.
Qgis::RasterAttributeTableFieldUsage usage() const
Returns the new column name.
Represents a Raster Attribute Table (RAT).
static QHash< Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation > usageInformation()
Returns information about supported Raster Attribute Table usages.
QList< Qgis::RasterAttributeTableFieldUsage > usages() const
Returns the list of field usages.
static QString usageName(const Qgis::RasterAttributeTableFieldUsage fieldusage)
Returns the translated human readable name of fieldUsage.