27 #include "qgssettings.h" 
   28 #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   mCrsSelector->setShowAccuracyWarnings( 
true );
 
   92   mFileFormatComboBox->setCurrentIndex( 0 );
 
   97   QString enc = QgsSettings().value( QStringLiteral( 
"/UI/encoding" ), 
"System" ).toString();
 
  101   int encindex = mFileEncoding->findText( enc );
 
  104     mFileEncoding->insertItem( 0, enc );
 
  107   mFileEncoding->setCurrentIndex( encindex );
 
  109   mAttributeView->addTopLevelItem( 
new QTreeWidgetItem( QStringList() << QStringLiteral( 
"id" ) << QStringLiteral( 
"Integer" ) << QStringLiteral( 
"10" ) << QString() ) );
 
  110   connect( mNameEdit, &QLineEdit::textChanged, 
this, &QgsNewVectorLayerDialog::nameChanged );
 
  111   connect( mAttributeView, &QTreeWidget::itemSelectionChanged, 
this, &QgsNewVectorLayerDialog::selectionChanged );
 
  112   connect( mGeometryTypeBox, 
static_cast<void( QComboBox::* )( 
int )
>( &QComboBox::currentIndexChanged ), 
this, [ = ]( 
int index )
 
  114     QString fileName = mFileName->filePath();
 
  115     if ( !fileName.isEmpty() )
 
  119         fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( 
".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( 
".dbf" ) );
 
  123         fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( 
".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( 
".shp" ) );
 
  125       mFileName->setFilePath( fileName );
 
  130   mAddAttributeButton->setEnabled( 
false );
 
  131   mRemoveAttributeButton->setEnabled( 
false );
 
  135   mFileName->setConfirmOverwrite( 
false );
 
  136   mFileName->setDialogTitle( tr( 
"Save Layer As" ) );
 
  137   QgsSettings settings;
 
  138   mFileName->setDefaultRoot( settings.value( QStringLiteral( 
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
 
  141     QgsSettings settings;
 
  142     QFileInfo tmplFileInfo( mFileName->filePath() );
 
  143     settings.setValue( QStringLiteral( 
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
 
  148 void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged( 
int index )
 
  151   if ( mFileFormatComboBox->currentText() == tr( 
"ESRI Shapefile" ) )
 
  152     mNameEdit->setMaxLength( 10 );
 
  154     mNameEdit->setMaxLength( 32767 );
 
  157 void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged( 
int index )
 
  163       if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
 
  164         mWidth->setText( QStringLiteral( 
"80" ) );
 
  165       mPrecision->setEnabled( 
false );
 
  166       mWidth->setValidator( 
new QIntValidator( 1, 255, 
this ) );
 
  170       if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
 
  171         mWidth->setText( QStringLiteral( 
"10" ) );
 
  172       mPrecision->setEnabled( 
false );
 
  173       mWidth->setValidator( 
new QIntValidator( 1, 10, 
this ) );
 
  177       if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
 
  178         mWidth->setText( QStringLiteral( 
"20" ) );
 
  179       if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 15 )
 
  180         mPrecision->setText( QStringLiteral( 
"6" ) );
 
  182       mPrecision->setEnabled( 
true );
 
  183       mWidth->setValidator( 
new QIntValidator( 1, 20, 
this ) );
 
  187       QgsDebugMsg( QStringLiteral( 
"unexpected index" ) );
 
  196             ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
 
  198   if ( mGeometryWithZRadioButton->isChecked() )
 
  201   if ( mGeometryWithMRadioButton->isChecked() )
 
  209   return mCrsSelector->crs();
 
  214   mCrsSelector->setCrs( 
crs );
 
  217 void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
 
  219   QString myName = mNameEdit->text();
 
  220   QString myWidth = mWidth->text();
 
  221   QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : QString();
 
  223   QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
 
  224   mAttributeView->addTopLevelItem( 
new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
 
  229 void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
 
  231   delete mAttributeView->currentItem();
 
  237   QTreeWidgetItemIterator it( mAttributeView );
 
  240     QTreeWidgetItem *item = *it;
 
  241     QString type = QStringLiteral( 
"%1;%2;%3" ).arg( item->text( 1 ), item->text( 2 ), item->text( 3 ) );
 
  242     at.push_back( qMakePair( item->text( 0 ), type ) );
 
  243     QgsDebugMsg( QStringLiteral( 
"appending %1//%2" ).arg( item->text( 0 ), type ) );
 
  251   QString myType = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
 
  257   return mFileEncoding->currentText();
 
  260 void QgsNewVectorLayerDialog::nameChanged( 
const QString &name )
 
  262   mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
 
  265 void QgsNewVectorLayerDialog::selectionChanged()
 
  267   mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
 
  272   return mFileName->filePath();
 
  280 void QgsNewVectorLayerDialog::checkOk()
 
  282   bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 && mGeometryTypeBox->currentIndex() != -1 );
 
  283   mOkButton->setEnabled( ok );
 
  291   if ( res.isEmpty() && error.isEmpty() )
 
  298   errorMessage.clear();
 
  301   if ( !initialPath.isEmpty() )
 
  303   if ( geomDialog.exec() == QDialog::Rejected )
 
  308   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?" ),
 
  309        QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel ) != QMessageBox::Yes )
 
  315   QgsDebugMsg( QStringLiteral( 
"New file format will be: %1" ).arg( fileformat ) );
 
  320   QgsSettings settings;
 
  321   QString fileName = geomDialog.
filename();
 
  322   if ( fileformat == QLatin1String( 
"ESRI Shapefile" ) && ( geometrytype != 
QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String( 
".shp" ), Qt::CaseInsensitive ) ) )
 
  323     fileName += QLatin1String( 
".shp" );
 
  324   else if ( fileformat == QLatin1String( 
"ESRI Shapefile" ) && ( geometrytype == 
QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String( 
".dbf" ), Qt::CaseInsensitive ) ) )
 
  326     if ( fileName.endsWith( QLatin1String( 
".shp" ), Qt::CaseInsensitive ) )
 
  327       fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( 
".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( 
".dbf" ) );
 
  329       fileName += QLatin1String( 
".dbf" );
 
  332   settings.setValue( QStringLiteral( 
"UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
 
  333   settings.setValue( QStringLiteral( 
"UI/encoding" ), enc );
 
  339     bool success = QgsOgrProviderUtils::createEmptyDataSource( fileName, fileformat, enc, geometrytype, 
attributes, srs, errorMessage );
 
  347     errorMessage = QObject::tr( 
"Geometry type not recognised" );
 
  358 void QgsNewVectorLayerDialog::showHelp()
 
  360   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 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.
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