QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 <QLibrary>
32 #include <QUuid>
33 #include <QFileDialog>
34 
36 {
37  QgsNewMemoryLayerDialog dialog( parent );
38  dialog.setCrs( defaultCrs );
39  if ( dialog.exec() == QDialog::Rejected )
40  {
41  return nullptr;
42  }
43 
44  QgsWkbTypes::Type geometrytype = dialog.selectedType();
45  QString name = dialog.layerName().isEmpty() ? tr( "New scratch layer" ) : dialog.layerName();
46  QgsVectorLayer *newLayer = QgsMemoryProviderUtils::createMemoryLayer( name, QgsFields(), geometrytype, dialog.crs() );
47  return newLayer;
48 }
49 
50 QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFlags fl )
51  : QDialog( parent, fl )
52 {
53  setupUi( this );
55 
56  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No geometry" ), QgsWkbTypes::NoGeometry );
57  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "Point" ), QgsWkbTypes::Point );
58  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "LineString / CompoundCurve" ), QgsWkbTypes::LineString );
59  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "Polygon / CurvePolygon" ), QgsWkbTypes::Polygon );
60  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "MultiPoint" ), QgsWkbTypes::MultiPoint );
61  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "MultiLineString / MultiCurve" ), QgsWkbTypes::MultiLineString );
62  mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "MultiPolygon / MultiSurface" ), QgsWkbTypes::MultiPolygon );
63 
64  mGeometryWithZCheckBox->setEnabled( false );
65  mGeometryWithMCheckBox->setEnabled( false );
66 
67  mNameLineEdit->setText( tr( "New scratch layer" ) );
68 
69  connect( mGeometryTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
70  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsNewMemoryLayerDialog::showHelp );
71  geometryTypeChanged( mGeometryTypeBox->currentIndex() );
72 }
73 
75 {
77  geomType = static_cast<QgsWkbTypes::Type>
78  ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
79 
80  if ( geomType != QgsWkbTypes::Unknown && geomType != QgsWkbTypes::NoGeometry )
81  {
82  if ( mGeometryWithZCheckBox->isChecked() )
83  geomType = QgsWkbTypes::addZ( geomType );
84  if ( mGeometryWithMCheckBox->isChecked() )
85  geomType = QgsWkbTypes::addM( geomType );
86  }
87 
88  return geomType;
89 }
90 
91 void QgsNewMemoryLayerDialog::geometryTypeChanged( int )
92 {
93  QgsWkbTypes::Type geomType = static_cast<QgsWkbTypes::Type>
94  ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
95 
96  bool isSpatial = geomType != QgsWkbTypes::NoGeometry;
97  mGeometryWithZCheckBox->setEnabled( isSpatial );
98  mGeometryWithMCheckBox->setEnabled( isSpatial );
99  mCrsSelector->setEnabled( isSpatial );
100 }
101 
103 {
104  mCrsSelector->setCrs( crs );
105 }
106 
108 {
109  return mCrsSelector->crs();
110 }
111 
113 {
114  return mNameLineEdit->text();
115 }
116 
117 void QgsNewMemoryLayerDialog::showHelp()
118 {
119  QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
120 }
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:892
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:867
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:104
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.