32 #include <QPushButton>
35 #include <QFileDialog>
40 dialog.
setCrs( defaultCrs );
41 if ( dialog.exec() == QDialog::Rejected )
48 const QString name = dialog.
layerName().isEmpty() ? tr(
"New scratch layer" ) : dialog.
layerName();
54 : QDialog( parent, fl )
59 mNameLineEdit->setText( tr(
"New scratch layer" ) );
76 for (
const auto type : geomTypes )
78 mGeometryTypeBox->setCurrentIndex( -1 );
80 mGeometryWithZCheckBox->setEnabled(
false );
81 mGeometryWithMCheckBox->setEnabled(
false );
82 mCrsSelector->setEnabled(
false );
83 mCrsSelector->setShowAccuracyWarnings(
true );
93 mTypeBox_currentIndexChanged( 1 );
95 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
96 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
98 mAddAttributeButton->setEnabled(
false );
99 mRemoveAttributeButton->setEnabled(
false );
101 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
102 mOkButton->setEnabled(
false );
104 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
105 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
106 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
107 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
108 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
109 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
110 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
117 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
121 if ( mGeometryWithZCheckBox->isChecked() )
123 if ( mGeometryWithMCheckBox->isChecked() )
130 void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
133 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
136 mGeometryWithZCheckBox->setEnabled( isSpatial );
137 mGeometryWithMCheckBox->setEnabled( isSpatial );
138 mCrsSelector->setEnabled( isSpatial );
140 const bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
141 mOkButton->setEnabled( ok );
144 void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int index )
149 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
150 mWidth->setText( QStringLiteral(
"255" ) );
152 mPrecision->setEnabled(
false );
153 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
156 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
157 mWidth->setText( QStringLiteral(
"10" ) );
159 mPrecision->setEnabled(
false );
160 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
163 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
164 mWidth->setText( QStringLiteral(
"30" ) );
165 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
166 mPrecision->setText( QStringLiteral(
"6" ) );
167 mPrecision->setEnabled(
true );
168 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
172 mWidth->setEnabled(
false );
174 mPrecision->setEnabled(
false );
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 );
202 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
209 mCrsSelector->setCrs(
crs );
214 return mCrsSelector->crs();
219 return mNameLineEdit->text();
222 void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
224 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
227 void QgsNewMemoryLayerDialog::selectionChanged()
229 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
236 QTreeWidgetItemIterator it( mAttributeView );
239 const QString name( ( *it )->text( 0 ) );
240 const QString
typeName( ( *it )->text( 1 ) );
241 const int width = ( *it )->text( 2 ).toInt();
242 const int precision = ( *it )->text( 3 ).toInt();
243 QVariant::Type fieldType = QVariant::Invalid;
244 if (
typeName == QLatin1String(
"string" ) )
245 fieldType = QVariant::String;
246 else if (
typeName == QLatin1String(
"integer" ) )
247 fieldType = QVariant::Int;
248 else if (
typeName == QLatin1String(
"double" ) )
249 fieldType = QVariant::Double;
250 else if (
typeName == QLatin1String(
"bool" ) )
251 fieldType = QVariant::Bool;
252 else if (
typeName == QLatin1String(
"date" ) )
253 fieldType = QVariant::Date;
254 else if (
typeName == QLatin1String(
"time" ) )
255 fieldType = QVariant::Time;
256 else if (
typeName == QLatin1String(
"datetime" ) )
257 fieldType = QVariant::DateTime;
267 void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
269 if ( !mFieldNameEdit->text().isEmpty() )
271 const QString fieldName = mFieldNameEdit->text();
272 const QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
273 const QString width = mWidth->text();
274 const QString
precision = mPrecision->text();
275 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
277 mFieldNameEdit->clear();
281 void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
283 delete mAttributeView->currentItem();
286 void QgsNewMemoryLayerDialog::showHelp()
288 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
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 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.
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