QGIS API Documentation 4.5.0-Master (3b5d115045b)
Loading...
Searching...
No Matches
qgsprojectproperty.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsproject.cpp - description
3 -------------------
4 begin : February 24, 2005
5 copyright : (C) 2005 by Mark Coletti
6 email : mcoletti at gmail.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
18#include "qgsprojectproperty.h"
19
20#include "qgis.h"
21#include "qgslogger.h"
22#include "qgsmessagelog.h"
23
24#include <QDomDocument>
25#include <QString>
26#include <QStringList>
27
28using namespace Qt::StringLiterals;
29
32
33void QgsProjectPropertyValue::dump( int tabs ) const
34{
35 Q_UNUSED( tabs )
36#ifdef QGISDEBUG
37
38 QString tabString;
39 tabString.fill( '\t', tabs );
40
41 if ( QMetaType::Type::QStringList == mValue.userType() )
42 {
43 const QStringList sl = mValue.toStringList();
44
45 for ( const auto &string : sl )
46 {
47 QgsDebugMsgLevel( u"%1[%2] "_s.arg( tabString, string ), 4 );
48 }
49 }
50 else
51 {
52 QgsDebugMsgLevel( u"%1%2"_s.arg( tabString, mValue.toString() ), 4 );
53 }
54#endif
55}
56
57bool QgsProjectPropertyValue::readXml( const QDomNode &keyNode )
58{
59 // this *should* be a Dom element node
60 QDomElement subkeyElement = keyNode.toElement();
61
62 // get the type so that we can properly parse the key value
63 QString typeString = subkeyElement.attribute( u"type"_s );
64
65 if ( typeString.isNull() )
66 {
67 QgsDebugError( u"null ``type'' attribute for %1"_s.arg( keyNode.nodeName() ) );
68
69 return false;
70 }
71
72 // the values come in as strings; we need to restore them to their
73 // original values *and* types
74 mValue.clear();
75
76 // get the type associated with the value first
77 QMetaType::Type type = static_cast<QMetaType::Type>( QMetaType::fromName( typeString.toLocal8Bit().constData() ).id() );
78
79 // This huge switch is left-over from an earlier incarnation of
80 // QgsProject where there was a fine level of granularity for value
81 // types. The current interface, borrowed from QgsSettings, supports a
82 // very small sub-set of these types. However, I've left all the
83 // other types just in case the interface is expanded to include these
84 // other types.
85
86 switch ( type )
87 {
88 case QMetaType::Type::UnknownType:
89 QgsDebugError( u"invalid value type %1 .. "_s.arg( typeString ) );
90 return false;
91
92 case QMetaType::Type::QVariantMap:
93 QgsDebugError( u"no support for QVariant::Map"_s );
94 return false;
95
96 case QMetaType::Type::QVariantList:
97 QgsDebugError( u"no support for QVariant::List"_s );
98 return false;
99
100 case QMetaType::Type::QString:
101 mValue = subkeyElement.text(); // no translating necessary
102 break;
103
104 case QMetaType::Type::QStringList:
105 {
106 int i = 0;
107 QDomNodeList values = keyNode.childNodes();
108
109 // all the QStringList values will be inside <value> elements
110 QStringList valueStringList;
111
112 while ( i < values.count() )
113 {
114 if ( "value" == values.item( i ).nodeName() )
115 {
116 // <value>s have only one element, which contains actual string value
117 valueStringList.append( values.item( i ).firstChild().nodeValue() );
118 }
119 else
120 {
121 QgsDebugError( u"non <value> element ``%1'' in string list"_s.arg( values.item( i ).nodeName() ) );
122 }
123
124 ++i;
125 }
126
127 mValue = valueStringList;
128 break;
129 }
130
131 case QMetaType::Type::QFont:
132 QgsDebugError( u"no support for QVariant::Font"_s );
133 return false;
134
135 case QMetaType::Type::QPixmap:
136 QgsDebugError( u"no support for QVariant::Pixmap"_s );
137 return false;
138
139 case QMetaType::Type::QBrush:
140 QgsDebugError( u"no support for QVariant::Brush"_s );
141 return false;
142
143 case QMetaType::Type::QRect:
144 QgsDebugError( u"no support for QVariant::Rect"_s );
145 return false;
146
147 case QMetaType::Type::QSize:
148 QgsDebugError( u"no support for QVariant::Size"_s );
149 return false;
150
151 case QMetaType::Type::QColor:
152 QgsDebugError( u"no support for QVariant::Color"_s );
153 return false;
154
155 case QMetaType::Type::QPalette:
156 QgsDebugError( u"no support for QVariant::Palette"_s );
157 return false;
158
159 case QMetaType::Type::QPoint:
160 QgsDebugError( u"no support for QVariant::Point"_s );
161 return false;
162
163 case QMetaType::Type::QImage:
164 QgsDebugError( u"no support for QVariant::Image"_s );
165 return false;
166
167 case QMetaType::Type::Int:
168 mValue = QVariant( subkeyElement.text() ).toInt();
169 break;
170
171 case QMetaType::Type::UInt:
172 mValue = QVariant( subkeyElement.text() ).toUInt();
173 break;
174
175 case QMetaType::Type::Bool:
176 mValue = QVariant( subkeyElement.text() ).toBool();
177 break;
178
179 case QMetaType::Type::Double:
180 mValue = QVariant( subkeyElement.text() ).toDouble();
181 break;
182
183 case QMetaType::Type::QByteArray:
184 mValue = QVariant( subkeyElement.text() ).toByteArray();
185 break;
186
187 case QMetaType::Type::QPolygon:
188 QgsDebugError( u"no support for QVariant::Polygon"_s );
189 return false;
190
191 case QMetaType::Type::QRegion:
192 QgsDebugError( u"no support for QVariant::Region"_s );
193 return false;
194
195 case QMetaType::Type::QBitmap:
196 QgsDebugError( u"no support for QVariant::Bitmap"_s );
197 return false;
198
199 case QMetaType::Type::QCursor:
200 QgsDebugError( u"no support for QVariant::Cursor"_s );
201 return false;
202
203 case QMetaType::Type::QBitArray:
204 QgsDebugError( u"no support for QVariant::BitArray"_s );
205 return false;
206
207 case QMetaType::Type::QKeySequence:
208 QgsDebugError( u"no support for QVariant::KeySequence"_s );
209 return false;
210
211 case QMetaType::Type::QPen:
212 QgsDebugError( u"no support for QVariant::Pen"_s );
213 return false;
214
215#if 0 // Currently unsupported variant types
216 case QVariant::LongLong :
217 value_ = QVariant( subkeyElement.text() ).toLongLong();
218 break;
219
220 case QVariant::ULongLong :
221 value_ = QVariant( subkeyElement.text() ).toULongLong();
222 break;
223#endif
224 default:
225 QgsDebugError( u"unsupported value type %1 .. not properly translated to QVariant"_s.arg( typeString ) );
226 }
227
228 return true;
229}
230
231
232// keyElement is created by parent QgsProjectPropertyKey
233bool QgsProjectPropertyValue::writeXml( QString const &nodeName, QDomElement &keyElement, QDomDocument &document )
234{
235 {
236 QDomElement valueElement = document.createElement( u"properties"_s );
237
238 // remember the type so that we can rebuild it when the project is read in
239 valueElement.setAttribute( u"name"_s, nodeName );
240 valueElement.setAttribute( u"type"_s, mValue.typeName() );
241
242 // we handle string lists differently from other types in that we
243 // create a sequence of repeated elements to cover all the string list
244 // members; each value will be in a <value></value> tag.
245 // XXX Not the most elegant way to handle string lists?
246 if ( QMetaType::Type::QStringList == mValue.userType() )
247 {
248 QStringList sl = mValue.toStringList();
249
250 for ( QStringList::iterator i = sl.begin(); i != sl.end(); ++i )
251 {
252 QDomElement stringListElement = document.createElement( u"value"_s );
253 QDomText valueText = document.createTextNode( *i );
254 stringListElement.appendChild( valueText );
255
256 valueElement.appendChild( stringListElement );
257 }
258 }
259 else // we just plop the value in as plain ole text
260 {
261 QDomText valueText = document.createTextNode( mValue.toString() );
262 valueElement.appendChild( valueText );
263 }
264
265 keyElement.appendChild( valueElement );
266 }
267
268 {
269 // retain compatibility with QGIS 3 for the time being
270 // consider dropping after the second LTR of the 4.x cycle is out
271 QDomElement valueElement = document.createElement( nodeName );
272
273 // remember the type so that we can rebuild it when the project is read in
274 valueElement.setAttribute( u"type"_s, mValue.typeName() );
275
276 // we handle string lists differently from other types in that we
277 // create a sequence of repeated elements to cover all the string list
278 // members; each value will be in a <value></value> tag.
279 // XXX Not the most elegant way to handle string lists?
280 if ( QMetaType::Type::QStringList == mValue.userType() )
281 {
282 QStringList sl = mValue.toStringList();
283
284 for ( QStringList::iterator i = sl.begin(); i != sl.end(); ++i )
285 {
286 QDomElement stringListElement = document.createElement( u"value"_s );
287 QDomText valueText = document.createTextNode( *i );
288 stringListElement.appendChild( valueText );
289
290 valueElement.appendChild( stringListElement );
291 }
292 }
293 else
294 {
295 // we just plop the value in as plain ole text
296 QDomText valueText = document.createTextNode( mValue.toString() );
297 valueElement.appendChild( valueText );
298 }
299
300 keyElement.appendChild( valueElement );
301 }
302
303 return true;
304}
305
306
308 : mName( name )
309{}
310
315
317{
318 QgsProjectProperty *foundQgsProperty = mProperties.value( name() );
319
320 if ( !foundQgsProperty )
321 {
322 QgsDebugError( u"key has null child"_s );
323 return QVariant(); // just return an QVariant::Invalid
324 }
325
326 return foundQgsProperty->value();
327}
328
329
330void QgsProjectPropertyKey::dump( int tabs ) const
331{
332 QString tabString;
333
334 tabString.fill( '\t', tabs );
335
336 QgsDebugMsgLevel( u"%1name: %2"_s.arg( tabString, name() ), 4 );
337
338 tabs++;
339 tabString.fill( '\t', tabs );
340
341 if ( !mProperties.isEmpty() )
342 {
343 QHashIterator< QString, QgsProjectProperty * > i( mProperties );
344 while ( i.hasNext() )
345 {
346 if ( i.next().value()->isValue() )
347 {
348 QgsProjectPropertyValue *propertyValue = static_cast<QgsProjectPropertyValue *>( i.value() );
349
350 if ( QMetaType::Type::QStringList == propertyValue->value().userType() )
351 {
352 QgsDebugMsgLevel( u"%1key: <%2> value:"_s.arg( tabString, i.key() ), 4 );
353 propertyValue->dump( tabs + 1 );
354 }
355 else
356 {
357 QgsDebugMsgLevel( u"%1key: <%2> value: %3"_s.arg( tabString, i.key(), propertyValue->value().toString() ), 4 );
358 }
359 }
360 else
361 {
362 QgsDebugMsgLevel( u"%1key: <%2> subkey: <%3>"_s.arg( tabString, i.key(), static_cast<QgsProjectPropertyKey *>( i.value() )->name() ), 4 );
363 i.value()->dump( tabs + 1 );
364 }
365
366#if 0
367 qDebug( "<%s>", name().toUtf8().constData() );
368 if ( i.value()->isValue() )
369 {
370 qDebug( " <%s>", i.key().toUtf8().constData() );
371 }
372 i.value()->dump();
373 if ( i.value()->isValue() )
374 {
375 qDebug( " </%s>", i.key().toUtf8().constData() );
376 }
377 qDebug( "</%s>", name().toUtf8().constData() );
378#endif
379 }
380 }
381}
382
383
384bool QgsProjectPropertyKey::readXml( const QDomNode &keyNode )
385{
386 QDomNodeList subkeys = keyNode.childNodes();
387 bool hasModernProperties = false;
388 for ( int i = 0; i < subkeys.count(); i++ )
389 {
390 const QDomNode subkey = subkeys.item( i );
391 if ( subkey.nodeName() == "properties"_L1
392 && subkey.hasAttributes()
393 && // if we have attributes
394 subkey.isElement()
395 && // and we're an element
396 subkey.toElement().hasAttribute( u"name"_s ) ) // and we have a "name" attribute
397 {
398 hasModernProperties = true;
399 }
400 }
401
402 for ( int i = 0; i < subkeys.count(); i++ )
403 {
404 const QDomNode subkey = subkeys.item( i );
405
406 QString name;
407 if ( subkey.nodeName() == "properties"_L1
408 && subkey.hasAttributes()
409 && // if we have attributes
410 subkey.isElement()
411 && // and we're an element
412 subkey.toElement().hasAttribute( u"name"_s ) ) // and we have a "name" attribute
413 {
414 name = subkey.toElement().attribute( u"name"_s );
415 }
416 else
417 {
418 if ( hasModernProperties )
419 {
420 continue;
421 }
422 name = subkey.nodeName();
423 }
424
425 // if the current node is an element that has a "type" attribute,
426 // then we know it's a leaf node; i.e., a subkey _value_, and not
427 // a subkey
428 if ( subkey.hasAttributes()
429 && // if we have attributes
430 subkey.isElement()
431 && // and we're an element
432 subkey.toElement().hasAttribute( u"type"_s ) ) // and we have a "type" attribute
433 {
434 // then we're a key value
435 delete mProperties.take( name );
436 mProperties.insert( name, new QgsProjectPropertyValue );
437
438 if ( !mProperties[name]->readXml( subkey ) )
439 {
440 QgsDebugError( u"unable to parse key value %1"_s.arg( name ) );
441 }
442 }
443 else // otherwise it's a subkey, so just recurse on down the remaining keys
444 {
445 addKey( name );
446
447 if ( !mProperties[name]->readXml( subkey ) )
448 {
449 QgsDebugError( u"unable to parse subkey %1"_s.arg( name ) );
450 }
451 }
452 }
453
454 return true;
455}
456
457
458/*
459 Property keys will always create a Dom element for itself and then
460 recursively call writeXml for any constituent properties.
461*/
462bool QgsProjectPropertyKey::writeXml( QString const &nodeName, QDomElement &element, QDomDocument &document )
463{
464 // If it's an _empty_ node (i.e., one with no properties) we need to emit
465 // an empty place holder; else create new Dom elements as necessary.
466
467 {
468 QDomElement keyElement = document.createElement( "properties" ); // Dom element for this property key
469 keyElement.toElement().setAttribute( u"name"_s, nodeName );
470
471 if ( !mProperties.isEmpty() )
472 {
473 auto keys = mProperties.keys();
474 std::sort( keys.begin(), keys.end() );
475
476 for ( const auto &key : std::as_const( keys ) )
477 {
478 if ( !mProperties.value( key )->writeXml( key, keyElement, document ) )
479 QgsMessageLog::logMessage( tr( "Failed to save project property %1" ).arg( key ) );
480 }
481 }
482
483 element.appendChild( keyElement );
484 }
485
486 {
487 // retain compatibility with QGIS 3 for the time being
488 const bool isRootProperties = element == document.documentElement();
489 QDomElement keyElement;
490 if ( isRootProperties )
491 {
492 keyElement = document.documentElement().firstChildElement( u"properties"_s );
493 }
494 else
495 {
496 keyElement = document.createElement( nodeName ); // Dom element for this property key
497 }
498
499 if ( !mProperties.isEmpty() )
500 {
501 auto keys = mProperties.keys();
502 std::sort( keys.begin(), keys.end() );
503
504 for ( const auto &key : std::as_const( keys ) )
505 {
506 if ( !mProperties.value( key )->writeXml( key, keyElement, document ) )
507 QgsMessageLog::logMessage( tr( "Failed to save project property %1" ).arg( key ) );
508 }
509 }
510
511 if ( !isRootProperties )
512 {
513 element.appendChild( keyElement );
514 }
515 }
516
517 return true;
518}
519
520void QgsProjectPropertyKey::entryList( QStringList &entries ) const
521{
522 // now add any leaf nodes to the entries list
523 QHashIterator< QString, QgsProjectProperty * > i( mProperties );
524 while ( i.hasNext() )
525 {
526 // add any of the nodes that have just a single value
527 if ( i.next().value()->isLeaf() )
528 {
529 entries.append( i.key() );
530 }
531 }
532}
533
534void QgsProjectPropertyKey::subkeyList( QStringList &entries ) const
535{
536 // now add any leaf nodes to the entries list
537 QHashIterator< QString, QgsProjectProperty * > i( mProperties );
538 while ( i.hasNext() )
539 {
540 // add any of the nodes that have just a single value
541 if ( !i.next().value()->isLeaf() )
542 {
543 entries.append( i.key() );
544 }
545 }
546}
547
548
550{
551 if ( 0 == count() )
552 {
553 return true;
554 }
555 else if ( 1 == count() )
556 {
557 QHashIterator< QString, QgsProjectProperty * > i( mProperties );
558
559 if ( i.hasNext() && i.next().value()->isValue() )
560 {
561 return true;
562 }
563 }
564
565 return false;
566}
567
569{
570 mName = name;
571}
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
bool isLeaf() const override
Returns true if property is a leaf node.
QString name() const
The name of the property is used as identifier.
QgsProjectPropertyKey(const QString &name=QString())
Create a new QgsProjectPropertyKey with the specified identifier.
void dump(int tabs=0) const override
Dumps out the keys and values.
virtual void clearKeys()
Deletes any sub-nodes from the property.
bool writeXml(const QString &nodeName, QDomElement &element, QDomDocument &document) override
Writes the property hierarchy to a specified DOM element.
void subkeyList(QStringList &entries) const
Returns any sub-keys contained by this property which themselves contain other keys.
void setName(const QString &name)
The name of the property is used as identifier.
QgsProjectPropertyKey * addKey(const QString &keyName)
Adds the specified property key as a sub-key.
QVariant value() const override
If this key has a value, it will be stored by its name in its properties.
void entryList(QStringList &entries) const
Returns any sub-keys contained by this property that do not contain other keys.
int count() const
Returns the number of sub-keys contained by this property.
bool readXml(const QDomNode &keyNode) override
Restores the property hierarchy from a specified DOM node.
Project property value node, contains a QgsProjectPropertyKey's value.
bool writeXml(const QString &nodeName, QDomElement &element, QDomDocument &document) override
Writes the property hierarchy to a specified DOM element.
QVariant value() const override
Returns the node's value.
void dump(int tabs=0) const override
Dumps out the keys and values.
bool readXml(const QDomNode &keyNode) override
Restores the property hierarchy from a specified DOM node.
virtual QVariant value() const =0
Returns the node's value.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:80
#define QgsDebugError(str)
Definition qgslogger.h:71