QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsnewmemorylayerdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnewmemorylayerdialog.cpp
3  -------------------
4  begin : September 2014
5  copyright : (C) 2014 by Nyall Dawson, Marco Hugentobler
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsapplication.h"
20 #include "qgis.h"
22 #include "qgsproviderregistry.h"
23 #include "qgsvectordataprovider.h"
24 #include "qgsvectorlayer.h"
25 #include "qgssettings.h"
26 #include "qgsmemoryproviderutils.h"
27 #include "qgsgui.h"
28 
29 #include <QPushButton>
30 #include <QComboBox>
31 #include <QUuid>
32 #include <QFileDialog>
33 
35 {
36  QgsNewMemoryLayerDialog dialog( parent );
37  dialog.setCrs( defaultCrs );
38  if ( dialog.exec() == QDialog::Rejected )
39  {
40  return nullptr;
41  }
42 
43  QgsWkbTypes::Type geometrytype = dialog.selectedType();
44  QString name = dialog.layerName().isEmpty() ? tr( "New scratch layer" ) : dialog.layerName();
45  QgsVectorLayer *newLayer = QgsMemoryProviderUtils::createMemoryLayer( name, QgsFields(), geometrytype, dialog.crs() );
46  return newLayer;
47 }
48 
49 QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFlags fl )
50  : QDialog( parent, fl )
51 {
52  setupUi( this );
54 
55  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No geometry" ), QgsWkbTypes::NoGeometry );
56  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "Point" ), QgsWkbTypes::Point );
57  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "LineString / CompoundCurve" ), QgsWkbTypes::LineString );
58  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "Polygon / CurvePolygon" ), QgsWkbTypes::Polygon );
59  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "MultiPoint" ), QgsWkbTypes::MultiPoint );
60  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "MultiLineString / MultiCurve" ), QgsWkbTypes::MultiLineString );
61  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "MultiPolygon / MultiSurface" ), QgsWkbTypes::MultiPolygon );
62 
63  mGeometryWithZCheckBox->setEnabled( false );
64  mGeometryWithMCheckBox->setEnabled( false );
65 
66  mNameLineEdit->setText( tr( "New scratch layer" ) );
67 
68  connect( mGeometryTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
69  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsNewMemoryLayerDialog::showHelp );
70  geometryTypeChanged( mGeometryTypeBox->currentIndex() );
71 }
72 
74 {
76  geomType = static_cast<QgsWkbTypes::Type>
77  ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
78 
79  if ( geomType != QgsWkbTypes::Unknown && geomType != QgsWkbTypes::NoGeometry )
80  {
81  if ( mGeometryWithZCheckBox->isChecked() )
82  geomType = QgsWkbTypes::addZ( geomType );
83  if ( mGeometryWithMCheckBox->isChecked() )
84  geomType = QgsWkbTypes::addM( geomType );
85  }
86 
87  return geomType;
88 }
89 
90 void QgsNewMemoryLayerDialog::geometryTypeChanged( int )
91 {
92  QgsWkbTypes::Type geomType = static_cast<QgsWkbTypes::Type>
93  ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
94 
95  bool isSpatial = geomType != QgsWkbTypes::NoGeometry;
96  mGeometryWithZCheckBox->setEnabled( isSpatial );
97  mGeometryWithMCheckBox->setEnabled( isSpatial );
98  mCrsSelector->setEnabled( isSpatial );
99 }
100 
102 {
103  mCrsSelector->setCrs( crs );
104 }
105 
107 {
108  return mCrsSelector->crs();
109 }
110 
112 {
113  return mNameLineEdit->text();
114 }
115 
116 void QgsNewMemoryLayerDialog::showHelp()
117 {
118  QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
119 }
QgsCoordinateReferenceSystem crs() const
Returns the selected CRS for the new layer.
QgsWkbTypes::Type selectedType() const
Returns the selected geometry type.
Container of fields for a vector layer.
Definition: qgsfields.h:42
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QgsNewMemoryLayerDialog(QWidget *parent SIP_TRANSFERTHIS=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
New dialog constructor.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1038
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1013
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.
This class represents a coordinate reference system (CRS).
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...
Definition: qgsgui.cpp:127
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
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.
Represents a vector layer which manages a vector based data sets.
QString layerName() const
Returns the layer name.