Skip to content
Permalink
Browse files
[FEATURE][ui] Show dataset file size and last modified information in…
… the layer properties window
  • Loading branch information
nirvn authored and nyalldawson committed Aug 24, 2021
1 parent d260961 commit 0ab3442ead472988f76beeb68815b51909f9280b
@@ -53,6 +53,13 @@ Updates a ``layer``'s data source, replacing its data source with a path referri
Returns ``True`` if the layer was updated, or ``False`` if the layer was not updated (e.g. it
uses a data provider which does not specify paths in a layer URI.

.. versionadded:: 3.22
%End

static QString generalHtmlMetadata( const QgsMapLayer *layer );
%Docstring
Returns an HTML-formatted string containing generic metadata information for a given ``layer``.

.. versionadded:: 3.22
%End

@@ -25,6 +25,10 @@
#include "qgsreferencedgeometry.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsfileutils.h"

#include <QLocale>
#include <QUrl>

QgsRectangle QgsMapLayerUtils::combinedExtent( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext )
{
@@ -136,3 +140,67 @@ bool QgsMapLayerUtils::updateLayerSourcePath( QgsMapLayer *layer, const QString
layer->setDataSource( newUri, layer->name(), layer->providerType() );
return true;
}

QString QgsMapLayerUtils::generalHtmlMetadata( const QgsMapLayer *layer )
{
QString metadata;

// name
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Name" ) + QStringLiteral( "</td><td>" ) + layer->name() + QStringLiteral( "</td></tr>\n" );

// local path
QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( layer->dataProvider()->name(), layer->publicSource() );
QString path;
bool isLocalPath = false;
if ( uriComponents.contains( QStringLiteral( "path" ) ) )
{
path = uriComponents[QStringLiteral( "path" )].toString();
QFileInfo fi( path );
if ( fi.exists() )
{
isLocalPath = true;
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Path" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( path ).toString(), QDir::toNativeSeparators( path ) ) ) + QStringLiteral( "</td></tr>\n" );

qint64 fileSize = fi.size();
QDateTime lastModified = fi.lastModified();
QString lastModifiedFileName;
QSet<QString> sidecarFiles = QgsFileUtils::sidecarFilesForPath( path );
if ( fi.isFile() )
{
if ( !sidecarFiles.isEmpty() )
{
lastModifiedFileName = fi.fileName();
QStringList sidecarFileNames;
for ( const QString &sidecarFile : sidecarFiles )
{
QFileInfo sidecarFi( sidecarFile );
fileSize += sidecarFi.size();
if ( sidecarFi.lastModified() > lastModified )
{
lastModified = sidecarFi.lastModified();
lastModifiedFileName = sidecarFi.fileName();
}
sidecarFileNames << sidecarFi.fileName();
}
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( sidecarFiles.size() > 1 ? QObject::tr( "Sidecar files" ) : QObject::tr( "Sidecar file" ) ) + QStringLiteral( "</td><td>%1" ).arg( sidecarFileNames.join( QLatin1String( ", " ) ) ) + QStringLiteral( "</td></tr>\n" );
}
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( !sidecarFiles.isEmpty() ? QObject::tr( "Total size" ) : QObject::tr( "Size" ) ) + QStringLiteral( "</td><td>%1" ).arg( QgsFileUtils::representFileSize( fileSize ) ) + QStringLiteral( "</td></tr>\n" );
}
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Last modified" ) + QStringLiteral( "</td><td>%1" ).arg( QLocale().toString( fi.lastModified() ) ) + ( !lastModifiedFileName.isEmpty() ? QStringLiteral( " (%1)" ).arg( lastModifiedFileName ) : "" ) + QStringLiteral( "</td></tr>\n" );
}
}
if ( uriComponents.contains( QStringLiteral( "url" ) ) )
{
const QString url = uriComponents[QStringLiteral( "url" )].toString();
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "URL" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl( url ).toString(), url ) ) + QStringLiteral( "</td></tr>\n" );
}

// data source
if ( layer->publicSource() != path || !isLocalPath )
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Source" ) + QStringLiteral( "</td><td>%1" ).arg( layer->publicSource() != path ? layer->publicSource() : path ) + QStringLiteral( "</td></tr>\n" );

// provider
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Provider" ) + QStringLiteral( "</td><td>%1" ).arg( layer->dataProvider()->name() ) + QStringLiteral( "</td></tr>\n" );

return metadata;
}
@@ -70,6 +70,13 @@ class CORE_EXPORT QgsMapLayerUtils
*/
static bool updateLayerSourcePath( QgsMapLayer *layer, const QString &newPath );

/**
* Returns an HTML-formatted string containing generic metadata information for a given \a layer.
*
* \since QGIS 3.22
*/
static QString generalHtmlMetadata( const QgsMapLayer *layer );


};

@@ -39,6 +39,7 @@
#include "qgsfeaturerequest.h"
#include "qgsfields.h"
#include "qgsmaplayerfactory.h"
#include "qgsmaplayerutils.h"
#include "qgsgeometry.h"
#include "qgslayermetadataformatter.h"
#include "qgslogger.h"
@@ -5124,31 +5125,7 @@ QString QgsVectorLayer::htmlMetadata() const
myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" );
myMetadata += QLatin1String( "<table class=\"list-view\">\n" );

// name
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + name() + QStringLiteral( "</td></tr>\n" );

// local path
QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( mProviderKey, publicSource() );
QString path;
bool isLocalPath = false;
if ( uriComponents.contains( QStringLiteral( "path" ) ) )
{
path = uriComponents[QStringLiteral( "path" )].toString();
if ( QFile::exists( path ) )
{
isLocalPath = true;
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Path" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( path ).toString(), QDir::toNativeSeparators( path ) ) ) + QStringLiteral( "</td></tr>\n" );
}
}
if ( uriComponents.contains( QStringLiteral( "url" ) ) )
{
const QString url = uriComponents[QStringLiteral( "url" )].toString();
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "URL" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl( url ).toString(), url ) ) + QStringLiteral( "</td></tr>\n" );
}

// data source
if ( publicSource() != path || !isLocalPath )
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Source" ) + QStringLiteral( "</td><td>%1" ).arg( publicSource() != path ? publicSource() : path ) + QStringLiteral( "</td></tr>\n" );
myMetadata += QgsMapLayerUtils::generalHtmlMetadata( this );

// storage type
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Storage" ) + QStringLiteral( "</td><td>" ) + storageType() + QStringLiteral( "</td></tr>\n" );

0 comments on commit 0ab3442

Please sign in to comment.