24 #include <QMessageBox>
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();
71 void QgsAddAttrDialog::mTypeBox_currentIndexChanged(
int idx )
73 mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
75 mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
76 mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
77 mLength->setVisible( mLength->minimum() < mLength->maximum() );
78 mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
79 if ( mLength->value() < mLength->minimum() )
80 mLength->setValue( mLength->minimum() );
81 if ( mLength->value() > mLength->maximum() )
82 mLength->setValue( mLength->maximum() );
86 void QgsAddAttrDialog::mLength_editingFinished()
91 void QgsAddAttrDialog::setPrecisionMinMax()
93 const int idx = mTypeBox->currentIndex();
94 const int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
95 const int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
96 const bool precisionIsEnabled = minPrecType < maxPrecType;
97 mPrec->setEnabled( precisionIsEnabled );
98 mPrec->setVisible( precisionIsEnabled );
99 mPrecLabel->setVisible( precisionIsEnabled );
104 if ( precisionIsEnabled )
106 mPrec->setMinimum( minPrecType );
107 mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
111 void QgsAddAttrDialog::accept()
113 if ( mIsShapeFile && mNameEdit->text().compare( QLatin1String(
"shape" ), Qt::CaseInsensitive ) == 0 )
115 QMessageBox::warning(
this, tr(
"Add Field" ),
116 tr(
"Invalid field name. This field name is reserved and cannot be used." ) );
119 if ( mNameEdit->text().isEmpty() )
121 QMessageBox::warning(
this, tr(
"Add Field" ),
122 tr(
"No name specified. Please specify a name to create a new field." ) );
132 QgsDebugMsgLevel( QStringLiteral(
"idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7" )
133 .arg( mTypeBox->currentIndex() )
134 .arg( mNameEdit->text() )
135 .arg( mTypeBox->currentData( Qt::UserRole ).toInt() )
136 .arg( mTypeBox->currentData( Qt::UserRole + 1 ).toString() )
137 .arg( mLength->value() )
138 .arg( mPrec->value() )
139 .arg( mCommentEdit->text() ), 2 );
143 ( QVariant::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
144 mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
146 mPrec->isEnabled() ? mPrec->value() : 0,
147 mCommentEdit->text(),
148 static_cast<QVariant::Type
>( mTypeBox->currentData( Qt::UserRole ).toInt() ) == QVariant::Map ? QVariant::String : QVariant::Invalid