33 #include <QPushButton>
36 #include <QFileDialog>
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 );
123 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
127 if ( mGeometryWithZCheckBox->isChecked() )
129 if ( mGeometryWithMCheckBox->isChecked() )
136 void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
139 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
142 mGeometryWithZCheckBox->setEnabled( isSpatial );
143 mGeometryWithMCheckBox->setEnabled( isSpatial );
144 mCrsSelector->setEnabled( isSpatial );
146 const bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
147 mOkButton->setEnabled( ok );
150 void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int index )
155 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
156 mWidth->setText( QStringLiteral(
"255" ) );
158 mPrecision->setEnabled(
false );
159 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
162 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
163 mWidth->setText( QStringLiteral(
"10" ) );
165 mPrecision->setEnabled(
false );
166 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
169 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
170 mWidth->setText( QStringLiteral(
"30" ) );
171 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
172 mPrecision->setText( QStringLiteral(
"6" ) );
173 mPrecision->setEnabled(
true );
174 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
178 mWidth->setEnabled(
false );
180 mPrecision->setEnabled(
false );
184 mWidth->setEnabled(
false );
186 mPrecision->setEnabled(
false );
190 mWidth->setEnabled(
false );
192 mPrecision->setEnabled(
false );
196 mWidth->setEnabled(
false );
198 mPrecision->setEnabled(
false );
207 mWidth->setEnabled(
false );
209 mPrecision->setEnabled(
false );
213 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
220 mCrsSelector->setCrs(
crs );
225 return mCrsSelector->crs();
230 return mNameLineEdit->text();
233 void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
235 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
238 void QgsNewMemoryLayerDialog::selectionChanged()
240 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
247 QTreeWidgetItemIterator it( mAttributeView );
250 const QString name( ( *it )->text( 0 ) );
251 const QString
typeName( ( *it )->text( 1 ) );
252 const int width = ( *it )->text( 2 ).toInt();
253 const int precision = ( *it )->text( 3 ).toInt();
254 QVariant::Type fieldType = QVariant::Invalid;
255 QVariant::Type fieldSubType = QVariant::Invalid;
256 if (
typeName == QLatin1String(
"string" ) )
257 fieldType = QVariant::String;
258 else if (
typeName == QLatin1String(
"integer" ) )
259 fieldType = QVariant::Int;
260 else if (
typeName == QLatin1String(
"double" ) )
261 fieldType = QVariant::Double;
262 else if (
typeName == QLatin1String(
"bool" ) )
263 fieldType = QVariant::Bool;
264 else if (
typeName == QLatin1String(
"date" ) )
265 fieldType = QVariant::Date;
266 else if (
typeName == QLatin1String(
"time" ) )
267 fieldType = QVariant::Time;
268 else if (
typeName == QLatin1String(
"datetime" ) )
269 fieldType = QVariant::DateTime;
270 else if (
typeName == QLatin1String(
"binary" ) )
271 fieldType = QVariant::ByteArray;
272 else if (
typeName == QLatin1String(
"stringlist" ) )
274 fieldType = QVariant::StringList;
275 fieldSubType = QVariant::String;
277 else if (
typeName == QLatin1String(
"integerlist" ) )
279 fieldType = QVariant::List;
280 fieldSubType = QVariant::Int;
282 else if (
typeName == QLatin1String(
"doublelist" ) )
284 fieldType = QVariant::List;
285 fieldSubType = QVariant::Double;
287 else if (
typeName == QLatin1String(
"integer64list" ) )
289 fieldType = QVariant::List;
290 fieldSubType = QVariant::LongLong;
292 else if (
typeName == QLatin1String(
"map" ) )
293 fieldType = QVariant::Map;
303 void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
305 if ( !mFieldNameEdit->text().isEmpty() )
307 const QString fieldName = mFieldNameEdit->text();
308 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
309 const QString width = mWidth->text();
310 const QString
precision = mPrecision->text();
311 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
313 mFieldNameEdit->clear();
317 void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
319 delete mAttributeView->currentItem();
322 void QgsNewMemoryLayerDialog::showHelp()
324 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