QGIS API Documentation  3.6.0-Noosa (5873452)
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 "qgswkbtypes.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
bool isReferenced() const
Is it a live layer or not ?
void setFields(const QgsFields &fields)
Sets 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
Gets 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)
Sets the file path.
QString filePath() const
Gets the file path. May be empty.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QString reference() const
The reference (id) of the live layer.
void setQuery(const QString &query)
Sets the SQL query.
void setGeometryField(const QString &geometryField)
Sets the name of the geometry field.
bool isLazy() const
Returns the lazy mode.
QString geometryField() const
Gets the name of the geometry field. Empty if no geometry field.
QgsFields fields() const
Gets field definitions.
const QgsVirtualLayerDefinition::SourceLayers & sourceLayers() const
Gets access to the source layers.
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.
bool hasDefinedGeometry() const
Convenient method to test if the geometry is defined (not NoGeometry and not Unknown) ...
void setUid(const QString &uid)
Sets the name of the field with unique identifiers.
void setGeometrySrid(long srid)
Sets the SRID of the geometry.
QString query() const
Gets the SQL query.
void setLazy(bool lazy)
Sets the lazy mode.
QgsWkbTypes::Type geometryWkbType() const
Gets the type of the geometry QgsWkbTypes::NoGeometry to hide any geometry QgsWkbTypes::Unknown for u...
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
Gets the SRID of the geometry.