28 #include "qgsogrprovider.h"
31 #include <QPushButton>
33 #include <QFileDialog>
34 #include <QMessageBox>
37 : QDialog( parent, fl )
42 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewVectorLayerDialog::mAddAttributeButton_clicked );
43 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked );
44 connect( mFileFormatComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged );
45 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged );
46 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewVectorLayerDialog::showHelp );
56 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
57 mPrecision->setValidator(
new QIntValidator( 0, 15,
this ) );
64 mGeometryTypeBox->setCurrentIndex( -1 );
66 mOkButton = buttonBox->button( QDialogButtonBox::Ok );
67 mOkButton->setEnabled(
false );
69 mFileFormatComboBox->addItem( tr(
"ESRI Shapefile" ),
"ESRI Shapefile" );
73 mFileFormatComboBox->addItem( tr(
"Comma Separated Value" ),
"Comma Separated Value" );
74 mFileFormatComboBox->addItem( tr(
"GML" ),
"GML" );
75 mFileFormatComboBox->addItem( tr(
"Mapinfo File" ),
"Mapinfo File" );
77 if ( mFileFormatComboBox->count() == 1 )
79 mFileFormatComboBox->setVisible(
false );
80 mFileFormatLabel->setVisible(
false );
83 mFileFormatComboBox->setCurrentIndex( 0 );
88 QString enc =
QgsSettings().
value( QStringLiteral(
"/UI/encoding" ),
"System" ).toString();
92 int encindex = mFileEncoding->findText( enc );
95 mFileEncoding->insertItem( 0, enc );
98 mFileEncoding->setCurrentIndex( encindex );
100 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << QStringLiteral(
"id" ) << QStringLiteral(
"Integer" ) << QStringLiteral(
"10" ) << QString() ) );
101 connect( mNameEdit, &QLineEdit::textChanged,
this, &QgsNewVectorLayerDialog::nameChanged );
102 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewVectorLayerDialog::selectionChanged );
103 connect( mGeometryTypeBox,
static_cast<void( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ](
int index )
105 QString fileName = mFileName->filePath();
106 if ( !fileName.isEmpty() )
110 fileName = fileName.replace( fileName.lastIndexOf( QLatin1String(
".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String(
".dbf" ) );
114 fileName = fileName.replace( fileName.lastIndexOf( QLatin1String(
".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String(
".shp" ) );
116 mFileName->setFilePath( fileName );
121 mAddAttributeButton->setEnabled(
false );
122 mRemoveAttributeButton->setEnabled(
false );
126 mFileName->setConfirmOverwrite(
false );
127 mFileName->setDialogTitle( tr(
"Save Layer As" ) );
129 mFileName->setDefaultRoot( settings.
value( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
133 QFileInfo tmplFileInfo( mFileName->filePath() );
134 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
139 void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged(
int index )
142 if ( mFileFormatComboBox->currentText() == tr(
"ESRI Shapefile" ) )
143 mNameEdit->setMaxLength( 10 );
145 mNameEdit->setMaxLength( 32767 );
148 void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged(
int index )
154 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
155 mWidth->setText( QStringLiteral(
"80" ) );
156 mPrecision->setEnabled(
false );
157 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
161 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
162 mWidth->setText( QStringLiteral(
"10" ) );
163 mPrecision->setEnabled(
false );
164 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
168 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
169 mWidth->setText( QStringLiteral(
"20" ) );
170 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 15 )
171 mPrecision->setText( QStringLiteral(
"6" ) );
173 mPrecision->setEnabled(
true );
174 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
178 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
187 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
189 if ( mGeometryWithZRadioButton->isChecked() )
192 if ( mGeometryWithMRadioButton->isChecked() )
200 return mCrsSelector->crs();
205 mCrsSelector->setCrs(
crs );
208 void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
210 QString myName = mNameEdit->text();
211 QString myWidth = mWidth->text();
212 QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : QString();
214 QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
215 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
220 void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
222 delete mAttributeView->currentItem();
228 QTreeWidgetItemIterator it( mAttributeView );
231 QTreeWidgetItem *item = *it;
232 QString type = QStringLiteral(
"%1;%2;%3" ).arg( item->text( 1 ), item->text( 2 ), item->text( 3 ) );
233 at.push_back( qMakePair( item->text( 0 ), type ) );
234 QgsDebugMsg( QStringLiteral(
"appending %1//%2" ).arg( item->text( 0 ), type ) );
242 QString myType = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
248 return mFileEncoding->currentText();
251 void QgsNewVectorLayerDialog::nameChanged(
const QString &name )
253 mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
256 void QgsNewVectorLayerDialog::selectionChanged()
258 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
263 return mFileName->filePath();
271 void QgsNewVectorLayerDialog::checkOk()
273 bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 && mGeometryTypeBox->currentIndex() != -1 );
274 mOkButton->setEnabled( ok );
282 if ( res.isEmpty() && error.isEmpty() )
289 errorMessage.clear();
292 if ( !initialPath.isEmpty() )
294 if ( geomDialog.exec() == QDialog::Rejected )
299 if ( QFile::exists( geomDialog.
filename() ) && QMessageBox::warning( parent, tr(
"New ShapeFile Layer" ), tr(
"The layer already exists. Are you sure you want to overwrite the existing file?" ),
300 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
306 QgsDebugMsg( QStringLiteral(
"New file format will be: %1" ).arg( fileformat ) );
312 QString fileName = geomDialog.
filename();
313 if ( fileformat == QLatin1String(
"ESRI Shapefile" ) && ( geometrytype !=
QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String(
".shp" ), Qt::CaseInsensitive ) ) )
314 fileName += QLatin1String(
".shp" );
315 else if ( fileformat == QLatin1String(
"ESRI Shapefile" ) && ( geometrytype ==
QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String(
".dbf" ), Qt::CaseInsensitive ) ) )
317 if ( fileName.endsWith( QLatin1String(
".shp" ), Qt::CaseInsensitive ) )
318 fileName = fileName.replace( fileName.lastIndexOf( QLatin1String(
".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String(
".dbf" ) );
320 fileName += QLatin1String(
".dbf" );
323 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
324 settings.
setValue( QStringLiteral(
"UI/encoding" ), enc );
330 bool success = QgsOgrProviderUtils::createEmptyDataSource( fileName, fileformat, enc, geometrytype,
attributes, srs, errorMessage );
338 errorMessage = QObject::tr(
"Geometry type not recognised" );
349 void QgsNewVectorLayerDialog::showHelp()
351 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-shapefile-layer" ) );