QGIS API Documentation  2.14.0-Essen
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 
25 #include <QPushButton>
26 #include <QComboBox>
27 #include <QLibrary>
28 #include <QSettings>
29 #include <QUuid>
30 #include <QFileDialog>
31 
33 {
34  QgsNewMemoryLayerDialog dialog( parent );
35  if ( dialog.exec() == QDialog::Rejected )
36  {
37  return nullptr;
38  }
39 
40  QGis::WkbType geometrytype = dialog.selectedType();
41 
42  QString geomType;
43  switch ( geometrytype )
44  {
45  case QGis::WKBPoint:
46  geomType = "point";
47  break;
49  geomType = "linestring";
50  break;
51  case QGis::WKBPolygon:
52  geomType = "polygon";
53  break;
55  geomType = "multipoint";
56  break;
58  geomType = "multilinestring";
59  break;
61  geomType = "multipolygon";
62  break;
64  geomType = "none";
65  break;
66  default:
67  geomType = "point";
68  }
69 
70  QString layerProperties = QString( "%1?" ).arg( geomType );
71  if ( QGis::WKBNoGeometry != geometrytype )
72  layerProperties.append( QString( "crs=%1&" ).arg( dialog.crs().authid() ) );
73  layerProperties.append( QString( "memoryid=%1" ).arg( QUuid::createUuid().toString() ) );
74 
75  QString name = dialog.layerName().isEmpty() ? tr( "New scratch layer" ) : dialog.layerName();
76  QgsVectorLayer* newLayer = new QgsVectorLayer( layerProperties, name, QString( "memory" ) );
77  return newLayer;
78 }
79 
81  : QDialog( parent, fl )
82 {
83  setupUi( this );
84 
85  QSettings settings;
86  restoreGeometry( settings.value( "/Windows/NewMemoryLayer/geometry" ).toByteArray() );
87 
88  mPointRadioButton->setChecked( true );
89 
91  defaultCrs.createFromOgcWmsCrs( 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 }
QGis::WkbType selectedType() const
Returns the selected geometry type.
QByteArray toByteArray() const
QgsNewMemoryLayerDialog(QWidget *parent=nullptr, const Qt::WindowFlags &fl=QgisGui::ModalDialogFlags)
QString & append(QChar ch)
void setupUi(QWidget *widget)
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:57
QString tr(const char *sourceText, const char *disambiguation, int n)
void setValue(const QString &key, const QVariant &value)
bool createFromOgcWmsCrs(QString theCrs)
Set up this CRS from the given OGC CRS.
const char * name() const
bool restoreGeometry(const QByteArray &geometry)
bool isEmpty() const
const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:74
QString layerName() const
Returns the layer name.
QVariant value(const QString &key, const QVariant &defaultValue) const
QByteArray saveGeometry() const
Class for storing a coordinate reference system (CRS)
QString authid() const
Returns the authority identifier for the CRS, which includes both the authority (eg EPSG) and the CRS...
QgsCoordinateReferenceSystem crs() const
Returns the selected 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
QUuid createUuid()