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" ) );
74 for (
const auto type : geomTypes )
76 mGeometryTypeBox->setCurrentIndex( -1 );
78 mGeometryWithZCheckBox->setEnabled(
false );
79 mGeometryWithMCheckBox->setEnabled(
false );
80 mCrsSelector->setEnabled(
false );
81 mCrsSelector->setShowAccuracyWarnings(
true );
96 mTypeBox->addItem(
QgsFields::iconForFieldType( QVariant::UserType, QVariant::Invalid, QStringLiteral(
"geometry" ) ), tr(
"Geometry" ),
"geometry" );
97 mTypeBox_currentIndexChanged( 1 );
99 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
100 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
102 mAddAttributeButton->setEnabled(
false );
103 mRemoveAttributeButton->setEnabled(
false );
105 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
106 mOkButton->setEnabled(
false );
108 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
109 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
110 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
111 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
112 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
113 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
115 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
117 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QgsNewMemoryLayerDialog::reject );
119 mNameLineEdit->selectAll();
120 mNameLineEdit->setFocus();
127 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
131 if ( mGeometryWithZCheckBox->isChecked() )
133 if ( mGeometryWithMCheckBox->isChecked() )
140void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
143 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
146 mGeometryWithZCheckBox->setEnabled( isSpatial );
147 mGeometryWithMCheckBox->setEnabled( isSpatial );
148 mCrsSelector->setEnabled( isSpatial );
150 const bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
151 mOkButton->setEnabled( ok );
154void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int )
156 const QString fieldType = mTypeBox->currentData().toString();
157 if ( fieldType == QLatin1String(
"string" ) )
159 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
160 mWidth->setText( QStringLiteral(
"255" ) );
162 mPrecision->setEnabled(
false );
163 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
165 else if ( fieldType == QLatin1String(
"integer" ) )
167 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
168 mWidth->setText( QStringLiteral(
"10" ) );
170 mPrecision->setEnabled(
false );
171 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
173 else if ( fieldType == QLatin1String(
"double" ) )
175 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
176 mWidth->setText( QStringLiteral(
"30" ) );
177 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
178 mPrecision->setText( QStringLiteral(
"6" ) );
179 mPrecision->setEnabled(
true );
180 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
182 else if ( fieldType == QLatin1String(
"bool" ) )
185 mWidth->setEnabled(
false );
187 mPrecision->setEnabled(
false );
189 else if ( fieldType == QLatin1String(
"date" ) )
192 mWidth->setEnabled(
false );
194 mPrecision->setEnabled(
false );
196 else if ( fieldType == QLatin1String(
"time" ) )
199 mWidth->setEnabled(
false );
201 mPrecision->setEnabled(
false );
203 else if ( fieldType == QLatin1String(
"datetime" ) )
206 mWidth->setEnabled(
false );
208 mPrecision->setEnabled(
false );
210 else if ( fieldType == QStringLiteral(
"binary" )
211 || fieldType == QStringLiteral(
"stringlist" )
212 || fieldType == QStringLiteral(
"integerlist" )
213 || fieldType == QStringLiteral(
"doublelist" )
214 || fieldType == QStringLiteral(
"integer64list" )
215 || fieldType == QStringLiteral(
"map" )
216 || fieldType == QLatin1String(
"geometry" ) )
219 mWidth->setEnabled(
false );
221 mPrecision->setEnabled(
false );
225 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
231 mCrsSelector->setCrs(
crs );
236 return mCrsSelector->crs();
241 return mNameLineEdit->text();
244void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
246 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
249void QgsNewMemoryLayerDialog::selectionChanged()
251 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
258 QTreeWidgetItemIterator it( mAttributeView );
261 const QString name( ( *it )->text( 0 ) );
262 const QString
typeName( ( *it )->text( 1 ) );
263 const int width = ( *it )->text( 2 ).toInt();
264 const int precision = ( *it )->text( 3 ).toInt();
265 QVariant::Type fieldType = QVariant::Invalid;
266 QVariant::Type fieldSubType = QVariant::Invalid;
267 if (
typeName == QLatin1String(
"string" ) )
268 fieldType = QVariant::String;
269 else if (
typeName == QLatin1String(
"integer" ) )
270 fieldType = QVariant::Int;
271 else if (
typeName == QLatin1String(
"double" ) )
272 fieldType = QVariant::Double;
273 else if (
typeName == QLatin1String(
"bool" ) )
274 fieldType = QVariant::Bool;
275 else if (
typeName == QLatin1String(
"date" ) )
276 fieldType = QVariant::Date;
277 else if (
typeName == QLatin1String(
"time" ) )
278 fieldType = QVariant::Time;
279 else if (
typeName == QLatin1String(
"datetime" ) )
280 fieldType = QVariant::DateTime;
281 else if (
typeName == QLatin1String(
"binary" ) )
282 fieldType = QVariant::ByteArray;
283 else if (
typeName == QLatin1String(
"stringlist" ) )
285 fieldType = QVariant::StringList;
286 fieldSubType = QVariant::String;
288 else if (
typeName == QLatin1String(
"integerlist" ) )
290 fieldType = QVariant::List;
291 fieldSubType = QVariant::Int;
293 else if (
typeName == QLatin1String(
"doublelist" ) )
295 fieldType = QVariant::List;
296 fieldSubType = QVariant::Double;
298 else if (
typeName == QLatin1String(
"integer64list" ) )
300 fieldType = QVariant::List;
301 fieldSubType = QVariant::LongLong;
303 else if (
typeName == QLatin1String(
"map" ) )
304 fieldType = QVariant::Map;
305 else if (
typeName == QLatin1String(
"geometry" ) )
306 fieldType = QVariant::UserType;
318 if ( !mFieldNameEdit->text().trimmed().isEmpty() )
320 const QString currentFieldName = mFieldNameEdit->text();
321 if (
fields().lookupField( currentFieldName ) == -1 )
323 if ( QMessageBox::question(
this, tr(
"New Temporary Scratch Layer" ),
324 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 ),
325 QMessageBox::Ok | QMessageBox::Cancel ) != QMessageBox::Ok )
335void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
337 if ( !mFieldNameEdit->text().isEmpty() )
339 const QString fieldName = mFieldNameEdit->text();
340 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
341 const QString width = mWidth->text();
342 const QString
precision = mPrecision->text();
343 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
345 mFieldNameEdit->clear();
349void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
351 delete mAttributeView->currentItem();
354void QgsNewMemoryLayerDialog::showHelp()
356 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
WkbType
The WKB type describes the number of dimensions a geometry has.
@ CompoundCurve
CompoundCurve.
@ MultiPolygon
MultiPolygon.
@ MultiLineString
MultiLineString.
@ CurvePolygon
CurvePolygon.
@ 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, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
static QIcon iconForFieldType(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid, 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(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns a user-friendly translated string representing a QVariant type.
Represents a vector layer which manages a vector based data sets.
static Qgis::WkbType addZ(Qgis::WkbType type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
static Qgis::WkbType addM(Qgis::WkbType type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
static QString translatedDisplayString(Qgis::WkbType type) SIP_HOLDGIL
Returns a translated display string type for a WKB type, e.g., the geometry name used in WKT geometry...
const QgsCoordinateReferenceSystem & crs