QGIS API Documentation  3.0.2-Girona (307d082)
qgsvirtuallayerdefinition.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvirtuallayerdefinition.h
3 begin : Feb, 2015
4 copyright : (C) 2015 Hugo Mercier, Oslandia
5 email : hugo dot mercier at oslandia dot com
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSVIRTUALLAYERDEFINITION_H
18 #define QGSVIRTUALLAYERDEFINITION_H
19 
20 #include "qgis_core.h"
21 #include "qgsfields.h"
22 #include "qgis.h"
23 
31 class CORE_EXPORT QgsVirtualLayerDefinition
32 {
33  public:
34 
40  class CORE_EXPORT SourceLayer
41  {
42  public:
44  SourceLayer( const QString &name, const QString &ref )
45  : mName( name )
46  , mRef( ref )
47  {}
49  SourceLayer( const QString &name, const QString &source, const QString &provider, const QString &encoding )
50  : mName( name )
51  , mSource( source )
52  , mProvider( provider )
53  , mEncoding( encoding )
54  {}
55 
57  bool isReferenced() const { return !mRef.isEmpty(); }
58 
60  QString reference() const { return mRef; }
61 
63  QString name() const { return mName; }
64 
66  QString provider() const { return mProvider; }
67 
69  QString source() const { return mSource; }
70 
72  QString encoding() const { return mEncoding; }
73 
74  private:
75  QString mName;
76  QString mSource;
77  QString mProvider;
78  QString mRef;
79  QString mEncoding;
80  };
81 
83  QgsVirtualLayerDefinition( const QString &filePath = "" );
84 
99  static QgsVirtualLayerDefinition fromUrl( const QUrl &url );
100 
102  QUrl toUrl() const;
103 
105  QString toString() const;
106 
108  void addSource( const QString &name, const QString &ref );
109 
111  void addSource( const QString &name, const QString &source, const QString &provider, const QString &encoding = "" );
112 
114  typedef QList<QgsVirtualLayerDefinition::SourceLayer> SourceLayers;
115 
117  const QgsVirtualLayerDefinition::SourceLayers &sourceLayers() const { return mSourceLayers; }
118 
120  QString query() const { return mQuery; }
122  void setQuery( const QString &query ) { mQuery = query; }
123 
125  QString filePath() const { return mFilePath; }
127  void setFilePath( const QString &filePath ) { mFilePath = filePath; }
128 
130  QString uid() const { return mUid; }
132  void setUid( const QString &uid ) { mUid = uid; }
133 
135  QString geometryField() const { return mGeometryField; }
137  void setGeometryField( const QString &geometryField ) { mGeometryField = geometryField; }
138 
144  QgsWkbTypes::Type geometryWkbType() const { return mGeometryWkbType; }
146  void setGeometryWkbType( QgsWkbTypes::Type t ) { mGeometryWkbType = t; }
147 
149  long geometrySrid() const { return mGeometrySrid; }
151  void setGeometrySrid( long srid ) { mGeometrySrid = srid; }
152 
154  QgsFields fields() const { return mFields; }
156  void setFields( const QgsFields &fields ) { mFields = fields; }
157 
159  bool hasSourceLayer( const QString &name ) const;
160 
162  bool hasReferencedLayers() const;
163 
165  bool hasDefinedGeometry() const
166  {
167  return geometryWkbType() != QgsWkbTypes::NoGeometry && geometryWkbType() != QgsWkbTypes::Unknown;
168  }
169 
170  private:
171  SourceLayers mSourceLayers;
172  QString mQuery;
173  QString mUid;
174  QString mGeometryField;
175  QString mFilePath;
176  QgsFields mFields;
177  QgsWkbTypes::Type mGeometryWkbType = QgsWkbTypes::Unknown;
178  long mGeometrySrid = 0;
179 };
180 
181 // clazy:excludeall=qstring-allocations
182 
183 #endif
bool isReferenced() const
Is it a live layer or not ?
void setFields(const QgsFields &fields)
Set field definitions.
QList< QgsVirtualLayerDefinition::SourceLayer > SourceLayers
List of source layers.
SourceLayer(const QString &name, const QString &source, const QString &provider, const QString &encoding)
Constructor variant to build a layer with a provider and a source.
QString name() const
Name of the layer.
QString uid() const
Get the name of the field with unique identifiers.
QString encoding() const
Optional encoding for the provider.
Container of fields for a vector layer.
Definition: qgsfields.h:42
QString source() const
The source url used by the provider to build the layer.
void setFilePath(const QString &filePath)
Set the file path.
QString filePath() const
Get the file path. May be empty.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
QString reference() const
The reference (id) of the live layer.
void setQuery(const QString &query)
Set the SQL query.
void setGeometryField(const QString &geometryField)
Set the name of the geometry field.
QString geometryField() const
Get the name of the geometry field. Empty if no geometry field.
QgsFields fields() const
Get field definitions.
const QgsVirtualLayerDefinition::SourceLayers & sourceLayers() const
Get access to the source layers.
void setGeometryWkbType(QgsWkbTypes::Type t)
Set the type of the geometry.
SourceLayer(const QString &name, const QString &ref)
Constructor variant to build a live layer reference.
bool hasDefinedGeometry() const
Convenient method to test if the geometry is defined (not NoGeometry and not Unknown) ...
void setUid(const QString &uid)
Set the name of the field with unique identifiers.
void setGeometrySrid(long srid)
Set the SRID of the geometry.
QString query() const
Get the SQL query.
QgsWkbTypes::Type geometryWkbType() const
Get the type of the geometry QgsWkbTypes::NoGeometry to hide any geometry QgsWkbTypes::Unknown for un...
A SourceLayer is either a reference to a live layer in the registry or all the parameters needed to l...
Class to manipulate the definition of a virtual layer.
long geometrySrid() const
Get the SRID of the geometry.