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 );
39 for (
int i = 0; i < typelist.size(); i++ )
41 QgsDebugMsgLevel( QStringLiteral(
"name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7" )
42 .arg( typelist[i].mTypeDesc )
43 .arg( typelist[i].mType )
44 .arg( typelist[i].mTypeName )
45 .arg( typelist[i].mMinLen ).arg( typelist[i].mMaxLen )
46 .arg( typelist[i].mMinPrec ).arg( typelist[i].mMaxPrec ), 2 );
49 typelist[i].mTypeDesc );
50 mTypeBox->setItemData( i,
static_cast<int>( typelist[i].mType ), Qt::UserRole );
51 mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
52 mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
53 mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
54 mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
55 mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
59 mLength->setValue( 10 );
60 mLength->setClearValue( 10 );
62 mPrec->setClearValue( 3 );
63 mTypeBox_currentIndexChanged( 0 );
66 mNameEdit->setMaxLength( 10 );
68 mNameEdit->setFocus();
73 mIllegalFieldNames = names;
76void QgsAddAttrDialog::mTypeBox_currentIndexChanged(
int idx )
78 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
80 mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
81 mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
82 mLength->setVisible( mLength->minimum() < mLength->maximum() );
83 mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
84 if ( mLength->value() < mLength->minimum() )
85 mLength->setValue( mLength->minimum() );
86 if ( mLength->value() > mLength->maximum() )
87 mLength->setValue( mLength->maximum() );
91void QgsAddAttrDialog::mLength_editingFinished()
96void QgsAddAttrDialog::setPrecisionMinMax()
98 const int idx = mTypeBox->currentIndex();
99 const int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
100 const int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
101 const bool precisionIsEnabled = minPrecType < maxPrecType;
102 mPrec->setEnabled( precisionIsEnabled );
103 mPrec->setVisible( precisionIsEnabled );
104 mPrecLabel->setVisible( precisionIsEnabled );
109 if ( precisionIsEnabled )
111 mPrec->setMinimum( minPrecType );
112 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
116void QgsAddAttrDialog::accept()
118 const QString newName = mNameEdit->text().trimmed();
119 if ( mIsShapeFile && newName.compare( QLatin1String(
"shape" ), Qt::CaseInsensitive ) == 0 )
121 QMessageBox::warning(
this, tr(
"Add Field" ),
122 tr(
"Invalid field name. This field name is reserved and cannot be used." ) );
127 for (
const QString &illegalName : std::as_const( mIllegalFieldNames ) )
129 if ( newName.compare( illegalName, Qt::CaseInsensitive ) == 0 )
131 QMessageBox::warning(
this, tr(
"Add Field" ),
132 tr(
"%1 is an illegal field name for this format and cannot be used." ).arg( newName ) );
137 if ( mNameEdit->text().isEmpty() )
139 QMessageBox::warning(
this, tr(
"Add Field" ),
140 tr(
"No name specified. Please specify a name to create a new field." ) );
150 QgsDebugMsgLevel( QStringLiteral(
"idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7" )
151 .arg( mTypeBox->currentIndex() )
152 .arg( mNameEdit->text() )
153 .arg( mTypeBox->currentData( Qt::UserRole ).toInt() )
154 .arg( mTypeBox->currentData( Qt::UserRole + 1 ).toString() )
155 .arg( mLength->value() )
156 .arg( mPrec->value() )
157 .arg( mCommentEdit->text() ), 2 );
161 ( QVariant::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
162 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
164 mPrec->isEnabled() ? mPrec->value() : 0,
165 mCommentEdit->text(),
166 static_cast<QVariant::Type
>( mTypeBox->currentData( Qt::UserRole ).toInt() ) == QVariant::Map ? QVariant::String : QVariant::Invalid
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.
static QIcon iconForFieldType(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid, const QString &typeString=QString())
Returns an icon corresponding to a field type.
QList< QgsVectorDataProvider::NativeType > nativeTypes() const
Returns the names of the supported types.
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)