QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
20#include "qgsfields.h"
21#include "qgslogger.h"
23#include "qgsvectorlayer.h"
24
25#include <QMessageBox>
26
27#include "moc_qgsaddattrdialog.cpp"
28
29QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt::WindowFlags fl )
30 : QDialog( parent, fl )
31 , mIsShapeFile( vlayer && vlayer->providerType() == QLatin1String( "ogr" ) && vlayer->storageType() == QLatin1String( "ESRI Shapefile" ) )
32{
33 setupUi( this );
34 connect( mTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsAddAttrDialog::mTypeBox_currentIndexChanged );
35 connect( mLength, &QSpinBox::editingFinished, this, &QgsAddAttrDialog::mLength_editingFinished );
36
37 if ( !vlayer || !vlayer->dataProvider() )
38 return;
39
41
42 //fill data types into the combo box
43 const QList<QgsVectorDataProvider::NativeType> &typelist = vlayer->dataProvider()->nativeTypes();
44 for ( int i = 0; i < typelist.size(); i++ )
45 {
46 QgsDebugMsgLevel( QStringLiteral( "name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7" ).arg( typelist[i].mTypeDesc ).arg( typelist[i].mType ).arg( typelist[i].mTypeName ).arg( typelist[i].mMinLen ).arg( typelist[i].mMaxLen ).arg( typelist[i].mMinPrec ).arg( typelist[i].mMaxPrec ), 2 );
47
48 whileBlocking( mTypeBox )->addItem( QgsFields::iconForFieldType( typelist[i].mType, typelist[i].mSubType, typelist[i].mTypeName ), typelist[i].mTypeDesc );
49 mTypeBox->setItemData( i, static_cast<int>( typelist[i].mType ), Qt::UserRole );
50 mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
51 mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
52 mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
53 mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
54 mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
55 }
56
57 //default values for field width and precision
58 mLength->setValue( 10 );
59 mLength->setClearValue( 10 );
60 mPrec->setValue( 3 );
61 mPrec->setClearValue( 3 );
62 mTypeBox_currentIndexChanged( 0 );
63
64 if ( mIsShapeFile )
65 mNameEdit->setMaxLength( 10 );
66
67 mNameEdit->setFocus();
68
69 if ( !( attributeEditCapabilities & Qgis::VectorDataProviderAttributeEditCapability::EditAlias ) )
70 {
71 mLabelAlias->hide();
72 mAliasEdit->hide();
73 }
74 if ( !( attributeEditCapabilities & Qgis::VectorDataProviderAttributeEditCapability::EditComment ) )
75 {
76 mLabelComment->hide();
77 mCommentEdit->hide();
78 }
79}
80
81void QgsAddAttrDialog::setIllegalFieldNames( const QSet<QString> &names )
82{
83 mIllegalFieldNames = names;
84}
85
86void QgsAddAttrDialog::mTypeBox_currentIndexChanged( int idx )
87{
88 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
89
90 mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
91 mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
92 mLength->setVisible( mLength->minimum() < mLength->maximum() );
93 mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
94 if ( mLength->value() < mLength->minimum() )
95 mLength->setValue( mLength->minimum() );
96 if ( mLength->value() > mLength->maximum() )
97 mLength->setValue( mLength->maximum() );
98 setPrecisionMinMax();
99}
100
101void QgsAddAttrDialog::mLength_editingFinished()
102{
103 setPrecisionMinMax();
104}
105
106void QgsAddAttrDialog::setPrecisionMinMax()
107{
108 const int idx = mTypeBox->currentIndex();
109 const int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
110 const int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
111 const bool precisionIsEnabled = minPrecType < maxPrecType;
112 mPrec->setEnabled( precisionIsEnabled );
113 mPrec->setVisible( precisionIsEnabled );
114 mPrecLabel->setVisible( precisionIsEnabled );
115
116 // Do not set min/max if it's disabled or we'll loose the default value,
117 // see https://github.com/qgis/QGIS/issues/26880 - QGIS saves integer field when
118 // I create a new real field through field calculator (Update field works as intended)
119 if ( precisionIsEnabled )
120 {
121 mPrec->setMinimum( minPrecType );
122 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
123 }
124}
125
126void QgsAddAttrDialog::accept()
127{
128 const QString newName = mNameEdit->text().trimmed();
129 if ( mIsShapeFile && newName.compare( QLatin1String( "shape" ), Qt::CaseInsensitive ) == 0 )
130 {
131 QMessageBox::warning( this, tr( "Add Field" ), tr( "Invalid field name. This field name is reserved and cannot be used." ) );
132 return;
133 }
134
135
136 for ( const QString &illegalName : std::as_const( mIllegalFieldNames ) )
137 {
138 if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
139 {
140 QMessageBox::warning( this, tr( "Add Field" ), tr( "%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
141 return;
142 }
143 }
144
145 if ( mNameEdit->text().isEmpty() )
146 {
147 QMessageBox::warning( this, tr( "Add Field" ), tr( "No name specified. Please specify a name to create a new field." ) );
148 return;
149 }
150
151 QDialog::accept();
152}
153
155{
156 QgsDebugMsgLevel( QStringLiteral( "idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7" ).arg( mTypeBox->currentIndex() ).arg( mNameEdit->text() ).arg( mTypeBox->currentData( Qt::UserRole ).toInt() ).arg( mTypeBox->currentData( Qt::UserRole + 1 ).toString() ).arg( mLength->value() ).arg( mPrec->value() ).arg( mCommentEdit->text() ), 2 );
157
158 QgsField res = QgsField(
159 mNameEdit->text(),
160 ( QMetaType::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
161 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
162 mLength->value(),
163 mPrec->isEnabled() ? mPrec->value() : 0,
164 mCommentEdit->text(),
165 static_cast<QMetaType::Type>( mTypeBox->currentData( Qt::UserRole ).toInt() ) == QMetaType::Type::QVariantMap ? QMetaType::Type::QString : QMetaType::Type::UnknownType
166 );
167
168 if ( !mAliasEdit->text().isEmpty() )
169 res.setAlias( mAliasEdit->text() );
170
171 return res;
172}
@ EditComment
Allows editing comments.
Definition qgis.h:592
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
Definition qgis.h:602
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:54
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:298
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 dataset.
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:6511
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61