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" ) );
78 for (
const auto type : geomTypes )
80 mGeometryTypeBox->setCurrentIndex( -1 );
82 mGeometryWithZCheckBox->setEnabled(
false );
83 mGeometryWithMCheckBox->setEnabled(
false );
84 mCrsSelector->setEnabled(
false );
85 mCrsSelector->setShowAccuracyWarnings(
true );
100 mTypeBox->addItem(
QgsFields::iconForFieldType( QMetaType::Type::User, QMetaType::Type::UnknownType, QStringLiteral(
"geometry" ) ), tr(
"Geometry" ),
"geometry" );
101 mTypeBox_currentIndexChanged( 0 );
103 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
104 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
106 mAddAttributeButton->setEnabled(
false );
107 mRemoveAttributeButton->setEnabled(
false );
108 mButtonUp->setEnabled(
false );
109 mButtonDown->setEnabled(
false );
111 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
112 mOkButton->setEnabled(
false );
114 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
115 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
116 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
117 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
118 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
119 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
120 connect( mButtonUp, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::moveFieldsUp );
121 connect( mButtonDown, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::moveFieldsDown );
123 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
125 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QgsNewMemoryLayerDialog::reject );
127 mNameLineEdit->selectAll();
128 mNameLineEdit->setFocus();
135 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
139 if ( mGeometryWithZCheckBox->isChecked() )
141 if ( mGeometryWithMCheckBox->isChecked() )
148void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
151 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
154 mGeometryWithZCheckBox->setEnabled( isSpatial );
155 mGeometryWithMCheckBox->setEnabled( isSpatial );
156 mCrsSelector->setEnabled( isSpatial );
158 const bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
159 mOkButton->setEnabled( ok );
162void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int )
164 const QString fieldType = mTypeBox->currentData().toString();
165 if ( fieldType == QLatin1String(
"string" ) )
167 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
168 mWidth->setText( QStringLiteral(
"255" ) );
170 mPrecision->setEnabled(
false );
171 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
172 mWidth->setEnabled(
true );
174 else if ( fieldType == QLatin1String(
"integer" ) )
176 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
177 mWidth->setText( QStringLiteral(
"10" ) );
179 mPrecision->setEnabled(
false );
180 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
181 mWidth->setEnabled(
true );
183 else if ( fieldType == QLatin1String(
"double" ) )
185 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
186 mWidth->setText( QStringLiteral(
"30" ) );
187 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
188 mPrecision->setText( QStringLiteral(
"6" ) );
189 mPrecision->setEnabled(
true );
190 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
191 mWidth->setEnabled(
true );
193 else if ( fieldType == QLatin1String(
"bool" ) )
196 mWidth->setEnabled(
false );
198 mPrecision->setEnabled(
false );
200 else if ( fieldType == QLatin1String(
"date" ) )
203 mWidth->setEnabled(
false );
205 mPrecision->setEnabled(
false );
207 else if ( fieldType == QLatin1String(
"time" ) )
210 mWidth->setEnabled(
false );
212 mPrecision->setEnabled(
false );
214 else if ( fieldType == QLatin1String(
"datetime" ) )
217 mWidth->setEnabled(
false );
219 mPrecision->setEnabled(
false );
221 else if ( fieldType == QStringLiteral(
"binary" )
222 || fieldType == QStringLiteral(
"stringlist" )
223 || fieldType == QStringLiteral(
"integerlist" )
224 || fieldType == QStringLiteral(
"doublelist" )
225 || fieldType == QStringLiteral(
"integer64list" )
226 || fieldType == QStringLiteral(
"map" )
227 || fieldType == QLatin1String(
"geometry" ) )
230 mWidth->setEnabled(
false );
232 mPrecision->setEnabled(
false );
242 mCrsSelector->setCrs(
crs );
247 return mCrsSelector->crs();
252 return mNameLineEdit->text();
255void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
257 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
260void QgsNewMemoryLayerDialog::selectionChanged()
262 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
263 mButtonUp->setDisabled( mAttributeView->selectedItems().isEmpty() );
264 mButtonDown->setDisabled( mAttributeView->selectedItems().isEmpty() );
271 QTreeWidgetItemIterator it( mAttributeView );
274 const QString name( ( *it )->text( 0 ) );
275 const QString
typeName( ( *it )->text( 1 ) );
276 const int width = ( *it )->text( 2 ).toInt();
277 const int precision = ( *it )->text( 3 ).toInt();
278 QMetaType::Type fieldType = QMetaType::Type::UnknownType;
279 QMetaType::Type fieldSubType = QMetaType::Type::UnknownType;
280 if (
typeName == QLatin1String(
"string" ) )
281 fieldType = QMetaType::Type::QString;
282 else if (
typeName == QLatin1String(
"integer" ) )
283 fieldType = QMetaType::Type::Int;
284 else if (
typeName == QLatin1String(
"double" ) )
285 fieldType = QMetaType::Type::Double;
286 else if (
typeName == QLatin1String(
"bool" ) )
287 fieldType = QMetaType::Type::Bool;
288 else if (
typeName == QLatin1String(
"date" ) )
289 fieldType = QMetaType::Type::QDate;
290 else if (
typeName == QLatin1String(
"time" ) )
291 fieldType = QMetaType::Type::QTime;
292 else if (
typeName == QLatin1String(
"datetime" ) )
293 fieldType = QMetaType::Type::QDateTime;
294 else if (
typeName == QLatin1String(
"binary" ) )
295 fieldType = QMetaType::Type::QByteArray;
296 else if (
typeName == QLatin1String(
"stringlist" ) )
298 fieldType = QMetaType::Type::QStringList;
299 fieldSubType = QMetaType::Type::QString;
301 else if (
typeName == QLatin1String(
"integerlist" ) )
303 fieldType = QMetaType::Type::QVariantList;
304 fieldSubType = QMetaType::Type::Int;
306 else if (
typeName == QLatin1String(
"doublelist" ) )
308 fieldType = QMetaType::Type::QVariantList;
309 fieldSubType = QMetaType::Type::Double;
311 else if (
typeName == QLatin1String(
"integer64list" ) )
313 fieldType = QMetaType::Type::QVariantList;
314 fieldSubType = QMetaType::Type::LongLong;
316 else if (
typeName == QLatin1String(
"map" ) )
317 fieldType = QMetaType::Type::QVariantMap;
318 else if (
typeName == QLatin1String(
"geometry" ) )
319 fieldType = QMetaType::Type::User;
331 if ( !mFieldNameEdit->text().trimmed().isEmpty() )
333 const QString currentFieldName = mFieldNameEdit->text();
334 if (
fields().lookupField( currentFieldName ) == -1 )
336 if ( QMessageBox::question(
this, tr(
"New Temporary Scratch Layer" ),
337 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 ),
338 QMessageBox::Ok | QMessageBox::Cancel ) != QMessageBox::Ok )
348void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
350 if ( !mFieldNameEdit->text().isEmpty() )
352 const QString fieldName = mFieldNameEdit->text();
353 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
354 const QString width = mWidth->text();
355 const QString
precision = mPrecision->text();
356 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
358 mFieldNameEdit->clear();
360 if ( !mFieldNameEdit->hasFocus() )
362 mFieldNameEdit->setFocus();
367void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
369 delete mAttributeView->currentItem();
372void QgsNewMemoryLayerDialog::showHelp()
374 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
377void QgsNewMemoryLayerDialog::moveFieldsUp()
379 int currentRow = mAttributeView->currentIndex().row();
380 if ( currentRow == 0 )
383 mAttributeView->insertTopLevelItem( currentRow - 1, mAttributeView->takeTopLevelItem( currentRow ) );
384 mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow - 1, 0 ) );
387void QgsNewMemoryLayerDialog::moveFieldsDown()
389 int currentRow = mAttributeView->currentIndex().row();
390 if ( currentRow == mAttributeView->topLevelItemCount() - 1 )
393 mAttributeView->insertTopLevelItem( currentRow + 1, mAttributeView->takeTopLevelItem( currentRow ) );
394 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