41 dialog.
setCrs( defaultCrs );
42 if ( dialog.exec() == QDialog::Rejected )
49 const QString name = dialog.
layerName().isEmpty() ? tr(
"New scratch layer" ) : dialog.
layerName();
55 : QDialog( parent, fl )
60 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_currentIndexChanged( 0 );
101 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
102 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
104 mAddAttributeButton->setEnabled(
false );
105 mRemoveAttributeButton->setEnabled(
false );
107 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
108 mOkButton->setEnabled(
false );
110 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
111 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
112 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
113 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
114 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
115 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
116 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
118 mNameLineEdit->selectAll();
119 mNameLineEdit->setFocus();
126 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
130 if ( mGeometryWithZCheckBox->isChecked() )
132 if ( mGeometryWithMCheckBox->isChecked() )
139void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
142 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
145 mGeometryWithZCheckBox->setEnabled( isSpatial );
146 mGeometryWithMCheckBox->setEnabled( isSpatial );
147 mCrsSelector->setEnabled( isSpatial );
149 const bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
150 mOkButton->setEnabled( ok );
153void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int index )
158 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
159 mWidth->setText( QStringLiteral(
"255" ) );
161 mPrecision->setEnabled(
false );
162 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
163 mWidth->setEnabled(
true );
166 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
167 mWidth->setText( QStringLiteral(
"10" ) );
169 mPrecision->setEnabled(
false );
170 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
171 mWidth->setEnabled(
true );
174 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
175 mWidth->setText( QStringLiteral(
"30" ) );
176 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
177 mPrecision->setText( QStringLiteral(
"6" ) );
178 mPrecision->setEnabled(
true );
179 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
180 mWidth->setEnabled(
true );
184 mWidth->setEnabled(
false );
186 mPrecision->setEnabled(
false );
190 mWidth->setEnabled(
false );
192 mPrecision->setEnabled(
false );
196 mWidth->setEnabled(
false );
198 mPrecision->setEnabled(
false );
202 mWidth->setEnabled(
false );
204 mPrecision->setEnabled(
false );
213 mWidth->setEnabled(
false );
215 mPrecision->setEnabled(
false );
219 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
226 mCrsSelector->setCrs(
crs );
231 return mCrsSelector->crs();
236 return mNameLineEdit->text();
239void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
241 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
244void QgsNewMemoryLayerDialog::selectionChanged()
246 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
253 QTreeWidgetItemIterator it( mAttributeView );
256 const QString name( ( *it )->text( 0 ) );
257 const QString
typeName( ( *it )->text( 1 ) );
258 const int width = ( *it )->text( 2 ).toInt();
259 const int precision = ( *it )->text( 3 ).toInt();
260 QVariant::Type fieldType = QVariant::Invalid;
261 QVariant::Type fieldSubType = QVariant::Invalid;
262 if (
typeName == QLatin1String(
"string" ) )
263 fieldType = QVariant::String;
264 else if (
typeName == QLatin1String(
"integer" ) )
265 fieldType = QVariant::Int;
266 else if (
typeName == QLatin1String(
"double" ) )
267 fieldType = QVariant::Double;
268 else if (
typeName == QLatin1String(
"bool" ) )
269 fieldType = QVariant::Bool;
270 else if (
typeName == QLatin1String(
"date" ) )
271 fieldType = QVariant::Date;
272 else if (
typeName == QLatin1String(
"time" ) )
273 fieldType = QVariant::Time;
274 else if (
typeName == QLatin1String(
"datetime" ) )
275 fieldType = QVariant::DateTime;
276 else if (
typeName == QLatin1String(
"binary" ) )
277 fieldType = QVariant::ByteArray;
278 else if (
typeName == QLatin1String(
"stringlist" ) )
280 fieldType = QVariant::StringList;
281 fieldSubType = QVariant::String;
283 else if (
typeName == QLatin1String(
"integerlist" ) )
285 fieldType = QVariant::List;
286 fieldSubType = QVariant::Int;
288 else if (
typeName == QLatin1String(
"doublelist" ) )
290 fieldType = QVariant::List;
291 fieldSubType = QVariant::Double;
293 else if (
typeName == QLatin1String(
"integer64list" ) )
295 fieldType = QVariant::List;
296 fieldSubType = QVariant::LongLong;
298 else if (
typeName == QLatin1String(
"map" ) )
299 fieldType = QVariant::Map;
309void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
311 if ( !mFieldNameEdit->text().isEmpty() )
313 const QString fieldName = mFieldNameEdit->text();
314 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
315 const QString width = mWidth->text();
316 const QString
precision = mPrecision->text();
317 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
319 mFieldNameEdit->clear();
323void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
325 delete mAttributeView->currentItem();
328void QgsNewMemoryLayerDialog::showHelp()
330 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
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)
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(QgsWkbTypes::Type type)
Returns the icon for a vector layer whose geometry type is provided.
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) SIP_FACTORY
Creates a new memory layer using the specified parameters.
QgsWkbTypes::Type selectedType() const
Returns the selected geometry type.
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.
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 Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Type
The WKB type describes the number of dimensions a geometry has.
static QString translatedDisplayString(Type type)
Returns a translated display string type for a WKB type, e.g., the geometry name used in WKT geometry...
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
const QgsCoordinateReferenceSystem & crs