28 #include "qgsogrprovider.h" 
   33 #include <QPushButton> 
   35 #include <QFileDialog> 
   36 #include <QMessageBox> 
   39   : QDialog( parent, fl )
 
   44   connect( mAddAttributeButton, &QToolButton::clicked, 
this, &QgsNewVectorLayerDialog::mAddAttributeButton_clicked );
 
   45   connect( mRemoveAttributeButton, &QToolButton::clicked, 
this, &QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked );
 
   46   connect( mFileFormatComboBox, 
static_cast<void ( QComboBox::* )( 
int )
>( &QComboBox::currentIndexChanged ), 
this, &QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged );
 
   47   connect( mTypeBox, 
static_cast<void ( QComboBox::* )( 
int )
>( &QComboBox::currentIndexChanged ), 
this, &QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged );
 
   48   connect( buttonBox, &QDialogButtonBox::helpRequested, 
this, &QgsNewVectorLayerDialog::showHelp );
 
   58   mWidth->setValidator( 
new QIntValidator( 1, 255, 
this ) );
 
   59   mPrecision->setValidator( 
new QIntValidator( 0, 15, 
this ) );
 
   70   for ( 
const auto type : geomTypes )
 
   72   mGeometryTypeBox->setCurrentIndex( -1 );
 
   74   mOkButton = buttonBox->button( QDialogButtonBox::Ok );
 
   75   mOkButton->setEnabled( 
false );
 
   77   mFileFormatComboBox->addItem( tr( 
"ESRI Shapefile" ), 
"ESRI Shapefile" );
 
   81   mFileFormatComboBox->addItem( tr( 
"Comma Separated Value" ), 
"Comma Separated Value" );
 
   82   mFileFormatComboBox->addItem( tr( 
"GML" ), 
"GML" );
 
   83   mFileFormatComboBox->addItem( tr( 
"Mapinfo File" ), 
"Mapinfo File" );
 
   85   if ( mFileFormatComboBox->count() == 1 )
 
   87     mFileFormatComboBox->setVisible( 
false );
 
   88     mFileFormatLabel->setVisible( 
false );
 
   91   mCrsSelector->setShowAccuracyWarnings( 
true );
 
   93   mFileFormatComboBox->setCurrentIndex( 0 );
 
   98   const QString enc = 
