QGIS API Documentation 4.1.0-Master (31622b25bb0)
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
25#include "moc_qgsabstractmetadatabase.cpp"
26
27using namespace Qt::StringLiterals;
28
30{
31 return mIdentifier;
32}
33
38
43
48
50{
51 return mType;
52}
53
55{
56 mType = type;
57}
58
60{
61 return mTitle;
62}
63
65{
66 mTitle = title;
67}
68
70{
71 return mAbstract;
72}
73
78
80{
81 return mHistory;
82}
83
85{
87}
88
89void QgsAbstractMetadataBase::addHistoryItem( const QString &text )
90{
91 mHistory << text;
92}
93
94QMap<QString, QStringList> QgsAbstractMetadataBase::keywords() const
95{
96 return mKeywords;
97}
98
99void QgsAbstractMetadataBase::setKeywords( const QMap<QString, QStringList> &keywords )
100{
102}
103
104void QgsAbstractMetadataBase::addKeywords( const QString &vocabulary, const QStringList &keywords )
105{
106 mKeywords.insert( vocabulary, keywords );
107}
108
109bool QgsAbstractMetadataBase::removeKeywords( const QString &vocabulary )
110{
111 return mKeywords.remove( vocabulary );
112}
113
115{
116 return mKeywords.keys();
117}
118
119QStringList QgsAbstractMetadataBase::keywords( const QString &vocabulary ) const
120{
121 return mKeywords.value( vocabulary );
122}
123
125{
126 if ( mKeywords.contains( u"gmd:topicCategory"_s ) )
127 {
128 return mKeywords.value( u"gmd:topicCategory"_s );
129 }
130 else
131 {
132 return QStringList();
133 }
134}
135
136void QgsAbstractMetadataBase::setCategories( const QStringList &category )
137{
138 mKeywords.insert( u"gmd:topicCategory"_s, category );
139}
140
141QList<QgsAbstractMetadataBase::Contact> QgsAbstractMetadataBase::contacts() const
142{
143 return mContacts;
144}
145
146void QgsAbstractMetadataBase::setContacts( const QList<QgsAbstractMetadataBase::Contact> &contacts )
147{
149}
150
155
156QList<QgsAbstractMetadataBase::Link> QgsAbstractMetadataBase::links() const
157{
158 return mLinks;
159}
160
161void QgsAbstractMetadataBase::setLinks( const QList<QgsAbstractMetadataBase::Link> &links )
162{
163 mLinks = links;
164}
165
170
172{
173 return mDates.value( type );
174}
175
177{
178 if ( !date.isValid() || date.isNull() )
179 mDates.remove( type );
180 else
181 mDates[type] = date;
182}
183
185{
186 return mLanguage;
187}
188
190{
192}
193
194bool QgsAbstractMetadataBase::readMetadataXml( const QDomElement &metadataElement, const QgsReadWriteContext &context )
195{
196 QDomNode mnl;
197 QDomElement mne;
198
199 // set identifier
200 mnl = metadataElement.namedItem( u"identifier"_s );
201 mIdentifier = mnl.toElement().text();
202
203 // set parent identifier
204 mnl = metadataElement.namedItem( u"parentidentifier"_s );
205 mParentIdentifier = mnl.toElement().text();
206
207 // set language
208 mnl = metadataElement.namedItem( u"language"_s );
209 mLanguage = mnl.toElement().text();
210
211 // set type
212 mnl = metadataElement.namedItem( u"type"_s );
213 mType = mnl.toElement().text();
214
215 // set title
216 mnl = metadataElement.namedItem( u"title"_s );
217 mTitle = mnl.toElement().text();
218
219 // set abstract
220 mnl = metadataElement.namedItem( u"abstract"_s );
221 mAbstract = mnl.toElement().text();
222
223 mType = context.projectTranslator()->translate( "metadata", mType );
224 mTitle = context.projectTranslator()->translate( "metadata", mTitle );
225 mAbstract = context.projectTranslator()->translate( "metadata", mAbstract );
226
227 // set keywords
228 const QDomNodeList keywords = metadataElement.elementsByTagName( u"keywords"_s );
229 mKeywords.clear();
230 for ( int i = 0; i < keywords.size(); i++ )
231 {
232 QStringList keywordsList;
233 mnl = keywords.at( i );
234 mne = mnl.toElement();
235
236 const QDomNodeList el = mne.elementsByTagName( u"keyword"_s );
237 for ( int j = 0; j < el.size(); j++ )
238 {
239 keywordsList.append( el.at( j ).toElement().text() );
240 }
241 addKeywords( mne.attribute( u"vocabulary"_s ), keywordsList );
242 }
243
244 // contact
245 const QDomNodeList contactsList = metadataElement.elementsByTagName( u"contact"_s );
246 mContacts.clear();
247 for ( int i = 0; i < contactsList.size(); i++ )
248 {
249 mnl = contactsList.at( i );
250 mne = mnl.toElement();
251
253 oneContact.name = mne.namedItem( u"name"_s ).toElement().text();
254 oneContact.organization = mne.namedItem( u"organization"_s ).toElement().text();
255 oneContact.position = mne.namedItem( u"position"_s ).toElement().text();
256 oneContact.voice = mne.namedItem( u"voice"_s ).toElement().text();
257 oneContact.fax = mne.namedItem( u"fax"_s ).toElement().text();
258 oneContact.email = mne.namedItem( u"email"_s ).toElement().text();
259 oneContact.role = mne.namedItem( u"role"_s ).toElement().text();
260
261 QList< QgsAbstractMetadataBase::Address > addresses;
262 const QDomNodeList addressList = mne.elementsByTagName( u"contactAddress"_s );
263 for ( int j = 0; j < addressList.size(); j++ )
264 {
265 const QDomElement addressElement = addressList.at( j ).toElement();
267 oneAddress.address = addressElement.namedItem( u"address"_s ).toElement().text();
268 oneAddress.administrativeArea = addressElement.namedItem( u"administrativearea"_s ).toElement().text();
269 oneAddress.city = addressElement.namedItem( u"city"_s ).toElement().text();
270 oneAddress.country = addressElement.namedItem( u"country"_s ).toElement().text();
271 oneAddress.postalCode = addressElement.namedItem( u"postalcode"_s ).toElement().text();
272 oneAddress.type = addressElement.namedItem( u"type"_s ).toElement().text();
273 addresses << oneAddress;
274 }
275 oneContact.addresses = addresses;
276 addContact( oneContact );
277 }
278
279 // links
280 mnl = metadataElement.namedItem( u"links"_s );
281 mne = mnl.toElement();
282 mLinks.clear();
283 const QDomNodeList el = mne.elementsByTagName( u"link"_s );
284 for ( int i = 0; i < el.size(); i++ )
285 {
286 mne = el.at( i ).toElement();
288 oneLink.name = mne.attribute( u"name"_s );
289 oneLink.type = mne.attribute( u"type"_s );
290 oneLink.url = mne.attribute( u"url"_s );
291 oneLink.description = mne.attribute( u"description"_s );
292 oneLink.format = mne.attribute( u"format"_s );
293 oneLink.mimeType = mne.attribute( u"mimeType"_s );
294 oneLink.size = mne.attribute( u"size"_s );
295 addLink( oneLink );
296 }
297
298 // history
299 const QDomNodeList historyNodeList = metadataElement.elementsByTagName( u"history"_s );
300 QStringList historyList;
301 for ( int i = 0; i < historyNodeList.size(); i++ )
302 {
303 mnl = historyNodeList.at( i );
304 mne = mnl.toElement();
305 historyList.append( mne.text() );
306 }
307 setHistory( historyList );
308
309 {
310 mDates.clear();
311 const QDomElement dateElement = metadataElement.firstChildElement( u"dates"_s );
312 if ( !dateElement.isNull() )
313 {
314 const QDomNodeList dateNodeList = dateElement.elementsByTagName( u"date"_s );
315 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
316 for ( int i = 0; i < dateNodeList.size(); i++ )
317 {
318 const QDomElement dateElement = dateNodeList.at( i ).toElement();
319 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.keyToValue( dateElement.attribute( u"type"_s ).toStdString().c_str() ) );
320 const QDateTime value = QDateTime::fromString( dateElement.attribute( u"value"_s ), Qt::ISODate );
321 if ( value.isValid() && !value.isNull() )
322 mDates.insert( type, value );
323 }
324 }
325 }
326
327 return true;
328}
329
330bool QgsAbstractMetadataBase::writeMetadataXml( QDomElement &metadataElement, QDomDocument &document, const QgsReadWriteContext & ) const
331{
332 // identifier
333 QDomElement identifier = document.createElement( u"identifier"_s );
334 const QDomText identifierText = document.createTextNode( mIdentifier );
335 identifier.appendChild( identifierText );
336 metadataElement.appendChild( identifier );
337
338 // parent identifier
339 QDomElement parentIdentifier = document.createElement( u"parentidentifier"_s );
340 const QDomText parentIdentifierText = document.createTextNode( mParentIdentifier );
341 parentIdentifier.appendChild( parentIdentifierText );
342 metadataElement.appendChild( parentIdentifier );
343
344 // language
345 QDomElement language = document.createElement( u"language"_s );
346 const QDomText languageText = document.createTextNode( mLanguage );
347 language.appendChild( languageText );
348 metadataElement.appendChild( language );
349
350 // type
351 QDomElement type = document.createElement( u"type"_s );
352 const QDomText typeText = document.createTextNode( mType );
353 type.appendChild( typeText );
354 metadataElement.appendChild( type );
355
356 // title
357 QDomElement title = document.createElement( u"title"_s );
358 const QDomText titleText = document.createTextNode( mTitle );
359 title.appendChild( titleText );
360 metadataElement.appendChild( title );
361
362 // abstract
363 QDomElement abstract = document.createElement( u"abstract"_s );
364 const QDomText abstractText = document.createTextNode( mAbstract );
365 abstract.appendChild( abstractText );
366 metadataElement.appendChild( abstract );
367
368 // keywords
369 QMapIterator<QString, QStringList> i( mKeywords );
370 while ( i.hasNext() )
371 {
372 i.next();
373 QDomElement keywordsElement = document.createElement( u"keywords"_s );
374 keywordsElement.setAttribute( u"vocabulary"_s, i.key() );
375 const QStringList values = i.value();
376 for ( const QString &kw : values )
377 {
378 QDomElement keyword = document.createElement( u"keyword"_s );
379 const QDomText keywordText = document.createTextNode( kw );
380 keyword.appendChild( keywordText );
381 keywordsElement.appendChild( keyword );
382 }
383 metadataElement.appendChild( keywordsElement );
384 }
385
386 // contact
387 for ( const QgsAbstractMetadataBase::Contact &contact : mContacts )
388 {
389 QDomElement contactElement = document.createElement( u"contact"_s );
390 QDomElement nameElement = document.createElement( u"name"_s );
391 QDomElement organizationElement = document.createElement( u"organization"_s );
392 QDomElement positionElement = document.createElement( u"position"_s );
393 QDomElement voiceElement = document.createElement( u"voice"_s );
394 QDomElement faxElement = document.createElement( u"fax"_s );
395 QDomElement emailElement = document.createElement( u"email"_s );
396 QDomElement roleElement = document.createElement( u"role"_s );
397
398 const QDomText nameText = document.createTextNode( contact.name );
399 const QDomText orgaText = document.createTextNode( contact.organization );
400 const QDomText positionText = document.createTextNode( contact.position );
401 const QDomText voiceText = document.createTextNode( contact.voice );
402 const QDomText faxText = document.createTextNode( contact.fax );
403 const QDomText emailText = document.createTextNode( contact.email );
404 const QDomText roleText = document.createTextNode( contact.role );
405
406 for ( const QgsAbstractMetadataBase::Address &oneAddress : contact.addresses )
407 {
408 QDomElement addressElement = document.createElement( u"contactAddress"_s );
409 QDomElement typeElement = document.createElement( u"type"_s );
410 QDomElement addressDetailedElement = document.createElement( u"address"_s );
411 QDomElement cityElement = document.createElement( u"city"_s );
412 QDomElement administrativeAreaElement = document.createElement( u"administrativearea"_s );
413 QDomElement postalCodeElement = document.createElement( u"postalcode"_s );
414 QDomElement countryElement = document.createElement( u"country"_s );
415
416 typeElement.appendChild( document.createTextNode( oneAddress.type ) );
417 addressDetailedElement.appendChild( document.createTextNode( oneAddress.address ) );
418 cityElement.appendChild( document.createTextNode( oneAddress.city ) );
419 administrativeAreaElement.appendChild( document.createTextNode( oneAddress.administrativeArea ) );
420 postalCodeElement.appendChild( document.createTextNode( oneAddress.postalCode ) );
421 countryElement.appendChild( document.createTextNode( oneAddress.country ) );
422
423 addressElement.appendChild( typeElement );
424 addressElement.appendChild( addressDetailedElement );
425 addressElement.appendChild( cityElement );
426 addressElement.appendChild( administrativeAreaElement );
427 addressElement.appendChild( postalCodeElement );
428 addressElement.appendChild( countryElement );
429 contactElement.appendChild( addressElement );
430 }
431
432 nameElement.appendChild( nameText );
433 organizationElement.appendChild( orgaText );
434 positionElement.appendChild( positionText );
435 voiceElement.appendChild( voiceText );
436 faxElement.appendChild( faxText );
437 emailElement.appendChild( emailText );
438 roleElement.appendChild( roleText );
439
440 contactElement.appendChild( nameElement );
441 contactElement.appendChild( organizationElement );
442 contactElement.appendChild( positionElement );
443 contactElement.appendChild( voiceElement );
444 contactElement.appendChild( faxElement );
445 contactElement.appendChild( emailElement );
446 contactElement.appendChild( roleElement );
447 metadataElement.appendChild( contactElement );
448 }
449
450 // links
451 QDomElement links = document.createElement( u"links"_s );
452 for ( const QgsAbstractMetadataBase::Link &link : mLinks )
453 {
454 QDomElement linkElement = document.createElement( u"link"_s );
455 linkElement.setAttribute( u"name"_s, link.name );
456 linkElement.setAttribute( u"type"_s, link.type );
457 linkElement.setAttribute( u"url"_s, link.url );
458 linkElement.setAttribute( u"description"_s, link.description );
459 linkElement.setAttribute( u"format"_s, link.format );
460 linkElement.setAttribute( u"mimeType"_s, link.mimeType );
461 linkElement.setAttribute( u"size"_s, link.size );
462 links.appendChild( linkElement );
463 }
464 metadataElement.appendChild( links );
465
466 // history
467 for ( const QString &history : mHistory )
468 {
469 QDomElement historyElement = document.createElement( u"history"_s );
470 const QDomText historyText = document.createTextNode( history );
471 historyElement.appendChild( historyText );
472 metadataElement.appendChild( historyElement );
473 }
474
475 // dates
476 {
477 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
478 QDomElement datesElement = document.createElement( u"dates"_s );
479 for ( int k = 0; k < dateEnum.keyCount(); k++ )
480 {
481 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.value( k ) );
482 if ( mDates.contains( type ) && mDates.value( type ).isValid() )
483 {
484 QDomElement dateElement = document.createElement( u"date"_s );
485 dateElement.setAttribute( u"type"_s, dateEnum.valueToKey( static_cast< int >( type ) ) );
486 dateElement.setAttribute( u"value"_s, mDates.value( type ).toString( Qt::ISODate ) );
487 datesElement.appendChild( dateElement );
488 }
489 }
490 metadataElement.appendChild( datesElement );
491 }
492
493 return true;
494}
495
497{
498 if ( !mTitle.isEmpty() )
499 {
500 translationContext->registerTranslation( u"metadata"_s, mTitle );
501 }
502 if ( !mType.isEmpty() )
503 {
504 translationContext->registerTranslation( u"metadata"_s, mType );
505 }
506 if ( !mAbstract.isEmpty() )
507 {
508 translationContext->registerTranslation( u"metadata"_s, mAbstract );
509 }
510}
511
513{
514 if ( !other )
515 return;
516
517 if ( !other->identifier().isEmpty() )
518 mIdentifier = other->identifier();
519
520 if ( !other->parentIdentifier().isEmpty() )
522
523 if ( !other->language().isEmpty() )
524 mLanguage = other->language();
525
526 if ( !other->type().isEmpty() )
527 mType = other->type();
528
529 if ( !other->title().isEmpty() )
530 mTitle = other->title();
531
532 if ( !other->abstract().isEmpty() )
533 mAbstract = other->abstract();
534
535 if ( !other->history().isEmpty() )
536 mHistory = other->history();
537
538 if ( !other->keywords().isEmpty() )
539 mKeywords = other->keywords();
540
541 if ( !other->contacts().isEmpty() )
542 mContacts = other->contacts();
543
544 if ( !other->links().isEmpty() )
545 mLinks = other->links();
546
547 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
548 for ( int k = 0; k < dateEnum.keyCount(); k++ )
549 {
550 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.value( k ) );
551 if ( other->mDates.contains( type ) && other->mDates.value( type ).isValid() )
552 {
553 mDates.insert( type, other->mDates[type] );
554 }
555 }
556}
557
559{
560 return (
561 ( mIdentifier == metadataOther.mIdentifier )
562 && ( mParentIdentifier == metadataOther.mParentIdentifier )
563 && ( mLanguage == metadataOther.mLanguage )
564 && ( mType == metadataOther.mType )
565 && ( mTitle == metadataOther.mTitle )
566 && ( mAbstract == metadataOther.mAbstract )
567 && ( mHistory == metadataOther.mHistory )
568 && ( mKeywords == metadataOther.mKeywords )
569 && ( mContacts == metadataOther.mContacts )
570 && ( mLinks == metadataOther.mLinks )
571 && ( mDates == metadataOther.mDates )
572 );
573}
574
575
577{
578 return name == other.name
579 && organization == other.organization
580 && position == other.position
581 && addresses == other.addresses
582 && voice == other.voice
583 && fax == other.fax
584 && email == other.email
585 && role == other.role;
586}
587
589{
590 return name == other.name && type == other.type && description == other.description && url == other.url && format == other.format && mimeType == other.mimeType && size == other.size;
591}
592
594{
595 return type == other.type && address == other.address && city == other.city && administrativeArea == other.administrativeArea && postalCode == other.postalCode && country == other.country;
596}
MetadataDateType
Date types for metadata.
Definition qgis.h:5062
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.
QMap< Qgis::MetadataDateType, QDateTime > mDates
Metadata dates.
QStringList keywordVocabularies() const
Returns a list of keyword vocabularies contained in the metadata.
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 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