19#include "moc_qgsaddattrdialog.cpp"
28 : QDialog( parent, fl )
29 , mIsShapeFile( vlayer && vlayer->providerType() == QLatin1String(
"ogr" ) && vlayer->storageType() == QLatin1String(
"ESRI Shapefile" ) )
32 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsAddAttrDialog::mTypeBox_currentIndexChanged );
33 connect( mLength, &QSpinBox::editingFinished,
this, &QgsAddAttrDialog::mLength_editingFinished );
42 for (
int i = 0; i < typelist.size(); i++ )
44 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 mTypeBox->setItemData( i,
static_cast<int>( typelist[i].mType ), Qt::UserRole );
48 mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
49 mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
50 mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
51 mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
52 mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
56 mLength->setValue( 10 );
57 mLength->setClearValue( 10 );
59 mPrec->setClearValue( 3 );
60 mTypeBox_currentIndexChanged( 0 );
63 mNameEdit->setMaxLength( 10 );
65 mNameEdit->setFocus();
74 mLabelComment->hide();
81 mIllegalFieldNames = names;
84void QgsAddAttrDialog::mTypeBox_currentIndexChanged(
int idx )
86 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
88 mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
89 mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
90 mLength->setVisible( mLength->minimum() < mLength->maximum() );
91 mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
92 if ( mLength->value() < mLength->minimum() )
93 mLength->setValue( mLength->minimum() );
94 if ( mLength->value() > mLength->maximum() )
95 mLength->setValue( mLength->maximum() );
99void QgsAddAttrDialog::mLength_editingFinished()
101 setPrecisionMinMax();
104void QgsAddAttrDialog::setPrecisionMinMax()
106 const int idx = mTypeBox->currentIndex();
107 const int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
108 const int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
109 const bool precisionIsEnabled = minPrecType < maxPrecType;
110 mPrec->setEnabled( precisionIsEnabled );
111 mPrec->setVisible( precisionIsEnabled );
112 mPrecLabel->setVisible( precisionIsEnabled );
117 if ( precisionIsEnabled )
119 mPrec->setMinimum( minPrecType );
120 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
124void QgsAddAttrDialog::accept()
126 const QString newName = mNameEdit->text().trimmed();
127 if ( mIsShapeFile && newName.compare( QLatin1String(
"shape" ), Qt::CaseInsensitive ) == 0 )
129 QMessageBox::warning(
this, tr(
"Add Field" ), tr(
"Invalid field name. This field name is reserved and cannot be used." ) );
134 for (
const QString &illegalName : std::as_const( mIllegalFieldNames ) )
136 if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
138 QMessageBox::warning(
this, tr(
"Add Field" ), tr(
"%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
143 if ( mNameEdit->text().isEmpty() )
145 QMessageBox::warning(
this, tr(
"Add Field" ), tr(
"No name specified. Please specify a name to create a new field." ) );
154 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 );
158 ( QMetaType::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
159 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
161 mPrec->isEnabled() ? mPrec->value() : 0,
162 mCommentEdit->text(),
163 static_cast<QMetaType::Type
>( mTypeBox->currentData( Qt::UserRole ).toInt() ) == QMetaType::Type::QVariantMap ? QMetaType::Type::QString : QMetaType::Type::UnknownType
166 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)