QgsSettings().
value( QStringLiteral( 
"/UI/encoding" ), 
"System" ).toString();
 
  102   int encindex = mFileEncoding->findText( enc );
 
  105     mFileEncoding->insertItem( 0, enc );
 
  108   mFileEncoding->setCurrentIndex( encindex );
 
  110   mAttributeView->addTopLevelItem( 
new QTreeWidgetItem( QStringList() << QStringLiteral( 
"id" ) << QStringLiteral( 
"Integer" ) << QStringLiteral( 
"10" ) << QString() ) );
 
  111   connect( mNameEdit, &QLineEdit::textChanged, 
this, &QgsNewVectorLayerDialog::nameChanged );
 
  112   connect( mAttributeView, &QTreeWidget::itemSelectionChanged, 
this, &QgsNewVectorLayerDialog::selectionChanged );
 
  113   connect( mGeometryTypeBox, 
static_cast<void( QComboBox::* )( 
int )
>( &QComboBox::currentIndexChanged ), 
this, [ = ]( 
int )
 
  119   mAddAttributeButton->setEnabled( 
false );
 
  120   mRemoveAttributeButton->setEnabled( 
false );
 
  124   mFileName->setConfirmOverwrite( 
false );
 
  125   mFileName->setDialogTitle( tr( 
"Save Layer As" ) );
 
  127   mFileName->setDefaultRoot( settings.
value( QStringLiteral( 
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
 
  131     const QFileInfo tmplFileInfo( mFileName->filePath() );
 
  132     settings.
setValue( QStringLiteral( 
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
 
  137 void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged( 
int index )
 
  140   if ( mFileFormatComboBox->currentText() == tr( 
"ESRI Shapefile" ) )
 
  141     mNameEdit->setMaxLength( 10 );
 
  143     mNameEdit->setMaxLength( 32767 );
 
  146 void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged( 
int index )
 
  152       if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
 
  153         mWidth->setText( QStringLiteral( 
"80" ) );
 
  154       mPrecision->setEnabled( 
false );
 
  155       mWidth->setValidator( 
new QIntValidator( 1, 255, 
this ) );
 
  159       if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
 
  160         mWidth->setText( QStringLiteral( 
"10" ) );
 
  161       mPrecision->setEnabled( 
false );
 
  162       mWidth->setValidator( 
new QIntValidator( 1, 10, 
this ) );
 
  166       if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
 
  167         mWidth->setText( QStringLiteral( 
"20" ) );
 
  168       if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 15 )
 
  169         mPrecision->setText( QStringLiteral( 
"6" ) );
 
  171       mPrecision->setEnabled( 
true );
 
  172       mWidth->setValidator( 
new QIntValidator( 1, 20, 
this ) );
 
  176       QgsDebugMsg( QStringLiteral( 
"unexpected index" ) );
 
  185             ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
 
  187   if ( mGeometryWithZRadioButton->isChecked() )
 
  190   if ( mGeometryWithMRadioButton->isChecked() )
 
  198   return mCrsSelector->crs();
 
  203   mCrsSelector->setCrs( 
crs );
 
  206 void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
 
  208   const QString myName = mNameEdit->text();
 
  209   const QString myWidth = mWidth->text();
 
  210   const QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : QString();
 
  212   const QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
 
  213   mAttributeView->addTopLevelItem( 
new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
 
  218 void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
 
  220   delete mAttributeView->currentItem();
 
  226   QTreeWidgetItemIterator it( mAttributeView );
 
  229     QTreeWidgetItem *item = *it;
 
  230     const QString type = QStringLiteral( 
"%1;%2;%3" ).arg( item->text( 1 ), item->text( 2 ), item->text( 3 ) );
 
  231     at.push_back( qMakePair( item->text( 0 ), type ) );
 
  232     QgsDebugMsg( QStringLiteral( 
"appending %1//%2" ).arg( item->text( 0 ), type ) );
 
  240   QString myType = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
 
  246   return mFileEncoding->currentText();
 
  249 void QgsNewVectorLayerDialog::nameChanged( 
const QString &name )
 
  251   mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
 
  254 void QgsNewVectorLayerDialog::selectionChanged()
 
  256   mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
 
  261   return mFileName->filePath();
 
  269 void QgsNewVectorLayerDialog::checkOk()
 
  271   const bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 && mGeometryTypeBox->currentIndex() != -1 );
 
  272   mOkButton->setEnabled( ok );
 
  280   if ( res.isEmpty() && error.isEmpty() )
 
  285 void QgsNewVectorLayerDialog::updateExtension()
 
  290   if ( fileformat == QLatin1String( 
"ESRI Shapefile" ) )
 
  294       fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( 
".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( 
".shp" ) );
 
  299       fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( 
".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( 
".dbf" ) );
 
  311   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?" ),
 
  312        QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
 
  320   errorMessage.clear();
 
  323   if ( !initialPath.isEmpty() )
 
  325   if ( geomDialog.exec() == QDialog::Rejected )
 
  332   QString fileName = geomDialog.
filename();
 
  335   QgsDebugMsg( QStringLiteral( 
"New file format will be: %1" ).arg( fileformat ) );
 
  341   settings.
setValue( QStringLiteral( 
"UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
 
  342   settings.
setValue( QStringLiteral( 
"UI/encoding" ), enc );
 
  348     const bool success = QgsOgrProviderUtils::createEmptyDataSource( fileName, fileformat, enc, geometrytype, 
attributes, srs, errorMessage );
 
  356     errorMessage = QObject::tr( 
"Geometry type not recognised" );
 
  367 void QgsNewVectorLayerDialog::showHelp()
 
  369   QgsHelp::openHelp( QStringLiteral( 
"managing_data_source/create_layers.html#creating-a-new-shapefile-layer" ) );
 
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
This class represents a coordinate reference system (CRS).
static QString ensureFileNameHasExtension(const QString &fileName, const QStringList &extensions)
Ensures that a fileName ends with an extension from the provided list of extensions.
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