38 dialog.
setCrs( defaultCrs );
39 if ( dialog.exec() == QDialog::Rejected )
46 const QString name = dialog.
layerName().isEmpty() ? tr(
"New scratch layer" ) : dialog.
layerName();
52 : QDialog( parent, fl )
57 mNameLineEdit->setText( tr(
"New scratch layer" ) );
77 for (
const auto type : geomTypes )
79 mGeometryTypeBox->setCurrentIndex( -1 );
81 mGeometryWithZCheckBox->setEnabled(
false );
82 mGeometryWithMCheckBox->setEnabled(
false );
83 mCrsSelector->setEnabled(
false );
84 mCrsSelector->setShowAccuracyWarnings(
true );
99 mTypeBox->addItem(
QgsFields::iconForFieldType( QMetaType::Type::User, QMetaType::Type::UnknownType, QStringLiteral(
"geometry" ) ), tr(
"Geometry" ),
"geometry" );
100 mTypeBox_currentIndexChanged( 0 );
102 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
103 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
105 mAddAttributeButton->setEnabled(
false );
106 mRemoveAttributeButton->setEnabled(
false );
107 mButtonUp->setEnabled(
false );
108 mButtonDown->setEnabled(
false );
110 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
111 mOkButton->setEnabled(
false );
113 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
114 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
115 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
116 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
117 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
118 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
119 connect( mButtonUp, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::moveFieldsUp );
120 connect( mButtonDown, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::moveFieldsDown );
122 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
124 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QgsNewMemoryLayerDialog::reject );
126 mNameLineEdit->selectAll();
127 mNameLineEdit->setFocus();
134 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
138 if ( mGeometryWithZCheckBox->isChecked() )
140 if ( mGeometryWithMCheckBox->isChecked() )
147void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
150 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
153 mGeometryWithZCheckBox->setEnabled( isSpatial );
154 mGeometryWithMCheckBox->setEnabled( isSpatial );
155 mCrsSelector->setEnabled( isSpatial );
157 const bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
158 mOkButton->setEnabled( ok );
161void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int )
163 const QString fieldType = mTypeBox->currentData().toString();
164 if ( fieldType == QLatin1String(
"string" ) )
166 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
167 mWidth->setText( QStringLiteral(
"255" ) );
169 mPrecision->setEnabled(
false );
170 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
171 mWidth->setEnabled(
true );
173 else if ( fieldType == QLatin1String(
"integer" ) )
175 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
176 mWidth->setText( QStringLiteral(
"10" ) );
178 mPrecision->setEnabled(
false );
179 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
180 mWidth->setEnabled(
true );
182 else if ( fieldType == QLatin1String(
"double" ) )
184 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
185 mWidth->setText( QStringLiteral(
"30" ) );
186 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
187 mPrecision->setText( QStringLiteral(
"6" ) );
188 mPrecision->setEnabled(
true );
189 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
190 mWidth->setEnabled(
true );
192 else if ( fieldType == QLatin1String(
"bool" ) )
195 mWidth->setEnabled(
false );
197 mPrecision->setEnabled(
false );
199 else if ( fieldType == QLatin1String(
"date" ) )
202 mWidth->setEnabled(
false );
204 mPrecision->setEnabled(
false );
206 else if ( fieldType == QLatin1String(
"time" ) )
209 mWidth->setEnabled(
false );
211 mPrecision->setEnabled(
false );
213 else if ( fieldType == QLatin1String(
"datetime" ) )
216 mWidth->setEnabled(
false );
218 mPrecision->setEnabled(
false );
220 else if ( fieldType == QStringLiteral(
"binary" )
221 || fieldType == QStringLiteral(
"stringlist" )
222 || fieldType == QStringLiteral(
"integerlist" )
223 || fieldType == QStringLiteral(
"doublelist" )
224 || fieldType == QStringLiteral(
"integer64list" )
225 || fieldType == QStringLiteral(
"map" )
226 || fieldType == QLatin1String(
"geometry" ) )
229 mWidth->setEnabled(
false );
231 mPrecision->setEnabled(
false );
241 mCrsSelector->setCrs(
crs );
246 return mCrsSelector->crs();
251 return mNameLineEdit->text();
254void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
256 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
259void QgsNewMemoryLayerDialog::selectionChanged()
261 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
262 mButtonUp->setDisabled( mAttributeView->selectedItems().isEmpty() );
263 mButtonDown->setDisabled( mAttributeView->selectedItems().isEmpty() );
270 QTreeWidgetItemIterator it( mAttributeView );
273 const QString name( ( *it )->text( 0 ) );
274 const QString
typeName( ( *it )->text( 1 ) );
275 const int width = ( *it )->text( 2 ).toInt();
276 const int precision = ( *it )->text( 3 ).toInt();
277 QMetaType::Type fieldType = QMetaType::Type::UnknownType;
278 QMetaType::Type fieldSubType = QMetaType::Type::UnknownType;
279 if (
typeName == QLatin1String(
"string" ) )
280 fieldType = QMetaType::Type::QString;
281 else if (
typeName == QLatin1String(
"integer" ) )
282 fieldType = QMetaType::Type::Int;
283 else if (
typeName == QLatin1String(
"double" ) )
284 fieldType = QMetaType::Type::Double;
285 else if (
typeName == QLatin1String(
"bool" ) )
286 fieldType = QMetaType::Type::Bool;
287 else if (
typeName == QLatin1String(
"date" ) )
288 fieldType = QMetaType::Type::QDate;
289 else if (
typeName == QLatin1String(
"time" ) )
290 fieldType = QMetaType::Type::QTime;
291 else if (
typeName == QLatin1String(
"datetime" ) )
292 fieldType = QMetaType::Type::QDateTime;
293 else if (
typeName == QLatin1String(
"binary" ) )
294 fieldType = QMetaType::Type::QByteArray;
295 else if (
typeName == QLatin1String(
"stringlist" ) )
297 fieldType = QMetaType::Type::QStringList;
298 fieldSubType = QMetaType::Type::QString;
300 else if (
typeName == QLatin1String(
"integerlist" ) )
302 fieldType = QMetaType::Type::QVariantList;
303 fieldSubType = QMetaType::Type::Int;
305 else if (
typeName == QLatin1String(
"doublelist" ) )
307 fieldType = QMetaType::Type::QVariantList;
308 fieldSubType = QMetaType::Type::Double;
310 else if (
typeName == QLatin1String(
"integer64list" ) )
312 fieldType = QMetaType::Type::QVariantList;
313 fieldSubType = QMetaType::Type::LongLong;
315 else if (
typeName == QLatin1String(
"map" ) )
316 fieldType = QMetaType::Type::QVariantMap;
317 else if (
typeName == QLatin1String(
"geometry" ) )
318 fieldType = QMetaType::Type::User;
330 if ( !mFieldNameEdit->text().trimmed().isEmpty() )
332 const QString currentFieldName = mFieldNameEdit->text();
333 if (
fields().lookupField( currentFieldName ) == -1 )
335 if ( QMessageBox::question(
this, tr(
"New Temporary Scratch Layer" ),
336 tr(
"The field “%1” has not been added to the fields list. Are you sure you want to proceed and discard this field?" ).arg( currentFieldName ),
337 QMessageBox::Ok | QMessageBox::Cancel ) != QMessageBox::Ok )
347void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
349 if ( !mFieldNameEdit->text().isEmpty() )
351 const QString fieldName = mFieldNameEdit->text();
352 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
353 const QString width = mWidth->text();
354 const QString
precision = mPrecision->text();
355 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
357 mFieldNameEdit->clear();
359 if ( !mFieldNameEdit->hasFocus() )
361 mFieldNameEdit->setFocus();
366void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
368 delete mAttributeView->currentItem();
371void QgsNewMemoryLayerDialog::showHelp()
373 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
376void QgsNewMemoryLayerDialog::moveFieldsUp()
378 int currentRow = mAttributeView->currentIndex().row();
379 if ( currentRow == 0 )
382 mAttributeView->insertTopLevelItem( currentRow - 1, mAttributeView->takeTopLevelItem( currentRow ) );
383 mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow - 1, 0 ) );
386void QgsNewMemoryLayerDialog::moveFieldsDown()
388 int currentRow = mAttributeView->currentIndex().row();
389 if ( currentRow == mAttributeView->topLevelItemCount() - 1 )
392 mAttributeView->insertTopLevelItem( currentRow + 1, mAttributeView->takeTopLevelItem( currentRow ) );
393 mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow + 1, 0 ) );
WkbType
The WKB type describes the number of dimensions a geometry has.
@ CompoundCurve
CompoundCurve.
@ MultiPolygon
MultiPolygon.
@ MultiLineString
MultiLineString.
@ CurvePolygon
CurvePolygon.
@ PolyhedralSurface
PolyhedralSurface.
@ MultiSurface
MultiSurface.
This class represents a coordinate reference system (CRS).
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
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(Qgis::WkbType type)
Returns the icon for a vector layer whose geometry type is provided.
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, Qgis::WkbType geometryType=Qgis::WkbType::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), bool loadDefaultStyle=true) SIP_FACTORY
Creates a new memory layer using the specified parameters.
QgsCoordinateReferenceSystem crs() const
Returns the selected CRS for the new layer.
QgsFields fields() const
Returns attributes for the new layer.
QString layerName() const
Returns the layer name.
Qgis::WkbType selectedType() const
Returns the selected geometry type.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs value for the new layer in the dialog.
static QgsVectorLayer * runAndCreateLayer(QWidget *parent=nullptr, const QgsCoordinateReferenceSystem &defaultCrs=QgsCoordinateReferenceSystem())
Runs the dialog and creates a new memory layer.
QgsNewMemoryLayerDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
New dialog constructor.
static QString typeToDisplayString(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType)
Returns a user-friendly translated string representing a QVariant type.
Represents a vector layer which manages a vector based data sets.
static QString translatedDisplayString(Qgis::WkbType type)
Returns a translated display string type for a WKB type, e.g., the geometry name used in WKT geometry...
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
#define QgsDebugError(str)
const QgsCoordinateReferenceSystem & crs