32 #include <QPushButton>
35 #include <QFileDialog>
40 dialog.
setCrs( defaultCrs );
41 if ( dialog.exec() == QDialog::Rejected )
48 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 );
92 mTypeBox_currentIndexChanged( 1 );
94 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
95 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
97 mAddAttributeButton->setEnabled(
false );
98 mRemoveAttributeButton->setEnabled(
false );
100 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
101 mOkButton->setEnabled(
false );
103 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
104 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
105 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
106 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
107 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
108 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
109 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
116 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
120 if ( mGeometryWithZCheckBox->isChecked() )
122 if ( mGeometryWithMCheckBox->isChecked() )
129 void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
132 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
135 mGeometryWithZCheckBox->setEnabled( isSpatial );
136 mGeometryWithMCheckBox->setEnabled( isSpatial );
137 mCrsSelector->setEnabled( isSpatial );
139 bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
140 mOkButton->setEnabled( ok );
143 void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int index )
148 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
149 mWidth->setText( QStringLiteral(
"255" ) );
151 mPrecision->setEnabled(
false );
152 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
155 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
156 mWidth->setText( QStringLiteral(
"10" ) );
158 mPrecision->setEnabled(
false );
159 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
162 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
163 mWidth->setText( QStringLiteral(
"30" ) );
164 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
165 mPrecision->setText( QStringLiteral(
"6" ) );
166 mPrecision->setEnabled(
true );
167 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
171 mWidth->setEnabled(
false );
173 mPrecision->setEnabled(
false );
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 );
201 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
208 mCrsSelector->setCrs(
crs );
213 return mCrsSelector->crs();
218 return mNameLineEdit->text();
221 void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
223 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
226 void QgsNewMemoryLayerDialog::selectionChanged()
228 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
235 QTreeWidgetItemIterator it( mAttributeView );
238 QString name( ( *it )->text( 0 ) );
239 QString
typeName( ( *it )->text( 1 ) );
240 int width = ( *it )->text( 2 ).toInt();
241 int precision = ( *it )->text( 3 ).toInt();
242 QVariant::Type fieldType = QVariant::Invalid;
243 if (
typeName == QLatin1String(
"string" ) )
244 fieldType = QVariant::String;
245 else if (
typeName == QLatin1String(
"integer" ) )
246 fieldType = QVariant::Int;
247 else if (
typeName == QLatin1String(
"double" ) )
248 fieldType = QVariant::Double;
249 else if (
typeName == QLatin1String(
"bool" ) )
250 fieldType = QVariant::Bool;
251 else if (
typeName == QLatin1String(
"date" ) )
252 fieldType = QVariant::Date;
253 else if (
typeName == QLatin1String(
"time" ) )
254 fieldType = QVariant::Time;
255 else if (
typeName == QLatin1String(
"datetime" ) )
256 fieldType = QVariant::DateTime;
266 void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
268 if ( !mFieldNameEdit->text().isEmpty() )
270 QString fieldName = mFieldNameEdit->text();
271 QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
272 QString width = mWidth->text();
274 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
276 mFieldNameEdit->clear();
280 void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
282 delete mAttributeView->currentItem();
285 void QgsNewMemoryLayerDialog::showHelp()
287 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
static QIcon getThemeIcon(const QString &name)
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())
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