QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgsmaplayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayer.cpp - description
3 -------------------
4 begin : Fri Jun 28 2002
5 copyright : (C) 2002 by Gary E.Sherman
6 email : sherman at mrcc.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
19#include "qgssqliteutils.h"
22#include "qgsapplication.h"
25#include "qgsdatasourceuri.h"
26#include "qgsfileutils.h"
27#include "qgslogger.h"
28#include "qgsauthmanager.h"
29#include "qgsmaplayer.h"
30#include "qgsmaplayerlegend.h"
32#include "qgspathresolver.h"
34#include "qgsproject.h"
35#include "qgsproviderregistry.h"
36#include "qgsrasterlayer.h"
37#include "qgsreadwritecontext.h"
38#include "qgsrectangle.h"
39#include "qgssldexportcontext.h"
40#include "qgsvectorlayer.h"
41#include "qgsxmlutils.h"
42#include "qgsstringutils.h"
43#include "qgsmessagelog.h"
46#include "qgsprovidermetadata.h"
47#include "qgslayernotesutils.h"
48#include "qgsdatums.h"
49#include "qgsprojoperation.h"
50#include "qgsthreadingutils.h"
51#include "qgsunittypes.h"
52
53#include <QDir>
54#include <QDomDocument>
55#include <QDomElement>
56#include <QDomImplementation>
57#include <QDomNode>
58#include <QFile>
59#include <QFileInfo>
60#include <QLocale>
61#include <QTextStream>
62#include <QUrl>
63#include <QTimer>
64#include <QStandardPaths>
65#include <QUuid>
66#include <QRegularExpression>
67
68#include <sqlite3.h>
69
71{
72 switch ( type )
73 {
74 case Metadata:
75 return QStringLiteral( ".qmd" );
76
77 case Style:
78 return QStringLiteral( ".qml" );
79 }
80 return QString();
81}
82
84 const QString &lyrname,
85 const QString &source )
86 : mDataSource( source )
87 , mLayerName( lyrname )
88 , mLayerType( type )
89 , mServerProperties( std::make_unique<QgsMapLayerServerProperties>( this ) )
90 , mUndoStack( new QUndoStack( this ) )
91 , mUndoStackStyles( new QUndoStack( this ) )
92 , mStyleManager( new QgsMapLayerStyleManager( this ) )
93 , mRefreshTimer( new QTimer( this ) )
94{
95 mID = generateId( lyrname );
98 connect( mRefreshTimer, &QTimer::timeout, this, [ = ]
99 {
100
101 switch ( mAutoRefreshMode )
102 {
104 break;
106 triggerRepaint( true );
107 break;
109 reload();
110 break;
111 }
112 } );
113}
114
116{
117 if ( project() && project()->pathResolver().writePath( mDataSource ).startsWith( "attachment:" ) )
118 {
120 }
121
122 delete m3DRenderer;
123 delete mLegend;
124 delete mStyleManager;
125}
126
127void QgsMapLayer::clone( QgsMapLayer *layer ) const
128{
130
131 layer->setBlendMode( blendMode() );
132
133 const auto constStyles = styleManager()->styles();
134 for ( const QString &s : constStyles )
135 {
136 layer->styleManager()->addStyle( s, styleManager()->style( s ) );
137 }
138
139 layer->setName( name() );
140 layer->setShortName( shortName() );
141 layer->setExtent( extent() );
142 layer->setMaximumScale( maximumScale() );
143 layer->setMinimumScale( minimumScale() );
145 layer->setTitle( title() );
146 layer->setAbstract( abstract() );
147 layer->setKeywordList( keywordList() );
148 layer->setDataUrl( dataUrl() );
150 layer->setAttribution( attribution() );
152 layer->setLegendUrl( legendUrl() );
154 layer->setDependencies( dependencies() );
156 layer->setCrs( crs() );
157 layer->setCustomProperties( mCustomProperties );
158 layer->setOpacity( mLayerOpacity );
159 layer->setMetadata( mMetadata );
160 layer->serverProperties()->copyTo( mServerProperties.get() );
161}
162
164{
165 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
167
168 return mLayerType;
169}
170
171QgsMapLayer::LayerFlags QgsMapLayer::flags() const
172{
174
175 return mFlags;
176}
177
178void QgsMapLayer::setFlags( QgsMapLayer::LayerFlags flags )
179{
181
182 if ( flags == mFlags )
183 return;
184
185 mFlags = flags;
186 emit flagsChanged();
187}
188
189Qgis::MapLayerProperties QgsMapLayer::properties() const
190{
192
193 return Qgis::MapLayerProperties();
194}
195
196QString QgsMapLayer::id() const
197{
198 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
200
201 return mID;
202}
203
204void QgsMapLayer::setName( const QString &name )
205{
207
208 if ( name == mLayerName )
209 return;
210
212
213 emit nameChanged();
214}
215
216QString QgsMapLayer::name() const
217{
218 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
220
221 QgsDebugMsgLevel( "returning name '" + mLayerName + '\'', 4 );
222 return mLayerName;
223}
224
231
233{
235
236 return nullptr;
237}
238
240{
242
243 return mShortName;
244}
245
246void QgsMapLayer::setMetadataUrl( const QString &metaUrl )
247{
249
250 QList<QgsMapLayerServerProperties::MetadataUrl> urls = serverProperties()->metadataUrls();
251 if ( urls.isEmpty() )
252 {
253 const QgsMapLayerServerProperties::MetadataUrl newItem = QgsMapLayerServerProperties::MetadataUrl( metaUrl, QLatin1String(), QLatin1String() );
254 urls.prepend( newItem );
255 }
256 else
257 {
258 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
259 const QgsMapLayerServerProperties::MetadataUrl newItem( metaUrl, old.type, old.format );
260 urls.prepend( newItem );
261 }
263}
264
266{
268
269 if ( mServerProperties->metadataUrls().isEmpty() )
270 {
271 return QLatin1String();
272 }
273 else
274 {
275 return mServerProperties->metadataUrls().first().url;
276 }
277}
278
279void QgsMapLayer::setMetadataUrlType( const QString &metaUrlType )
280{
282
283 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
284 if ( urls.isEmpty() )
285 {
286 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), metaUrlType, QLatin1String() );
287 urls.prepend( newItem );
288 }
289 else
290 {
291 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
292 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, metaUrlType, old.format );
293 urls.prepend( newItem );
294 }
295 mServerProperties->setMetadataUrls( urls );
296}
297
299{
301
302 if ( mServerProperties->metadataUrls().isEmpty() )
303 {
304 return QLatin1String();
305 }
306 else
307 {
308 return mServerProperties->metadataUrls().first().type;
309 }
310}
311
312void QgsMapLayer::setMetadataUrlFormat( const QString &metaUrlFormat )
313{
315
316 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
317 if ( urls.isEmpty() )
318 {
319 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), QLatin1String(), metaUrlFormat );
320 urls.prepend( newItem );
321 }
322 else
323 {
324 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst( );
325 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, old.type, metaUrlFormat );
326 urls.prepend( newItem );
327 }
328 mServerProperties->setMetadataUrls( urls );
329}
330
332{
334
335 if ( mServerProperties->metadataUrls().isEmpty() )
336 {
337 return QString();
338 }
339 else
340 {
341 return mServerProperties->metadataUrls().first().format;
342 }
343}
344
345QString QgsMapLayer::publicSource( bool hidePassword ) const
346{
348
349 // Redo this every time we're asked for it, as we don't know if
350 // dataSource has changed.
351 QString safeName = QgsDataSourceUri::removePassword( mDataSource, hidePassword );
352 return safeName;
353}
354
355QString QgsMapLayer::source() const
356{
358
359 return mDataSource;
360}
361
363{
365
366 return mExtent;
367}
368
369void QgsMapLayer::setBlendMode( const QPainter::CompositionMode blendMode )
370{
372
373 if ( mBlendMode == blendMode )
374 return;
375
376 mBlendMode = blendMode;
379}
380
381QPainter::CompositionMode QgsMapLayer::blendMode() const
382{
383 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
385
386 return mBlendMode;
387}
388
389void QgsMapLayer::setOpacity( double opacity )
390{
392
394 return;
396 emit opacityChanged( opacity );
398}
399
401{
402 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
404
405 return mLayerOpacity;
406}
407
408bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags, QgsDataProvider *preloadedProvider )
409{
411
412 mPreloadedProvider.reset( preloadedProvider );
413
414 bool layerError;
416
417 QDomNode mnl;
418 QDomElement mne;
419
420 // read provider
421 QString provider;
422 mnl = layerElement.namedItem( QStringLiteral( "provider" ) );
423 mne = mnl.toElement();
424 provider = mne.text();
425
426 // set data source
427 mnl = layerElement.namedItem( QStringLiteral( "datasource" ) );
428 mne = mnl.toElement();
429 mDataSource = context.pathResolver().readPath( mne.text() );
430
431 // if the layer needs authentication, ensure the master password is set
432 const thread_local QRegularExpression rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
433 if ( rx.match( mDataSource ).hasMatch()
435 {
436 return false;
437 }
438
439 mDataSource = decodedSource( mDataSource, provider, context );
440
441 // Set the CRS from project file, asking the user if necessary.
442 // Make it the saved CRS to have WMS layer projected correctly.
443 // We will still overwrite whatever GDAL etc picks up anyway
444 // further down this function.
445 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
446 mne = mnl.toElement();
447
449 CUSTOM_CRS_VALIDATION savedValidation;
450
451 const QDomNode srsNode = layerElement.namedItem( QStringLiteral( "srs" ) );
452 mCRS.readXml( srsNode );
453 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) );
455 mCRS.validate();
456 savedCRS = mCRS;
457
458 // Do not validate any projections in children, they will be overwritten anyway.
459 // No need to ask the user for a projections when it is overwritten, is there?
462
463 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );
464
465 // the internal name is just the data source basename
466 //QFileInfo dataSourceFileInfo( mDataSource );
467 //internalName = dataSourceFileInfo.baseName();
468
469 // set ID
470 mnl = layerElement.namedItem( QStringLiteral( "id" ) );
471 if ( ! mnl.isNull() )
472 {
473 mne = mnl.toElement();
474 if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz)
475 {
476 mID = mne.text();
477 }
478 }
479
480 // set name
481 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
482 mne = mnl.toElement();
483
484 //name can be translated
485 setName( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
486
487 // now let the children grab what they need from the Dom node.
488 layerError = !readXml( layerElement, context );
489
490 // overwrite CRS with what we read from project file before the raster/vector
491 // file reading functions changed it. They will if projections is specified in the file.
492 // FIXME: is this necessary? Yes, it is (autumn 2019)
494 mCRS = savedCRS;
495
496 //short name
497 const QDomElement shortNameElem = layerElement.firstChildElement( QStringLiteral( "shortname" ) );
498 if ( !shortNameElem.isNull() )
499 {
500 mShortName = shortNameElem.text();
501 }
502
503 //title
504 const QDomElement titleElem = layerElement.firstChildElement( QStringLiteral( "title" ) );
505 if ( !titleElem.isNull() )
506 {
507 mTitle = titleElem.text();
508 }
509
510 //abstract
511 const QDomElement abstractElem = layerElement.firstChildElement( QStringLiteral( "abstract" ) );
512 if ( !abstractElem.isNull() )
513 {
514 mAbstract = abstractElem.text();
515 }
516
517 //keywordList
518 const QDomElement keywordListElem = layerElement.firstChildElement( QStringLiteral( "keywordList" ) );
519 if ( !keywordListElem.isNull() )
520 {
521 QStringList kwdList;
522 for ( QDomNode n = keywordListElem.firstChild(); !n.isNull(); n = n.nextSibling() )
523 {
524 kwdList << n.toElement().text();
525 }
526 mKeywordList = kwdList.join( QLatin1String( ", " ) );
527 }
528
529 //dataUrl
530 const QDomElement dataUrlElem = layerElement.firstChildElement( QStringLiteral( "dataUrl" ) );
531 if ( !dataUrlElem.isNull() )
532 {
533 mDataUrl = dataUrlElem.text();
534 mDataUrlFormat = dataUrlElem.attribute( QStringLiteral( "format" ), QString() );
535 }
536
537 //legendUrl
538 const QDomElement legendUrlElem = layerElement.firstChildElement( QStringLiteral( "legendUrl" ) );
539 if ( !legendUrlElem.isNull() )
540 {
541 mLegendUrl = legendUrlElem.text();
542 mLegendUrlFormat = legendUrlElem.attribute( QStringLiteral( "format" ), QString() );
543 }
544
545 //attribution
546 const QDomElement attribElem = layerElement.firstChildElement( QStringLiteral( "attribution" ) );
547 if ( !attribElem.isNull() )
548 {
549 mAttribution = attribElem.text();
550 mAttributionUrl = attribElem.attribute( QStringLiteral( "href" ), QString() );
551 }
552
553 serverProperties()->readXml( layerElement );
554
555 if ( serverProperties()->metadataUrls().isEmpty() )
556 {
557 // metadataUrl is still empty, maybe it's a QGIS Project < 3.22
558 // keep for legacy
559 const QDomElement metaUrlElem = layerElement.firstChildElement( QStringLiteral( "metadataUrl" ) );
560 if ( !metaUrlElem.isNull() )
561 {
562 const QString url = metaUrlElem.text();
563 const QString type = metaUrlElem.attribute( QStringLiteral( "type" ), QString() );
564 const QString format = metaUrlElem.attribute( QStringLiteral( "format" ), QString() );
565 const QgsMapLayerServerProperties::MetadataUrl newItem( url, type, format );
566 mServerProperties->setMetadataUrls( QList<QgsMapLayerServerProperties::MetadataUrl>() << newItem );
567 }
568 }
569
570 // mMetadata.readFromLayer( this );
571 const QDomElement metadataElem = layerElement.firstChildElement( QStringLiteral( "resourceMetadata" ) );
572 mMetadata.readMetadataXml( metadataElem );
573
574 setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), QStringLiteral( "0" ) ).toInt() );
575 if ( layerElement.hasAttribute( QStringLiteral( "autoRefreshMode" ) ) )
576 {
577 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( QStringLiteral( "autoRefreshMode" ) ), Qgis::AutoRefreshMode::Disabled ) );
578 }
579 else
580 {
581 setAutoRefreshMode( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() ? Qgis::AutoRefreshMode::RedrawOnly : Qgis::AutoRefreshMode::Disabled );
582 }
583 setRefreshOnNofifyMessage( layerElement.attribute( QStringLiteral( "refreshOnNotifyMessage" ), QString() ) );
584 setRefreshOnNotifyEnabled( layerElement.attribute( QStringLiteral( "refreshOnNotifyEnabled" ), QStringLiteral( "0" ) ).toInt() );
585
586 // geographic extent is read only if necessary
588 {
589 const QDomNode wgs84ExtentNode = layerElement.namedItem( QStringLiteral( "wgs84extent" ) );
590 if ( !wgs84ExtentNode.isNull() )
591 mWgs84Extent = QgsXmlUtils::readRectangle( wgs84ExtentNode.toElement() );
592 }
593
594 mLegendPlaceholderImage = layerElement.attribute( QStringLiteral( "legendPlaceholderImage" ) );
595
596 return ! layerError;
597} // bool QgsMapLayer::readLayerXML
598
599
600bool QgsMapLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
601{
603
604 Q_UNUSED( layer_node )
605 Q_UNUSED( context )
606 // NOP by default; children will over-ride with behavior specific to them
607
608 // read Extent
610 {
611 const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
612 if ( !extentNode.isNull() )
613 {
614 mExtent = QgsXmlUtils::readRectangle( extentNode.toElement() );
615 }
616 }
617
618 return true;
619} // void QgsMapLayer::readXml
620
621
622bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const
623{
625
626 if ( !extent().isNull() )
627 {
628 layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent, document ) );
629 layerElement.appendChild( QgsXmlUtils::writeRectangle( wgs84Extent( true ), document, QStringLiteral( "wgs84extent" ) ) );
630 }
631
632 layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer->interval() ) );
633 layerElement.setAttribute( QStringLiteral( "autoRefreshMode" ), qgsEnumValueToKey( mAutoRefreshMode ) );
634 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyEnabled" ), mIsRefreshOnNofifyEnabled ? 1 : 0 );
635 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyMessage" ), mRefreshOnNofifyMessage );
636
637 // ID
638 QDomElement layerId = document.createElement( QStringLiteral( "id" ) );
639 const QDomText layerIdText = document.createTextNode( id() );
640 layerId.appendChild( layerIdText );
641
642 layerElement.appendChild( layerId );
643
644 // data source
645 QDomElement dataSource = document.createElement( QStringLiteral( "datasource" ) );
646 const QString src = context.pathResolver().writePath( encodedSource( source(), context ) );
647 const QDomText dataSourceText = document.createTextNode( src );
648 dataSource.appendChild( dataSourceText );
649 layerElement.appendChild( dataSource );
650
651 // layer name
652 QDomElement layerName = document.createElement( QStringLiteral( "layername" ) );
653 const QDomText layerNameText = document.createTextNode( name() );
654 layerName.appendChild( layerNameText );
655 layerElement.appendChild( layerName );
656
657 // layer short name
658 if ( !mShortName.isEmpty() )
659 {
660 QDomElement layerShortName = document.createElement( QStringLiteral( "shortname" ) );
661 const QDomText layerShortNameText = document.createTextNode( mShortName );
662 layerShortName.appendChild( layerShortNameText );
663 layerElement.appendChild( layerShortName );
664 }
665
666 // layer title
667 if ( !mTitle.isEmpty() )
668 {
669 QDomElement layerTitle = document.createElement( QStringLiteral( "title" ) );
670 const QDomText layerTitleText = document.createTextNode( mTitle );
671 layerTitle.appendChild( layerTitleText );
672 layerElement.appendChild( layerTitle );
673 }
674
675 // layer abstract
676 if ( !mAbstract.isEmpty() )
677 {
678 QDomElement layerAbstract = document.createElement( QStringLiteral( "abstract" ) );
679 const QDomText layerAbstractText = document.createTextNode( mAbstract );
680 layerAbstract.appendChild( layerAbstractText );
681 layerElement.appendChild( layerAbstract );
682 }
683
684 // layer keyword list
685 const QStringList keywordStringList = keywordList().split( ',' );
686 if ( !keywordStringList.isEmpty() )
687 {
688 QDomElement layerKeywordList = document.createElement( QStringLiteral( "keywordList" ) );
689 for ( int i = 0; i < keywordStringList.size(); ++i )
690 {
691 QDomElement layerKeywordValue = document.createElement( QStringLiteral( "value" ) );
692 const QDomText layerKeywordText = document.createTextNode( keywordStringList.at( i ).trimmed() );
693 layerKeywordValue.appendChild( layerKeywordText );
694 layerKeywordList.appendChild( layerKeywordValue );
695 }
696 layerElement.appendChild( layerKeywordList );
697 }
698
699 // layer dataUrl
700 const QString aDataUrl = dataUrl();
701 if ( !aDataUrl.isEmpty() )
702 {
703 QDomElement layerDataUrl = document.createElement( QStringLiteral( "dataUrl" ) );
704 const QDomText layerDataUrlText = document.createTextNode( aDataUrl );
705 layerDataUrl.appendChild( layerDataUrlText );
706 layerDataUrl.setAttribute( QStringLiteral( "format" ), dataUrlFormat() );
707 layerElement.appendChild( layerDataUrl );
708 }
709
710 // layer legendUrl
711 const QString aLegendUrl = legendUrl();
712 if ( !aLegendUrl.isEmpty() )
713 {
714 QDomElement layerLegendUrl = document.createElement( QStringLiteral( "legendUrl" ) );
715 const QDomText layerLegendUrlText = document.createTextNode( aLegendUrl );
716 layerLegendUrl.appendChild( layerLegendUrlText );
717 layerLegendUrl.setAttribute( QStringLiteral( "format" ), legendUrlFormat() );
718 layerElement.appendChild( layerLegendUrl );
719 }
720
721 // layer attribution
722 const QString aAttribution = attribution();
723 if ( !aAttribution.isEmpty() )
724 {
725 QDomElement layerAttribution = document.createElement( QStringLiteral( "attribution" ) );
726 const QDomText layerAttributionText = document.createTextNode( aAttribution );
727 layerAttribution.appendChild( layerAttributionText );
728 layerAttribution.setAttribute( QStringLiteral( "href" ), attributionUrl() );
729 layerElement.appendChild( layerAttribution );
730 }
731
732 // timestamp if supported
733 if ( timestamp() > QDateTime() )
734 {
735 QDomElement stamp = document.createElement( QStringLiteral( "timestamp" ) );
736 const QDomText stampText = document.createTextNode( timestamp().toString( Qt::ISODate ) );
737 stamp.appendChild( stampText );
738 layerElement.appendChild( stamp );
739 }
740
741 layerElement.appendChild( layerName );
742
743 // zorder
744 // This is no longer stored in the project file. It is superfluous since the layers
745 // are written and read in the proper order.
746
747 // spatial reference system id
748 QDomElement mySrsElement = document.createElement( QStringLiteral( "srs" ) );
749 mCRS.writeXml( mySrsElement, document );
750 layerElement.appendChild( mySrsElement );
751
752 // layer metadata
753 QDomElement myMetadataElem = document.createElement( QStringLiteral( "resourceMetadata" ) );
754 mMetadata.writeMetadataXml( myMetadataElem, document );
755 layerElement.appendChild( myMetadataElem );
756
757 layerElement.setAttribute( QStringLiteral( "legendPlaceholderImage" ), mLegendPlaceholderImage );
758
759 // now append layer node to map layer node
760 return writeXml( layerElement, document, context );
761}
762
763void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
764 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
765{
767
768 // save categories
769 const QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::StyleCategories>();
770 const QString categoriesKeys( metaEnum.valueToKeys( static_cast<int>( categories ) ) );
771 layerElement.setAttribute( QStringLiteral( "styleCategories" ), categoriesKeys );
772
773 if ( categories.testFlag( Rendering ) )
774 {
775 // use scale dependent visibility flag
776 layerElement.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
777 layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
778 layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );
779 }
780
781 if ( categories.testFlag( Symbology3D ) )
782 {
783 if ( m3DRenderer )
784 {
785 QDomElement renderer3DElem = document.createElement( QStringLiteral( "renderer-3d" ) );
786 renderer3DElem.setAttribute( QStringLiteral( "type" ), m3DRenderer->type() );
787 m3DRenderer->writeXml( renderer3DElem, context );
788 layerElement.appendChild( renderer3DElem );
789 }
790 }
791
792 if ( categories.testFlag( LayerConfiguration ) )
793 {
794 // flags
795 // this code is saving automatically all the flags entries
796 QDomElement layerFlagsElem = document.createElement( QStringLiteral( "flags" ) );
797 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
798 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
799 {
800 const bool flagValue = mFlags.testFlag( it.key() );
801 QDomElement flagElem = document.createElement( it.value() );
802 flagElem.appendChild( document.createTextNode( QString::number( flagValue ) ) );
803 layerFlagsElem.appendChild( flagElem );
804 }
805 layerElement.appendChild( layerFlagsElem );
806 }
807
808 if ( categories.testFlag( Temporal ) )
809 {
811 properties->writeXml( layerElement, document, context );
812 }
813
814 if ( categories.testFlag( Elevation ) )
815 {
817 properties->writeXml( layerElement, document, context );
818 }
819
820 if ( categories.testFlag( Notes ) && QgsLayerNotesUtils::layerHasNotes( this ) )
821 {
822 QDomElement notesElem = document.createElement( QStringLiteral( "userNotes" ) );
823 notesElem.setAttribute( QStringLiteral( "value" ), QgsLayerNotesUtils::layerNotes( this ) );
824 layerElement.appendChild( notesElem );
825 }
826
827 // custom properties
828 if ( categories.testFlag( CustomProperties ) )
829 {
830 writeCustomProperties( layerElement, document );
831 }
832}
833
834
835bool QgsMapLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const
836{
838
839 Q_UNUSED( layer_node )
840 Q_UNUSED( document )
841 Q_UNUSED( context )
842 // NOP by default; children will over-ride with behavior specific to them
843
844 return true;
845}
846
847QString QgsMapLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
848{
850
851 Q_UNUSED( context )
852 return source;
853}
854
855QString QgsMapLayer::decodedSource( const QString &source, const QString &dataProvider, const QgsReadWriteContext &context ) const
856{
858
859 Q_UNUSED( context )
860 Q_UNUSED( dataProvider )
861 return source;
862}
863
865{
867
869 if ( m3DRenderer )
870 m3DRenderer->resolveReferences( *project );
871}
872
873
874void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
875{
877
878 const QgsObjectCustomProperties oldKeys = mCustomProperties;
879
880 mCustomProperties.readXml( layerNode, keyStartsWith );
881
882 for ( const QString &key : mCustomProperties.keys() )
883 {
884 if ( !oldKeys.contains( key ) || mCustomProperties.value( key ) != oldKeys.value( key ) )
885 {
886 emit customPropertyChanged( key );
887 }
888 }
889}
890
891void QgsMapLayer::writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const
892{
894
895 mCustomProperties.writeXml( layerNode, doc );
896}
897
898void QgsMapLayer::readStyleManager( const QDomNode &layerNode )
899{
901
902 const QDomElement styleMgrElem = layerNode.firstChildElement( QStringLiteral( "map-layer-style-manager" ) );
903 if ( !styleMgrElem.isNull() )
904 mStyleManager->readXml( styleMgrElem );
905 else
906 mStyleManager->reset();
907}
908
909void QgsMapLayer::writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const
910{
912
913 if ( mStyleManager )
914 {
915 QDomElement styleMgrElem = doc.createElement( QStringLiteral( "map-layer-style-manager" ) );
916 mStyleManager->writeXml( styleMgrElem );
917 layerNode.appendChild( styleMgrElem );
918 }
919}
920
922{
924
925 return mMapTipTemplate;
926}
927
928void QgsMapLayer::setMapTipTemplate( const QString &mapTip )
929{
931
932 if ( mMapTipTemplate == mapTip )
933 return;
934
935 mMapTipTemplate = mapTip;
937}
938
940{
942
943 if ( mMapTipsEnabled == enabled )
944 return;
945
946 mMapTipsEnabled = enabled;
948}
949
951{
953
954 return mMapTipsEnabled;
955}
956
957QgsDataProvider::ReadFlags QgsMapLayer::providerReadFlags( const QDomNode &layerNode, QgsMapLayer::ReadFlags layerReadFlags )
958{
959 QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags();
960 if ( layerReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
961 {
963 }
964 if ( layerReadFlags & QgsMapLayer::FlagForceReadOnly )
965 {
967 }
968
969 if ( layerReadFlags & QgsMapLayer::FlagReadExtentFromXml )
970 {
971 const QDomNode extentNode = layerNode.namedItem( QStringLiteral( "extent" ) );
972 if ( !extentNode.isNull() )
973 {
975 }
976 }
977
978 return flags;
979}
980
982{
983 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
985
986 return mValid;
987}
988
989#if 0
990void QgsMapLayer::connectNotify( const char *signal )
991{
992 Q_UNUSED( signal )
993 QgsDebugMsgLevel( "QgsMapLayer connected to " + QString( signal ), 3 );
994} // QgsMapLayer::connectNotify
995#endif
996
997bool QgsMapLayer::isInScaleRange( double scale ) const
998{
999 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1001
1002 return !mScaleBasedVisibility ||
1003 ( ( mMinScale == 0 || mMinScale * Qgis::SCALE_PRECISION < scale )
1004 && ( mMaxScale == 0 || scale < mMaxScale ) );
1005}
1006
1008{
1009 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1011
1012 return mScaleBasedVisibility;
1013}
1014
1016{
1018
1019 return mAutoRefreshMode != Qgis::AutoRefreshMode::Disabled;;
1020}
1021
1023{
1025
1026 return mAutoRefreshMode;
1027}
1028
1030{
1032
1033 return mRefreshTimer->interval();
1034}
1035
1037{
1039
1040 if ( interval <= 0 )
1041 {
1042 mRefreshTimer->stop();
1043 mRefreshTimer->setInterval( 0 );
1045 }
1046 else
1047 {
1048 mRefreshTimer->setInterval( interval );
1049 }
1050 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1051}
1052
1059
1061{
1063
1064 if ( mode == mAutoRefreshMode )
1065 return;
1066
1067 mAutoRefreshMode = mode;
1068 switch ( mAutoRefreshMode )
1069 {
1071 mRefreshTimer->stop();
1072 break;
1073
1076 if ( mRefreshTimer->interval() > 0 )
1077 mRefreshTimer->start();
1078 break;
1079 }
1080
1081 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1082}
1083
1085{
1087
1088 return mMetadata;
1089}
1090
1092{
1094
1095 mMinScale = scale;
1096}
1097
1099{
1101
1102 return mMinScale;
1103}
1104
1106{
1108
1109 mMaxScale = scale;
1110}
1111
1113{
1115
1116 mScaleBasedVisibility = enabled;
1117}
1118
1120{
1122
1123 return mMaxScale;
1124}
1125
1126QStringList QgsMapLayer::subLayers() const
1127{
1129
1130 return QStringList();
1131}
1132
1133void QgsMapLayer::setLayerOrder( const QStringList &layers )
1134{
1136
1137 Q_UNUSED( layers )
1138}
1139
1140void QgsMapLayer::setSubLayerVisibility( const QString &name, bool vis )
1141{
1143
1144 Q_UNUSED( name )
1145 Q_UNUSED( vis )
1146}
1147
1149{
1151
1152 return false;
1153}
1154
1156{
1157 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1159
1160 return mCRS;
1161}
1162
1163void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal )
1164{
1166
1167 mCRS = srs;
1168
1169 if ( mShouldValidateCrs && isSpatial() && !mCRS.isValid() && type() != Qgis::LayerType::Annotation )
1170 {
1171 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( name() ) );
1172 mCRS.validate();
1173 }
1174
1175 if ( emitSignal )
1176 emit crsChanged();
1177}
1178
1180{
1182
1183 const QgsDataProvider *lDataProvider = dataProvider();
1184 return lDataProvider ? lDataProvider->transformContext() : QgsCoordinateTransformContext();
1185}
1186
1187QString QgsMapLayer::formatLayerName( const QString &name )
1188{
1189 QString layerName( name );
1190 layerName.replace( '_', ' ' );
1192 return layerName;
1193}
1194
1195QString QgsMapLayer::baseURI( PropertyType type ) const
1196{
1198
1199 QString myURI = publicSource();
1200
1201 // first get base path for delimited text, spatialite and OGR layers,
1202 // as in these cases URI may contain layer name and/or additional
1203 // information. This also strips prefix in case if VSIFILE mechanism
1204 // is used
1205 if ( providerType() == QLatin1String( "ogr" ) || providerType() == QLatin1String( "delimitedtext" )
1206 || providerType() == QLatin1String( "gdal" ) || providerType() == QLatin1String( "spatialite" ) )
1207 {
1208 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( providerType(), myURI );
1209 myURI = components["path"].toString();
1210 }
1211
1212 QFileInfo myFileInfo( myURI );
1213 QString key;
1214
1215 if ( myFileInfo.exists() )
1216 {
1217 // if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name
1218 if ( myURI.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
1219 myURI.chop( 3 );
1220 else if ( myURI.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
1221 myURI.chop( 4 );
1222 else if ( myURI.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) )
1223 myURI.chop( 4 );
1224 else if ( myURI.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) )
1225 myURI.chop( 7 );
1226 else if ( myURI.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
1227 myURI.chop( 4 );
1228 myFileInfo.setFile( myURI );
1229 // get the file name for our .qml style file
1230 key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1231 }
1232 else
1233 {
1234 key = publicSource();
1235 }
1236
1237 return key;
1238}
1239
1241{
1243
1244 return baseURI( PropertyType::Metadata );
1245}
1246
1247QString QgsMapLayer::saveDefaultMetadata( bool &resultFlag )
1248{
1250
1251 if ( const QgsProviderMetadata *metadata = QgsProviderRegistry::instance()->providerMetadata( providerType() ) )
1252 {
1253 if ( metadata->providerCapabilities() & QgsProviderMetadata::SaveLayerMetadata )
1254 {
1255 try
1256 {
1257 QString errorMessage;
1258 resultFlag = QgsProviderRegistry::instance()->saveLayerMetadata( providerType(), mDataSource, mMetadata, errorMessage );
1259 if ( resultFlag )
1260 return tr( "Successfully saved default layer metadata" );
1261 else
1262 return errorMessage;
1263 }
1264 catch ( QgsNotSupportedException &e )
1265 {
1266 resultFlag = false;
1267 return e.what();
1268 }
1269 }
1270 }
1271
1272 // fallback default metadata saving method, for providers which don't support (or implement) saveLayerMetadata
1273 return saveNamedMetadata( metadataUri(), resultFlag );
1274}
1275
1276QString QgsMapLayer::loadDefaultMetadata( bool &resultFlag )
1277{
1279
1280 return loadNamedMetadata( metadataUri(), resultFlag );
1281}
1282
1284{
1286
1287 return baseURI( PropertyType::Style );
1288}
1289
1290QString QgsMapLayer::loadDefaultStyle( bool &resultFlag )
1291{
1293
1294 return loadNamedStyle( styleURI(), resultFlag );
1295}
1296
1297bool QgsMapLayer::loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd )
1298{
1300
1301 return loadNamedPropertyFromDatabase( db, uri, qmd, PropertyType::Metadata );
1302}
1303
1304bool QgsMapLayer::loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml )
1305{
1307
1308 return loadNamedPropertyFromDatabase( db, uri, qml, PropertyType::Style );
1309}
1310
1311bool QgsMapLayer::loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type )
1312{
1314
1315 QgsDebugMsgLevel( QStringLiteral( "db = %1 uri = %2" ).arg( db, uri ), 4 );
1316
1317 bool resultFlag = false;
1318
1319 // read from database
1322
1323 int myResult;
1324
1325 QgsDebugMsgLevel( QStringLiteral( "Trying to load style or metadata for \"%1\" from \"%2\"" ).arg( uri, db ), 4 );
1326
1327 if ( db.isEmpty() || !QFile( db ).exists() )
1328 return false;
1329
1330 myResult = database.open_v2( db, SQLITE_OPEN_READONLY, nullptr );
1331 if ( myResult != SQLITE_OK )
1332 {
1333 return false;
1334 }
1335
1336 QString mySql;
1337 switch ( type )
1338 {
1339 case Metadata:
1340 mySql = QStringLiteral( "select qmd from tbl_metadata where metadata=?" );
1341 break;
1342
1343 case Style:
1344 mySql = QStringLiteral( "select qml from tbl_styles where style=?" );
1345 break;
1346 }
1347
1348 statement = database.prepare( mySql, myResult );
1349 if ( myResult == SQLITE_OK )
1350 {
1351 QByteArray param = uri.toUtf8();
1352
1353 if ( sqlite3_bind_text( statement.get(), 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK &&
1354 sqlite3_step( statement.get() ) == SQLITE_ROW )
1355 {
1356 xml = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( statement.get(), 0 ) ) );
1357 resultFlag = true;
1358 }
1359 }
1360 return resultFlag;
1361}
1362
1363
1364QString QgsMapLayer::loadNamedStyle( const QString &uri, bool &resultFlag, QgsMapLayer::StyleCategories categories )
1365{
1367
1368 return loadNamedStyle( uri, resultFlag, false, categories );
1369}
1370
1371QString QgsMapLayer::loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag, StyleCategories categories )
1372{
1374
1375 QgsDebugMsgLevel( QStringLiteral( "uri = %1 myURI = %2" ).arg( uri, publicSource() ), 4 );
1376
1377 resultFlag = false;
1378 if ( uri.isEmpty() )
1379 return QString();
1380
1381 QDomDocument myDocument( QStringLiteral( "qgis" ) );
1382
1383 // location of problem associated with errorMsg
1384 int line, column;
1385 QString myErrorMessage;
1386
1387 QFile myFile( uri );
1388 if ( myFile.open( QFile::ReadOnly ) )
1389 {
1390 QgsDebugMsgLevel( QStringLiteral( "file found %1" ).arg( uri ), 2 );
1391 // read file
1392 resultFlag = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
1393 if ( !resultFlag )
1394 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1395 myFile.close();
1396 }
1397 else
1398 {
1399 const QFileInfo project( QgsProject::instance()->fileName() );
1400 QgsDebugMsgLevel( QStringLiteral( "project fileName: %1" ).arg( project.absoluteFilePath() ), 4 );
1401
1402 QString xml;
1403 switch ( type )
1404 {
1405 case QgsMapLayer::Style:
1406 {
1407 if ( loadNamedStyleFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1408 ( project.exists() && loadNamedStyleFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1409 loadNamedStyleFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1410 {
1411 resultFlag = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1412 if ( !resultFlag )
1413 {
1414 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1415 }
1416 }
1417 else
1418 {
1419 myErrorMessage = tr( "Style not found in database" );
1420 resultFlag = false;
1421 }
1422 break;
1423 }
1425 {
1426 if ( loadNamedMetadataFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1427 ( project.exists() && loadNamedMetadataFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1428 loadNamedMetadataFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1429 {
1430 resultFlag = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1431 if ( !resultFlag )
1432 {
1433 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1434 }
1435 }
1436 else
1437 {
1438 myErrorMessage = tr( "Metadata not found in database" );
1439 resultFlag = false;
1440 }
1441 break;
1442 }
1443 }
1444 }
1445
1446 if ( !resultFlag )
1447 {
1448 return myErrorMessage;
1449 }
1450
1451 switch ( type )
1452 {
1453 case QgsMapLayer::Style:
1454 resultFlag = importNamedStyle( myDocument, myErrorMessage, categories );
1455 if ( !resultFlag )
1456 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1457 break;
1459 resultFlag = importNamedMetadata( myDocument, myErrorMessage );
1460 if ( !resultFlag )
1461 myErrorMessage = tr( "Loading metadata file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1462 break;
1463 }
1464 return myErrorMessage;
1465}
1466
1467bool QgsMapLayer::importNamedMetadata( QDomDocument &document, QString &errorMessage )
1468{
1470
1471 const QDomElement myRoot = document.firstChildElement( QStringLiteral( "qgis" ) );
1472 if ( myRoot.isNull() )
1473 {
1474 errorMessage = tr( "Root <qgis> element could not be found" );
1475 return false;
1476 }
1477
1478 return mMetadata.readMetadataXml( myRoot );
1479}
1480
1481bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMessage, QgsMapLayer::StyleCategories categories )
1482{
1484
1485 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "qgis" ) );
1486 if ( myRoot.isNull() )
1487 {
1488 myErrorMessage = tr( "Root <qgis> element could not be found" );
1489 return false;
1490 }
1491
1492 // get style file version string, if any
1493 const QgsProjectVersion fileVersion( myRoot.attribute( QStringLiteral( "version" ) ) );
1494 const QgsProjectVersion thisVersion( Qgis::version() );
1495
1496 if ( thisVersion > fileVersion )
1497 {
1498 QgsProjectFileTransform styleFile( myDocument, fileVersion );
1499 styleFile.updateRevision( thisVersion );
1500 }
1501
1502 // Get source categories
1503 const QgsMapLayer::StyleCategories sourceCategories = QgsXmlUtils::readFlagAttribute( myRoot, QStringLiteral( "styleCategories" ), QgsMapLayer::AllStyleCategories );
1504
1505 //Test for matching geometry type on vector layers when applying, if geometry type is given in the style
1506 if ( ( sourceCategories.testFlag( QgsMapLayer::Symbology ) || sourceCategories.testFlag( QgsMapLayer::Symbology3D ) ) &&
1507 ( categories.testFlag( QgsMapLayer::Symbology ) || categories.testFlag( QgsMapLayer::Symbology3D ) ) )
1508 {
1509 if ( type() == Qgis::LayerType::Vector && !myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).isNull() )
1510 {
1511 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( this );
1512 const Qgis::GeometryType importLayerGeometryType = static_cast<Qgis::GeometryType>( myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).text().toInt() );
1513 if ( importLayerGeometryType != Qgis::GeometryType::Unknown && vl->geometryType() != importLayerGeometryType )
1514 {
1515 myErrorMessage = tr( "Cannot apply style with symbology to layer with a different geometry type" );
1516 return false;
1517 }
1518 }
1519 }
1520
1522 return readSymbology( myRoot, myErrorMessage, context, categories ); // TODO: support relative paths in QML?
1523}
1524
1525void QgsMapLayer::exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const
1526{
1528
1529 QDomImplementation DomImplementation;
1530 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1531 QDomDocument myDocument( documentType );
1532
1533 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1534 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1535 myDocument.appendChild( myRootNode );
1536
1537 if ( !mMetadata.writeMetadataXml( myRootNode, myDocument ) )
1538 {
1539 errorMsg = QObject::tr( "Could not save metadata" );
1540 return;
1541 }
1542
1543 doc = myDocument;
1544}
1545
1546void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
1547{
1549
1550 QDomImplementation DomImplementation;
1551 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1552 QDomDocument myDocument( documentType );
1553
1554 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1555 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1556 myDocument.appendChild( myRootNode );
1557
1558 if ( !writeSymbology( myRootNode, myDocument, errorMsg, context, categories ) ) // TODO: support relative paths in QML?
1559 {
1560 errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1561 return;
1562 }
1563
1564 /*
1565 * Check to see if the layer is vector - in which case we should also export its geometryType
1566 * to avoid eventually pasting to a layer with a different geometry
1567 */
1568 if ( type() == Qgis::LayerType::Vector )
1569 {
1570 //Getting the selectionLayer geometry
1571 const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( this );
1572 const QString geoType = QString::number( static_cast<int>( vl->geometryType() ) );
1573
1574 //Adding geometryinformation
1575 QDomElement layerGeometryType = myDocument.createElement( QStringLiteral( "layerGeometryType" ) );
1576 const QDomText type = myDocument.createTextNode( geoType );
1577
1578 layerGeometryType.appendChild( type );
1579 myRootNode.appendChild( layerGeometryType );
1580 }
1581
1582 doc = myDocument;
1583}
1584
1585QString QgsMapLayer::saveDefaultStyle( bool &resultFlag )
1586{
1588
1589 return saveDefaultStyle( resultFlag, AllStyleCategories );
1590}
1591
1592QString QgsMapLayer::saveDefaultStyle( bool &resultFlag, StyleCategories categories )
1593{
1595
1596 return saveNamedStyle( styleURI(), resultFlag, categories );
1597}
1598
1599QString QgsMapLayer::saveNamedMetadata( const QString &uri, bool &resultFlag )
1600{
1602
1603 return saveNamedProperty( uri, QgsMapLayer::Metadata, resultFlag );
1604}
1605
1606QString QgsMapLayer::loadNamedMetadata( const QString &uri, bool &resultFlag )
1607{
1609
1610 return loadNamedProperty( uri, QgsMapLayer::Metadata, resultFlag );
1611}
1612
1613QString QgsMapLayer::saveNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag, StyleCategories categories )
1614{
1616
1617 // check if the uri is a file or ends with .qml/.qmd,
1618 // which indicates that it should become one
1619 // everything else goes to the database
1620 QString filename;
1621
1622 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1623 if ( vlayer && vlayer->providerType() == QLatin1String( "ogr" ) )
1624 {
1625 QStringList theURIParts = uri.split( '|' );
1626 filename = theURIParts[0];
1627 }
1628 else if ( vlayer && vlayer->providerType() == QLatin1String( "gpx" ) )
1629 {
1630 QStringList theURIParts = uri.split( '?' );
1631 filename = theURIParts[0];
1632 }
1633 else if ( vlayer && vlayer->providerType() == QLatin1String( "delimitedtext" ) )
1634 {
1635 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
1636 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1637 if ( filename.isEmpty() )
1638 filename = uri;
1639 }
1640 else
1641 {
1642 filename = uri;
1643 }
1644
1645 QString myErrorMessage;
1646 QDomDocument myDocument;
1647 switch ( type )
1648 {
1649 case Metadata:
1650 exportNamedMetadata( myDocument, myErrorMessage );
1651 break;
1652
1653 case Style:
1654 const QgsReadWriteContext context;
1655 exportNamedStyle( myDocument, myErrorMessage, context, categories );
1656 break;
1657 }
1658
1659 const QFileInfo myFileInfo( filename );
1660 if ( myFileInfo.exists() || filename.endsWith( QgsMapLayer::extensionPropertyType( type ), Qt::CaseInsensitive ) )
1661 {
1662 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1663 if ( !myDirInfo.isWritable() )
1664 {
1665 resultFlag = false;
1666 return tr( "The directory containing your dataset needs to be writable!" );
1667 }
1668
1669 // now construct the file name for our .qml or .qmd file
1670 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1671
1672 QFile myFile( myFileName );
1673 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1674 {
1675 QTextStream myFileStream( &myFile );
1676 // save as utf-8 with 2 spaces for indents
1677 myDocument.save( myFileStream, 2 );
1678 myFile.close();
1679 resultFlag = true;
1680 switch ( type )
1681 {
1682 case Metadata:
1683 return tr( "Created default metadata file as %1" ).arg( myFileName );
1684
1685 case Style:
1686 return tr( "Created default style file as %1" ).arg( myFileName );
1687 }
1688
1689 }
1690 else
1691 {
1692 resultFlag = false;
1693 switch ( type )
1694 {
1695 case Metadata:
1696 return tr( "ERROR: Failed to created default metadata file as %1. Check file permissions and retry." ).arg( myFileName );
1697
1698 case Style:
1699 return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
1700 }
1701 }
1702 }
1703 else
1704 {
1705 const QString qml = myDocument.toString();
1706
1707 // read from database
1710
1711 int myResult = database.open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ) );
1712 if ( myResult != SQLITE_OK )
1713 {
1714 return tr( "User database could not be opened." );
1715 }
1716
1717 QByteArray param0 = uri.toUtf8();
1718 QByteArray param1 = qml.toUtf8();
1719
1720 QString mySql;
1721 switch ( type )
1722 {
1723 case Metadata:
1724 mySql = QStringLiteral( "create table if not exists tbl_metadata(metadata varchar primary key,qmd varchar)" );
1725 break;
1726
1727 case Style:
1728 mySql = QStringLiteral( "create table if not exists tbl_styles(style varchar primary key,qml varchar)" );
1729 break;
1730 }
1731
1732 statement = database.prepare( mySql, myResult );
1733 if ( myResult == SQLITE_OK )
1734 {
1735 if ( sqlite3_step( statement.get() ) != SQLITE_DONE )
1736 {
1737 resultFlag = false;
1738 switch ( type )
1739 {
1740 case Metadata:
1741 return tr( "The metadata table could not be created." );
1742
1743 case Style:
1744 return tr( "The style table could not be created." );
1745 }
1746 }
1747 }
1748
1749 switch ( type )
1750 {
1751 case Metadata:
1752 mySql = QStringLiteral( "insert into tbl_metadata(metadata,qmd) values (?,?)" );
1753 break;
1754
1755 case Style:
1756 mySql = QStringLiteral( "insert into tbl_styles(style,qml) values (?,?)" );
1757 break;
1758 }
1759 statement = database.prepare( mySql, myResult );
1760 if ( myResult == SQLITE_OK )
1761 {
1762 if ( sqlite3_bind_text( statement.get(), 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1763 sqlite3_bind_text( statement.get(), 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1764 sqlite3_step( statement.get() ) == SQLITE_DONE )
1765 {
1766 resultFlag = true;
1767 switch ( type )
1768 {
1769 case Metadata:
1770 myErrorMessage = tr( "The metadata %1 was saved to database" ).arg( uri );
1771 break;
1772
1773 case Style:
1774 myErrorMessage = tr( "The style %1 was saved to database" ).arg( uri );
1775 break;
1776 }
1777 }
1778 }
1779
1780 if ( !resultFlag )
1781 {
1782 QString mySql;
1783 switch ( type )
1784 {
1785 case Metadata:
1786 mySql = QStringLiteral( "update tbl_metadata set qmd=? where metadata=?" );
1787 break;
1788
1789 case Style:
1790 mySql = QStringLiteral( "update tbl_styles set qml=? where style=?" );
1791 break;
1792 }
1793 statement = database.prepare( mySql, myResult );
1794 if ( myResult == SQLITE_OK )
1795 {
1796 if ( sqlite3_bind_text( statement.get(), 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1797 sqlite3_bind_text( statement.get(), 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1798 sqlite3_step( statement.get() ) == SQLITE_DONE )
1799 {
1800 resultFlag = true;
1801 switch ( type )
1802 {
1803 case Metadata:
1804 myErrorMessage = tr( "The metadata %1 was updated in the database." ).arg( uri );
1805 break;
1806
1807 case Style:
1808 myErrorMessage = tr( "The style %1 was updated in the database." ).arg( uri );
1809 break;
1810 }
1811 }
1812 else
1813 {
1814 resultFlag = false;
1815 switch ( type )
1816 {
1817 case Metadata:
1818 myErrorMessage = tr( "The metadata %1 could not be updated in the database." ).arg( uri );
1819 break;
1820
1821 case Style:
1822 myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( uri );
1823 break;
1824 }
1825 }
1826 }
1827 else
1828 {
1829 resultFlag = false;
1830 switch ( type )
1831 {
1832 case Metadata:
1833 myErrorMessage = tr( "The metadata %1 could not be inserted into database." ).arg( uri );
1834 break;
1835
1836 case Style:
1837 myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( uri );
1838 break;
1839 }
1840 }
1841 }
1842 }
1843
1844 return myErrorMessage;
1845}
1846
1847QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag, StyleCategories categories )
1848{
1850
1851 return saveNamedProperty( uri, QgsMapLayer::Style, resultFlag, categories );
1852}
1853
1854void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
1855{
1856
1857 return exportSldStyleV2( doc, errorMsg, QgsSldExportContext() );
1858}
1859
1860void QgsMapLayer::exportSldStyleV2( QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext ) const
1861{
1863
1864 QDomDocument myDocument = QDomDocument();
1865
1866 const QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
1867 myDocument.appendChild( header );
1868
1869 const QgsVectorLayer *vlayer = qobject_cast<const QgsVectorLayer *>( this );
1870 const QgsRasterLayer *rlayer = qobject_cast<const QgsRasterLayer *>( this );
1871 if ( !vlayer && !rlayer )
1872 {
1873 errorMsg = tr( "Could not save symbology because:\n%1" )
1874 .arg( tr( "Only vector and raster layers are supported" ) );
1875 return;
1876 }
1877
1878 // Create the root element
1879 QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "StyledLayerDescriptor" ) );
1880 QDomElement layerNode;
1881 if ( vlayer )
1882 {
1883 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.1.0" ) );
1884 root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" ) );
1885 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
1886 root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) );
1887 root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
1888 root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
1889 myDocument.appendChild( root );
1890
1891 // Create the NamedLayer element
1892 layerNode = myDocument.createElement( QStringLiteral( "NamedLayer" ) );
1893 root.appendChild( layerNode );
1894 }
1895
1896 // note: Only SLD 1.0 version is generated because seems none is using SE1.1.0 at least for rasters
1897 if ( rlayer )
1898 {
1899 // Create the root element
1900 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0.0" ) );
1901 root.setAttribute( QStringLiteral( "xmlns:gml" ), QStringLiteral( "http://www.opengis.net/gml" ) );
1902 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
1903 root.setAttribute( QStringLiteral( "xmlns:sld" ), QStringLiteral( "http://www.opengis.net/sld" ) );
1904 myDocument.appendChild( root );
1905
1906 // Create the NamedLayer element
1907 layerNode = myDocument.createElement( QStringLiteral( "UserLayer" ) );
1908 root.appendChild( layerNode );
1909 }
1910
1911 QVariantMap props;
1912
1913 QVariant context;
1914 context.setValue( exportContext );
1915
1916 props[ QStringLiteral( "SldExportContext" ) ] = context;
1917
1919 {
1920 props[ QStringLiteral( "scaleMinDenom" ) ] = QString::number( mMinScale );
1921 props[ QStringLiteral( "scaleMaxDenom" ) ] = QString::number( mMaxScale );
1922 }
1923
1924 if ( vlayer )
1925 {
1926 if ( !vlayer->writeSld( layerNode, myDocument, errorMsg, props ) )
1927 {
1928 errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1929 return;
1930 }
1931 }
1932
1933 if ( rlayer )
1934 {
1935 if ( !rlayer->writeSld( layerNode, myDocument, errorMsg, props ) )
1936 {
1937 errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1938 return;
1939 }
1940 }
1941
1942 doc = myDocument;
1943}
1944
1945QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const
1946{
1947 QgsSldExportContext context;
1948 context.setExportFilePath( uri );
1949 return saveSldStyleV2( resultFlag, context );
1950}
1951
1952QString QgsMapLayer::saveSldStyleV2( bool &resultFlag, const QgsSldExportContext &exportContext ) const
1953{
1955
1956 const QgsMapLayer *mlayer = qobject_cast<const QgsMapLayer *>( this );
1957
1958 const QString uri { exportContext.exportFilePath() };
1959
1960 // check if the uri is a file or ends with .sld,
1961 // which indicates that it should become one
1962 QString filename;
1963 if ( mlayer->providerType() == QLatin1String( "ogr" ) )
1964 {
1965 QStringList theURIParts = uri.split( '|' );
1966 filename = theURIParts[0];
1967 }
1968 else if ( mlayer->providerType() == QLatin1String( "gpx" ) )
1969 {
1970 QStringList theURIParts = uri.split( '?' );
1971 filename = theURIParts[0];
1972 }
1973 else if ( mlayer->providerType() == QLatin1String( "delimitedtext" ) )
1974 {
1975 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
1976 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1977 if ( filename.isEmpty() )
1978 filename = uri;
1979 }
1980 else
1981 {
1982 filename = uri;
1983 }
1984
1985 const QFileInfo myFileInfo( filename );
1986 if ( myFileInfo.exists() || filename.endsWith( QLatin1String( ".sld" ), Qt::CaseInsensitive ) )
1987 {
1988 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1989 if ( !myDirInfo.isWritable() )
1990 {
1991 resultFlag = false;
1992 return tr( "The directory containing your dataset needs to be writable!" );
1993 }
1994
1995 // now construct the file name for our .sld style file
1996 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld";
1997
1998 QString errorMsg;
1999 QDomDocument myDocument;
2000
2001 QgsSldExportContext context { exportContext };
2002 context.setExportFilePath( myFileName );
2003
2004 mlayer->exportSldStyleV2( myDocument, errorMsg, context );
2005
2006 if ( !errorMsg.isNull() )
2007 {
2008 resultFlag = false;
2009 return errorMsg;
2010 }
2011
2012 QFile myFile( myFileName );
2013 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
2014 {
2015 QTextStream myFileStream( &myFile );
2016 // save as utf-8 with 2 spaces for indents
2017 myDocument.save( myFileStream, 2 );
2018 myFile.close();
2019 resultFlag = true;
2020 return tr( "Created default style file as %1" ).arg( myFileName );
2021 }
2022 }
2023
2024 resultFlag = false;
2025 return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );
2026
2027}
2028
2029QString QgsMapLayer::loadSldStyle( const QString &uri, bool &resultFlag )
2030{
2032
2033 resultFlag = false;
2034
2035 QDomDocument myDocument;
2036
2037 // location of problem associated with errorMsg
2038 int line, column;
2039 QString myErrorMessage;
2040
2041 QFile myFile( uri );
2042 if ( myFile.open( QFile::ReadOnly ) )
2043 {
2044 // read file
2045 resultFlag = myDocument.setContent( &myFile, true, &myErrorMessage, &line, &column );
2046 if ( !resultFlag )
2047 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
2048 myFile.close();
2049 }
2050 else
2051 {
2052 myErrorMessage = tr( "Unable to open file %1" ).arg( uri );
2053 }
2054
2055 if ( !resultFlag )
2056 {
2057 return myErrorMessage;
2058 }
2059
2060 // check for root SLD element
2061 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "StyledLayerDescriptor" ) );
2062 if ( myRoot.isNull() )
2063 {
2064 myErrorMessage = QStringLiteral( "Error: StyledLayerDescriptor element not found in %1" ).arg( uri );
2065 resultFlag = false;
2066 return myErrorMessage;
2067 }
2068
2069 // now get the style node out and pass it over to the layer
2070 // to deserialise...
2071 const QDomElement namedLayerElem = myRoot.firstChildElement( QStringLiteral( "NamedLayer" ) );
2072 if ( namedLayerElem.isNull() )
2073 {
2074 myErrorMessage = QStringLiteral( "Info: NamedLayer element not found." );
2075 resultFlag = false;
2076 return myErrorMessage;
2077 }
2078
2079 QString errorMsg;
2080 resultFlag = readSld( namedLayerElem, errorMsg );
2081 if ( !resultFlag )
2082 {
2083 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, errorMsg );
2084 return myErrorMessage;
2085 }
2086
2087 return QString();
2088}
2089
2090bool QgsMapLayer::readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2091{
2093
2094 Q_UNUSED( node )
2095 Q_UNUSED( errorMessage )
2096 Q_UNUSED( context )
2097 Q_UNUSED( categories )
2098 return false;
2099}
2100
2101bool QgsMapLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
2102 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
2103{
2105
2106 Q_UNUSED( node )
2107 Q_UNUSED( doc )
2108 Q_UNUSED( errorMessage )
2109 Q_UNUSED( context )
2110 Q_UNUSED( categories )
2111 return false;
2112}
2113
2114
2115void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2116 bool loadDefaultStyleFlag )
2117{
2119
2121
2122 QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags();
2123 if ( loadDefaultStyleFlag )
2124 {
2126 }
2127
2129 {
2131 }
2132 setDataSource( dataSource, baseName, provider, options, flags );
2133}
2134
2135void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2136 const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
2137{
2139
2140 QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags();
2141 if ( loadDefaultStyleFlag )
2142 {
2144 }
2145
2147 {
2149 }
2150 setDataSource( dataSource, baseName, provider, options, flags );
2151}
2152
2153void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2154 const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
2155{
2157
2160 {
2162 }
2163 setDataSourcePrivate( dataSource, baseName, provider, options, flags );
2164 emit dataSourceChanged();
2165 emit dataChanged();
2167}
2168
2169
2170void QgsMapLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
2171 const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
2172{
2174
2175 Q_UNUSED( dataSource )
2176 Q_UNUSED( baseName )
2177 Q_UNUSED( provider )
2178 Q_UNUSED( options )
2179 Q_UNUSED( flags )
2180}
2181
2182
2184{
2186
2187 return mProviderKey;
2188}
2189
2190void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
2191 QgsMapLayer::StyleCategories categories )
2192{
2194
2195 if ( categories.testFlag( Symbology3D ) )
2196 {
2197 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "3D Symbology" ) );
2198
2199 QgsAbstract3DRenderer *r3D = nullptr;
2200 QDomElement renderer3DElem = layerElement.firstChildElement( QStringLiteral( "renderer-3d" ) );
2201 if ( !renderer3DElem.isNull() )
2202 {
2203 const QString type3D = renderer3DElem.attribute( QStringLiteral( "type" ) );
2205 if ( meta3D )
2206 {
2207 r3D = meta3D->createRenderer( renderer3DElem, context );
2208 }
2209 }
2210 setRenderer3D( r3D );
2211 }
2212
2213 if ( categories.testFlag( CustomProperties ) )
2214 {
2215 // read custom properties before passing reading further to a subclass, so that
2216 // the subclass can also read custom properties
2217 readCustomProperties( layerElement );
2218 }
2219
2220 // use scale dependent visibility flag
2221 if ( categories.testFlag( Rendering ) )
2222 {
2223 setScaleBasedVisibility( layerElement.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
2224 if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
2225 {
2226 // older element, when scales were reversed
2227 setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
2228 setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
2229 }
2230 else
2231 {
2232 setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
2233 setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
2234 }
2235 }
2236
2237 if ( categories.testFlag( LayerConfiguration ) )
2238 {
2239 // flags
2240 const QDomElement flagsElem = layerElement.firstChildElement( QStringLiteral( "flags" ) );
2241 LayerFlags flags = mFlags;
2242 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
2243 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
2244 {
2245 const QDomNode flagNode = flagsElem.namedItem( it.value() );
2246 if ( flagNode.isNull() )
2247 continue;
2248 const bool flagValue = flagNode.toElement().text() == "1" ? true : false;
2249 if ( flags.testFlag( it.key() ) && !flagValue )
2250 flags &= ~it.key();
2251 else if ( !flags.testFlag( it.key() ) && flagValue )
2252 flags |= it.key();
2253 }
2254 setFlags( flags );
2255 }
2256
2257 if ( categories.testFlag( Temporal ) )
2258 {
2259 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Temporal" ) );
2260
2262 properties->readXml( layerElement.toElement(), context );
2263 }
2264
2265 if ( categories.testFlag( Elevation ) )
2266 {
2267 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Elevation" ) );
2268
2270 properties->readXml( layerElement.toElement(), context );
2271 }
2272
2273 if ( categories.testFlag( Notes ) )
2274 {
2275 const QDomElement notesElem = layerElement.firstChildElement( QStringLiteral( "userNotes" ) );
2276 if ( !notesElem.isNull() )
2277 {
2278 const QString notes = notesElem.attribute( QStringLiteral( "value" ) );
2279 QgsLayerNotesUtils::setLayerNotes( this, notes );
2280 }
2281 }
2282}
2283
2285{
2287
2288 return mUndoStack;
2289}
2290
2292{
2294
2295 return mUndoStackStyles;
2296}
2297
2299{
2301
2302 return mCustomProperties.keys();
2303}
2304
2305void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
2306{
2308
2309 if ( !mCustomProperties.contains( key ) || mCustomProperties.value( key ) != value )
2310 {
2311 mCustomProperties.setValue( key, value );
2312 emit customPropertyChanged( key );
2313 }
2314}
2315
2317{
2319
2320 mCustomProperties = properties;
2321 for ( const QString &key : mCustomProperties.keys() )
2322 {
2323 emit customPropertyChanged( key );
2324 }
2325}
2326
2328{
2330
2331 return mCustomProperties;
2332}
2333
2334QVariant QgsMapLayer::customProperty( const QString &value, const QVariant &defaultValue ) const
2335{
2336 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
2338
2339 return mCustomProperties.value( value, defaultValue );
2340}
2341
2342void QgsMapLayer::removeCustomProperty( const QString &key )
2343{
2345
2346 if ( mCustomProperties.contains( key ) )
2347 {
2348 mCustomProperties.remove( key );
2349 emit customPropertyChanged( key );
2350 }
2351}
2352
2353int QgsMapLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError )
2354{
2356
2357 return QgsProviderRegistry::instance()->listStyles( mProviderKey, mDataSource, ids, names, descriptions, msgError );
2358}
2359
2360QString QgsMapLayer::getStyleFromDatabase( const QString &styleId, QString &msgError )
2361{
2363
2364 return QgsProviderRegistry::instance()->getStyleById( mProviderKey, mDataSource, styleId, msgError );
2365}
2366
2367bool QgsMapLayer::deleteStyleFromDatabase( const QString &styleId, QString &msgError )
2368{
2370
2372}
2373
2374void QgsMapLayer::saveStyleToDatabase( const QString &name, const QString &description,
2375 bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories )
2376{
2378
2379 QString sldStyle, qmlStyle;
2380 QDomDocument qmlDocument, sldDocument;
2381 QgsReadWriteContext context;
2382 exportNamedStyle( qmlDocument, msgError, context, categories );
2383 if ( !msgError.isNull() )
2384 {
2385 return;
2386 }
2387 qmlStyle = qmlDocument.toString();
2388
2389 this->exportSldStyle( sldDocument, msgError );
2390 if ( !msgError.isNull() )
2391 {
2392 return;
2393 }
2394 sldStyle = sldDocument.toString();
2395
2397 mDataSource, qmlStyle, sldStyle, name,
2398 description, uiFileContent, useAsDefault, msgError );
2399}
2400
2401QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &resultFlag, bool loadFromLocalDB, QgsMapLayer::StyleCategories categories )
2402{
2404
2405 QString returnMessage;
2406 QString qml, errorMsg;
2407 QString styleName;
2408 if ( !loadFromLocalDB && dataProvider() && dataProvider()->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2409 {
2411 }
2412
2413 // Style was successfully loaded from provider storage
2414 if ( !qml.isEmpty() )
2415 {
2416 QDomDocument myDocument( QStringLiteral( "qgis" ) );
2417 myDocument.setContent( qml );
2418 resultFlag = importNamedStyle( myDocument, errorMsg );
2419 returnMessage = QObject::tr( "Loaded from Provider" );
2420 }
2421 else
2422 {
2423 returnMessage = loadNamedProperty( theURI, PropertyType::Style, resultFlag, categories );
2424 }
2425
2426 if ( ! styleName.isEmpty() )
2427 {
2428 styleManager()->renameStyle( styleManager()->currentStyle(), styleName );
2429 }
2430
2431 if ( resultFlag )
2432 emit styleLoaded( categories );
2433
2434 return returnMessage;
2435}
2436
2443
2445{
2447
2448 return false;
2449}
2450
2452{
2454
2455 return false;
2456}
2457
2459{
2461
2462 return true;
2463}
2464
2466{
2468
2469 // invalid layers are temporary? -- who knows?!
2470 if ( !isValid() )
2471 return false;
2472
2473 if ( mProviderKey == QLatin1String( "memory" ) )
2474 return true;
2475
2476 const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( mProviderKey, mDataSource );
2477 const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
2478 if ( path.isEmpty() )
2479 return false;
2480
2481 // check if layer path is inside one of the standard temporary file locations for this platform
2482 const QStringList tempPaths = QStandardPaths::standardLocations( QStandardPaths::TempLocation );
2483 for ( const QString &tempPath : tempPaths )
2484 {
2485 if ( path.startsWith( tempPath ) )
2486 return true;
2487 }
2488
2489 return false;
2490}
2491
2492void QgsMapLayer::setValid( bool valid )
2493{
2495
2496 if ( mValid == valid )
2497 return;
2498
2499 mValid = valid;
2500 emit isValidChanged();
2501}
2502
2504{
2506
2507 if ( legend == mLegend )
2508 return;
2509
2510 delete mLegend;
2511 mLegend = legend;
2512
2513 if ( mLegend )
2514 {
2515 mLegend->setParent( this );
2516 connect( mLegend, &QgsMapLayerLegend::itemsChanged, this, &QgsMapLayer::legendChanged, Qt::UniqueConnection );
2517 }
2518
2519 emit legendChanged();
2520}
2521
2523{
2525
2526 return mLegend;
2527}
2528
2530{
2532
2533 return mStyleManager;
2534}
2535
2537{
2539
2540 if ( renderer == m3DRenderer )
2541 return;
2542
2543 delete m3DRenderer;
2544 m3DRenderer = renderer;
2545 emit renderer3DChanged();
2546 emit repaintRequested();
2548}
2549
2551{
2553
2554 return m3DRenderer;
2555}
2556
2557void QgsMapLayer::triggerRepaint( bool deferredUpdate )
2558{
2560
2561 if ( mRepaintRequestedFired )
2562 return;
2563 mRepaintRequestedFired = true;
2564 emit repaintRequested( deferredUpdate );
2565 mRepaintRequestedFired = false;
2566}
2567
2574
2576{
2578
2579 mMetadata = metadata;
2580// mMetadata.saveToLayer( this );
2581 emit metadataChanged();
2582}
2583
2585{
2587
2588 return QString();
2589}
2590
2591QDateTime QgsMapLayer::timestamp() const
2592{
2594
2595 return QDateTime();
2596}
2597
2605
2607{
2609
2610 updateExtent( extent );
2611}
2612
2613bool QgsMapLayer::isReadOnly() const
2614{
2616
2617 return true;
2618}
2619
2621{
2623
2624 return mOriginalXmlProperties;
2625}
2626
2627void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
2628{
2630
2631 mOriginalXmlProperties = originalXmlProperties;
2632}
2633
2634QString QgsMapLayer::generateId( const QString &layerName )
2635{
2636 // Generate the unique ID of this layer
2637 const QString uuid = QUuid::createUuid().toString();
2638 // trim { } from uuid
2639 QString id = layerName + '_' + uuid.mid( 1, uuid.length() - 2 );
2640 // Tidy the ID up to avoid characters that may cause problems
2641 // elsewhere (e.g in some parts of XML). Replaces every non-word
2642 // character (word characters are the alphabet, numbers and
2643 // underscore) with an underscore.
2644 // Note that the first backslash in the regular expression is
2645 // there for the compiler, so the pattern is actually \W
2646 const thread_local QRegularExpression idRx( QStringLiteral( "[\\W]" ) );
2647 id.replace( idRx, QStringLiteral( "_" ) );
2648 return id;
2649}
2650
2652{
2654
2655 return true;
2656}
2657
2659{
2661
2662 return mapTipsEnabled() && !mMapTipTemplate.isEmpty();
2663}
2664
2665void QgsMapLayer::setProviderType( const QString &providerType )
2666{
2668
2670}
2671
2672QSet<QgsMapLayerDependency> QgsMapLayer::dependencies() const
2673{
2675
2676 return mDependencies;
2677}
2678
2679bool QgsMapLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
2680{
2682
2683 QSet<QgsMapLayerDependency> deps;
2684 const auto constODeps = oDeps;
2685 for ( const QgsMapLayerDependency &dep : constODeps )
2686 {
2687 if ( dep.origin() == QgsMapLayerDependency::FromUser )
2688 deps << dep;
2689 }
2690
2691 mDependencies = deps;
2692 emit dependenciesChanged();
2693 return true;
2694}
2695
2697{
2699
2700 QgsDataProvider *lDataProvider = dataProvider();
2701
2702 if ( !lDataProvider )
2703 return;
2704
2705 if ( enabled && !isRefreshOnNotifyEnabled() )
2706 {
2707 lDataProvider->setListening( enabled );
2708 connect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
2709 }
2710 else if ( !enabled && isRefreshOnNotifyEnabled() )
2711 {
2712 // we don't want to disable provider listening because someone else could need it (e.g. actions)
2713 disconnect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
2714 }
2715 mIsRefreshOnNofifyEnabled = enabled;
2716}
2717
2719{
2721
2722 if ( QgsMapLayerStore *store = qobject_cast<QgsMapLayerStore *>( parent() ) )
2723 {
2724 return qobject_cast<QgsProject *>( store->parent() );
2725 }
2726 return nullptr;
2727}
2728
2729void QgsMapLayer::onNotified( const QString &message )
2730{
2732
2733 if ( refreshOnNotifyMessage().isEmpty() || refreshOnNotifyMessage() == message )
2734 {
2736 emit dataChanged();
2737 }
2738}
2739
2740QgsRectangle QgsMapLayer::wgs84Extent( bool forceRecalculate ) const
2741{
2743
2745
2746 if ( ! forceRecalculate && ! mWgs84Extent.isNull() )
2747 {
2748 wgs84Extent = mWgs84Extent;
2749 }
2750 else if ( ! mExtent.isNull() )
2751 {
2753 transformer.setBallparkTransformsAreAppropriate( true );
2754 try
2755 {
2756 wgs84Extent = transformer.transformBoundingBox( mExtent );
2757 }
2758 catch ( const QgsCsException &cse )
2759 {
2760 QgsMessageLog::logMessage( tr( "Error transforming extent: %1" ).arg( cse.what() ) );
2762 }
2763 }
2764 return wgs84Extent;
2765}
2766
2767void QgsMapLayer::updateExtent( const QgsRectangle &extent ) const
2768{
2770
2771 if ( extent == mExtent )
2772 return;
2773
2774 mExtent = extent;
2775
2776 // do not update the wgs84 extent if we trust layer metadata
2778 return;
2779
2780 mWgs84Extent = wgs84Extent( true );
2781}
2782
2784{
2786
2787 // do not update the wgs84 extent if we trust layer metadata
2789 return;
2790
2791 mWgs84Extent = QgsRectangle();
2792}
2793
2795{
2797
2798 QString metadata = QStringLiteral( "<h1>" ) + tr( "General" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );
2799
2800 // name
2801 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + name() + QStringLiteral( "</td></tr>\n" );
2802
2803 QString path;
2804 bool isLocalPath = false;
2805 if ( dataProvider() )
2806 {
2807 // local path
2808 QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( dataProvider()->name(), publicSource() );
2809 if ( uriComponents.contains( QStringLiteral( "path" ) ) )
2810 {
2811 path = uriComponents[QStringLiteral( "path" )].toString();
2812 QFileInfo fi( path );
2813 if ( fi.exists() )
2814 {
2815 isLocalPath = true;
2816 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Path" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( path ).toString(), QDir::toNativeSeparators( path ) ) ) + QStringLiteral( "</td></tr>\n" );
2817
2818 QDateTime lastModified = fi.lastModified();
2819 QString lastModifiedFileName;
2820 QSet<QString> sidecarFiles = QgsFileUtils::sidecarFilesForPath( path );
2821 if ( fi.isFile() )
2822 {
2823 qint64 fileSize = fi.size();
2824 if ( !sidecarFiles.isEmpty() )
2825 {
2826 lastModifiedFileName = fi.fileName();
2827 QStringList sidecarFileNames;
2828 for ( const QString &sidecarFile : sidecarFiles )
2829 {
2830 QFileInfo sidecarFi( sidecarFile );
2831 fileSize += sidecarFi.size();
2832 if ( sidecarFi.lastModified() > lastModified )
2833 {
2834 lastModified = sidecarFi.lastModified();
2835 lastModifiedFileName = sidecarFi.fileName();
2836 }
2837 sidecarFileNames << sidecarFi.fileName();
2838 }
2839 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( sidecarFiles.size() > 1 ? tr( "Sidecar files" ) : tr( "Sidecar file" ) ) + QStringLiteral( "</td><td>%1" ).arg( sidecarFileNames.join( QLatin1String( ", " ) ) ) + QStringLiteral( "</td></tr>\n" );
2840 }
2841 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + ( !sidecarFiles.isEmpty() ? tr( "Total size" ) : tr( "Size" ) ) + QStringLiteral( "</td><td>%1" ).arg( QgsFileUtils::representFileSize( fileSize ) ) + QStringLiteral( "</td></tr>\n" );
2842 }
2843 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Last modified" ) + QStringLiteral( "</td><td>%1" ).arg( QLocale().toString( fi.lastModified() ) ) + ( !lastModifiedFileName.isEmpty() ? QStringLiteral( " (%1)" ).arg( lastModifiedFileName ) : QString() ) + QStringLiteral( "</td></tr>\n" );
2844 }
2845 }
2846 if ( uriComponents.contains( QStringLiteral( "url" ) ) )
2847 {
2848 const QString url = uriComponents[QStringLiteral( "url" )].toString();
2849 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "URL" ) + QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl( url ).toString(), url ) ) + QStringLiteral( "</td></tr>\n" );
2850 }
2851 }
2852
2853 // data source
2854 if ( publicSource() != path || !isLocalPath )
2855 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Source" ) + QStringLiteral( "</td><td>%1" ).arg( publicSource() != path ? publicSource() : path ) + QStringLiteral( "</td></tr>\n" );
2856
2857 // provider
2858 if ( dataProvider() )
2859 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Provider" ) + QStringLiteral( "</td><td>%1" ).arg( dataProvider()->name() ) + QStringLiteral( "</td></tr>\n" );
2860
2861 metadata += QLatin1String( "</table>\n<br><br>" );
2862 return metadata;
2863}
2864
2866{
2868
2869 QString metadata = QStringLiteral( "<h1>" ) + tr( "Coordinate Reference System (CRS)" ) + QStringLiteral( "</h1>\n<hr>\n" );
2870 metadata += QLatin1String( "<table class=\"list-view\">\n" );
2871
2872 // Identifier
2874 if ( !c.isValid() )
2875 metadata += QStringLiteral( "<tr><td colspan=\"2\" class=\"highlight\">" ) + tr( "Unknown" ) + QStringLiteral( "</td></tr>\n" );
2876 else
2877 {
2878 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + c.userFriendlyIdentifier( QgsCoordinateReferenceSystem::FullString ) + QStringLiteral( "</td></tr>\n" );
2879
2880 // map units
2881 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Units" ) + QStringLiteral( "</td><td>" )
2882 + ( c.isGeographic() ? tr( "Geographic (uses latitude and longitude for coordinates)" ) : QgsUnitTypes::toString( c.mapUnits() ) )
2883 + QStringLiteral( "</td></tr>\n" );
2884
2885 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Type" ) + QStringLiteral( "</td><td>" ) + QgsCoordinateReferenceSystemUtils::crsTypeToString( c.type() ) + QStringLiteral( "</td></tr>\n" );
2886
2887 // operation
2888 const QgsProjOperation operation = c.operation();
2889 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Method" ) + QStringLiteral( "</td><td>" ) + operation.description() + QStringLiteral( "</td></tr>\n" );
2890
2891 // celestial body
2892 try
2893 {
2894 const QString celestialBody = c.celestialBodyName();
2895 if ( !celestialBody.isEmpty() )
2896 {
2897 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Celestial Body" ) + QStringLiteral( "</td><td>" ) + celestialBody + QStringLiteral( "</td></tr>\n" );
2898 }
2899 }
2900 catch ( QgsNotSupportedException & )
2901 {
2902
2903 }
2904
2905 QString accuracyString;
2906 // dynamic crs with no epoch?
2907 if ( c.isDynamic() && std::isnan( c.coordinateEpoch() ) )
2908 {
2909 accuracyString = tr( "Based on a dynamic CRS, but no coordinate epoch is set. Coordinates are ambiguous and of limited accuracy." );
2910 }
2911
2912 // based on datum ensemble?
2913 try
2914 {
2915 const QgsDatumEnsemble ensemble = c.datumEnsemble();
2916 if ( ensemble.isValid() )
2917 {
2918 QString id;
2919 if ( !ensemble.code().isEmpty() )
2920 id = QStringLiteral( "<i>%1</i> (%2:%3)" ).arg( ensemble.name(), ensemble.authority(), ensemble.code() );
2921 else
2922 id = QStringLiteral( "<i>%</i>”" ).arg( ensemble.name() );
2923
2924 if ( ensemble.accuracy() > 0 )
2925 {
2926 accuracyString = tr( "Based on %1, which has a limited accuracy of <b>at best %2 meters</b>." ).arg( id ).arg( ensemble.accuracy() );
2927 }
2928 else
2929 {
2930 accuracyString = tr( "Based on %1, which has a limited accuracy." ).arg( id );
2931 }
2932 }
2933 }
2934 catch ( QgsNotSupportedException & )
2935 {
2936
2937 }
2938
2939 if ( !accuracyString.isEmpty() )
2940 {
2941 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Accuracy" ) + QStringLiteral( "</td><td>" ) + accuracyString + QStringLiteral( "</td></tr>\n" );
2942 }
2943
2944 // static/dynamic
2945 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Reference" ) + QStringLiteral( "</td><td>%1</td></tr>\n" ).arg( c.isDynamic() ? tr( "Dynamic (relies on a datum which is not plate-fixed)" ) : tr( "Static (relies on a datum which is plate-fixed)" ) );
2946
2947 // coordinate epoch
2948 if ( !std::isnan( c.coordinateEpoch() ) )
2949 {
2950 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Coordinate Epoch" ) + QStringLiteral( "</td><td>%1</td></tr>\n" ).arg( c.coordinateEpoch() );
2951 }
2952 }
2953
2954 metadata += QLatin1String( "</table>\n<br><br>\n" );
2955 return metadata;
2956}
static QString version()
Version string.
Definition qgis.cpp:258
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition qgis.h:4065
@ ForceFirstLetterToCapital
Convert just the first letter of each word to uppercase, leave the rest untouched.
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:255
@ Unknown
Unknown types.
LayerType
Types of layers that can be added to a map.
Definition qgis.h:114
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
AutoRefreshMode
Map layer properties.
Definition qgis.h:1667
@ RedrawOnly
Redraw current data only.
@ ReloadData
Reload data (and draw the new data)
@ Disabled
Automatic refreshing is disabled.
Base metadata class for 3D renderers.
virtual QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context)=0
Returns new instance of the renderer given the DOM element.
Qgs3DRendererAbstractMetadata * rendererMetadata(const QString &type) const
Returns metadata for a 3D renderer type (may be used to create a new instance of the type)
Base class for all renderers that may to participate in 3D view.
virtual QString type() const =0
Returns unique identifier of the renderer class (used to identify subclass)
virtual void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const =0
Writes renderer's properties to given XML element.
virtual void resolveReferences(const QgsProject &project)
Resolves references to other objects - second phase of loading - after readXml()
static QString pkgDataPath()
Returns the common root path of all application data directories.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static Qgs3DRendererRegistry * renderer3DRegistry()
Returns registry of available 3D renderers.
bool setMasterPassword(bool verify=false)
Main call to initially set or continually check master password is set.
static QString crsTypeToString(Qgis::CrsType type)
Returns a translated string representing a CRS type.
This class represents a coordinate reference system (CRS).
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
void validate()
Perform some validation on this CRS.
static CUSTOM_CRS_VALIDATION customCrsValidation()
Gets custom function.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
static void setCustomCrsValidation(CUSTOM_CRS_VALIDATION f)
Sets custom function to force valid CRS.
void setValidationHint(const QString &html)
Set user hint for validation.
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
Contains information about the context in which a coordinate transform is executed.
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
Abstract base class for spatial data provider implementations.
@ FlagLoadDefaultStyle
Reset the layer's style to the default for the datasource.
@ FlagTrustDataSource
Trust datasource config (primary key unicity, geometry type and srid, etc). Improves provider load ti...
@ ForceReadOnly
Open layer in a read-only mode (since QGIS 3.28)
@ SkipGetExtent
Skip the extent from provider.
void notify(const QString &msg)
Emitted when the datasource issues a notification.
static QString removePassword(const QString &aUri, bool hide=false)
Removes the password element from a URI.
Contains information about a datum ensemble.
Definition qgsdatums.h:95
QString code() const
Identification code, e.g.
Definition qgsdatums.h:122
QString authority() const
Authority name, e.g.
Definition qgsdatums.h:117
bool isValid() const
Returns true if the datum ensemble is a valid object, or false if it is a null/invalid object.
Definition qgsdatums.h:102
QString name() const
Display name of datum ensemble.
Definition qgsdatums.h:107
double accuracy() const
Positional accuracy (in meters).
Definition qgsdatums.h:112
QgsError is container for error messages (report).
Definition qgserror.h:81
QString what() const
static QSet< QString > sidecarFilesForPath(const QString &path)
Returns a list of the sidecar files which exist for the dataset a the specified path.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
A structured metadata store for a map layer.
bool readMetadataXml(const QDomElement &metadataElement) override
Sets state from DOM document.
bool writeMetadataXml(QDomElement &metadataElement, QDomDocument &document) const override
Stores state in a DOM node.
static void setLayerNotes(QgsMapLayer *layer, const QString &notes)
Sets the notes for the specified layer, where notes is a HTML formatted string.
static bool layerHasNotes(const QgsMapLayer *layer)
Returns true if the specified layer has notes available.
static QString layerNotes(const QgsMapLayer *layer)
Returns the notes for the specified layer.
This class models dependencies with or between map layers.
Base class for storage of map layer elevation properties.
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer.
void itemsChanged()
Emitted when existing items/nodes got invalid and should be replaced by new ones.
Manages QGIS Server properties for a map layer.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
void copyTo(QgsMapLayerServerProperties *properties) const
Copy properties to another instance.
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Management of styles for use with one map layer.
bool addStyle(const QString &name, const QgsMapLayerStyle &style)
Add a style with given name and data.
QStringList styles() const
Returns list of all defined style names.
void writeXml(QDomElement &mgrElement) const
Write configuration (for project saving)
void reset()
Reset the style manager to a basic state - with one default style which is set as current.
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
QgsMapLayerStyle style(const QString &name) const
Returns data of a stored style - accessed by its unique name.
void readXml(const QDomElement &mgrElement)
Read configuration (for project loading)
Base class for storage of map layer temporal properties.
Base class for all map layer types.
Definition qgsmaplayer.h:74
QString mKeywordList
void setShortName(const QString &shortName)
Sets the short name of the layer used by QGIS Server to identify the layer.
virtual bool deleteStyleFromDatabase(const QString &styleId, QString &msgError)
Deletes a style from the database.
bool importNamedMetadata(QDomDocument &document, QString &errorMessage)
Import the metadata of this layer from a QDomDocument.
QString name
Definition qgsmaplayer.h:77
void readStyleManager(const QDomNode &layerNode)
Read style manager's configuration (if any). To be called by subclasses.
virtual bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const =0
Write the style for the layer into the document provided.
QString legendUrlFormat() const
Returns the format for a URL based layer legend.
QgsRectangle wgs84Extent(bool forceRecalculate=false) const
Returns the WGS84 extent (EPSG:4326) of the layer according to ReadFlag::FlagTrustLayerMetadata.
void setRefreshOnNotifyEnabled(bool enabled)
Set whether provider notification is connected to triggerRepaint.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QgsAbstract3DRenderer * renderer3D() const
Returns 3D renderer associated with the layer.
virtual bool isTemporary() const
Returns true if the layer is considered a temporary layer.
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context=QgsReadWriteContext(), QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) const
Export the properties of this layer as named style in a QDomDocument.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the layer.
void dependenciesChanged()
Emitted when dependencies are changed.
virtual bool hasMapTips() const
Returns true if the layer contains map tips.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
void legendChanged()
Signal emitted when legend of the layer has changed.
void writeStyleManager(QDomNode &layerNode, QDomDocument &doc) const
Write style manager's configuration (if exists). To be called by subclasses.
QgsMapLayerLegend * legend() const
Can be nullptr.
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Import the properties of this layer from a QDomDocument.
void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
void metadataChanged()
Emitted when the layer's metadata is changed.
virtual QgsRectangle extent() const
Returns the extent of the layer.
virtual QString saveSldStyle(const QString &uri, bool &resultFlag) const
Saves the properties of this layer to an SLD format file.
QString source() const
Returns the source for the layer.
void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer's legend.
virtual bool setDependencies(const QSet< QgsMapLayerDependency > &layers)
Sets the list of dependencies.
void request3DUpdate()
Signal emitted when a layer requires an update in any 3D maps.
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QgsError mError
Error.
int mBlockStyleChangedSignal
If non-zero, the styleChanged signal should not be emitted.
QString providerType() const
Returns the provider type (provider key) for this layer.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
Qgis::AutoRefreshMode autoRefreshMode() const
Returns the layer's automatical refresh mode.
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
void configChanged()
Emitted whenever the configuration is changed.
void trigger3DUpdate()
Will advise any 3D maps that this layer requires to be updated in the scene.
void autoRefreshIntervalChanged(int interval)
Emitted when the auto refresh interval changes.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
virtual QSet< QgsMapLayerDependency > dependencies() const
Gets the list of dependencies.
void setCustomProperties(const QgsObjectCustomProperties &properties)
Set custom properties for layer.
virtual QString encodedSource(const QString &source, const QgsReadWriteContext &context) const
Called by writeLayerXML(), used by derived classes to encode provider's specific data source to proje...
QgsMapLayer::LayerFlags flags() const
Returns the flags for this layer.
virtual void setSubLayerVisibility(const QString &name, bool visible)
Set the visibility of the given sublayer name.
void isValidChanged()
Emitted when the validity of this layer changed.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:80
bool loadNamedMetadataFromDatabase(const QString &db, const QString &uri, QString &qmd)
Retrieve a named metadata for this layer from a sqlite database.
virtual bool readXml(const QDomNode &layer_node, QgsReadWriteContext &context)
Called by readLayerXML(), used by children to read state specific to them from project files.
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the map layer.
QString attribution() const
Returns the attribution of the layer used by QGIS Server in GetCapabilities request.
void setOriginalXmlProperties(const QString &originalXmlProperties)
Sets the original XML properties for the layer to originalXmlProperties.
void writeCustomProperties(QDomNode &layerNode, QDomDocument &doc) const
Write custom properties to project file.
QString mRefreshOnNofifyMessage
QString mLegendUrl
WMS legend.
virtual int listStylesInDatabase(QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError)
Lists all the style in db split into related to the layer and not related to.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
virtual QString loadDefaultStyle(bool &resultFlag)
Retrieve the default style for this layer if one exists (either as a .qml file on disk or as a record...
QString mLayerName
Name of the layer - used for display.
virtual QString loadNamedMetadata(const QString &uri, bool &resultFlag)
Retrieve a named metadata for this layer if one exists (either as a .qmd file on disk or as a record ...
virtual bool writeXml(QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context) const
Called by writeLayerXML(), used by children to write state specific to them to project files.
Q_DECL_DEPRECATED bool hasAutoRefreshEnabled() const
Returns true if auto refresh is enabled for the layer.
void mapTipTemplateChanged()
Emitted when the map tip template changes.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
QString crsHtmlMetadata() const
Returns a HTML fragment containing the layer's CRS metadata, for use in the htmlMetadata() method.
void setAttributionUrl(const QString &attribUrl)
Sets the attribution URL of the layer used by QGIS Server in GetCapabilities request.
Q_DECL_DEPRECATED void setAutoRefreshEnabled(bool enabled)
Sets whether auto refresh is enabled for the layer.
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
QgsLayerMetadata metadata
Definition qgsmaplayer.h:79
static QString formatLayerName(const QString &name)
A convenience function to capitalize and format a layer name.
void renderer3DChanged()
Signal emitted when 3D renderer associated with the layer has changed.
QString abstract() const
Returns the abstract of the layer used by QGIS Server in GetCapabilities request.
QgsMapLayer(Qgis::LayerType type=Qgis::LayerType::Vector, const QString &name=QString(), const QString &source=QString())
Constructor for QgsMapLayer.
QString originalXmlProperties() const
Returns the XML properties of the original layer as they were when the layer was first read from the ...
Qgis::LayerType type
Definition qgsmaplayer.h:81
QString dataUrlFormat() const
Returns the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
QString mTitle
void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
void setKeywordList(const QString &keywords)
Sets the keyword list of the layer used by QGIS Server in GetCapabilities request.
void setAttribution(const QString &attrib)
Sets the attribution of the layer used by QGIS Server in GetCapabilities request.
void setFlags(QgsMapLayer::LayerFlags flags)
Returns the flags for this layer.
bool isRefreshOnNotifyEnabled() const
Returns true if the refresh on provider nofification is enabled.
QString publicSource(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
QSet< QgsMapLayerDependency > mDependencies
List of layers that may modify this layer on modification.
void readCustomProperties(const QDomNode &layerNode, const QString &keyStartsWith=QString())
Read custom properties from project file.
virtual Qgis::MapLayerProperties properties() const
Returns the map layer properties of this layer.
virtual QString loadSldStyle(const QString &uri, bool &resultFlag)
Attempts to style the layer using the formatting from an SLD type file.
virtual void setMetadata(const QgsLayerMetadata &metadata)
Sets the layer's metadata store.
virtual bool readStyle(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read the style for the current layer from the DOM node supplied.
virtual QString saveDefaultMetadata(bool &resultFlag)
Save the current metadata of this layer as the default metadata (either as a .qmd file on disk or as ...
virtual bool supportsEditing() const
Returns whether the layer supports editing or not.
void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
QString mLegendUrlFormat
virtual void saveStyleToDatabase(const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Saves named and sld style of the layer to the style table in the db.
Q_INVOKABLE void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
QString mProviderKey
Data provider key (name of the data provider)
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
void styleChanged()
Signal emitted whenever a change affects the layer's style.
virtual bool isEditable() const
Returns true if the layer can be edited.
QUndoStack * undoStack()
Returns pointer to layer's undo stack.
std::unique_ptr< QgsDataProvider > mPreloadedProvider
Optionally used when loading a project, it is released when the layer is effectively created.
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
void crsChanged()
Emit a signal that layer's CRS has been reset.
virtual QgsError error() const
Gets current status error.
bool writeLayerXml(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores state in DOM node.
virtual QString styleURI() const
Retrieve the style URI for this layer (either as a .qml file on disk or as a record in the users styl...
QString mAttributionUrl
void setScaleBasedVisibility(bool enabled)
Sets whether scale based visibility is enabled for the layer.
void dataSourceChanged()
Emitted whenever the layer's data source has been changed.
QString dataUrl() const
Returns the DataUrl of the layer used by QGIS Server in GetCapabilities request.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
Q_DECL_DEPRECATED QString metadataUrlFormat() const
Returns the metadata format of the layer used by QGIS Server in GetCapabilities request.
void setRefreshOnNofifyMessage(const QString &message)
Set the notification message that triggers repaint If refresh on notification is enabled,...
static QString generateId(const QString &layerName)
Generates an unique identifier for this layer, the generate ID is prefixed by layerName.
void opacityChanged(double opacity)
Emitted when the layer's opacity is changed, where opacity is a value between 0 (transparent) and 1 (...
virtual bool isModified() const
Returns true if the layer has been modified since last commit/save.
virtual QString loadNamedStyle(const QString &theURI, bool &resultFlag, bool loadFromLocalDb, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Loads a named style from file/local db/datasource db.
void styleLoaded(QgsMapLayer::StyleCategories categories)
Emitted when a style has been loaded.
virtual QString getStyleFromDatabase(const QString &styleId, QString &msgError)
Returns the named style corresponding to style id provided.
void emitStyleChanged()
Triggers an emission of the styleChanged() signal.
virtual QgsMapLayerTemporalProperties * temporalProperties()
Returns the layer's temporal properties.
QString mShortName
QUndoStack * undoStackStyles()
Returns pointer to layer's style undo stack.
void dataChanged()
Data of layer changed.
virtual QStringList subLayers() const
Returns the sublayers of this layer.
virtual QString htmlMetadata() const
Obtain a formatted HTML string containing assorted metadata for this layer.
Q_DECL_DEPRECATED void setMetadataUrlFormat(const QString &metaUrlFormat)
Sets the metadata format of the layer used by QGIS Server in GetCapabilities request.
virtual bool loadNamedStyleFromDatabase(const QString &db, const QString &uri, QString &qml)
Retrieve a named style for this layer from a sqlite database.
static QString extensionPropertyType(PropertyType type)
Returns the extension of a Property.
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
void blendModeChanged(QPainter::CompositionMode blendMode)
Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode()
virtual QString saveSldStyleV2(bool &resultFlag, const QgsSldExportContext &exportContext) const
Saves the properties of this layer to an SLD format file.
void setName(const QString &name)
Set the display name of the layer.
void setAutoRefreshInterval(int interval)
Sets the auto refresh interval (in milliseconds) for the layer.
virtual bool readSymbology(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)=0
Read the symbology for the current layer from the DOM node supplied.
Q_DECL_DEPRECATED QString metadataUrl() const
Returns the metadata URL of the layer used by QGIS Server in GetCapabilities request.
virtual void setExtent(const QgsRectangle &rect)
Sets the extent.
virtual void resolveReferences(QgsProject *project)
Resolve references to other layers (kept as layer IDs after reading XML) into layer objects.
QString saveNamedMetadata(const QString &uri, bool &resultFlag)
Save the current metadata of this layer as a named metadata (either as a .qmd file on disk or as a re...
QString mDataSource
Data source description string, varies by layer type.
void setAutoRefreshMode(Qgis::AutoRefreshMode mode)
Sets the automatic refresh mode for the layer.
QString refreshOnNotifyMessage() const
Returns the message that should be notified by the provider to triggerRepaint.
virtual bool readSld(const QDomNode &node, QString &errorMessage)
void setMapTipsEnabled(bool enabled)
Enable or disable map tips for this layer.
virtual QString loadDefaultMetadata(bool &resultFlag)
Retrieve the default metadata for this layer if one exists (either as a .qmd file on disk or as a rec...
@ FlagReadExtentFromXml
Read extent from xml and skip get extent from provider.
@ FlagTrustLayerMetadata
Trust layer metadata. Improves layer load time by skipping expensive checks like primary key unicity,...
@ FlagForceReadOnly
Force open as read only.
void setValid(bool valid)
Sets whether layer is valid or not.
QString attributionUrl() const
Returns the attribution URL of the layer used by QGIS Server in GetCapabilities request.
void readCommonStyle(const QDomElement &layerElement, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read style data common to all layer types.
QString mAbstract
Description of the layer.
void customPropertyChanged(const QString &key)
Emitted when a custom property of the layer has been changed or removed.
QgsMapLayer::ReadFlags mReadFlags
Read flags. It's up to the subclass to respect these when restoring state from XML.
void setDataSource(const QString &dataSource, const QString &baseName, const QString &provider, bool loadDefaultStyleFlag=false)
Updates the data source of the layer.
double minimumScale() const
Returns the minimum map scale (i.e.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
QString legendUrl() const
Returns the URL for the layer's legend.
QString mDataUrlFormat
void flagsChanged()
Emitted when layer's flags have been modified.
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
void setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
void exportNamedMetadata(QDomDocument &doc, QString &errorMsg) const
Export the current metadata of this layer as named metadata in a QDomDocument.
virtual QString saveNamedStyle(const QString &uri, bool &resultFlag, StyleCategories categories=AllStyleCategories)
Save the properties of this layer as a named style (either as a .qml file on disk or as a record in t...
virtual void exportSldStyle(QDomDocument &doc, QString &errorMsg) const
Export the properties of this layer as SLD style in a QDomDocument.
void beforeResolveReferences(QgsProject *project)
Emitted when all layers are loaded and references can be resolved, just before the references of this...
void setMapTipTemplate(const QString &mapTipTemplate)
The mapTip is a pretty, html representation for feature information.
Q_DECL_DEPRECATED void setMetadataUrl(const QString &metaUrl)
Sets the metadata URL of the layer used by QGIS Server in GetCapabilities request.
virtual QgsMapLayerElevationProperties * elevationProperties()
Returns the layer's elevation properties.
Q_INVOKABLE QStringList customPropertyKeys() const
Returns list of all keys within custom properties.
QgsProject * project() const
Returns the parent project if this map layer is added to a project.
Q_DECL_DEPRECATED void setMetadataUrlType(const QString &metaUrlType)
Set the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
bool mapTipsEnabled
Definition qgsmaplayer.h:85
bool readLayerXml(const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags=QgsMapLayer::ReadFlags(), QgsDataProvider *preloadedProvider=nullptr)
Sets state from DOM document.
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
double opacity
Definition qgsmaplayer.h:83
virtual QString decodedSource(const QString &source, const QString &dataProvider, const QgsReadWriteContext &context) const
Called by readLayerXML(), used by derived classes to decode provider's specific data source from proj...
void nameChanged()
Emitted when the name has been changed.
virtual QString metadataUri() const
Retrieve the metadata URI for this layer (either as a .qmd file on disk or as a record in the users s...
int autoRefreshInterval
Definition qgsmaplayer.h:78
virtual bool writeStyle(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write just the symbology information for the layer into the document.
bool mIsRefreshOnNofifyEnabled
QString mDataUrl
DataUrl of the layer.
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
double mLayerOpacity
Layer opacity.
bool mValid
Indicates if the layer is valid and can be drawn.
@ LayerConfiguration
General configuration: identifiable, removable, searchable, display expression, read-only.
@ Symbology
Symbology.
@ Notes
Layer user notes (since QGIS 3.20)
@ Temporal
Temporal properties (since QGIS 3.14)
@ Rendering
Rendering: scale visibility, simplify method, opacity.
@ Elevation
Elevation settings (since QGIS 3.18)
@ Symbology3D
3D symbology
@ CustomProperties
Custom properties (by plugins for instance)
virtual Q_INVOKABLE void reload()
Synchronises with changes in the datasource.
virtual QDateTime timestamp() const
Time stamp of data source in the moment when data/metadata were loaded by provider.
void setProviderType(const QString &providerType)
Sets the providerType (provider key)
void mapTipsEnabledChanged()
Emitted when map tips are enabled or disabled for the layer.
virtual QString saveDefaultStyle(bool &resultFlag, StyleCategories categories)
Save the properties of this layer as the default style (either as a .qml file on disk or as a record ...
void setRenderer3D(QgsAbstract3DRenderer *renderer)
Sets 3D renderer for the layer.
QString mAttribution
Attribution of the layer.
~QgsMapLayer() override
const QgsObjectCustomProperties & customProperties() const
Read all custom properties from layer.
virtual void exportSldStyleV2(QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext) const
Export the properties of this layer as SLD style in a QDomDocument.
QString generalHtmlMetadata() const
Returns an HTML fragment containing general metadata information, for use in the htmlMetadata() metho...
Q_DECL_DEPRECATED QString metadataUrlType() const
Returns the metadata type of the layer used by QGIS Server in GetCapabilities request.
void writeCommonStyle(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write style data common to all layer types.
double maximumScale() const
Returns the maximum map scale (i.e.
QString keywordList() const
Returns the keyword list of the layer used by QGIS Server in GetCapabilities request.
virtual void setLayerOrder(const QStringList &layers)
Reorders the previously selected sublayers of this layer from bottom to top.
void invalidateWgs84Extent()
Invalidates the WGS84 extent.
QString mapTipTemplate
Definition qgsmaplayer.h:84
void setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
PropertyType
Maplayer has a style and a metadata property.
bool mShouldValidateCrs
true if the layer's CRS should be validated and invalid CRSes are not permitted.
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
static QgsDataProvider::ReadFlags providerReadFlags(const QDomNode &layerNode, QgsMapLayer::ReadFlags layerReadFlags)
Returns provider read flag deduced from layer read flags layerReadFlags and a dom node layerNode that...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Custom exception class which is raised when an operation is not supported.
Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML i...
void setValue(const QString &key, const QVariant &value)
Add an entry to the store with the specified key.
QStringList keys() const
Returns a list of all stored keys.
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Writes the store contents to an XML node.
void remove(const QString &key)
Removes a key (entry) from the store.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for the given key.
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from an XML node.
bool contains(const QString &key) const
Returns true if the properties contains a key with the specified name.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
Contains information about a PROJ operation.
QString description() const
Description.
Class to convert from older project file versions to newer.
bool updateRevision(const QgsProjectVersion &version)
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
The derived translate() translates with QTranslator and qm file the sourceText.
A class to describe the version of a project.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
bool removeAttachedFile(const QString &path)
Removes the attached file.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QString baseName() const
Returns the base name of the project file without the path and without extension - derived from fileN...
QString absoluteFilePath() const
Returns full absolute path to the project file if the project is stored in a file system - derived fr...
Holds data provider key, description, and associated shared library file or function pointer informat...
@ SaveLayerMetadata
Indicates that the provider supports saving native layer metadata (since QGIS 3.20)
QString getStyleById(const QString &providerKey, const QString &uri, const QString &styleId, QString &errCause)
Gets a layer style defined by styleId.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
bool saveLayerMetadata(const QString &providerKey, const QString &uri, const QgsLayerMetadata &metadata, QString &errorMessage)
Saves metadata to the layer corresponding to the specified uri.
bool deleteStyleById(const QString &providerKey, const QString &uri, const QString &styleId, QString &errCause)
Deletes a layer style defined by styleId.
QString loadStoredStyle(const QString &providerKey, const QString &uri, QString &styleName, QString &errCause)
Loads a layer style from the provider storage, reporting its name.
int listStyles(const QString &providerKey, const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause)
Lists stored layer styles in the provider defined by providerKey and uri.
bool saveStyle(const QString &providerKey, const QString &uri, const QString &qmlStyle, const QString &sldStyle, const QString &styleName, const QString &styleDescription, const QString &uiFileContent, bool useAsDefault, QString &errCause)
Saves a layer style to provider.
Represents a raster layer.
bool writeSld(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props=QVariantMap()) const
Writes the symbology of the layer into the document provided in SLD 1.0.0 format.
Allows entering a context category and takes care of leaving this category on deletion of the class.
The class is used as a container of context for various read/write operations on other objects.
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory(const QString &category, const QString &details=QString()) const
Push a category to the stack.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
A rectangle specified with double values.
bool isNull() const
Test if the rectangle is null (holding no spatial information).
void setMetadataUrls(const QList< QgsServerMetadataUrlProperties::MetadataUrl > &metaUrls)
Sets a the list of metadata URL for the layer.
QList< QgsServerMetadataUrlProperties::MetadataUrl > metadataUrls() const
Returns a list of metadataUrl resources associated for the layer.
The QgsSldExportContext class holds SLD export options and other information related to SLD export of...
QString exportFilePath() const
Returns the export file path for the SLD.
void setExportFilePath(const QString &exportFilePath)
Sets the export file path for the SLD to exportFilePath.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
An interface for classes which can visit style entity (e.g.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
Represents a vector layer which manages a vector based data sets.
bool writeSld(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props=QVariantMap()) const
Writes the symbology of the layer into the document provided in SLD 1.1 format.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
static T readFlagAttribute(const QDomElement &element, const QString &attributeName, T defaultValue)
Read a flag value from an attribute of the element.
Definition qgsxmlutils.h:99
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc, const QString &elementName=QStringLiteral("extent"))
Encodes a rectangle to a DOM element.
static QgsRectangle readRectangle(const QDomElement &element)
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
int open(const QString &path)
Opens the database at the specified file path.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:4555
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:4536
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:4332
CONSTLATIN1STRING geoEpsgCrsAuthId()
Geographic coord sys from EPSG authority.
Definition qgis.h:4828
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Setting options for creating vector data providers.
QString format
Format specification of online resource.