27 : QDialog( parent, fl )
28 , mIsShapeFile( vlayer && vlayer->providerType() == QLatin1String(
"ogr" ) && vlayer->storageType() == QLatin1String(
"ESRI Shapefile" ) )
31 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsAddAttrDialog::mTypeBox_currentIndexChanged );
32 connect( mLength, &QSpinBox::editingFinished,
this, &QgsAddAttrDialog::mLength_editingFinished );
41 for (
int i = 0; i < typelist.size(); i++ )
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 );
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 );
61 mLength->setValue( 10 );
62 mLength->setClearValue( 10 );
64 mPrec->setClearValue( 3 );
65 mTypeBox_currentIndexChanged( 0 );
68 mNameEdit->setMaxLength( 10 );
70 mNameEdit->setFocus();
79 mLabelComment->hide();
86 mIllegalFieldNames = names;
89void QgsAddAttrDialog::mTypeBox_currentIndexChanged(
int idx )
91 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
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();
104void QgsAddAttrDialog::mLength_editingFinished()
106 setPrecisionMinMax();
109void QgsAddAttrDialog::setPrecisionMinMax()
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 );
122 if ( precisionIsEnabled )
124 mPrec->setMinimum( minPrecType );
125 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
129void QgsAddAttrDialog::accept()
131 const QString newName = mNameEdit->text().trimmed();
132 if ( mIsShapeFile && newName.compare( QLatin1String(
"shape" ), Qt::CaseInsensitive ) == 0 )
134 QMessageBox::warning(
this, tr(
"Add Field" ),
135 tr(
"Invalid field name. This field name is reserved and cannot be used." ) );
140 for (
const QString &illegalName : std::as_const( mIllegalFieldNames ) )
142 if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
144 QMessageBox::warning(
this, tr(
"Add Field" ),
145 tr(
"%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
150 if ( mNameEdit->text().isEmpty() )
152 QMessageBox::warning(
this, tr(
"Add Field" ),
153 tr(
"No name specified. Please specify a name to create a new field." ) );
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 );
174 ( QMetaType::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
175 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
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
182 if ( !mAliasEdit->text().isEmpty() )
@ EditAlias
Allows editing aliases.
@ EditComment
Allows editing comments.
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
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.
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
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.
#define QgsDebugMsgLevel(str, level)