QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgscrscache.h"
25 
26 #include <QPushButton>
27 #include <QComboBox>
28 #include <QLibrary>
29 #include <QSettings>
30 #include <QUuid>
31 #include <QFileDialog>
32 
34 {
35  QgsNewMemoryLayerDialog dialog( parent );
36  if ( dialog.exec() == QDialog::Rejected )
37  {
38  return nullptr;
39  }
40 
41  QGis::WkbType geometrytype = dialog.selectedType();
42 
43  QString geomType;
44  switch ( geometrytype )
45  {
46  case QGis::WKBPoint:
47  geomType = "point";
48  break;
50  geomType = "linestring";
51  break;
52  case QGis::WKBPolygon:
53  geomType = "polygon";
54  break;
56  geomType = "multipoint";
57  break;
59  geomType = "multilinestring";
60  break;
62  geomType = "multipolygon";
63  break;
65  geomType = "none";
66  break;
67  default:
68  geomType = "point";
69  }
70 
71  QString layerProperties = QString( "%1?" ).arg( geomType );
72  if ( QGis::WKBNoGeometry != geometrytype )
73  layerProperties.append( QString( "crs=%1&" ).arg( dialog.crs().authid() ) );
74  layerProperties.append( QString( "memoryid=%1" ).arg( QUuid::createUuid().toString() ) );
75 
76  QString name = dialog.layerName().isEmpty() ? tr( "New scratch layer" ) : dialog.layerName();
77  QgsVectorLayer* newLayer = new QgsVectorLayer( layerProperties, name, QString( "memory" ) );
78  return newLayer;
79 }
80 
82  : QDialog( parent, fl )
83 {
84  setupUi( this );
85 
86  QSettings settings;
87  restoreGeometry( settings.value( "/Windows/NewMemoryLayer/geometry" ).toByteArray() );
88 
89  mPointRadioButton->setChecked( true );
90 
91  QgsCoordinateReferenceSystem defaultCrs = QgsCRSCache::instance()->crsByOgcWmsCrs( settings.value( "/Projections/layerDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString() );
92  defaultCrs.validate();
93  mCrsSelector->setCrs( defaultCrs );
94 
95  mNameLineEdit->setText( tr( "New scratch layer" ) );
96 }
97 
99 {
100  QSettings settings;
101  settings.setValue( "/Windows/NewMemoryLayer/geometry", saveGeometry() );
102 }
103 
105 {
106  if ( !buttonGroupGeometry->isChecked() )
107  {
108  return QGis::WKBNoGeometry;
109  }
110  else if ( mPointRadioButton->isChecked() )
111  {
112  return QGis::WKBPoint;
113  }
114  else if ( mLineRadioButton->isChecked() )
115  {
116  return QGis::WKBLineString;
117  }
118  else if ( mPolygonRadioButton->isChecked() )
119  {
120  return QGis::WKBPolygon;
121  }
122  else if ( mMultiPointRadioButton->isChecked() )
123  {
124  return QGis::WKBMultiPoint;
125  }
126  else if ( mMultiLineRadioButton->isChecked() )
127  {
129  }
130  else if ( mMultiPolygonRadioButton->isChecked() )
131  {
132  return QGis::WKBMultiPolygon;
133  }
134  return QGis::WKBUnknown;
135 }
136 
138 {
139  return mCrsSelector->crs();
140 }
141 
143 {
144  return mNameLineEdit->text();
145 }
QByteArray toByteArray() const
QgsNewMemoryLayerDialog(QWidget *parent=nullptr, const Qt::WindowFlags &fl=QgisGui::ModalDialogFlags)
QgsCoordinateReferenceSystem crs() const
Returns the selected crs.
QString & append(QChar ch)
void setupUi(QWidget *widget)
QgsCoordinateReferenceSystem crsByOgcWmsCrs(const QString &ogcCrs) const
Returns the CRS from a given OGC WMS-format Coordinate Reference System string.
void validate()
Perform some validation on this CRS.
int exec()
static QgsVectorLayer * runAndCreateLayer(QWidget *parent=nullptr)
Runs the dialoag and creates a new memory layer.
WkbType
Used for symbology operations.
Definition: qgis.h:61
QString tr(const char *sourceText, const char *disambiguation, int n)
void setValue(const QString &key, const QVariant &value)
const char * name() const
bool restoreGeometry(const QByteArray &geometry)
QGis::WkbType selectedType() const
Returns the selected geometry type.
bool isEmpty() const
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:74
QVariant value(const QString &key, const QVariant &defaultValue) const
QByteArray saveGeometry() const
Class for storing a coordinate reference system (CRS)
typedef WindowFlags
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
static QgsCRSCache * instance()
Returns a pointer to the QgsCRSCache singleton.
Definition: qgscrscache.cpp:91
QString authid() const
Returns the authority identifier for the CRS, which includes both the authority (eg EPSG) and the CRS...
QUuid createUuid()
QString layerName() const
Returns the layer name.