QGIS API Documentation  3.0.2-Girona (307d082)
qgslayermetadataformatter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayermetadataformatter.cpp
3  ---------------------
4  begin : September 2017
5  copyright : (C) 2017 by Etienne Trimaille
6  email : etienne.trimaille at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include <QStringBuilder>
16 
18 #include "qgslayermetadata.h"
19 
20 
22  : mMetadata( metadata )
23 {
24 }
25 
27 {
28  QString myMetadata = QStringLiteral( "<table class=\"list-view\">\n" );
29  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Fees" ) + QStringLiteral( "</td><td>" ) + mMetadata.fees() + QStringLiteral( "</td></tr>\n" );
30  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Licenses" ) + QStringLiteral( "</td><td>" ) + mMetadata.licenses().join( QStringLiteral( "<br />" ) ) + QStringLiteral( "</td></tr>\n" );
31  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Rights" ) + QStringLiteral( "</td><td>" ) + mMetadata.rights().join( QStringLiteral( "<br />" ) ) + QStringLiteral( "</td></tr>\n" );
32  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Constraints" ) + QStringLiteral( "</td><td>" );
33  const QList<QgsLayerMetadata::Constraint> &constraints = mMetadata.constraints();
34  bool notFirstRow = false;
35  for ( const QgsLayerMetadata::Constraint &constraint : constraints )
36  {
37  if ( notFirstRow )
38  {
39  myMetadata += QLatin1String( "<br />" );
40  }
41  myMetadata += QStringLiteral( "<strong>" ) + constraint.type + QStringLiteral( ": </strong>" ) + constraint.constraint;
42  notFirstRow = true;
43  }
44  myMetadata += QLatin1String( "</td></tr>\n" );
45  mMetadata.rights().join( QStringLiteral( "<br />" ) ) + QStringLiteral( "</td></tr>\n" );
46  myMetadata += QLatin1String( "</table>\n" );
47  return myMetadata;
48 }
49 
51 {
52  const QList<QgsLayerMetadata::Contact> &contacts = mMetadata.contacts();
53  QString myMetadata;
54  if ( contacts.isEmpty() )
55  {
56  myMetadata += QStringLiteral( "<p>" ) + QObject::tr( "No contact yet." ) + QStringLiteral( "</p>" );
57  }
58  else
59  {
60  myMetadata = QStringLiteral( "<table width=\"100%\" class=\"tabular-view\">\n" );
61  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";
62  int i = 1;
63  for ( const QgsLayerMetadata::Contact &contact : contacts )
64  {
65  QString rowClass;
66  if ( i % 2 )
67  rowClass = QStringLiteral( "class=\"odd-row\"" );
68  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>";
69  bool notFirstRow = false;
70  for ( const QgsLayerMetadata::Address &oneAddress : contact.addresses )
71  {
72  if ( notFirstRow )
73  {
74  myMetadata += QLatin1String( "<br />\n" );
75  }
76  if ( ! oneAddress.type.isEmpty() )
77  {
78  myMetadata += oneAddress.type + QStringLiteral( "<br />" );
79  }
80  if ( ! oneAddress.address.isEmpty() )
81  {
82  myMetadata += oneAddress.address + QStringLiteral( "<br />" );
83  }
84  if ( ! oneAddress.postalCode.isEmpty() )
85  {
86  myMetadata += oneAddress.postalCode + QStringLiteral( "<br />" );
87  }
88  if ( ! oneAddress.city.isEmpty() )
89  {
90  myMetadata += oneAddress.city + QStringLiteral( "<br />" );
91  }
92  if ( ! oneAddress.administrativeArea.isEmpty() )
93  {
94  myMetadata += oneAddress.administrativeArea + QStringLiteral( "<br />" );
95  }
96  if ( ! oneAddress.country.isEmpty() )
97  {
98  myMetadata += oneAddress.country;
99  }
100  notFirstRow = true;
101  }
102  myMetadata += QLatin1String( "</td></tr>\n" );
103  i++;
104  }
105  myMetadata += QLatin1String( "</table>\n" );
106  }
107  return myMetadata;
108 }
109 
111 {
112  QString myMetadata = QStringLiteral( "<table class=\"list-view\">\n" );
113  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "CRS" ) + QStringLiteral( "</td><td>" );
114  if ( mMetadata.crs().isValid() )
115  {
116  myMetadata += mMetadata.crs().authid() + QStringLiteral( " - " );
117  myMetadata += mMetadata.crs().description() + QStringLiteral( " - " );
118  if ( mMetadata.crs().isGeographic() )
119  myMetadata += QObject::tr( "Geographic" );
120  else
121  myMetadata += QObject::tr( "Projected" );
122  }
123  myMetadata += QLatin1String( "</td></tr>\n" );
124 
125  const QgsLayerMetadata::Extent extent = mMetadata.extent();
126  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Spatial Extent" ) + QStringLiteral( "</td><td>" );
127  const QList< QgsLayerMetadata::SpatialExtent > spatialExtents = extent.spatialExtents();
128  bool notFirstRow = false;
129  for ( const QgsLayerMetadata::SpatialExtent &spatialExtent : spatialExtents )
130  {
131  if ( notFirstRow )
132  {
133  myMetadata += QLatin1String( "<br />\n" );
134  }
135  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "CRS" ) + QStringLiteral( ": </strong>" ) + spatialExtent.extentCrs.authid() + QStringLiteral( " - " );
136  myMetadata += spatialExtent.extentCrs.description() + QStringLiteral( " - " );
137  if ( spatialExtent.extentCrs.isGeographic() )
138  myMetadata += QObject::tr( "Geographic" );
139  else
140  myMetadata += QObject::tr( "Projected" );
141  myMetadata += QStringLiteral( "<br />" );
142  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "X Minimum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.xMinimum() ) + QStringLiteral( "<br />" );
143  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Y Minimum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.yMinimum() ) + QStringLiteral( "<br />" );
144  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "X Maximum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.xMaximum() ) + QStringLiteral( "<br />" );
145  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Y Maximum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.yMaximum() ) + QStringLiteral( "<br />" );
146  if ( spatialExtent.bounds.zMinimum() || spatialExtent.bounds.zMinimum() )
147  {
148  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Z Minimum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.zMinimum() ) + QStringLiteral( "<br />" );
149  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Z Maximum" ) + QStringLiteral( ": </strong>" ) + qgsDoubleToString( spatialExtent.bounds.zMaximum() );
150  }
151  notFirstRow = true;
152  }
153  myMetadata += QLatin1String( "</td></tr>\n" );
154  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Temporal Extent" ) + QStringLiteral( "</td><td>" );
155  const QList< QgsDateTimeRange > temporalExtents = extent.temporalExtents();
156  notFirstRow = false;
157  for ( const QgsDateTimeRange &temporalExtent : temporalExtents )
158  {
159  if ( notFirstRow )
160  {
161  myMetadata += QLatin1String( "<br />\n" );
162  }
163  if ( temporalExtent.isInstant() )
164  {
165  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Instant" ) + QStringLiteral( ": </strong>" ) + temporalExtent.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate );
166  }
167  else
168  {
169  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "Start" ) + QStringLiteral( ": </strong>" ) + temporalExtent.begin().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate ) + QStringLiteral( "<br />\n" );
170  myMetadata += QStringLiteral( "<strong>" ) + QObject::tr( "End" ) + QStringLiteral( ": </strong>" ) + temporalExtent.end().toTimeSpec( Qt::OffsetFromUTC ).toString( Qt::ISODate );
171  }
172  notFirstRow = true;
173  }
174  myMetadata += QLatin1String( "</td></tr>\n" );
175  myMetadata += QLatin1String( "</table>\n" );
176  return myMetadata;
177 }
178 
180 {
181  QString myMetadata = QStringLiteral( "<table class=\"list-view\">\n" );
182 
183  // Identifier
184  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Identifier" ) + QStringLiteral( "</td><td>" ) + mMetadata.identifier() + QStringLiteral( "</td></tr>\n" );
185 
186  // Parent Identifier
187  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Parent Identifier" ) + QStringLiteral( "</td><td>" ) + mMetadata.parentIdentifier() + QStringLiteral( "</td></tr>\n" );
188 
189  // Title
190  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Title" ) + QStringLiteral( "</td><td>" ) + mMetadata.title() + QStringLiteral( "</td></tr>\n" );
191 
192  // Type
193  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Type" ) + QStringLiteral( "</td><td>" ) + mMetadata.type() + QStringLiteral( "</td></tr>\n" );
194 
195  // Language
196  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Language" ) + QStringLiteral( "</td><td>" ) + mMetadata.language() + QStringLiteral( "</td></tr>\n" );
197 
198  // Abstract
199  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Abstract" ) + QStringLiteral( "</td><td>" ) + mMetadata.abstract() + QStringLiteral( "</td></tr>\n" );
200 
201  // Categories
202  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Categories" ) + QStringLiteral( "</td><td>" ) + mMetadata.categories().join( QStringLiteral( ", " ) ) + QStringLiteral( "</td></tr>\n" );
203 
204  // Keywords
205  myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + QObject::tr( "Keywords" ) + QStringLiteral( "</td><td>\n" );
206  QMapIterator<QString, QStringList> i( mMetadata.keywords() );
207  if ( i.hasNext() )
208  {
209  myMetadata += QStringLiteral( "<table width=\"100%\" class=\"tabular-view\">\n" );
210  myMetadata += "<tr><th>" + QObject::tr( "Vocabulary" ) + "</th><th>" + QObject::tr( "Items" ) + "</th></tr>\n";
211  int j = 1;
212  while ( i.hasNext() )
213  {
214  i.next();
215  QString rowClass;
216  if ( j % 2 )
217  rowClass = QStringLiteral( "class=\"odd-row\"" );
218  myMetadata += "<tr " + rowClass + "><td>" + i.key() + "</td><td>" + i.value().join( QStringLiteral( ", " ) ) + "</td></tr>\n";
219  j++;
220  }
221  myMetadata += QLatin1String( "</table>\n" ); // End keywords table
222  }
223  myMetadata += QLatin1String( "</td></tr>\n" ); // End of keywords row
224  myMetadata += QLatin1String( "</table>\n" ); // End identification table
225  return myMetadata;
226 }
227 
229 {
230  QString myMetadata;
231  const QStringList historyItems = mMetadata.history();
232  if ( historyItems.isEmpty() )
233  {
234  myMetadata += QStringLiteral( "<p>" ) + QObject::tr( "No history yet." ) + QStringLiteral( "</p>\n" );
235  }
236  else
237  {
238  myMetadata = QStringLiteral( "<table width=\"100%\" class=\"tabular-view\">\n" );
239  myMetadata += "<tr><th>" + QObject::tr( "ID" ) + "</th><th>" + QObject::tr( "Action" ) + "</th></tr>\n";
240  int i = 1;
241  for ( const QString &history : historyItems )
242  {
243  QString rowClass;
244  if ( i % 2 )
245  rowClass = QStringLiteral( "class=\"odd-row\"" );
246  myMetadata += "<tr " + rowClass + "><td width=\"5%\">" + QString::number( i ) + "</td><td>" + history + "</td></tr>\n";
247  i++;
248  }
249  myMetadata += QLatin1String( "</table>\n" );
250  }
251  return myMetadata;
252 }
253 
255 {
256  QString myMetadata;
257  const QList<QgsLayerMetadata::Link> &links = mMetadata.links();
258  if ( links.isEmpty() )
259  {
260  myMetadata += QStringLiteral( "<p>" ) + QObject::tr( "No links yet." ) + QStringLiteral( "</p>\n" );
261  }
262  else
263  {
264  myMetadata = QStringLiteral( "<table width=\"100%\" class=\"tabular-view\">\n" );
265  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";
266  int i = 1;
267  for ( const QgsLayerMetadata::Link &link : links )
268  {
269  QString rowClass;
270  if ( i % 2 )
271  rowClass = QStringLiteral( "class=\"odd-row\"" );
272  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";
273  i++;
274  }
275  myMetadata += QLatin1String( "</table>\n" );
276  }
277  return myMetadata;
278 }
QString parentIdentifier() const
A reference, URI, URL or some other mechanism to identify the parent resource that this resource is a...
QString contactsSectionHtml() const
Formats the "Contacts" section according to a metadata object.
QString historySectionHtml() const
Formats the "History" section according to a metadata object.
QStringList rights() const
Returns a list of attribution or copyright strings associated with the resource.
QString accessSectionHtml() const
Formats the "Access" section according to a metadata object.
QString title() const
Returns the human readable name of the resource, typically displayed in search results.
QString linksSectionHtml() const
Formats the "Links" section according to a metadata object.
QString abstract() const
Returns a free-form description of the resource.
QList< QgsDateTimeRange > temporalExtents() const
Temporal extents of the resource.
QString identifier() const
A reference, URI, URL or some other mechanism to identify the resource.
const QgsLayerMetadata::Extent & extent() const
Returns the spatial and temporal extents associated with the resource.
QgsLayerMetadata::ContactList contacts() const
Returns a list of contact persons or entities associated with the resource.
Metadata address structure.
Metadata constraint structure.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:237
QString extentSectionHtml() const
Formats the "Extents" section according to a metadata object.
QString language() const
Returns the human language associated with the resource.
QStringList history() const
Returns a freeform description of the history or lineage of the resource.
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
QString postalCode
Postal (or ZIP) code.
Metadata extent structure.
QString type() const
Returns the nature of the resource.
bool isGeographic() const
Returns whether the CRS is a geographic CRS (using lat/lon coordinates)
QString identificationSectionHtml() const
Formats the "Identification" section according to a metadata object.
KeywordMap keywords() const
Returns the keywords map, which is a set of descriptive keywords associated with the resource...
A structured metadata store for a map layer.
QgsLayerMetadata::LinkList links() const
Returns a list of online resources associated with the resource.
Metadata contact structure.
QgsLayerMetadata::ConstraintList constraints() const
Returns a list of constraints associated with using the resource.
QgsLayerMetadataFormatter(const QgsLayerMetadata &metadata)
Constructor for QgsLayerMetadataFormatter.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system described by the layer&#39;s metadata.
QString city
City or locality name.
QStringList licenses() const
Returns a list of licenses associated with the resource (examples: http://opendefinition.org/licenses/).
QList< QgsLayerMetadata::SpatialExtent > spatialExtents() const
Spatial extents of the resource.
QString fees() const
Returns any fees associated with using the resource.
QStringList categories() const
Returns categories of the resource.
Metadata spatial extent structure.
QString address
Free-form physical address component, e.g.
QString authid() const
Returns the authority identifier for the CRS.
QString administrativeArea
Administrative area (state, province/territory, etc.).
QString country
Free-form country string.
QString type
Type of address, e.g.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.