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 );
98 mTypeBox_currentIndexChanged( 1 );
100 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
101 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
103 mAddAttributeButton->setEnabled(
false );
104 mRemoveAttributeButton->setEnabled(
false );
106 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
107 mOkButton->setEnabled(
false );
109 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
110 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
111 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
112 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
113 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
114 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
115 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
122 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
126 if ( mGeometryWithZCheckBox->isChecked() )
128 if ( mGeometryWithMCheckBox->isChecked() )
135 void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
138 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
141 mGeometryWithZCheckBox->setEnabled( isSpatial );
142 mGeometryWithMCheckBox->setEnabled( isSpatial );
143 mCrsSelector->setEnabled( isSpatial );
145 const bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
146 mOkButton->setEnabled( ok );
149 void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int index )
154 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
155 mWidth->setText( QStringLiteral(
"255" ) );
157 mPrecision->setEnabled(
false );
158 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
161 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
162 mWidth->setText( QStringLiteral(
"10" ) );
164 mPrecision->setEnabled(
false );
165 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
168 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
169 mWidth->setText( QStringLiteral(
"30" ) );
170 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
171 mPrecision->setText( QStringLiteral(
"6" ) );
172 mPrecision->setEnabled(
true );
173 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
177 mWidth->setEnabled(
false );
179 mPrecision->setEnabled(
false );
183 mWidth->setEnabled(
false );
185 mPrecision->setEnabled(
false );
189 mWidth->setEnabled(
false );
191 mPrecision->setEnabled(
false );
195 mWidth->setEnabled(
false );
197 mPrecision->setEnabled(
false );
205 mWidth->setEnabled(
false );
207 mPrecision->setEnabled(
false );
211 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
218 mCrsSelector->setCrs(
crs );
223 return mCrsSelector->crs();
228 return mNameLineEdit->text();
231 void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
233 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
236 void QgsNewMemoryLayerDialog::selectionChanged()
238 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
245 QTreeWidgetItemIterator it( mAttributeView );
248 const QString name( ( *it )->text( 0 ) );
249 const QString
typeName( ( *it )->text( 1 ) );
250 const int width = ( *it )->text( 2 ).toInt();
251 const int precision = ( *it )->text( 3 ).toInt();
252 QVariant::Type fieldType = QVariant::Invalid;
253 QVariant::Type fieldSubType = QVariant::Invalid;
254 if (
typeName == QLatin1String(
"string" ) )
255 fieldType = QVariant::String;
256 else if (
typeName == QLatin1String(
"integer" ) )
257 fieldType = QVariant::Int;
258 else if (
typeName == QLatin1String(
"double" ) )
259 fieldType = QVariant::Double;
260 else if (
typeName == QLatin1String(
"bool" ) )
261 fieldType = QVariant::Bool;
262 else if (
typeName == QLatin1String(
"date" ) )
263 fieldType = QVariant::Date;
264 else if (
typeName == QLatin1String(
"time" ) )
265 fieldType = QVariant::Time;
266 else if (
typeName == QLatin1String(
"datetime" ) )
267 fieldType = QVariant::DateTime;
268 else if (
typeName == QLatin1String(
"binary" ) )
269 fieldType = QVariant::ByteArray;
270 else if (
typeName == QLatin1String(
"stringlist" ) )
272 fieldType = QVariant::StringList;
273 fieldSubType = QVariant::String;
275 else if (
typeName == QLatin1String(
"integerlist" ) )
277 fieldType = QVariant::List;
278 fieldSubType = QVariant::Int;
280 else if (
typeName == QLatin1String(
"doublelist" ) )
282 fieldType = QVariant::List;
283 fieldSubType = QVariant::Double;
285 else if (
typeName == QLatin1String(
"integer64list" ) )
287 fieldType = QVariant::List;
288 fieldSubType = QVariant::LongLong;
299 void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
301 if ( !mFieldNameEdit->text().isEmpty() )
303 const QString fieldName = mFieldNameEdit->text();
304 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
305 const QString width = mWidth->text();
306 const QString
precision = mPrecision->text();
307 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
309 mFieldNameEdit->clear();
313 void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
315 delete mAttributeView->currentItem();
318 void QgsNewMemoryLayerDialog::showHelp()
320 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