23 #include <QMessageBox>
26 : QDialog( parent, fl )
27 , mIsShapeFile( vlayer && vlayer->providerType() == QLatin1String(
"ogr" ) && vlayer->storageType() == QLatin1String(
"ESRI Shapefile" ) )
30 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsAddAttrDialog::mTypeBox_currentIndexChanged );
31 connect( mLength, &QSpinBox::editingFinished,
this, &QgsAddAttrDialog::mLength_editingFinished );
38 for (
int i = 0; i < typelist.size(); i++ )
40 QgsDebugMsg( QStringLiteral(
"name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7" )
41 .arg( typelist[i].mTypeDesc )
42 .arg( typelist[i].mType )
43 .arg( typelist[i].mTypeName )
44 .arg( typelist[i].mMinLen ).arg( typelist[i].mMaxLen )
45 .arg( typelist[i].mMinPrec ).arg( typelist[i].mMaxPrec ) );
48 mTypeBox->setItemData( i,
static_cast<int>( typelist[i].mType ), Qt::UserRole );
49 mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
50 mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
51 mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
52 mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
53 mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
57 mLength->setValue( 10 );
58 mLength->setClearValue( 10 );
60 mPrec->setClearValue( 3 );
61 mTypeBox_currentIndexChanged( 0 );
64 mNameEdit->setMaxLength( 10 );
66 mNameEdit->setFocus();
69 void QgsAddAttrDialog::mTypeBox_currentIndexChanged(
int idx )
71 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
73 mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
74 mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
75 mLength->setVisible( mLength->minimum() < mLength->maximum() );
76 mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
77 if ( mLength->value() < mLength->minimum() )
78 mLength->setValue( mLength->minimum() );
79 if ( mLength->value() > mLength->maximum() )
80 mLength->setValue( mLength->maximum() );
84 void QgsAddAttrDialog::mLength_editingFinished()
89 void QgsAddAttrDialog::setPrecisionMinMax()
91 const int idx = mTypeBox->currentIndex();
92 const int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
93 const int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
94 const bool precisionIsEnabled = minPrecType < maxPrecType;
95 mPrec->setEnabled( precisionIsEnabled );
96 mPrec->setVisible( precisionIsEnabled );
97 mPrecLabel->setVisible( precisionIsEnabled );
102 if ( precisionIsEnabled )
104 mPrec->setMinimum( minPrecType );
105 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
109 void QgsAddAttrDialog::accept()
111 if ( mIsShapeFile && mNameEdit->text().compare( QLatin1String(
"shape" ), Qt::CaseInsensitive ) == 0 )
113 QMessageBox::warning(
this, tr(
"Add Field" ),
114 tr(
"Invalid field name. This field name is reserved and cannot be used." ) );
117 if ( mNameEdit->text().isEmpty() )
119 QMessageBox::warning(
this, tr(
"Add Field" ),
120 tr(
"No name specified. Please specify a name to create a new field." ) );
130 QgsDebugMsg( QStringLiteral(
"idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7" )
131 .arg( mTypeBox->currentIndex() )
132 .arg( mNameEdit->text() )
133 .arg( mTypeBox->currentData( Qt::UserRole ).toInt() )
134 .arg( mTypeBox->currentData( Qt::UserRole + 1 ).toString() )
135 .arg( mLength->value() )
136 .arg( mPrec->value() )
137 .arg( mCommentEdit->text() ) );
141 ( QVariant::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
142 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
144 mPrec->isEnabled() ? mPrec->value() : 0,
145 mCommentEdit->text(),
146 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.
QgsAddAttrDialog(QgsVectorLayer *vlayer, QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
constructor
Encapsulate a field in an attribute table or data source.
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.