Skip to content

Commit

Permalink
fix .dbf fields allowed length and num
Browse files Browse the repository at this point in the history
  • Loading branch information
notguiltyspark committed Sep 23, 2024
1 parent 1afc8d2 commit ac28aa1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 57 deletions.
42 changes: 30 additions & 12 deletions src/gui/qgsnewvectorlayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
mTypeBox->addItem( QgsFields::iconForFieldType( QMetaType::Type::Double ), QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ), "Real" );
mTypeBox->addItem( QgsFields::iconForFieldType( QMetaType::Type::QDate ), QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDate ), "Date" );

mWidth->setValidator( new QIntValidator( 1, 255, this ) );
mPrecision->setValidator( new QIntValidator( 0, 15, this ) );

const Qgis::WkbType geomTypes[] =
Expand Down Expand Up @@ -150,30 +149,48 @@ void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged( int index
void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged( int index )
{
// FIXME: sync with providers/ogr/qgsogrprovider.cpp

// ESRI field width specification is taken from here
// https://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Geoprocessing%20considerations%20for%20shapefile%20output

constexpr int ESRI_TEXT_MAX_WIDTH_INCLUSIVE = 254;
constexpr int ESRI_SHORT_INTEGER_MAX_WIDTH_INCLUSIVE = 4;
// constexpr int ESRI_LONG_INTEGER_MAX_WIDTH_INCLUSIVE = 9;
// constexpr int ESRI_FLOAT_MAX_WIDTH_INCLUSIVE = 13;
constexpr int ESRI_DOUBLE_MAX_WIDTH_INCLUSIVE = 13;
constexpr int ESRI_DATE_MAX_WIDTH_INCLUSIVE = 8;

switch ( index )
{
case 0: // Text data
if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
mWidth->setText( QStringLiteral( "80" ) );
mWidth->setMaximum(ESRI_TEXT_MAX_WIDTH_INCLUSIVE);
if ( mWidth->value() < 1 || mWidth->value() > ESRI_TEXT_MAX_WIDTH_INCLUSIVE )
mWidth->setValue( 80 );
mPrecision->setEnabled( false );
mWidth->setValidator( new QIntValidator( 1, 255, this ) );
break;

case 1: // Whole number
if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
mWidth->setText( QStringLiteral( "10" ) );
mWidth->setMaximum(ESRI_SHORT_INTEGER_MAX_WIDTH_INCLUSIVE);
if ( mWidth->value() < 1 || mWidth->value() > ESRI_SHORT_INTEGER_MAX_WIDTH_INCLUSIVE )
mWidth->setValue( ESRI_SHORT_INTEGER_MAX_WIDTH_INCLUSIVE );
mPrecision->setEnabled( false );
mWidth->setValidator( new QIntValidator( 1, 10, this ) );
break;

case 2: // Decimal number
if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
mWidth->setText( QStringLiteral( "20" ) );
mWidth->setMaximum(ESRI_DOUBLE_MAX_WIDTH_INCLUSIVE);
if ( mWidth->value() < 1 || mWidth->value() > ESRI_DOUBLE_MAX_WIDTH_INCLUSIVE )
mWidth->setValue( ESRI_DOUBLE_MAX_WIDTH_INCLUSIVE );

if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 15 )
mPrecision->setText( QStringLiteral( "6" ) );

mPrecision->setEnabled( true );
mWidth->setValidator( new QIntValidator( 1, 20, this ) );
break;

case 3: // Date
mWidth->setMaximum(ESRI_DATE_MAX_WIDTH_INCLUSIVE);
if ( mWidth->value() < 1 || mWidth->value() > ESRI_DATE_MAX_WIDTH_INCLUSIVE )
mWidth->setValue( ESRI_DATE_MAX_WIDTH_INCLUSIVE );
mPrecision->setEnabled( false );
break;

default:
Expand Down Expand Up @@ -301,7 +318,8 @@ void QgsNewVectorLayerDialog::setFilename( const QString &filename )

void QgsNewVectorLayerDialog::checkOk()
{
const bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 && mGeometryTypeBox->currentIndex() != -1 );
constexpr int MAX_FIELD_NUM = 255;
const bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 && mGeometryTypeBox->currentIndex() != -1 && mAttributeView->topLevelItemCount() < MAX_FIELD_NUM );
mOkButton->setEnabled( ok );
}

Expand Down
94 changes: 49 additions & 45 deletions src/ui/qgsnewvectorlayerdialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,16 @@
<string>New Field</string>
</property>
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QLabel" name="textLabel1">
<item row="2" column="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Name</string>
<string>Precision</string>
</property>
<property name="buddy">
<cstring>mNameEdit</cstring>
<cstring>mPrecision</cstring>
</property>
</widget>
</item>
<item row="0" column="1" colspan="4">
<widget class="QLineEdit" name="mNameEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="textLabel2">
<property name="text">
Expand All @@ -184,36 +181,7 @@
</property>
</widget>
</item>
<item row="1" column="1" colspan="4">
<widget class="QComboBox" name="mTypeBox"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Length</string>
</property>
<property name="buddy">
<cstring>mWidth</cstring>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QLineEdit" name="mWidth"/>
</item>
<item row="2" column="3">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Precision</string>
</property>
<property name="buddy">
<cstring>mPrecision</cstring>
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QLineEdit" name="mPrecision"/>
</item>
<item row="4" column="4">
<item row="4" column="3">
<widget class="QToolButton" name="mAddAttributeButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
Expand All @@ -239,7 +207,33 @@
</property>
</widget>
</item>
<item row="4" column="0" colspan="4">
<item row="0" column="0">
<widget class="QLabel" name="textLabel1">
<property name="text">
<string>Name</string>
</property>
<property name="buddy">
<cstring>mNameEdit</cstring>
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QLineEdit" name="mPrecision"/>
</item>
<item row="0" column="1" colspan="3">
<widget class="QLineEdit" name="mNameEdit"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Length</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="3">
<widget class="QComboBox" name="mTypeBox"/>
</item>
<item row="4" column="0" colspan="3">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
Expand All @@ -252,6 +246,16 @@
</property>
</spacer>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="mWidth">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>254</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -352,7 +356,7 @@
</widget>
</item>
<item row="5" column="1" colspan="3">
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector" native="true">
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector">
<property name="minimumSize">
<size>
<width>0</width>
Expand Down Expand Up @@ -399,7 +403,7 @@
</widget>
</item>
<item row="0" column="1" colspan="3">
<widget class="QgsFileWidget" name="mFileName" native="true"/>
<widget class="QgsFileWidget" name="mFileName"/>
</item>
</layout>
</item>
Expand All @@ -418,15 +422,15 @@
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsProjectionSelectionWidget</class>
<class>QgsFileWidget</class>
<extends>QWidget</extends>
<header location="global">qgsprojectionselectionwidget.h</header>
<header>qgsfilewidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFileWidget</class>
<class>QgsProjectionSelectionWidget</class>
<extends>QWidget</extends>
<header>qgsfilewidget.h</header>
<header>qgsprojectionselectionwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
Expand All @@ -440,7 +444,6 @@
<tabstop>mCrsSelector</tabstop>
<tabstop>mNameEdit</tabstop>
<tabstop>mTypeBox</tabstop>
<tabstop>mWidth</tabstop>
<tabstop>mPrecision</tabstop>
<tabstop>mAddAttributeButton</tabstop>
<tabstop>mAttributeView</tabstop>
Expand All @@ -450,6 +453,7 @@
</tabstops>
<resources>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
</resources>
<connections>
<connection>
Expand Down

0 comments on commit ac28aa1

Please sign in to comment.