Skip to content
Permalink
Browse files
Remove duplicate code
  • Loading branch information
nyalldawson committed Jul 12, 2021
1 parent 79b622f commit 59f7b4cc0760e44a6509317e23b32766c9bc354f
@@ -32,6 +32,17 @@ then a query with the Qgis.SublayerQueryFlag.ResolveGeometryType flag is require

If ``ignoreUnknownFeatureCount`` is ``True`` then sublayers with an unknown feature count
will not be considered as incomplete.
%End

static QString suggestLayerNameFromFilePath( const QString &path );
%Docstring
Suggests a suitable layer name given only a file ``path``.

Usually this corresponds to the base file name of ``path`` (e.g.
"rivers" for a path of "c:/my data/water/rivers.shp"). However
some data formats which use fixed file paths (such as aigrid files)
will instead return the parent directory name (e.g. "rivers" for
a path of "c:/my data/water/rivers/hdr.adf").
%End

};
@@ -7666,20 +7666,12 @@ bool QgisApp::openLayer( const QString &fileName, bool allowInteractive )
QgsCanvasRefreshBlocker refreshBlocker;
QgsSettings settings;

const QFileInfo info( fileName );
QString base = info.completeBaseName();

// special handling for .adf files -- use directory as base name, not the unhelpful .adf file name
if ( info.suffix().compare( QLatin1String( "adf" ), Qt::CaseInsensitive ) == 0 )
{
const QString dirName = info.path();
base = QFileInfo( dirName ).completeBaseName();
}

QString base = QgsProviderUtils::suggestLayerNameFromFilePath( fileName );
if ( settings.value( QStringLiteral( "qgis/formatLayerName" ), false ).toBool() )
{
base = QgsMapLayer::formatLayerName( base );
}

addSublayers( sublayers, base, groupName );
}
activateDeactivateLayerRelatedActions( activeLayer() );
@@ -205,14 +205,8 @@ QString QgsProviderSublayersDialog::groupName() const
return QString();

const QFileInfo fi( mFilePath );
QString res = fi.completeBaseName();

// special handling for .adf files -- use directory as group name, not the unhelpful .adf file name
if ( fi.suffix().compare( QLatin1String( "adf" ), Qt::CaseInsensitive ) == 0 )
{
const QString dirName = fi.path();
res = QFileInfo( dirName ).completeBaseName();
}
QString res = QgsProviderUtils::suggestLayerNameFromFilePath( mFilePath );

QgsSettings settings;
if ( settings.value( QStringLiteral( "qgis/formatLayerName" ), false ).toBool() )
@@ -45,6 +45,7 @@
#include "qgsruntimeprofiler.h"
#include "qgszipitem.h"
#include "qgsprovidersublayerdetails.h"
#include "qgsproviderutils.h"

#include <QImage>
#include <QColor>
@@ -3639,15 +3640,7 @@ QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const
const QVariantMap parts = decodeUri( uri );
if ( parts.contains( QStringLiteral( "path" ) ) )
{
const QFileInfo fi( parts.value( QStringLiteral( "path" ) ).toString() );
name = fi.baseName();

if ( fi.suffix().compare( QLatin1String( "adf" ), Qt::CaseInsensitive ) == 0 )
{
// for .adf files, we use the directory name as layer name
const QString dirName = fi.path();
name = QFileInfo( dirName ).completeBaseName();
}
name = QgsProviderUtils::suggestLayerNameFromFilePath( parts.value( QStringLiteral( "path" ) ).toString() );
}
details.setName( name.isEmpty() ? uri : name );
return {details};
@@ -17,6 +17,8 @@
#include "qgsproviderutils.h"
#include "qgsprovidersublayerdetails.h"

#include <QFileInfo>

bool QgsProviderUtils::sublayerDetailsAreIncomplete( const QList<QgsProviderSublayerDetails> &details, bool ignoreUnknownFeatureCount )
{
for ( const QgsProviderSublayerDetails &sublayer : details )
@@ -43,3 +45,18 @@ bool QgsProviderUtils::sublayerDetailsAreIncomplete( const QList<QgsProviderSubl

return false;
}

QString QgsProviderUtils::suggestLayerNameFromFilePath( const QString &path )
{
const QFileInfo info( path );
// default to base name of file
QString name = info.completeBaseName();

// special handling for .adf files -- use directory as base name, not the unhelpful .adf file name
if ( info.suffix().compare( QLatin1String( "adf" ), Qt::CaseInsensitive ) == 0 )
{
const QString dirName = info.path();
name = QFileInfo( dirName ).completeBaseName();
}
return name;
}
@@ -46,6 +46,17 @@ class CORE_EXPORT QgsProviderUtils
*/
static bool sublayerDetailsAreIncomplete( const QList< QgsProviderSublayerDetails > &details, bool ignoreUnknownFeatureCount );

/**
* Suggests a suitable layer name given only a file \a path.
*
* Usually this corresponds to the base file name of \a path (e.g.
* "rivers" for a path of "c:/my data/water/rivers.shp"). However
* some data formats which use fixed file paths (such as aigrid files)
* will instead return the parent directory name (e.g. "rivers" for
* a path of "c:/my data/water/rivers/hdr.adf").
*/
static QString suggestLayerNameFromFilePath( const QString &path );

};

#endif //QGSPROVIDERUTILS_H
@@ -24,6 +24,7 @@
#include "qgsmdaldataitems.h"
#include "qgsmeshdataprovidertemporalcapabilities.h"
#include "qgsprovidersublayerdetails.h"
#include "qgsproviderutils.h"

#include <QFileInfo>
#include <mutex>
@@ -1090,17 +1091,7 @@ QList<QgsProviderSublayerDetails> QgsMdalProviderMetadata::querySublayers( const
if ( details.name().isEmpty() )
{
// use file name as layer name if no layer name available from mdal

// special handling for .adf files -- use directory as group name, not the unhelpful .adf file name
if ( info.suffix().compare( QLatin1String( "adf" ), Qt::CaseInsensitive ) == 0 )
{
const QString dirName = info.path();
details.setName( QFileInfo( dirName ).completeBaseName() );
}
else
{
details.setName( info.completeBaseName() );
}
details.setName( QgsProviderUtils::suggestLayerNameFromFilePath( uri ) );
}

res << details;
@@ -81,6 +81,15 @@ def test_sublayerDetailsAreIncomplete(self):
self.assertEqual(sublayers[3].name(), 'lines')
self.assertEqual(sublayers[3].featureCount(), 6)

def test_suggestLayerNameFromFilePath(self):
"""
test suggestLayerNameFromFilePath
"""
self.assertEqual(QgsProviderUtils.suggestLayerNameFromFilePath(''), '')
self.assertEqual(QgsProviderUtils.suggestLayerNameFromFilePath('/home/me/data/rivers.shp'), 'rivers')
# adf files should return parent dir name
self.assertEqual(QgsProviderUtils.suggestLayerNameFromFilePath('/home/me/data/rivers/hdr.adf'), 'rivers')


if __name__ == '__main__':
unittest.main()

0 comments on commit 59f7b4c

Please sign in to comment.