Skip to content

Commit d084cc6

Browse files
authored
Merge pull request #5178 from Gustry/metadata_viewer
use the metadata API for the metadata viewer
2 parents 742f3e5 + c3309b9 commit d084cc6

7 files changed

+533
-130
lines changed

python/core/core_auto.sip

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
%Include layout/qgslayoututils.sip
169169
%Include metadata/qgslayermetadata.sip
170170
%Include metadata/qgslayermetadatavalidator.sip
171+
%Include metadata/qgslayermetadataformatter.sip
171172
%Include processing/qgsprocessing.sip
172173
%Include processing/qgsprocessingalgorithm.sip
173174
%Include processing/qgsprocessingcontext.sip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/metadata/qgslayermetadataformatter.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsLayerMetadataFormatter
13+
{
14+
%Docstring
15+
Class for metadata formatter.
16+
.. versionadded:: 3.0
17+
%End
18+
19+
%TypeHeaderCode
20+
#include "qgslayermetadataformatter.h"
21+
%End
22+
public:
23+
24+
QgsLayerMetadataFormatter( const QgsLayerMetadata &metadata );
25+
%Docstring
26+
Constructor for QgsLayerMetadataFormatter
27+
%End
28+
29+
QString accessSectionHtml() const;
30+
%Docstring
31+
Formats the "Access" section according to a ``metadata`` object.
32+
This will return a HTML table.
33+
:rtype: str
34+
%End
35+
36+
QString contactsSectionHtml() const;
37+
%Docstring
38+
Formats the "Contacts" section according to a ``metadata`` object.
39+
This will return a HTML table.
40+
:rtype: str
41+
%End
42+
43+
QString extentSectionHtml() const;
44+
%Docstring
45+
Formats the "Extents" section according to a ``metadata`` object.
46+
This will return a HTML table.
47+
:rtype: str
48+
%End
49+
50+
QString identificationSectionHtml() const;
51+
%Docstring
52+
Formats the "Identification" section according to a ``metadata`` object.
53+
This will return a HTML table.
54+
:rtype: str
55+
%End
56+
57+
QString historySectionHtml() const;
58+
%Docstring
59+
Formats the "History" section according to a ``metadata`` object.
60+
This will return a HTML table.
61+
:rtype: str
62+
%End
63+
64+
QString linksSectionHtml() const;
65+
%Docstring
66+
Formats the "Links" section according to a ``metadata`` object.
67+
This will return a HTML table.
68+
:rtype: str
69+
%End
70+
71+
};
72+
73+
/************************************************************************
74+
* This file has been generated automatically from *
75+
* *
76+
* src/core/metadata/qgslayermetadataformatter.h *
77+
* *
78+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
79+
************************************************************************/

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ SET(QGIS_CORE_SRCS
7373

7474
metadata/qgslayermetadata.cpp
7575
metadata/qgslayermetadatavalidator.cpp
76+
metadata/qgslayermetadataformatter.cpp
7677

7778
auth/qgsauthcertutils.cpp
7879
auth/qgsauthconfig.cpp
@@ -967,6 +968,7 @@ SET(QGIS_CORE_HDRS
967968

968969
metadata/qgslayermetadata.h
969970
metadata/qgslayermetadatavalidator.h
971+
metadata/qgslayermetadataformatter.h
970972

971973
processing/qgsprocessing.h
972974
processing/qgsprocessingalgorithm.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
#include <QStringBuilder>
2+
3+
#include "qgslayermetadataformatter.h"
4+
#include "qgslayermetadata.h"
5+
6+
7+
QgsLayerMetadataFormatter::QgsLayerMetadataFormatter( const QgsLayerMetadata &metadata )
8+
: mMetadata( metadata )
9+
{
10+
}
11+
12+
QString QgsLayerMetadataFormatter::accessSectionHtml() const
13+
{
14+
QString myMetadata = QStringLiteral( "<table class=\"list-view\">\n" );
15+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Fees" ) + QLatin1String( "</td><td>" ) + mMetadata.fees() + QLatin1String( "</td></tr>\n" );
16+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Licenses" ) + QLatin1String( "</td><td>" ) + mMetadata.licenses().join( QStringLiteral( "<br />" ) ) + QLatin1String( "</td></tr>\n" );
17+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Rights" ) + QLatin1String( "</td><td>" ) + mMetadata.rights().join( QStringLiteral( "<br />" ) ) + QLatin1String( "</td></tr>\n" );
18+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Constraints" ) + QLatin1String( "</td><td>" );
19+
const QList<QgsLayerMetadata::Constraint> &constraints = mMetadata.constraints();
20+
bool notFirstRow = false;
21+
for ( const QgsLayerMetadata::Constraint &constraint : constraints )
22+
{
23+
if ( notFirstRow )
24+
{
25+
myMetadata += QLatin1String( "<br />" );
26+
}
27+
myMetadata += QLatin1String( "<strong>" ) + constraint.type + QLatin1String( ": </strong>" ) + constraint.constraint;
28+
notFirstRow = true;
29+
}
30+
myMetadata += QLatin1String( "</td></tr>\n" );
31+
mMetadata.rights().join( QStringLiteral( "<br />" ) ) + QLatin1String( "</td></tr>\n" );
32+
myMetadata += QLatin1String( "</table>\n" );
33+
return myMetadata;
34+
}
35+
36+
QString QgsLayerMetadataFormatter::contactsSectionHtml() const
37+
{
38+
const QList<QgsLayerMetadata::Contact> &contacts = mMetadata.contacts();
39+
QString myMetadata;
40+
if ( contacts.isEmpty() )
41+
{
42+
myMetadata += QLatin1String( "<p>" ) + QObject::tr( "No contact yet." ) + QLatin1String( "</p>" );
43+
}
44+
else
45+
{
46+
myMetadata = QStringLiteral( "<table width=\"100%\" class=\"tabular-view\">\n" );
47+
myMetadata += "<tr><th>" + QObject::tr( "ID" ) + "</th><th>" + QObject::tr( "Name" ) + "</th><th>" + QObject::tr( "Position" ) + "</th><th>" + QObject::tr( "Organization" ) + "</th><th>" + QObject::tr( "Role" ) + "</th><th>" + QObject::tr( "Email" ) + "</th><th>" + QObject::tr( "Voice" ) + "</th><th>" + QObject::tr( "Fax" ) + "</th><th>" + QObject::tr( "Addresses" ) + "</th></tr>\n";
48+
int i = 1;
49+
for ( const QgsLayerMetadata::Contact &contact : contacts )
50+
{
51+
QString rowClass;
52+
if ( i % 2 )
53+
rowClass = QString( "class=\"odd-row\"" );
54+
myMetadata += "<tr " + rowClass + "><td>" + QString::number( i ) + "</td><td>" + contact.name + "</td><td>" + contact.position + "</td><td>" + contact.organization + "</td><td>" + contact.role + "</td><td>" + contact.email + "</td><td>" + contact.voice + "</td><td>" + contact.fax + "</td><td>";
55+
bool notFirstRow = false;
56+
for ( const QgsLayerMetadata::Address &oneAddress : contact.addresses )
57+
{
58+
if ( notFirstRow )
59+
{
60+
myMetadata += QLatin1String( "<br />\n" );
61+
}
62+
if ( ! oneAddress.type.isEmpty() )
63+
{
64+
myMetadata += oneAddress.type + QLatin1String( "<br />" );
65+
}
66+
if ( ! oneAddress.address.isEmpty() )
67+
{
68+
myMetadata += oneAddress.address + QLatin1String( "<br />" );
69+
}
70+
if ( ! oneAddress.postalCode.isEmpty() )
71+
{
72+
myMetadata += oneAddress.postalCode + QLatin1String( "<br />" );
73+
}
74+
if ( ! oneAddress.city.isEmpty() )
75+
{
76+
myMetadata += oneAddress.city + QLatin1String( "<br />" );
77+
}
78+
if ( ! oneAddress.administrativeArea.isEmpty() )
79+
{
80+
myMetadata += oneAddress.administrativeArea + QLatin1String( "<br />" );
81+
}
82+
if ( ! oneAddress.country.isEmpty() )
83+
{
84+
myMetadata += oneAddress.country;
85+
}
86+
notFirstRow = true;
87+
}
88+
myMetadata += "</td></tr>\n";
89+
i++;
90+
}
91+
myMetadata += QLatin1String( "</table>\n" );
92+
}
93+
return myMetadata;
94+
}
95+
96+
QString QgsLayerMetadataFormatter::extentSectionHtml() const
97+
{
98+
QString myMetadata = QStringLiteral( "<table class=\"list-view\">\n" );
99+
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "CRS" ) + QLatin1String( "</td><td>" ) + mMetadata.crs().authid() + QLatin1String( " - " );
100+
myMetadata += mMetadata.crs().description() + QLatin1String( " - " );
101+
if ( mMetadata.crs().isGeographic() )
102+
myMetadata += QObject::tr( "Geographic" );
103+
else
104+
myMetadata += QObject::tr( "Projected" );
105+
myMetadata += QLatin1String( "</td></tr>\n" );
106+
107+
const QgsLayerMetadata::Extent extent = mMetadata.extent();
108+
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Spatial Extent" ) + QLatin1String( "</td><td>" );
109+
const QList< QgsLayerMetadata::SpatialExtent > spatialExtents = extent.spatialExtents();
110+
bool notFirstRow = false;
111+
for ( const QgsLayerMetadata::SpatialExtent &spatialExtent : spatialExtents )
112+
{
113+
if ( notFirstRow )
114+
{
115+
myMetadata += QLatin1String( "<br />\n" );
116+
}
117+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "CRS" ) + QStringLiteral( ": </strong>" ) + spatialExtent.extentCrs.authid() + QLatin1String( " - " );
118+
myMetadata += spatialExtent.extentCrs.description() + QLatin1String( " - " );
119+
if ( spatialExtent.extentCrs.isGeographic() )
120+
myMetadata += QObject::tr( "Geographic" );
121+
else
122+
myMetadata += QObject::tr( "Projected" );
123+
myMetadata += QStringLiteral( "<br />" );
124+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "X Minimum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.xMinimum() ) + QStringLiteral( "<br />" );
125+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Y Minimum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.yMinimum() ) + QStringLiteral( "<br />" );
126+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "X Maximum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.xMaximum() ) + QStringLiteral( "<br />" );
127+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Y Maximum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.yMaximum() ) + QStringLiteral( "<br />" );
128+
if ( spatialExtent.bounds.zMinimum() || spatialExtent.bounds.zMinimum() )
129+
{
130+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Z Minimum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.zMinimum() ) + QStringLiteral( "<br />" );
131+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Z Maximum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.zMaximum() );
132+
}
133+
notFirstRow = true;
134+
}
135+
myMetadata += QLatin1String( "</td></tr>\n" );
136+
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Temporal Extent" ) + QLatin1String( "</td><td>" );
137+
const QList< QgsDateTimeRange > temporalExtents = extent.temporalExtents();
138+
notFirstRow = false;
139+
for ( const QgsDateTimeRange &temporalExtent : temporalExtents )
140+
{
141+
if ( notFirstRow )
142+
{
143+
myMetadata += QLatin1String( "<br />\n" );
144+
}
145+
if ( temporalExtent.isInstant() )
146+
{
147+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Instant" ) + QStringLiteral( ": </strong>" ) + temporalExtent.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate );
148+
}
149+
else
150+
{
151+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Start" ) + QStringLiteral( ": </strong>" ) + temporalExtent.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) + QStringLiteral( "<br />\n" );
152+
myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "End" ) + QStringLiteral( ": </strong>" ) + temporalExtent.end().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate );
153+
}
154+
notFirstRow = true;
155+
}
156+
myMetadata += QLatin1String( "</td></tr>\n" );
157+
myMetadata += QLatin1String( "</table>\n" );
158+
return myMetadata;
159+
}
160+
161+
QString QgsLayerMetadataFormatter::identificationSectionHtml() const
162+
{
163+
QString myMetadata = QStringLiteral( "<table class=\"list-view\">\n" );
164+
165+
// Identifier
166+
myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Identifier" ) + QLatin1String( "</td><td>" ) + mMetadata.identifier() + QLatin1String( "</td></tr>\n" );
167+
168+
// Parent Identifier
169+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Parent Identifier" ) + QLatin1String( "</td><td>" ) + mMetadata.parentIdentifier() + QLatin1String( "</td></tr>\n" );
170+
171+
// Title
172+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Title" ) + QLatin1String( "</td><td>" ) + mMetadata.title() + QLatin1String( "</td></tr>\n" );
173+
174+
// Type
175+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Type" ) + QLatin1String( "</td><td>" ) + mMetadata.type() + QLatin1String( "</td></tr>\n" );
176+
177+
// Language
178+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Language" ) + QLatin1String( "</td><td>" ) + mMetadata.language() + QLatin1String( "</td></tr>\n" );
179+
180+
// Abstract
181+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Abstract" ) + QLatin1String( "</td><td>" ) + mMetadata.abstract() + QLatin1String( "</td></tr>\n" );
182+
183+
// Categories
184+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Categories" ) + QLatin1String( "</td><td>" ) + mMetadata.categories().join( QStringLiteral( ", " ) ) + QLatin1String( "</td></tr>\n" );
185+
186+
// Keywords
187+
myMetadata += QLatin1String( "<tr><td class=\"highlight\">" ) + QObject::tr( "Keywords" ) + QLatin1String( "</td><td>\n" );
188+
QMapIterator<QString, QStringList> i( mMetadata.keywords() );
189+
if ( i.hasNext() )
190+
{
191+
myMetadata += QStringLiteral( "<table width=\"100%\" class=\"tabular-view\">\n" );
192+
myMetadata += "<tr><th>" + QObject::tr( "Vocabulary" ) + "</th><th>" + QObject::tr( "Items" ) + "</th></tr>\n";
193+
int j = 1;
194+
while ( i.hasNext() )
195+
{
196+
i.next();
197+
QString rowClass;
198+
if ( j % 2 )
199+
rowClass = QString( "class=\"odd-row\"" );
200+
myMetadata += "<tr " + rowClass + "><td>" + i.key() + "</td><td>" + i.value().join( QStringLiteral( ", " ) ) + "</td></tr>\n";
201+
j++;
202+
}
203+
myMetadata += QLatin1String( "</table>\n" ); // End keywords table
204+
}
205+
myMetadata += QLatin1String( "</td></tr>\n" ); // End of keywords row
206+
myMetadata += QLatin1String( "</table>\n" ); // End identification table
207+
return myMetadata;
208+
}
209+
210+
QString QgsLayerMetadataFormatter::historySectionHtml() const
211+
{
212+
QString myMetadata;
213+
const QStringList historyItems = mMetadata.history();
214+
if ( historyItems.isEmpty() )
215+
{
216+
myMetadata += QLatin1String( "<p>" ) + QObject::tr( "No history yet." ) + QLatin1String( "</p>\n" );
217+
}
218+
else
219+
{
220+
myMetadata = QStringLiteral( "<table width=\"100%\" class=\"tabular-view\">\n" );
221+
myMetadata += "<tr><th>" + QObject::tr( "ID" ) + "</th><th>" + QObject::tr( "Action" ) + "</th></tr>\n";
222+
int i = 1;
223+
for ( const QString &history : historyItems )
224+
{
225+
QString rowClass;
226+
if ( i % 2 )
227+
rowClass = QString( "class=\"odd-row\"" );
228+
myMetadata += "<tr " + rowClass + "><td width=\"5%\">" + QString::number( i ) + "</td><td>" + history + "</td></tr>\n";
229+
i++;
230+
}
231+
myMetadata += QLatin1String( "</table>\n" );
232+
}
233+
return myMetadata;
234+
}
235+
236+
QString QgsLayerMetadataFormatter::linksSectionHtml() const
237+
{
238+
QString myMetadata;
239+
const QList<QgsLayerMetadata::Link> &links = mMetadata.links();
240+
if ( links.isEmpty() )
241+
{
242+
myMetadata += QLatin1String( "<p>" ) + QObject::tr( "No links yet." ) + QLatin1String( "</p>\n" );
243+
}
244+
else
245+
{
246+
myMetadata = QStringLiteral( "<table width=\"100%\" class=\"tabular-view\">\n" );
247+
myMetadata += "<tr><th>" + QObject::tr( "ID" ) + "</th><th>" + QObject::tr( "Name" ) + "</th><th>" + QObject::tr( "Type" ) + "</th><th>" + QObject::tr( "URL" ) + "</th><th>" + QObject::tr( "Description" ) + "</th><th>" + QObject::tr( "Format" ) + "</th><th>" + QObject::tr( "MIME Type" ) + "</th><th>" + QObject::tr( "Size" ) + "</th></tr>\n";
248+
int i = 1;
249+
for ( const QgsLayerMetadata::Link &link : links )
250+
{
251+
QString rowClass;
252+
if ( i % 2 )
253+
rowClass = QString( "class=\"odd-row\"" );
254+
myMetadata += "<tr " + rowClass + "><td>" + QString::number( i ) + "</td><td>" + link.name + "</td><td>" + link.type + "</td><td>" + link.url + "</td><td>" + link.description + "</td><td>" + link.format + "</td><td>" + link.mimeType + "</td><td>" + link.size + "</td></tr>\n";
255+
i++;
256+
}
257+
myMetadata += QLatin1String( "</table>\n" );
258+
}
259+
return myMetadata;
260+
}

0 commit comments

Comments
 (0)