28#include "moc_qgsaddattrdialog.cpp"
30using namespace Qt::StringLiterals;
33 : QDialog( parent, fl )
34 , mIsShapeFile( vlayer && vlayer->providerType() ==
"ogr"_L1 && vlayer->storageType() ==
"ESRI Shapefile"_L1 )
37 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsAddAttrDialog::mTypeBox_currentIndexChanged );
38 connect( mLength, &QSpinBox::editingFinished,
this, &QgsAddAttrDialog::mLength_editingFinished );
47 for (
int i = 0; i < typelist.size(); i++ )
50 u
"name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7"_s.arg( typelist[i].mTypeDesc )
51 .arg( typelist[i].mType )
52 .arg( typelist[i].mTypeName )
53 .arg( typelist[i].mMinLen )
54 .arg( typelist[i].mMaxLen )
55 .arg( typelist[i].mMinPrec )
56 .arg( typelist[i].mMaxPrec ),
61 mTypeBox->setItemData( i,
static_cast<int>( typelist[i].mType ), Qt::UserRole );
62 mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
63 mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
64 mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
65 mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
66 mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
70 mLength->setValue( 10 );
71 mLength->setClearValue( 10 );
73 mPrec->setClearValue( 3 );
74 mTypeBox_currentIndexChanged( 0 );
77 mNameEdit->setMaxLength( 10 );
79 mNameEdit->setFocus();
88 mLabelComment->hide();
95 mIllegalFieldNames = names;
98void QgsAddAttrDialog::mTypeBox_currentIndexChanged(
int idx )
100 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
102 mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
103 mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
104 mLength->setVisible( mLength->minimum() < mLength->maximum() );
105 mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
106 if ( mLength->value() < mLength->minimum() )
107 mLength->setValue( mLength->minimum() );
108 if ( mLength->value() > mLength->maximum() )
109 mLength->setValue( mLength->maximum() );
110 setPrecisionMinMax();
113void QgsAddAttrDialog::mLength_editingFinished()
115 setPrecisionMinMax();
118void QgsAddAttrDialog::setPrecisionMinMax()
120 const int idx = mTypeBox->currentIndex();
121 const int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
122 const int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
123 const bool precisionIsEnabled = minPrecType < maxPrecType;
124 mPrec->setEnabled( precisionIsEnabled );
125 mPrec->setVisible( precisionIsEnabled );
126 mPrecLabel->setVisible( precisionIsEnabled );
131 if ( precisionIsEnabled )
133 mPrec->setMinimum( minPrecType );
134 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
138void QgsAddAttrDialog::accept()
140 const QString newName = mNameEdit->text().trimmed();
141 if ( mIsShapeFile && newName.compare(
"shape"_L1, Qt::CaseInsensitive ) == 0 )
143 QMessageBox::warning(
this, tr(
"Add Field" ), tr(
"Invalid field name. This field name is reserved and cannot be used." ) );
148 for (
const QString &illegalName : std::as_const( mIllegalFieldNames ) )
150 if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
152 QMessageBox::warning(
this, tr(
"Add Field" ), tr(
"%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
157 if ( mNameEdit->text().isEmpty() )
159 QMessageBox::warning(
this, tr(
"Add Field" ), tr(
"No name specified. Please specify a name to create a new field." ) );
169 u
"idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7"_s.arg( mTypeBox->currentIndex() )
170 .arg( mNameEdit->text() )
171 .arg( mTypeBox->currentData( Qt::UserRole ).toInt() )
172 .arg( mTypeBox->currentData( Qt::UserRole + 1 ).toString() )
173 .arg( mLength->value() )
174 .arg( mPrec->value() )
175 .arg( mCommentEdit->text() ),
181 ( QMetaType::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
182 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
184 mPrec->isEnabled() ? mPrec->value() : 0,
185 mCommentEdit->text(),
186 static_cast<QMetaType::Type
>( mTypeBox->currentData( Qt::UserRole ).toInt() ) == QMetaType::Type::QVariantMap ? QMetaType::Type::QString : QMetaType::Type::UnknownType
189 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 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.
#define QgsDebugMsgLevel(str, level)