QGIS API Documentation 3.38.0-Grenoble (exported)
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, [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 QgsDebugMsgLevel( QStringLiteral( "Cloning layer '%1'" ).arg( name() ), 3 );
132 layer->setBlendMode( blendMode() );
133
134 const auto constStyles = styleManager()->styles();
135 for ( const QString &s : constStyles )
136 {
137 layer->styleManager()->addStyle( s, styleManager()->style( s ) );
138 }
139
140 layer->setName( name() );
141
142 if ( layer->dataProvider() && layer->dataProvider()->elevationProperties() )
143 {
145 layer->mExtent3D = mExtent3D;
146 else
147 layer->mExtent2D = mExtent2D;
148 }
149
150 layer->setMaximumScale( maximumScale() );
151 layer->setMinimumScale( minimumScale() );
153 layer->setLegendUrl( legendUrl() );
155 layer->setDependencies( dependencies() );
157 layer->setCrs( crs() );
158 layer->setCustomProperties( mCustomProperties );
159 layer->setOpacity( mLayerOpacity );
160 layer->setMetadata( mMetadata );
161 layer->serverProperties()->copyTo( mServerProperties.get() );
162}
163
165{
166 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
168
169 return mLayerType;
170}
171
178
180{
182
183 if ( flags == mFlags )
184 return;
185
186 mFlags = flags;
187 emit flagsChanged();
188}
189
196
197QString QgsMapLayer::id() const
198{
199 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
201
202 return mID;
203}
204
205bool QgsMapLayer::setId( const QString &id )
206{
208 if ( qobject_cast< QgsMapLayerStore * >( parent() ) )
209 {
210 // layer is already registered, cannot change id
211 return false;
212 }
213
214 if ( id == mID )
215 return false;
216
217 mID = id;
218 emit idChanged( id );
219 return true;
220}
221
222void QgsMapLayer::setName( const QString &name )
223{
225
226 if ( name == mLayerName )
227 return;
228
230
231 emit nameChanged();
232}
233
234QString QgsMapLayer::name() const
235{
236 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
238
239 QgsDebugMsgLevel( "returning name '" + mLayerName + '\'', 4 );
240 return mLayerName;
241}
242
249
251{
253
254 return nullptr;
255}
256
257void QgsMapLayer::setShortName( const QString &shortName )
258{
260
261 mServerProperties->setShortName( shortName );
262}
263
265{
267
268 return mServerProperties->shortName();
269}
270
271void QgsMapLayer::setTitle( const QString &title )
272{
274
275 mServerProperties->setTitle( title );
276}
277
278QString QgsMapLayer::title() const
279{
281
282 return mServerProperties->title();
283}
284
285void QgsMapLayer::setAbstract( const QString &abstract )
286{
288
289 mServerProperties->setAbstract( abstract );
290}
291
293{
295
296 return mServerProperties->abstract();
297}
298
299void QgsMapLayer::setKeywordList( const QString &keywords )
300{
302
303 mServerProperties->setKeywordList( keywords );
304}
305
307{
309
310 return mServerProperties->keywordList();
311}
312
313void QgsMapLayer::setDataUrl( const QString &dataUrl )
314{
316
317 mServerProperties->setDataUrl( dataUrl );
318}
319
320QString QgsMapLayer::dataUrl() const
321{
323
324 return mServerProperties->dataUrl();
325}
326
327void QgsMapLayer::setDataUrlFormat( const QString &dataUrlFormat )
328{
330
331 mServerProperties->setDataUrlFormat( dataUrlFormat );
332}
333
335{
337
338 return mServerProperties->dataUrlFormat();
339}
340
341void QgsMapLayer::setAttribution( const QString &attrib )
342{
344
345 mServerProperties->setAttribution( attrib );
346}
347
349{
351
352 return mServerProperties->attribution();
353}
354
355void QgsMapLayer::setAttributionUrl( const QString &attribUrl )
356{
358
359 mServerProperties->setAttributionUrl( attribUrl );
360}
361
363{
365
366 return mServerProperties->attributionUrl();
367
368}
369
370void QgsMapLayer::setMetadataUrl( const QString &metaUrl )
371{
373
374 QList<QgsMapLayerServerProperties::MetadataUrl> urls = serverProperties()->metadataUrls();
375 if ( urls.isEmpty() )
376 {
377 const QgsMapLayerServerProperties::MetadataUrl newItem = QgsMapLayerServerProperties::MetadataUrl( metaUrl, QLatin1String(), QLatin1String() );
378 urls.prepend( newItem );
379 }
380 else
381 {
382 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
383 const QgsMapLayerServerProperties::MetadataUrl newItem( metaUrl, old.type, old.format );
384 urls.prepend( newItem );
385 }
387}
388
390{
392
393 if ( mServerProperties->metadataUrls().isEmpty() )
394 {
395 return QLatin1String();
396 }
397 else
398 {
399 return mServerProperties->metadataUrls().first().url;
400 }
401}
402
403void QgsMapLayer::setMetadataUrlType( const QString &metaUrlType )
404{
406
407 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
408 if ( urls.isEmpty() )
409 {
410 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), metaUrlType, QLatin1String() );
411 urls.prepend( newItem );
412 }
413 else
414 {
415 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst();
416 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, metaUrlType, old.format );
417 urls.prepend( newItem );
418 }
419 mServerProperties->setMetadataUrls( urls );
420}
421
423{
425
426 if ( mServerProperties->metadataUrls().isEmpty() )
427 {
428 return QLatin1String();
429 }
430 else
431 {
432 return mServerProperties->metadataUrls().first().type;
433 }
434}
435
436void QgsMapLayer::setMetadataUrlFormat( const QString &metaUrlFormat )
437{
439
440 QList<QgsMapLayerServerProperties::MetadataUrl> urls = mServerProperties->metadataUrls();
441 if ( urls.isEmpty() )
442 {
443 const QgsMapLayerServerProperties::MetadataUrl newItem( QLatin1String(), QLatin1String(), metaUrlFormat );
444 urls.prepend( newItem );
445 }
446 else
447 {
448 const QgsMapLayerServerProperties::MetadataUrl old = urls.takeFirst( );
449 const QgsMapLayerServerProperties::MetadataUrl newItem( old.url, old.type, metaUrlFormat );
450 urls.prepend( newItem );
451 }
452 mServerProperties->setMetadataUrls( urls );
453}
454
456{
458
459 if ( mServerProperties->metadataUrls().isEmpty() )
460 {
461 return QString();
462 }
463 else
464 {
465 return mServerProperties->metadataUrls().first().format;
466 }
467}
468
469QString QgsMapLayer::publicSource( bool hidePassword ) const
470{
472
473 // Redo this every time we're asked for it, as we don't know if
474 // dataSource has changed.
475 QString safeName = QgsDataSourceUri::removePassword( mDataSource, hidePassword );
476 return safeName;
477}
478
479QString QgsMapLayer::source() const
480{
482
483 return mDataSource;
484}
485
487{
489
490 return mExtent2D.isNull() ? mExtent3D.toRectangle() : mExtent2D;
491}
492
494{
496
497 return mExtent3D;
498}
499
500void QgsMapLayer::setBlendMode( const QPainter::CompositionMode blendMode )
501{
503
504 if ( mBlendMode == blendMode )
505 return;
506
507 mBlendMode = blendMode;
510}
511
512QPainter::CompositionMode QgsMapLayer::blendMode() const
513{
514 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
516
517 return mBlendMode;
518}
519
520void QgsMapLayer::setOpacity( double opacity )
521{
523
525 return;
527 emit opacityChanged( opacity );
529}
530
532{
533 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
535
536 return mLayerOpacity;
537}
538
539bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags, QgsDataProvider *preloadedProvider )
540{
542
543 mPreloadedProvider.reset( preloadedProvider );
544
545 bool layerError;
547
548 QDomNode mnl;
549 QDomElement mne;
550
551 // read provider
552 QString provider;
553 mnl = layerElement.namedItem( QStringLiteral( "provider" ) );
554 mne = mnl.toElement();
555 provider = mne.text();
556
557 // set data source
558 mnl = layerElement.namedItem( QStringLiteral( "datasource" ) );
559 mne = mnl.toElement();
560 const QString dataSourceRaw = mne.text();
561 mDataSource = provider.isEmpty() ? dataSourceRaw : QgsProviderRegistry::instance()->relativeToAbsoluteUri( provider, dataSourceRaw, context );
562
563 // if the layer needs authentication, ensure the master password is set
564 const thread_local QRegularExpression rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
565 if ( rx.match( mDataSource ).hasMatch()
567 {
568 return false;
569 }
570
571 mDataSource = decodedSource( mDataSource, provider, context );
572
573 // Set the CRS from project file, asking the user if necessary.
574 // Make it the saved CRS to have WMS layer projected correctly.
575 // We will still overwrite whatever GDAL etc picks up anyway
576 // further down this function.
577 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
578 mne = mnl.toElement();
579
581 CUSTOM_CRS_VALIDATION savedValidation;
582
583 const QDomNode srsNode = layerElement.namedItem( QStringLiteral( "srs" ) );
584 mCRS.readXml( srsNode );
585 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) );
587 mCRS.validate();
588 savedCRS = mCRS;
589
590 // Do not validate any projections in children, they will be overwritten anyway.
591 // No need to ask the user for a projections when it is overwritten, is there?
594
595 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );
596
597 // the internal name is just the data source basename
598 //QFileInfo dataSourceFileInfo( mDataSource );
599 //internalName = dataSourceFileInfo.baseName();
600
601 // set ID
602 mnl = layerElement.namedItem( QStringLiteral( "id" ) );
603 if ( ! mnl.isNull() )
604 {
605 mne = mnl.toElement();
606 if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz)
607 {
608 const QString newId = mne.text();
609 if ( newId != mID )
610 {
611 mID = mne.text();
612 emit idChanged( mID );
613 }
614 }
615 }
616
617 // set name
618 mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
619 mne = mnl.toElement();
620
621 //name can be translated
622 setName( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
623
624 // now let the children grab what they need from the Dom node.
625 layerError = !readXml( layerElement, context );
626
627 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
628 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
629
630 // overwrite CRS with what we read from project file before the raster/vector
631 // file reading functions changed it. They will if projections is specified in the file.
632 // FIXME: is this necessary? Yes, it is (autumn 2019)
634 mCRS = savedCRS;
635
636 //vertical CRS
637 {
639 const QDomNode verticalCrsNode = layerElement.firstChildElement( QStringLiteral( "verticalCrs" ) );
640 if ( !verticalCrsNode.isNull() )
641 {
642 verticalCrs.readXml( verticalCrsNode );
643 }
644 mVerticalCrs = verticalCrs;
645 }
646 rebuildCrs3D();
647
648 //legendUrl
649 const QDomElement legendUrlElem = layerElement.firstChildElement( QStringLiteral( "legendUrl" ) );
650 if ( !legendUrlElem.isNull() )
651 {
652 mLegendUrl = legendUrlElem.text();
653 mLegendUrlFormat = legendUrlElem.attribute( QStringLiteral( "format" ), QString() );
654 }
655
656 serverProperties()->readXml( layerElement );
657
658 if ( serverProperties()->metadataUrls().isEmpty() )
659 {
660 // metadataUrl is still empty, maybe it's a QGIS Project < 3.22
661 // keep for legacy
662 const QDomElement metaUrlElem = layerElement.firstChildElement( QStringLiteral( "metadataUrl" ) );
663 if ( !metaUrlElem.isNull() )
664 {
665 const QString url = metaUrlElem.text();
666 const QString type = metaUrlElem.attribute( QStringLiteral( "type" ), QString() );
667 const QString format = metaUrlElem.attribute( QStringLiteral( "format" ), QString() );
668 const QgsMapLayerServerProperties::MetadataUrl newItem( url, type, format );
669 mServerProperties->setMetadataUrls( QList<QgsMapLayerServerProperties::MetadataUrl>() << newItem );
670 }
671 }
672
673 // mMetadata.readFromLayer( this );
674 const QDomElement metadataElem = layerElement.firstChildElement( QStringLiteral( "resourceMetadata" ) );
675 mMetadata.readMetadataXml( metadataElem );
676
677 setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), QStringLiteral( "0" ) ).toInt() );
678 if ( layerElement.hasAttribute( QStringLiteral( "autoRefreshMode" ) ) )
679 {
680 setAutoRefreshMode( qgsEnumKeyToValue( layerElement.attribute( QStringLiteral( "autoRefreshMode" ) ), Qgis::AutoRefreshMode::Disabled ) );
681 }
682 else
683 {
684 setAutoRefreshMode( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() ? Qgis::AutoRefreshMode::RedrawOnly : Qgis::AutoRefreshMode::Disabled );
685 }
686 setRefreshOnNofifyMessage( layerElement.attribute( QStringLiteral( "refreshOnNotifyMessage" ), QString() ) );
687 setRefreshOnNotifyEnabled( layerElement.attribute( QStringLiteral( "refreshOnNotifyEnabled" ), QStringLiteral( "0" ) ).toInt() );
688
689 // geographic extent is read only if necessary
691 {
692 const QDomNode wgs84ExtentNode = layerElement.namedItem( QStringLiteral( "wgs84extent" ) );
693 if ( !wgs84ExtentNode.isNull() )
694 mWgs84Extent = QgsXmlUtils::readRectangle( wgs84ExtentNode.toElement() );
695 }
696
697 mLegendPlaceholderImage = layerElement.attribute( QStringLiteral( "legendPlaceholderImage" ) );
698
699 if ( verticalCrs() != oldVerticalCrs )
700 emit verticalCrsChanged();
701 if ( mCrs3D != oldCrs3D )
702 emit crs3DChanged();
703
704 return ! layerError;
705} // bool QgsMapLayer::readLayerXML
706
707
708bool QgsMapLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
709{
711
712 Q_UNUSED( layer_node )
713 Q_UNUSED( context )
714 // NOP by default; children will over-ride with behavior specific to them
715
716 // read Extent
718 {
719 const QDomNode extent3DNode = layer_node.namedItem( QStringLiteral( "extent3D" ) );
720 if ( extent3DNode.isNull() )
721 {
722 const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
723 if ( !extentNode.isNull() )
724 {
725 mExtent2D = QgsXmlUtils::readRectangle( extentNode.toElement() );
726 }
727 }
728 else
729 {
730 mExtent3D = QgsXmlUtils::readBox3D( extent3DNode.toElement() );
731 }
732 }
733
734 return true;
735} // void QgsMapLayer::readXml
736
737
738bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const
739{
741
742 if ( !mExtent3D.isNull() && dataProvider() && dataProvider()->elevationProperties() && dataProvider()->elevationProperties()->containsElevationData() )
743 layerElement.appendChild( QgsXmlUtils::writeBox3D( mExtent3D, document ) );
744 else if ( !mExtent2D.isNull() )
745 layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent2D, document ) );
746
747 if ( const QgsRectangle lWgs84Extent = wgs84Extent( true ); !lWgs84Extent.isNull() )
748 {
749 layerElement.appendChild( QgsXmlUtils::writeRectangle( lWgs84Extent, document, QStringLiteral( "wgs84extent" ) ) );
750 }
751
752 layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer->interval() ) );
753 layerElement.setAttribute( QStringLiteral( "autoRefreshMode" ), qgsEnumValueToKey( mAutoRefreshMode ) );
754 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyEnabled" ), mIsRefreshOnNofifyEnabled ? 1 : 0 );
755 layerElement.setAttribute( QStringLiteral( "refreshOnNotifyMessage" ), mRefreshOnNofifyMessage );
756
757 // ID
758 QDomElement layerId = document.createElement( QStringLiteral( "id" ) );
759 const QDomText layerIdText = document.createTextNode( id() );
760 layerId.appendChild( layerIdText );
761
762 layerElement.appendChild( layerId );
763
764 if ( mVerticalCrs.isValid() )
765 {
766 QDomElement verticalSrsNode = document.createElement( QStringLiteral( "verticalCrs" ) );
767 mVerticalCrs.writeXml( verticalSrsNode, document );
768 layerElement.appendChild( verticalSrsNode );
769 }
770
771 // data source
772 QDomElement dataSource = document.createElement( QStringLiteral( "datasource" ) );
773 const QgsDataProvider *provider = dataProvider();
774 const QString providerKey = provider ? provider->name() : QString();
775 const QString srcRaw = encodedSource( source(), context );
776 const QString src = providerKey.isEmpty() ? srcRaw : QgsProviderRegistry::instance()->absoluteToRelativeUri( providerKey, srcRaw, context );
777 const QDomText dataSourceText = document.createTextNode( src );
778 dataSource.appendChild( dataSourceText );
779 layerElement.appendChild( dataSource );
780
781 // layer name
782 QDomElement layerName = document.createElement( QStringLiteral( "layername" ) );
783 const QDomText layerNameText = document.createTextNode( name() );
784 layerName.appendChild( layerNameText );
785 layerElement.appendChild( layerName );
786
787 // layer short name
788
789 // TODO -- ideally this would be in QgsMapLayerServerProperties::writeXml, but that's currently
790 // only called for SOME map layer subclasses!
791 if ( !mServerProperties->shortName().isEmpty() )
792 {
793 QDomElement layerShortName = document.createElement( QStringLiteral( "shortname" ) );
794 const QDomText layerShortNameText = document.createTextNode( mServerProperties->shortName() );
795 layerShortName.appendChild( layerShortNameText );
796 layerElement.appendChild( layerShortName );
797 }
798
799 // layer title
800 if ( !mServerProperties->title().isEmpty() )
801 {
802 QDomElement layerTitle = document.createElement( QStringLiteral( "title" ) );
803 const QDomText layerTitleText = document.createTextNode( mServerProperties->title() );
804 layerTitle.appendChild( layerTitleText );
805 layerElement.appendChild( layerTitle );
806 }
807
808 // layer abstract
809 if ( !mServerProperties->abstract().isEmpty() )
810 {
811 QDomElement layerAbstract = document.createElement( QStringLiteral( "abstract" ) );
812 const QDomText layerAbstractText = document.createTextNode( mServerProperties->abstract() );
813 layerAbstract.appendChild( layerAbstractText );
814 layerElement.appendChild( layerAbstract );
815 }
816
817 // layer keyword list
818 const QStringList keywordStringList = mServerProperties->keywordList().split( ',' );
819 if ( !keywordStringList.isEmpty() )
820 {
821 QDomElement layerKeywordList = document.createElement( QStringLiteral( "keywordList" ) );
822 for ( int i = 0; i < keywordStringList.size(); ++i )
823 {
824 QDomElement layerKeywordValue = document.createElement( QStringLiteral( "value" ) );
825 const QDomText layerKeywordText = document.createTextNode( keywordStringList.at( i ).trimmed() );
826 layerKeywordValue.appendChild( layerKeywordText );
827 layerKeywordList.appendChild( layerKeywordValue );
828 }
829 layerElement.appendChild( layerKeywordList );
830 }
831
832 // layer dataUrl
833 const QString aDataUrl = mServerProperties->dataUrl();
834 if ( !aDataUrl.isEmpty() )
835 {
836 QDomElement layerDataUrl = document.createElement( QStringLiteral( "dataUrl" ) );
837 const QDomText layerDataUrlText = document.createTextNode( aDataUrl );
838 layerDataUrl.appendChild( layerDataUrlText );
839 layerDataUrl.setAttribute( QStringLiteral( "format" ), mServerProperties->dataUrlFormat() );
840 layerElement.appendChild( layerDataUrl );
841 }
842
843 // layer legendUrl
844 const QString aLegendUrl = legendUrl();
845 if ( !aLegendUrl.isEmpty() )
846 {
847 QDomElement layerLegendUrl = document.createElement( QStringLiteral( "legendUrl" ) );
848 const QDomText layerLegendUrlText = document.createTextNode( aLegendUrl );
849 layerLegendUrl.appendChild( layerLegendUrlText );
850 layerLegendUrl.setAttribute( QStringLiteral( "format" ), legendUrlFormat() );
851 layerElement.appendChild( layerLegendUrl );
852 }
853
854 // layer attribution
855 const QString aAttribution = mServerProperties->attribution();
856 if ( !aAttribution.isEmpty() )
857 {
858 QDomElement layerAttribution = document.createElement( QStringLiteral( "attribution" ) );
859 const QDomText layerAttributionText = document.createTextNode( aAttribution );
860 layerAttribution.appendChild( layerAttributionText );
861 layerAttribution.setAttribute( QStringLiteral( "href" ), mServerProperties->attributionUrl() );
862 layerElement.appendChild( layerAttribution );
863 }
864
865 // timestamp if supported
866 if ( timestamp() > QDateTime() )
867 {
868 QDomElement stamp = document.createElement( QStringLiteral( "timestamp" ) );
869 const QDomText stampText = document.createTextNode( timestamp().toString( Qt::ISODate ) );
870 stamp.appendChild( stampText );
871 layerElement.appendChild( stamp );
872 }
873
874 layerElement.appendChild( layerName );
875
876 // zorder
877 // This is no longer stored in the project file. It is superfluous since the layers
878 // are written and read in the proper order.
879
880 // spatial reference system id
881 QDomElement mySrsElement = document.createElement( QStringLiteral( "srs" ) );
882 mCRS.writeXml( mySrsElement, document );
883 layerElement.appendChild( mySrsElement );
884
885 // layer metadata
886 QDomElement myMetadataElem = document.createElement( QStringLiteral( "resourceMetadata" ) );
887 mMetadata.writeMetadataXml( myMetadataElem, document );
888 layerElement.appendChild( myMetadataElem );
889
890 layerElement.setAttribute( QStringLiteral( "legendPlaceholderImage" ), mLegendPlaceholderImage );
891
892 // now append layer node to map layer node
893 return writeXml( layerElement, document, context );
894}
895
896void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
897 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
898{
900
901 // save categories
902 const QMetaEnum metaEnum = QMetaEnum::fromType<QgsMapLayer::StyleCategories>();
903 const QString categoriesKeys( metaEnum.valueToKeys( static_cast<int>( categories ) ) );
904 layerElement.setAttribute( QStringLiteral( "styleCategories" ), categoriesKeys );
905
906 if ( categories.testFlag( Rendering ) )
907 {
908 // use scale dependent visibility flag
909 layerElement.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
910 layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
911 layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );
912 }
913
914 if ( categories.testFlag( Symbology3D ) )
915 {
916 if ( m3DRenderer )
917 {
918 QDomElement renderer3DElem = document.createElement( QStringLiteral( "renderer-3d" ) );
919 renderer3DElem.setAttribute( QStringLiteral( "type" ), m3DRenderer->type() );
920 m3DRenderer->writeXml( renderer3DElem, context );
921 layerElement.appendChild( renderer3DElem );
922 }
923 }
924
925 if ( categories.testFlag( LayerConfiguration ) )
926 {
927 // flags
928 // this code is saving automatically all the flags entries
929 QDomElement layerFlagsElem = document.createElement( QStringLiteral( "flags" ) );
930 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
931 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
932 {
933 const bool flagValue = mFlags.testFlag( it.key() );
934 QDomElement flagElem = document.createElement( it.value() );
935 flagElem.appendChild( document.createTextNode( QString::number( flagValue ) ) );
936 layerFlagsElem.appendChild( flagElem );
937 }
938 layerElement.appendChild( layerFlagsElem );
939 }
940
941 if ( categories.testFlag( Temporal ) )
942 {
944 properties->writeXml( layerElement, document, context );
945 }
946
947 if ( categories.testFlag( Elevation ) )
948 {
950 properties->writeXml( layerElement, document, context );
951 }
952
953 if ( categories.testFlag( Notes ) && QgsLayerNotesUtils::layerHasNotes( this ) )
954 {
955 QDomElement notesElem = document.createElement( QStringLiteral( "userNotes" ) );
956 notesElem.setAttribute( QStringLiteral( "value" ), QgsLayerNotesUtils::layerNotes( this ) );
957 layerElement.appendChild( notesElem );
958 }
959
960 // custom properties
961 if ( categories.testFlag( CustomProperties ) )
962 {
963 writeCustomProperties( layerElement, document );
964 }
965}
966
967
968bool QgsMapLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const
969{
971
972 Q_UNUSED( layer_node )
973 Q_UNUSED( document )
974 Q_UNUSED( context )
975 // NOP by default; children will over-ride with behavior specific to them
976
977 return true;
978}
979
980QString QgsMapLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
981{
983
984 Q_UNUSED( context )
985 return source;
986}
987
988QString QgsMapLayer::decodedSource( const QString &source, const QString &dataProvider, const QgsReadWriteContext &context ) const
989{
991
992 Q_UNUSED( context )
993 Q_UNUSED( dataProvider )
994 return source;
995}
996
998{
1000
1002 if ( m3DRenderer )
1003 m3DRenderer->resolveReferences( *project );
1004}
1005
1006
1007void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
1008{
1010
1011 const QgsObjectCustomProperties oldKeys = mCustomProperties;
1012
1013 mCustomProperties.readXml( layerNode, keyStartsWith );
1014
1015 for ( const QString &key : mCustomProperties.keys() )
1016 {
1017 if ( !oldKeys.contains( key ) || mCustomProperties.value( key ) != oldKeys.value( key ) )
1018 {
1019 emit customPropertyChanged( key );
1020 }
1021 }
1022}
1023
1024void QgsMapLayer::writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const
1025{
1027
1028 mCustomProperties.writeXml( layerNode, doc );
1029}
1030
1031void QgsMapLayer::readStyleManager( const QDomNode &layerNode )
1032{
1034
1035 const QDomElement styleMgrElem = layerNode.firstChildElement( QStringLiteral( "map-layer-style-manager" ) );
1036 if ( !styleMgrElem.isNull() )
1037 mStyleManager->readXml( styleMgrElem );
1038 else
1039 mStyleManager->reset();
1040}
1041
1042void QgsMapLayer::writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const
1043{
1045
1046 if ( mStyleManager )
1047 {
1048 QDomElement styleMgrElem = doc.createElement( QStringLiteral( "map-layer-style-manager" ) );
1049 mStyleManager->writeXml( styleMgrElem );
1050 layerNode.appendChild( styleMgrElem );
1051 }
1052}
1053
1055{
1057
1058 return mMapTipTemplate;
1059}
1060
1061void QgsMapLayer::setMapTipTemplate( const QString &mapTip )
1062{
1064
1065 if ( mMapTipTemplate == mapTip )
1066 return;
1067
1068 mMapTipTemplate = mapTip;
1069 emit mapTipTemplateChanged();
1070}
1071
1073{
1075
1076 if ( mMapTipsEnabled == enabled )
1077 return;
1078
1079 mMapTipsEnabled = enabled;
1080 emit mapTipsEnabledChanged();
1081}
1082
1084{
1086
1087 return mMapTipsEnabled;
1088}
1089
1091{
1093 if ( layerReadFlags & QgsMapLayer::FlagTrustLayerMetadata )
1094 {
1096 }
1097 if ( layerReadFlags & QgsMapLayer::FlagForceReadOnly )
1098 {
1100 }
1101
1102 if ( layerReadFlags & QgsMapLayer::FlagReadExtentFromXml )
1103 {
1104 const QDomNode extent3DNode = layerNode.namedItem( QStringLiteral( "extent3D" ) );
1105 if ( extent3DNode.isNull() )
1106 {
1107 const QDomNode extentNode = layerNode.namedItem( QStringLiteral( "extent" ) );
1108 if ( !extentNode.isNull() )
1109 {
1111 }
1112 }
1113 else
1114 {
1116 }
1117 }
1118
1119 return flags;
1120}
1121
1123{
1124 // because QgsVirtualLayerProvider is not anywhere NEAR thread safe:
1126
1127 return mValid;
1128}
1129
1130#if 0
1131void QgsMapLayer::connectNotify( const char *signal )
1132{
1133 Q_UNUSED( signal )
1134 QgsDebugMsgLevel( "QgsMapLayer connected to " + QString( signal ), 3 );
1135} // QgsMapLayer::connectNotify
1136#endif
1137
1138bool QgsMapLayer::isInScaleRange( double scale ) const
1139{
1140 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1142
1143 return !mScaleBasedVisibility ||
1144 ( ( mMinScale == 0 || mMinScale * Qgis::SCALE_PRECISION < scale )
1145 && ( mMaxScale == 0 || scale < mMaxScale ) );
1146}
1147
1149{
1150 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1152
1153 return mScaleBasedVisibility;
1154}
1155
1157{
1159
1160 return mAutoRefreshMode != Qgis::AutoRefreshMode::Disabled;;
1161}
1162
1164{
1166
1167 return mAutoRefreshMode;
1168}
1169
1171{
1173
1174 return mRefreshTimer->interval();
1175}
1176
1178{
1180
1181 if ( interval <= 0 )
1182 {
1183 mRefreshTimer->stop();
1184 mRefreshTimer->setInterval( 0 );
1186 }
1187 else
1188 {
1189 mRefreshTimer->setInterval( interval );
1190 }
1191 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1192}
1193
1200
1202{
1204
1205 if ( mode == mAutoRefreshMode )
1206 return;
1207
1208 mAutoRefreshMode = mode;
1209 switch ( mAutoRefreshMode )
1210 {
1212 mRefreshTimer->stop();
1213 break;
1214
1217 if ( mRefreshTimer->interval() > 0 )
1218 mRefreshTimer->start();
1219 break;
1220 }
1221
1222 emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
1223}
1224
1226{
1228
1229 return mMetadata;
1230}
1231
1233{
1235
1236 mMinScale = scale;
1237}
1238
1240{
1242
1243 return mMinScale;
1244}
1245
1247{
1249
1250 mMaxScale = scale;
1251}
1252
1254{
1256
1257 mScaleBasedVisibility = enabled;
1258}
1259
1261{
1263
1264 return mMaxScale;
1265}
1266
1267QStringList QgsMapLayer::subLayers() const
1268{
1270
1271 return QStringList();
1272}
1273
1274void QgsMapLayer::setLayerOrder( const QStringList &layers )
1275{
1277
1278 Q_UNUSED( layers )
1279}
1280
1281void QgsMapLayer::setSubLayerVisibility( const QString &name, bool vis )
1282{
1284
1285 Q_UNUSED( name )
1286 Q_UNUSED( vis )
1287}
1288
1290{
1292
1293 return false;
1294}
1295
1297{
1298 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1300
1301 return mCRS;
1302}
1303
1305{
1307
1308 switch ( mCRS.type() )
1309 {
1310 case Qgis::CrsType::Vertical: // would hope this never happens!
1311 QgsDebugError( QStringLiteral( "Layer has a vertical CRS set as the horizontal CRS!" ) );
1312 return mCRS;
1313
1315 return mCRS.verticalCrs();
1316
1328 break;
1329 }
1330 return mVerticalCrs;
1331}
1332
1334{
1336
1337 return mCrs3D.isValid() ? mCrs3D : mCRS;
1338}
1339
1340void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal )
1341{
1343 if ( mCRS == srs )
1344 return;
1345
1346 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
1347 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
1348
1349 mCRS = srs;
1350
1352 {
1353 mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( name() ) );
1354 mCRS.validate();
1355 }
1356
1357 rebuildCrs3D();
1358
1359 if ( emitSignal )
1360 emit crsChanged();
1361
1362 // Did vertical crs also change as a result of this? If so, emit signal
1363 if ( oldVerticalCrs != verticalCrs() )
1364 emit verticalCrsChanged();
1365 if ( oldCrs3D != mCrs3D )
1366 emit crs3DChanged();
1367}
1368
1370{
1372 bool res = true;
1373 if ( crs.isValid() )
1374 {
1375 // validate that passed crs is a vertical crs
1376 switch ( crs.type() )
1377 {
1379 break;
1380
1393 if ( errorMessage )
1394 *errorMessage = QObject::tr( "Specified CRS is a %1 CRS, not a Vertical CRS" ).arg( qgsEnumValueToKey( crs.type() ) );
1395 return false;
1396 }
1397 }
1398
1399 if ( crs != mVerticalCrs )
1400 {
1401 const QgsCoordinateReferenceSystem oldVerticalCrs = verticalCrs();
1402 const QgsCoordinateReferenceSystem oldCrs3D = mCrs3D;
1403
1404 switch ( mCRS.type() )
1405 {
1407 if ( crs != oldVerticalCrs )
1408 {
1409 if ( errorMessage )
1410 *errorMessage = QObject::tr( "Layer CRS is a Compound CRS, specified Vertical CRS will be ignored" );
1411 return false;
1412 }
1413 break;
1414
1416 if ( crs != oldVerticalCrs )
1417 {
1418 if ( errorMessage )
1419 *errorMessage = QObject::tr( "Layer CRS is a Geographic 3D CRS, specified Vertical CRS will be ignored" );
1420 return false;
1421 }
1422 break;
1423
1425 if ( crs != oldVerticalCrs )
1426 {
1427 if ( errorMessage )
1428 *errorMessage = QObject::tr( "Layer CRS is a Geocentric CRS, specified Vertical CRS will be ignored" );
1429 return false;
1430 }
1431 break;
1432
1434 if ( mCRS.hasVerticalAxis() && crs != oldVerticalCrs )
1435 {
1436 if ( errorMessage )
1437 *errorMessage = QObject::tr( "Layer CRS is a Projected 3D CRS, specified Vertical CRS will be ignored" );
1438 return false;
1439 }
1440 break;
1441
1451 break;
1452 }
1453
1454 mVerticalCrs = crs;
1455 res = rebuildCrs3D( errorMessage );
1456
1457 // only emit signal if vertical crs was actually changed, so eg if mCrs is compound
1458 // then we haven't actually changed the vertical crs by this call!
1459 if ( verticalCrs() != oldVerticalCrs )
1460 emit verticalCrsChanged();
1461 if ( mCrs3D != oldCrs3D )
1462 emit crs3DChanged();
1463 }
1464 return res;
1465}
1466
1468{
1470
1471 const QgsDataProvider *lDataProvider = dataProvider();
1472 return lDataProvider ? lDataProvider->transformContext() : QgsCoordinateTransformContext();
1473}
1474
1475QString QgsMapLayer::formatLayerName( const QString &name )
1476{
1477 QString layerName( name );
1478 layerName.replace( '_', ' ' );
1480 return layerName;
1481}
1482
1483QString QgsMapLayer::baseURI( PropertyType type ) const
1484{
1486
1487 QString myURI = publicSource();
1488
1489 // first get base path for delimited text, spatialite and OGR layers,
1490 // as in these cases URI may contain layer name and/or additional
1491 // information. This also strips prefix in case if VSIFILE mechanism
1492 // is used
1493 if ( providerType() == QLatin1String( "ogr" ) || providerType() == QLatin1String( "delimitedtext" )
1494 || providerType() == QLatin1String( "gdal" ) || providerType() == QLatin1String( "spatialite" ) )
1495 {
1496 QVariantMap components = QgsProviderRegistry::instance()->decodeUri( providerType(), myURI );
1497 myURI = components["path"].toString();
1498 }
1499
1500 QFileInfo myFileInfo( myURI );
1501 QString key;
1502
1503 if ( myFileInfo.exists() )
1504 {
1505 // if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name
1506 if ( myURI.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
1507 myURI.chop( 3 );
1508 else if ( myURI.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
1509 myURI.chop( 4 );
1510 else if ( myURI.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) )
1511 myURI.chop( 4 );
1512 else if ( myURI.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) )
1513 myURI.chop( 7 );
1514 else if ( myURI.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
1515 myURI.chop( 4 );
1516 myFileInfo.setFile( myURI );
1517 // get the file name for our .qml style file
1518 key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1519 }
1520 else
1521 {
1522 key = publicSource();
1523 }
1524
1525 return key;
1526}
1527
1529{
1531
1532 return baseURI( PropertyType::Metadata );
1533}
1534
1535QString QgsMapLayer::saveDefaultMetadata( bool &resultFlag )
1536{
1538
1539 if ( const QgsProviderMetadata *metadata = QgsProviderRegistry::instance()->providerMetadata( providerType() ) )
1540 {
1541 if ( metadata->providerCapabilities() & QgsProviderMetadata::SaveLayerMetadata )
1542 {
1543 try
1544 {
1545 QString errorMessage;
1546 resultFlag = QgsProviderRegistry::instance()->saveLayerMetadata( providerType(), mDataSource, mMetadata, errorMessage );
1547 if ( resultFlag )
1548 return tr( "Successfully saved default layer metadata" );
1549 else
1550 return errorMessage;
1551 }
1552 catch ( QgsNotSupportedException &e )
1553 {
1554 resultFlag = false;
1555 return e.what();
1556 }
1557 }
1558 }
1559
1560 // fallback default metadata saving method, for providers which don't support (or implement) saveLayerMetadata
1561 return saveNamedMetadata( metadataUri(), resultFlag );
1562}
1563
1564QString QgsMapLayer::loadDefaultMetadata( bool &resultFlag )
1565{
1567
1568 return loadNamedMetadata( metadataUri(), resultFlag );
1569}
1570
1572{
1574
1575 return baseURI( PropertyType::Style );
1576}
1577
1584
1585bool QgsMapLayer::loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd )
1586{
1588
1589 return loadNamedPropertyFromDatabase( db, uri, qmd, PropertyType::Metadata );
1590}
1591
1592bool QgsMapLayer::loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml )
1593{
1595
1596 return loadNamedPropertyFromDatabase( db, uri, qml, PropertyType::Style );
1597}
1598
1599bool QgsMapLayer::loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type )
1600{
1602
1603 QgsDebugMsgLevel( QStringLiteral( "db = %1 uri = %2" ).arg( db, uri ), 4 );
1604
1605 bool resultFlag = false;
1606
1607 // read from database
1610
1611 int myResult;
1612
1613 QgsDebugMsgLevel( QStringLiteral( "Trying to load style or metadata for \"%1\" from \"%2\"" ).arg( uri, db ), 4 );
1614
1615 if ( db.isEmpty() || !QFile( db ).exists() )
1616 return false;
1617
1618 myResult = database.open_v2( db, SQLITE_OPEN_READONLY, nullptr );
1619 if ( myResult != SQLITE_OK )
1620 {
1621 return false;
1622 }
1623
1624 QString mySql;
1625 switch ( type )
1626 {
1627 case Metadata:
1628 mySql = QStringLiteral( "select qmd from tbl_metadata where metadata=?" );
1629 break;
1630
1631 case Style:
1632 mySql = QStringLiteral( "select qml from tbl_styles where style=?" );
1633 break;
1634 }
1635
1636 statement = database.prepare( mySql, myResult );
1637 if ( myResult == SQLITE_OK )
1638 {
1639 QByteArray param = uri.toUtf8();
1640
1641 if ( sqlite3_bind_text( statement.get(), 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK &&
1642 sqlite3_step( statement.get() ) == SQLITE_ROW )
1643 {
1644 xml = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( statement.get(), 0 ) ) );
1645 resultFlag = true;
1646 }
1647 }
1648 return resultFlag;
1649}
1650
1651
1652QString QgsMapLayer::loadNamedStyle( const QString &uri, bool &resultFlag, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
1653{
1655
1656 return loadNamedStyle( uri, resultFlag, false, categories, flags );
1657}
1658
1659QString QgsMapLayer::loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &namedPropertyExists, bool &propertySuccessfullyLoaded, StyleCategories categories, Qgis::LoadStyleFlags flags )
1660{
1662
1663 QgsDebugMsgLevel( QStringLiteral( "uri = %1 myURI = %2" ).arg( uri, publicSource() ), 4 );
1664
1665 namedPropertyExists = false;
1666 propertySuccessfullyLoaded = false;
1667 if ( uri.isEmpty() )
1668 return QString();
1669
1670 QDomDocument myDocument( QStringLiteral( "qgis" ) );
1671
1672 // location of problem associated with errorMsg
1673 int line, column;
1674 QString myErrorMessage;
1675
1676 QFile myFile( uri );
1677 if ( myFile.open( QFile::ReadOnly ) )
1678 {
1679 QgsDebugMsgLevel( QStringLiteral( "file found %1" ).arg( uri ), 2 );
1680 namedPropertyExists = true;
1681
1682 // read file
1683 propertySuccessfullyLoaded = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
1684 if ( !propertySuccessfullyLoaded )
1685 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1686 myFile.close();
1687 }
1688 else
1689 {
1690 const QFileInfo project( QgsProject::instance()->fileName() );
1691 QgsDebugMsgLevel( QStringLiteral( "project fileName: %1" ).arg( project.absoluteFilePath() ), 4 );
1692
1693 QString xml;
1694 switch ( type )
1695 {
1696 case QgsMapLayer::Style:
1697 {
1698 if ( loadNamedStyleFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1699 ( project.exists() && loadNamedStyleFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1700 loadNamedStyleFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1701 {
1702 namedPropertyExists = true;
1703 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1704 if ( !propertySuccessfullyLoaded )
1705 {
1706 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1707 }
1708 }
1709 else
1710 {
1712 {
1713 myErrorMessage = tr( "Style not found in database" );
1714 }
1715 }
1716 break;
1717 }
1719 {
1720 if ( loadNamedMetadataFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
1721 ( project.exists() && loadNamedMetadataFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
1722 loadNamedMetadataFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
1723 {
1724 namedPropertyExists = true;
1725 propertySuccessfullyLoaded = myDocument.setContent( xml, &myErrorMessage, &line, &column );
1726 if ( !propertySuccessfullyLoaded )
1727 {
1728 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1729 }
1730 }
1731 else
1732 {
1733 myErrorMessage = tr( "Metadata not found in database" );
1734 }
1735 break;
1736 }
1737 }
1738 }
1739
1740 if ( !propertySuccessfullyLoaded )
1741 {
1742 return myErrorMessage;
1743 }
1744
1745 switch ( type )
1746 {
1747 case QgsMapLayer::Style:
1748 propertySuccessfullyLoaded = importNamedStyle( myDocument, myErrorMessage, categories );
1749 if ( !propertySuccessfullyLoaded )
1750 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1751 break;
1753 propertySuccessfullyLoaded = importNamedMetadata( myDocument, myErrorMessage );
1754 if ( !propertySuccessfullyLoaded )
1755 myErrorMessage = tr( "Loading metadata file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
1756 break;
1757 }
1758 return myErrorMessage;
1759}
1760
1761bool QgsMapLayer::importNamedMetadata( QDomDocument &document, QString &errorMessage )
1762{
1764
1765 const QDomElement myRoot = document.firstChildElement( QStringLiteral( "qgis" ) );
1766 if ( myRoot.isNull() )
1767 {
1768 errorMessage = tr( "Root <qgis> element could not be found" );
1769 return false;
1770 }
1771
1772 return mMetadata.readMetadataXml( myRoot );
1773}
1774
1775bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMessage, QgsMapLayer::StyleCategories categories )
1776{
1778
1779 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "qgis" ) );
1780 if ( myRoot.isNull() )
1781 {
1782 myErrorMessage = tr( "Root <qgis> element could not be found" );
1783 return false;
1784 }
1785
1786 // get style file version string, if any
1787 const QgsProjectVersion fileVersion( myRoot.attribute( QStringLiteral( "version" ) ) );
1788 const QgsProjectVersion thisVersion( Qgis::version() );
1789
1790 if ( thisVersion > fileVersion )
1791 {
1792 QgsProjectFileTransform styleFile( myDocument, fileVersion );
1793 styleFile.updateRevision( thisVersion );
1794 }
1795
1796 // Get source categories
1797 const QgsMapLayer::StyleCategories sourceCategories = QgsXmlUtils::readFlagAttribute( myRoot, QStringLiteral( "styleCategories" ), QgsMapLayer::AllStyleCategories );
1798
1799 //Test for matching geometry type on vector layers when applying, if geometry type is given in the style
1800 if ( ( sourceCategories.testFlag( QgsMapLayer::Symbology ) || sourceCategories.testFlag( QgsMapLayer::Symbology3D ) ) &&
1801 ( categories.testFlag( QgsMapLayer::Symbology ) || categories.testFlag( QgsMapLayer::Symbology3D ) ) )
1802 {
1803 if ( type() == Qgis::LayerType::Vector && !myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).isNull() )
1804 {
1805 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( this );
1806 const Qgis::GeometryType importLayerGeometryType = static_cast<Qgis::GeometryType>( myRoot.firstChildElement( QStringLiteral( "layerGeometryType" ) ).text().toInt() );
1807 if ( importLayerGeometryType != Qgis::GeometryType::Unknown && vl->geometryType() != importLayerGeometryType )
1808 {
1809 myErrorMessage = tr( "Cannot apply style with symbology to layer with a different geometry type" );
1810 return false;
1811 }
1812 }
1813 }
1814
1816 return readSymbology( myRoot, myErrorMessage, context, categories ); // TODO: support relative paths in QML?
1817}
1818
1819void QgsMapLayer::exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const
1820{
1822
1823 QDomImplementation DomImplementation;
1824 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1825 QDomDocument myDocument( documentType );
1826
1827 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1828 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1829 myDocument.appendChild( myRootNode );
1830
1831 if ( !mMetadata.writeMetadataXml( myRootNode, myDocument ) )
1832 {
1833 errorMsg = QObject::tr( "Could not save metadata" );
1834 return;
1835 }
1836
1837 doc = myDocument;
1838}
1839
1840void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
1841{
1843
1844 QDomImplementation DomImplementation;
1845 const QDomDocumentType documentType = DomImplementation.createDocumentType( QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
1846 QDomDocument myDocument( documentType );
1847
1848 QDomElement myRootNode = myDocument.createElement( QStringLiteral( "qgis" ) );
1849 myRootNode.setAttribute( QStringLiteral( "version" ), Qgis::version() );
1850 myDocument.appendChild( myRootNode );
1851
1852 if ( !writeSymbology( myRootNode, myDocument, errorMsg, context, categories ) ) // TODO: support relative paths in QML?
1853 {
1854 errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1855 return;
1856 }
1857
1858 /*
1859 * Check to see if the layer is vector - in which case we should also export its geometryType
1860 * to avoid eventually pasting to a layer with a different geometry
1861 */
1862 if ( type() == Qgis::LayerType::Vector )
1863 {
1864 //Getting the selectionLayer geometry
1865 const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( this );
1866 const QString geoType = QString::number( static_cast<int>( vl->geometryType() ) );
1867
1868 //Adding geometryinformation
1869 QDomElement layerGeometryType = myDocument.createElement( QStringLiteral( "layerGeometryType" ) );
1870 const QDomText type = myDocument.createTextNode( geoType );
1871
1872 layerGeometryType.appendChild( type );
1873 myRootNode.appendChild( layerGeometryType );
1874 }
1875
1876 doc = myDocument;
1877}
1878
1879QString QgsMapLayer::saveDefaultStyle( bool &resultFlag )
1880{
1882
1883 return saveDefaultStyle( resultFlag, AllStyleCategories );
1884}
1885
1886QString QgsMapLayer::saveDefaultStyle( bool &resultFlag, StyleCategories categories )
1887{
1889
1890 return saveNamedStyle( styleURI(), resultFlag, categories );
1891}
1892
1893QString QgsMapLayer::saveNamedMetadata( const QString &uri, bool &resultFlag )
1894{
1896
1897 return saveNamedProperty( uri, QgsMapLayer::Metadata, resultFlag );
1898}
1899
1900QString QgsMapLayer::loadNamedMetadata( const QString &uri, bool &resultFlag )
1901{
1903
1904 bool metadataExists = false;
1905 bool metadataSuccessfullyLoaded = false;
1906 const QString message = loadNamedProperty( uri, QgsMapLayer::Metadata, metadataExists, metadataSuccessfullyLoaded );
1907
1908 // TODO QGIS 4.0 -- fix API for loadNamedMetadata so we can return metadataExists too
1909 ( void )metadataExists;
1910 resultFlag = metadataSuccessfullyLoaded;
1911 return message;
1912}
1913
1914QString QgsMapLayer::saveNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag, StyleCategories categories )
1915{
1917
1918 // check if the uri is a file or ends with .qml/.qmd,
1919 // which indicates that it should become one
1920 // everything else goes to the database
1921 QString filename;
1922
1923 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1924 if ( vlayer && vlayer->providerType() == QLatin1String( "ogr" ) )
1925 {
1926 QStringList theURIParts = uri.split( '|' );
1927 filename = theURIParts[0];
1928 }
1929 else if ( vlayer && vlayer->providerType() == QLatin1String( "gpx" ) )
1930 {
1931 QStringList theURIParts = uri.split( '?' );
1932 filename = theURIParts[0];
1933 }
1934 else if ( vlayer && vlayer->providerType() == QLatin1String( "delimitedtext" ) )
1935 {
1936 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
1937 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1938 if ( filename.isEmpty() )
1939 filename = uri;
1940 }
1941 else
1942 {
1943 filename = uri;
1944 }
1945
1946 QString myErrorMessage;
1947 QDomDocument myDocument;
1948 switch ( type )
1949 {
1950 case Metadata:
1951 exportNamedMetadata( myDocument, myErrorMessage );
1952 break;
1953
1954 case Style:
1955 const QgsReadWriteContext context;
1956 exportNamedStyle( myDocument, myErrorMessage, context, categories );
1957 break;
1958 }
1959
1960 const QFileInfo myFileInfo( filename );
1961 if ( myFileInfo.exists() || filename.endsWith( QgsMapLayer::extensionPropertyType( type ), Qt::CaseInsensitive ) )
1962 {
1963 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1964 if ( !myDirInfo.isWritable() )
1965 {
1966 resultFlag = false;
1967 return tr( "The directory containing your dataset needs to be writable!" );
1968 }
1969
1970 // now construct the file name for our .qml or .qmd file
1971 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
1972
1973 QFile myFile( myFileName );
1974 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1975 {
1976 QTextStream myFileStream( &myFile );
1977 // save as utf-8 with 2 spaces for indents
1978 myDocument.save( myFileStream, 2 );
1979 myFile.close();
1980 resultFlag = true;
1981 switch ( type )
1982 {
1983 case Metadata:
1984 return tr( "Created default metadata file as %1" ).arg( myFileName );
1985
1986 case Style:
1987 return tr( "Created default style file as %1" ).arg( myFileName );
1988 }
1989
1990 }
1991 else
1992 {
1993 resultFlag = false;
1994 switch ( type )
1995 {
1996 case Metadata:
1997 return tr( "ERROR: Failed to created default metadata file as %1. Check file permissions and retry." ).arg( myFileName );
1998
1999 case Style:
2000 return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
2001 }
2002 }
2003 }
2004 else
2005 {
2006 const QString qml = myDocument.toString();
2007
2008 // read from database
2011
2012 int myResult = database.open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ) );
2013 if ( myResult != SQLITE_OK )
2014 {
2015 return tr( "User database could not be opened." );
2016 }
2017
2018 QByteArray param0 = uri.toUtf8();
2019 QByteArray param1 = qml.toUtf8();
2020
2021 QString mySql;
2022 switch ( type )
2023 {
2024 case Metadata:
2025 mySql = QStringLiteral( "create table if not exists tbl_metadata(metadata varchar primary key,qmd varchar)" );
2026 break;
2027
2028 case Style:
2029 mySql = QStringLiteral( "create table if not exists tbl_styles(style varchar primary key,qml varchar)" );
2030 break;
2031 }
2032
2033 statement = database.prepare( mySql, myResult );
2034 if ( myResult == SQLITE_OK )
2035 {
2036 if ( sqlite3_step( statement.get() ) != SQLITE_DONE )
2037 {
2038 resultFlag = false;
2039 switch ( type )
2040 {
2041 case Metadata:
2042 return tr( "The metadata table could not be created." );
2043
2044 case Style:
2045 return tr( "The style table could not be created." );
2046 }
2047 }
2048 }
2049
2050 switch ( type )
2051 {
2052 case Metadata:
2053 mySql = QStringLiteral( "insert into tbl_metadata(metadata,qmd) values (?,?)" );
2054 break;
2055
2056 case Style:
2057 mySql = QStringLiteral( "insert into tbl_styles(style,qml) values (?,?)" );
2058 break;
2059 }
2060 statement = database.prepare( mySql, myResult );
2061 if ( myResult == SQLITE_OK )
2062 {
2063 if ( sqlite3_bind_text( statement.get(), 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
2064 sqlite3_bind_text( statement.get(), 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
2065 sqlite3_step( statement.get() ) == SQLITE_DONE )
2066 {
2067 resultFlag = true;
2068 switch ( type )
2069 {
2070 case Metadata:
2071 myErrorMessage = tr( "The metadata %1 was saved to database" ).arg( uri );
2072 break;
2073
2074 case Style:
2075 myErrorMessage = tr( "The style %1 was saved to database" ).arg( uri );
2076 break;
2077 }
2078 }
2079 }
2080
2081 if ( !resultFlag )
2082 {
2083 QString mySql;
2084 switch ( type )
2085 {
2086 case Metadata:
2087 mySql = QStringLiteral( "update tbl_metadata set qmd=? where metadata=?" );
2088 break;
2089
2090 case Style:
2091 mySql = QStringLiteral( "update tbl_styles set qml=? where style=?" );
2092 break;
2093 }
2094 statement = database.prepare( mySql, myResult );
2095 if ( myResult == SQLITE_OK )
2096 {
2097 if ( sqlite3_bind_text( statement.get(), 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
2098 sqlite3_bind_text( statement.get(), 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
2099 sqlite3_step( statement.get() ) == SQLITE_DONE )
2100 {
2101 resultFlag = true;
2102 switch ( type )
2103 {
2104 case Metadata:
2105 myErrorMessage = tr( "The metadata %1 was updated in the database." ).arg( uri );
2106 break;
2107
2108 case Style:
2109 myErrorMessage = tr( "The style %1 was updated in the database." ).arg( uri );
2110 break;
2111 }
2112 }
2113 else
2114 {
2115 resultFlag = false;
2116 switch ( type )
2117 {
2118 case Metadata:
2119 myErrorMessage = tr( "The metadata %1 could not be updated in the database." ).arg( uri );
2120 break;
2121
2122 case Style:
2123 myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( uri );
2124 break;
2125 }
2126 }
2127 }
2128 else
2129 {
2130 resultFlag = false;
2131 switch ( type )
2132 {
2133 case Metadata:
2134 myErrorMessage = tr( "The metadata %1 could not be inserted into database." ).arg( uri );
2135 break;
2136
2137 case Style:
2138 myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( uri );
2139 break;
2140 }
2141 }
2142 }
2143 }
2144
2145 return myErrorMessage;
2146}
2147
2148QString QgsMapLayer::saveNamedStyle( const QString &uri, bool &resultFlag, StyleCategories categories )
2149{
2151
2152 return saveNamedProperty( uri, QgsMapLayer::Style, resultFlag, categories );
2153}
2154
2155void QgsMapLayer::exportSldStyle( QDomDocument &doc, QString &errorMsg ) const
2156{
2157
2158 return exportSldStyleV2( doc, errorMsg, QgsSldExportContext() );
2159}
2160
2161void QgsMapLayer::exportSldStyleV2( QDomDocument &doc, QString &errorMsg, const QgsSldExportContext &exportContext ) const
2162{
2164
2165 QDomDocument myDocument = QDomDocument();
2166
2167 const QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) );
2168 myDocument.appendChild( header );
2169
2170 const QgsVectorLayer *vlayer = qobject_cast<const QgsVectorLayer *>( this );
2171 const QgsRasterLayer *rlayer = qobject_cast<const QgsRasterLayer *>( this );
2172 if ( !vlayer && !rlayer )
2173 {
2174 errorMsg = tr( "Could not save symbology because:\n%1" )
2175 .arg( tr( "Only vector and raster layers are supported" ) );
2176 return;
2177 }
2178
2179 // Create the root element
2180 QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "StyledLayerDescriptor" ) );
2181 QDomElement layerNode;
2182 if ( vlayer )
2183 {
2184 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.1.0" ) );
2185 root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" ) );
2186 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
2187 root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) );
2188 root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) );
2189 root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) );
2190 myDocument.appendChild( root );
2191
2192 // Create the NamedLayer element
2193 layerNode = myDocument.createElement( QStringLiteral( "NamedLayer" ) );
2194 root.appendChild( layerNode );
2195 }
2196
2197 // note: Only SLD 1.0 version is generated because seems none is using SE1.1.0 at least for rasters
2198 if ( rlayer )
2199 {
2200 // Create the root element
2201 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0.0" ) );
2202 root.setAttribute( QStringLiteral( "xmlns:gml" ), QStringLiteral( "http://www.opengis.net/gml" ) );
2203 root.setAttribute( QStringLiteral( "xmlns:ogc" ), QStringLiteral( "http://www.opengis.net/ogc" ) );
2204 root.setAttribute( QStringLiteral( "xmlns:sld" ), QStringLiteral( "http://www.opengis.net/sld" ) );
2205 myDocument.appendChild( root );
2206
2207 // Create the NamedLayer element
2208 layerNode = myDocument.createElement( QStringLiteral( "UserLayer" ) );
2209 root.appendChild( layerNode );
2210 }
2211
2212 QVariantMap props;
2213
2214 QVariant context;
2215 context.setValue( exportContext );
2216
2217 props[ QStringLiteral( "SldExportContext" ) ] = context;
2218
2220 {
2221 props[ QStringLiteral( "scaleMinDenom" ) ] = QString::number( mMinScale );
2222 props[ QStringLiteral( "scaleMaxDenom" ) ] = QString::number( mMaxScale );
2223 }
2224
2225 if ( vlayer )
2226 {
2227 if ( !vlayer->writeSld( layerNode, myDocument, errorMsg, props ) )
2228 {
2229 errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
2230 return;
2231 }
2232 }
2233
2234 if ( rlayer )
2235 {
2236 if ( !rlayer->writeSld( layerNode, myDocument, errorMsg, props ) )
2237 {
2238 errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
2239 return;
2240 }
2241 }
2242
2243 doc = myDocument;
2244}
2245
2246QString QgsMapLayer::saveSldStyle( const QString &uri, bool &resultFlag ) const
2247{
2248 QgsSldExportContext context;
2249 context.setExportFilePath( uri );
2250 return saveSldStyleV2( resultFlag, context );
2251}
2252
2253QString QgsMapLayer::saveSldStyleV2( bool &resultFlag, const QgsSldExportContext &exportContext ) const
2254{
2256
2257 const QgsMapLayer *mlayer = qobject_cast<const QgsMapLayer *>( this );
2258
2259 const QString uri { exportContext.exportFilePath() };
2260
2261 // check if the uri is a file or ends with .sld,
2262 // which indicates that it should become one
2263 QString filename;
2264 if ( mlayer->providerType() == QLatin1String( "ogr" ) )
2265 {
2266 QStringList theURIParts = uri.split( '|' );
2267 filename = theURIParts[0];
2268 }
2269 else if ( mlayer->providerType() == QLatin1String( "gpx" ) )
2270 {
2271 QStringList theURIParts = uri.split( '?' );
2272 filename = theURIParts[0];
2273 }
2274 else if ( mlayer->providerType() == QLatin1String( "delimitedtext" ) )
2275 {
2276 filename = QUrl::fromEncoded( uri.toLatin1() ).toLocalFile();
2277 // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
2278 if ( filename.isEmpty() )
2279 filename = uri;
2280 }
2281 else
2282 {
2283 filename = uri;
2284 }
2285
2286 const QFileInfo myFileInfo( filename );
2287 if ( myFileInfo.exists() || filename.endsWith( QLatin1String( ".sld" ), Qt::CaseInsensitive ) )
2288 {
2289 const QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
2290 if ( !myDirInfo.isWritable() )
2291 {
2292 resultFlag = false;
2293 return tr( "The directory containing your dataset needs to be writable!" );
2294 }
2295
2296 // now construct the file name for our .sld style file
2297 const QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld";
2298
2299 QString errorMsg;
2300 QDomDocument myDocument;
2301
2302 QgsSldExportContext context { exportContext };
2303 context.setExportFilePath( myFileName );
2304
2305 mlayer->exportSldStyleV2( myDocument, errorMsg, context );
2306
2307 if ( !errorMsg.isNull() )
2308 {
2309 resultFlag = false;
2310 return errorMsg;
2311 }
2312
2313 QFile myFile( myFileName );
2314 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
2315 {
2316 QTextStream myFileStream( &myFile );
2317 // save as utf-8 with 2 spaces for indents
2318 myDocument.save( myFileStream, 2 );
2319 myFile.close();
2320 resultFlag = true;
2321 return tr( "Created default style file as %1" ).arg( myFileName );
2322 }
2323 }
2324
2325 resultFlag = false;
2326 return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );
2327
2328}
2329
2330QString QgsMapLayer::loadSldStyle( const QString &uri, bool &resultFlag )
2331{
2333
2334 resultFlag = false;
2335
2336 QDomDocument myDocument;
2337
2338 // location of problem associated with errorMsg
2339 int line, column;
2340 QString myErrorMessage;
2341
2342 QFile myFile( uri );
2343 if ( myFile.open( QFile::ReadOnly ) )
2344 {
2345 // read file
2346 resultFlag = myDocument.setContent( &myFile, true, &myErrorMessage, &line, &column );
2347 if ( !resultFlag )
2348 myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
2349 myFile.close();
2350 }
2351 else
2352 {
2353 myErrorMessage = tr( "Unable to open file %1" ).arg( uri );
2354 }
2355
2356 if ( !resultFlag )
2357 {
2358 return myErrorMessage;
2359 }
2360
2361 // check for root SLD element
2362 const QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "StyledLayerDescriptor" ) );
2363 if ( myRoot.isNull() )
2364 {
2365 myErrorMessage = QStringLiteral( "Error: StyledLayerDescriptor element not found in %1" ).arg( uri );
2366 resultFlag = false;
2367 return myErrorMessage;
2368 }
2369
2370 // now get the style node out and pass it over to the layer
2371 // to deserialise...
2372 const QDomElement namedLayerElem = myRoot.firstChildElement( QStringLiteral( "NamedLayer" ) );
2373 if ( namedLayerElem.isNull() )
2374 {
2375 myErrorMessage = QStringLiteral( "Info: NamedLayer element not found." );
2376 resultFlag = false;
2377 return myErrorMessage;
2378 }
2379
2380 QString errorMsg;
2381 resultFlag = readSld( namedLayerElem, errorMsg );
2382 if ( !resultFlag )
2383 {
2384 myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, errorMsg );
2385 return myErrorMessage;
2386 }
2387
2388 return QString();
2389}
2390
2391bool QgsMapLayer::readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2392{
2394
2395 Q_UNUSED( node )
2396 Q_UNUSED( errorMessage )
2397 Q_UNUSED( context )
2398 Q_UNUSED( categories )
2399 return false;
2400}
2401
2402bool QgsMapLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
2403 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
2404{
2406
2407 Q_UNUSED( node )
2408 Q_UNUSED( doc )
2409 Q_UNUSED( errorMessage )
2410 Q_UNUSED( context )
2411 Q_UNUSED( categories )
2412 return false;
2413}
2414
2415
2416void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2417 bool loadDefaultStyleFlag )
2418{
2420
2422
2424 if ( loadDefaultStyleFlag )
2425 {
2427 }
2428
2430 {
2432 }
2433 setDataSource( dataSource, baseName, provider, options, flags );
2434}
2435
2436void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2437 const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag )
2438{
2440
2442 if ( loadDefaultStyleFlag )
2443 {
2445 }
2446
2448 {
2450 }
2451 setDataSource( dataSource, baseName, provider, options, flags );
2452}
2453
2454void QgsMapLayer::setDataSource( const QString &dataSource, const QString &baseName, const QString &provider,
2456{
2458
2461 {
2463 }
2464 setDataSourcePrivate( dataSource, baseName, provider, options, flags );
2465 emit dataSourceChanged();
2466 emit dataChanged();
2468}
2469
2470
2471void QgsMapLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
2473{
2475
2476 Q_UNUSED( dataSource )
2477 Q_UNUSED( baseName )
2478 Q_UNUSED( provider )
2479 Q_UNUSED( options )
2480 Q_UNUSED( flags )
2481}
2482
2483
2485{
2487
2488 return mProviderKey;
2489}
2490
2491void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
2492 QgsMapLayer::StyleCategories categories )
2493{
2495
2496 if ( categories.testFlag( Symbology3D ) )
2497 {
2498 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "3D Symbology" ) );
2499
2500 QgsAbstract3DRenderer *r3D = nullptr;
2501 QDomElement renderer3DElem = layerElement.firstChildElement( QStringLiteral( "renderer-3d" ) );
2502 if ( !renderer3DElem.isNull() )
2503 {
2504 const QString type3D = renderer3DElem.attribute( QStringLiteral( "type" ) );
2506 if ( meta3D )
2507 {
2508 r3D = meta3D->createRenderer( renderer3DElem, context );
2509 }
2510 }
2511 setRenderer3D( r3D );
2512 }
2513
2514 if ( categories.testFlag( CustomProperties ) )
2515 {
2516 // read custom properties before passing reading further to a subclass, so that
2517 // the subclass can also read custom properties
2518 readCustomProperties( layerElement );
2519 }
2520
2521 // use scale dependent visibility flag
2522 if ( categories.testFlag( Rendering ) )
2523 {
2524 setScaleBasedVisibility( layerElement.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
2525 if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
2526 {
2527 // older element, when scales were reversed
2528 setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
2529 setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
2530 }
2531 else
2532 {
2533 setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
2534 setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
2535 }
2536 }
2537
2538 if ( categories.testFlag( LayerConfiguration ) )
2539 {
2540 // flags
2541 const QDomElement flagsElem = layerElement.firstChildElement( QStringLiteral( "flags" ) );
2542 LayerFlags flags = mFlags;
2543 const auto enumMap = qgsEnumMap<QgsMapLayer::LayerFlag>();
2544 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
2545 {
2546 const QDomNode flagNode = flagsElem.namedItem( it.value() );
2547 if ( flagNode.isNull() )
2548 continue;
2549 const bool flagValue = flagNode.toElement().text() == "1" ? true : false;
2550 if ( flags.testFlag( it.key() ) && !flagValue )
2551 flags &= ~it.key();
2552 else if ( !flags.testFlag( it.key() ) && flagValue )
2553 flags |= it.key();
2554 }
2555 setFlags( flags );
2556 }
2557
2558 if ( categories.testFlag( Temporal ) )
2559 {
2560 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Temporal" ) );
2561
2563 properties->readXml( layerElement.toElement(), context );
2564 }
2565
2566 if ( categories.testFlag( Elevation ) )
2567 {
2568 const QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Elevation" ) );
2569
2571 properties->readXml( layerElement.toElement(), context );
2572 }
2573
2574 if ( categories.testFlag( Notes ) )
2575 {
2576 const QDomElement notesElem = layerElement.firstChildElement( QStringLiteral( "userNotes" ) );
2577 if ( !notesElem.isNull() )
2578 {
2579 const QString notes = notesElem.attribute( QStringLiteral( "value" ) );
2580 QgsLayerNotesUtils::setLayerNotes( this, notes );
2581 }
2582 }
2583}
2584
2586{
2588
2589 return mUndoStack;
2590}
2591
2593{
2595
2596 return mUndoStackStyles;
2597}
2598
2600{
2602
2603 return mCustomProperties.keys();
2604}
2605
2606void QgsMapLayer::setCustomProperty( const QString &key, const QVariant &value )
2607{
2609
2610 if ( !mCustomProperties.contains( key ) || mCustomProperties.value( key ) != value )
2611 {
2612 mCustomProperties.setValue( key, value );
2613 emit customPropertyChanged( key );
2614 }
2615}
2616
2618{
2620
2621 mCustomProperties = properties;
2622 for ( const QString &key : mCustomProperties.keys() )
2623 {
2624 emit customPropertyChanged( key );
2625 }
2626}
2627
2629{
2631
2632 return mCustomProperties;
2633}
2634
2635QVariant QgsMapLayer::customProperty( const QString &value, const QVariant &defaultValue ) const
2636{
2637 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
2639
2640 return mCustomProperties.value( value, defaultValue );
2641}
2642
2643void QgsMapLayer::removeCustomProperty( const QString &key )
2644{
2646
2647 if ( mCustomProperties.contains( key ) )
2648 {
2649 mCustomProperties.remove( key );
2650 emit customPropertyChanged( key );
2651 }
2652}
2653
2654int QgsMapLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError )
2655{
2657
2658 return QgsProviderRegistry::instance()->listStyles( mProviderKey, mDataSource, ids, names, descriptions, msgError );
2659}
2660
2661QString QgsMapLayer::getStyleFromDatabase( const QString &styleId, QString &msgError )
2662{
2664
2665 return QgsProviderRegistry::instance()->getStyleById( mProviderKey, mDataSource, styleId, msgError );
2666}
2667
2668bool QgsMapLayer::deleteStyleFromDatabase( const QString &styleId, QString &msgError )
2669{
2671
2673}
2674
2675void QgsMapLayer::saveStyleToDatabase( const QString &name, const QString &description,
2676 bool useAsDefault, const QString &uiFileContent, QString &msgError, QgsMapLayer::StyleCategories categories )
2677{
2679
2680 QString sldStyle, qmlStyle;
2681 QDomDocument qmlDocument, sldDocument;
2682 QgsReadWriteContext context;
2683 exportNamedStyle( qmlDocument, msgError, context, categories );
2684 if ( !msgError.isNull() )
2685 {
2686 return;
2687 }
2688 qmlStyle = qmlDocument.toString();
2689
2690 this->exportSldStyle( sldDocument, msgError );
2691 if ( !msgError.isNull() )
2692 {
2693 return;
2694 }
2695 sldStyle = sldDocument.toString();
2696
2698 mDataSource, qmlStyle, sldStyle, name,
2699 description, uiFileContent, useAsDefault, msgError );
2700}
2701
2702QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &resultFlag, bool loadFromLocalDB, QgsMapLayer::StyleCategories categories, Qgis::LoadStyleFlags flags )
2703{
2705
2706 QString returnMessage;
2707 QString qml, errorMsg;
2708 QString styleName;
2709 if ( !loadFromLocalDB && dataProvider() && dataProvider()->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2710 {
2712 }
2713
2714 // Style was successfully loaded from provider storage
2715 if ( !qml.isEmpty() )
2716 {
2717 QDomDocument myDocument( QStringLiteral( "qgis" ) );
2718 myDocument.setContent( qml );
2719 resultFlag = importNamedStyle( myDocument, errorMsg );
2720 returnMessage = QObject::tr( "Loaded from Provider" );
2721 }
2722 else
2723 {
2725
2726 bool styleExists = false;
2727 bool styleSuccessfullyLoaded = false;
2728
2729 returnMessage = loadNamedProperty( theURI, PropertyType::Style, styleExists, styleSuccessfullyLoaded, categories, flags );
2730
2731 // TODO QGIS 4.0 -- fix API for loadNamedStyle so we can return styleExists too
2732 ( void )styleExists;
2733 resultFlag = styleSuccessfullyLoaded;
2734 }
2735
2736 if ( ! styleName.isEmpty() )
2737 {
2738 styleManager()->renameStyle( styleManager()->currentStyle(), styleName );
2739 }
2740
2741 if ( resultFlag )
2742 emit styleLoaded( categories );
2743
2744 return returnMessage;
2745}
2746
2753
2755{
2757
2758 return false;
2759}
2760
2762{
2764
2765 return false;
2766}
2767
2769{
2771
2772 return true;
2773}
2774
2776{
2778
2779 // invalid layers are temporary? -- who knows?!
2780 if ( !isValid() )
2781 return false;
2782
2783 if ( mProviderKey == QLatin1String( "memory" ) )
2784 return true;
2785
2786 const QVariantMap sourceParts = QgsProviderRegistry::instance()->decodeUri( mProviderKey, mDataSource );
2787 const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
2788 if ( path.isEmpty() )
2789 return false;
2790
2791 // check if layer path is inside one of the standard temporary file locations for this platform
2792 const QStringList tempPaths = QStandardPaths::standardLocations( QStandardPaths::TempLocation );
2793 for ( const QString &tempPath : tempPaths )
2794 {
2795 if ( path.startsWith( tempPath ) )
2796 return true;
2797 }
2798
2799 return false;
2800}
2801
2802void QgsMapLayer::setValid( bool valid )
2803{
2805
2806 if ( mValid == valid )
2807 return;
2808
2809 mValid = valid;
2810 emit isValidChanged();
2811}
2812
2814{
2816
2817 if ( legend == mLegend )
2818 return;
2819
2820 delete mLegend;
2821 mLegend = legend;
2822
2823 if ( mLegend )
2824 {
2825 mLegend->setParent( this );
2826 connect( mLegend, &QgsMapLayerLegend::itemsChanged, this, &QgsMapLayer::legendChanged, Qt::UniqueConnection );
2827 }
2828
2829 emit legendChanged();
2830}
2831
2833{
2835
2836 return mLegend;
2837}
2838
2840{
2842
2843 return mStyleManager;
2844}
2845
2847{
2849
2850 if ( renderer == m3DRenderer )
2851 return;
2852
2853 delete m3DRenderer;
2854 m3DRenderer = renderer;
2855 emit renderer3DChanged();
2856 emit repaintRequested();
2858}
2859
2861{
2863
2864 return m3DRenderer;
2865}
2866
2867void QgsMapLayer::triggerRepaint( bool deferredUpdate )
2868{
2870
2871 if ( mRepaintRequestedFired )
2872 return;
2873 mRepaintRequestedFired = true;
2874 emit repaintRequested( deferredUpdate );
2875 mRepaintRequestedFired = false;
2876}
2877
2884
2886{
2888
2889 mMetadata = metadata;
2890// mMetadata.saveToLayer( this );
2891 emit metadataChanged();
2892}
2893
2895{
2897
2898 return QString();
2899}
2900
2901QDateTime QgsMapLayer::timestamp() const
2902{
2904
2905 return QDateTime();
2906}
2907
2915
2917{
2918 updateExtent( extent );
2919}
2920
2922{
2924
2925 updateExtent( extent );
2926}
2927
2928bool QgsMapLayer::isReadOnly() const
2929{
2931
2932 return true;
2933}
2934
2936{
2938
2939 return mOriginalXmlProperties;
2940}
2941
2942void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
2943{
2945
2946 mOriginalXmlProperties = originalXmlProperties;
2947}
2948
2949QString QgsMapLayer::generateId( const QString &layerName )
2950{
2951 // Generate the unique ID of this layer
2952 const QString uuid = QUuid::createUuid().toString();
2953 // trim { } from uuid
2954 QString id = layerName + '_' + uuid.mid( 1, uuid.length() - 2 );
2955 // Tidy the ID up to avoid characters that may cause problems
2956 // elsewhere (e.g in some parts of XML). Replaces every non-word
2957 // character (word characters are the alphabet, numbers and
2958 // underscore) with an underscore.
2959 // Note that the first backslash in the regular expression is
2960 // there for the compiler, so the pattern is actually \W
2961 const thread_local QRegularExpression idRx( QStringLiteral( "[\\W]" ) );
2962 id.replace( idRx, QStringLiteral( "_" ) );
2963 return id;
2964}
2965
2967{
2969
2970 return true;
2971}
2972
2974{
2976
2977 return mapTipsEnabled() && !mMapTipTemplate.isEmpty();
2978}
2979
2980void QgsMapLayer::setProviderType( const QString &providerType )
2981{
2983
2985}
2986
2987QSet<QgsMapLayerDependency> QgsMapLayer::dependencies() const
2988{
2990
2991 return mDependencies;
2992}
2993
2994bool QgsMapLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
2995{
2997
2998 QSet<QgsMapLayerDependency> deps;
2999 const auto constODeps = oDeps;
3000 for ( const QgsMapLayerDependency &dep : constODeps )
3001 {
3002 if ( dep.origin() == QgsMapLayerDependency::FromUser )
3003 deps << dep;
3004 }
3005
3006 mDependencies = deps;
3007 emit dependenciesChanged();
3008 return true;
3009}
3010
3012{
3014
3015 QgsDataProvider *lDataProvider = dataProvider();
3016
3017 if ( !lDataProvider )
3018 return;
3019
3020 if ( enabled && !isRefreshOnNotifyEnabled() )
3021 {
3022 lDataProvider->setListening( enabled );
3023 connect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
3024 }
3025 else if ( !enabled && isRefreshOnNotifyEnabled() )
3026 {
3027 // we don't want to disable provider listening because someone else could need it (e.g. actions)
3028 disconnect( lDataProvider, &QgsDataProvider::notify, this, &QgsMapLayer::onNotified );
3029 }
3030 mIsRefreshOnNofifyEnabled = enabled;
3031}
3032
3034{
3036
3037 if ( QgsMapLayerStore *store = qobject_cast<QgsMapLayerStore *>( parent() ) )
3038 {
3039 return qobject_cast<QgsProject *>( store->parent() );
3040 }
3041 return nullptr;
3042}
3043
3044void QgsMapLayer::onNotified( const QString &message )
3045{
3047
3048 if ( refreshOnNotifyMessage().isEmpty() || refreshOnNotifyMessage() == message )
3049 {
3051 emit dataChanged();
3052 }
3053}
3054
3055QgsRectangle QgsMapLayer::wgs84Extent( bool forceRecalculate ) const
3056{
3058
3060
3061 if ( ! forceRecalculate && ! mWgs84Extent.isNull() )
3062 {
3063 wgs84Extent = mWgs84Extent;
3064 }
3065 else if ( ! mExtent2D.isNull() || ! mExtent3D.isNull() )
3066 {
3068 transformer.setBallparkTransformsAreAppropriate( true );
3069 try
3070 {
3071 if ( mExtent2D.isNull() )
3072 wgs84Extent = transformer.transformBoundingBox( mExtent3D.toRectangle() );
3073 else
3074 wgs84Extent = transformer.transformBoundingBox( mExtent2D );
3075 }
3076 catch ( const QgsCsException &cse )
3077 {
3078 QgsMessageLog::logMessage( tr( "Error transforming extent: %1" ).arg( cse.what() ) );
3080 }
3081 }
3082 return wgs84Extent;
3083}
3084
3085void QgsMapLayer::updateExtent( const QgsRectangle &extent ) const
3086{
3088
3089 if ( extent == mExtent2D )
3090 return;
3091
3092 mExtent2D = extent;
3093
3094 // do not update the wgs84 extent if we trust layer metadata
3096 return;
3097
3098 mWgs84Extent = wgs84Extent( true );
3099}
3100
3101void QgsMapLayer::updateExtent( const QgsBox3D &extent ) const
3102{
3104
3105 if ( extent == mExtent3D )
3106 return;
3107
3108 if ( extent.isNull() )
3109 {
3110 if ( !extent.toRectangle().isNull() )
3111 {
3112 // bad 3D extent param but valid in 2d --> update 2D extent
3113 updateExtent( extent.toRectangle() );
3114 }
3115 else
3116 {
3117 QgsDebugMsgLevel( QStringLiteral( "Unable to update extent with empty parameter" ), 1 );
3118 }
3119 }
3120 else
3121 {
3122 mExtent3D = extent;
3123
3124 // do not update the wgs84 extent if we trust layer metadata
3126 return;
3127
3128 mWgs84Extent = wgs84Extent( true );
3129 }
3130}
3131
3132bool QgsMapLayer::rebuildCrs3D( QString *error )
3133{
3134 bool res = true;
3135 if ( !mCRS.isValid() )
3136 {
3138 }
3139 else if ( !mVerticalCrs.isValid() )
3140 {
3141 mCrs3D = mCRS;
3142 }
3143 else
3144 {
3145 switch ( mCRS.type() )
3146 {
3150 mCrs3D = mCRS;
3151 break;
3152
3154 {
3155 QString tempError;
3156 mCrs3D = mCRS.hasVerticalAxis() ? mCRS : QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3157 res = mCrs3D.isValid();
3158 break;
3159 }
3160
3162 // nonsense situation
3164 res = false;
3165 break;
3166
3175 {
3176 QString tempError;
3177 mCrs3D = QgsCoordinateReferenceSystem::createCompoundCrs( mCRS, mVerticalCrs, error ? *error : tempError );
3178 res = mCrs3D.isValid();
3179 break;
3180 }
3181 }
3182 }
3183 return res;
3184}
3185
3187{
3189
3190 // do not update the wgs84 extent if we trust layer metadata
3192 return;
3193
3194 mWgs84Extent = QgsRectangle();
3195}
3196
3198{
3200
3201 QString metadata = QStringLiteral( "<h1>" ) + tr( "General" ) + QStringLiteral( "</h1>\n<hr>\n" ) + QStringLiteral( "<table class=\"list-view\">\n" );
3202
3203 // name
3204 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + name() + QStringLiteral( "</td></tr>\n" );
3205
3206 QString path;
3207 bool isLocalPath = false;
3208 if ( dataProvider() )
3209 {
3210 // local path
3211 QVariantMap uriComponents = QgsProviderRegistry::instance()->decodeUri( dataProvider()->name(), publicSource() );
3212 if ( uriComponents.contains( QStringLiteral( "path" ) ) )
3213 {
3214 path = uriComponents[QStringLiteral( "path" )].toString();
3215 QFileInfo fi( path );
3216 if ( fi.exists() )
3217 {
3218 isLocalPath = true;
3219 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" );
3220
3221 QDateTime lastModified = fi.lastModified();
3222 QString lastModifiedFileName;
3223 QSet<QString> sidecarFiles = QgsFileUtils::sidecarFilesForPath( path );
3224 if ( fi.isFile() )
3225 {
3226 qint64 fileSize = fi.size();
3227 if ( !sidecarFiles.isEmpty() )
3228 {
3229 lastModifiedFileName = fi.fileName();
3230 QStringList sidecarFileNames;
3231 for ( const QString &sidecarFile : sidecarFiles )
3232 {
3233 QFileInfo sidecarFi( sidecarFile );
3234 fileSize += sidecarFi.size();
3235 if ( sidecarFi.lastModified() > lastModified )
3236 {
3237 lastModified = sidecarFi.lastModified();
3238 lastModifiedFileName = sidecarFi.fileName();
3239 }
3240 sidecarFileNames << sidecarFi.fileName();
3241 }
3242 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" );
3243 }
3244 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" );
3245 }
3246 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" );
3247 }
3248 }
3249 if ( uriComponents.contains( QStringLiteral( "url" ) ) )
3250 {
3251 const QString url = uriComponents[QStringLiteral( "url" )].toString();
3252 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" );
3253 }
3254 }
3255
3256 // data source
3257 if ( publicSource() != path || !isLocalPath )
3258 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Source" ) + QStringLiteral( "</td><td>%1" ).arg( publicSource() != path ? publicSource() : path ) + QStringLiteral( "</td></tr>\n" );
3259
3260 // provider
3261 if ( dataProvider() )
3262 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Provider" ) + QStringLiteral( "</td><td>%1" ).arg( dataProvider()->name() ) + QStringLiteral( "</td></tr>\n" );
3263
3264 metadata += QLatin1String( "</table>\n<br><br>" );
3265
3266 // custom properties
3267 if ( const auto keys = customPropertyKeys(); !keys.isEmpty() )
3268 {
3269 metadata += QStringLiteral( "<h1>" ) + tr( "Custom Properties" ) + QStringLiteral( "</h1>\n<hr>\n" );
3270 metadata += QLatin1String( "<table class=\"list-view\">\n<tbody>" );
3271 for ( const QString &key : keys )
3272 {
3273 // keys prefaced with _ are considered private/internal details
3274 if ( key.startsWith( '_' ) )
3275 continue;
3276
3277 const QVariant propValue = customProperty( key );
3278 QString stringValue;
3279 if ( propValue.type() == QVariant::List || propValue.type() == QVariant::StringList )
3280 {
3281 for ( const QString &s : propValue.toStringList() )
3282 {
3283 stringValue += "<p style=\"margin: 0;\">" + s.toHtmlEscaped() + "</p>";
3284 }
3285 }
3286 else
3287 {
3288 stringValue = propValue.toString().toHtmlEscaped();
3289
3290 //if the result string is empty but propValue is not, the conversion has failed
3291 if ( stringValue.isEmpty() && !QgsVariantUtils::isNull( propValue ) )
3292 stringValue = tr( "<i>value cannot be displayed</i>" );
3293 }
3294
3295 metadata += QStringLiteral( "<tr><td class=\"highlight\">%1</td><td>%2</td></tr>" ).arg( key.toHtmlEscaped(), stringValue );
3296 }
3297 metadata += QLatin1String( "</tbody></table>\n" );
3298 metadata += QLatin1String( "<br><br>\n" );
3299 }
3300
3301 return metadata;
3302}
3303
3305{
3307
3308 QString metadata = QStringLiteral( "<h1>" ) + tr( "Coordinate Reference System (CRS)" ) + QStringLiteral( "</h1>\n<hr>\n" );
3309 metadata += QLatin1String( "<table class=\"list-view\">\n" );
3310
3311 // Identifier
3313 if ( !c.isValid() )
3314 metadata += QStringLiteral( "<tr><td colspan=\"2\" class=\"highlight\">" ) + tr( "Unknown" ) + QStringLiteral( "</td></tr>\n" );
3315 else
3316 {
3317 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Name" ) + QStringLiteral( "</td><td>" ) + c.userFriendlyIdentifier( Qgis::CrsIdentifierType::FullString ) + QStringLiteral( "</td></tr>\n" );
3318
3319 // map units
3320 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Units" ) + QStringLiteral( "</td><td>" )
3321 + ( c.isGeographic() ? tr( "Geographic (uses latitude and longitude for coordinates)" ) : QgsUnitTypes::toString( c.mapUnits() ) )
3322 + QStringLiteral( "</td></tr>\n" );
3323
3324 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Type" ) + QStringLiteral( "</td><td>" ) + QgsCoordinateReferenceSystemUtils::crsTypeToString( c.type() ) + QStringLiteral( "</td></tr>\n" );
3325
3326 // operation
3327 const QgsProjOperation operation = c.operation();
3328 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Method" ) + QStringLiteral( "</td><td>" ) + operation.description() + QStringLiteral( "</td></tr>\n" );
3329
3330 // celestial body
3331 try
3332 {
3333 const QString celestialBody = c.celestialBodyName();
3334 if ( !celestialBody.isEmpty() )
3335 {
3336 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Celestial Body" ) + QStringLiteral( "</td><td>" ) + celestialBody + QStringLiteral( "</td></tr>\n" );
3337 }
3338 }
3339 catch ( QgsNotSupportedException & )
3340 {
3341
3342 }
3343
3344 QString accuracyString;
3345 // dynamic crs with no epoch?
3346 if ( c.isDynamic() && std::isnan( c.coordinateEpoch() ) )
3347 {
3348 accuracyString = tr( "Based on a dynamic CRS, but no coordinate epoch is set. Coordinates are ambiguous and of limited accuracy." );
3349 }
3350
3351 // based on datum ensemble?
3352 try
3353 {
3354 const QgsDatumEnsemble ensemble = c.datumEnsemble();
3355 if ( ensemble.isValid() )
3356 {
3357 QString id;
3358 if ( !ensemble.code().isEmpty() )
3359 id = QStringLiteral( "<i>%1</i> (%2:%3)" ).arg( ensemble.name(), ensemble.authority(), ensemble.code() );
3360 else
3361 id = QStringLiteral( "<i>%</i>”" ).arg( ensemble.name() );
3362
3363 if ( ensemble.accuracy() > 0 )
3364 {
3365 accuracyString = tr( "Based on %1, which has a limited accuracy of <b>at best %2 meters</b>." ).arg( id ).arg( ensemble.accuracy() );
3366 }
3367 else
3368 {
3369 accuracyString = tr( "Based on %1, which has a limited accuracy." ).arg( id );
3370 }
3371 }
3372 }
3373 catch ( QgsNotSupportedException & )
3374 {
3375
3376 }
3377
3378 if ( !accuracyString.isEmpty() )
3379 {
3380 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Accuracy" ) + QStringLiteral( "</td><td>" ) + accuracyString + QStringLiteral( "</td></tr>\n" );
3381 }
3382
3383 // static/dynamic
3384 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)" ) );
3385
3386 // coordinate epoch
3387 if ( !std::isnan( c.coordinateEpoch() ) )
3388 {
3389 metadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Coordinate Epoch" ) + QStringLiteral( "</td><td>%1</td></tr>\n" ).arg( qgsDoubleToString( c.coordinateEpoch(), 3 ) );
3390 }
3391 }
3392
3393 metadata += QLatin1String( "</table>\n<br><br>\n" );
3394 return metadata;
3395}
static QString version()
Version string.
Definition qgis.cpp:258
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition qgis.h:5143
@ Vertical
Vertical CRS.
@ Temporal
Temporal CRS.
@ Compound
Compound (horizontal + vertical) CRS.
@ Projected
Projected CRS.
@ Other
Other type.
@ Bound
Bound CRS.
@ DerivedProjected
Derived projected CRS.
@ Unknown
Unknown type.
@ Engineering
Engineering CRS.
@ Geographic3d
3D geopraphic CRS
@ Geodetic
Geodetic CRS.
@ Geographic2d
2D geographic CRS
@ Geocentric
Geocentric CRS.
@ 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:274
@ 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.
QFlags< MapLayerProperty > MapLayerProperties
Map layer properties.
Definition qgis.h:1918
QFlags< LoadStyleFlag > LoadStyleFlags
Flags for loading layer styles.
Definition qgis.h:170
@ IgnoreMissingStyleErrors
If the style is missing, then don't flag it as an error. This flag can be used when the caller is not...
AutoRefreshMode
Map layer automatic refresh modes.
Definition qgis.h:1928
@ 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.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
Definition qgsbox3d.h:338
bool isNull() const
Test if the box is null (holding no spatial information).
Definition qgsbox3d.cpp:289
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.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool hasVerticalAxis() const
Returns true if the CRS has a vertical axis.
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 QgsCoordinateReferenceSystem createCompoundCrs(const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error)
Given a horizontal and vertical CRS, attempts to create a compound CRS from them.
static void setCustomCrsValidation(CUSTOM_CRS_VALIDATION f)
Sets custom function to force valid CRS.
void setValidationHint(const QString &html)
Set user hint for validation.
QgsCoordinateReferenceSystem verticalCrs() const
Returns the vertical CRS associated with this CRS object.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
Qgis::CrsType type() const
Returns the type of the CRS.
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.
virtual bool containsElevationData() const
Returns true if the data provider definitely contains elevation related data.
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.
virtual QString name() const =0
Returns a provider name.
QFlags< ReadFlag > ReadFlags
void notify(const QString &msg)
Emitted when the datasource issues a notification.
virtual QgsDataProviderElevationProperties * elevationProperties()
Returns the provider's elevation properties.
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:75
void crs3DChanged()
Emitted when the crs3D() of the layer has changed.
Q_DECL_DEPRECATED 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:79
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.
bool setId(const QString &id)
Sets the layer's id.
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.
QFlags< ReadFlag > ReadFlags
QFlags< LayerFlag > LayerFlags
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Import the properties of this layer from a QDomDocument.
Q_DECL_DEPRECATED 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.
virtual void setExtent3D(const QgsBox3D &box)
Sets the extent.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
Qgis::AutoRefreshMode autoRefreshMode() const
Returns the layer's automatic 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 loadNamedStyle(const QString &theURI, bool &resultFlag, bool loadFromLocalDb, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories, Qgis::LoadStyleFlags flags=Qgis::LoadStyleFlags())
Loads a named style from file/local db/datasource db.
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.
QgsCoordinateReferenceSystem crs3D
Definition qgsmaplayer.h:84
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:82
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.
Q_DECL_DEPRECATED 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.
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.
QString id
Definition qgsmaplayer.h:78
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.
Q_DECL_DEPRECATED 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:81
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.
Q_DECL_DEPRECATED 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:85
Q_DECL_DEPRECATED 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.
Q_DECL_DEPRECATED 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....
Q_DECL_DEPRECATED void setKeywordList(const QString &keywords)
Sets the keyword list of the layerused by QGIS Server in GetCapabilities request.
Q_DECL_DEPRECATED void setAttribution(const QString &attrib)
Sets the attribution of the layerused 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,...
Q_DECL_DEPRECATED 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.
Q_DECL_DEPRECATED void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layer used by QGIS Server in GetCapabilities request.
QString mLegendUrlFormat
QFlags< StyleCategory > StyleCategories
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.
Q_DECL_DEPRECATED QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
void crsChanged()
Emitted when the crs() of the layer has changed.
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...
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.
void idChanged(const QString &id)
Emitted when the layer's ID has been changed.
Q_DECL_DEPRECATED 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.
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.
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.
void verticalCrsChanged()
Emitted when the verticalCrs() of the layer has changed.
virtual QgsBox3D extent3D() const
Returns the 3D extent of the layer.
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.
Q_DECL_DEPRECATED 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.
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.
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.
bool setVerticalCrs(const QgsCoordinateReferenceSystem &crs, QString *errorMessage=nullptr)
Sets the layer's vertical coordinate reference system.
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:89
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:87
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:80
QgsCoordinateReferenceSystem verticalCrs
Definition qgsmaplayer.h:83
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
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.
~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.
Q_DECL_DEPRECATED 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:88
Q_DECL_DEPRECATED 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.
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 absoluteToRelativeUri(const QString &providerKey, const QString &uri, const QgsReadWriteContext &context) const
Converts absolute path(s) to relative path(s) in the given provider-specific URI.
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.
QString relativeToAbsoluteUri(const QString &providerKey, const QString &uri, const QgsReadWriteContext &context) const
Converts relative path(s) to absolute path(s) in the given provider-specific URI.
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.
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.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
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.
static QDomElement writeBox3D(const QgsBox3D &box, QDomDocument &doc, const QString &elementName=QStringLiteral("extent3D"))
Encodes a 3D box to a DOM element.
static QgsBox3D readBox3D(const QDomElement &element)
Decodes a DOM element to a 3D box.
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:5655
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5362
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:5636
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5445
CONSTLATIN1STRING geoEpsgCrsAuthId()
Geographic coord sys from EPSG authority.
Definition qgis.h:5934
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
const QgsCoordinateReferenceSystem & crs
Setting options for creating vector data providers.
QString format
Format specification of online resource.