28 #include "qgsogrprovider.h"
34 #include <QPushButton>
36 #include <QFileDialog>
37 #include <QMessageBox>
40 : QDialog( parent, fl )
45 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewVectorLayerDialog::mAddAttributeButton_clicked );
46 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked );
47 connect( mFileFormatComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged );
48 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged );
49 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewVectorLayerDialog::showHelp );
59 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
60 mPrecision->setValidator(
new QIntValidator( 0, 15,
this ) );
71 for (
const auto type : geomTypes )
73 mGeometryTypeBox->setCurrentIndex( -1 );
75 mOkButton = buttonBox->button( QDialogButtonBox::Ok );
76 mOkButton->setEnabled(
false );
78 mFileFormatComboBox->addItem( tr(
"ESRI Shapefile" ),
"ESRI Shapefile" );
82 mFileFormatComboBox->addItem( tr(
"Comma Separated Value" ),
"Comma Separated Value" );
83 mFileFormatComboBox->addItem( tr(
"GML" ),
"GML" );
84 mFileFormatComboBox->addItem( tr(
"Mapinfo File" ),
"Mapinfo File" );
86 if ( mFileFormatComboBox->count() == 1 )
88 mFileFormatComboBox->setVisible(
false );
89 mFileFormatLabel->setVisible(
false );
92 mCrsSelector->setShowAccuracyWarnings(
true );
94 mFileFormatComboBox->setCurrentIndex( 0 );
99 const QString enc =
QgsSettings().
value( QStringLiteral(
"/UI/encoding" ),
"System" ).toString();
103 int encindex = mFileEncoding->findText( enc );
106 mFileEncoding->insertItem( 0, enc );
109 mFileEncoding->setCurrentIndex( encindex );
111 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << QStringLiteral(
"id" ) << QStringLiteral(
"Integer" ) << QStringLiteral(
"10" ) << QString() ) );
112 connect( mNameEdit, &QLineEdit::textChanged,
this, &QgsNewVectorLayerDialog::nameChanged );
113 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewVectorLayerDialog::selectionChanged );
114 connect( mGeometryTypeBox,
static_cast<void( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ](
int )
120 mAddAttributeButton->setEnabled(
false );
121 mRemoveAttributeButton->setEnabled(
false );
125 mFileName->setConfirmOverwrite(
false );
126 mFileName->setDialogTitle( tr(
"Save Layer As" ) );
128 mFileName->setDefaultRoot( settings.
value( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
132 const QFileInfo tmplFileInfo( mFileName->filePath() );
133 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
138 void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged(
int index )
141 if ( mFileFormatComboBox->currentText() == tr(
"ESRI Shapefile" ) )
142 mNameEdit->setMaxLength( 10 );
144 mNameEdit->setMaxLength( 32767 );
147 void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged(
int index )
153 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
154 mWidth->setText( QStringLiteral(
"80" ) );
155 mPrecision->setEnabled(
false );
156 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
160 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
161 mWidth->setText( QStringLiteral(
"10" ) );
162 mPrecision->setEnabled(
false );
163 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
167 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
168 mWidth->setText( QStringLiteral(
"20" ) );
169 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 15 )
170 mPrecision->setText( QStringLiteral(
"6" ) );
172 mPrecision->setEnabled(
true );
173 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
177 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
186 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
188 if ( mGeometryWithZRadioButton->isChecked() )
191 if ( mGeometryWithMRadioButton->isChecked() )
199 return mCrsSelector->crs();
204 mCrsSelector->setCrs(
crs );
207 void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
209 const QString myName = mNameEdit->text();
210 const QString myWidth = mWidth->text();
211 const QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : QString();
213 const QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
214 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
219 void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
221 delete mAttributeView->currentItem();
227 QTreeWidgetItemIterator it( mAttributeView );
230 QTreeWidgetItem *item = *it;
231 const QString type = QStringLiteral(
"%1;%2;%3" ).arg( item->text( 1 ), item->text( 2 ), item->text( 3 ) );
232 at.push_back( qMakePair( item->text( 0 ), type ) );
233 QgsDebugMsg( QStringLiteral(
"appending %1//%2" ).arg( item->text( 0 ), type ) );
241 QString myType = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
247 return mFileEncoding->currentText();
250 void QgsNewVectorLayerDialog::nameChanged(
const QString &name )
252 mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
255 void QgsNewVectorLayerDialog::selectionChanged()
257 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
262 return mFileName->filePath();
270 void QgsNewVectorLayerDialog::checkOk()
272 const bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 && mGeometryTypeBox->currentIndex() != -1 );
273 mOkButton->setEnabled( ok );
281 if ( res.isEmpty() && error.isEmpty() )
286 void QgsNewVectorLayerDialog::updateExtension()
291 if ( fileformat == QLatin1String(
"ESRI Shapefile" ) )
295 fileName = fileName.replace( fileName.lastIndexOf( QLatin1String(
".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String(
".shp" ) );
300 fileName = fileName.replace( fileName.lastIndexOf( QLatin1String(
".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String(
".dbf" ) );
312 if ( QFile::exists(
filename() ) && QMessageBox::warning(
this, tr(
"New ShapeFile Layer" ), tr(
"The layer already exists. Are you sure you want to overwrite the existing file?" ),
313 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
321 errorMessage.clear();
324 if ( !initialPath.isEmpty() )
326 if ( geomDialog.exec() == QDialog::Rejected )
333 QString fileName = geomDialog.
filename();
336 QgsDebugMsg( QStringLiteral(
"New file format will be: %1" ).arg( fileformat ) );
342 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
343 settings.
setValue( QStringLiteral(
"UI/encoding" ), enc );
349 const bool success = QgsOgrProviderUtils::createEmptyDataSource( fileName, fileformat, enc, geometrytype,
attributes, srs, errorMessage );
357 errorMessage = QObject::tr(
"Geometry type not recognised" );
368 void QgsNewVectorLayerDialog::showHelp()
370 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-shapefile-layer" ) );