Skip to content
Permalink
Browse files

Add API for providers to indicate their priority in opening a specified

URI

And add new method to QgsProviderRegistry to select the preferred
provider for opening a URI based on this
  • Loading branch information
nyalldawson committed Nov 4, 2020
1 parent 4aa1c2f commit 0ebe7888775201bbdb00fee9b1a77ac4b9666ed8
@@ -182,6 +182,19 @@ Suitable for use in a QFileDialog.getOpenFileNames() call.
Builds the list of available mesh drivers metadata

.. versionadded:: 3.12
%End

virtual int priorityForUri( const QString &uri );
%Docstring
Returns an integer representing the priority which this provider should have when opening
a dataset with the specified ``uri``.

A larger priority means that the provider should be selected over others with a lower
priority for the same URI.

The default implementation returns 0 for all URIs.

.. versionadded:: 3.18
%End

virtual QgsDataProvider *createProvider( const QString &uri,
@@ -279,6 +279,24 @@ Returns list of available providers by their keys
QgsProviderMetadata *providerMetadata( const QString &providerKey ) const;
%Docstring
Returns metadata of the provider or ``None`` if not found
%End

QgsProviderMetadata *preferredProviderForUri( const QString &uri ) const;
%Docstring
Returns the metadata for the preferred provider for opening the specified ``uri``.

The preferred provider is determined by comparing the priority returned by
:py:func:`QgsProviderMetadata.priorityForUri()` for all registered providers, and selecting
the provider with the largest non-zero priority.

A ``None`` may be returned, which indicates that no providers are available which
returned a non-zero priority for the specified URI.

In the case that multiple providers returned the same priority for the URI then
either of these providers will be returned. In this case the :py:func:`QgsProviderMetadata.priorityForUri()`
implementation for the providers should be refined to avoid ties.

.. versionadded:: 3.18
%End

QString fileVectorFilters() const;
@@ -20,7 +20,6 @@

#include "ui_qgsmeshcalculatordialogbase.h"
#include "qgsmeshcalculator.h"
#include "qgsprovidermetadata.h"
#include "qgshelp.h"
#include "qgis_app.h"

@@ -20,7 +20,6 @@

#include "qgsdataitem.h"
#include "qgsdataitemprovider.h"
#include "qgsprovidermetadata.h"

///@cond PRIVATE
#define SIP_NO_FILE
@@ -81,6 +81,11 @@ QList<QgsMeshDriverMetadata> QgsProviderMetadata::meshDriversMetadata()
return QList<QgsMeshDriverMetadata>();
}

int QgsProviderMetadata::priorityForUri( const QString & )
{
return 0;
}

QgsDataProvider *QgsProviderMetadata::createProvider( const QString &uri,
const QgsDataProvider::ProviderOptions &options,
QgsDataProvider::ReadFlags flags )
@@ -239,6 +239,19 @@ class CORE_EXPORT QgsProviderMetadata : public QObject
*/
virtual QList<QgsMeshDriverMetadata> meshDriversMetadata();

/**
* Returns an integer representing the priority which this provider should have when opening
* a dataset with the specified \a uri.
*
* A larger priority means that the provider should be selected over others with a lower
* priority for the same URI.
*
* The default implementation returns 0 for all URIs.
*
* \since QGIS 3.18
*/
virtual int priorityForUri( const QString &uri );

/**
* Class factory to return a pointer to a newly created QgsDataProvider object
*
@@ -745,3 +745,19 @@ QgsProviderMetadata *QgsProviderRegistry::providerMetadata( const QString &provi
{
return findMetadata_( mProviders, providerKey );
}

QgsProviderMetadata *QgsProviderRegistry::preferredProviderForUri( const QString &uri ) const
{
QgsProviderMetadata *res = nullptr;
int maxPriority = 0;
for ( auto it = mProviders.begin(); it != mProviders.end(); ++it )
{
const int thisProviderPriority = it->second->priorityForUri( uri );
if ( thisProviderPriority > maxPriority )
{
maxPriority = thisProviderPriority;
res = it->second;
}
}
return res;
}
@@ -282,6 +282,24 @@ class CORE_EXPORT QgsProviderRegistry
//! Returns metadata of the provider or NULLPTR if not found
QgsProviderMetadata *providerMetadata( const QString &providerKey ) const;

/**
* Returns the metadata for the preferred provider for opening the specified \a uri.
*
* The preferred provider is determined by comparing the priority returned by
* QgsProviderMetadata::priorityForUri() for all registered providers, and selecting
* the provider with the largest non-zero priority.
*
* A NULLPTR may be returned, which indicates that no providers are available which
* returned a non-zero priority for the specified URI.
*
* In the case that multiple providers returned the same priority for the URI then
* either of these providers will be returned. In this case the QgsProviderMetadata::priorityForUri()
* implementation for the providers should be refined to avoid ties.
*
* \since QGIS 3.18
*/
QgsProviderMetadata *preferredProviderForUri( const QString &uri ) const;

/**
* Returns a file filter string for supported vector files.
*

0 comments on commit 0ebe788

Please sign in to comment.
You can’t perform that action at this time.