22#include "qgsgeopackagedataitems.h"
24#include "qgsogrproviderutils.h"
36#include "moc_qgsfilebaseddataitemprovider.cpp"
38using namespace Qt::StringLiterals;
62 if ( mDetails.driverName() ==
"SQLite"_L1 )
65 path() + u
"/columns/ "_s,
66 QStringLiteral( R
"(dbname="%1")" ).arg( parent()->path().replace( '"', R
"(\")"_L1 ) ),
67 u"spatialite"_s, QString(),
name() ) );
69 else if ( mDetails.providerKey() ==
"ogr"_L1 )
74 path() + u
"/columns/ "_s,
76 u
"ogr"_s, QString(),
name() ) );
81 QString relationError;
82 QList< QgsWeakRelation > relations;
85 relations = conn->relationships( QString(), mDetails.name() );
89 relationError = ex.
what();
92 if ( !relations.empty() || !relationError.isEmpty() )
94 auto relationsItem = std::make_unique< QgsRelationshipsItem >(
this,
mPath +
"/relations", conn->uri(), u
"ogr"_s, QString(), mDetails.name() );
96 relationsItem->setSortKey( QString( QChar( 0x11FFFF ) ) );
97 children.append( relationsItem.release() );
118 if ( mDetails.providerKey() ==
"ogr"_L1 )
123 parts.insert( u
"path"_s,
path() );
133 switch ( sublayer.
type() )
184 return mDetails.name();
199 mSublayers.append( sublayer );
210 res.reserve( mSublayers.size() );
226 , mExtraUriParts( extraUriParts )
241 QList< QgsProviderSublayerDetails>
sublayers;
244 QSet< QString > providers;
247 providers.insert( details.providerKey() );
250 for (
const QString &provider : std::as_const( providers ) )
254 if ( !mExtraUriParts.empty() )
256 QVariantMap uriParts = metadata->decodeUri(
path() );
257 for (
auto it = mExtraUriParts.constBegin(); it != mExtraUriParts.constEnd(); ++it )
259 uriParts.insert( it.key(), it.value() );
261 const QString updatedUri = metadata->encodeUri( uriParts );
272 else if ( mSublayers.empty() )
288 QMap< QStringList, QgsFileDataCollectionGroupItem * > groupItems;
293 if ( !sublayer.
path().isEmpty() )
295 QStringList currentPath;
296 QStringList remainingPaths = sublayer.
path();
299 while ( !remainingPaths.empty() )
301 currentPath << remainingPaths.takeAt( 0 );
303 auto it = groupItems.constFind( currentPath );
304 if ( it == groupItems.constEnd() )
308 groupItems.insert( currentPath, newGroupItem );
313 groupItem = newGroupItem;
317 groupItem = it.value();
336 mCachedCapabilities = conn->capabilities();
337 mCachedCapabilities2 = conn->capabilities2();
338 mHasCachedCapabilities =
true;
343 QStringList fieldDomains;
346 fieldDomains = conn->fieldDomainNames();
350 domainError = ex.
what();
353 if ( !fieldDomains.empty() || !domainError.isEmpty() )
355 auto domainsItem = std::make_unique< QgsFieldDomainsItem >(
this,
mPath +
"/domains", conn->uri(), u
"ogr"_s );
357 domainsItem->setSortKey( QString( QChar( 0x10FFFF ) ) );
358 children.append( domainsItem.release() );
363 QString relationError;
364 QList< QgsWeakRelation > relations;
367 relations = conn->relationships();
371 relationError = ex.
what();
374 if ( !relations.empty() || !relationError.isEmpty() )
376 auto relationsItem = std::make_unique< QgsRelationshipsItem >(
this,
mPath +
"/relations", conn->uri(), u
"ogr"_s );
378 relationsItem->setSortKey( QString( QChar( 0x11FFFF ) ) );
379 children.append( relationsItem.release() );
395 if ( mHasCachedCapabilities )
398 if ( mHasCachedDropSupport )
399 return mCachedSupportsDrop;
404 mHasCachedDropSupport =
true;
405 if ( !QFileInfo(
path() ).isWritable() )
407 mCachedSupportsDrop =
false;
408 return mCachedSupportsDrop;
411 GDALDriverH hDriver = GDALIdentifyDriverEx(
path().toUtf8().constData(), GDAL_OF_VECTOR,
nullptr,
nullptr );
414 mCachedSupportsDrop =
false;
415 return mCachedSupportsDrop;
419 const QString driverName = GDALGetDriverShortName( hDriver );
420 if ( driverName ==
"PDF"_L1
421 || driverName ==
"DXF"_L1 )
423 mCachedSupportsDrop =
false;
424 return mCachedSupportsDrop;
428#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,4,0)
429 const bool isSingleTableDriver = !GDALGetMetadataItem( hDriver, GDAL_DCAP_MULTIPLE_VECTOR_LAYERS,
nullptr );
431 const QFileInfo pathInfo(
path() );
432 const QString suffix = pathInfo.suffix().toLower();
436 if ( isSingleTableDriver )
438 mCachedSupportsDrop =
false;
439 return mCachedSupportsDrop;
443 mCachedSupportsDrop =
true;
444 return mCachedSupportsDrop;
451 collectionUri.
layerType = u
"collection"_s;
453 return { collectionUri };
459 if ( OGRGetDriverCount() == 0 )
464 CPLPushErrorHandler( CPLQuietErrorHandler );
466 GDALDriverH hDriver = GDALIdentifyDriverEx(
path().toUtf8().constData(), GDAL_OF_VECTOR,
nullptr,
nullptr );
467 CPLPopErrorHandler();
471 QgsDebugMsgLevel( u
"GDALIdentifyDriverEx error # %1 : %2 on %3"_s.arg( CPLGetLastErrorNo() ).arg( CPLGetLastErrorMsg() ).arg(
path() ), 2 );
475 const QString driverName = GDALGetDriverShortName( hDriver );
476 if ( driverName ==
"PDF"_L1
477 || driverName ==
"DXF"_L1 )
485 if ( driverName ==
"SQLite"_L1 )
501 parts.insert( u
"path"_s,
path() );
510 mHasCachedCapabilities =
true;
518 if ( mHasCachedCapabilities )
519 return mCachedCapabilities;
524 mCachedCapabilities = conn->capabilities();
525 mCachedCapabilities2 = conn->capabilities2();
526 mHasCachedCapabilities =
true;
528 return mCachedCapabilities;
533 if ( mHasCachedCapabilities )
534 return mCachedCapabilities2;
539 mCachedCapabilities = conn->capabilities();
540 mCachedCapabilities2 = conn->capabilities2();
541 mHasCachedCapabilities =
true;
543 return mCachedCapabilities2;
572 return createDataItemForPathPrivate( path, parentItem, &allowedProviders, queryFlags, extraUriParts );
575QgsDataItem *QgsFileBasedDataItemProvider::createDataItemForPathPrivate(
const QString &path,
QgsDataItem *parentItem,
const QStringList *allowedProviders,
Qgis::SublayerQueryFlags queryFlags,
const QVariantMap &extraUriParts )
577 if ( path.isEmpty() )
580 const QFileInfo info( path );
581 QString suffix = info.suffix().toLower();
582 const QString
name = info.fileName();
585 if ( suffix.compare(
"gpkg"_L1, Qt::CaseInsensitive ) == 0 )
588 QgsGeoPackageCollectionItem *item =
new QgsGeoPackageCollectionItem( parentItem,
name, path );
592 else if ( suffix ==
"txt"_L1 )
600 else if ( suffix ==
"map"_L1 || suffix ==
"dat"_L1 )
602 if ( QFile::exists( QDir( info.path() ).filePath( info.baseName() +
".tab" ) ) || QFile::exists( QDir( info.path() ).filePath( info.baseName() +
".TAB" ) ) )
606 else if ( suffix ==
"dbf"_L1 || suffix ==
"shx"_L1 )
608 if ( QFile::exists( QDir( info.path() ).filePath( info.baseName() +
".shp" ) ) || QFile::exists( QDir( info.path() ).filePath( info.baseName() +
".SHP" ) ) )
617 else if ( path.endsWith(
".shp.zip"_L1, Qt::CaseInsensitive ) &&
618 GDALIdentifyDriverEx( path.toUtf8().constData(), GDAL_OF_VECTOR,
nullptr,
nullptr ) )
620 suffix = u
"shp.zip"_s;
627 QgsSettings settings;
630 if ( ( settings.
value( u
"qgis/scanItemsInBrowser2"_s,
631 "extension" ).toString() ==
"extension"_L1 ) ||
632 ( parentItem && settings.
value( u
"qgis/scanItemsFastScanUris"_s,
633 QStringList() ).toStringList().contains( parentItem->
path() ) ) )
638 QList<QgsProviderSublayerDetails> sublayers;
639 if ( !allowedProviders )
645 for (
const QString &provider : *allowedProviders )
649 if ( !extraUriParts.empty() )
651 QVariantMap uriParts = metadata->decodeUri( path );
652 for (
auto it = extraUriParts.constBegin(); it != extraUriParts.constEnd(); ++it )
654 uriParts.insert( it.key(), it.value() );
657 sublayers.append( metadata->querySublayers( metadata->encodeUri( uriParts ), queryFlags ) );
661 sublayers.append( metadata->querySublayers( path, queryFlags ) );
667 if ( sublayers.size() == 1
672 QgsProviderSublayerItem *item =
new QgsProviderSublayerItem( parentItem,
name, sublayers.at( 0 ), path );
676 else if ( !sublayers.empty() )
678 QgsFileDataCollectionItem *item =
new QgsFileDataCollectionItem( parentItem,
name, path, sublayers, extraUriParts );
690 QFileInfo info( path );
691 QString suffix = info.suffix().toLower();
693 QStringList dirExtensions = QgsOgrProviderUtils::directoryExtensions();
694 return dirExtensions.contains( suffix );
@ Files
Can provides items which corresponds to files.
@ Directories
Can provides items which corresponds to directories.
@ NotPopulated
Children not yet created.
@ Populated
Children created.
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn.
@ ItemRepresentsFile
Item's path() directly represents a file on disk.
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
@ Archive
File archive type (e.g. vsizip).
QFlags< DataItemProviderCapability > DataItemProviderCapabilities
Capabilities for data item providers.
@ FastScan
Indicates that the provider must scan for sublayers using the fastest possible approach – e....
@ ResolveGeometryType
Attempt to resolve the geometry type for vector sublayers.
BrowserLayerType
Browser item layer types.
@ Point
Vector point layer.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer.
@ Polygon
Vector polygon layer.
@ Vector
Generic vector layer.
@ VectorTile
Vector tile layer.
@ TableLayer
Vector non-spatial layer.
@ PointCloud
Point cloud layer.
QFlags< SublayerQueryFlag > SublayerQueryFlags
Sublayer query flags.
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
QFlags< DatabaseProviderConnectionCapability2 > DatabaseProviderConnectionCapabilities2
Provides common functionality for database based connections.
Qgis::DatabaseProviderConnectionCapabilities2 capabilities2() const
Returns extended connection capabilities.
@ CreateVectorTable
Can CREATE a vector (or aspatial) table/layer.
@ RetrieveRelationships
Can retrieve relationships from the database.
@ ListFieldDomains
Can return a list of field domain names via fieldDomainNames().
QFlags< Capability > Capabilities
Capabilities capabilities() const
Returns connection capabilities.
QgsDataCollectionItem(QgsDataItem *parent, const QString &name, const QString &path=QString(), const QString &providerKey=QString())
Constructor for QgsDataCollectionItem, with the specified parent item.
Base class for all items in the model.
QVector< QgsDataItem * > children() const
Qgis::BrowserItemCapabilities mCapabilities
QgsDataItem(Qgis::BrowserItemType type, QgsDataItem *parent, const QString &name, const QString &path, const QString &providerKey=QString())
Constructor for QgsDataItem, with the specified parent item.
QString name() const
Returns the name of the item (the displayed text for the item).
virtual void setState(Qgis::BrowserItemState state)
Set item state.
virtual void setCapabilities(Qgis::BrowserItemCapabilities capabilities)
Sets the capabilities for the data item.
virtual void addChildItem(QgsDataItem *child, bool refresh=false)
Inserts a new child item.
QgsDataItem * parent() const
Gets item parent.
QString providerKey() const
Returns the provider key that created this item (e.g.
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
Stores the component parts of a data source URI (e.g.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
void setDatabase(const QString &database)
Sets the URI database name.
A browser item which contains a collection of field items.
static QgsDataItem * createLayerItemForPath(const QString &path, QgsDataItem *parentItem, const QStringList &providers, const QVariantMap &extraUriParts, Qgis::SublayerQueryFlags queryFlags)
Static method to create a data item for sublayers corresponding to a file-like path.
Qgis::DataItemProviderCapabilities capabilities() const override
Returns combination of flags from QgsDataProvider::DataCapabilities.
QString name() override
Human-readable name of the provider name.
bool handlesDirectoryPath(const QString &path) override
Returns true if the provider will handle the directory at the specified path.
QgsDataItem * createDataItem(const QString &path, QgsDataItem *parentItem) override
Create a new instance of QgsDataItem (or nullptr) for given path and parent item.
A data collection item for grouping of the content in file based data collections (e....
QgsFileDataCollectionGroupItem(QgsDataItem *parent, const QString &groupName, const QString &path)
Constructor for QgsFileDataCollectionGroupItem.
void appendSublayer(const QgsProviderSublayerDetails &sublayer)
Adds a sublayer to the group.
QgsMimeDataUtils::UriList mimeUris() const override
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
bool hasDragEnabled() const override
Returns true if the item may be dragged.
bool hasDragEnabled() const override
Returns true if the item may be dragged.
QVector< QgsDataItem * > createChildren() override
Create children.
QgsAbstractDatabaseProviderConnection::Capabilities databaseConnectionCapabilities() const
Returns the associated connection capabilities, if a databaseConnection() is available.
QgsFileDataCollectionItem(QgsDataItem *parent, const QString &name, const QString &path, const QList< QgsProviderSublayerDetails > &sublayers, const QVariantMap &extraUriParts=QVariantMap())
Constructor for QgsFileDataCollectionItem.
QgsMimeDataUtils::UriList mimeUris() const override
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
QList< QgsProviderSublayerDetails > sublayers() const
Returns the sublayers.
bool canAddVectorLayers() const
Returns true if the file is likely to support addition of vector layers.
QgsAbstractDatabaseProviderConnection * databaseConnection() const override
For data items that represent a DB connection or one of its children, this method returns a connectio...
Qgis::DatabaseProviderConnectionCapabilities2 databaseConnectionCapabilities2() const
Returns extended connection capabilities, if a databaseConnection() is available.
static Qgis::VsiHandlerType vsiHandlerType(const QString &prefix)
Returns the VSI handler type for a given VSI prefix.
static QString vsiPrefixForPath(const QString &path)
Returns a the vsi prefix which corresponds to a file path, or an empty string if the path is not asso...
static QStringList multiLayerFileExtensions()
Returns a list of file extensions which potentially contain multiple layers representing GDAL raster ...
QString uri() const
Returns layer uri or empty string if layer cannot be created.
QgsLayerItem(QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, Qgis::BrowserLayerType layerType, const QString &providerKey)
Constructor for QgsLayerItem.
QList< QgsMimeDataUtils::Uri > UriList
Custom exception class for provider connection related exceptions.
QList< QgsProviderSublayerDetails > querySublayers(const QString &uri, Qgis::SublayerQueryFlags flags=Qgis::SublayerQueryFlags(), QgsFeedback *feedback=nullptr) const
Queries the specified uri and returns a list of any valid sublayers found in the dataset which can be...
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Contains details about a sub layer available from a dataset.
QStringList path() const
Returns the path to the sublayer.
Qgis::LayerType type() const
Returns the layer type.
Qgis::WkbType wkbType() const
Returns the layer's WKB type, or QgsWkbTypes::Unknown if the WKB type is not application or unknown.
QString uri() const
Returns the layer's URI.
QgsMimeDataUtils::Uri toMimeUri() const
Converts the sublayer details to a QgsMimeDataUtils::Uri representing the sublayer.
QString name() const
Returns the layer's name.
A generic data item for file based layers.
QString layerName() const override
Returns the layer name.
QgsAbstractDatabaseProviderConnection * databaseConnection() const override
For data items that represent a DB connection or one of its children, this method returns a connectio...
QVector< QgsDataItem * > createChildren() override
Create children.
QgsProviderSublayerItem(QgsDataItem *parent, const QString &name, const QgsProviderSublayerDetails &details, const QString &filePath)
Constructor for QgsProviderSublayerItem.
QgsProviderSublayerDetails sublayerDetails() const
Returns the sublayer details for the item.
static bool sublayerDetailsAreIncomplete(const QList< QgsProviderSublayerDetails > &details, QgsProviderUtils::SublayerCompletenessFlags flags=QgsProviderUtils::SublayerCompletenessFlags())
Returns true if the sublayer details are incomplete, and require a more in-depth scan.
@ IgnoreUnknownGeometryType
Indicates that an unknown geometry type should not be considered as incomplete.
@ IgnoreUnknownFeatureCount
Indicates that an unknown feature count should not be considered as incomplete.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static bool isXmlStyleFile(const QString &path)
Tests if the file at path is a QGIS style XML file.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
#define QgsDebugMsgLevel(str, level)
QString filePath
Path to file, if uri is associated with a file.
QString uri
Identifier of the data source recognized by its providerKey.
QString layerType
Type of URI.