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