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( 1 );
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 ) );
165 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
166 mWidth->setText( QStringLiteral(
"10" ) );
168 mPrecision->setEnabled(
false );
169 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
172 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
173 mWidth->setText( QStringLiteral(
"30" ) );
174 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
175 mPrecision->setText( QStringLiteral(
"6" ) );
176 mPrecision->setEnabled(
true );
177 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
181 mWidth->setEnabled(
false );
183 mPrecision->setEnabled(
false );
187 mWidth->setEnabled(
false );
189 mPrecision->setEnabled(
false );
193 mWidth->setEnabled(
false );
195 mPrecision->setEnabled(
false );
199 mWidth->setEnabled(
false );
201 mPrecision->setEnabled(
false );
210 mWidth->setEnabled(
false );
212 mPrecision->setEnabled(
false );
216 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
223 mCrsSelector->setCrs(
crs );
228 return mCrsSelector->crs();
233 return mNameLineEdit->text();
236void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
238 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
241void QgsNewMemoryLayerDialog::selectionChanged()
243 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
250 QTreeWidgetItemIterator it( mAttributeView );
253 const QString name( ( *it )->text( 0 ) );
254 const QString
typeName( ( *it )->text( 1 ) );
255 const int width = ( *it )->text( 2 ).toInt();
256 const int precision = ( *it )->text( 3 ).toInt();
257 QVariant::Type fieldType = QVariant::Invalid;
258 QVariant::Type fieldSubType = QVariant::Invalid;
259 if (
typeName == QLatin1String(
"string" ) )
260 fieldType = QVariant::String;
261 else if (
typeName == QLatin1String(
"integer" ) )
262 fieldType = QVariant::Int;
263 else if (
typeName == QLatin1String(
"double" ) )
264 fieldType = QVariant::Double;
265 else if (
typeName == QLatin1String(
"bool" ) )
266 fieldType = QVariant::Bool;
267 else if (
typeName == QLatin1String(
"date" ) )
268 fieldType = QVariant::Date;
269 else if (
typeName == QLatin1String(
"time" ) )
270 fieldType = QVariant::Time;
271 else if (
typeName == QLatin1String(
"datetime" ) )
272 fieldType = QVariant::DateTime;
273 else if (
typeName == QLatin1String(
"binary" ) )
274 fieldType = QVariant::ByteArray;
275 else if (
typeName == QLatin1String(
"stringlist" ) )
277 fieldType = QVariant::StringList;
278 fieldSubType = QVariant::String;
280 else if (
typeName == QLatin1String(
"integerlist" ) )
282 fieldType = QVariant::List;
283 fieldSubType = QVariant::Int;
285 else if (
typeName == QLatin1String(
"doublelist" ) )
287 fieldType = QVariant::List;
288 fieldSubType = QVariant::Double;
290 else if (
typeName == QLatin1String(
"integer64list" ) )
292 fieldType = QVariant::List;
293 fieldSubType = QVariant::LongLong;
295 else if (
typeName == QLatin1String(
"map" ) )
296 fieldType = QVariant::Map;
306void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
308 if ( !mFieldNameEdit->text().isEmpty() )
310 const QString fieldName = mFieldNameEdit->text();
311 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
312 const QString width = mWidth->text();
313 const QString
precision = mPrecision->text();
314 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
316 mFieldNameEdit->clear();
320void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
322 delete mAttributeView->currentItem();
325void QgsNewMemoryLayerDialog::showHelp()
327 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 QString translatedDisplayString(Type type) SIP_HOLDGIL
Returns a translated display string type for a WKB type, e.g., the geometry name used in WKT geometry...
Type
The WKB type describes the number of dimensions a geometry has.
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
const QgsCoordinateReferenceSystem & crs