29 #include "qgsogrprovider.h"
32 #include <QPushButton>
34 #include <QFileDialog>
35 #include <QMessageBox>
38 : QDialog( parent, fl )
43 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewVectorLayerDialog::mAddAttributeButton_clicked );
44 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked );
45 connect( mFileFormatComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged );
46 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged );
47 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewVectorLayerDialog::showHelp );
57 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
58 mPrecision->setValidator(
new QIntValidator( 0, 15,
this ) );
69 for (
const auto type : geomTypes )
71 mGeometryTypeBox->setCurrentIndex( -1 );
73 mOkButton = buttonBox->button( QDialogButtonBox::Ok );
74 mOkButton->setEnabled(
false );
76 mFileFormatComboBox->addItem( tr(
"ESRI Shapefile" ),
"ESRI Shapefile" );
80 mFileFormatComboBox->addItem( tr(
"Comma Separated Value" ),
"Comma Separated Value" );
81 mFileFormatComboBox->addItem( tr(
"GML" ),
"GML" );
82 mFileFormatComboBox->addItem( tr(
"Mapinfo File" ),
"Mapinfo File" );
84 if ( mFileFormatComboBox->count() == 1 )
86 mFileFormatComboBox->setVisible(
false );
87 mFileFormatLabel->setVisible(
false );
90 mFileFormatComboBox->setCurrentIndex( 0 );
95 QString enc =
QgsSettings().
value( QStringLiteral(
"/UI/encoding" ),
"System" ).toString();
99 int encindex = mFileEncoding->findText( enc );
102 mFileEncoding->insertItem( 0, enc );
105 mFileEncoding->setCurrentIndex( encindex );
107 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << QStringLiteral(
"id" ) << QStringLiteral(
"Integer" ) << QStringLiteral(
"10" ) << QString() ) );
108 connect( mNameEdit, &QLineEdit::textChanged,
this, &QgsNewVectorLayerDialog::nameChanged );
109 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewVectorLayerDialog::selectionChanged );
110 connect( mGeometryTypeBox,
static_cast<void( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [ = ](
int index )
112 QString fileName = mFileName->filePath();
113 if ( !fileName.isEmpty() )
117 fileName = fileName.replace( fileName.lastIndexOf( QLatin1String(
".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String(
".dbf" ) );
121 fileName = fileName.replace( fileName.lastIndexOf( QLatin1String(
".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String(
".shp" ) );
123 mFileName->setFilePath( fileName );
128 mAddAttributeButton->setEnabled(
false );
129 mRemoveAttributeButton->setEnabled(
false );
133 mFileName->setConfirmOverwrite(
false );
134 mFileName->setDialogTitle( tr(
"Save Layer As" ) );
136 mFileName->setDefaultRoot( settings.
value( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
140 QFileInfo tmplFileInfo( mFileName->filePath() );
141 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
146 void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged(
int index )
149 if ( mFileFormatComboBox->currentText() == tr(
"ESRI Shapefile" ) )
150 mNameEdit->setMaxLength( 10 );
152 mNameEdit->setMaxLength( 32767 );
155 void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged(
int index )
161 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
162 mWidth->setText( QStringLiteral(
"80" ) );
163 mPrecision->setEnabled(
false );
164 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
168 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
169 mWidth->setText( QStringLiteral(
"10" ) );
170 mPrecision->setEnabled(
false );
171 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
175 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
176 mWidth->setText( QStringLiteral(
"20" ) );
177 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 15 )
178 mPrecision->setText( QStringLiteral(
"6" ) );
180 mPrecision->setEnabled(
true );
181 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
185 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
194 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
196 if ( mGeometryWithZRadioButton->isChecked() )
199 if ( mGeometryWithMRadioButton->isChecked() )
207 return mCrsSelector->crs();
212 mCrsSelector->setCrs(
crs );
215 void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
217 QString myName = mNameEdit->text();
218 QString myWidth = mWidth->text();
219 QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : QString();
221 QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
222 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
227 void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
229 delete mAttributeView->currentItem();
235 QTreeWidgetItemIterator it( mAttributeView );
238 QTreeWidgetItem *item = *it;
239 QString type = QStringLiteral(
"%1;%2;%3" ).arg( item->text( 1 ), item->text( 2 ), item->text( 3 ) );
240 at.push_back( qMakePair( item->text( 0 ), type ) );
241 QgsDebugMsg( QStringLiteral(
"appending %1//%2" ).arg( item->text( 0 ), type ) );
249 QString myType = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
255 return mFileEncoding->currentText();
258 void QgsNewVectorLayerDialog::nameChanged(
const QString &name )
260 mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
263 void QgsNewVectorLayerDialog::selectionChanged()
265 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
270 return mFileName->filePath();
278 void QgsNewVectorLayerDialog::checkOk()
280 bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 && mGeometryTypeBox->currentIndex() != -1 );
281 mOkButton->setEnabled( ok );
289 if ( res.isEmpty() && error.isEmpty() )
296 errorMessage.clear();
299 if ( !initialPath.isEmpty() )
301 if ( geomDialog.exec() == QDialog::Rejected )
306 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?" ),
307 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
313 QgsDebugMsg( QStringLiteral(
"New file format will be: %1" ).arg( fileformat ) );
319 QString fileName = geomDialog.
filename();
320 if ( fileformat == QLatin1String(
"ESRI Shapefile" ) && ( geometrytype !=
QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String(
".shp" ), Qt::CaseInsensitive ) ) )
321 fileName += QLatin1String(
".shp" );
322 else if ( fileformat == QLatin1String(
"ESRI Shapefile" ) && ( geometrytype ==
QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String(
".dbf" ), Qt::CaseInsensitive ) ) )
324 if ( fileName.endsWith( QLatin1String(
".shp" ), Qt::CaseInsensitive ) )
325 fileName = fileName.replace( fileName.lastIndexOf( QLatin1String(
".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String(
".dbf" ) );
327 fileName += QLatin1String(
".dbf" );
330 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
331 settings.
setValue( QStringLiteral(
"UI/encoding" ), enc );
337 bool success = QgsOgrProviderUtils::createEmptyDataSource( fileName, fileformat, enc, geometrytype,
attributes, srs, errorMessage );
345 errorMessage = QObject::tr(
"Geometry type not recognised" );
356 void QgsNewVectorLayerDialog::showHelp()
358 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-shapefile-layer" ) );
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
This class represents a coordinate reference system (CRS).
static QgsGui * instance()
Returns a pointer to the singleton instance.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
static QIcon iconForWkbType(QgsWkbTypes::Type type)
Returns the icon for a vector layer whose geometry type is provided.
static Q_DECL_DEPRECATED QString runAndCreateLayer(QWidget *parent=nullptr, QString *enc=nullptr, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), const QString &initialPath=QString())
Runs the dialog and creates a layer matching the dialog parameters.
void attributes(QList< QPair< QString, QString > > &at) const
Appends the chosen attribute names and types to at.
QgsCoordinateReferenceSystem crs() const
Returns the selected CRS for the new layer.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs value for the new layer in the dialog.
QString filename() const
Returns the name for the new layer.
QString selectedFileFormat() const
Returns the file format for storage.
QString selectedFileEncoding() const
Returns the file format for storage.
QgsNewVectorLayerDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
New dialog constructor.
QgsWkbTypes::Type selectedType() const
Returns the selected geometry type.
static QString execAndCreateLayer(QString &errorMessage, QWidget *parent=nullptr, const QString &initialPath=QString(), QString *encoding=nullptr, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Runs the dialog and creates a layer matching the dialog parameters.
void setFilename(const QString &filename)
Sets the initial file name to show in the dialog.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QStringList availableEncodings()
Returns a list of available encodings.
static QString filterForDriver(const QString &driverName)
Creates a filter for an OGR driver key.
static QString translatedDisplayString(Type type) SIP_HOLDGIL
Returns a translated display string type for a WKB type, e.g., the geometry name used in WKT geometry...
Type
The WKB type describes the number of dimensions a geometry has.
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
const QgsCoordinateReferenceSystem & crs