1 /***************************************************************************
2     qgsmimedatautils.h
3     ---------------------
4     begin                : November 2011
5     copyright            : (C) 2011 by Martin Dobias
6     email                : wonder dot sk at gmail dot com
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 #ifndef QGSMIMEDATAUTILS_H
16 #define QGSMIMEDATAUTILS_H
17 
18 #include <QMimeData>
19 #include <QStringList>
20 
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include "qgswkbtypes.h"
24 
25 class QgsLayerItem;
26 class QgsLayerTreeNode;
27 class QgsVectorLayer;
28 class QgsRasterLayer;
29 class QgsMeshLayer;
30 class QgsMapLayer;
31 
32 /**
33  * \ingroup core
34  * \class QgsMimeDataUtils
35  */
36 class CORE_EXPORT QgsMimeDataUtils
37 {
38   public:
39 
40     struct CORE_EXPORT Uri
41     {
42       //! Constructs invalid URI
43       Uri() = default;
44       //! Constructs URI from encoded data
45       explicit Uri( const QString &encData );
46 
47       /**
48        * Constructs a URI corresponding to the specified \a layer.
49        *
50        * \since QGIS 3.8
51        */
52       explicit Uri( QgsMapLayer *layer );
53 
54       /**
55        * Returns whether the object contains valid data
56        * \since QGIS 3.0
57        */
isValidUri58       bool isValid() const { return !layerType.isEmpty(); }
59 
60       //! Returns encoded representation of the object
61       QString data() const;
62 
63       /**
64        * Gets vector layer from uri if possible, otherwise returns NULLPTR and error is set
65        * \param owner set to TRUE if caller becomes owner
66        * \param error set to error message if cannot get vector
67        */
68       QgsVectorLayer *vectorLayer( bool &owner, QString &error ) const;
69 
70       /**
71        * Gets raster layer from uri if possible, otherwise returns NULLPTR and error is set
72        * \param owner set to TRUE if caller becomes owner
73        * \param error set to error message if cannot get raster
74        */
75       QgsRasterLayer *rasterLayer( bool &owner, QString &error ) const;
76 
77       /**
78        * Gets mesh layer from uri if possible, otherwise returns NULLPTR and error is set
79        * \param owner set to TRUE if caller becomes owner
80        * \param error set to error message if cannot get raster
81        */
82       QgsMeshLayer *meshLayer( bool &owner, QString &error ) const;
83 
84       /**
85        * Returns the layer from the active project corresponding to this uri (if possible),
86        * otherwise returns NULLPTR.
87        *
88        * Unlike vectorLayer(), rasterLayer(), or meshLayer(), this method will not attempt
89        * to create a new layer corresponding to the URI.
90        *
91        * \since QGIS 3.8
92        */
93       QgsMapLayer *mapLayer() const;
94 
95       /**
96        * Type of URI.
97        *
98        * Recognized types include
99        *
100        * - "vector": vector layers
101        * - "raster": raster layers
102        * - "mesh": mesh layers
103        * - "plugin": plugin layers
104        * - "custom": custom types
105        * - "project": QGS/QGZ project file
106        * - "directory": directory path
107        *
108        * Mime data from plugins may use additional custom layer types.
109        */
110       QString layerType;
111 
112       /**
113        * For "vector" / "raster" type: provider id.
114        * For "plugin" type: plugin layer type name.
115        * For "custom" type: key of its QgsCustomDropHandler
116        * For "project" and "directory" types: unused
117        */
118       QString providerKey;
119 
120       //! Human readable name to be used e.g. in layer tree
121       QString name;
122       //! Identifier of the data source recognized by its providerKey
123       QString uri;
124       QStringList supportedCrs;
125       QStringList supportedFormats;
126 
127       /**
128        * Layer ID, if uri is associated with a layer from a QgsProject.
129        * \since QGIS 3.8
130        */
131       QString layerId;
132 
133       /**
134        * Unique ID associated with application instance. Can be used to identify
135        * if mime data was created inside the current application instance or not.
136        * \since QGIS 3.8
137        */
138       QString pId;
139 
140       /**
141        * WKB type, if associated with a vector layer, or QgsWkbTypes::Unknown if not
142        * yet known.
143        *
144        * \since QGIS 3.8
145        */
146       QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown;
147 
148 #ifdef SIP_RUN
149       SIP_PYOBJECT __repr__();
150       % MethodCode
151       QString str = QStringLiteral( "<QgsMimeDataUtils::Uri (%1): %2>" ).arg( sipCpp->providerKey, sipCpp->uri );
152       sipRes = PyUnicode_FromString( str.toUtf8().constData() );
153       % End
154 #endif
155     };
156     typedef QList<QgsMimeDataUtils::Uri> UriList;
157 
158     /**
159      * Encodes a URI list to a new QMimeData object.
160      */
161     static QMimeData *encodeUriList( const UriList &layers ) SIP_FACTORY;
162 
163     static bool isUriList( const QMimeData *data );
164 
165     static UriList decodeUriList( const QMimeData *data );
166 
167     /**
168      * Returns encoded URI list from a list of layer tree nodes.
169      * \since QGIS 3.0
170      */
171     static QByteArray layerTreeNodesToUriList( const QList<QgsLayerTreeNode *> &nodes );
172 
173     /**
174      * Returns TRUE if \a uri originated from the current QGIS application
175      * instance.
176      *
177      * \since QGIS 3.8
178      */
179     static bool hasOriginatedFromCurrentAppInstance( const QgsMimeDataUtils::Uri &uri );
180 
181   private:
182     static QString encode( const QStringList &items );
183     static QStringList decode( const QString &encoded );
184     static QByteArray uriListToByteArray( const UriList &layers );
185 
186 
187     friend class TestQgsMimeDataUtils;
188 
189 };
190 
191 Q_DECLARE_METATYPE( QgsMimeDataUtils::UriList )
192 
193 #endif // QGSMIMEDATAUTILS_H
194 
195