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