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" )
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 );
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 );
62 mLength->setValue( 10 );
63 mLength->setClearValue( 10 );
65 mPrec->setClearValue( 3 );
66 mTypeBox_currentIndexChanged( 0 );
69 mNameEdit->setMaxLength( 10 );
71 mNameEdit->setFocus();
80 mLabelComment->hide();
87 mIllegalFieldNames = names;
90void QgsAddAttrDialog::mTypeBox_currentIndexChanged(
int idx )
92 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
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();
105void QgsAddAttrDialog::mLength_editingFinished()
107 setPrecisionMinMax();
110void QgsAddAttrDialog::setPrecisionMinMax()
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 );
123 if ( precisionIsEnabled )
125 mPrec->setMinimum( minPrecType );
126 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
130void QgsAddAttrDialog::accept()
132 const QString newName = mNameEdit->text().trimmed();
133 if ( mIsShapeFile && newName.compare( QLatin1String(
"shape" ), Qt::CaseInsensitive ) == 0 )
135 QMessageBox::warning(
this, tr(
"Add Field" ),
136 tr(
"Invalid field name. This field name is reserved and cannot be used." ) );
141 for (
const QString &illegalName : std::as_const( mIllegalFieldNames ) )
143 if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
145 QMessageBox::warning(
this, tr(
"Add Field" ),
146 tr(
"%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
151 if ( mNameEdit->text().isEmpty() )
153 QMessageBox::warning(
this, tr(
"Add Field" ),
154 tr(
"No name specified. Please specify a name to create a new field." ) );
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 );
175 ( QMetaType::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
176 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
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
183 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)