29 #include <QPushButton> 32 #include <QFileDialog> 33 #include <QMessageBox> 36 : QDialog( parent, fl )
40 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewVectorLayerDialog::mAddAttributeButton_clicked );
41 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked );
42 connect( mFileFormatComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged );
43 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged );
44 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewVectorLayerDialog::showHelp );
47 restoreGeometry( settings.
value( QStringLiteral(
"Windows/NewVectorLayer/geometry" ) ).toByteArray() );
57 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
58 mPrecision->setValidator(
new QIntValidator( 0, 15,
this ) );
65 mOkButton = buttonBox->button( QDialogButtonBox::Ok );
66 mOkButton->setEnabled(
false );
68 mFileFormatComboBox->addItem( tr(
"ESRI Shapefile" ),
"ESRI Shapefile" );
72 mFileFormatComboBox->addItem( tr(
"Comma Separated Value" ),
"Comma Separated Value" );
73 mFileFormatComboBox->addItem( tr(
"GML" ),
"GML" );
74 mFileFormatComboBox->addItem( tr(
"Mapinfo File" ),
"Mapinfo File" );
76 if ( mFileFormatComboBox->count() == 1 )
78 mFileFormatComboBox->setVisible(
false );
79 mFileFormatLabel->setVisible(
false );
82 mFileFormatComboBox->setCurrentIndex( 0 );
87 QString enc =
QgsSettings().
value( QStringLiteral(
"/UI/encoding" ),
"System" ).toString();
91 int encindex = mFileEncoding->findText( enc );
94 mFileEncoding->insertItem( 0, enc );
97 mFileEncoding->setCurrentIndex( encindex );
99 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << QStringLiteral(
"id" ) << QStringLiteral(
"Integer" ) << QStringLiteral(
"10" ) << QString() ) );
100 connect( mNameEdit, &QLineEdit::textChanged,
this, &QgsNewVectorLayerDialog::nameChanged );
101 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewVectorLayerDialog::selectionChanged );
103 mAddAttributeButton->setEnabled(
false );
104 mRemoveAttributeButton->setEnabled(
false );
108 mFileName->setConfirmOverwrite(
false );
109 mFileName->setDialogTitle( tr(
"Save Layer As" ) );
110 mFileName->setDefaultRoot( settings.
value( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
114 QFileInfo tmplFileInfo( mFileName->filePath() );
115 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
126 void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged(
int index )
129 if ( mFileFormatComboBox->currentText() == tr(
"ESRI Shapefile" ) )
130 mNameEdit->setMaxLength( 10 );
132 mNameEdit->setMaxLength( 32767 );
135 void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged(
int index )
141 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
142 mWidth->setText( QStringLiteral(
"80" ) );
143 mPrecision->setEnabled(
false );
144 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
148 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
149 mWidth->setText( QStringLiteral(
"10" ) );
150 mPrecision->setEnabled(
false );
151 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
155 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
156 mWidth->setText( QStringLiteral(
"20" ) );
157 mPrecision->setEnabled(
true );
158 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
162 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
171 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
173 if ( mGeometryWithZCheckBox->isChecked() )
176 if ( mGeometryWithMCheckBox->isChecked() )
184 return mCrsSelector->crs();
189 mCrsSelector->setCrs( crs );
192 void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
194 QString myName = mNameEdit->text();
195 QString myWidth = mWidth->text();
196 QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : QString();
198 QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
199 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
204 void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
206 delete mAttributeView->currentItem();
212 QTreeWidgetItemIterator it( mAttributeView );
215 QTreeWidgetItem *item = *it;
216 QString type = QStringLiteral(
"%1;%2;%3" ).arg( item->text( 1 ), item->text( 2 ), item->text( 3 ) );
217 at.push_back( qMakePair( item->text( 0 ), type ) );
218 QgsDebugMsg( QStringLiteral(
"appending %1//%2" ).arg( item->text( 0 ), type ) );
226 QString myType = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
232 return mFileEncoding->currentText();
235 void QgsNewVectorLayerDialog::nameChanged(
const QString &name )
237 mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
240 void QgsNewVectorLayerDialog::selectionChanged()
242 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
247 return mFileName->filePath();
252 mFileName->setFilePath( filename );
255 void QgsNewVectorLayerDialog::checkOk()
257 bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 );
258 mOkButton->setEnabled( ok );
266 if ( res.isEmpty() && error.isEmpty() )
273 errorMessage.clear();
276 if ( !initialPath.isEmpty() )
278 if ( geomDialog.exec() == QDialog::Rejected )
283 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?" ),
284 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
290 QgsDebugMsg( QStringLiteral(
"New file format will be: %1" ).arg( fileformat ) );
296 QString fileName = geomDialog.
filename();
297 if ( fileformat == QLatin1String(
"ESRI Shapefile" ) && !fileName.endsWith( QLatin1String(
".shp" ), Qt::CaseInsensitive ) )
298 fileName += QLatin1String(
".shp" );
300 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
301 settings.
setValue( QStringLiteral(
"UI/encoding" ), enc );
305 QString ogrlib = pReg->
library( QStringLiteral(
"ogr" ) );
307 QLibrary *myLib =
new QLibrary( ogrlib );
308 bool loaded = myLib->load();
311 QgsDebugMsg( QStringLiteral(
"ogr provider loaded" ) );
313 typedef bool ( *createEmptyDataSourceProc )(
const QString &,
const QString &,
const QString &,
QgsWkbTypes::Type,
315 createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc )
cast_to_fptr( myLib->resolve(
"createEmptyDataSource" ) );
316 if ( createEmptyDataSource )
320 QgsCoordinateReferenceSystem srs = geomDialog.
crs();
321 if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, srs, errorMessage ) )
328 errorMessage = QObject::tr(
"Geometry type not recognised" );
335 errorMessage = QObject::tr(
"Resolving newEmptyDataSource(...) failed" );
347 void QgsNewVectorLayerDialog::showHelp()
349 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-shapefile-layer" ) );
QString selectedFileEncoding() const
Returns the file format for storage.
This class is a composition of two QSettings instances:
QString library(const QString &providerKey) const
Returns path for the library of the provider.
QgsCoordinateReferenceSystem crs() const
Returns the selected CRS for the new layer.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QString selectedFileFormat() const
Returns the file format for storage.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
Type
The WKB type describes the number of dimensions a geometry has.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
void setFilename(const QString &filename)
Sets the initial file name to show in the dialog.
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
QString filename() const
Returns the name for the new layer.
~QgsNewVectorLayerDialog() override
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs value for the new layer in the dialog.
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.
A registry / canonical manager of data providers.
QgsNewVectorLayerDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
New dialog constructor.
void attributes(QList< QPair< QString, QString > > &at) const
Appends the chosen attribute names and types to at.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
This class represents a coordinate reference system (CRS).
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
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.
static QString filterForDriver(const QString &driverName)
Creates a filter for an OGR driver key.
static QStringList availableEncodings()
Returns a list of available encodings.
QgsWkbTypes::Type selectedType() const
Returns the selected geometry type.