Skip to content
Permalink
Browse files
Move generalHtmlMetadata to QgsMapLayer class, guard from null data p…
…rovider, harmonize looks and details ordering across map layer types
  • Loading branch information
nirvn authored and nyalldawson committed Aug 24, 2021
1 parent 90b104f commit 2a2eace0e1440af9b52a6ba738e00160559c022a
@@ -1960,6 +1960,7 @@ this method is now deprecated and always return ``False``, because circular depe




};

QFlags<QgsMapLayer::LayerFlag> operator|(QgsMapLayer::LayerFlag f1, QFlags<QgsMapLayer::LayerFlag> f2);
@@ -53,13 +53,6 @@ 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

@@ -1499,12 +1499,12 @@ QString QgsMeshLayer::htmlMetadata() const
const QgsLayerMetadataFormatter htmlFormatter( metadata() );
QString myMetadata = QStringLiteral( "<html>\n<body>\n" );

myMetadata += generalHtmlMetadata();

// Begin Provider section
myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" );
myMetadata += QLatin1String( "<table class=\"list-view\">\n" );

myMetadata += QgsMapLayerUtils::generalHtmlMetadata( this );

// Extent
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Extent" ) + QStringLiteral( "</td><td>" ) + extent().toString() + QStringLiteral( "</td></tr>\n" );

@@ -452,12 +452,12 @@ QString QgsPointCloudLayer::htmlMetadata() const
const QgsLayerMetadataFormatter htmlFormatter( metadata() );
QString myMetadata = QStringLiteral( "<html>\n<body>\n" );

myMetadata += generalHtmlMetadata();

// Begin Provider section
myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" );
myMetadata += QLatin1String( "<table class=\"list-view\">\n" );

myMetadata += QgsMapLayerUtils::generalHtmlMetadata( this );

// Extent
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Extent" ) + QStringLiteral( "</td><td>" ) + extent().toString() + QStringLiteral( "</td></tr>\n" );

@@ -22,6 +22,7 @@
#include "qgsapplication.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsdatasourceuri.h"
#include "qgsfileutils.h"
#include "qgslogger.h"
#include "qgsauthmanager.h"
#include "qgsmaplayer.h"
@@ -54,6 +55,7 @@
#include <QDomNode>
#include <QFile>
#include <QFileInfo>
#include <QLocale>
#include <QTextStream>
#include <QUrl>
#include <QTimer>
@@ -2189,6 +2191,75 @@ void QgsMapLayer::invalidateWgs84Extent()
mWgs84Extent = QgsRectangle();
}

QString QgsMapLayer::generalHtmlMetadata() const
{
QString metadata = QStringLiteral( "<h1>" ) + tr( "General" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );

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

QString path;
bool isLocalPath = false;
if ( dataProvider() )
{
// local path
QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( dataProvider()->name(), publicSource() );
if ( uriComponents.contains( QStringLiteral( "path" ) ) )
{
path = uriComponents[QStringLiteral( "path" )].toString();
QFileInfo fi( path );
if ( fi.exists() )
{
isLocalPath = true;
metadata += 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" );

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 ? tr( "Sidecar files" ) : tr( "Sidecar file" ) ) + QStringLiteral( "</td><td>%1" ).arg( sidecarFileNames.join( QLatin1String( ", " ) ) ) + QStringLiteral( "</td></tr>\n" );
}
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( !sidecarFiles.isEmpty() ? tr( "Total size" ) : tr( "Size" ) ) + QStringLiteral( "</td><td>%1" ).arg( QgsFileUtils::representFileSize( fileSize ) ) + QStringLiteral( "</td></tr>\n" );
}
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + 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\">" ) + 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 )
metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Source" ) + QStringLiteral( "</td><td>%1" ).arg( publicSource() != path ? publicSource() : path ) + QStringLiteral( "</td></tr>\n" );

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

metadata += QStringLiteral( "</table>\n<br><br>" );
return metadata;
}

QString QgsMapLayer::crsHtmlMetadata() const
{
QString metadata = QStringLiteral( "<h1>" ) + tr( "Coordinate Reference System (CRS)" ) + QStringLiteral( "</h1>\n<hr>\n" );
@@ -1985,6 +1985,19 @@ class CORE_EXPORT QgsMapLayer : public QObject
QString crsHtmlMetadata() const;
#endif

#ifndef SIP_RUN

/**
* Returns an HTML fragment containing general metadata information, for use
* in the htmlMetadata() method.
*
* \note Not available in Python bindings.
*
* \since QGIS 3.22
*/
QString generalHtmlMetadata() const;
#endif

private:

virtual QString baseURI( PropertyType type ) const;
@@ -25,10 +25,6 @@
#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 )
{
@@ -140,67 +136,3 @@ 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,13 +70,6 @@ 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 );


};

@@ -326,20 +326,12 @@ QString QgsRasterLayer::htmlMetadata() const
const QgsLayerMetadataFormatter htmlFormatter( metadata() );
QString myMetadata = QStringLiteral( "<html><head></head>\n<body>\n" );

// Begin Provider section
myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n<table class=\"list-view\">\n" );

myMetadata += QgsMapLayerUtils::generalHtmlMetadata( this );

myMetadata += QLatin1String( "</table>\n<br><br>" );

// CRS
myMetadata += crsHtmlMetadata();
myMetadata += generalHtmlMetadata();

myMetadata += QStringLiteral( "<h1>" ) + tr( "Properties" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );
// Begin Provider section
myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );

myMetadata += QStringLiteral( "\n" ) %

// Extent
QStringLiteral( "<tr><td class=\"highlight\">" ) % tr( "Extent" ) % QStringLiteral( "</td><td>" ) % extent().toString() % QStringLiteral( "</td></tr>\n" ) %

@@ -406,10 +398,13 @@ QString QgsRasterLayer::htmlMetadata() const
mDataProvider->htmlMetadata() %

// End Provider section
QStringLiteral( "</table>\n<br><br>" ) %
QStringLiteral( "</table>\n<br><br>" );

// CRS
myMetadata += crsHtmlMetadata();

// Identification section
QStringLiteral( "<h1>" ) % tr( "Identification" ) % QStringLiteral( "</h1>\n<hr>\n" ) %
// Identification section
myMetadata += QStringLiteral( "<h1>" ) % tr( "Identification" ) % QStringLiteral( "</h1>\n<hr>\n" ) %
htmlFormatter.identificationSectionHtml() %
QStringLiteral( "<br><br>\n" ) %

@@ -5119,14 +5119,14 @@ void QgsVectorLayer::setDiagramLayerSettings( const QgsDiagramLayerSettings &s )
QString QgsVectorLayer::htmlMetadata() const
{
QgsLayerMetadataFormatter htmlFormatter( metadata() );
QString myMetadata = QStringLiteral( "<html>\n<body>\n" );
QString myMetadata = QStringLiteral( "<html><head></head>\n<body>\n" );

myMetadata += generalHtmlMetadata();

// Begin Provider section
myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" );
myMetadata += QLatin1String( "<table class=\"list-view\">\n" );

myMetadata += QgsMapLayerUtils::generalHtmlMetadata( this );

// storage type
if ( !storageType().isEmpty() )
{

0 comments on commit 2a2eace

Please sign in to comment.