QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
142  void setLazy( bool lazy ) { mLazy = lazy; }
143 
150  bool isLazy() const { return mLazy; }
151 
153  QString geometryField() const { return mGeometryField; }
155  void setGeometryField( const QString &geometryField ) { mGeometryField = geometryField; }
156 
162  QgsWkbTypes::Type geometryWkbType() const { return mGeometryWkbType; }
164  void setGeometryWkbType( QgsWkbTypes::Type t ) { mGeometryWkbType = t; }
165 
167  long geometrySrid() const { return mGeometrySrid; }
169  void setGeometrySrid( long srid ) { mGeometrySrid = srid; }
170 
172  QgsFields fields() const { return mFields; }
174  void setFields( const QgsFields &fields ) { mFields = fields; }
175 
177  bool hasSourceLayer( const QString &name ) const;
178 
180  bool hasReferencedLayers() const;
181 
183  bool hasDefinedGeometry() const
184  {
185  return geometryWkbType() != QgsWkbTypes::NoGeometry && geometryWkbType() != QgsWkbTypes::Unknown;
186  }
187 
188  private:
189  SourceLayers mSourceLayers;
190  QString mQuery;
191  QString mUid;
192  QString mGeometryField;
193  QString mFilePath;
194  QgsFields mFields;
195  bool mLazy = false;
196  QgsWkbTypes::Type mGeometryWkbType = QgsWkbTypes::Unknown;
197  long mGeometrySrid = 0;
198 };
199 
200 // clazy:excludeall=qstring-allocations
201 
202 #endif
QString geometryField() const
Gets the name of the geometry field. Empty if no geometry field.
void setFields(const QgsFields &fields)
Sets field definitions.
QList< QgsVirtualLayerDefinition::SourceLayer > SourceLayers
List of source layers.
QString query() const
Gets the SQL query.
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.
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)
Sets the file path.
bool isLazy() const
Returns the lazy mode.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QString uid() const
Gets the name of the field with unique identifiers.
void setQuery(const QString &query)
Sets the SQL query.
QgsFields fields() const
Gets field definitions.
void setGeometryField(const QString &geometryField)
Sets the name of the geometry field.
bool hasDefinedGeometry() const
Convenient method to test if the geometry is defined (not NoGeometry and not Unknown) ...
QString name() const
Name of the layer.
void setGeometryWkbType(QgsWkbTypes::Type t)
Sets the type of the geometry.
SourceLayer(const QString &name, const QString &ref)
Constructor variant to build a live layer reference.
void setUid(const QString &uid)
Sets the name of the field with unique identifiers.
void setGeometrySrid(long srid)
Sets the SRID of the geometry.
QgsWkbTypes::Type geometryWkbType() const
Gets the type of the geometry QgsWkbTypes::NoGeometry to hide any geometry QgsWkbTypes::Unknown for u...
bool isReferenced() const
Is it a live layer or not ?
long geometrySrid() const
Gets the SRID of the geometry.
void setLazy(bool lazy)
Sets the lazy mode.
QString reference() const
The reference (id) of the live layer.
const QgsVirtualLayerDefinition::SourceLayers & sourceLayers() const
Gets access to the source layers.
QString filePath() const
Gets the file path. May be empty.
QString encoding() const
Optional encoding for the provider.
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.