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( 0 );
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 ) );
164 mWidth->setEnabled(
true );
166 else if ( fieldType == QLatin1String(
"integer" ) )
168 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
169 mWidth->setText( QStringLiteral(
"10" ) );
171 mPrecision->setEnabled(
false );
172 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
173 mWidth->setEnabled(
true );
175 else if ( fieldType == QLatin1String(
"double" ) )
177 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
178 mWidth->setText( QStringLiteral(
"30" ) );
179 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
180 mPrecision->setText( QStringLiteral(
"6" ) );
181 mPrecision->setEnabled(
true );
182 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
183 mWidth->setEnabled(
true );
185 else if ( fieldType == QLatin1String(
"bool" ) )
188 mWidth->setEnabled(
false );
190 mPrecision->setEnabled(
false );
192 else if ( fieldType == QLatin1String(
"date" ) )
195 mWidth->setEnabled(
false );
197 mPrecision->setEnabled(
false );
199 else if ( fieldType == QLatin1String(
"time" ) )
202 mWidth->setEnabled(
false );
204 mPrecision->setEnabled(
false );
206 else if ( fieldType == QLatin1String(
"datetime" ) )
209 mWidth->setEnabled(
false );
211 mPrecision->setEnabled(
false );
213 else if ( fieldType == QStringLiteral(
"binary" )
214 || fieldType == QStringLiteral(
"stringlist" )
215 || fieldType == QStringLiteral(
"integerlist" )
216 || fieldType == QStringLiteral(
"doublelist" )
217 || fieldType == QStringLiteral(
"integer64list" )
218 || fieldType == QStringLiteral(
"map" )
219 || fieldType == QLatin1String(
"geometry" ) )
222 mWidth->setEnabled(
false );
224 mPrecision->setEnabled(
false );
234 mCrsSelector->setCrs(
crs );
239 return mCrsSelector->crs();
244 return mNameLineEdit->text();
247void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
249 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
252void QgsNewMemoryLayerDialog::selectionChanged()
254 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
261 QTreeWidgetItemIterator it( mAttributeView );
264 const QString name( ( *it )->text( 0 ) );
265 const QString
typeName( ( *it )->text( 1 ) );
266 const int width = ( *it )->text( 2 ).toInt();
267 const int precision = ( *it )->text( 3 ).toInt();
268 QVariant::Type fieldType = QVariant::Invalid;
269 QVariant::Type fieldSubType = QVariant::Invalid;
270 if (
typeName == QLatin1String(
"string" ) )
271 fieldType = QVariant::String;
272 else if (
typeName == QLatin1String(
"integer" ) )
273 fieldType = QVariant::Int;
274 else if (
typeName == QLatin1String(
"double" ) )
275 fieldType = QVariant::Double;
276 else if (
typeName == QLatin1String(
"bool" ) )
277 fieldType = QVariant::Bool;
278 else if (
typeName == QLatin1String(
"date" ) )
279 fieldType = QVariant::Date;
280 else if (
typeName == QLatin1String(
"time" ) )
281 fieldType = QVariant::Time;
282 else if (
typeName == QLatin1String(
"datetime" ) )
283 fieldType = QVariant::DateTime;
284 else if (
typeName == QLatin1String(
"binary" ) )
285 fieldType = QVariant::ByteArray;
286 else if (
typeName == QLatin1String(
"stringlist" ) )
288 fieldType = QVariant::StringList;
289 fieldSubType = QVariant::String;
291 else if (
typeName == QLatin1String(
"integerlist" ) )
293 fieldType = QVariant::List;
294 fieldSubType = QVariant::Int;
296 else if (
typeName == QLatin1String(
"doublelist" ) )
298 fieldType = QVariant::List;
299 fieldSubType = QVariant::Double;
301 else if (
typeName == QLatin1String(
"integer64list" ) )
303 fieldType = QVariant::List;
304 fieldSubType = QVariant::LongLong;
306 else if (
typeName == QLatin1String(
"map" ) )
307 fieldType = QVariant::Map;
308 else if (
typeName == QLatin1String(
"geometry" ) )
309 fieldType = QVariant::UserType;
321 if ( !mFieldNameEdit->text().trimmed().isEmpty() )
323 const QString currentFieldName = mFieldNameEdit->text();
324 if (
fields().lookupField( currentFieldName ) == -1 )
326 if ( QMessageBox::question(
this, tr(
"New Temporary Scratch Layer" ),
327 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 ),
328 QMessageBox::Ok | QMessageBox::Cancel ) != QMessageBox::Ok )
338void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
340 if ( !mFieldNameEdit->text().isEmpty() )
342 const QString fieldName = mFieldNameEdit->text();
343 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
344 const QString width = mWidth->text();
345 const QString
precision = mPrecision->text();
346 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
348 mFieldNameEdit->clear();
352void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
354 delete mAttributeView->currentItem();
357void QgsNewMemoryLayerDialog::showHelp()
359 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 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