QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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, [] { QgsHelp::openHelp( u"working_with_raster/raster_properties.html#raster-attribute-tables"_s ); } );
43
44 mDataType->addItem( QgsFields::iconForFieldType( QMetaType::Type::QString ), tr( "String" ), static_cast<int>( QMetaType::Type::QString ) );
45 mDataType->addItem( QgsFields::iconForFieldType( QMetaType::Type::Int ), tr( "Integer" ), static_cast<int>( QMetaType::Type::Int ) );
46 mDataType->addItem( QgsFields::iconForFieldType( QMetaType::Type::LongLong ), tr( "Long Integer" ), static_cast<int>( QMetaType::Type::LongLong ) );
47 mDataType->addItem( QgsFields::iconForFieldType( QMetaType::Type::Double ), tr( "Double" ), static_cast<int>( QMetaType::Type::Double ) );
48 mStandardColumn->setChecked( true );
49
50 updateDialog();
51
53}
54
56{
57 if ( mAfter->isChecked() )
58 {
59 return mColumn->currentIndex() + 1;
60 }
61 else
62 {
63 return mColumn->currentIndex();
64 }
65}
66
68{
69 return mColor->isChecked();
70}
71
73{
74 return mRamp->isChecked();
75}
76
78{
79 return mName->text();
80}
81
86
88{
89 return static_cast<QMetaType::Type>( mDataType->currentData().toInt() );
90}
91
92void QgsRasterAttributeTableAddColumnDialog::updateDialog()
93{
94 mDefinition->setEnabled( mStandardColumn->isChecked() );
95 mError->hide();
96 mError->clear();
97
98 QList<Qgis::RasterAttributeTableFieldUsage> usages;
99 usages = mAttributeTable->usages();
100 const bool hasMinMax { usages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) };
101 const bool hasMinAndMax { usages.contains( Qgis::RasterAttributeTableFieldUsage::Min ) && usages.contains( Qgis::RasterAttributeTableFieldUsage::Max ) };
102 const bool canAddMinMax { !hasMinMax && mAttributeTable->type() == Qgis::RasterAttributeTableType::Thematic };
103 const bool canAddMinAndMax { !hasMinAndMax && mAttributeTable->type() == Qgis::RasterAttributeTableType::Athematic };
104
105 if ( mAttributeTable->hasColor() || mAttributeTable->hasRamp() )
106 {
107 mColor->setChecked( false );
108 mColor->setEnabled( false );
109 mRamp->setChecked( false );
110 mRamp->setEnabled( false );
111 mStandardColumn->setChecked( true );
112 }
113 else if ( mAttributeTable->type() == Qgis::RasterAttributeTableType::Thematic )
114 {
115 mColor->setEnabled( true );
116 mRamp->setChecked( false );
117 mRamp->setEnabled( false );
118 }
119 else
120 {
121 mColor->setEnabled( true );
122 mRamp->setEnabled( true );
123 }
124
125 bool isValid { true };
126 if ( mStandardColumn->isChecked() )
127 {
128 const QString upperName { mName->text().trimmed().toUpper() };
129 if ( upperName.isEmpty() )
130 {
131 mError->setText( tr( "A field name cannot be blank." ) );
132 isValid = false;
133 }
134
135 const QList<QgsRasterAttributeTable::Field> fields { mAttributeTable->fields() };
136 for ( const QgsRasterAttributeTable::Field &f : std::as_const( fields ) )
137 {
138 if ( f.name.toUpper() == upperName )
139 {
140 mError->setText( tr( "A field with this name already exists." ) );
141 isValid = false;
142 break;
143 }
144 }
145 }
146
147 const QHash<Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation> usageInfo { QgsRasterAttributeTable::usageInformation() };
148
149 const int currentUsageIndex { mUsage->currentIndex() };
150 const QSignalBlocker usageBlocker( mUsage );
151 mUsage->clear();
152
153
154 for ( auto it = usageInfo.cbegin(); it != usageInfo.cend(); ++it )
155 {
156 // We don't want duplicated columns or columns that are not suitable for color or ramps
157 // if they are already there, it could be a single if condition but it is more readable
158 // this way
159 if ( !it.value().unique || !usages.contains( it.key() ) )
160 {
161 if ( ( it.key() == Qgis::RasterAttributeTableFieldUsage::MinMax && !canAddMinMax )
162 || ( it.key() == Qgis::RasterAttributeTableFieldUsage::Min && !canAddMinAndMax )
163 || ( it.key() == Qgis::RasterAttributeTableFieldUsage::Max && !canAddMinAndMax )
164 || ( it.value().isColor )
165 || ( it.value().isRamp ) )
166 {
167 continue;
168 }
169 mUsage->addItem( QgsRasterAttributeTable::usageName( it.key() ), static_cast<int>( it.key() ) );
170 }
171 }
172 mUsage->setCurrentIndex( std::clamp( currentUsageIndex, 0, static_cast<int>( mUsage->count() - 1 ) ) );
173
174 const QList<QgsRasterAttributeTable::Field> fields { mAttributeTable->fields() };
175
176 int currentIndex { mColumn->currentIndex() };
177 if ( mColumn->currentIndex() < 0 )
178 {
179 currentIndex = fields.count() - 1;
180 }
181
182 const QSignalBlocker columnBlocker( mColumn );
183 mColumn->clear();
184 for ( const QgsRasterAttributeTable::Field &field : std::as_const( fields ) )
185 {
186 mColumn->addItem( field.name );
187 }
188 mColumn->setCurrentIndex( std::clamp( currentIndex, 0, static_cast<int>( fields.count() - 1 ) ) );
189
190 if ( !isValid )
191 {
192 mError->show();
193 }
194
195 mButtonBox->button( QDialogButtonBox::StandardButton::Ok )->setEnabled( isValid );
196}
RasterAttributeTableFieldUsage
The RasterAttributeTableFieldUsage enum represents the usage of a Raster Attribute Table field.
Definition qgis.h:1666
@ MinMax
Field usage MinMax.
Definition qgis.h:1672
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.