Skip to content

Commit 231a2df

Browse files
alexbruynyalldawson
authored andcommittedMay 18, 2020
sensible defaults for float precision in the Add field dialog, same as
in Field Calculator (fix #16581)
1 parent a8dc87f commit 231a2df

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed
 

‎src/gui/qgsaddattrdialog.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt:
3535

3636
//fill data types into the combo box
3737
const QList< QgsVectorDataProvider::NativeType > &typelist = vlayer->dataProvider()->nativeTypes();
38-
3938
for ( int i = 0; i < typelist.size(); i++ )
4039
{
4140
QgsDebugMsg( QStringLiteral( "name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7" )
@@ -45,7 +44,7 @@ QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt:
4544
.arg( typelist[i].mMinLen ).arg( typelist[i].mMaxLen )
4645
.arg( typelist[i].mMinPrec ).arg( typelist[i].mMaxPrec ) );
4746

48-
mTypeBox->addItem( typelist[i].mTypeDesc );
47+
whileBlocking( mTypeBox )->addItem( typelist[i].mTypeDesc );
4948
mTypeBox->setItemData( i, static_cast<int>( typelist[i].mType ), Qt::UserRole );
5049
mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
5150
mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
@@ -54,6 +53,9 @@ QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt:
5453
mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
5554
}
5655

56+
//default values for field width and precision
57+
mLength->setValue( 10 );
58+
mPrec->setValue( 3 );
5759
mTypeBox_currentIndexChanged( 0 );
5860

5961
if ( mIsShapeFile )
@@ -87,10 +89,18 @@ void QgsAddAttrDialog::setPrecisionMinMax()
8789
int idx = mTypeBox->currentIndex();
8890
int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
8991
int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
90-
mPrec->setVisible( minPrecType < maxPrecType );
91-
mPrecLabel->setVisible( minPrecType < maxPrecType );
92-
mPrec->setMinimum( minPrecType );
93-
mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
92+
bool precisionIsEnabled = minPrecType < maxPrecType;
93+
mPrec->setVisible( precisionIsEnabled );
94+
mPrecLabel->setVisible( precisionIsEnabled );
95+
96+
// Do not set min/max if it's disabled or we'll loose the default value,
97+
// see https://github.com/qgis/QGIS/issues/26880 - QGIS saves integer field when
98+
// I create a new real field through field calculator (Update field works as intended)
99+
if ( precisionIsEnabled )
100+
{
101+
mPrec->setMinimum( minPrecType );
102+
mPrec->setMaximum( std::max( minPrecType, std::min( maxPrecType, mLength->value() ) ) );
103+
}
94104
}
95105

96106
void QgsAddAttrDialog::accept()

0 commit comments

Comments
 (0)
Please sign in to comment.