QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsabstractmetadatabase.cpp
Go to the documentation of this file.
1/***************************************************************************
2 QgsAbstractMetadataBase.cpp
3 --------------------
4 begin : March 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsmaplayer.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28{
29 return mIdentifier;
30}
31
36
41
46
48{
49 return mType;
50}
51
53{
54 mType = type;
55}
56
58{
59 return mTitle;
60}
61
63{
64 mTitle = title;
65}
66
68{
69 return mAbstract;
70}
71
76
78{
79 return mHistory;
80}
81
83{
85}
86
87void QgsAbstractMetadataBase::addHistoryItem( const QString &text )
88{
89 mHistory << text;
90}
91
92QMap<QString, QStringList> QgsAbstractMetadataBase::keywords() const
93{
94 return mKeywords;
95}
96
97void QgsAbstractMetadataBase::setKeywords( const QMap<QString, QStringList> &keywords )
98{
100}
101
102void QgsAbstractMetadataBase::addKeywords( const QString &vocabulary, const QStringList &keywords )
103{
104 mKeywords.insert( vocabulary, keywords );
105}
106
107bool QgsAbstractMetadataBase::removeKeywords( const QString &vocabulary )
108{
109 return mKeywords.remove( vocabulary );
110}
111
113{
114 return mKeywords.keys();
115}
116
117QStringList QgsAbstractMetadataBase::keywords( const QString &vocabulary ) const
118{
119 return mKeywords.value( vocabulary );
120}
121
123{
124 if ( mKeywords.contains( u"gmd:topicCategory"_s ) )
125 {
126 return mKeywords.value( u"gmd:topicCategory"_s );
127 }
128 else
129 {
130 return QStringList();
131 }
132}
133
134void QgsAbstractMetadataBase::setCategories( const QStringList &category )
135{
136 mKeywords.insert( u"gmd:topicCategory"_s, category );
137}
138
139QList<QgsAbstractMetadataBase::Contact> QgsAbstractMetadataBase::contacts() const
140{
141 return mContacts;
142}
143
144void QgsAbstractMetadataBase::setContacts( const QList<QgsAbstractMetadataBase::Contact> &contacts )
145{
147}
148
153
154QList<QgsAbstractMetadataBase::Link> QgsAbstractMetadataBase::links() const
155{
156 return mLinks;
157}
158
159void QgsAbstractMetadataBase::setLinks( const QList<QgsAbstractMetadataBase::Link> &links )
160{
161 mLinks = links;
162}
163
168
170{
171 return mDates.value( type );
172}
173
175{
176 if ( !date.isValid() || date.isNull() )
177 mDates.remove( type );
178 else
179 mDates[type] = date;
180}
181
183{
184 return mLanguage;
185}
186
188{
190}
191
192bool QgsAbstractMetadataBase::readMetadataXml( const QDomElement &metadataElement, const QgsReadWriteContext &context )
193{
194 QDomNode mnl;
195 QDomElement mne;
196
197 // set identifier
198 mnl = metadataElement.namedItem( u"identifier"_s );
199 mIdentifier = mnl.toElement().text();
200
201 // set parent identifier
202 mnl = metadataElement.namedItem( u"parentidentifier"_s );
203 mParentIdentifier = mnl.toElement().text();
204
205 // set language
206 mnl = metadataElement.namedItem( u"language"_s );
207 mLanguage = mnl.toElement().text();
208
209 // set type
210 mnl = metadataElement.namedItem( u"type"_s );
211 mType = mnl.toElement().text();
212
213 // set title
214 mnl = metadataElement.namedItem( u"title"_s );
215 mTitle = mnl.toElement().text();
216
217 // set abstract
218 mnl = metadataElement.namedItem( u"abstract"_s );
219 mAbstract = mnl.toElement().text();
220
221 mType = context.projectTranslator()->translate( "metadata", mType );
222 mTitle = context.projectTranslator()->translate( "metadata", mTitle );
223 mAbstract = context.projectTranslator()->translate( "metadata", mAbstract );
224
225 // set keywords
226 const QDomNodeList keywords = metadataElement.elementsByTagName( u"keywords"_s );
227 mKeywords.clear();
228 for ( int i = 0; i < keywords.size(); i++ )
229 {
230 QStringList keywordsList;
231 mnl = keywords.at( i );
232 mne = mnl.toElement();
233
234 const QDomNodeList el = mne.elementsByTagName( u"keyword"_s );
235 for ( int j = 0; j < el.size(); j++ )
236 {
237 keywordsList.append( el.at( j ).toElement().text() );
238 }
239 addKeywords( mne.attribute( u"vocabulary"_s ), keywordsList );
240 }
241
242 // contact
243 const QDomNodeList contactsList = metadataElement.elementsByTagName( u"contact"_s );
244 mContacts.clear();
245 for ( int i = 0; i < contactsList.size(); i++ )
246 {
247 mnl = contactsList.at( i );
248 mne = mnl.toElement();
249
251 oneContact.name = mne.namedItem( u"name"_s ).toElement().text();
252 oneContact.organization = mne.namedItem( u"organization"_s ).toElement().text();
253 oneContact.position = mne.namedItem( u"position"_s ).toElement().text();
254 oneContact.voice = mne.namedItem( u"voice"_s ).toElement().text();
255 oneContact.fax = mne.namedItem( u"fax"_s ).toElement().text();
256 oneContact.email = mne.namedItem( u"email"_s ).toElement().text();
257 oneContact.role = mne.namedItem( u"role"_s ).toElement().text();
258
259 QList< QgsAbstractMetadataBase::Address > addresses;
260 const QDomNodeList addressList = mne.elementsByTagName( u"contactAddress"_s );
261 for ( int j = 0; j < addressList.size(); j++ )
262 {
263 const QDomElement addressElement = addressList.at( j ).toElement();
265 oneAddress.address = addressElement.namedItem( u"address"_s ).toElement().text();
266 oneAddress.administrativeArea = addressElement.namedItem( u"administrativearea"_s ).toElement().text();
267 oneAddress.city = addressElement.namedItem( u"city"_s ).toElement().text();
268 oneAddress.country = addressElement.namedItem( u"country"_s ).toElement().text();
269 oneAddress.postalCode = addressElement.namedItem( u"postalcode"_s ).toElement().text();
270 oneAddress.type = addressElement.namedItem( u"type"_s ).toElement().text();
271 addresses << oneAddress;
272 }
273 oneContact.addresses = addresses;
274 addContact( oneContact );
275 }
276
277 // links
278 mnl = metadataElement.namedItem( u"links"_s );
279 mne = mnl.toElement();
280 mLinks.clear();
281 const QDomNodeList el = mne.elementsByTagName( u"link"_s );
282 for ( int i = 0; i < el.size(); i++ )
283 {
284 mne = el.at( i ).toElement();
286 oneLink.name = mne.attribute( u"name"_s );
287 oneLink.type = mne.attribute( u"type"_s );
288 oneLink.url = mne.attribute( u"url"_s );
289 oneLink.description = mne.attribute( u"description"_s );
290 oneLink.format = mne.attribute( u"format"_s );
291 oneLink.mimeType = mne.attribute( u"mimeType"_s );
292 oneLink.size = mne.attribute( u"size"_s );
293 addLink( oneLink );
294 }
295
296 // history
297 const QDomNodeList historyNodeList = metadataElement.elementsByTagName( u"history"_s );
298 QStringList historyList;
299 for ( int i = 0; i < historyNodeList.size(); i++ )
300 {
301 mnl = historyNodeList.at( i );
302 mne = mnl.toElement();
303 historyList.append( mne.text() );
304 }
305 setHistory( historyList );
306
307 {
308 mDates.clear();
309 const QDomElement dateElement = metadataElement.firstChildElement( u"dates"_s );
310 if ( !dateElement.isNull() )
311 {
312 const QDomNodeList dateNodeList = dateElement.elementsByTagName( u"date"_s );
313 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
314 for ( int i = 0; i < dateNodeList.size(); i++ )
315 {
316 const QDomElement dateElement = dateNodeList.at( i ).toElement();
317 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.keyToValue( dateElement.attribute( u"type"_s ).toStdString().c_str() ) );
318 const QDateTime value = QDateTime::fromString( dateElement.attribute( u"value"_s ), Qt::ISODate );
319 if ( value.isValid() && !value.isNull() )
320 mDates.insert( type, value );
321 }
322 }
323 }
324
325 return true;
326}
327
328bool QgsAbstractMetadataBase::writeMetadataXml( QDomElement &metadataElement, QDomDocument &document, const QgsReadWriteContext & ) const
329{
330 // identifier
331 QDomElement identifier = document.createElement( u"identifier"_s );
332 const QDomText identifierText = document.createTextNode( mIdentifier );
333 identifier.appendChild( identifierText );
334 metadataElement.appendChild( identifier );
335
336 // parent identifier
337 QDomElement parentIdentifier = document.createElement( u"parentidentifier"_s );
338 const QDomText parentIdentifierText = document.createTextNode( mParentIdentifier );
339 parentIdentifier.appendChild( parentIdentifierText );
340 metadataElement.appendChild( parentIdentifier );
341
342 // language
343 QDomElement language = document.createElement( u"language"_s );
344 const QDomText languageText = document.createTextNode( mLanguage );
345 language.appendChild( languageText );
346 metadataElement.appendChild( language );
347
348 // type
349 QDomElement type = document.createElement( u"type"_s );
350 const QDomText typeText = document.createTextNode( mType );
351 type.appendChild( typeText );
352 metadataElement.appendChild( type );
353
354 // title
355 QDomElement title = document.createElement( u"title"_s );
356 const QDomText titleText = document.createTextNode( mTitle );
357 title.appendChild( titleText );
358 metadataElement.appendChild( title );
359
360 // abstract
361 QDomElement abstract = document.createElement( u"abstract"_s );
362 const QDomText abstractText = document.createTextNode( mAbstract );
363 abstract.appendChild( abstractText );
364 metadataElement.appendChild( abstract );
365
366 // keywords
367 QMapIterator<QString, QStringList> i( mKeywords );
368 while ( i.hasNext() )
369 {
370 i.next();
371 QDomElement keywordsElement = document.createElement( u"keywords"_s );
372 keywordsElement.setAttribute( u"vocabulary"_s, i.key() );
373 const QStringList values = i.value();
374 for ( const QString &kw : values )
375 {
376 QDomElement keyword = document.createElement( u"keyword"_s );
377 const QDomText keywordText = document.createTextNode( kw );
378 keyword.appendChild( keywordText );
379 keywordsElement.appendChild( keyword );
380 }
381 metadataElement.appendChild( keywordsElement );
382 }
383
384 // contact
385 for ( const QgsAbstractMetadataBase::Contact &contact : mContacts )
386 {
387 QDomElement contactElement = document.createElement( u"contact"_s );
388 QDomElement nameElement = document.createElement( u"name"_s );
389 QDomElement organizationElement = document.createElement( u"organization"_s );
390 QDomElement positionElement = document.createElement( u"position"_s );
391 QDomElement voiceElement = document.createElement( u"voice"_s );
392 QDomElement faxElement = document.createElement( u"fax"_s );
393 QDomElement emailElement = document.createElement( u"email"_s );
394 QDomElement roleElement = document.createElement( u"role"_s );
395
396 const QDomText nameText = document.createTextNode( contact.name );
397 const QDomText orgaText = document.createTextNode( contact.organization );
398 const QDomText positionText = document.createTextNode( contact.position );
399 const QDomText voiceText = document.createTextNode( contact.voice );
400 const QDomText faxText = document.createTextNode( contact.fax );
401 const QDomText emailText = document.createTextNode( contact.email );
402 const QDomText roleText = document.createTextNode( contact.role );
403
404 for ( const QgsAbstractMetadataBase::Address &oneAddress : contact.addresses )
405 {
406 QDomElement addressElement = document.createElement( u"contactAddress"_s );
407 QDomElement typeElement = document.createElement( u"type"_s );
408 QDomElement addressDetailedElement = document.createElement( u"address"_s );
409 QDomElement cityElement = document.createElement( u"city"_s );
410 QDomElement administrativeAreaElement = document.createElement( u"administrativearea"_s );
411 QDomElement postalCodeElement = document.createElement( u"postalcode"_s );
412 QDomElement countryElement = document.createElement( u"country"_s );
413
414 typeElement.appendChild( document.createTextNode( oneAddress.type ) );
415 addressDetailedElement.appendChild( document.createTextNode( oneAddress.address ) );
416 cityElement.appendChild( document.createTextNode( oneAddress.city ) );
417 administrativeAreaElement.appendChild( document.createTextNode( oneAddress.administrativeArea ) );
418 postalCodeElement.appendChild( document.createTextNode( oneAddress.postalCode ) );
419 countryElement.appendChild( document.createTextNode( oneAddress.country ) );
420
421 addressElement.appendChild( typeElement );
422 addressElement.appendChild( addressDetailedElement );
423 addressElement.appendChild( cityElement );
424 addressElement.appendChild( administrativeAreaElement );
425 addressElement.appendChild( postalCodeElement );
426 addressElement.appendChild( countryElement );
427 contactElement.appendChild( addressElement );
428 }
429
430 nameElement.appendChild( nameText );
431 organizationElement.appendChild( orgaText );
432 positionElement.appendChild( positionText );
433 voiceElement.appendChild( voiceText );
434 faxElement.appendChild( faxText );
435 emailElement.appendChild( emailText );
436 roleElement.appendChild( roleText );
437
438 contactElement.appendChild( nameElement );
439 contactElement.appendChild( organizationElement );
440 contactElement.appendChild( positionElement );
441 contactElement.appendChild( voiceElement );
442 contactElement.appendChild( faxElement );
443 contactElement.appendChild( emailElement );
444 contactElement.appendChild( roleElement );
445 metadataElement.appendChild( contactElement );
446 }
447
448 // links
449 QDomElement links = document.createElement( u"links"_s );
450 for ( const QgsAbstractMetadataBase::Link &link : mLinks )
451 {
452 QDomElement linkElement = document.createElement( u"link"_s );
453 linkElement.setAttribute( u"name"_s, link.name );
454 linkElement.setAttribute( u"type"_s, link.type );
455 linkElement.setAttribute( u"url"_s, link.url );
456 linkElement.setAttribute( u"description"_s, link.description );
457 linkElement.setAttribute( u"format"_s, link.format );
458 linkElement.setAttribute( u"mimeType"_s, link.mimeType );
459 linkElement.setAttribute( u"size"_s, link.size );
460 links.appendChild( linkElement );
461 }
462 metadataElement.appendChild( links );
463
464 // history
465 for ( const QString &history : mHistory )
466 {
467 QDomElement historyElement = document.createElement( u"history"_s );
468 const QDomText historyText = document.createTextNode( history );
469 historyElement.appendChild( historyText );
470 metadataElement.appendChild( historyElement );
471 }
472
473 // dates
474 {
475 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
476 QDomElement datesElement = document.createElement( u"dates"_s );
477 for ( int k = 0; k < dateEnum.keyCount(); k++ )
478 {
479 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.value( k ) );
480 if ( mDates.contains( type ) && mDates.value( type ).isValid() )
481 {
482 QDomElement dateElement = document.createElement( u"date"_s );
483 dateElement.setAttribute( u"type"_s, dateEnum.valueToKey( static_cast< int >( type ) ) );
484 dateElement.setAttribute( u"value"_s, mDates.value( type ).toString( Qt::ISODate ) );
485 datesElement.appendChild( dateElement );
486 }
487 }
488 metadataElement.appendChild( datesElement );
489 }
490
491 return true;
492}
493
495{
496 if ( !mTitle.isEmpty() )
497 {
498 translationContext->registerTranslation( u"metadata"_s, mTitle );
499 }
500 if ( !mType.isEmpty() )
501 {
502 translationContext->registerTranslation( u"metadata"_s, mType );
503 }
504 if ( !mAbstract.isEmpty() )
505 {
506 translationContext->registerTranslation( u"metadata"_s, mAbstract );
507 }
508}
509
511{
512 if ( !other )
513 return;
514
515 if ( !other->identifier().isEmpty() )
516 mIdentifier = other->identifier();
517
518 if ( !other->parentIdentifier().isEmpty() )
520
521 if ( !other->language().isEmpty() )
522 mLanguage = other->language();
523
524 if ( !other->type().isEmpty() )
525 mType = other->type();
526
527 if ( !other->title().isEmpty() )
528 mTitle = other->title();
529
530 if ( !other->abstract().isEmpty() )
531 mAbstract = other->abstract();
532
533 if ( !other->history().isEmpty() )
534 mHistory = other->history();
535
536 if ( !other->keywords().isEmpty() )
537 mKeywords = other->keywords();
538
539 if ( !other->contacts().isEmpty() )
540 mContacts = other->contacts();
541
542 if ( !other->links().isEmpty() )
543 mLinks = other->links();
544
545 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
546 for ( int k = 0; k < dateEnum.keyCount(); k++ )
547 {
548 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.value( k ) );
549 if ( other->mDates.contains( type ) && other->mDates.value( type ).isValid() )
550 {
551 mDates.insert( type, other->mDates[type] );
552 }
553 }
554
555}
556
558{
559 return ( ( mIdentifier == metadataOther.mIdentifier ) &&
560 ( mParentIdentifier == metadataOther.mParentIdentifier ) &&
561 ( mLanguage == metadataOther.mLanguage ) &&
562 ( mType == metadataOther.mType ) &&
563 ( mTitle == metadataOther.mTitle ) &&
564 ( mAbstract == metadataOther.mAbstract ) &&
565 ( mHistory == metadataOther.mHistory ) &&
566 ( mKeywords == metadataOther.mKeywords ) &&
567 ( mContacts == metadataOther.mContacts ) &&
568 ( mLinks == metadataOther.mLinks ) &&
569 ( mDates == metadataOther.mDates ) );
570}
571
572
574{
575 return name == other.name &&
576 organization == other.organization &&
577 position == other.position &&
578 addresses == other.addresses &&
579 voice == other.voice &&
580 fax == other.fax &&
581 email == other.email &&
582 role == other.role;
583}
584
586{
587 return name == other.name &&
588 type == other.type &&
589 description == other.description &&
590 url == other.url &&
591 format == other.format &&
592 mimeType == other.mimeType &&
593 size == other.size;
594}
595
597{
598 return type == other.type &&
599 address == other.address &&
600 city == other.city &&
602 postalCode == other.postalCode &&
603 country == other.country;
604}
MetadataDateType
Date types for metadata.
Definition qgis.h:4765
QgsAbstractMetadataBase::ContactList mContacts
void setAbstract(const QString &abstract)
Sets a free-form abstract (description) of the resource.
void setType(const QString &type)
Sets the type (nature) of the resource.
void setParentIdentifier(const QString &parentIdentifier)
Sets a reference, URI, URL or some other mechanism to identify the parent resource that this resource...
void addContact(const QgsAbstractMetadataBase::Contact &contact)
Adds an individual contact to the existing contacts.
void setTitle(const QString &title)
Sets the human readable title (name) of the resource, typically displayed in search results.
void setHistory(const QStringList &history)
Sets the freeform description of the history or lineage of the resource.
QgsAbstractMetadataBase::ContactList contacts() const
Returns a list of contact persons or entities associated with the resource.
virtual void combine(const QgsAbstractMetadataBase *other)
Combines the metadata from this object with the metadata from an other object.
void setLinks(const QgsAbstractMetadataBase::LinkList &links)
Sets the list of online resources associated with the resource.
void setIdentifier(const QString &identifier)
Sets the reference, URI, URL or some other mechanism to identify the resource.
QStringList categories() const
Returns categories of the resource.
QString abstract() const
Returns a free-form description of the resource.
QMap< Qgis::MetadataDateType, QDateTime > mDates
Metadata dates.
QStringList keywordVocabularies() const
Returns a list of keyword vocabularies contained in the metadata.
QString title() const
Returns the human readable name of the resource, typically displayed in search results.
virtual bool writeMetadataXml(QDomElement &metadataElement, QDomDocument &document, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Stores state in a DOM node.
void setContacts(const QgsAbstractMetadataBase::ContactList &contacts)
Sets the list of contacts or entities associated with the resource.
void setKeywords(const QgsAbstractMetadataBase::KeywordMap &keywords)
Sets the keywords map, which is a set of descriptive keywords associated with the resource.
QStringList history() const
Returns a freeform description of the history or lineage of the resource.
void setDateTime(Qgis::MetadataDateType type, QDateTime date)
Sets a date value for the specified date type.
QDateTime dateTime(Qgis::MetadataDateType type) const
Returns the date for the specified date type.
QgsAbstractMetadataBase::KeywordMap mKeywords
Keywords map.
QgsAbstractMetadataBase::KeywordMap keywords() const
Returns the keywords map, which is a set of descriptive keywords associated with the resource.
bool equals(const QgsAbstractMetadataBase &other) const
Tests whether the common metadata fields in this object are equal to other.
QgsAbstractMetadataBase::LinkList links() const
Returns a list of online resources associated with the resource.
void setLanguage(const QString &language)
Sets the human language associated with the resource.
virtual void registerTranslations(QgsTranslationContext *translationContext) const
Registers metadata translation strings.
QString parentIdentifier() const
A reference, URI, URL or some other mechanism to identify the parent resource that this resource is a...
QString language() const
Returns the human language associated with the resource.
void addKeywords(const QString &vocabulary, const QStringList &keywords)
Adds a list of descriptive keywords for a specified vocabulary.
void addLink(const QgsAbstractMetadataBase::Link &link)
Adds an individual link to the existing links.
bool removeKeywords(const QString &vocabulary)
Remove a vocabulary from the list.
QgsAbstractMetadataBase::LinkList mLinks
QString type() const
Returns the nature of the resource.
QString identifier() const
A reference, URI, URL or some other mechanism to identify the resource.
QgsAbstractMetadataBase()=default
Constructor for QgsAbstractMetadataBase.
void setCategories(const QStringList &categories)
Sets categories of the resource.
virtual bool readMetadataXml(const QDomElement &metadataElement, const QgsReadWriteContext &context=QgsReadWriteContext())
Sets state from DOM document.
void addHistoryItem(const QString &text)
Adds a single history text to the end of the existing history list.
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
Translates a string using the Qt QTranslator mechanism.
A container for the context for various read/write operations on objects.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
Used for the collecting of strings from projects for translation and creation of ts files.
void registerTranslation(const QString &context, const QString &source)
Registers the source to be translated.
bool operator==(const QgsAbstractMetadataBase::Address &other) const
QString administrativeArea
Administrative area (state, province/territory, etc.).
QString address
Free-form physical address component, e.g.
QString city
City or locality name.
QString country
Free-form country string.
QString postalCode
Postal (or ZIP) code.
QString position
Position/title of contact.
QList< QgsAbstractMetadataBase::Address > addresses
List of addresses associated with this contact.
QString email
Electronic mail address.
QString organization
Organization contact belongs to/represents.
bool operator==(const QgsAbstractMetadataBase::Contact &other) const