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 mOkButton = buttonBox->button( QDialogButtonBox::Ok );
65 mOkButton->setEnabled(
false );
67 mFileFormatComboBox->addItem( tr(
"ESRI Shapefile" ),
"ESRI Shapefile" );
71 mFileFormatComboBox->addItem( tr(
"Comma Separated Value" ),
"Comma Separated Value" );
72 mFileFormatComboBox->addItem( tr(
"GML" ),
"GML" );
73 mFileFormatComboBox->addItem( tr(
"Mapinfo File" ),
"Mapinfo File" );
75 if ( mFileFormatComboBox->count() == 1 )
77 mFileFormatComboBox->setVisible(
false );
78 mFileFormatLabel->setVisible(
false );
81 mFileFormatComboBox->setCurrentIndex( 0 );
86 QString enc =
QgsSettings().
value( QStringLiteral(
"/UI/encoding" ),
"System" ).toString();
90 int encindex = mFileEncoding->findText( enc );
93 mFileEncoding->insertItem( 0, enc );
96 mFileEncoding->setCurrentIndex( encindex );
98 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << QStringLiteral(
"id" ) << QStringLiteral(
"Integer" ) << QStringLiteral(
"10" ) << QString() ) );
99 connect( mNameEdit, &QLineEdit::textChanged,
this, &QgsNewVectorLayerDialog::nameChanged );
100 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewVectorLayerDialog::selectionChanged );
102 mAddAttributeButton->setEnabled(
false );
103 mRemoveAttributeButton->setEnabled(
false );
107 mFileName->setConfirmOverwrite(
false );
108 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() );
120 void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged(
int index )
123 if ( mFileFormatComboBox->currentText() == tr(
"ESRI Shapefile" ) )
124 mNameEdit->setMaxLength( 10 );
126 mNameEdit->setMaxLength( 32767 );
129 void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged(
int index )
135 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
136 mWidth->setText( QStringLiteral(
"80" ) );
137 mPrecision->setEnabled(
false );
138 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
142 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
143 mWidth->setText( QStringLiteral(
"10" ) );
144 mPrecision->setEnabled(
false );
145 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
149 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
150 mWidth->setText( QStringLiteral(
"20" ) );
151 mPrecision->setEnabled(
true );
152 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
156 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
165 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
167 if ( mGeometryWithZRadioButton->isChecked() )
170 if ( mGeometryWithMRadioButton->isChecked() )
178 return mCrsSelector->crs();
183 mCrsSelector->setCrs( crs );
186 void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
188 QString myName = mNameEdit->text();
189 QString myWidth = mWidth->text();
190 QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : QString();
192 QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
193 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
198 void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
200 delete mAttributeView->currentItem();
206 QTreeWidgetItemIterator it( mAttributeView );
209 QTreeWidgetItem *item = *it;
210 QString type = QStringLiteral(
"%1;%2;%3" ).arg( item->text( 1 ), item->text( 2 ), item->text( 3 ) );
211 at.push_back( qMakePair( item->text( 0 ), type ) );
212 QgsDebugMsg( QStringLiteral(
"appending %1//%2" ).arg( item->text( 0 ), type ) );
220 QString myType = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
226 return mFileEncoding->currentText();
229 void QgsNewVectorLayerDialog::nameChanged(
const QString &name )
231 mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
234 void QgsNewVectorLayerDialog::selectionChanged()
236 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
241 return mFileName->filePath();
246 mFileName->setFilePath( filename );
249 void QgsNewVectorLayerDialog::checkOk()
251 bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 );
252 mOkButton->setEnabled( ok );
260 if ( res.isEmpty() && error.isEmpty() )
267 errorMessage.clear();
270 if ( !initialPath.isEmpty() )
272 if ( geomDialog.exec() == QDialog::Rejected )
277 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?" ),
278 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
284 QgsDebugMsg( QStringLiteral(
"New file format will be: %1" ).arg( fileformat ) );
290 QString fileName = geomDialog.
filename();
291 if ( fileformat == QLatin1String(
"ESRI Shapefile" ) && !fileName.endsWith( QLatin1String(
".shp" ), Qt::CaseInsensitive ) )
292 fileName += QLatin1String(
".shp" );
294 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
295 settings.
setValue( QStringLiteral(
"UI/encoding" ), enc );
302 bool success = QgsOgrProviderUtils::createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, srs, errorMessage );
310 errorMessage = QObject::tr(
"Geometry type not recognised" );
321 void QgsNewVectorLayerDialog::showHelp()
323 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-shapefile-layer" ) );
QString selectedFileEncoding() const
Returns the file format for storage.
QString selectedFileFormat() const
Returns the file format for storage.
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.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void attributes(QList< QPair< QString, QString > > &at) const
Appends the chosen attribute names and types to at.
static QgsGui * instance()
Returns a pointer to the singleton instance.
Type
The WKB type describes the number of dimensions a geometry has.
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
QString filename() const
Returns the name for the new layer.
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.
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.
QgsNewVectorLayerDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
New dialog constructor.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsCoordinateReferenceSystem crs() const
Returns the selected CRS for the new layer.
This class represents a coordinate reference system (CRS).
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 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.
QgsWkbTypes::Type selectedType() const
Returns the selected geometry type.
static QString filterForDriver(const QString &driverName)
Creates a filter for an OGR driver key.
static QStringList availableEncodings()
Returns a list of available encodings.