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