1 /***************************************************************************
2   qgsmaplayerfactory.cpp
3   --------------------------------------
4   Date                 : March 2021
5   Copyright            : (C) 2021 by Nyall Dawson
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 
18 #include "qgsmaplayerfactory.h"
19 #include "qgsvectorlayer.h"
20 #include "qgsrasterlayer.h"
21 #include "qgsmeshlayer.h"
22 #include "qgspointcloudlayer.h"
23 #include "qgsvectortilelayer.h"
24 #include "qgsannotationlayer.h"
25 
typeFromString(const QString & string,bool & ok)26 QgsMapLayerType QgsMapLayerFactory::typeFromString( const QString &string, bool &ok )
27 {
28   ok = true;
29   if ( string.compare( QLatin1String( "vector" ), Qt::CaseInsensitive ) == 0 )
30     return QgsMapLayerType::VectorLayer;
31   else if ( string.compare( QLatin1String( "raster" ), Qt::CaseInsensitive ) == 0 )
32     return QgsMapLayerType::RasterLayer;
33   else if ( string.compare( QLatin1String( "mesh" ), Qt::CaseInsensitive ) == 0 )
34     return QgsMapLayerType::MeshLayer;
35   else if ( string.compare( QLatin1String( "vector-tile" ), Qt::CaseInsensitive ) == 0 )
36     return QgsMapLayerType::VectorTileLayer;
37   else if ( string.compare( QLatin1String( "point-cloud" ), Qt::CaseInsensitive ) == 0 )
38     return QgsMapLayerType::PointCloudLayer;
39   else if ( string.compare( QLatin1String( "plugin" ), Qt::CaseInsensitive ) == 0 )
40     return QgsMapLayerType::PluginLayer;
41   else if ( string.compare( QLatin1String( "annotation" ), Qt::CaseInsensitive ) == 0 )
42     return QgsMapLayerType::AnnotationLayer;
43 
44   ok = false;
45   return QgsMapLayerType::VectorLayer;
46 }
47 
typeToString(QgsMapLayerType type)48 QString QgsMapLayerFactory::typeToString( QgsMapLayerType type )
49 {
50   switch ( type )
51   {
52     case QgsMapLayerType::VectorLayer:
53       return QStringLiteral( "vector" );
54     case QgsMapLayerType::RasterLayer:
55       return QStringLiteral( "raster" );
56     case QgsMapLayerType::PluginLayer:
57       return QStringLiteral( "plugin" );
58     case QgsMapLayerType::MeshLayer:
59       return QStringLiteral( "mesh" );
60     case QgsMapLayerType::VectorTileLayer:
61       return QStringLiteral( "vector-tile" );
62     case QgsMapLayerType::AnnotationLayer:
63       return QStringLiteral( "annotation" );
64     case QgsMapLayerType::PointCloudLayer:
65       return QStringLiteral( "point-cloud" );
66   }
67   return QString();
68 }
69 
createLayer(const QString & uri,const QString & name,QgsMapLayerType type,const LayerOptions & options,const QString & provider)70 QgsMapLayer *QgsMapLayerFactory::createLayer( const QString &uri, const QString &name, QgsMapLayerType type, const LayerOptions &options, const QString &provider )
71 {
72   switch ( type )
73   {
74     case QgsMapLayerType::VectorLayer:
75     {
76       QgsVectorLayer::LayerOptions vectorOptions;
77       vectorOptions.transformContext = options.transformContext;
78       vectorOptions.loadDefaultStyle = options.loadDefaultStyle;
79       return new QgsVectorLayer( uri, name, provider, vectorOptions );
80     }
81 
82     case QgsMapLayerType::RasterLayer:
83     {
84       QgsRasterLayer::LayerOptions rasterOptions;
85       rasterOptions.transformContext = options.transformContext;
86       rasterOptions.loadDefaultStyle = options.loadDefaultStyle;
87       return new QgsRasterLayer( uri, name, provider, rasterOptions );
88     }
89 
90     case QgsMapLayerType::MeshLayer:
91     {
92       QgsMeshLayer::LayerOptions meshOptions;
93       meshOptions.transformContext = options.transformContext;
94       meshOptions.loadDefaultStyle = options.loadDefaultStyle;
95       return new QgsMeshLayer( uri, name, provider, meshOptions );
96     }
97 
98     case QgsMapLayerType::VectorTileLayer:
99     {
100       const QgsVectorTileLayer::LayerOptions vectorTileOptions( options.transformContext );
101       return new QgsVectorTileLayer( uri, name, vectorTileOptions );
102     }
103 
104     case QgsMapLayerType::AnnotationLayer:
105     {
106       const QgsAnnotationLayer::LayerOptions annotationOptions( options.transformContext );
107       return new QgsAnnotationLayer( name, annotationOptions );
108     }
109 
110     case QgsMapLayerType::PointCloudLayer:
111     {
112       QgsPointCloudLayer::LayerOptions pointCloudOptions;
113       pointCloudOptions.loadDefaultStyle = options.loadDefaultStyle;
114       pointCloudOptions.transformContext = options.transformContext;
115       return new QgsPointCloudLayer( uri, name, provider, pointCloudOptions );
116     }
117 
118     case QgsMapLayerType::PluginLayer:
119       break;
120   }
121   return nullptr;
122 }
123