QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgsaddattrdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsaddattrdialog.h - description
3 -------------------
4 begin : January 2005
5 copyright : (C) 2005 by Marco Hugentobler
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsaddattrdialog.h"
19#include "qgsvectorlayer.h"
21#include "qgslogger.h"
22#include "qgsfields.h"
23
24#include <QMessageBox>
25
26QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt::WindowFlags fl )
27 : QDialog( parent, fl )
28 , mIsShapeFile( vlayer && vlayer->providerType() == QLatin1String( "ogr" ) && vlayer->storageType() == QLatin1String( "ESRI Shapefile" ) )
29{
30 setupUi( this );
31 connect( mTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsAddAttrDialog::mTypeBox_currentIndexChanged );
32 connect( mLength, &QSpinBox::editingFinished, this, &QgsAddAttrDialog::mLength_editingFinished );
33
34 if ( !vlayer || !vlayer->dataProvider() )
35 return;
36
38
39 //fill data types into the combo box
40 const QList< QgsVectorDataProvider::NativeType > &typelist = vlayer->dataProvider()->nativeTypes();
41 for ( int i = 0; i < typelist.size(); i++ )
42 {
43 QgsDebugMsgLevel( QStringLiteral( "name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7" )
44 .arg( typelist[i].mTypeDesc )
45 .arg( typelist[i].mType )
46 .arg( typelist[i].mTypeName )
47 .arg( typelist[i].mMinLen ).arg( typelist[i].mMaxLen )
48 .arg( typelist[i].mMinPrec ).arg( typelist[i].mMaxPrec ), 2 );
49
50 whileBlocking( mTypeBox )->addItem( QgsFields::iconForFieldType( typelist[i].mType, typelist[i].mSubType, typelist[i].mTypeName ),
51 typelist[i].mTypeDesc );
52 mTypeBox->setItemData( i, static_cast<int>( typelist[i].mType ), Qt::UserRole );
53 mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
54 mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
55 mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
56 mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
57 mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
58 }
59
60 //default values for field width and precision
61 mLength->setValue( 10 );
62 mLength->setClearValue( 10 );
63 mPrec->setValue( 3 );
64 mPrec->setClearValue( 3 );
65 mTypeBox_currentIndexChanged( 0 );
66
67 if ( mIsShapeFile )
68 mNameEdit->setMaxLength( 10 );
69
70 mNameEdit->setFocus();
71
72 if ( !( attributeEditCapabilities & Qgis::VectorDataProviderAttributeEditCapability::EditAlias ) )
73 {
74 mLabelAlias->hide();
75 mAliasEdit->hide();
76 }
77 if ( !( attributeEditCapabilities & Qgis::VectorDataProviderAttributeEditCapability::EditComment ) )
78 {
79 mLabelComment->hide();
80 mCommentEdit->hide();
81 }
82}
83
84void QgsAddAttrDialog::setIllegalFieldNames( const QSet<QString> &names )
85{
86 mIllegalFieldNames = names;
87}
88
89void QgsAddAttrDialog::mTypeBox_currentIndexChanged( int idx )
90{
91 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
92
93 mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
94 mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
95 mLength->setVisible( mLength->minimum() < mLength->maximum() );
96 mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
97 if ( mLength->value() < mLength->minimum() )
98 mLength->setValue( mLength->minimum() );
99 if ( mLength->value() > mLength->maximum() )
100 mLength->setValue( mLength->maximum() );
101 setPrecisionMinMax();
102}
103
104void QgsAddAttrDialog::mLength_editingFinished()
105{
106 setPrecisionMinMax();
107}
108
109void QgsAddAttrDialog::setPrecisionMinMax()
110{
111 const int idx = mTypeBox->currentIndex();
112 const int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
113 const int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
114 const bool precisionIsEnabled = minPrecType < maxPrecType;
115 mPrec->setEnabled( precisionIsEnabled );
116 mPrec->setVisible( precisionIsEnabled );
117 mPrecLabel->setVisible( precisionIsEnabled );
118
119 // Do not set min/max if it's disabled or we'll loose the default value,
120 // see https://github.com/qgis/QGIS/issues/26880 - QGIS saves integer field when
121 // I create a new real field through field calculator (Update field works as intended)
122 if ( precisionIsEnabled )
123 {
124 mPrec->setMinimum( minPrecType );
125 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
126 }
127}
128
129void QgsAddAttrDialog::accept()
130{
131 const QString newName = mNameEdit->text().trimmed();
132 if ( mIsShapeFile && newName.compare( QLatin1String( "shape" ), Qt::CaseInsensitive ) == 0 )
133 {
134 QMessageBox::warning( this, tr( "Add Field" ),
135 tr( "Invalid field name. This field name is reserved and cannot be used." ) );
136 return;
137 }
138
139
140 for ( const QString &illegalName : std::as_const( mIllegalFieldNames ) )
141 {
142 if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
143 {
144 QMessageBox::warning( this, tr( "Add Field" ),
145 tr( "%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
146 return;
147 }
148 }
149
150 if ( mNameEdit->text().isEmpty() )
151 {
152 QMessageBox::warning( this, tr( "Add Field" ),
153 tr( "No name specified. Please specify a name to create a new field." ) );
154 return;
155 }
156
157 QDialog::accept();
158}
159
161{
162
163 QgsDebugMsgLevel( QStringLiteral( "idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7" )
164 .arg( mTypeBox->currentIndex() )
165 .arg( mNameEdit->text() )
166 .arg( mTypeBox->currentData( Qt::UserRole ).toInt() )
167 .arg( mTypeBox->currentData( Qt::UserRole + 1 ).toString() )
168 .arg( mLength->value() )
169 .arg( mPrec->value() )
170 .arg( mCommentEdit->text() ), 2 );
171
172 QgsField res = QgsField(
173 mNameEdit->text(),
174 ( QMetaType::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
175 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
176 mLength->value(),
177 mPrec->isEnabled() ? mPrec->value() : 0,
178 mCommentEdit->text(),
179 static_cast<QMetaType::Type>( mTypeBox->currentData( Qt::UserRole ).toInt() ) == QMetaType::Type::QVariantMap ? QMetaType::Type::QString : QMetaType::Type::UnknownType
180 );
181
182 if ( !mAliasEdit->text().isEmpty() )
183 res.setAlias( mAliasEdit->text() );
184
185 return res;
186}
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
Definition qgis.h:566
QgsField field() const
Returns a field for the configured attribute.
void setIllegalFieldNames(const QSet< QString > &names)
Sets a list of field names which are considered illegal and should not be accepted by the dialog.
QgsAddAttrDialog(QgsVectorLayer *vlayer, QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
constructor
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:295
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
QList< QgsVectorDataProvider::NativeType > nativeTypes() const
Returns the names of the supported types.
virtual Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const
Returns the provider's supported attribute editing capabilities.
Represents a vector layer which manages a vector based data sets.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5821
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39