19#include "moc_qgsnewmemorylayerdialog.cpp"
39 dialog.
setCrs( defaultCrs );
40 if ( dialog.exec() == QDialog::Rejected )
47 const QString name = dialog.
layerName().isEmpty() ? tr(
"New scratch layer" ) : dialog.
layerName();
53 : QDialog( parent, fl )
58 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();
133 geomType =
static_cast<Qgis::WkbType>( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
137 if ( mGeometryWithZCheckBox->isChecked() )
139 if ( mGeometryWithMCheckBox->isChecked() )
146void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
151 mGeometryWithZCheckBox->setEnabled( isSpatial );
152 mGeometryWithMCheckBox->setEnabled( isSpatial );
153 mCrsSelector->setEnabled( isSpatial );
155 const bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
156 mOkButton->setEnabled( ok );
159void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int )
161 const QString fieldType = mTypeBox->currentData().toString();
162 if ( fieldType == QLatin1String(
"string" ) )
164 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
165 mWidth->setText( QStringLiteral(
"255" ) );
167 mPrecision->setEnabled(
false );
168 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
169 mWidth->setEnabled(
true );
171 else if ( fieldType == QLatin1String(
"integer" ) )
173 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
174 mWidth->setText( QStringLiteral(
"10" ) );
176 mPrecision->setEnabled(
false );
177 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
178 mWidth->setEnabled(
true );
180 else if ( fieldType == QLatin1String(
"double" ) )
182 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
183 mWidth->setText( QStringLiteral(
"30" ) );
184 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
185 mPrecision->setText( QStringLiteral(
"6" ) );
186 mPrecision->setEnabled(
true );
187 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
188 mWidth->setEnabled(
true );
190 else if ( fieldType == QLatin1String(
"bool" ) )
193 mWidth->setEnabled(
false );
195 mPrecision->setEnabled(
false );
197 else if ( fieldType == QLatin1String(
"date" ) )
200 mWidth->setEnabled(
false );
202 mPrecision->setEnabled(
false );
204 else if ( fieldType == QLatin1String(
"time" ) )
207 mWidth->setEnabled(
false );
209 mPrecision->setEnabled(
false );
211 else if ( fieldType == QLatin1String(
"datetime" ) )
214 mWidth->setEnabled(
false );
216 mPrecision->setEnabled(
false );
218 else if ( fieldType == QStringLiteral(
"binary" )
219 || fieldType == QStringLiteral(
"stringlist" )
220 || fieldType == QStringLiteral(
"integerlist" )
221 || fieldType == QStringLiteral(
"doublelist" )
222 || fieldType == QStringLiteral(
"integer64list" )
223 || fieldType == QStringLiteral(
"map" )
224 || fieldType == QLatin1String(
"geometry" ) )
227 mWidth->setEnabled(
false );
229 mPrecision->setEnabled(
false );
239 mCrsSelector->setCrs(
crs );
244 return mCrsSelector->crs();
249 return mNameLineEdit->text();
252void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
254 mAddAttributeButton->setDisabled( name.isEmpty() || !mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
257void QgsNewMemoryLayerDialog::selectionChanged()
259 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
260 mButtonUp->setDisabled( mAttributeView->selectedItems().isEmpty() );
261 mButtonDown->setDisabled( mAttributeView->selectedItems().isEmpty() );
268 QTreeWidgetItemIterator it( mAttributeView );
271 const QString name( ( *it )->text( 0 ) );
272 const QString
typeName( ( *it )->text( 1 ) );
273 const int width = ( *it )->text( 2 ).toInt();
274 const int precision = ( *it )->text( 3 ).toInt();
275 QMetaType::Type fieldType = QMetaType::Type::UnknownType;
276 QMetaType::Type fieldSubType = QMetaType::Type::UnknownType;
277 if (
typeName == QLatin1String(
"string" ) )
278 fieldType = QMetaType::Type::QString;
279 else if (
typeName == QLatin1String(
"integer" ) )
280 fieldType = QMetaType::Type::Int;
281 else if (
typeName == QLatin1String(
"double" ) )
282 fieldType = QMetaType::Type::Double;
283 else if (
typeName == QLatin1String(
"bool" ) )
284 fieldType = QMetaType::Type::Bool;
285 else if (
typeName == QLatin1String(
"date" ) )
286 fieldType = QMetaType::Type::QDate;
287 else if (
typeName == QLatin1String(
"time" ) )
288 fieldType = QMetaType::Type::QTime;
289 else if (
typeName == QLatin1String(
"datetime" ) )
290 fieldType = QMetaType::Type::QDateTime;
291 else if (
typeName == QLatin1String(
"binary" ) )
292 fieldType = QMetaType::Type::QByteArray;
293 else if (
typeName == QLatin1String(
"stringlist" ) )
295 fieldType = QMetaType::Type::QStringList;
296 fieldSubType = QMetaType::Type::QString;
298 else if (
typeName == QLatin1String(
"integerlist" ) )
300 fieldType = QMetaType::Type::QVariantList;
301 fieldSubType = QMetaType::Type::Int;
303 else if (
typeName == QLatin1String(
"doublelist" ) )
305 fieldType = QMetaType::Type::QVariantList;
306 fieldSubType = QMetaType::Type::Double;
308 else if (
typeName == QLatin1String(
"integer64list" ) )
310 fieldType = QMetaType::Type::QVariantList;
311 fieldSubType = QMetaType::Type::LongLong;
313 else if (
typeName == QLatin1String(
"map" ) )
314 fieldType = QMetaType::Type::QVariantMap;
315 else if (
typeName == QLatin1String(
"geometry" ) )
316 fieldType = QMetaType::Type::User;
328 if ( !mFieldNameEdit->text().trimmed().isEmpty() )
330 const QString currentFieldName = mFieldNameEdit->text();
331 if (
fields().lookupField( currentFieldName ) == -1 )
333 if ( QMessageBox::question(
this, tr(
"New Temporary Scratch Layer" ), 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 ), QMessageBox::Ok | QMessageBox::Cancel ) != QMessageBox::Ok )
343void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
345 if ( !mFieldNameEdit->text().isEmpty() )
347 const QString fieldName = mFieldNameEdit->text();
348 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
349 const QString width = mWidth->text();
350 const QString
precision = mPrecision->text();
351 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
353 mFieldNameEdit->clear();
355 if ( !mFieldNameEdit->hasFocus() )
357 mFieldNameEdit->setFocus();
362void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
364 delete mAttributeView->currentItem();
367void QgsNewMemoryLayerDialog::showHelp()
369 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
372void QgsNewMemoryLayerDialog::moveFieldsUp()
374 int currentRow = mAttributeView->currentIndex().row();
375 if ( currentRow == 0 )
378 mAttributeView->insertTopLevelItem( currentRow - 1, mAttributeView->takeTopLevelItem( currentRow ) );
379 mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow - 1, 0 ) );
382void QgsNewMemoryLayerDialog::moveFieldsDown()
384 int currentRow = mAttributeView->currentIndex().row();
385 if ( currentRow == mAttributeView->topLevelItemCount() - 1 )
388 mAttributeView->insertTopLevelItem( currentRow + 1, mAttributeView->takeTopLevelItem( currentRow ) );
389 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