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