29 #include <QPushButton> 32 #include <QFileDialog> 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 ) );
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" ) << QLatin1String(
"" ) ) );
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->setDialogTitle( tr(
"Save Layer as…" ) );
108 mFileName->setDefaultRoot( settings.
value( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
112 QFileInfo tmplFileInfo( mFileName->filePath() );
113 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
124 void QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged(
int index )
127 if ( mFileFormatComboBox->currentText() == tr(
"ESRI Shapefile" ) )
128 mNameEdit->setMaxLength( 10 );
130 mNameEdit->setMaxLength( 32767 );
133 void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged(
int index )
139 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
140 mWidth->setText( QStringLiteral(
"80" ) );
141 mPrecision->setEnabled(
false );
142 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
146 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
147 mWidth->setText( QStringLiteral(
"10" ) );
148 mPrecision->setEnabled(
false );
149 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
153 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
154 mWidth->setText( QStringLiteral(
"20" ) );
155 mPrecision->setEnabled(
true );
156 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
169 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
171 if ( mGeometryWithZCheckBox->isChecked() )
174 if ( mGeometryWithMCheckBox->isChecked() )
182 return mCrsSelector->crs();
187 mCrsSelector->setCrs( crs );
190 void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
192 QString myName = mNameEdit->text();
193 QString myWidth = mWidth->text();
194 QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : QLatin1String(
"" );
196 QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
197 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
202 void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
204 delete mAttributeView->currentItem();
210 QTreeWidgetItemIterator it( mAttributeView );
213 QTreeWidgetItem *item = *it;
214 QString type = QStringLiteral(
"%1;%2;%3" ).arg( item->text( 1 ), item->text( 2 ), item->text( 3 ) );
215 at.push_back( qMakePair( item->text( 0 ), type ) );
216 QgsDebugMsg( QString(
"appending %1//%2" ).arg( item->text( 0 ), type ) );
224 QString myType = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
230 return mFileEncoding->currentText();
233 void QgsNewVectorLayerDialog::nameChanged(
const QString &name )
235 mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
238 void QgsNewVectorLayerDialog::selectionChanged()
240 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
245 return mFileName->filePath();
248 void QgsNewVectorLayerDialog::checkOk()
250 bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 );
251 mOkButton->setEnabled( ok );
259 if ( geomDialog.exec() == QDialog::Rejected )
261 return QLatin1String(
"" );
267 QgsDebugMsg( QString(
"New file format will be: %1" ).arg( fileformat ) );
273 QString fileName = geomDialog.
filename();
274 if ( fileformat == QLatin1String(
"ESRI Shapefile" ) && !fileName.endsWith( QLatin1String(
".shp" ), Qt::CaseInsensitive ) )
275 fileName += QLatin1String(
".shp" );
277 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
278 settings.
setValue( QStringLiteral(
"UI/encoding" ), enc );
282 QString ogrlib = pReg->
library( QStringLiteral(
"ogr" ) );
284 QLibrary *myLib =
new QLibrary( ogrlib );
285 bool loaded = myLib->load();
290 typedef bool ( *createEmptyDataSourceProc )(
const QString &,
const QString &,
const QString &,
QgsWkbTypes::Type,
292 createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc )
cast_to_fptr( myLib->resolve(
"createEmptyDataSource" ) );
293 if ( createEmptyDataSource )
298 if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, srs ) )
311 QgsDebugMsg(
"Resolving newEmptyDataSource(...) failed" );
322 void QgsNewVectorLayerDialog::showHelp()
324 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:
QString library(const QString &providerKey) const
Return path for the library of the provider.
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.
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.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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.
QString filename() const
Returns the name for the new layer.
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
~QgsNewVectorLayerDialog() override
static QString runAndCreateLayer(QWidget *parent=nullptr, QString *enc=nullptr, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Runs the dialog and creates a layer matching the dialog parameters.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs value for the new layer in the dialog.
A registry / canonical manager of data providers.
QgsNewVectorLayerDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
QgsCoordinateReferenceSystem crs() const
Returns the selected CRS for the new layer.
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.
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.