QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
Loading...
Searching...
No Matches
qgsvectorlayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayer.cpp
3 --------------------
4 begin : Oct 29, 2003
5 copyright : (C) 2003 by Gary E.Sherman
6 email : sherman at mrcc.com
7
8 This class implements a generic means to display vector layers. The features
9 and attributes are read from the data store using a "data provider" plugin.
10 QgsVectorLayer can be used with any data store for which an appropriate
11 plugin is available.
12
13***************************************************************************/
14
15/***************************************************************************
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 ***************************************************************************/
23
24#include "qgis.h" //for globals
25#include "qgssettings.h"
26#include "qgsvectorlayer.h"
27#include "qgsactionmanager.h"
28#include "qgsapplication.h"
29#include "qgsconditionalstyle.h"
31#include "qgscurve.h"
32#include "qgsdatasourceuri.h"
35#include "qgsfeature.h"
36#include "qgsfeaturerequest.h"
37#include "qgsfields.h"
38#include "qgsmaplayerfactory.h"
40#include "qgsgeometry.h"
42#include "qgslogger.h"
43#include "qgsmaplayerlegend.h"
44#include "qgsmessagelog.h"
45#include "qgsogcutils.h"
46#include "qgspainting.h"
47#include "qgspointxy.h"
48#include "qgsproject.h"
49#include "qgsproviderregistry.h"
50#include "qgsrectangle.h"
51#include "qgsrelationmanager.h"
52#include "qgsweakrelation.h"
53#include "qgsrendercontext.h"
66#include "qgspoint.h"
67#include "qgsrenderer.h"
68#include "qgssymbollayer.h"
69#include "qgsdiagramrenderer.h"
70#include "qgspallabeling.h"
74#include "qgsfeedback.h"
75#include "qgsxmlutils.h"
76#include "qgstaskmanager.h"
77#include "qgstransaction.h"
78#include "qgsauxiliarystorage.h"
79#include "qgsgeometryoptions.h"
81#include "qgsruntimeprofiler.h"
83#include "qgsvectorlayerutils.h"
85#include "qgsprofilerequest.h"
86#include "qgssymbollayerutils.h"
87#include "qgsthreadingutils.h"
88
89#include <QDir>
90#include <QFile>
91#include <QImage>
92#include <QPainter>
93#include <QPainterPath>
94#include <QPolygonF>
95#include <QProgressDialog>
96#include <QString>
97#include <QDomNode>
98#include <QVector>
99#include <QStringBuilder>
100#include <QUrl>
101#include <QUndoCommand>
102#include <QUrlQuery>
103#include <QUuid>
104#include <QRegularExpression>
105#include <QTimer>
106
107#include <limits>
108#include <optional>
109
111#include "qgssettingsentryimpl.h"
112#include "qgssettingstree.h"
113
119
120
121#ifdef TESTPROVIDERLIB
122#include <dlfcn.h>
123#endif
124
125typedef bool saveStyle_t(
126 const QString &uri,
127 const QString &qmlStyle,
128 const QString &sldStyle,
129 const QString &styleName,
130 const QString &styleDescription,
131 const QString &uiFileContent,
132 bool useAsDefault,
133 QString &errCause
134);
135
136typedef QString loadStyle_t(
137 const QString &uri,
138 QString &errCause
139);
140
141typedef int listStyles_t(
142 const QString &uri,
143 QStringList &ids,
144 QStringList &names,
145 QStringList &descriptions,
146 QString &errCause
147);
148
149typedef QString getStyleById_t(
150 const QString &uri,
151 QString styleID,
152 QString &errCause
153);
154
155typedef bool deleteStyleById_t(
156 const QString &uri,
157 QString styleID,
158 QString &errCause
159);
160
161
162QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
163 const QString &baseName,
164 const QString &providerKey,
165 const QgsVectorLayer::LayerOptions &options )
166 : QgsMapLayer( Qgis::LayerType::Vector, baseName, vectorLayerPath )
167 , mSelectionProperties( new QgsVectorLayerSelectionProperties( this ) )
168 , mTemporalProperties( new QgsVectorLayerTemporalProperties( this ) )
169 , mElevationProperties( new QgsVectorLayerElevationProperties( this ) )
170 , mAuxiliaryLayer( nullptr )
171 , mAuxiliaryLayerKey( QString() )
172 , mReadExtentFromXml( options.readExtentFromXml )
173 , mRefreshRendererTimer( new QTimer( this ) )
174{
176 mLoadAllStoredStyle = options.loadAllStoredStyles;
177
178 if ( options.fallbackCrs.isValid() )
179 setCrs( options.fallbackCrs, false );
180 mWkbType = options.fallbackWkbType;
181
182 setProviderType( providerKey );
183
184 mGeometryOptions = std::make_unique<QgsGeometryOptions>();
185 mActions = new QgsActionManager( this );
186 mConditionalStyles = new QgsConditionalLayerStyles( this );
187 mStoredExpressionManager = new QgsStoredExpressionManager();
188 mStoredExpressionManager->setParent( this );
189
190 mJoinBuffer = new QgsVectorLayerJoinBuffer( this );
191 mJoinBuffer->setParent( this );
192 connect( mJoinBuffer, &QgsVectorLayerJoinBuffer::joinedFieldsChanged, this, &QgsVectorLayer::onJoinedFieldsChanged );
193
194 mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
195 // if we're given a provider type, try to create and bind one to this layer
196 if ( !vectorLayerPath.isEmpty() && !mProviderKey.isEmpty() )
197 {
198 QgsDataProvider::ProviderOptions providerOptions { options.transformContext };
199 QgsDataProvider::ReadFlags providerFlags = QgsDataProvider::ReadFlags();
200 if ( options.loadDefaultStyle )
201 {
203 }
204 if ( options.forceReadOnly )
205 {
206 providerFlags |= QgsDataProvider::ForceReadOnly;
207 mDataSourceReadOnly = true;
208 }
209 setDataSource( vectorLayerPath, baseName, providerKey, providerOptions, providerFlags );
210 }
211
212 for ( const QgsField &field : std::as_const( mFields ) )
213 {
214 if ( !mAttributeAliasMap.contains( field.name() ) )
215 mAttributeAliasMap.insert( field.name(), QString() );
216 }
217
218 if ( isValid() )
219 {
220 mTemporalProperties->setDefaultsFromDataProviderTemporalCapabilities( mDataProvider->temporalCapabilities() );
221 if ( !mTemporalProperties->isActive() )
222 {
223 // didn't populate temporal properties from provider metadata, so at least try to setup some initially nice
224 // selections
225 mTemporalProperties->guessDefaultsFromFields( mFields );
226 }
227
228 mElevationProperties->setDefaultsFromLayer( this );
229 }
230
231 connect( this, &QgsVectorLayer::selectionChanged, this, [this] { triggerRepaint(); } );
232 connect( QgsProject::instance()->relationManager(), &QgsRelationManager::relationsLoaded, this, &QgsVectorLayer::onRelationsLoaded );
233
237
238 // Default simplify drawing settings
239 QgsSettings settings;
240 mSimplifyMethod.setSimplifyHints( QgsVectorLayer::settingsSimplifyDrawingHints->valueWithDefaultOverride( mSimplifyMethod.simplifyHints() ) );
241 mSimplifyMethod.setSimplifyAlgorithm( QgsVectorLayer::settingsSimplifyAlgorithm->valueWithDefaultOverride( mSimplifyMethod.simplifyAlgorithm() ) );
242 mSimplifyMethod.setThreshold( QgsVectorLayer::settingsSimplifyDrawingTol->valueWithDefaultOverride( mSimplifyMethod.threshold() ) );
243 mSimplifyMethod.setForceLocalOptimization( QgsVectorLayer::settingsSimplifyLocal->valueWithDefaultOverride( mSimplifyMethod.forceLocalOptimization() ) );
244 mSimplifyMethod.setMaximumScale( QgsVectorLayer::settingsSimplifyMaxScale->valueWithDefaultOverride( mSimplifyMethod.maximumScale() ) );
245
246 connect( mRefreshRendererTimer, &QTimer::timeout, this, [this] { triggerRepaint( true ); } );
247}
248
250{
251 emit willBeDeleted();
252
253 setValid( false );
254
255 delete mDataProvider;
256 delete mEditBuffer;
257 delete mJoinBuffer;
258 delete mExpressionFieldBuffer;
259 delete mLabeling;
260 delete mDiagramLayerSettings;
261 delete mDiagramRenderer;
262
263 delete mActions;
264
265 delete mRenderer;
266 delete mConditionalStyles;
267 delete mStoredExpressionManager;
268
269 if ( mFeatureCounter )
270 mFeatureCounter->cancel();
271
272 qDeleteAll( mRendererGenerators );
273}
274
276{
278
280 // We get the data source string from the provider when
281 // possible because some providers may have changed it
282 // directly (memory provider does that).
283 QString dataSource;
284 if ( mDataProvider )
285 {
286 dataSource = mDataProvider->dataSourceUri();
287 options.transformContext = mDataProvider->transformContext();
288 }
289 else
290 {
291 dataSource = source();
292 }
293 options.forceReadOnly = mDataSourceReadOnly;
294 QgsVectorLayer *layer = new QgsVectorLayer( dataSource, name(), mProviderKey, options );
295 if ( mDataProvider && layer->dataProvider() )
296 {
297 layer->dataProvider()->handlePostCloneOperations( mDataProvider );
298 }
299 QgsMapLayer::clone( layer );
300 layer->mXmlExtent2D = mXmlExtent2D;
301 layer->mLazyExtent2D = mLazyExtent2D;
302 layer->mValidExtent2D = mValidExtent2D;
303 layer->mXmlExtent3D = mXmlExtent3D;
304 layer->mLazyExtent3D = mLazyExtent3D;
305 layer->mValidExtent3D = mValidExtent3D;
306
307 QList<QgsVectorLayerJoinInfo> joins = vectorJoins();
308 const auto constJoins = joins;
309 for ( const QgsVectorLayerJoinInfo &join : constJoins )
310 {
311 // do not copy join information for auxiliary layer
312 if ( !auxiliaryLayer()
313 || ( auxiliaryLayer() && auxiliaryLayer()->id() != join.joinLayerId() ) )
314 layer->addJoin( join );
315 }
316
317 if ( mDataProvider )
318 layer->setProviderEncoding( mDataProvider->encoding() );
319 layer->setSubsetString( subsetString() );
323 layer->setReadOnly( isReadOnly() );
328
329 const auto constActions = actions()->actions();
330 for ( const QgsAction &action : constActions )
331 {
332 layer->actions()->addAction( action );
333 }
334
335 if ( auto *lRenderer = renderer() )
336 {
337 layer->setRenderer( lRenderer->clone() );
338 }
339
340 if ( auto *lLabeling = labeling() )
341 {
342 layer->setLabeling( lLabeling->clone() );
343 }
345
347
348 if ( auto *lDiagramRenderer = diagramRenderer() )
349 {
350 layer->setDiagramRenderer( lDiagramRenderer->clone() );
351 }
352
353 if ( auto *lDiagramLayerSettings = diagramLayerSettings() )
354 {
355 layer->setDiagramLayerSettings( *lDiagramLayerSettings );
356 }
357
358 for ( int i = 0; i < fields().count(); i++ )
359 {
360 layer->setFieldAlias( i, attributeAlias( i ) );
362 layer->setEditorWidgetSetup( i, editorWidgetSetup( i ) );
365
366 QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength> constraints = fieldConstraintsAndStrength( i );
367 auto constraintIt = constraints.constBegin();
368 for ( ; constraintIt != constraints.constEnd(); ++ constraintIt )
369 {
370 layer->setFieldConstraint( i, constraintIt.key(), constraintIt.value() );
371 }
372
373 if ( fields().fieldOrigin( i ) == QgsFields::OriginExpression )
374 {
375 layer->addExpressionField( expressionField( i ), fields().at( i ) );
376 }
377 }
378
380
381 if ( auto *lAuxiliaryLayer = auxiliaryLayer() )
382 layer->setAuxiliaryLayer( lAuxiliaryLayer->clone( layer ) );
383
384 layer->mElevationProperties = mElevationProperties->clone();
385 layer->mElevationProperties->setParent( layer );
386
387 layer->mSelectionProperties = mSelectionProperties->clone();
388 layer->mSelectionProperties->setParent( layer );
389
390 return layer;
391}
392
394{
396
397 if ( mDataProvider )
398 {
399 return mDataProvider->storageType();
400 }
401 return QString();
402}
403
404
406{
408
409 if ( mDataProvider )
410 {
411 return mDataProvider->capabilitiesString();
412 }
413 return QString();
414}
415
417{
419
420 return mDataProvider && mDataProvider->isSqlQuery();
421}
422
423Qgis::VectorLayerTypeFlags QgsVectorLayer::vectorLayerTypeFlags() const
424{
426
427 return mDataProvider ? mDataProvider->vectorLayerTypeFlags() : Qgis::VectorLayerTypeFlags();
428}
429
431{
433
434 if ( mDataProvider )
435 {
436 return mDataProvider->dataComment();
437 }
438 return QString();
439}
440
447
449{
451
452 return name();
453}
454
456{
457 // non fatal for now -- the QgsVirtualLayerTask class is not thread safe and calls this
459
460 if ( mDataProvider )
461 {
462 mDataProvider->reloadData();
463 updateFields();
464 }
465}
466
468{
469 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
471
472 return new QgsVectorLayerRenderer( this, rendererContext );
473}
474
475
476void QgsVectorLayer::drawVertexMarker( double x, double y, QPainter &p, Qgis::VertexMarkerType type, int m )
477{
478 switch ( type )
479 {
481 p.setPen( QColor( 50, 100, 120, 200 ) );
482 p.setBrush( QColor( 200, 200, 210, 120 ) );
483 p.drawEllipse( x - m, y - m, m * 2 + 1, m * 2 + 1 );
484 break;
485
487 p.setPen( QColor( 255, 0, 0 ) );
488 p.drawLine( x - m, y + m, x + m, y - m );
489 p.drawLine( x - m, y - m, x + m, y + m );
490 break;
491
493 break;
494 }
495}
496
498{
500
501 mSelectedFeatureIds.insert( fid );
502 mPreviousSelectedFeatureIds.clear();
503
504 emit selectionChanged( QgsFeatureIds() << fid, QgsFeatureIds(), false );
505}
506
507void QgsVectorLayer::select( const QgsFeatureIds &featureIds )
508{
510
511 mSelectedFeatureIds.unite( featureIds );
512 mPreviousSelectedFeatureIds.clear();
513
514 emit selectionChanged( featureIds, QgsFeatureIds(), false );
515}
516
518{
520
521 mSelectedFeatureIds.remove( fid );
522 mPreviousSelectedFeatureIds.clear();
523
524 emit selectionChanged( QgsFeatureIds(), QgsFeatureIds() << fid, false );
525}
526
528{
530
531 mSelectedFeatureIds.subtract( featureIds );
532 mPreviousSelectedFeatureIds.clear();
533
534 emit selectionChanged( QgsFeatureIds(), featureIds, false );
535}
536
538{
540
541 // normalize the rectangle
542 rect.normalize();
543
544 QgsFeatureIds newSelection;
545
547 .setFilterRect( rect )
549 .setNoAttributes() );
550
551 QgsFeature feat;
552 while ( features.nextFeature( feat ) )
553 {
554 newSelection << feat.id();
555 }
556 features.close();
557
558 selectByIds( newSelection, behavior );
559}
560
561void QgsVectorLayer::selectByExpression( const QString &expression, Qgis::SelectBehavior behavior, QgsExpressionContext *context )
562{
564
565 QgsFeatureIds newSelection;
566
567 std::optional< QgsExpressionContext > defaultContext;
568 if ( !context )
569 {
570 defaultContext.emplace( QgsExpressionContextUtils::globalProjectLayerScopes( this ) );
571 context = &defaultContext.value();
572 }
573
575 {
577 .setExpressionContext( *context )
580
581 QgsFeatureIterator features = getFeatures( request );
582
583 if ( behavior == Qgis::SelectBehavior::AddToSelection )
584 {
585 newSelection = selectedFeatureIds();
586 }
587 QgsFeature feat;
588 while ( features.nextFeature( feat ) )
589 {
590 newSelection << feat.id();
591 }
592 features.close();
593 }
595 {
596 QgsExpression exp( expression );
597 exp.prepare( context );
599 QgsFeatureIds oldSelection = selectedFeatureIds();
600 QgsFeatureRequest request = QgsFeatureRequest().setFilterFids( oldSelection );
601
602 //refine request
603 if ( !exp.needsGeometry() )
606
607 QgsFeatureIterator features = getFeatures( request );
608 QgsFeature feat;
609 while ( features.nextFeature( feat ) )
610 {
611 context->setFeature( feat );
612 bool matches = exp.evaluate( context ).toBool();
613
614 if ( matches && behavior == Qgis::SelectBehavior::IntersectSelection )
615 {
616 newSelection << feat.id();
617 }
618 else if ( !matches && behavior == Qgis::SelectBehavior::RemoveFromSelection )
619 {
620 newSelection << feat.id();
621 }
622 }
623 }
624
625 selectByIds( newSelection );
626}
627
629{
631
632 QgsFeatureIds newSelection;
633
634 switch ( behavior )
635 {
637 newSelection = ids;
638 break;
639
641 newSelection = mSelectedFeatureIds + ids;
642 break;
643
645 newSelection = mSelectedFeatureIds - ids;
646 break;
647
649 newSelection = mSelectedFeatureIds.intersect( ids );
650 break;
651 }
652
653 QgsFeatureIds deselectedFeatures = mSelectedFeatureIds - newSelection;
654 mSelectedFeatureIds = newSelection;
655 mPreviousSelectedFeatureIds.clear();
656
657 emit selectionChanged( newSelection, deselectedFeatures, true );
658}
659
660void QgsVectorLayer::modifySelection( const QgsFeatureIds &selectIds, const QgsFeatureIds &deselectIds )
661{
663
664 QgsFeatureIds intersectingIds = selectIds & deselectIds;
665 if ( !intersectingIds.isEmpty() )
666 {
667 QgsDebugMsgLevel( QStringLiteral( "Trying to select and deselect the same item at the same time. Unsure what to do. Selecting dubious items." ), 3 );
668 }
669
670 mSelectedFeatureIds -= deselectIds;
671 mSelectedFeatureIds += selectIds;
672 mPreviousSelectedFeatureIds.clear();
673
674 emit selectionChanged( selectIds, deselectIds - intersectingIds, false );
675}
676
678{
680
682 ids.subtract( mSelectedFeatureIds );
683 selectByIds( ids );
684}
685
692
694{
696
697 // normalize the rectangle
698 rect.normalize();
699
701 .setFilterRect( rect )
703 .setNoAttributes() );
704
705 QgsFeatureIds selectIds;
706 QgsFeatureIds deselectIds;
707
708 QgsFeature fet;
709 while ( fit.nextFeature( fet ) )
710 {
711 if ( mSelectedFeatureIds.contains( fet.id() ) )
712 {
713 deselectIds << fet.id();
714 }
715 else
716 {
717 selectIds << fet.id();
718 }
719 }
720
721 modifySelection( selectIds, deselectIds );
722}
723
725{
727
728 if ( mSelectedFeatureIds.isEmpty() )
729 return;
730
731 const QgsFeatureIds previous = mSelectedFeatureIds;
733 mPreviousSelectedFeatureIds = previous;
734}
735
737{
739
740 if ( mPreviousSelectedFeatureIds.isEmpty() || !mSelectedFeatureIds.empty() )
741 return;
742
743 selectByIds( mPreviousSelectedFeatureIds );
744}
745
747{
748 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
750
751 return mDataProvider;
752}
753
755{
756 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
758
759 return mDataProvider;
760}
761
763{
764 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
766
767 return mSelectionProperties;
768}
769
776
783
785{
787
788 QgsProfileRequest modifiedRequest( request );
789 modifiedRequest.expressionContext().appendScope( createExpressionContextScope() );
790 return new QgsVectorLayerProfileGenerator( this, modifiedRequest );
791}
792
793void QgsVectorLayer::setProviderEncoding( const QString &encoding )
794{
796
797 if ( isValid() && mDataProvider && mDataProvider->encoding() != encoding )
798 {
799 mDataProvider->setEncoding( encoding );
800 updateFields();
801 }
802}
803
805{
807
808 delete mDiagramRenderer;
809 mDiagramRenderer = r;
810 emit rendererChanged();
811 emit styleChanged();
812}
813
815{
816 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
818
819 return QgsWkbTypes::geometryType( mWkbType );
820}
821
823{
825
826 return mWkbType;
827}
828
830{
832
833 if ( !isValid() || !isSpatial() || mSelectedFeatureIds.isEmpty() || !mDataProvider ) //no selected features
834 {
835 return QgsRectangle( 0, 0, 0, 0 );
836 }
837
838 QgsRectangle r, retval;
839 retval.setNull();
840
841 QgsFeature fet;
842 if ( mDataProvider->capabilities() & QgsVectorDataProvider::SelectAtId )
843 {
845 .setFilterFids( mSelectedFeatureIds )
846 .setNoAttributes() );
847
848 while ( fit.nextFeature( fet ) )
849 {
850 if ( !fet.hasGeometry() )
851 continue;
852 r = fet.geometry().boundingBox();
853 retval.combineExtentWith( r );
854 }
855 }
856 else
857 {
859 .setNoAttributes() );
860
861 while ( fit.nextFeature( fet ) )
862 {
863 if ( mSelectedFeatureIds.contains( fet.id() ) )
864 {
865 if ( fet.hasGeometry() )
866 {
867 r = fet.geometry().boundingBox();
868 retval.combineExtentWith( r );
869 }
870 }
871 }
872 }
873
874 if ( retval.width() == 0.0 || retval.height() == 0.0 )
875 {
876 // If all of the features are at the one point, buffer the
877 // rectangle a bit. If they are all at zero, do something a bit
878 // more crude.
879
880 if ( retval.xMinimum() == 0.0 && retval.xMaximum() == 0.0 &&
881 retval.yMinimum() == 0.0 && retval.yMaximum() == 0.0 )
882 {
883 retval.set( -1.0, -1.0, 1.0, 1.0 );
884 }
885 }
886
887 return retval;
888}
889
891{
892 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
894
895 return mLabelsEnabled && static_cast< bool >( mLabeling );
896}
897
899{
901
902 mLabelsEnabled = enabled;
903}
904
906{
907 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
909
910 if ( !mDiagramRenderer || !mDiagramLayerSettings )
911 return false;
912
913 QList<QgsDiagramSettings> settingList = mDiagramRenderer->diagramSettings();
914 if ( !settingList.isEmpty() )
915 {
916 return settingList.at( 0 ).enabled;
917 }
918 return false;
919}
920
921long long QgsVectorLayer::featureCount( const QString &legendKey ) const
922{
924
925 if ( !mSymbolFeatureCounted )
926 return -1;
927
928 return mSymbolFeatureCountMap.value( legendKey, -1 );
929}
930
931QgsFeatureIds QgsVectorLayer::symbolFeatureIds( const QString &legendKey ) const
932{
934
935 if ( !mSymbolFeatureCounted )
936 return QgsFeatureIds();
937
938 return mSymbolFeatureIdMap.value( legendKey, QgsFeatureIds() );
939}
941{
943
944 if ( ( mSymbolFeatureCounted || mFeatureCounter ) && !( storeSymbolFids && mSymbolFeatureIdMap.isEmpty() ) )
945 return mFeatureCounter;
946
947 mSymbolFeatureCountMap.clear();
948 mSymbolFeatureIdMap.clear();
949
950 if ( !isValid() )
951 {
952 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer" ), 3 );
953 return mFeatureCounter;
954 }
955 if ( !mDataProvider )
956 {
957 QgsDebugMsgLevel( QStringLiteral( "invoked with null mDataProvider" ), 3 );
958 return mFeatureCounter;
959 }
960 if ( !mRenderer )
961 {
962 QgsDebugMsgLevel( QStringLiteral( "invoked with null mRenderer" ), 3 );
963 return mFeatureCounter;
964 }
965
966 if ( !mFeatureCounter || ( storeSymbolFids && mSymbolFeatureIdMap.isEmpty() ) )
967 {
968 mFeatureCounter = new QgsVectorLayerFeatureCounter( this, QgsExpressionContext(), storeSymbolFids );
969 connect( mFeatureCounter, &QgsTask::taskCompleted, this, &QgsVectorLayer::onFeatureCounterCompleted, Qt::UniqueConnection );
970 connect( mFeatureCounter, &QgsTask::taskTerminated, this, &QgsVectorLayer::onFeatureCounterTerminated, Qt::UniqueConnection );
971 QgsApplication::taskManager()->addTask( mFeatureCounter );
972 }
973
974 return mFeatureCounter;
975}
976
978{
980
981 // do not update extent by default when trust project option is activated
982 if ( force || !mReadExtentFromXml || ( mReadExtentFromXml && mXmlExtent2D.isNull() && mXmlExtent3D.isNull() ) )
983 {
984 mValidExtent2D = false;
985 mValidExtent3D = false;
986 }
987}
988
990{
992
994 mValidExtent2D = true;
995}
996
998{
1000
1002 mValidExtent3D = true;
1003}
1004
1005void QgsVectorLayer::updateDefaultValues( QgsFeatureId fid, QgsFeature feature )
1006{
1008
1009 if ( !mDefaultValueOnUpdateFields.isEmpty() )
1010 {
1011 if ( !feature.isValid() )
1012 feature = getFeature( fid );
1013
1014 int size = mFields.size();
1015 for ( int idx : std::as_const( mDefaultValueOnUpdateFields ) )
1016 {
1017 if ( idx < 0 || idx >= size )
1018 continue;
1019 feature.setAttribute( idx, defaultValue( idx, feature ) );
1020 updateFeature( feature, true );
1021 }
1022 }
1023}
1024
1026{
1028
1029 QgsRectangle rect;
1030 rect.setNull();
1031
1032 if ( !isSpatial() )
1033 return rect;
1034
1035 if ( !mValidExtent2D && mLazyExtent2D && mReadExtentFromXml && !mXmlExtent2D.isNull() )
1036 {
1037 updateExtent( mXmlExtent2D );
1038 mValidExtent2D = true;
1039 mLazyExtent2D = false;
1040 }
1041
1042 if ( !mValidExtent2D && mLazyExtent2D && mDataProvider && mDataProvider->isValid() )
1043 {
1044 // store the extent
1045 updateExtent( mDataProvider->extent() );
1046 mValidExtent2D = true;
1047 mLazyExtent2D = false;
1048
1049 // show the extent
1050 QgsDebugMsgLevel( QStringLiteral( "2D Extent of layer: %1" ).arg( mExtent2D.toString() ), 3 );
1051 }
1052
1053 if ( mValidExtent2D )
1054 return QgsMapLayer::extent();
1055
1056 if ( !isValid() || !mDataProvider )
1057 {
1058 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1059 return rect;
1060 }
1061
1062 if ( !mEditBuffer ||
1063 ( !mDataProvider->transaction() && ( mEditBuffer->deletedFeatureIds().isEmpty() && mEditBuffer->changedGeometries().isEmpty() ) ) ||
1065 {
1066 mDataProvider->updateExtents();
1067
1068 // get the extent of the layer from the provider
1069 // but only when there are some features already
1070 if ( mDataProvider->featureCount() != 0 )
1071 {
1072 const QgsRectangle r = mDataProvider->extent();
1073 rect.combineExtentWith( r );
1074 }
1075
1076 if ( mEditBuffer && !mDataProvider->transaction() )
1077 {
1078 const auto addedFeatures = mEditBuffer->addedFeatures();
1079 for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it )
1080 {
1081 if ( it->hasGeometry() )
1082 {
1083 const QgsRectangle r = it->geometry().boundingBox();
1084 rect.combineExtentWith( r );
1085 }
1086 }
1087 }
1088 }
1089 else
1090 {
1092 .setNoAttributes() );
1093
1094 QgsFeature fet;
1095 while ( fit.nextFeature( fet ) )
1096 {
1097 if ( fet.hasGeometry() && fet.geometry().type() != Qgis::GeometryType::Unknown )
1098 {
1099 const QgsRectangle bb = fet.geometry().boundingBox();
1100 rect.combineExtentWith( bb );
1101 }
1102 }
1103 }
1104
1105 if ( rect.xMinimum() > rect.xMaximum() && rect.yMinimum() > rect.yMaximum() )
1106 {
1107 // special case when there are no features in provider nor any added
1108 rect = QgsRectangle(); // use rectangle with zero coordinates
1109 }
1110
1111 updateExtent( rect );
1112 mValidExtent2D = true;
1113
1114 // Send this (hopefully) up the chain to the map canvas
1115 emit recalculateExtents();
1116
1117 return rect;
1118}
1119
1121{
1123
1124 // if data is 2D, redirect to 2D extend computation, and save it as 2D extent (in 3D bbox)
1125 if ( mDataProvider && mDataProvider->elevationProperties() && !mDataProvider->elevationProperties()->containsElevationData() )
1126 {
1127 return QgsBox3D( extent() );
1128 }
1129
1131 extent.setNull();
1132
1133 if ( !isSpatial() )
1134 return extent;
1135
1136 if ( !mValidExtent3D && mLazyExtent3D && mReadExtentFromXml && !mXmlExtent3D.isNull() )
1137 {
1138 updateExtent( mXmlExtent3D );
1139 mValidExtent3D = true;
1140 mLazyExtent3D = false;
1141 }
1142
1143 if ( !mValidExtent3D && mLazyExtent3D && mDataProvider && mDataProvider->isValid() )
1144 {
1145 // store the extent
1146 updateExtent( mDataProvider->extent3D() );
1147 mValidExtent3D = true;
1148 mLazyExtent3D = false;
1149
1150 // show the extent
1151 QgsDebugMsgLevel( QStringLiteral( "3D Extent of layer: %1" ).arg( mExtent3D.toString() ), 3 );
1152 }
1153
1154 if ( mValidExtent3D )
1155 return QgsMapLayer::extent3D();
1156
1157 if ( !isValid() || !mDataProvider )
1158 {
1159 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1160 return extent;
1161 }
1162
1163 if ( !mEditBuffer ||
1164 ( !mDataProvider->transaction() && ( mEditBuffer->deletedFeatureIds().isEmpty() && mEditBuffer->changedGeometries().isEmpty() ) ) ||
1166 {
1167 mDataProvider->updateExtents();
1168
1169 // get the extent of the layer from the provider
1170 // but only when there are some features already
1171 if ( mDataProvider->featureCount() != 0 )
1172 {
1173 const QgsBox3D ext = mDataProvider->extent3D();
1174 extent.combineWith( ext );
1175 }
1176
1177 if ( mEditBuffer && !mDataProvider->transaction() )
1178 {
1179 const auto addedFeatures = mEditBuffer->addedFeatures();
1180 for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it )
1181 {
1182 if ( it->hasGeometry() )
1183 {
1184 const QgsBox3D bbox = it->geometry().boundingBox3D();
1185 extent.combineWith( bbox );
1186 }
1187 }
1188 }
1189 }
1190 else
1191 {
1193 .setNoAttributes() );
1194
1195 QgsFeature fet;
1196 while ( fit.nextFeature( fet ) )
1197 {
1198 if ( fet.hasGeometry() && fet.geometry().type() != Qgis::GeometryType::Unknown )
1199 {
1200 const QgsBox3D bb = fet.geometry().boundingBox3D();
1201 extent.combineWith( bb );
1202 }
1203 }
1204 }
1205
1206 if ( extent.xMinimum() > extent.xMaximum() && extent.yMinimum() > extent.yMaximum() && extent.zMinimum() > extent.zMaximum() )
1207 {
1208 // special case when there are no features in provider nor any added
1209 extent = QgsBox3D(); // use rectangle with zero coordinates
1210 }
1211
1212 updateExtent( extent );
1213 mValidExtent3D = true;
1214
1215 // Send this (hopefully) up the chain to the map canvas
1216 emit recalculateExtents();
1217
1218 return extent;
1219}
1220
1227
1234
1236{
1238
1239 if ( !isValid() || !mDataProvider )
1240 {
1241 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1242 return customProperty( QStringLiteral( "storedSubsetString" ) ).toString();
1243 }
1244 return mDataProvider->subsetString();
1245}
1246
1247bool QgsVectorLayer::setSubsetString( const QString &subset )
1248{
1250
1251 if ( !isValid() || !mDataProvider )
1252 {
1253 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider or while editing" ), 3 );
1254 setCustomProperty( QStringLiteral( "storedSubsetString" ), subset );
1255 return false;
1256 }
1257 else if ( mEditBuffer )
1258 {
1259 QgsDebugMsgLevel( QStringLiteral( "invoked while editing" ), 3 );
1260 return false;
1261 }
1262
1263 if ( subset == mDataProvider->subsetString() )
1264 return true;
1265
1266 bool res = mDataProvider->setSubsetString( subset );
1267
1268 // get the updated data source string from the provider
1269 mDataSource = mDataProvider->dataSourceUri();
1270 updateExtents();
1271 updateFields();
1272
1273 if ( res )
1274 {
1275 emit subsetStringChanged();
1277 }
1278
1279 return res;
1280}
1281
1283{
1284 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1286
1287 if ( isValid() && mDataProvider && !mEditBuffer && ( isSpatial() && geometryType() != Qgis::GeometryType::Point ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && renderContext.useRenderingOptimization() )
1288 {
1289 double maximumSimplificationScale = mSimplifyMethod.maximumScale();
1290
1291 // check maximum scale at which generalisation should be carried out
1292 return !( maximumSimplificationScale > 1 && renderContext.rendererScale() <= maximumSimplificationScale );
1293 }
1294 return false;
1295}
1296
1298{
1300
1301 return mConditionalStyles;
1302}
1303
1305{
1306 // non fatal for now -- the aggregate expression functions are not thread safe and call this
1308
1309 if ( !isValid() || !mDataProvider )
1310 return QgsFeatureIterator();
1311
1312 return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
1313}
1314
1316{
1318
1319 QgsFeature feature;
1321 if ( feature.isValid() )
1322 return feature.geometry();
1323 else
1324 return QgsGeometry();
1325}
1326
1328{
1330
1331 if ( !isValid() || !mEditBuffer || !mDataProvider )
1332 return false;
1333
1334
1335 if ( mGeometryOptions->isActive() )
1336 {
1337 QgsGeometry geom = feature.geometry();
1338 mGeometryOptions->apply( geom );
1339 feature.setGeometry( geom );
1340 }
1341
1342 bool success = mEditBuffer->addFeature( feature );
1343
1344 if ( success )
1345 {
1346 updateExtents();
1347
1348 if ( mJoinBuffer->containsJoins() )
1349 success = mJoinBuffer->addFeature( feature );
1350 }
1351
1352 return success;
1353}
1354
1355bool QgsVectorLayer::updateFeature( QgsFeature &updatedFeature, bool skipDefaultValues )
1356{
1358
1359 if ( !mEditBuffer || !mDataProvider )
1360 {
1361 return false;
1362 }
1363
1364 QgsFeature currentFeature = getFeature( updatedFeature.id() );
1365 if ( currentFeature.isValid() )
1366 {
1367 bool hasChanged = false;
1368 bool hasError = false;
1369
1370 if ( ( updatedFeature.hasGeometry() || currentFeature.hasGeometry() ) && !updatedFeature.geometry().equals( currentFeature.geometry() ) )
1371 {
1372 QgsGeometry geometry = updatedFeature.geometry();
1373 if ( changeGeometry( updatedFeature.id(), geometry, true ) )
1374 {
1375 hasChanged = true;
1376 updatedFeature.setGeometry( geometry );
1377 }
1378 else
1379 {
1380 QgsDebugMsgLevel( QStringLiteral( "geometry of feature %1 could not be changed." ).arg( updatedFeature.id() ), 3 );
1381 }
1382 }
1383
1384 QgsAttributes fa = updatedFeature.attributes();
1385 QgsAttributes ca = currentFeature.attributes();
1386
1387 for ( int attr = 0; attr < fa.count(); ++attr )
1388 {
1389 if ( !qgsVariantEqual( fa.at( attr ), ca.at( attr ) ) )
1390 {
1391 if ( changeAttributeValue( updatedFeature.id(), attr, fa.at( attr ), ca.at( attr ), true ) )
1392 {
1393 hasChanged = true;
1394 }
1395 else
1396 {
1397 QgsDebugMsgLevel( QStringLiteral( "attribute %1 of feature %2 could not be changed." ).arg( attr ).arg( updatedFeature.id() ), 3 );
1398 hasError = true;
1399 }
1400 }
1401 }
1402 if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )
1403 updateDefaultValues( updatedFeature.id(), updatedFeature );
1404
1405 return !hasError;
1406 }
1407 else
1408 {
1409 QgsDebugMsgLevel( QStringLiteral( "feature %1 could not be retrieved" ).arg( updatedFeature.id() ), 3 );
1410 return false;
1411 }
1412}
1413
1414
1415bool QgsVectorLayer::insertVertex( double x, double y, QgsFeatureId atFeatureId, int beforeVertex )
1416{
1418
1419 if ( !isValid() || !mEditBuffer || !mDataProvider )
1420 return false;
1421
1422 QgsVectorLayerEditUtils utils( this );
1423 bool result = utils.insertVertex( x, y, atFeatureId, beforeVertex );
1424 if ( result )
1425 updateExtents();
1426 return result;
1427}
1428
1429
1430bool QgsVectorLayer::insertVertex( const QgsPoint &point, QgsFeatureId atFeatureId, int beforeVertex )
1431{
1433
1434 if ( !isValid() || !mEditBuffer || !mDataProvider )
1435 return false;
1436
1437 QgsVectorLayerEditUtils utils( this );
1438 bool result = utils.insertVertex( point, atFeatureId, beforeVertex );
1439 if ( result )
1440 updateExtents();
1441 return result;
1442}
1443
1444
1445bool QgsVectorLayer::moveVertex( double x, double y, QgsFeatureId atFeatureId, int atVertex )
1446{
1448
1449 if ( !isValid() || !mEditBuffer || !mDataProvider )
1450 return false;
1451
1452 QgsVectorLayerEditUtils utils( this );
1453 bool result = utils.moveVertex( x, y, atFeatureId, atVertex );
1454
1455 if ( result )
1456 updateExtents();
1457 return result;
1458}
1459
1460bool QgsVectorLayer::moveVertex( const QgsPoint &p, QgsFeatureId atFeatureId, int atVertex )
1461{
1463
1464 if ( !isValid() || !mEditBuffer || !mDataProvider )
1465 return false;
1466
1467 QgsVectorLayerEditUtils utils( this );
1468 bool result = utils.moveVertex( p, atFeatureId, atVertex );
1469
1470 if ( result )
1471 updateExtents();
1472 return result;
1473}
1474
1476{
1478
1479 if ( !isValid() || !mEditBuffer || !mDataProvider )
1481
1482 QgsVectorLayerEditUtils utils( this );
1483 Qgis::VectorEditResult result = utils.deleteVertex( featureId, vertex );
1484
1485 if ( result == Qgis::VectorEditResult::Success )
1486 updateExtents();
1487 return result;
1488}
1489
1490
1492{
1494
1495 if ( !isValid() || !mDataProvider || !( mDataProvider->capabilities() & QgsVectorDataProvider::DeleteFeatures ) )
1496 {
1497 return false;
1498 }
1499
1500 if ( !isEditable() )
1501 {
1502 return false;
1503 }
1504
1505 int deleted = 0;
1506 int count = mSelectedFeatureIds.size();
1507 // Make a copy since deleteFeature modifies mSelectedFeatureIds
1508 QgsFeatureIds selectedFeatures( mSelectedFeatureIds );
1509 for ( QgsFeatureId fid : std::as_const( selectedFeatures ) )
1510 {
1511 deleted += deleteFeature( fid, context ); // removes from selection
1512 }
1513
1515 updateExtents();
1516
1517 if ( deletedCount )
1518 {
1519 *deletedCount = deleted;
1520 }
1521
1522 return deleted == count;
1523}
1524
1525static const QgsPointSequence vectorPointXY2pointSequence( const QVector<QgsPointXY> &points )
1526{
1527 QgsPointSequence pts;
1528 pts.reserve( points.size() );
1529 QVector<QgsPointXY>::const_iterator it = points.constBegin();
1530 while ( it != points.constEnd() )
1531 {
1532 pts.append( QgsPoint( *it ) );
1533 ++it;
1534 }
1535 return pts;
1536}
1537Qgis::GeometryOperationResult QgsVectorLayer::addRing( const QVector<QgsPointXY> &ring, QgsFeatureId *featureId )
1538{
1540
1541 return addRing( vectorPointXY2pointSequence( ring ), featureId );
1542}
1543
1545{
1547
1548 if ( !isValid() || !mEditBuffer || !mDataProvider )
1550
1551 QgsVectorLayerEditUtils utils( this );
1553
1554 //first try with selected features
1555 if ( !mSelectedFeatureIds.isEmpty() )
1556 {
1557 result = utils.addRing( ring, mSelectedFeatureIds, featureId );
1558 }
1559
1561 {
1562 //try with all intersecting features
1563 result = utils.addRing( ring, QgsFeatureIds(), featureId );
1564 }
1565
1566 return result;
1567}
1568
1570{
1572
1573 if ( !isValid() || !mEditBuffer || !mDataProvider )
1574 {
1575 delete ring;
1577 }
1578
1579 if ( !ring )
1580 {
1582 }
1583
1584 if ( !ring->isClosed() )
1585 {
1586 delete ring;
1588 }
1589
1590 QgsVectorLayerEditUtils utils( this );
1592
1593 //first try with selected features
1594 if ( !mSelectedFeatureIds.isEmpty() )
1595 {
1596 result = utils.addRing( static_cast< QgsCurve * >( ring->clone() ), mSelectedFeatureIds, featureId );
1597 }
1598
1600 {
1601 //try with all intersecting features
1602 result = utils.addRing( static_cast< QgsCurve * >( ring->clone() ), QgsFeatureIds(), featureId );
1603 }
1604
1605 delete ring;
1606 return result;
1607}
1608
1610{
1612
1613 QgsPointSequence pts;
1614 pts.reserve( points.size() );
1615 for ( QList<QgsPointXY>::const_iterator it = points.constBegin(); it != points.constEnd() ; ++it )
1616 {
1617 pts.append( QgsPoint( *it ) );
1618 }
1619 return addPart( pts );
1620}
1621
1622#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1623Qgis::GeometryOperationResult QgsVectorLayer::addPart( const QVector<QgsPointXY> &points )
1624{
1626
1627 return addPart( vectorPointXY2pointSequence( points ) );
1628}
1629#endif
1630
1632{
1634
1635 if ( !isValid() || !mEditBuffer || !mDataProvider )
1637
1638 //number of selected features must be 1
1639
1640 if ( mSelectedFeatureIds.empty() )
1641 {
1642 QgsDebugMsgLevel( QStringLiteral( "Number of selected features <1" ), 3 );
1644 }
1645 else if ( mSelectedFeatureIds.size() > 1 )
1646 {
1647 QgsDebugMsgLevel( QStringLiteral( "Number of selected features >1" ), 3 );
1649 }
1650
1651 QgsVectorLayerEditUtils utils( this );
1652 Qgis::GeometryOperationResult result = utils.addPart( points, *mSelectedFeatureIds.constBegin() );
1653
1655 updateExtents();
1656 return result;
1657}
1658
1660{
1662
1663 if ( !isValid() || !mEditBuffer || !mDataProvider )
1665
1666 //number of selected features must be 1
1667
1668 if ( mSelectedFeatureIds.empty() )
1669 {
1670 QgsDebugMsgLevel( QStringLiteral( "Number of selected features <1" ), 3 );
1672 }
1673 else if ( mSelectedFeatureIds.size() > 1 )
1674 {
1675 QgsDebugMsgLevel( QStringLiteral( "Number of selected features >1" ), 3 );
1677 }
1678
1679 QgsVectorLayerEditUtils utils( this );
1680 Qgis::GeometryOperationResult result = utils.addPart( ring, *mSelectedFeatureIds.constBegin() );
1681
1683 updateExtents();
1684 return result;
1685}
1686
1687// TODO QGIS 4.0 -- this should return Qgis::GeometryOperationResult, not int
1688int QgsVectorLayer::translateFeature( QgsFeatureId featureId, double dx, double dy )
1689{
1691
1692 if ( !isValid() || !mEditBuffer || !mDataProvider )
1693 return static_cast< int >( Qgis::GeometryOperationResult::LayerNotEditable );
1694
1695 QgsVectorLayerEditUtils utils( this );
1696 int result = utils.translateFeature( featureId, dx, dy );
1697
1698 if ( result == static_cast< int >( Qgis::GeometryOperationResult::Success ) )
1699 updateExtents();
1700 return result;
1701}
1702
1703Qgis::GeometryOperationResult QgsVectorLayer::splitParts( const QVector<QgsPointXY> &splitLine, bool topologicalEditing )
1704{
1706
1707 return splitParts( vectorPointXY2pointSequence( splitLine ), topologicalEditing );
1708}
1709
1711{
1713
1714 if ( !isValid() || !mEditBuffer || !mDataProvider )
1716
1717 QgsVectorLayerEditUtils utils( this );
1718 return utils.splitParts( splitLine, topologicalEditing );
1719}
1720
1721Qgis::GeometryOperationResult QgsVectorLayer::splitFeatures( const QVector<QgsPointXY> &splitLine, bool topologicalEditing )
1722{
1724
1725 return splitFeatures( vectorPointXY2pointSequence( splitLine ), topologicalEditing );
1726}
1727
1729{
1731
1732 QgsLineString splitLineString( splitLine );
1733 QgsPointSequence topologyTestPoints;
1734 bool preserveCircular = false;
1735 return splitFeatures( &splitLineString, topologyTestPoints, preserveCircular, topologicalEditing );
1736}
1737
1738Qgis::GeometryOperationResult QgsVectorLayer::splitFeatures( const QgsCurve *curve, QgsPointSequence &topologyTestPoints, bool preserveCircular, bool topologicalEditing )
1739{
1741
1742 if ( !isValid() || !mEditBuffer || !mDataProvider )
1744
1745 QgsVectorLayerEditUtils utils( this );
1746 return utils.splitFeatures( curve, topologyTestPoints, preserveCircular, topologicalEditing );
1747}
1748
1750{
1752
1753 if ( !isValid() || !mEditBuffer || !mDataProvider )
1754 return -1;
1755
1756 QgsVectorLayerEditUtils utils( this );
1757 return utils.addTopologicalPoints( geom );
1758}
1759
1766
1768{
1770
1771 if ( !isValid() || !mEditBuffer || !mDataProvider )
1772 return -1;
1773
1774 QgsVectorLayerEditUtils utils( this );
1775 return utils.addTopologicalPoints( p );
1776}
1777
1779{
1781
1782 if ( !mValid || !mEditBuffer || !mDataProvider )
1783 return -1;
1784
1785 QgsVectorLayerEditUtils utils( this );
1786 return utils.addTopologicalPoints( ps );
1787}
1788
1790{
1792
1793 if ( mLabeling == labeling )
1794 return;
1795
1796 delete mLabeling;
1797 mLabeling = labeling;
1798}
1799
1801{
1803
1804 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
1805 return project()->startEditing( this );
1806
1807 if ( !isValid() || !mDataProvider )
1808 {
1809 return false;
1810 }
1811
1812 // allow editing if provider supports any of the capabilities
1813 if ( !supportsEditing() )
1814 {
1815 return false;
1816 }
1817
1818 if ( mEditBuffer )
1819 {
1820 // editing already underway
1821 return false;
1822 }
1823
1824 mDataProvider->enterUpdateMode();
1825
1826 emit beforeEditingStarted();
1827
1828 createEditBuffer();
1829
1830 updateFields();
1831
1832 emit editingStarted();
1833
1834 return true;
1835}
1836
1838{
1840
1841 if ( mDataProvider )
1842 mDataProvider->setTransformContext( transformContext );
1843}
1844
1851
1853{
1855
1856 if ( mRenderer )
1857 if ( !mRenderer->accept( visitor ) )
1858 return false;
1859
1860 if ( mLabeling )
1861 if ( !mLabeling->accept( visitor ) )
1862 return false;
1863
1864 return true;
1865}
1866
1867bool QgsVectorLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
1868{
1870
1871 QgsDebugMsgLevel( QStringLiteral( "Datasource in QgsVectorLayer::readXml: %1" ).arg( mDataSource.toLocal8Bit().data() ), 3 );
1872
1873 //process provider key
1874 QDomNode pkeyNode = layer_node.namedItem( QStringLiteral( "provider" ) );
1875
1876 if ( pkeyNode.isNull() )
1877 {
1878 mProviderKey.clear();
1879 }
1880 else
1881 {
1882 QDomElement pkeyElt = pkeyNode.toElement();
1883 mProviderKey = pkeyElt.text();
1884 }
1885
1886 // determine type of vector layer
1887 if ( !mProviderKey.isNull() )
1888 {
1889 // if the provider string isn't empty, then we successfully
1890 // got the stored provider
1891 }
1892 else if ( mDataSource.contains( QLatin1String( "dbname=" ) ) )
1893 {
1894 mProviderKey = QStringLiteral( "postgres" );
1895 }
1896 else
1897 {
1898 mProviderKey = QStringLiteral( "ogr" );
1899 }
1900
1901 const QDomElement elem = layer_node.toElement();
1903
1904 mDataSourceReadOnly = mReadFlags & QgsMapLayer::FlagForceReadOnly;
1905 QgsDataProvider::ReadFlags flags = providerReadFlags( layer_node, mReadFlags );
1906
1907 if ( ( mReadFlags & QgsMapLayer::FlagDontResolveLayers ) || !setDataProvider( mProviderKey, options, flags ) )
1908 {
1910 {
1911 QgsDebugError( QStringLiteral( "Could not set data provider for layer %1" ).arg( publicSource() ) );
1912 }
1913
1914 // for invalid layer sources, we fallback to stored wkbType if available
1915 if ( elem.hasAttribute( QStringLiteral( "wkbType" ) ) )
1916 mWkbType = qgsEnumKeyToValue( elem.attribute( QStringLiteral( "wkbType" ) ), mWkbType );
1917 }
1918
1919 QDomElement pkeyElem = pkeyNode.toElement();
1920 if ( !pkeyElem.isNull() )
1921 {
1922 QString encodingString = pkeyElem.attribute( QStringLiteral( "encoding" ) );
1923 if ( mDataProvider && !encodingString.isEmpty() )
1924 {
1925 mDataProvider->setEncoding( encodingString );
1926 }
1927 }
1928
1929 // load vector joins - does not resolve references to layers yet
1930 mJoinBuffer->readXml( layer_node );
1931
1932 updateFields();
1933
1934 // If style doesn't include a legend, we'll need to make a default one later...
1935 mSetLegendFromStyle = false;
1936
1937 QString errorMsg;
1938 if ( !readSymbology( layer_node, errorMsg, context ) )
1939 {
1940 return false;
1941 }
1942
1943 readStyleManager( layer_node );
1944
1945 QDomNode depsNode = layer_node.namedItem( QStringLiteral( "dataDependencies" ) );
1946 QDomNodeList depsNodes = depsNode.childNodes();
1947 QSet<QgsMapLayerDependency> sources;
1948 for ( int i = 0; i < depsNodes.count(); i++ )
1949 {
1950 QString source = depsNodes.at( i ).toElement().attribute( QStringLiteral( "id" ) );
1951 sources << QgsMapLayerDependency( source );
1952 }
1953 setDependencies( sources );
1954
1955 if ( !mSetLegendFromStyle )
1957
1958 // read extent
1960 {
1961 mReadExtentFromXml = true;
1962 }
1963 if ( mReadExtentFromXml )
1964 {
1965 const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
1966 if ( !extentNode.isNull() )
1967 {
1968 mXmlExtent2D = QgsXmlUtils::readRectangle( extentNode.toElement() );
1969 }
1970 const QDomNode extent3DNode = layer_node.namedItem( QStringLiteral( "extent3D" ) );
1971 if ( !extent3DNode.isNull() )
1972 {
1973 mXmlExtent3D = QgsXmlUtils::readBox3D( extent3DNode.toElement() );
1974 }
1975 }
1976
1977 // auxiliary layer
1978 const QDomNode asNode = layer_node.namedItem( QStringLiteral( "auxiliaryLayer" ) );
1979 const QDomElement asElem = asNode.toElement();
1980 if ( !asElem.isNull() )
1981 {
1982 mAuxiliaryLayerKey = asElem.attribute( QStringLiteral( "key" ) );
1983 }
1984
1985 // QGIS Server WMS Dimensions
1986 mServerProperties->readXml( layer_node );
1987
1988 return isValid(); // should be true if read successfully
1989
1990} // void QgsVectorLayer::readXml
1991
1992
1993void QgsVectorLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
1994 const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
1995{
1997
1998 Qgis::GeometryType geomType = geometryType();
1999
2000 mDataSource = dataSource;
2001 setName( baseName );
2002 setDataProvider( provider, options, flags );
2003
2004 if ( !isValid() )
2005 {
2006 return;
2007 }
2008
2009 // Always set crs
2011
2012 bool loadDefaultStyleFlag = false;
2014 {
2015 loadDefaultStyleFlag = true;
2016 }
2017
2018 // reset style if loading default style, style is missing, or geometry type is has changed (and layer is valid)
2019 if ( !renderer() || !legend() || ( isValid() && geomType != geometryType() ) || loadDefaultStyleFlag )
2020 {
2021 std::unique_ptr< QgsScopedRuntimeProfile > profile;
2022 if ( QgsApplication::profiler()->groupIsActive( QStringLiteral( "projectload" ) ) )
2023 profile = std::make_unique< QgsScopedRuntimeProfile >( tr( "Load layer style" ), QStringLiteral( "projectload" ) );
2024
2025 bool defaultLoadedFlag = false;
2026
2027 // defer style changed signal until we've set the renderer, labeling, everything.
2028 // we don't want multiple signals!
2029 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
2030
2031 // need to check whether the default style included a legend, and if not, we need to make a default legend
2032 // later...
2033 mSetLegendFromStyle = false;
2034
2035 // first check if there is a default style / propertysheet defined
2036 // for this layer and if so apply it
2037 // this should take precedence over all
2038 if ( !defaultLoadedFlag && loadDefaultStyleFlag )
2039 {
2040 loadDefaultStyle( defaultLoadedFlag );
2041 }
2042
2043 if ( loadDefaultStyleFlag && !defaultLoadedFlag && isSpatial() && mDataProvider->capabilities() & QgsVectorDataProvider::CreateRenderer )
2044 {
2045 // if we didn't load a default style for this layer, try to create a renderer directly from the data provider
2046 std::unique_ptr< QgsFeatureRenderer > defaultRenderer( mDataProvider->createRenderer() );
2047 if ( defaultRenderer )
2048 {
2049 defaultLoadedFlag = true;
2050 setRenderer( defaultRenderer.release() );
2051 }
2052 }
2053
2054 // if the default style failed to load or was disabled use some very basic defaults
2055 if ( !defaultLoadedFlag )
2056 {
2057 // add single symbol renderer for spatial layers
2059 }
2060
2061 if ( !mSetLegendFromStyle )
2063
2064 if ( mDataProvider->capabilities() & QgsVectorDataProvider::CreateLabeling )
2065 {
2066 std::unique_ptr< QgsAbstractVectorLayerLabeling > defaultLabeling( mDataProvider->createLabeling() );
2067 if ( defaultLabeling )
2068 {
2069 setLabeling( defaultLabeling.release() );
2070 setLabelsEnabled( true );
2071 }
2072 }
2073
2074 styleChangedSignalBlocker.release();
2076 }
2077}
2078
2079QString QgsVectorLayer::loadDefaultStyle( bool &resultFlag )
2080{
2082
2083 // first try to load a user-defined default style - this should always take precedence
2084 QString styleXml = QgsMapLayer::loadDefaultStyle( resultFlag );
2085
2086 if ( resultFlag )
2087 {
2088 // Try to load all stored styles from DB
2089 if ( mLoadAllStoredStyle && mDataProvider && mDataProvider->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2090 {
2091 QStringList ids, names, descriptions;
2092 QString errorMessage;
2093 // Get the number of styles related to current layer.
2094 const int relatedStylesCount { listStylesInDatabase( ids, names, descriptions, errorMessage ) };
2095 Q_ASSERT( ids.count() == names.count() );
2096 const QString currentStyleName { mStyleManager->currentStyle() };
2097 for ( int i = 0; i < relatedStylesCount; ++i )
2098 {
2099 if ( names.at( i ) == currentStyleName )
2100 {
2101 continue;
2102 }
2103 errorMessage.clear();
2104 const QString styleXml { getStyleFromDatabase( ids.at( i ), errorMessage ) };
2105 if ( ! styleXml.isEmpty() && errorMessage.isEmpty() )
2106 {
2107 mStyleManager->addStyle( names.at( i ), QgsMapLayerStyle( styleXml ) );
2108 }
2109 else
2110 {
2111 QgsDebugMsgLevel( QStringLiteral( "Error retrieving style %1 from DB: %2" ).arg( ids.at( i ), errorMessage ), 2 );
2112 }
2113 }
2114 }
2115 return styleXml ;
2116 }
2117
2118 if ( isSpatial() && mDataProvider->capabilities() & QgsVectorDataProvider::CreateRenderer )
2119 {
2120 // otherwise try to create a renderer directly from the data provider
2121 std::unique_ptr< QgsFeatureRenderer > defaultRenderer( mDataProvider->createRenderer() );
2122 if ( defaultRenderer )
2123 {
2124 resultFlag = true;
2125 setRenderer( defaultRenderer.release() );
2126 return QString();
2127 }
2128 }
2129
2130 return QString();
2131}
2132
2133bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
2134{
2136
2137 mProviderKey = provider;
2138 delete mDataProvider;
2139
2140 // For Postgres provider primary key unicity is tested at construction time,
2141 // so it has to be set before initializing the provider,
2142 // this manipulation is necessary to preserve default behavior when
2143 // "trust layer metadata" project level option is set and checkPrimaryKeyUnicity
2144 // was not explicitly passed in the uri
2145 if ( provider.compare( QLatin1String( "postgres" ) ) == 0 )
2146 {
2147 const QString checkUnicityKey { QStringLiteral( "checkPrimaryKeyUnicity" ) };
2149 if ( ! uri.hasParam( checkUnicityKey ) )
2150 {
2151 uri.setParam( checkUnicityKey, mReadExtentFromXml ? "0" : "1" );
2152 mDataSource = uri.uri( false );
2153 }
2154 }
2155
2156 std::unique_ptr< QgsScopedRuntimeProfile > profile;
2157 if ( QgsApplication::profiler()->groupIsActive( QStringLiteral( "projectload" ) ) )
2158 profile = std::make_unique< QgsScopedRuntimeProfile >( tr( "Create %1 provider" ).arg( provider ), QStringLiteral( "projectload" ) );
2159
2160 if ( mPreloadedProvider )
2161 mDataProvider = qobject_cast< QgsVectorDataProvider * >( mPreloadedProvider.release() );
2162 else
2163 mDataProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, mDataSource, options, flags ) );
2164
2165 if ( !mDataProvider )
2166 {
2167 setValid( false );
2168 QgsDebugMsgLevel( QStringLiteral( "Unable to get data provider" ), 2 );
2169 return false;
2170 }
2171
2172 mDataProvider->setParent( this );
2173 connect( mDataProvider, &QgsVectorDataProvider::raiseError, this, &QgsVectorLayer::raiseError );
2174
2175 QgsDebugMsgLevel( QStringLiteral( "Instantiated the data provider plugin" ), 2 );
2176
2177 setValid( mDataProvider->isValid() );
2178 if ( !isValid() )
2179 {
2180 QgsDebugMsgLevel( QStringLiteral( "Invalid provider plugin %1" ).arg( QString( mDataSource.toUtf8() ) ), 2 );
2181 return false;
2182 }
2183
2184 if ( profile )
2185 profile->switchTask( tr( "Read layer metadata" ) );
2187 {
2188 // we combine the provider metadata with the layer's existing metadata, so as not to reset any user customizations to the metadata
2189 // back to the default if a layer's data source is changed
2190 QgsLayerMetadata newMetadata = mDataProvider->layerMetadata();
2191 // this overwrites the provider metadata with any properties which are non-empty from the existing layer metadata
2192 newMetadata.combine( &mMetadata );
2193
2194 setMetadata( newMetadata );
2195 QgsDebugMsgLevel( QStringLiteral( "Set Data provider QgsLayerMetadata identifier[%1]" ).arg( metadata().identifier() ), 4 );
2196 }
2197
2198 // TODO: Check if the provider has the capability to send fullExtentCalculated
2199 connect( mDataProvider, &QgsVectorDataProvider::fullExtentCalculated, this, [this] { updateExtents(); } );
2200
2201 // get and store the feature type
2202 mWkbType = mDataProvider->wkbType();
2203
2204 // before we update the layer fields from the provider, we first copy any default set alias and
2205 // editor widget config from the data provider fields, if present
2206 const QgsFields providerFields = mDataProvider->fields();
2207 for ( const QgsField &field : providerFields )
2208 {
2209 // we only copy defaults from the provider if we aren't overriding any configuration made in the layer
2210 if ( !field.editorWidgetSetup().isNull() && mFieldWidgetSetups.value( field.name() ).isNull() )
2211 {
2212 mFieldWidgetSetups[ field.name() ] = field.editorWidgetSetup();
2213 }
2214 if ( !field.alias().isEmpty() && mAttributeAliasMap.value( field.name() ).isEmpty() )
2215 {
2216 mAttributeAliasMap[ field.name() ] = field.alias();
2217 }
2218 if ( !mAttributeSplitPolicy.contains( field.name() ) )
2219 {
2220 mAttributeSplitPolicy[ field.name() ] = field.splitPolicy();
2221 }
2222 }
2223
2224 if ( profile )
2225 profile->switchTask( tr( "Read layer fields" ) );
2226 updateFields();
2227
2228 if ( mProviderKey == QLatin1String( "postgres" ) )
2229 {
2230 // update datasource from data provider computed one
2231 mDataSource = mDataProvider->dataSourceUri( false );
2232
2233 QgsDebugMsgLevel( QStringLiteral( "Beautifying layer name %1" ).arg( name() ), 3 );
2234
2235 // adjust the display name for postgres layers
2236 const thread_local QRegularExpression reg( R"lit("[^"]+"\."([^"] + )"( \‍([^)]+\))?)lit" );
2237 const QRegularExpressionMatch match = reg.match( name() );
2238 if ( match.hasMatch() )
2239 {
2240 QStringList stuff = match.capturedTexts();
2241 QString lName = stuff[1];
2242
2243 const QMap<QString, QgsMapLayer *> &layers = QgsProject::instance()->mapLayers();
2244
2245 QMap<QString, QgsMapLayer *>::const_iterator it;
2246 for ( it = layers.constBegin(); it != layers.constEnd() && ( *it )->name() != lName; ++it )
2247 ;
2248
2249 if ( it != layers.constEnd() && stuff.size() > 2 )
2250 {
2251 lName += '.' + stuff[2].mid( 2, stuff[2].length() - 3 );
2252 }
2253
2254 if ( !lName.isEmpty() )
2255 setName( lName );
2256 }
2257 QgsDebugMsgLevel( QStringLiteral( "Beautified layer name %1" ).arg( name() ), 3 );
2258 }
2259 else if ( mProviderKey == QLatin1String( "osm" ) )
2260 {
2261 // make sure that the "observer" has been removed from URI to avoid crashes
2262 mDataSource = mDataProvider->dataSourceUri();
2263 }
2264 else if ( provider == QLatin1String( "ogr" ) )
2265 {
2266 // make sure that the /vsigzip or /vsizip is added to uri, if applicable
2267 mDataSource = mDataProvider->dataSourceUri();
2268 if ( mDataSource.right( 10 ) == QLatin1String( "|layerid=0" ) )
2269 mDataSource.chop( 10 );
2270 }
2271 else if ( provider == QLatin1String( "memory" ) )
2272 {
2273 // required so that source differs between memory layers
2274 mDataSource = mDataSource + QStringLiteral( "&uid=%1" ).arg( QUuid::createUuid().toString() );
2275 }
2276 else if ( provider == QLatin1String( "hana" ) )
2277 {
2278 // update datasource from data provider computed one
2279 mDataSource = mDataProvider->dataSourceUri( false );
2280 }
2281
2282 connect( mDataProvider, &QgsVectorDataProvider::dataChanged, this, &QgsVectorLayer::emitDataChanged );
2284
2285 return true;
2286} // QgsVectorLayer:: setDataProvider
2287
2288
2289
2290
2291/* virtual */
2292bool QgsVectorLayer::writeXml( QDomNode &layer_node,
2293 QDomDocument &document,
2294 const QgsReadWriteContext &context ) const
2295{
2297
2298 // first get the layer element so that we can append the type attribute
2299
2300 QDomElement mapLayerNode = layer_node.toElement();
2301
2302 if ( mapLayerNode.isNull() || ( "maplayer" != mapLayerNode.nodeName() ) )
2303 {
2304 QgsDebugMsgLevel( QStringLiteral( "can't find <maplayer>" ), 2 );
2305 return false;
2306 }
2307
2308 mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( Qgis::LayerType::Vector ) );
2309
2310 // set the geometry type
2311 mapLayerNode.setAttribute( QStringLiteral( "geometry" ), QgsWkbTypes::geometryDisplayString( geometryType() ) );
2312 mapLayerNode.setAttribute( QStringLiteral( "wkbType" ), qgsEnumValueToKey( wkbType() ) );
2313
2314 // add provider node
2315 if ( mDataProvider )
2316 {
2317 QDomElement provider = document.createElement( QStringLiteral( "provider" ) );
2318 provider.setAttribute( QStringLiteral( "encoding" ), mDataProvider->encoding() );
2319 QDomText providerText = document.createTextNode( providerType() );
2320 provider.appendChild( providerText );
2321 layer_node.appendChild( provider );
2322 }
2323
2324 //save joins
2325 mJoinBuffer->writeXml( layer_node, document );
2326
2327 // dependencies
2328 QDomElement dependenciesElement = document.createElement( QStringLiteral( "layerDependencies" ) );
2329 const auto constDependencies = dependencies();
2330 for ( const QgsMapLayerDependency &dep : constDependencies )
2331 {
2333 continue;
2334 QDomElement depElem = document.createElement( QStringLiteral( "layer" ) );
2335 depElem.setAttribute( QStringLiteral( "id" ), dep.layerId() );
2336 dependenciesElement.appendChild( depElem );
2337 }
2338 layer_node.appendChild( dependenciesElement );
2339
2340 // change dependencies
2341 QDomElement dataDependenciesElement = document.createElement( QStringLiteral( "dataDependencies" ) );
2342 for ( const QgsMapLayerDependency &dep : constDependencies )
2343 {
2344 if ( dep.type() != QgsMapLayerDependency::DataDependency )
2345 continue;
2346 QDomElement depElem = document.createElement( QStringLiteral( "layer" ) );
2347 depElem.setAttribute( QStringLiteral( "id" ), dep.layerId() );
2348 dataDependenciesElement.appendChild( depElem );
2349 }
2350 layer_node.appendChild( dataDependenciesElement );
2351
2352 // save expression fields
2353 mExpressionFieldBuffer->writeXml( layer_node, document );
2354
2355 writeStyleManager( layer_node, document );
2356
2357 // auxiliary layer
2358 QDomElement asElem = document.createElement( QStringLiteral( "auxiliaryLayer" ) );
2359 if ( mAuxiliaryLayer )
2360 {
2361 const QString pkField = mAuxiliaryLayer->joinInfo().targetFieldName();
2362 asElem.setAttribute( QStringLiteral( "key" ), pkField );
2363 }
2364 layer_node.appendChild( asElem );
2365
2366 // save QGIS Server properties (WMS Dimension, metadata URLS...)
2367 mServerProperties->writeXml( layer_node, document );
2368
2369 // renderer specific settings
2370 QString errorMsg;
2371 return writeSymbology( layer_node, document, errorMsg, context );
2372}
2373
2374QString QgsVectorLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
2375{
2377
2378 if ( providerType() == QLatin1String( "memory" ) )
2379 {
2380 // Refetch the source from the provider, because adding fields actually changes the source for this provider.
2381 return dataProvider()->dataSourceUri();
2382 }
2383
2385}
2386
2387QString QgsVectorLayer::decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const
2388{
2390
2391 return QgsProviderRegistry::instance()->relativeToAbsoluteUri( provider, source, context );
2392}
2393
2394
2395
2403
2404
2405bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMessage,
2406 QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2407{
2409
2410 if ( categories.testFlag( Fields ) )
2411 {
2412 if ( !mExpressionFieldBuffer )
2413 mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
2414 mExpressionFieldBuffer->readXml( layerNode );
2415
2416 updateFields();
2417 }
2418
2419 if ( categories.testFlag( Relations ) )
2420 {
2421 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Relations" ) );
2422
2423 const QgsPathResolver resolver { QgsProject::instance()->pathResolver() };
2424
2425 // Restore referenced layers: relations where "this" is the child layer (the referencing part, that holds the FK)
2426 QDomNodeList referencedLayersNodeList = layerNode.toElement().elementsByTagName( QStringLiteral( "referencedLayers" ) );
2427 if ( referencedLayersNodeList.size() > 0 )
2428 {
2429 const QDomNodeList relationNodes { referencedLayersNodeList.at( 0 ).childNodes() };
2430 for ( int i = 0; i < relationNodes.length(); ++i )
2431 {
2432 const QDomElement relationElement = relationNodes.at( i ).toElement();
2433
2434 mWeakRelations.push_back( QgsWeakRelation::readXml( this, QgsWeakRelation::Referencing, relationElement, resolver ) );
2435 }
2436 }
2437
2438 // Restore referencing layers: relations where "this" is the parent layer (the referenced part where the FK points to)
2439 QDomNodeList referencingLayersNodeList = layerNode.toElement().elementsByTagName( QStringLiteral( "referencingLayers" ) );
2440 if ( referencingLayersNodeList.size() > 0 )
2441 {
2442 const QDomNodeList relationNodes { referencingLayersNodeList.at( 0 ).childNodes() };
2443 for ( int i = 0; i < relationNodes.length(); ++i )
2444 {
2445 const QDomElement relationElement = relationNodes.at( i ).toElement();
2446 mWeakRelations.push_back( QgsWeakRelation::readXml( this, QgsWeakRelation::Referenced, relationElement, resolver ) );
2447 }
2448 }
2449 }
2450
2451 QDomElement layerElement = layerNode.toElement();
2452
2453 readCommonStyle( layerElement, context, categories );
2454
2455 readStyle( layerNode, errorMessage, context, categories );
2456
2457 if ( categories.testFlag( MapTips ) )
2458 {
2459 QDomElement mapTipElem = layerNode.namedItem( QStringLiteral( "mapTip" ) ).toElement();
2460 setMapTipTemplate( mapTipElem.text() );
2461 setMapTipsEnabled( mapTipElem.attribute( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt() == 1 );
2462 }
2463
2464 if ( categories.testFlag( LayerConfiguration ) )
2465 mDisplayExpression = layerNode.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text();
2466
2467 // Try to migrate pre QGIS 3.0 display field property
2468 QString displayField = layerNode.namedItem( QStringLiteral( "displayfield" ) ).toElement().text();
2469 if ( mFields.lookupField( displayField ) < 0 )
2470 {
2471 // if it's not a field, it's a maptip
2472 if ( mMapTipTemplate.isEmpty() && categories.testFlag( MapTips ) )
2473 mMapTipTemplate = displayField;
2474 }
2475 else
2476 {
2477 if ( mDisplayExpression.isEmpty() && categories.testFlag( LayerConfiguration ) )
2478 mDisplayExpression = QgsExpression::quotedColumnRef( displayField );
2479 }
2480
2481 // process the attribute actions
2482 if ( categories.testFlag( Actions ) )
2483 mActions->readXml( layerNode );
2484
2485 if ( categories.testFlag( Fields ) )
2486 {
2487 // IMPORTANT - we don't clear mAttributeAliasMap here, as it may contain aliases which are coming direct
2488 // from the data provider. Instead we leave any existing aliases and only overwrite them if the style
2489 // has a specific value for that field's alias
2490 QDomNode aliasesNode = layerNode.namedItem( QStringLiteral( "aliases" ) );
2491 if ( !aliasesNode.isNull() )
2492 {
2493 QDomElement aliasElem;
2494
2495 QDomNodeList aliasNodeList = aliasesNode.toElement().elementsByTagName( QStringLiteral( "alias" ) );
2496 for ( int i = 0; i < aliasNodeList.size(); ++i )
2497 {
2498 aliasElem = aliasNodeList.at( i ).toElement();
2499
2500 QString field;
2501 if ( aliasElem.hasAttribute( QStringLiteral( "field" ) ) )
2502 {
2503 field = aliasElem.attribute( QStringLiteral( "field" ) );
2504 }
2505 else
2506 {
2507 int index = aliasElem.attribute( QStringLiteral( "index" ) ).toInt();
2508
2509 if ( index >= 0 && index < fields().count() )
2510 field = fields().at( index ).name();
2511 }
2512
2513 QString alias;
2514
2515 if ( !aliasElem.attribute( QStringLiteral( "name" ) ).isEmpty() )
2516 {
2517 //if it has alias
2518 alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
2519 QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + aliasElem.attribute( QStringLiteral( "name" ) ), 3 );
2520 }
2521 else
2522 {
2523 //if it has no alias, it should be the fields translation
2524 alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
2525 QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + field, 3 );
2526 //if it gets the exact field value, there has been no translation (or not even translation loaded) - so no alias should be generated;
2527 if ( alias == aliasElem.attribute( QStringLiteral( "field" ) ) )
2528 alias.clear();
2529 }
2530
2531 QgsDebugMsgLevel( "field " + field + " origalias " + aliasElem.attribute( QStringLiteral( "name" ) ) + " trans " + alias, 3 );
2532 mAttributeAliasMap.insert( field, alias );
2533 }
2534 }
2535
2536 // IMPORTANT - we don't clear mAttributeSplitPolicy here, as it may contain policies which are coming direct
2537 // from the data provider. Instead we leave any existing policies and only overwrite them if the style
2538 // has a specific value for that field's policy
2539 const QDomNode splitPoliciesNode = layerNode.namedItem( QStringLiteral( "splitPolicies" ) );
2540 if ( !splitPoliciesNode.isNull() )
2541 {
2542 const QDomNodeList splitPolicyNodeList = splitPoliciesNode.toElement().elementsByTagName( QStringLiteral( "policy" ) );
2543 for ( int i = 0; i < splitPolicyNodeList.size(); ++i )
2544 {
2545 const QDomElement splitPolicyElem = splitPolicyNodeList.at( i ).toElement();
2546 const QString field = splitPolicyElem.attribute( QStringLiteral( "field" ) );
2547 const Qgis::FieldDomainSplitPolicy policy = qgsEnumKeyToValue( splitPolicyElem.attribute( QStringLiteral( "policy" ) ), Qgis::FieldDomainSplitPolicy::Duplicate );
2548 mAttributeSplitPolicy.insert( field, policy );
2549 }
2550 }
2551
2552 // default expressions
2553 mDefaultExpressionMap.clear();
2554 QDomNode defaultsNode = layerNode.namedItem( QStringLiteral( "defaults" ) );
2555 if ( !defaultsNode.isNull() )
2556 {
2557 QDomNodeList defaultNodeList = defaultsNode.toElement().elementsByTagName( QStringLiteral( "default" ) );
2558 for ( int i = 0; i < defaultNodeList.size(); ++i )
2559 {
2560 QDomElement defaultElem = defaultNodeList.at( i ).toElement();
2561
2562 QString field = defaultElem.attribute( QStringLiteral( "field" ), QString() );
2563 QString expression = defaultElem.attribute( QStringLiteral( "expression" ), QString() );
2564 bool applyOnUpdate = defaultElem.attribute( QStringLiteral( "applyOnUpdate" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
2565 if ( field.isEmpty() || expression.isEmpty() )
2566 continue;
2567
2568 mDefaultExpressionMap.insert( field, QgsDefaultValue( expression, applyOnUpdate ) );
2569 }
2570 }
2571
2572 // constraints
2573 mFieldConstraints.clear();
2574 mFieldConstraintStrength.clear();
2575 QDomNode constraintsNode = layerNode.namedItem( QStringLiteral( "constraints" ) );
2576 if ( !constraintsNode.isNull() )
2577 {
2578 QDomNodeList constraintNodeList = constraintsNode.toElement().elementsByTagName( QStringLiteral( "constraint" ) );
2579 for ( int i = 0; i < constraintNodeList.size(); ++i )
2580 {
2581 QDomElement constraintElem = constraintNodeList.at( i ).toElement();
2582
2583 QString field = constraintElem.attribute( QStringLiteral( "field" ), QString() );
2584 int constraints = constraintElem.attribute( QStringLiteral( "constraints" ), QStringLiteral( "0" ) ).toInt();
2585 if ( field.isEmpty() || constraints == 0 )
2586 continue;
2587
2588 mFieldConstraints.insert( field, static_cast< QgsFieldConstraints::Constraints >( constraints ) );
2589
2590 int uniqueStrength = constraintElem.attribute( QStringLiteral( "unique_strength" ), QStringLiteral( "1" ) ).toInt();
2591 int notNullStrength = constraintElem.attribute( QStringLiteral( "notnull_strength" ), QStringLiteral( "1" ) ).toInt();
2592 int expStrength = constraintElem.attribute( QStringLiteral( "exp_strength" ), QStringLiteral( "1" ) ).toInt();
2593
2594 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintUnique ), static_cast< QgsFieldConstraints::ConstraintStrength >( uniqueStrength ) );
2595 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintNotNull ), static_cast< QgsFieldConstraints::ConstraintStrength >( notNullStrength ) );
2596 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintExpression ), static_cast< QgsFieldConstraints::ConstraintStrength >( expStrength ) );
2597 }
2598 }
2599 mFieldConstraintExpressions.clear();
2600 QDomNode constraintExpressionsNode = layerNode.namedItem( QStringLiteral( "constraintExpressions" ) );
2601 if ( !constraintExpressionsNode.isNull() )
2602 {
2603 QDomNodeList constraintNodeList = constraintExpressionsNode.toElement().elementsByTagName( QStringLiteral( "constraint" ) );
2604 for ( int i = 0; i < constraintNodeList.size(); ++i )
2605 {
2606 QDomElement constraintElem = constraintNodeList.at( i ).toElement();
2607
2608 QString field = constraintElem.attribute( QStringLiteral( "field" ), QString() );
2609 QString exp = constraintElem.attribute( QStringLiteral( "exp" ), QString() );
2610 QString desc = constraintElem.attribute( QStringLiteral( "desc" ), QString() );
2611 if ( field.isEmpty() || exp.isEmpty() )
2612 continue;
2613
2614 mFieldConstraintExpressions.insert( field, qMakePair( exp, desc ) );
2615 }
2616 }
2617
2618 updateFields();
2619 }
2620
2621 // load field configuration
2622 if ( categories.testFlag( Fields ) || categories.testFlag( Forms ) )
2623 {
2624 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Forms" ) );
2625
2626 QDomElement widgetsElem = layerNode.namedItem( QStringLiteral( "fieldConfiguration" ) ).toElement();
2627 QDomNodeList fieldConfigurationElementList = widgetsElem.elementsByTagName( QStringLiteral( "field" ) );
2628 for ( int i = 0; i < fieldConfigurationElementList.size(); ++i )
2629 {
2630 const QDomElement fieldConfigElement = fieldConfigurationElementList.at( i ).toElement();
2631 const QDomElement fieldWidgetElement = fieldConfigElement.elementsByTagName( QStringLiteral( "editWidget" ) ).at( 0 ).toElement();
2632
2633 QString fieldName = fieldConfigElement.attribute( QStringLiteral( "name" ) );
2634
2635 if ( categories.testFlag( Fields ) )
2636 mFieldConfigurationFlags[fieldName] = qgsFlagKeysToValue( fieldConfigElement.attribute( QStringLiteral( "configurationFlags" ) ), Qgis::FieldConfigurationFlag::NoFlag );
2637
2638 // Load editor widget configuration
2639 if ( categories.testFlag( Forms ) )
2640 {
2641 const QString widgetType = fieldWidgetElement.attribute( QStringLiteral( "type" ) );
2642 const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
2643 const QDomElement optionsElem = cfgElem.childNodes().at( 0 ).toElement();
2644 QVariantMap optionsMap = QgsXmlUtils::readVariant( optionsElem ).toMap();
2645 if ( widgetType == QLatin1String( "ValueRelation" ) )
2646 {
2647 optionsMap[ QStringLiteral( "Value" ) ] = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fields:%2:valuerelationvalue" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text(), fieldName ), optionsMap[ QStringLiteral( "Value" ) ].toString() );
2648 }
2649 QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, optionsMap );
2650 mFieldWidgetSetups[fieldName] = setup;
2651 }
2652 }
2653 }
2654
2655 // Legacy reading for QGIS 3.14 and older projects
2656 // Attributes excluded from WMS and WFS
2657 if ( categories.testFlag( Fields ) )
2658 {
2659 const QList<QPair<QString, Qgis::FieldConfigurationFlag>> legacyConfig
2660 {
2661 qMakePair( QStringLiteral( "excludeAttributesWMS" ), Qgis::FieldConfigurationFlag::HideFromWms ),
2662 qMakePair( QStringLiteral( "excludeAttributesWFS" ), Qgis::FieldConfigurationFlag::HideFromWfs )
2663 };
2664 for ( const auto &config : legacyConfig )
2665 {
2666 QDomNode excludeNode = layerNode.namedItem( config.first );
2667 if ( !excludeNode.isNull() )
2668 {
2669 QDomNodeList attributeNodeList = excludeNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
2670 for ( int i = 0; i < attributeNodeList.size(); ++i )
2671 {
2672 QString fieldName = attributeNodeList.at( i ).toElement().text();
2673 if ( !mFieldConfigurationFlags.contains( fieldName ) )
2674 mFieldConfigurationFlags[fieldName] = config.second;
2675 else
2676 mFieldConfigurationFlags[fieldName].setFlag( config.second, true );
2677 }
2678 }
2679 }
2680 }
2681
2682 if ( categories.testFlag( GeometryOptions ) )
2683 mGeometryOptions->readXml( layerNode.namedItem( QStringLiteral( "geometryOptions" ) ) );
2684
2685 if ( categories.testFlag( Forms ) )
2686 mEditFormConfig.readXml( layerNode, context );
2687
2688 if ( categories.testFlag( AttributeTable ) )
2689 {
2690 mAttributeTableConfig.readXml( layerNode );
2691 mConditionalStyles->readXml( layerNode, context );
2692 mStoredExpressionManager->readXml( layerNode );
2693 }
2694
2695 if ( categories.testFlag( CustomProperties ) )
2696 readCustomProperties( layerNode, QStringLiteral( "variable" ) );
2697
2698 QDomElement mapLayerNode = layerNode.toElement();
2699 if ( categories.testFlag( LayerConfiguration )
2700 && mapLayerNode.attribute( QStringLiteral( "readOnly" ), QStringLiteral( "0" ) ).toInt() == 1 )
2701 mReadOnly = true;
2702
2703 updateFields();
2704
2705 if ( categories.testFlag( Legend ) )
2706 {
2707 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Legend" ) );
2708
2709 const QDomElement legendElem = layerNode.firstChildElement( QStringLiteral( "legend" ) );
2710 if ( !legendElem.isNull() )
2711 {
2712 std::unique_ptr< QgsMapLayerLegend > legend( QgsMapLayerLegend::defaultVectorLegend( this ) );
2713 legend->readXml( legendElem, context );
2714 setLegend( legend.release() );
2715 mSetLegendFromStyle = true;
2716 }
2717 }
2718
2719 return true;
2720}
2721
2722bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage,
2723 QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
2724{
2726
2727 bool result = true;
2728 emit readCustomSymbology( node.toElement(), errorMessage );
2729
2730 // we must try to restore a renderer if our geometry type is unknown
2731 // as this allows the renderer to be correctly restored even for layers
2732 // with broken sources
2733 if ( isSpatial() || mWkbType == Qgis::WkbType::Unknown )
2734 {
2735 // defer style changed signal until we've set the renderer, labeling, everything.
2736 // we don't want multiple signals!
2737 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
2738
2739 // try renderer v2 first
2740 if ( categories.testFlag( Symbology ) )
2741 {
2742 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Symbology" ) );
2743
2744 QDomElement rendererElement = node.firstChildElement( RENDERER_TAG_NAME );
2745 if ( !rendererElement.isNull() )
2746 {
2747 QgsFeatureRenderer *r = QgsFeatureRenderer::load( rendererElement, context );
2748 if ( r )
2749 {
2750 setRenderer( r );
2751 }
2752 else
2753 {
2754 result = false;
2755 }
2756 }
2757 // make sure layer has a renderer - if none exists, fallback to a default renderer
2758 if ( isSpatial() && !renderer() )
2759 {
2761 }
2762
2763 if ( mSelectionProperties )
2764 mSelectionProperties->readXml( node.toElement(), context );
2765 }
2766
2767 // read labeling definition
2768 if ( categories.testFlag( Labeling ) )
2769 {
2770 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Labeling" ) );
2771
2772 QDomElement labelingElement = node.firstChildElement( QStringLiteral( "labeling" ) );
2774 if ( labelingElement.isNull() ||
2775 ( labelingElement.attribute( QStringLiteral( "type" ) ) == QLatin1String( "simple" ) && labelingElement.firstChildElement( QStringLiteral( "settings" ) ).isNull() ) )
2776 {
2777 // make sure we have custom properties for labeling for 2.x projects
2778 // (custom properties should be already loaded when reading the whole layer from XML,
2779 // but when reading style, custom properties are not read)
2780 readCustomProperties( node, QStringLiteral( "labeling" ) );
2781
2782 // support for pre-QGIS 3 labeling configurations written in custom properties
2783 labeling = readLabelingFromCustomProperties();
2784 }
2785 else
2786 {
2787 labeling = QgsAbstractVectorLayerLabeling::create( labelingElement, context );
2788 }
2790
2791 if ( node.toElement().hasAttribute( QStringLiteral( "labelsEnabled" ) ) )
2792 mLabelsEnabled = node.toElement().attribute( QStringLiteral( "labelsEnabled" ) ).toInt();
2793 else
2794 mLabelsEnabled = true;
2795 }
2796
2797 if ( categories.testFlag( Symbology ) )
2798 {
2799 // get and set the blend mode if it exists
2800 QDomNode blendModeNode = node.namedItem( QStringLiteral( "blendMode" ) );
2801 if ( !blendModeNode.isNull() )
2802 {
2803 QDomElement e = blendModeNode.toElement();
2804 setBlendMode( QgsPainting::getCompositionMode( static_cast< Qgis::BlendMode >( e.text().toInt() ) ) );
2805 }
2806
2807 // get and set the feature blend mode if it exists
2808 QDomNode featureBlendModeNode = node.namedItem( QStringLiteral( "featureBlendMode" ) );
2809 if ( !featureBlendModeNode.isNull() )
2810 {
2811 QDomElement e = featureBlendModeNode.toElement();
2812 setFeatureBlendMode( QgsPainting::getCompositionMode( static_cast< Qgis::BlendMode >( e.text().toInt() ) ) );
2813 }
2814 }
2815
2816 // get and set the layer transparency and scale visibility if they exists
2817 if ( categories.testFlag( Rendering ) )
2818 {
2819 QDomNode layerTransparencyNode = node.namedItem( QStringLiteral( "layerTransparency" ) );
2820 if ( !layerTransparencyNode.isNull() )
2821 {
2822 QDomElement e = layerTransparencyNode.toElement();
2823 setOpacity( 1.0 - e.text().toInt() / 100.0 );
2824 }
2825 QDomNode layerOpacityNode = node.namedItem( QStringLiteral( "layerOpacity" ) );
2826 if ( !layerOpacityNode.isNull() )
2827 {
2828 QDomElement e = layerOpacityNode.toElement();
2829 setOpacity( e.text().toDouble() );
2830 }
2831
2832 const bool hasScaleBasedVisibiliy { node.attributes().namedItem( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).nodeValue() == '1' };
2833 setScaleBasedVisibility( hasScaleBasedVisibiliy );
2834 bool ok;
2835 const double maxScale { node.attributes().namedItem( QStringLiteral( "maxScale" ) ).nodeValue().toDouble( &ok ) };
2836 if ( ok )
2837 {
2838 setMaximumScale( maxScale );
2839 }
2840 const double minScale { node.attributes().namedItem( QStringLiteral( "minScale" ) ).nodeValue().toDouble( &ok ) };
2841 if ( ok )
2842 {
2843 setMinimumScale( minScale );
2844 }
2845
2846 QDomElement e = node.toElement();
2847
2848 // get the simplification drawing settings
2849 mSimplifyMethod.setSimplifyHints( static_cast< QgsVectorSimplifyMethod::SimplifyHints >( e.attribute( QStringLiteral( "simplifyDrawingHints" ), QStringLiteral( "1" ) ).toInt() ) );
2850 mSimplifyMethod.setSimplifyAlgorithm( static_cast< QgsVectorSimplifyMethod::SimplifyAlgorithm >( e.attribute( QStringLiteral( "simplifyAlgorithm" ), QStringLiteral( "0" ) ).toInt() ) );
2851 mSimplifyMethod.setThreshold( e.attribute( QStringLiteral( "simplifyDrawingTol" ), QStringLiteral( "1" ) ).toFloat() );
2852 mSimplifyMethod.setForceLocalOptimization( e.attribute( QStringLiteral( "simplifyLocal" ), QStringLiteral( "1" ) ).toInt() );
2853 mSimplifyMethod.setMaximumScale( e.attribute( QStringLiteral( "simplifyMaxScale" ), QStringLiteral( "1" ) ).toFloat() );
2854
2855 if ( mRenderer )
2856 mRenderer->setReferenceScale( e.attribute( QStringLiteral( "symbologyReferenceScale" ), QStringLiteral( "-1" ) ).toDouble() );
2857 }
2858
2859 //diagram renderer and diagram layer settings
2860 if ( categories.testFlag( Diagrams ) )
2861 {
2862 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Diagrams" ) );
2863
2864 delete mDiagramRenderer;
2865 mDiagramRenderer = nullptr;
2866 QDomElement singleCatDiagramElem = node.firstChildElement( QStringLiteral( "SingleCategoryDiagramRenderer" ) );
2867 if ( !singleCatDiagramElem.isNull() )
2868 {
2869 mDiagramRenderer = new QgsSingleCategoryDiagramRenderer();
2870 mDiagramRenderer->readXml( singleCatDiagramElem, context );
2871 }
2872 QDomElement linearDiagramElem = node.firstChildElement( QStringLiteral( "LinearlyInterpolatedDiagramRenderer" ) );
2873 if ( !linearDiagramElem.isNull() )
2874 {
2875 if ( linearDiagramElem.hasAttribute( QStringLiteral( "classificationAttribute" ) ) )
2876 {
2877 // fix project from before QGIS 3.0
2878 int idx = linearDiagramElem.attribute( QStringLiteral( "classificationAttribute" ) ).toInt();
2879 if ( idx >= 0 && idx < mFields.count() )
2880 linearDiagramElem.setAttribute( QStringLiteral( "classificationField" ), mFields.at( idx ).name() );
2881 }
2882
2883 mDiagramRenderer = new QgsLinearlyInterpolatedDiagramRenderer();
2884 mDiagramRenderer->readXml( linearDiagramElem, context );
2885 }
2886
2887 if ( mDiagramRenderer )
2888 {
2889 QDomElement diagramSettingsElem = node.firstChildElement( QStringLiteral( "DiagramLayerSettings" ) );
2890 if ( !diagramSettingsElem.isNull() )
2891 {
2892 bool oldXPos = diagramSettingsElem.hasAttribute( QStringLiteral( "xPosColumn" ) );
2893 bool oldYPos = diagramSettingsElem.hasAttribute( QStringLiteral( "yPosColumn" ) );
2894 bool oldShow = diagramSettingsElem.hasAttribute( QStringLiteral( "showColumn" ) );
2895 if ( oldXPos || oldYPos || oldShow )
2896 {
2897 // fix project from before QGIS 3.0
2899 if ( oldXPos )
2900 {
2901 int xPosColumn = diagramSettingsElem.attribute( QStringLiteral( "xPosColumn" ) ).toInt();
2902 if ( xPosColumn >= 0 && xPosColumn < mFields.count() )
2904 }
2905 if ( oldYPos )
2906 {
2907 int yPosColumn = diagramSettingsElem.attribute( QStringLiteral( "yPosColumn" ) ).toInt();
2908 if ( yPosColumn >= 0 && yPosColumn < mFields.count() )
2910 }
2911 if ( oldShow )
2912 {
2913 int showColumn = diagramSettingsElem.attribute( QStringLiteral( "showColumn" ) ).toInt();
2914 if ( showColumn >= 0 && showColumn < mFields.count() )
2915 ddp.setProperty( QgsDiagramLayerSettings::Property::Show, QgsProperty::fromField( mFields.at( showColumn ).name(), true ) );
2916 }
2917 QDomElement propertiesElem = diagramSettingsElem.ownerDocument().createElement( QStringLiteral( "properties" ) );
2919 {
2920 { static_cast< int >( QgsDiagramLayerSettings::Property::PositionX ), QgsPropertyDefinition( "positionX", QObject::tr( "Position (X)" ), QgsPropertyDefinition::Double ) },
2921 { static_cast< int >( QgsDiagramLayerSettings::Property::PositionY ), QgsPropertyDefinition( "positionY", QObject::tr( "Position (Y)" ), QgsPropertyDefinition::Double ) },
2922 { static_cast< int >( QgsDiagramLayerSettings::Property::Show ), QgsPropertyDefinition( "show", QObject::tr( "Show diagram" ), QgsPropertyDefinition::Boolean ) },
2923 };
2924 ddp.writeXml( propertiesElem, defs );
2925 diagramSettingsElem.appendChild( propertiesElem );
2926 }
2927
2928 delete mDiagramLayerSettings;
2929 mDiagramLayerSettings = new QgsDiagramLayerSettings();
2930 mDiagramLayerSettings->readXml( diagramSettingsElem );
2931 }
2932 }
2933 }
2934 // end diagram
2935
2936 styleChangedSignalBlocker.release();
2938 }
2939 return result;
2940}
2941
2942
2943bool QgsVectorLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
2944 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
2945{
2947
2948 QDomElement layerElement = node.toElement();
2949 writeCommonStyle( layerElement, doc, context, categories );
2950
2951 ( void )writeStyle( node, doc, errorMessage, context, categories );
2952
2953 if ( categories.testFlag( GeometryOptions ) )
2954 mGeometryOptions->writeXml( node );
2955
2956 if ( categories.testFlag( Legend ) && legend() )
2957 {
2958 QDomElement legendElement = legend()->writeXml( doc, context );
2959 if ( !legendElement.isNull() )
2960 node.appendChild( legendElement );
2961 }
2962
2963 // Relation information for both referenced and referencing sides
2964 if ( categories.testFlag( Relations ) )
2965 {
2966 // Store referenced layers: relations where "this" is the child layer (the referencing part, that holds the FK)
2967 QDomElement referencedLayersElement = doc.createElement( QStringLiteral( "referencedLayers" ) );
2968 node.appendChild( referencedLayersElement );
2969
2970 const QList<QgsRelation> referencingRelations { QgsProject::instance()->relationManager()->referencingRelations( this ) };
2971 for ( const QgsRelation &rel : referencingRelations )
2972 {
2973 switch ( rel.type() )
2974 {
2976 QgsWeakRelation::writeXml( this, QgsWeakRelation::Referencing, rel, referencedLayersElement, doc );
2977 break;
2979 break;
2980 }
2981 }
2982
2983 // Store referencing layers: relations where "this" is the parent layer (the referenced part, that holds the FK)
2984 QDomElement referencingLayersElement = doc.createElement( QStringLiteral( "referencingLayers" ) );
2985 node.appendChild( referencedLayersElement );
2986
2987 const QList<QgsRelation> referencedRelations { QgsProject::instance()->relationManager()->referencedRelations( this ) };
2988 for ( const QgsRelation &rel : referencedRelations )
2989 {
2990 switch ( rel.type() )
2991 {
2993 QgsWeakRelation::writeXml( this, QgsWeakRelation::Referenced, rel, referencingLayersElement, doc );
2994 break;
2996 break;
2997 }
2998 }
2999 }
3000
3001 // write field configurations
3002 if ( categories.testFlag( Fields ) || categories.testFlag( Forms ) )
3003 {
3004 QDomElement fieldConfigurationElement;
3005 // field configuration flag
3006 fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );
3007 node.appendChild( fieldConfigurationElement );
3008
3009 for ( const QgsField &field : std::as_const( mFields ) )
3010 {
3011 QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
3012 fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );
3013 fieldConfigurationElement.appendChild( fieldElement );
3014
3015 if ( categories.testFlag( Fields ) )
3016 {
3017 fieldElement.setAttribute( QStringLiteral( "configurationFlags" ), qgsFlagValueToKeys( field.configurationFlags() ) );
3018 }
3019
3020 if ( categories.testFlag( Forms ) )
3021 {
3022 QgsEditorWidgetSetup widgetSetup = field.editorWidgetSetup();
3023
3024 // TODO : wrap this part in an if to only save if it was user-modified
3025 QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) );
3026 fieldElement.appendChild( editWidgetElement );
3027 editWidgetElement.setAttribute( QStringLiteral( "type" ), field.editorWidgetSetup().type() );
3028 QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) );
3029
3030 editWidgetConfigElement.appendChild( QgsXmlUtils::writeVariant( widgetSetup.config(), doc ) );
3031 editWidgetElement.appendChild( editWidgetConfigElement );
3032 // END TODO : wrap this part in an if to only save if it was user-modified
3033 }
3034 }
3035 }
3036
3037 if ( categories.testFlag( Fields ) )
3038 {
3039 //attribute aliases
3040 QDomElement aliasElem = doc.createElement( QStringLiteral( "aliases" ) );
3041 for ( const QgsField &field : std::as_const( mFields ) )
3042 {
3043 QDomElement aliasEntryElem = doc.createElement( QStringLiteral( "alias" ) );
3044 aliasEntryElem.setAttribute( QStringLiteral( "field" ), field.name() );
3045 aliasEntryElem.setAttribute( QStringLiteral( "index" ), mFields.indexFromName( field.name() ) );
3046 aliasEntryElem.setAttribute( QStringLiteral( "name" ), field.alias() );
3047 aliasElem.appendChild( aliasEntryElem );
3048 }
3049 node.appendChild( aliasElem );
3050
3051 //split policies
3052 {
3053 QDomElement splitPoliciesElement = doc.createElement( QStringLiteral( "splitPolicies" ) );
3054 for ( const QgsField &field : std::as_const( mFields ) )
3055 {
3056 QDomElement splitPolicyElem = doc.createElement( QStringLiteral( "policy" ) );
3057 splitPolicyElem.setAttribute( QStringLiteral( "field" ), field.name() );
3058 splitPolicyElem.setAttribute( QStringLiteral( "policy" ), qgsEnumValueToKey( field.splitPolicy() ) );
3059 splitPoliciesElement.appendChild( splitPolicyElem );
3060 }
3061 node.appendChild( splitPoliciesElement );
3062 }
3063
3064 //default expressions
3065 QDomElement defaultsElem = doc.createElement( QStringLiteral( "defaults" ) );
3066 for ( const QgsField &field : std::as_const( mFields ) )
3067 {
3068 QDomElement defaultElem = doc.createElement( QStringLiteral( "default" ) );
3069 defaultElem.setAttribute( QStringLiteral( "field" ), field.name() );
3070 defaultElem.setAttribute( QStringLiteral( "expression" ), field.defaultValueDefinition().expression() );
3071 defaultElem.setAttribute( QStringLiteral( "applyOnUpdate" ), field.defaultValueDefinition().applyOnUpdate() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
3072 defaultsElem.appendChild( defaultElem );
3073 }
3074 node.appendChild( defaultsElem );
3075
3076 // constraints
3077 QDomElement constraintsElem = doc.createElement( QStringLiteral( "constraints" ) );
3078 for ( const QgsField &field : std::as_const( mFields ) )
3079 {
3080 QDomElement constraintElem = doc.createElement( QStringLiteral( "constraint" ) );
3081 constraintElem.setAttribute( QStringLiteral( "field" ), field.name() );
3082 constraintElem.setAttribute( QStringLiteral( "constraints" ), field.constraints().constraints() );
3083 constraintElem.setAttribute( QStringLiteral( "unique_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
3084 constraintElem.setAttribute( QStringLiteral( "notnull_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
3085 constraintElem.setAttribute( QStringLiteral( "exp_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
3086 constraintsElem.appendChild( constraintElem );
3087 }
3088 node.appendChild( constraintsElem );
3089
3090 // constraint expressions
3091 QDomElement constraintExpressionsElem = doc.createElement( QStringLiteral( "constraintExpressions" ) );
3092 for ( const QgsField &field : std::as_const( mFields ) )
3093 {
3094 QDomElement constraintExpressionElem = doc.createElement( QStringLiteral( "constraint" ) );
3095 constraintExpressionElem.setAttribute( QStringLiteral( "field" ), field.name() );
3096 constraintExpressionElem.setAttribute( QStringLiteral( "exp" ), field.constraints().constraintExpression() );
3097 constraintExpressionElem.setAttribute( QStringLiteral( "desc" ), field.constraints().constraintDescription() );
3098 constraintExpressionsElem.appendChild( constraintExpressionElem );
3099 }
3100 node.appendChild( constraintExpressionsElem );
3101
3102 // save expression fields
3103 if ( !mExpressionFieldBuffer )
3104 {
3105 // can happen when saving style on a invalid layer
3107 dummy.writeXml( node, doc );
3108 }
3109 else
3110 {
3111 mExpressionFieldBuffer->writeXml( node, doc );
3112 }
3113 }
3114
3115 // add attribute actions
3116 if ( categories.testFlag( Actions ) )
3117 mActions->writeXml( node );
3118
3119 if ( categories.testFlag( AttributeTable ) )
3120 {
3121 mAttributeTableConfig.writeXml( node );
3122 mConditionalStyles->writeXml( node, doc, context );
3123 mStoredExpressionManager->writeXml( node );
3124 }
3125
3126 if ( categories.testFlag( Forms ) )
3127 mEditFormConfig.writeXml( node, context );
3128
3129 // save readonly state
3130 if ( categories.testFlag( LayerConfiguration ) )
3131 node.toElement().setAttribute( QStringLiteral( "readOnly" ), mReadOnly );
3132
3133 // save preview expression
3134 if ( categories.testFlag( LayerConfiguration ) )
3135 {
3136 QDomElement prevExpElem = doc.createElement( QStringLiteral( "previewExpression" ) );
3137 QDomText prevExpText = doc.createTextNode( mDisplayExpression );
3138 prevExpElem.appendChild( prevExpText );
3139 node.appendChild( prevExpElem );
3140 }
3141
3142 // save map tip
3143 if ( categories.testFlag( MapTips ) )
3144 {
3145 QDomElement mapTipElem = doc.createElement( QStringLiteral( "mapTip" ) );
3146 mapTipElem.setAttribute( QStringLiteral( "enabled" ), mapTipsEnabled() );
3147 QDomText mapTipText = doc.createTextNode( mMapTipTemplate );
3148 mapTipElem.appendChild( mapTipText );
3149 node.toElement().appendChild( mapTipElem );
3150 }
3151
3152 return true;
3153}
3154
3155bool QgsVectorLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
3156 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
3157{
3159
3160 QDomElement mapLayerNode = node.toElement();
3161
3162 emit writeCustomSymbology( mapLayerNode, doc, errorMessage );
3163
3164 // we must try to write the renderer if our geometry type is unknown
3165 // as this allows the renderer to be correctly restored even for layers
3166 // with broken sources
3167 if ( isSpatial() || mWkbType == Qgis::WkbType::Unknown )
3168 {
3169 if ( categories.testFlag( Symbology ) )
3170 {
3171 if ( mRenderer )
3172 {
3173 QDomElement rendererElement = mRenderer->save( doc, context );
3174 node.appendChild( rendererElement );
3175 }
3176 if ( mSelectionProperties )
3177 {
3178 mSelectionProperties->writeXml( mapLayerNode, doc, context );
3179 }
3180 }
3181
3182 if ( categories.testFlag( Labeling ) )
3183 {
3184 if ( mLabeling )
3185 {
3186 QDomElement labelingElement = mLabeling->save( doc, context );
3187 node.appendChild( labelingElement );
3188 }
3189 mapLayerNode.setAttribute( QStringLiteral( "labelsEnabled" ), mLabelsEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
3190 }
3191
3192 // save the simplification drawing settings
3193 if ( categories.testFlag( Rendering ) )
3194 {
3195 mapLayerNode.setAttribute( QStringLiteral( "simplifyDrawingHints" ), QString::number( mSimplifyMethod.simplifyHints() ) );
3196 mapLayerNode.setAttribute( QStringLiteral( "simplifyAlgorithm" ), QString::number( mSimplifyMethod.simplifyAlgorithm() ) );
3197 mapLayerNode.setAttribute( QStringLiteral( "simplifyDrawingTol" ), QString::number( mSimplifyMethod.threshold() ) );
3198 mapLayerNode.setAttribute( QStringLiteral( "simplifyLocal" ), mSimplifyMethod.forceLocalOptimization() ? 1 : 0 );
3199 mapLayerNode.setAttribute( QStringLiteral( "simplifyMaxScale" ), QString::number( mSimplifyMethod.maximumScale() ) );
3200 }
3201
3202 //save customproperties
3203 if ( categories.testFlag( CustomProperties ) )
3204 {
3205 writeCustomProperties( node, doc );
3206 }
3207
3208 if ( categories.testFlag( Symbology ) )
3209 {
3210 // add the blend mode field
3211 QDomElement blendModeElem = doc.createElement( QStringLiteral( "blendMode" ) );
3212 QDomText blendModeText = doc.createTextNode( QString::number( static_cast< int >( QgsPainting::getBlendModeEnum( blendMode() ) ) ) );
3213 blendModeElem.appendChild( blendModeText );
3214 node.appendChild( blendModeElem );
3215
3216 // add the feature blend mode field
3217 QDomElement featureBlendModeElem = doc.createElement( QStringLiteral( "featureBlendMode" ) );
3218 QDomText featureBlendModeText = doc.createTextNode( QString::number( static_cast< int >( QgsPainting::getBlendModeEnum( featureBlendMode() ) ) ) );
3219 featureBlendModeElem.appendChild( featureBlendModeText );
3220 node.appendChild( featureBlendModeElem );
3221 }
3222
3223 // add the layer opacity and scale visibility
3224 if ( categories.testFlag( Rendering ) )
3225 {
3226 QDomElement layerOpacityElem = doc.createElement( QStringLiteral( "layerOpacity" ) );
3227 QDomText layerOpacityText = doc.createTextNode( QString::number( opacity() ) );
3228 layerOpacityElem.appendChild( layerOpacityText );
3229 node.appendChild( layerOpacityElem );
3230 mapLayerNode.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
3231 mapLayerNode.setAttribute( QStringLiteral( "maxScale" ), maximumScale() );
3232 mapLayerNode.setAttribute( QStringLiteral( "minScale" ), minimumScale() );
3233
3234 mapLayerNode.setAttribute( QStringLiteral( "symbologyReferenceScale" ), mRenderer ? mRenderer->referenceScale() : -1 );
3235 }
3236
3237 if ( categories.testFlag( Diagrams ) && mDiagramRenderer )
3238 {
3239 mDiagramRenderer->writeXml( mapLayerNode, doc, context );
3240 if ( mDiagramLayerSettings )
3241 mDiagramLayerSettings->writeXml( mapLayerNode, doc );
3242 }
3243 }
3244 return true;
3245}
3246
3247bool QgsVectorLayer::readSld( const QDomNode &node, QString &errorMessage )
3248{
3250
3251 // get the Name element
3252 QDomElement nameElem = node.firstChildElement( QStringLiteral( "Name" ) );
3253 if ( nameElem.isNull() )
3254 {
3255 errorMessage = QStringLiteral( "Warning: Name element not found within NamedLayer while it's required." );
3256 }
3257
3258 if ( isSpatial() )
3259 {
3260 QgsFeatureRenderer *r = QgsFeatureRenderer::loadSld( node, geometryType(), errorMessage );
3261 if ( !r )
3262 return false;
3263
3264 // defer style changed signal until we've set the renderer, labeling, everything.
3265 // we don't want multiple signals!
3266 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
3267
3268 setRenderer( r );
3269
3270 // labeling
3271 readSldLabeling( node );
3272
3273 styleChangedSignalBlocker.release();
3275 }
3276 return true;
3277}
3278
3279bool QgsVectorLayer::writeSld( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props ) const
3280{
3282
3283 Q_UNUSED( errorMessage )
3284
3285 QVariantMap localProps = QVariantMap( props );
3287 {
3289 }
3290
3291 if ( isSpatial() )
3292 {
3293 // store the Name element
3294 QDomElement nameNode = doc.createElement( QStringLiteral( "se:Name" ) );
3295 nameNode.appendChild( doc.createTextNode( name() ) );
3296 node.appendChild( nameNode );
3297
3298 QDomElement userStyleElem = doc.createElement( QStringLiteral( "UserStyle" ) );
3299 node.appendChild( userStyleElem );
3300
3301 QDomElement nameElem = doc.createElement( QStringLiteral( "se:Name" ) );
3302 nameElem.appendChild( doc.createTextNode( name() ) );
3303
3304 userStyleElem.appendChild( nameElem );
3305
3306 QDomElement featureTypeStyleElem = doc.createElement( QStringLiteral( "se:FeatureTypeStyle" ) );
3307 userStyleElem.appendChild( featureTypeStyleElem );
3308
3309 mRenderer->toSld( doc, featureTypeStyleElem, localProps );
3310 if ( labelsEnabled() )
3311 {
3312 mLabeling->toSld( featureTypeStyleElem, localProps );
3313 }
3314 }
3315 return true;
3316}
3317
3318
3319bool QgsVectorLayer::changeGeometry( QgsFeatureId fid, QgsGeometry &geom, bool skipDefaultValue )
3320{
3322
3323 if ( !mEditBuffer || !mDataProvider )
3324 {
3325 return false;
3326 }
3327
3328 if ( mGeometryOptions->isActive() )
3329 mGeometryOptions->apply( geom );
3330
3331 updateExtents();
3332
3333 bool result = mEditBuffer->changeGeometry( fid, geom );
3334
3335 if ( result )
3336 {
3337 updateExtents();
3338 if ( !skipDefaultValue && !mDefaultValueOnUpdateFields.isEmpty() )
3339 updateDefaultValues( fid );
3340 }
3341 return result;
3342}
3343
3344
3345bool QgsVectorLayer::changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue, bool skipDefaultValues )
3346{
3348
3349 bool result = false;
3350
3351 switch ( fields().fieldOrigin( field ) )
3352 {
3354 result = mJoinBuffer->changeAttributeValue( fid, field, newValue, oldValue );
3355 if ( result )
3356 emit attributeValueChanged( fid, field, newValue );
3357 break;
3358
3362 {
3363 if ( mEditBuffer && mDataProvider )
3364 result = mEditBuffer->changeAttributeValue( fid, field, newValue, oldValue );
3365 break;
3366 }
3367
3369 break;
3370 }
3371
3372 if ( result && !skipDefaultValues && !mDefaultValueOnUpdateFields.isEmpty() )
3373 updateDefaultValues( fid );
3374
3375 return result;
3376}
3377
3378bool QgsVectorLayer::changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues, bool skipDefaultValues )
3379{
3381
3382 bool result = true;
3383
3384 QgsAttributeMap newValuesJoin;
3385 QgsAttributeMap oldValuesJoin;
3386
3387 QgsAttributeMap newValuesNotJoin;
3388 QgsAttributeMap oldValuesNotJoin;
3389
3390 for ( auto it = newValues.constBegin(); it != newValues.constEnd(); ++it )
3391 {
3392 const int field = it.key();
3393 const QVariant newValue = it.value();
3394 QVariant oldValue;
3395
3396 if ( oldValues.contains( field ) )
3397 oldValue = oldValues[field];
3398
3399 switch ( fields().fieldOrigin( field ) )
3400 {
3402 newValuesJoin[field] = newValue;
3403 oldValuesJoin[field] = oldValue;
3404 break;
3405
3409 {
3410 newValuesNotJoin[field] = newValue;
3411 oldValuesNotJoin[field] = oldValue;
3412 break;
3413 }
3414
3416 break;
3417 }
3418 }
3419
3420 if ( ! newValuesJoin.isEmpty() && mJoinBuffer )
3421 {
3422 result = mJoinBuffer->changeAttributeValues( fid, newValuesJoin, oldValuesJoin );
3423 }
3424
3425 if ( ! newValuesNotJoin.isEmpty() )
3426 {
3427 if ( mEditBuffer && mDataProvider )
3428 result &= mEditBuffer->changeAttributeValues( fid, newValuesNotJoin, oldValues );
3429 else
3430 result = false;
3431 }
3432
3433 if ( result && !skipDefaultValues && !mDefaultValueOnUpdateFields.isEmpty() )
3434 {
3435 updateDefaultValues( fid );
3436 }
3437
3438 return result;
3439}
3440
3442{
3444
3445 if ( !mEditBuffer || !mDataProvider )
3446 return false;
3447
3448 return mEditBuffer->addAttribute( field );
3449}
3450
3452{
3454
3455 if ( attIndex < 0 || attIndex >= fields().count() )
3456 return;
3457
3458 QString name = fields().at( attIndex ).name();
3459 mFields[ attIndex ].setAlias( QString() );
3460 if ( mAttributeAliasMap.contains( name ) )
3461 {
3462 mAttributeAliasMap.remove( name );
3463 updateFields();
3464 mEditFormConfig.setFields( mFields );
3465 emit layerModified();
3466 }
3467}
3468
3469bool QgsVectorLayer::renameAttribute( int index, const QString &newName )
3470{
3472
3473 if ( index < 0 || index >= fields().count() )
3474 return false;
3475
3476 switch ( mFields.fieldOrigin( index ) )
3477 {
3479 {
3480 if ( mExpressionFieldBuffer )
3481 {
3482 int oi = mFields.fieldOriginIndex( index );
3483 mExpressionFieldBuffer->renameExpression( oi, newName );
3484 updateFields();
3485 return true;
3486 }
3487 else
3488 {
3489 return false;
3490 }
3491 }
3492
3495
3496 if ( !mEditBuffer || !mDataProvider )
3497 return false;
3498
3499 return mEditBuffer->renameAttribute( index, newName );
3500
3503 return false;
3504
3505 }
3506
3507 return false; // avoid warning
3508}
3509
3510void QgsVectorLayer::setFieldAlias( int attIndex, const QString &aliasString )
3511{
3513
3514 if ( attIndex < 0 || attIndex >= fields().count() )
3515 return;
3516
3517 QString name = fields().at( attIndex ).name();
3518
3519 mAttributeAliasMap.insert( name, aliasString );
3520 mFields[ attIndex ].setAlias( aliasString );
3521 mEditFormConfig.setFields( mFields );
3522 emit layerModified(); // TODO[MD]: should have a different signal?
3523}
3524
3525QString QgsVectorLayer::attributeAlias( int index ) const
3526{
3528
3529 if ( index < 0 || index >= fields().count() )
3530 return QString();
3531
3532 return fields().at( index ).alias();
3533}
3534
3536{
3538
3539 if ( index >= 0 && index < mFields.count() )
3540 return mFields.at( index ).displayName();
3541 else
3542 return QString();
3543}
3544
3546{
3548
3549 return mAttributeAliasMap;
3550}
3551
3553{
3555
3556 if ( index < 0 || index >= fields().count() )
3557 return;
3558
3559 const QString name = fields().at( index ).name();
3560
3561 mAttributeSplitPolicy.insert( name, policy );
3562 mFields[ index ].setSplitPolicy( policy );
3563 mEditFormConfig.setFields( mFields );
3564 emit layerModified(); // TODO[MD]: should have a different signal?
3565}
3566
3568{
3570
3571 QSet<QString> excludeList;
3572 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
3573 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
3574 {
3575 if ( flagsIt->testFlag( Qgis::FieldConfigurationFlag::HideFromWms ) )
3576 {
3577 excludeList << flagsIt.key();
3578 }
3579 }
3580 return excludeList;
3581}
3582
3583void QgsVectorLayer::setExcludeAttributesWms( const QSet<QString> &att )
3584{
3586
3587 QMap< QString, Qgis::FieldConfigurationFlags >::iterator flagsIt = mFieldConfigurationFlags.begin();
3588 for ( ; flagsIt != mFieldConfigurationFlags.end(); ++flagsIt )
3589 {
3590 flagsIt->setFlag( Qgis::FieldConfigurationFlag::HideFromWms, att.contains( flagsIt.key() ) );
3591 }
3592 updateFields();
3593}
3594
3596{
3598
3599 QSet<QString> excludeList;
3600 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
3601 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
3602 {
3603 if ( flagsIt->testFlag( Qgis::FieldConfigurationFlag::HideFromWfs ) )
3604 {
3605 excludeList << flagsIt.key();
3606 }
3607 }
3608 return excludeList;
3609}
3610
3611void QgsVectorLayer::setExcludeAttributesWfs( const QSet<QString> &att )
3612{
3614
3615 QMap< QString, Qgis::FieldConfigurationFlags >::iterator flagsIt = mFieldConfigurationFlags.begin();
3616 for ( ; flagsIt != mFieldConfigurationFlags.end(); ++flagsIt )
3617 {
3618 flagsIt->setFlag( Qgis::FieldConfigurationFlag::HideFromWfs, att.contains( flagsIt.key() ) );
3619 }
3620 updateFields();
3621}
3622
3624{
3626
3627 if ( index < 0 || index >= fields().count() )
3628 return false;
3629
3630 if ( mFields.fieldOrigin( index ) == QgsFields::OriginExpression )
3631 {
3632 removeExpressionField( index );
3633 return true;
3634 }
3635
3636 if ( !mEditBuffer || !mDataProvider )
3637 return false;
3638
3639 return mEditBuffer->deleteAttribute( index );
3640}
3641
3642bool QgsVectorLayer::deleteAttributes( const QList<int> &attrs )
3643{
3645
3646 bool deleted = false;
3647
3648 // Remove multiple occurrences of same attribute
3649 QList<int> attrList = qgis::setToList( qgis::listToSet( attrs ) );
3650
3651 std::sort( attrList.begin(), attrList.end(), std::greater<int>() );
3652
3653 for ( int attr : std::as_const( attrList ) )
3654 {
3655 if ( deleteAttribute( attr ) )
3656 {
3657 deleted = true;
3658 }
3659 }
3660
3661 return deleted;
3662}
3663
3664bool QgsVectorLayer::deleteFeatureCascade( QgsFeatureId fid, QgsVectorLayer::DeleteContext *context )
3665{
3667
3668 if ( !mEditBuffer )
3669 return false;
3670
3671 if ( context && context->cascade )
3672 {
3673 const QList<QgsRelation> relations = context->project->relationManager()->referencedRelations( this );
3674 const bool hasRelationsOrJoins = !relations.empty() || mJoinBuffer->containsJoins();
3675 if ( hasRelationsOrJoins )
3676 {
3677 if ( context->mHandledFeatures.contains( this ) )
3678 {
3679 QgsFeatureIds &handledFeatureIds = context->mHandledFeatures[ this ];
3680 if ( handledFeatureIds.contains( fid ) )
3681 {
3682 // avoid endless recursion
3683 return false;
3684 }
3685 else
3686 {
3687 // add feature id
3688 handledFeatureIds << fid;
3689 }
3690 }
3691 else
3692 {
3693 // add layer and feature id
3694 context->mHandledFeatures.insert( this, QgsFeatureIds() << fid );
3695 }
3696
3697 for ( const QgsRelation &relation : relations )
3698 {
3699 //check if composition (and not association)
3700 switch ( relation.strength() )
3701 {
3703 {
3704 //get features connected over this relation
3705 QgsFeatureIterator relatedFeaturesIt = relation.getRelatedFeatures( getFeature( fid ) );
3706 QgsFeatureIds childFeatureIds;
3707 QgsFeature childFeature;
3708 while ( relatedFeaturesIt.nextFeature( childFeature ) )
3709 {
3710 childFeatureIds.insert( childFeature.id() );
3711 }
3712 if ( childFeatureIds.count() > 0 )
3713 {
3714 relation.referencingLayer()->startEditing();
3715 relation.referencingLayer()->deleteFeatures( childFeatureIds, context );
3716 }
3717 break;
3718 }
3719
3721 break;
3722 }
3723 }
3724 }
3725 }
3726
3727 if ( mJoinBuffer->containsJoins() )
3728 mJoinBuffer->deleteFeature( fid, context );
3729
3730 bool res = mEditBuffer->deleteFeature( fid );
3731
3732 return res;
3733}
3734
3736{
3738
3739 if ( !mEditBuffer )
3740 return false;
3741
3742 bool res = deleteFeatureCascade( fid, context );
3743
3744 if ( res )
3745 {
3746 updateExtents();
3747 }
3748
3749 return res;
3750}
3751
3753{
3755
3756 bool res = true;
3757
3758 if ( ( context && context->cascade ) || mJoinBuffer->containsJoins() )
3759 {
3760 // should ideally be "deleteFeaturesCascade" for performance!
3761 for ( QgsFeatureId fid : fids )
3762 res = deleteFeatureCascade( fid, context ) && res;
3763 }
3764 else
3765 {
3766 res = mEditBuffer && mEditBuffer->deleteFeatures( fids );
3767 }
3768
3769 if ( res )
3770 {
3771 mSelectedFeatureIds.subtract( fids ); // remove it from selection
3772 updateExtents();
3773 }
3774
3775 return res;
3776}
3777
3779{
3780 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
3782
3783 return mFields;
3784}
3785
3787{
3789
3790 QgsAttributeList pkAttributesList;
3791 if ( !mDataProvider )
3792 return pkAttributesList;
3793
3794 QgsAttributeList providerIndexes = mDataProvider->pkAttributeIndexes();
3795 for ( int i = 0; i < mFields.count(); ++i )
3796 {
3797 if ( mFields.fieldOrigin( i ) == QgsFields::OriginProvider &&
3798 providerIndexes.contains( mFields.fieldOriginIndex( i ) ) )
3799 pkAttributesList << i;
3800 }
3801
3802 return pkAttributesList;
3803}
3804
3806{
3808
3809 if ( !mDataProvider )
3810 return static_cast< long long >( Qgis::FeatureCountState::UnknownCount );
3811 return mDataProvider->featureCount() +
3812 ( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures().size() - mEditBuffer->deletedFeatureIds().size() : 0 );
3813}
3814
3816{
3818
3819 const QgsFeatureIds deletedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->deletedFeatureIds() : QgsFeatureIds() );
3820 const QgsFeatureMap addedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures() : QgsFeatureMap() );
3821
3822 if ( mEditBuffer && !deletedFeatures.empty() )
3823 {
3824 if ( addedFeatures.size() > deletedFeatures.size() )
3826 else
3828 }
3829
3830 if ( ( !mEditBuffer || addedFeatures.empty() ) && mDataProvider && mDataProvider->empty() )
3832 else
3834}
3835
3836bool QgsVectorLayer::commitChanges( bool stopEditing )
3837{
3839
3840 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
3841 return project()->commitChanges( mCommitErrors, stopEditing, this );
3842
3843 mCommitErrors.clear();
3844
3845 if ( !mDataProvider )
3846 {
3847 mCommitErrors << tr( "ERROR: no provider" );
3848 return false;
3849 }
3850
3851 if ( !mEditBuffer )
3852 {
3853 mCommitErrors << tr( "ERROR: layer not editable" );
3854 return false;
3855 }
3856
3857 emit beforeCommitChanges( stopEditing );
3858
3859 if ( !mAllowCommit )
3860 return false;
3861
3862 mCommitChangesActive = true;
3863
3864 bool success = false;
3865 if ( mEditBuffer->editBufferGroup() )
3866 success = mEditBuffer->editBufferGroup()->commitChanges( mCommitErrors, stopEditing );
3867 else
3868 success = mEditBuffer->commitChanges( mCommitErrors );
3869
3870 mCommitChangesActive = false;
3871
3872 if ( !mDeletedFids.empty() )
3873 {
3874 emit featuresDeleted( mDeletedFids );
3875 mDeletedFids.clear();
3876 }
3877
3878 if ( success )
3879 {
3880 if ( stopEditing )
3881 {
3882 clearEditBuffer();
3883 }
3884 undoStack()->clear();
3885 emit afterCommitChanges();
3886 if ( stopEditing )
3887 emit editingStopped();
3888 }
3889 else
3890 {
3891 QgsMessageLog::logMessage( tr( "Commit errors:\n %1" ).arg( mCommitErrors.join( QLatin1String( "\n " ) ) ) );
3892 }
3893
3894 updateFields();
3895
3896 mDataProvider->updateExtents();
3897 mDataProvider->leaveUpdateMode();
3898
3899 // This second call is required because OGR provider with JSON
3900 // driver might have changed fields order after the call to
3901 // leaveUpdateMode
3902 if ( mFields.names() != mDataProvider->fields().names() )
3903 {
3904 updateFields();
3905 }
3906
3908
3909 return success;
3910}
3911
3913{
3915
3916 return mCommitErrors;
3917}
3918
3919bool QgsVectorLayer::rollBack( bool deleteBuffer )
3920{
3922
3923 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
3924 return project()->rollBack( mCommitErrors, deleteBuffer, this );
3925
3926 if ( !mEditBuffer )
3927 {
3928 return false;
3929 }
3930
3931 if ( !mDataProvider )
3932 {
3933 mCommitErrors << tr( "ERROR: no provider" );
3934 return false;
3935 }
3936
3937 bool rollbackExtent = !mDataProvider->transaction() && ( !mEditBuffer->deletedFeatureIds().isEmpty() ||
3938 !mEditBuffer->addedFeatures().isEmpty() ||
3939 !mEditBuffer->changedGeometries().isEmpty() );
3940
3941 emit beforeRollBack();
3942
3943 mEditBuffer->rollBack();
3944
3945 emit afterRollBack();
3946
3947 if ( isModified() )
3948 {
3949 // new undo stack roll back method
3950 // old method of calling every undo could cause many canvas refreshes
3951 undoStack()->setIndex( 0 );
3952 }
3953
3954 updateFields();
3955
3956 if ( deleteBuffer )
3957 {
3958 delete mEditBuffer;
3959 mEditBuffer = nullptr;
3960 undoStack()->clear();
3961 }
3962 emit editingStopped();
3963
3964 if ( rollbackExtent )
3965 updateExtents();
3966
3967 mDataProvider->leaveUpdateMode();
3968
3970 return true;
3971}
3972
3974{
3976
3977 return mSelectedFeatureIds.size();
3978}
3979
3981{
3982 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
3984
3985 return mSelectedFeatureIds;
3986}
3987
3989{
3991
3992 QgsFeatureList features;
3993 features.reserve( mSelectedFeatureIds.count() );
3994 QgsFeature f;
3995
3997
3998 while ( it.nextFeature( f ) )
3999 {
4000 features.push_back( f );
4001 }
4002
4003 return features;
4004}
4005
4007{
4009
4010 if ( mSelectedFeatureIds.isEmpty() )
4011 return QgsFeatureIterator();
4012
4015
4016 if ( mSelectedFeatureIds.count() == 1 )
4017 request.setFilterFid( *mSelectedFeatureIds.constBegin() );
4018 else
4019 request.setFilterFids( mSelectedFeatureIds );
4020
4021 return getFeatures( request );
4022}
4023
4025{
4027
4028 if ( !mEditBuffer || !mDataProvider )
4029 return false;
4030
4031 if ( mGeometryOptions->isActive() )
4032 {
4033 for ( auto feature = features.begin(); feature != features.end(); ++feature )
4034 {
4035 QgsGeometry geom = feature->geometry();
4036 mGeometryOptions->apply( geom );
4037 feature->setGeometry( geom );
4038 }
4039 }
4040
4041 bool res = mEditBuffer->addFeatures( features );
4042 updateExtents();
4043
4044 if ( res && mJoinBuffer->containsJoins() )
4045 res = mJoinBuffer->addFeatures( features );
4046
4047 return res;
4048}
4049
4051{
4053
4054 // if layer is not spatial, it has not CRS!
4055 setCrs( ( isSpatial() && mDataProvider ) ? mDataProvider->crs() : QgsCoordinateReferenceSystem() );
4056}
4057
4059{
4061
4063 if ( exp.isField() )
4064 {
4065 return static_cast<const QgsExpressionNodeColumnRef *>( exp.rootNode() )->name();
4066 }
4067
4068 return QString();
4069}
4070
4071void QgsVectorLayer::setDisplayExpression( const QString &displayExpression )
4072{
4074
4075 if ( mDisplayExpression == displayExpression )
4076 return;
4077
4078 mDisplayExpression = displayExpression;
4080}
4081
4083{
4085
4086 if ( !mDisplayExpression.isEmpty() || mFields.isEmpty() )
4087 {
4088 return mDisplayExpression;
4089 }
4090 else
4091 {
4092 const QString candidateName = QgsVectorLayerUtils::guessFriendlyIdentifierField( mFields );
4093 if ( !candidateName.isEmpty() )
4094 {
4095 return QgsExpression::quotedColumnRef( candidateName );
4096 }
4097 else
4098 {
4099 return QString();
4100 }
4101 }
4102}
4103
4105{
4107
4108 // display expressions are used as a fallback when no explicit map tip template is set
4109 return mapTipsEnabled() && ( !mapTipTemplate().isEmpty() || !displayExpression().isEmpty() );
4110}
4111
4113{
4115
4116 return ( mEditBuffer && mDataProvider );
4117}
4118
4120{
4121 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
4123
4126}
4127
4128bool QgsVectorLayer::isReadOnly() const
4129{
4131
4132 return mDataSourceReadOnly || mReadOnly;
4133}
4134
4135bool QgsVectorLayer::setReadOnly( bool readonly )
4136{
4138
4139 // exit if the layer is in editing mode
4140 if ( readonly && mEditBuffer )
4141 return false;
4142
4143 // exit if the data source is in read-only mode
4144 if ( !readonly && mDataSourceReadOnly )
4145 return false;
4146
4147 mReadOnly = readonly;
4148 emit readOnlyChanged();
4149 return true;
4150}
4151
4153{
4155
4156 if ( ! mDataProvider )
4157 return false;
4158
4159 if ( mDataSourceReadOnly )
4160 return false;
4161
4162 return mDataProvider->capabilities() & QgsVectorDataProvider::EditingCapabilities && ! mReadOnly;
4163}
4164
4166{
4168
4169 emit beforeModifiedCheck();
4170 return mEditBuffer && mEditBuffer->isModified();
4171}
4172
4173bool QgsVectorLayer::isAuxiliaryField( int index, int &srcIndex ) const
4174{
4176
4177 bool auxiliaryField = false;
4178 srcIndex = -1;
4179
4180 if ( !auxiliaryLayer() )
4181 return auxiliaryField;
4182
4183 if ( index >= 0 && fields().fieldOrigin( index ) == QgsFields::OriginJoin )
4184 {
4185 const QgsVectorLayerJoinInfo *info = mJoinBuffer->joinForFieldIndex( index, fields(), srcIndex );
4186
4187 if ( info && info->joinLayerId() == auxiliaryLayer()->id() )
4188 auxiliaryField = true;
4189 }
4190
4191 return auxiliaryField;
4192}
4193
4195{
4197
4198 // we must allow setting a renderer if our geometry type is unknown
4199 // as this allows the renderer to be correctly set even for layers
4200 // with broken sources
4201 // (note that we allow REMOVING the renderer for non-spatial layers,
4202 // e.g. to permit removing the renderer when the layer changes from
4203 // a spatial layer to a non-spatial one)
4204 if ( r && !isSpatial() && mWkbType != Qgis::WkbType::Unknown )
4205 return;
4206
4207 if ( r != mRenderer )
4208 {
4209 delete mRenderer;
4210 mRenderer = r;
4211 mSymbolFeatureCounted = false;
4212 mSymbolFeatureCountMap.clear();
4213 mSymbolFeatureIdMap.clear();
4214
4215 if ( mRenderer )
4216 {
4217 const double refreshRate = QgsSymbolLayerUtils::rendererFrameRate( mRenderer );
4218 if ( refreshRate <= 0 )
4219 {
4220 mRefreshRendererTimer->stop();
4221 mRefreshRendererTimer->setInterval( 0 );
4222 }
4223 else
4224 {
4225 mRefreshRendererTimer->setInterval( 1000 / refreshRate );
4226 mRefreshRendererTimer->start();
4227 }
4228 }
4229
4230 emit rendererChanged();
4232 }
4233}
4234
4236{
4238
4239 if ( generator )
4240 {
4241 mRendererGenerators << generator;
4242 }
4243}
4244
4246{
4248
4249 for ( int i = mRendererGenerators.count() - 1; i >= 0; --i )
4250 {
4251 if ( mRendererGenerators.at( i )->id() == id )
4252 {
4253 delete mRendererGenerators.at( i );
4254 mRendererGenerators.removeAt( i );
4255 }
4256 }
4257}
4258
4259QList<const QgsFeatureRendererGenerator *> QgsVectorLayer::featureRendererGenerators() const
4260{
4261 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
4263
4264 QList< const QgsFeatureRendererGenerator * > res;
4265 for ( const QgsFeatureRendererGenerator *generator : mRendererGenerators )
4266 res << generator;
4267 return res;
4268}
4269
4270void QgsVectorLayer::beginEditCommand( const QString &text )
4271{
4273
4274 if ( !mDataProvider )
4275 {
4276 return;
4277 }
4278 if ( mDataProvider->transaction() )
4279 {
4280 QString ignoredError;
4281 mDataProvider->transaction()->createSavepoint( ignoredError );
4282 }
4283 undoStack()->beginMacro( text );
4284 mEditCommandActive = true;
4285 emit editCommandStarted( text );
4286}
4287
4289{
4291
4292 if ( !mDataProvider )
4293 {
4294 return;
4295 }
4296 undoStack()->endMacro();
4297 mEditCommandActive = false;
4298 if ( !mDeletedFids.isEmpty() )
4299 {
4300 if ( selectedFeatureCount() > 0 )
4301 {
4302 mSelectedFeatureIds.subtract( mDeletedFids );
4303 }
4304 emit featuresDeleted( mDeletedFids );
4305 mDeletedFids.clear();
4306 }
4307 emit editCommandEnded();
4308}
4309
4311{
4313
4314 if ( !mDataProvider )
4315 {
4316 return;
4317 }
4318 undoStack()->endMacro();
4319 undoStack()->undo();
4320
4321 // it's not directly possible to pop the last command off the stack (the destroyed one)
4322 // and delete, so we add a dummy obsolete command to force this to occur.
4323 // Pushing the new command deletes the destroyed one, and since the new
4324 // command is obsolete it's automatically deleted by the undo stack.
4325 std::unique_ptr< QUndoCommand > command = std::make_unique< QUndoCommand >();
4326 command->setObsolete( true );
4327 undoStack()->push( command.release() );
4328
4329 mEditCommandActive = false;
4330 mDeletedFids.clear();
4331 emit editCommandDestroyed();
4332}
4333
4335{
4337
4338 return mJoinBuffer->addJoin( joinInfo );
4339}
4340
4341bool QgsVectorLayer::removeJoin( const QString &joinLayerId )
4342{
4344
4345 return mJoinBuffer->removeJoin( joinLayerId );
4346}
4347
4348const QList< QgsVectorLayerJoinInfo > QgsVectorLayer::vectorJoins() const
4349{
4351
4352 return mJoinBuffer->vectorJoins();
4353}
4354
4355int QgsVectorLayer::addExpressionField( const QString &exp, const QgsField &fld )
4356{
4358
4359 emit beforeAddingExpressionField( fld.name() );
4360 mExpressionFieldBuffer->addExpression( exp, fld );
4361 updateFields();
4362 int idx = mFields.indexFromName( fld.name() );
4363 emit attributeAdded( idx );
4364 return idx;
4365}
4366
4368{
4370
4371 emit beforeRemovingExpressionField( index );
4372 int oi = mFields.fieldOriginIndex( index );
4373 mExpressionFieldBuffer->removeExpression( oi );
4374 updateFields();
4375 emit attributeDeleted( index );
4376}
4377
4378QString QgsVectorLayer::expressionField( int index ) const
4379{
4381
4382 if ( mFields.fieldOrigin( index ) != QgsFields::OriginExpression )
4383 return QString();
4384
4385 int oi = mFields.fieldOriginIndex( index );
4386 if ( oi < 0 || oi >= mExpressionFieldBuffer->expressions().size() )
4387 return QString();
4388
4389 return mExpressionFieldBuffer->expressions().at( oi ).cachedExpression.expression();
4390}
4391
4392void QgsVectorLayer::updateExpressionField( int index, const QString &exp )
4393{
4395
4396 int oi = mFields.fieldOriginIndex( index );
4397 mExpressionFieldBuffer->updateExpression( oi, exp );
4398}
4399
4401{
4402 // non fatal for now -- the QgsVirtualLayerTask class is not thread safe and calls this
4404
4405 if ( !mDataProvider )
4406 return;
4407
4408 QgsFields oldFields = mFields;
4409
4410 mFields = mDataProvider->fields();
4411
4412 // added / removed fields
4413 if ( mEditBuffer )
4414 mEditBuffer->updateFields( mFields );
4415
4416 // joined fields
4417 if ( mJoinBuffer->containsJoins() )
4418 mJoinBuffer->updateFields( mFields );
4419
4420 if ( mExpressionFieldBuffer )
4421 mExpressionFieldBuffer->updateFields( mFields );
4422
4423 // set aliases and default values
4424 for ( auto aliasIt = mAttributeAliasMap.constBegin(); aliasIt != mAttributeAliasMap.constEnd(); ++aliasIt )
4425 {
4426 int index = mFields.lookupField( aliasIt.key() );
4427 if ( index < 0 )
4428 continue;
4429
4430 mFields[ index ].setAlias( aliasIt.value() );
4431 }
4432
4433 for ( auto splitPolicyIt = mAttributeSplitPolicy.constBegin(); splitPolicyIt != mAttributeSplitPolicy.constEnd(); ++splitPolicyIt )
4434 {
4435 int index = mFields.lookupField( splitPolicyIt.key() );
4436 if ( index < 0 )
4437 continue;
4438
4439 mFields[ index ].setSplitPolicy( splitPolicyIt.value() );
4440 }
4441
4442 // Update configuration flags
4443 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
4444 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
4445 {
4446 int index = mFields.lookupField( flagsIt.key() );
4447 if ( index < 0 )
4448 continue;
4449
4450 mFields[index].setConfigurationFlags( flagsIt.value() );
4451 }
4452
4453 // Update default values
4454 mDefaultValueOnUpdateFields.clear();
4455 QMap< QString, QgsDefaultValue >::const_iterator defaultIt = mDefaultExpressionMap.constBegin();
4456 for ( ; defaultIt != mDefaultExpressionMap.constEnd(); ++defaultIt )
4457 {
4458 int index = mFields.lookupField( defaultIt.key() );
4459 if ( index < 0 )
4460 continue;
4461
4462 mFields[ index ].setDefaultValueDefinition( defaultIt.value() );
4463 if ( defaultIt.value().applyOnUpdate() )
4464 mDefaultValueOnUpdateFields.insert( index );
4465 }
4466
4467 QMap< QString, QgsFieldConstraints::Constraints >::const_iterator constraintIt = mFieldConstraints.constBegin();
4468 for ( ; constraintIt != mFieldConstraints.constEnd(); ++constraintIt )
4469 {
4470 int index = mFields.lookupField( constraintIt.key() );
4471 if ( index < 0 )
4472 continue;
4473
4474 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4475
4476 // always keep provider constraints intact
4477 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintNotNull ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintNotNull ) )
4479 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintUnique ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintUnique ) )
4481 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintExpression ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintExpression ) )
4483 mFields[ index ].setConstraints( constraints );
4484 }
4485
4486 QMap< QString, QPair< QString, QString > >::const_iterator constraintExpIt = mFieldConstraintExpressions.constBegin();
4487 for ( ; constraintExpIt != mFieldConstraintExpressions.constEnd(); ++constraintExpIt )
4488 {
4489 int index = mFields.lookupField( constraintExpIt.key() );
4490 if ( index < 0 )
4491 continue;
4492
4493 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4494
4495 // always keep provider constraints intact
4497 continue;
4498
4499 constraints.setConstraintExpression( constraintExpIt.value().first, constraintExpIt.value().second );
4500 mFields[ index ].setConstraints( constraints );
4501 }
4502
4503 QMap< QPair< QString, QgsFieldConstraints::Constraint >, QgsFieldConstraints::ConstraintStrength >::const_iterator constraintStrengthIt = mFieldConstraintStrength.constBegin();
4504 for ( ; constraintStrengthIt != mFieldConstraintStrength.constEnd(); ++constraintStrengthIt )
4505 {
4506 int index = mFields.lookupField( constraintStrengthIt.key().first );
4507 if ( index < 0 )
4508 continue;
4509
4510 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4511
4512 // always keep provider constraints intact
4514 continue;
4515
4516 constraints.setConstraintStrength( constraintStrengthIt.key().second, constraintStrengthIt.value() );
4517 mFields[ index ].setConstraints( constraints );
4518 }
4519
4520 auto fieldWidgetIterator = mFieldWidgetSetups.constBegin();
4521 for ( ; fieldWidgetIterator != mFieldWidgetSetups.constEnd(); ++ fieldWidgetIterator )
4522 {
4523 int index = mFields.indexOf( fieldWidgetIterator.key() );
4524 if ( index < 0 )
4525 continue;
4526
4527 mFields[index].setEditorWidgetSetup( fieldWidgetIterator.value() );
4528 }
4529
4530 if ( oldFields != mFields )
4531 {
4532 emit updatedFields();
4533 mEditFormConfig.setFields( mFields );
4534 }
4535
4536}
4537
4538QVariant QgsVectorLayer::defaultValue( int index, const QgsFeature &feature, QgsExpressionContext *context ) const
4539{
4541
4542 if ( index < 0 || index >= mFields.count() || !mDataProvider )
4543 return QVariant();
4544
4545 QString expression = mFields.at( index ).defaultValueDefinition().expression();
4546 if ( expression.isEmpty() )
4547 return mDataProvider->defaultValue( index );
4548
4549 QgsExpressionContext *evalContext = context;
4550 std::unique_ptr< QgsExpressionContext > tempContext;
4551 if ( !evalContext )
4552 {
4553 // no context passed, so we create a default one
4555 evalContext = tempContext.get();
4556 }
4557
4558 if ( feature.isValid() )
4559 {
4561 featScope->setFeature( feature );
4562 featScope->setFields( feature.fields() );
4563 evalContext->appendScope( featScope );
4564 }
4565
4566 QVariant val;
4567 QgsExpression exp( expression );
4568 exp.prepare( evalContext );
4569 if ( exp.hasEvalError() )
4570 {
4571 QgsLogger::warning( "Error evaluating default value: " + exp.evalErrorString() );
4572 }
4573 else
4574 {
4575 val = exp.evaluate( evalContext );
4576 }
4577
4578 if ( feature.isValid() )
4579 {
4580 delete evalContext->popScope();
4581 }
4582
4583 return val;
4584}
4585
4587{
4589
4590 if ( index < 0 || index >= mFields.count() )
4591 return;
4592
4593 if ( definition.isValid() )
4594 {
4595 mDefaultExpressionMap.insert( mFields.at( index ).name(), definition );
4596 }
4597 else
4598 {
4599 mDefaultExpressionMap.remove( mFields.at( index ).name() );
4600 }
4601 updateFields();
4602}
4603
4605{
4607
4608 if ( index < 0 || index >= mFields.count() )
4609 return QgsDefaultValue();
4610 else
4611 return mFields.at( index ).defaultValueDefinition();
4612}
4613
4614QSet<QVariant> QgsVectorLayer::uniqueValues( int index, int limit ) const
4615{
4617
4618 QSet<QVariant> uniqueValues;
4619 if ( !mDataProvider )
4620 {
4621 return uniqueValues;
4622 }
4623
4624 QgsFields::FieldOrigin origin = mFields.fieldOrigin( index );
4625 switch ( origin )
4626 {
4628 return uniqueValues;
4629
4630 case QgsFields::OriginProvider: //a provider field
4631 {
4632 uniqueValues = mDataProvider->uniqueValues( index, limit );
4633
4634 if ( mEditBuffer && ! mDataProvider->transaction() )
4635 {
4636 QSet<QString> vals;
4637 const auto constUniqueValues = uniqueValues;
4638 for ( const QVariant &v : constUniqueValues )
4639 {
4640 vals << v.toString();
4641 }
4642
4643 QgsFeatureMap added = mEditBuffer->addedFeatures();
4644 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
4645 while ( addedIt.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
4646 {
4647 addedIt.next();
4648 QVariant v = addedIt.value().attribute( index );
4649 if ( v.isValid() )
4650 {
4651 QString vs = v.toString();
4652 if ( !vals.contains( vs ) )
4653 {
4654 vals << vs;
4655 uniqueValues << v;
4656 }
4657 }
4658 }
4659
4660 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
4661 while ( it.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
4662 {
4663 it.next();
4664 QVariant v = it.value().value( index );
4665 if ( v.isValid() )
4666 {
4667 QString vs = v.toString();
4668 if ( !vals.contains( vs ) )
4669 {
4670 vals << vs;
4671 uniqueValues << v;
4672 }
4673 }
4674 }
4675 }
4676
4677 return uniqueValues;
4678 }
4679
4681 // the layer is editable, but in certain cases it can still be avoided going through all features
4682 if ( mDataProvider->transaction() || (
4683 mEditBuffer->deletedFeatureIds().isEmpty() &&
4684 mEditBuffer->addedFeatures().isEmpty() &&
4685 !mEditBuffer->deletedAttributeIds().contains( index ) &&
4686 mEditBuffer->changedAttributeValues().isEmpty() ) )
4687 {
4688 uniqueValues = mDataProvider->uniqueValues( index, limit );
4689 return uniqueValues;
4690 }
4691 [[fallthrough]];
4692 //we need to go through each feature
4695 {
4696 QgsAttributeList attList;
4697 attList << index;
4698
4701 .setSubsetOfAttributes( attList ) );
4702
4703 QgsFeature f;
4704 QVariant currentValue;
4705 QHash<QString, QVariant> val;
4706 while ( fit.nextFeature( f ) )
4707 {
4708 currentValue = f.attribute( index );
4709 val.insert( currentValue.toString(), currentValue );
4710 if ( limit >= 0 && val.size() >= limit )
4711 {
4712 break;
4713 }
4714 }
4715
4716 return qgis::listToSet( val.values() );
4717 }
4718 }
4719
4720 Q_ASSERT_X( false, "QgsVectorLayer::uniqueValues()", "Unknown source of the field!" );
4721 return uniqueValues;
4722}
4723
4724QStringList QgsVectorLayer::uniqueStringsMatching( int index, const QString &substring, int limit, QgsFeedback *feedback ) const
4725{
4727
4728 QStringList results;
4729 if ( !mDataProvider )
4730 {
4731 return results;
4732 }
4733
4734 QgsFields::FieldOrigin origin = mFields.fieldOrigin( index );
4735 switch ( origin )
4736 {
4738 return results;
4739
4740 case QgsFields::OriginProvider: //a provider field
4741 {
4742 results = mDataProvider->uniqueStringsMatching( index, substring, limit, feedback );
4743
4744 if ( mEditBuffer && ! mDataProvider->transaction() )
4745 {
4746 QgsFeatureMap added = mEditBuffer->addedFeatures();
4747 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
4748 while ( addedIt.hasNext() && ( limit < 0 || results.count() < limit ) && ( !feedback || !feedback->isCanceled() ) )
4749 {
4750 addedIt.next();
4751 QVariant v = addedIt.value().attribute( index );
4752 if ( v.isValid() )
4753 {
4754 QString vs = v.toString();
4755 if ( vs.contains( substring, Qt::CaseInsensitive ) && !results.contains( vs ) )
4756 {
4757 results << vs;
4758 }
4759 }
4760 }
4761
4762 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
4763 while ( it.hasNext() && ( limit < 0 || results.count() < limit ) && ( !feedback || !feedback->isCanceled() ) )
4764 {
4765 it.next();
4766 QVariant v = it.value().value( index );
4767 if ( v.isValid() )
4768 {
4769 QString vs = v.toString();
4770 if ( vs.contains( substring, Qt::CaseInsensitive ) && !results.contains( vs ) )
4771 {
4772 results << vs;
4773 }
4774 }
4775 }
4776 }
4777
4778 return results;
4779 }
4780
4782 // the layer is editable, but in certain cases it can still be avoided going through all features
4783 if ( mDataProvider->transaction() || ( mEditBuffer->deletedFeatureIds().isEmpty() &&
4784 mEditBuffer->addedFeatures().isEmpty() &&
4785 !mEditBuffer->deletedAttributeIds().contains( index ) &&
4786 mEditBuffer->changedAttributeValues().isEmpty() ) )
4787 {
4788 return mDataProvider->uniqueStringsMatching( index, substring, limit, feedback );
4789 }
4790 [[fallthrough]];
4791 //we need to go through each feature
4794 {
4795 QgsAttributeList attList;
4796 attList << index;
4797
4798 QgsFeatureRequest request;
4799 request.setSubsetOfAttributes( attList );
4801 QString fieldName = mFields.at( index ).name();
4802 request.setFilterExpression( QStringLiteral( "\"%1\" ILIKE '%%2%'" ).arg( fieldName, substring ) );
4803 QgsFeatureIterator fit = getFeatures( request );
4804
4805 QgsFeature f;
4806 QString currentValue;
4807 while ( fit.nextFeature( f ) )
4808 {
4809 currentValue = f.attribute( index ).toString();
4810 if ( !results.contains( currentValue ) )
4811 results << currentValue;
4812
4813 if ( ( limit >= 0 && results.size() >= limit ) || ( feedback && feedback->isCanceled() ) )
4814 {
4815 break;
4816 }
4817 }
4818
4819 return results;
4820 }
4821 }
4822
4823 Q_ASSERT_X( false, "QgsVectorLayer::uniqueStringsMatching()", "Unknown source of the field!" );
4824 return results;
4825}
4826
4827QVariant QgsVectorLayer::minimumValue( int index ) const
4828{
4830
4831 QVariant minimum;
4832 minimumOrMaximumValue( index, &minimum, nullptr );
4833 return minimum;
4834}
4835
4836QVariant QgsVectorLayer::maximumValue( int index ) const
4837{
4839
4840 QVariant maximum;
4841 minimumOrMaximumValue( index, nullptr, &maximum );
4842 return maximum;
4843}
4844
4845void QgsVectorLayer::minimumAndMaximumValue( int index, QVariant &minimum, QVariant &maximum ) const
4846{
4848
4849 minimumOrMaximumValue( index, &minimum, &maximum );
4850}
4851
4852void QgsVectorLayer::minimumOrMaximumValue( int index, QVariant *minimum, QVariant *maximum ) const
4853{
4855
4856 if ( minimum )
4857 *minimum = QVariant();
4858 if ( maximum )
4859 *maximum = QVariant();
4860
4861 if ( !mDataProvider )
4862 {
4863 return;
4864 }
4865
4866 QgsFields::FieldOrigin origin = mFields.fieldOrigin( index );
4867
4868 switch ( origin )
4869 {
4871 {
4872 return;
4873 }
4874
4875 case QgsFields::OriginProvider: //a provider field
4876 {
4877 if ( minimum )
4878 *minimum = mDataProvider->minimumValue( index );
4879 if ( maximum )
4880 *maximum = mDataProvider->maximumValue( index );
4881 if ( mEditBuffer && ! mDataProvider->transaction() )
4882 {
4883 const QgsFeatureMap added = mEditBuffer->addedFeatures();
4884 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
4885 while ( addedIt.hasNext() )
4886 {
4887 addedIt.next();
4888 const QVariant v = addedIt.value().attribute( index );
4889 if ( minimum && v.isValid() && qgsVariantLessThan( v, *minimum ) )
4890 *minimum = v;
4891 if ( maximum && v.isValid() && qgsVariantGreaterThan( v, *maximum ) )
4892 *maximum = v;
4893 }
4894
4895 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
4896 while ( it.hasNext() )
4897 {
4898 it.next();
4899 const QVariant v = it.value().value( index );
4900 if ( minimum && v.isValid() && qgsVariantLessThan( v, *minimum ) )
4901 *minimum = v;
4902 if ( maximum && v.isValid() && qgsVariantGreaterThan( v, *maximum ) )
4903 *maximum = v;
4904 }
4905 }
4906 return;
4907 }
4908
4910 {
4911 // the layer is editable, but in certain cases it can still be avoided going through all features
4912 if ( mDataProvider->transaction() || ( mEditBuffer->deletedFeatureIds().isEmpty() &&
4913 mEditBuffer->addedFeatures().isEmpty() &&
4914 !mEditBuffer->deletedAttributeIds().contains( index ) &&
4915 mEditBuffer->changedAttributeValues().isEmpty() ) )
4916 {
4917 if ( minimum )
4918 *minimum = mDataProvider->minimumValue( index );
4919 if ( maximum )
4920 *maximum = mDataProvider->maximumValue( index );
4921 return;
4922 }
4923 }
4924 [[fallthrough]];
4925 // no choice but to go through all features
4928 {
4929 // we need to go through each feature
4930 QgsAttributeList attList;
4931 attList << index;
4932
4935 .setSubsetOfAttributes( attList ) );
4936
4937 QgsFeature f;
4938 bool firstValue = true;
4939 while ( fit.nextFeature( f ) )
4940 {
4941 const QVariant currentValue = f.attribute( index );
4942 if ( QgsVariantUtils::isNull( currentValue ) )
4943 continue;
4944
4945 if ( firstValue )
4946 {
4947 if ( minimum )
4948 *minimum = currentValue;
4949 if ( maximum )
4950 *maximum = currentValue;
4951 firstValue = false;
4952 }
4953 else
4954 {
4955 if ( minimum && currentValue.isValid() && qgsVariantLessThan( currentValue, *minimum ) )
4956 *minimum = currentValue;
4957 if ( maximum && currentValue.isValid() && qgsVariantGreaterThan( currentValue, *maximum ) )
4958 *maximum = currentValue;
4959 }
4960 }
4961 return;
4962 }
4963 }
4964
4965 Q_ASSERT_X( false, "QgsVectorLayer::minimumOrMaximumValue()", "Unknown source of the field!" );
4966}
4967
4968void QgsVectorLayer::createEditBuffer()
4969{
4971
4972 if ( mEditBuffer )
4973 clearEditBuffer();
4974
4975 if ( mDataProvider->transaction() )
4976 {
4977 mEditBuffer = new QgsVectorLayerEditPassthrough( this );
4978
4979 connect( mDataProvider->transaction(), &QgsTransaction::dirtied, this, &QgsVectorLayer::onDirtyTransaction, Qt::UniqueConnection );
4980 }
4981 else
4982 {
4983 mEditBuffer = new QgsVectorLayerEditBuffer( this );
4984 }
4985 // forward signals
4986 connect( mEditBuffer, &QgsVectorLayerEditBuffer::layerModified, this, &QgsVectorLayer::invalidateSymbolCountedFlag );
4987 connect( mEditBuffer, &QgsVectorLayerEditBuffer::layerModified, this, &QgsVectorLayer::layerModified ); // TODO[MD]: necessary?
4988 //connect( mEditBuffer, SIGNAL( layerModified() ), this, SLOT( triggerRepaint() ) ); // TODO[MD]: works well?
4990 connect( mEditBuffer, &QgsVectorLayerEditBuffer::featureDeleted, this, &QgsVectorLayer::onFeatureDeleted );
5001
5002}
5003
5004void QgsVectorLayer::clearEditBuffer()
5005{
5007
5008 delete mEditBuffer;
5009 mEditBuffer = nullptr;
5010}
5011
5012QVariant QgsVectorLayer::aggregate( Qgis::Aggregate aggregate, const QString &fieldOrExpression,
5014 bool *ok, QgsFeatureIds *fids, QgsFeedback *feedback, QString *error ) const
5015{
5016 // non fatal for now -- the aggregate expression functions are not thread safe and call this
5018
5019 if ( ok )
5020 *ok = false;
5021 if ( error )
5022 error->clear();
5023
5024 if ( !mDataProvider )
5025 {
5026 if ( error )
5027 *error = tr( "Layer is invalid" );
5028 return QVariant();
5029 }
5030
5031 // test if we are calculating based on a field
5032 const int attrIndex = QgsExpression::expressionToLayerFieldIndex( fieldOrExpression, this );
5033 if ( attrIndex >= 0 )
5034 {
5035 // aggregate is based on a field - if it's a provider field, we could possibly hand over the calculation
5036 // to the provider itself
5037 QgsFields::FieldOrigin origin = mFields.fieldOrigin( attrIndex );
5038 if ( origin == QgsFields::OriginProvider )
5039 {
5040 bool providerOk = false;
5041 QVariant val = mDataProvider->aggregate( aggregate, attrIndex, parameters, context, providerOk, fids );
5042 if ( providerOk )
5043 {
5044 // provider handled calculation
5045 if ( ok )
5046 *ok = true;
5047 return val;
5048 }
5049 }
5050 }
5051
5052 // fallback to using aggregate calculator to determine aggregate
5053 QgsAggregateCalculator c( this );
5054 if ( fids )
5055 c.setFidsFilter( *fids );
5056 c.setParameters( parameters );
5057 bool aggregateOk = false;
5058 const QVariant result = c.calculate( aggregate, fieldOrExpression, context, &aggregateOk, feedback );
5059 if ( ok )
5060 *ok = aggregateOk;
5061 if ( !aggregateOk && error )
5062 *error = c.lastError();
5063
5064 return result;
5065}
5066
5067void QgsVectorLayer::setFeatureBlendMode( QPainter::CompositionMode featureBlendMode )
5068{
5070
5071 if ( mFeatureBlendMode == featureBlendMode )
5072 return;
5073
5074 mFeatureBlendMode = featureBlendMode;
5077}
5078
5079QPainter::CompositionMode QgsVectorLayer::featureBlendMode() const
5080{
5081 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
5083
5084 return mFeatureBlendMode;
5085}
5086
5087void QgsVectorLayer::readSldLabeling( const QDomNode &node )
5088{
5090
5091 setLabeling( nullptr ); // start with no labeling
5092 setLabelsEnabled( false );
5093
5094 QDomElement element = node.toElement();
5095 if ( element.isNull() )
5096 return;
5097
5098 QDomElement userStyleElem = element.firstChildElement( QStringLiteral( "UserStyle" ) );
5099 if ( userStyleElem.isNull() )
5100 {
5101 QgsDebugMsgLevel( QStringLiteral( "Info: UserStyle element not found." ), 4 );
5102 return;
5103 }
5104
5105 QDomElement featTypeStyleElem = userStyleElem.firstChildElement( QStringLiteral( "FeatureTypeStyle" ) );
5106 if ( featTypeStyleElem.isNull() )
5107 {
5108 QgsDebugMsgLevel( QStringLiteral( "Info: FeatureTypeStyle element not found." ), 4 );
5109 return;
5110 }
5111
5112 // create empty FeatureTypeStyle element to merge TextSymbolizer's Rule's from all FeatureTypeStyle's
5113 QDomElement mergedFeatTypeStyle = featTypeStyleElem.cloneNode( false ).toElement();
5114
5115 // use the RuleRenderer when more rules are present or the rule
5116 // has filters or min/max scale denominators set,
5117 // otherwise use the Simple labeling
5118 bool needRuleBasedLabeling = false;
5119 int ruleCount = 0;
5120
5121 while ( !featTypeStyleElem.isNull() )
5122 {
5123 QDomElement ruleElem = featTypeStyleElem.firstChildElement( QStringLiteral( "Rule" ) );
5124 while ( !ruleElem.isNull() )
5125 {
5126 // test rule children element to check if we need to create RuleRenderer
5127 // and if the rule has a symbolizer
5128 bool hasTextSymbolizer = false;
5129 bool hasRuleBased = false;
5130 QDomElement ruleChildElem = ruleElem.firstChildElement();
5131 while ( !ruleChildElem.isNull() )
5132 {
5133 // rule has filter or min/max scale denominator, use the RuleRenderer
5134 if ( ruleChildElem.localName() == QLatin1String( "Filter" ) ||
5135 ruleChildElem.localName() == QLatin1String( "MinScaleDenominator" ) ||
5136 ruleChildElem.localName() == QLatin1String( "MaxScaleDenominator" ) )
5137 {
5138 hasRuleBased = true;
5139 }
5140 // rule has a renderer symbolizer, not a text symbolizer
5141 else if ( ruleChildElem.localName() == QLatin1String( "TextSymbolizer" ) )
5142 {
5143 QgsDebugMsgLevel( QStringLiteral( "Info: TextSymbolizer element found" ), 4 );
5144 hasTextSymbolizer = true;
5145 }
5146
5147 ruleChildElem = ruleChildElem.nextSiblingElement();
5148 }
5149
5150 if ( hasTextSymbolizer )
5151 {
5152 ruleCount++;
5153
5154 // append a clone of all Rules to the merged FeatureTypeStyle element
5155 mergedFeatTypeStyle.appendChild( ruleElem.cloneNode().toElement() );
5156
5157 if ( hasRuleBased )
5158 {
5159 QgsDebugMsgLevel( QStringLiteral( "Info: Filter or Min/MaxScaleDenominator element found: need a RuleBasedLabeling" ), 4 );
5160 needRuleBasedLabeling = true;
5161 }
5162 }
5163
5164 // more rules present, use the RuleRenderer
5165 if ( ruleCount > 1 )
5166 {
5167 QgsDebugMsgLevel( QStringLiteral( "Info: More Rule elements found: need a RuleBasedLabeling" ), 4 );
5168 needRuleBasedLabeling = true;
5169 }
5170
5171 // not use the rule based labeling if no rules with textSymbolizer
5172 if ( ruleCount == 0 )
5173 {
5174 needRuleBasedLabeling = false;
5175 }
5176
5177 ruleElem = ruleElem.nextSiblingElement( QStringLiteral( "Rule" ) );
5178 }
5179 featTypeStyleElem = featTypeStyleElem.nextSiblingElement( QStringLiteral( "FeatureTypeStyle" ) );
5180 }
5181
5182 if ( ruleCount == 0 )
5183 {
5184 QgsDebugMsgLevel( QStringLiteral( "Info: No TextSymbolizer element." ), 4 );
5185 return;
5186 }
5187
5188 QDomElement ruleElem = mergedFeatTypeStyle.firstChildElement( QStringLiteral( "Rule" ) );
5189
5190 if ( needRuleBasedLabeling )
5191 {
5192 QgsDebugMsgLevel( QStringLiteral( "Info: rule based labeling" ), 4 );
5193 QgsRuleBasedLabeling::Rule *rootRule = new QgsRuleBasedLabeling::Rule( nullptr );
5194 while ( !ruleElem.isNull() )
5195 {
5196
5197 QString label, description, filterExp;
5198 int scaleMinDenom = 0, scaleMaxDenom = 0;
5199 QgsPalLayerSettings settings;
5200
5201 // retrieve the Rule element child nodes
5202 QDomElement childElem = ruleElem.firstChildElement();
5203 while ( !childElem.isNull() )
5204 {
5205 if ( childElem.localName() == QLatin1String( "Name" ) )
5206 {
5207 // <se:Name> tag contains the rule identifier,
5208 // so prefer title tag for the label property value
5209 if ( label.isEmpty() )
5210 label = childElem.firstChild().nodeValue();
5211 }
5212 else if ( childElem.localName() == QLatin1String( "Description" ) )
5213 {
5214 // <se:Description> can contains a title and an abstract
5215 QDomElement titleElem = childElem.firstChildElement( QStringLiteral( "Title" ) );
5216 if ( !titleElem.isNull() )
5217 {
5218 label = titleElem.firstChild().nodeValue();
5219 }
5220
5221 QDomElement abstractElem = childElem.firstChildElement( QStringLiteral( "Abstract" ) );
5222 if ( !abstractElem.isNull() )
5223 {
5224 description = abstractElem.firstChild().nodeValue();
5225 }
5226 }
5227 else if ( childElem.localName() == QLatin1String( "Abstract" ) )
5228 {
5229 // <sld:Abstract> (v1.0)
5230 description = childElem.firstChild().nodeValue();
5231 }
5232 else if ( childElem.localName() == QLatin1String( "Title" ) )
5233 {
5234 // <sld:Title> (v1.0)
5235 label = childElem.firstChild().nodeValue();
5236 }
5237 else if ( childElem.localName() == QLatin1String( "Filter" ) )
5238 {
5240 if ( filter )
5241 {
5242 if ( filter->hasParserError() )
5243 {
5244 QgsDebugMsgLevel( QStringLiteral( "SLD Filter parsing error: %1" ).arg( filter->parserErrorString() ), 3 );
5245 }
5246 else
5247 {
5248 filterExp = filter->expression();
5249 }
5250 delete filter;
5251 }
5252 }
5253 else if ( childElem.localName() == QLatin1String( "MinScaleDenominator" ) )
5254 {
5255 bool ok;
5256 int v = childElem.firstChild().nodeValue().toInt( &ok );
5257 if ( ok )
5258 scaleMinDenom = v;
5259 }
5260 else if ( childElem.localName() == QLatin1String( "MaxScaleDenominator" ) )
5261 {
5262 bool ok;
5263 int v = childElem.firstChild().nodeValue().toInt( &ok );
5264 if ( ok )
5265 scaleMaxDenom = v;
5266 }
5267 else if ( childElem.localName() == QLatin1String( "TextSymbolizer" ) )
5268 {
5269 readSldTextSymbolizer( childElem, settings );
5270 }
5271
5272 childElem = childElem.nextSiblingElement();
5273 }
5274
5275 QgsRuleBasedLabeling::Rule *ruleLabeling = new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings( settings ), scaleMinDenom, scaleMaxDenom, filterExp, label );
5276 rootRule->appendChild( ruleLabeling );
5277
5278 ruleElem = ruleElem.nextSiblingElement();
5279 }
5280
5281 setLabeling( new QgsRuleBasedLabeling( rootRule ) );
5282 setLabelsEnabled( true );
5283 }
5284 else
5285 {
5286 QgsDebugMsgLevel( QStringLiteral( "Info: simple labeling" ), 4 );
5287 // retrieve the TextSymbolizer element child node
5288 QDomElement textSymbolizerElem = ruleElem.firstChildElement( QStringLiteral( "TextSymbolizer" ) );
5290 if ( readSldTextSymbolizer( textSymbolizerElem, s ) )
5291 {
5293 setLabelsEnabled( true );
5294 }
5295 }
5296}
5297
5298bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSettings &settings ) const
5299{
5301
5302 if ( node.localName() != QLatin1String( "TextSymbolizer" ) )
5303 {
5304 QgsDebugMsgLevel( QStringLiteral( "Not a TextSymbolizer element: %1" ).arg( node.localName() ), 3 );
5305 return false;
5306 }
5307 QDomElement textSymbolizerElem = node.toElement();
5308 // Label
5309 QDomElement labelElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Label" ) );
5310 if ( !labelElem.isNull() )
5311 {
5312 QDomElement propertyNameElem = labelElem.firstChildElement( QStringLiteral( "PropertyName" ) );
5313 if ( !propertyNameElem.isNull() )
5314 {
5315 // set labeling defaults
5316
5317 // label attribute
5318 QString labelAttribute = propertyNameElem.text();
5319 settings.fieldName = labelAttribute;
5320 settings.isExpression = false;
5321
5322 int fieldIndex = mFields.lookupField( labelAttribute );
5323 if ( fieldIndex == -1 )
5324 {
5325 // label attribute is not in columns, check if it is an expression
5326 QgsExpression exp( labelAttribute );
5327 if ( !exp.hasEvalError() )
5328 {
5329 settings.isExpression = true;
5330 }
5331 else
5332 {
5333 QgsDebugMsgLevel( QStringLiteral( "SLD label attribute error: %1" ).arg( exp.evalErrorString() ), 3 );
5334 }
5335 }
5336 }
5337 else
5338 {
5339 QgsDebugMsgLevel( QStringLiteral( "Info: PropertyName element not found." ), 4 );
5340 return false;
5341 }
5342 }
5343 else
5344 {
5345 QgsDebugMsgLevel( QStringLiteral( "Info: Label element not found." ), 4 );
5346 return false;
5347 }
5348
5350 if ( textSymbolizerElem.hasAttribute( QStringLiteral( "uom" ) ) )
5351 {
5352 sldUnitSize = QgsSymbolLayerUtils::decodeSldUom( textSymbolizerElem.attribute( QStringLiteral( "uom" ) ) );
5353 }
5354
5355 QString fontFamily = QStringLiteral( "Sans-Serif" );
5356 int fontPointSize = 10;
5358 int fontWeight = -1;
5359 bool fontItalic = false;
5360 bool fontUnderline = false;
5361
5362 // Font
5363 QDomElement fontElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Font" ) );
5364 if ( !fontElem.isNull() )
5365 {
5366 QgsStringMap fontSvgParams = QgsSymbolLayerUtils::getSvgParameterList( fontElem );
5367 for ( QgsStringMap::iterator it = fontSvgParams.begin(); it != fontSvgParams.end(); ++it )
5368 {
5369 QgsDebugMsgLevel( QStringLiteral( "found fontSvgParams %1: %2" ).arg( it.key(), it.value() ), 4 );
5370
5371 if ( it.key() == QLatin1String( "font-family" ) )
5372 {
5373 fontFamily = it.value();
5374 }
5375 else if ( it.key() == QLatin1String( "font-style" ) )
5376 {
5377 fontItalic = ( it.value() == QLatin1String( "italic" ) ) || ( it.value() == QLatin1String( "Italic" ) );
5378 }
5379 else if ( it.key() == QLatin1String( "font-size" ) )
5380 {
5381 bool ok;
5382 int fontSize = it.value().toInt( &ok );
5383 if ( ok )
5384 {
5385 fontPointSize = fontSize;
5386 fontUnitSize = sldUnitSize;
5387 }
5388 }
5389 else if ( it.key() == QLatin1String( "font-weight" ) )
5390 {
5391 if ( ( it.value() == QLatin1String( "bold" ) ) || ( it.value() == QLatin1String( "Bold" ) ) )
5392 fontWeight = QFont::Bold;
5393 }
5394 else if ( it.key() == QLatin1String( "font-underline" ) )
5395 {
5396 fontUnderline = ( it.value() == QLatin1String( "underline" ) ) || ( it.value() == QLatin1String( "Underline" ) );
5397 }
5398 }
5399 }
5400
5401 QgsTextFormat format;
5402 QFont font( fontFamily, fontPointSize, fontWeight, fontItalic );
5403 font.setUnderline( fontUnderline );
5404 format.setFont( font );
5405 format.setSize( fontPointSize );
5406 format.setSizeUnit( fontUnitSize );
5407
5408 // Fill
5409 QDomElement fillElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Fill" ) );
5410 QColor textColor;
5411 Qt::BrushStyle textBrush = Qt::SolidPattern;
5412 QgsSymbolLayerUtils::fillFromSld( fillElem, textBrush, textColor );
5413 if ( textColor.isValid() )
5414 {
5415 QgsDebugMsgLevel( QStringLiteral( "Info: textColor %1." ).arg( QVariant( textColor ).toString() ), 4 );
5416 format.setColor( textColor );
5417 }
5418
5419 QgsTextBufferSettings bufferSettings;
5420
5421 // Halo
5422 QDomElement haloElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Halo" ) );
5423 if ( !haloElem.isNull() )
5424 {
5425 bufferSettings.setEnabled( true );
5426 bufferSettings.setSize( 1 );
5427
5428 QDomElement radiusElem = haloElem.firstChildElement( QStringLiteral( "Radius" ) );
5429 if ( !radiusElem.isNull() )
5430 {
5431 bool ok;
5432 double bufferSize = radiusElem.text().toDouble( &ok );
5433 if ( ok )
5434 {
5435 bufferSettings.setSize( bufferSize );
5436 bufferSettings.setSizeUnit( sldUnitSize );
5437 }
5438 }
5439
5440 QDomElement haloFillElem = haloElem.firstChildElement( QStringLiteral( "Fill" ) );
5441 QColor bufferColor;
5442 Qt::BrushStyle bufferBrush = Qt::SolidPattern;
5443 QgsSymbolLayerUtils::fillFromSld( haloFillElem, bufferBrush, bufferColor );
5444 if ( bufferColor.isValid() )
5445 {
5446 QgsDebugMsgLevel( QStringLiteral( "Info: bufferColor %1." ).arg( QVariant( bufferColor ).toString() ), 4 );
5447 bufferSettings.setColor( bufferColor );
5448 }
5449 }
5450
5451 // LabelPlacement
5452 QDomElement labelPlacementElem = textSymbolizerElem.firstChildElement( QStringLiteral( "LabelPlacement" ) );
5453 if ( !labelPlacementElem.isNull() )
5454 {
5455 // PointPlacement
5456 QDomElement pointPlacementElem = labelPlacementElem.firstChildElement( QStringLiteral( "PointPlacement" ) );
5457 if ( !pointPlacementElem.isNull() )
5458 {
5461 {
5463 }
5464
5465 QDomElement displacementElem = pointPlacementElem.firstChildElement( QStringLiteral( "Displacement" ) );
5466 if ( !displacementElem.isNull() )
5467 {
5468 QDomElement displacementXElem = displacementElem.firstChildElement( QStringLiteral( "DisplacementX" ) );
5469 if ( !displacementXElem.isNull() )
5470 {
5471 bool ok;
5472 double xOffset = displacementXElem.text().toDouble( &ok );
5473 if ( ok )
5474 {
5475 settings.xOffset = xOffset;
5476 settings.offsetUnits = sldUnitSize;
5477 }
5478 }
5479 QDomElement displacementYElem = displacementElem.firstChildElement( QStringLiteral( "DisplacementY" ) );
5480 if ( !displacementYElem.isNull() )
5481 {
5482 bool ok;
5483 double yOffset = displacementYElem.text().toDouble( &ok );
5484 if ( ok )
5485 {
5486 settings.yOffset = yOffset;
5487 settings.offsetUnits = sldUnitSize;
5488 }
5489 }
5490 }
5491 QDomElement anchorPointElem = pointPlacementElem.firstChildElement( QStringLiteral( "AnchorPoint" ) );
5492 if ( !anchorPointElem.isNull() )
5493 {
5494 QDomElement anchorPointXElem = anchorPointElem.firstChildElement( QStringLiteral( "AnchorPointX" ) );
5495 if ( !anchorPointXElem.isNull() )
5496 {
5497 bool ok;
5498 double xOffset = anchorPointXElem.text().toDouble( &ok );
5499 if ( ok )
5500 {
5501 settings.xOffset = xOffset;
5502 settings.offsetUnits = sldUnitSize;
5503 }
5504 }
5505 QDomElement anchorPointYElem = anchorPointElem.firstChildElement( QStringLiteral( "AnchorPointY" ) );
5506 if ( !anchorPointYElem.isNull() )
5507 {
5508 bool ok;
5509 double yOffset = anchorPointYElem.text().toDouble( &ok );
5510 if ( ok )
5511 {
5512 settings.yOffset = yOffset;
5513 settings.offsetUnits = sldUnitSize;
5514 }
5515 }
5516 }
5517
5518 QDomElement rotationElem = pointPlacementElem.firstChildElement( QStringLiteral( "Rotation" ) );
5519 if ( !rotationElem.isNull() )
5520 {
5521 bool ok;
5522 double rotation = rotationElem.text().toDouble( &ok );
5523 if ( ok )
5524 {
5525 settings.angleOffset = 360 - rotation;
5526 }
5527 }
5528 }
5529 else
5530 {
5531 // PointPlacement
5532 QDomElement linePlacementElem = labelPlacementElem.firstChildElement( QStringLiteral( "LinePlacement" ) );
5533 if ( !linePlacementElem.isNull() )
5534 {
5536 }
5537 }
5538 }
5539
5540 // read vendor options
5541 QgsStringMap vendorOptions;
5542 QDomElement vendorOptionElem = textSymbolizerElem.firstChildElement( QStringLiteral( "VendorOption" ) );
5543 while ( !vendorOptionElem.isNull() && vendorOptionElem.localName() == QLatin1String( "VendorOption" ) )
5544 {
5545 QString optionName = vendorOptionElem.attribute( QStringLiteral( "name" ) );
5546 QString optionValue;
5547 if ( vendorOptionElem.firstChild().nodeType() == QDomNode::TextNode )
5548 {
5549 optionValue = vendorOptionElem.firstChild().nodeValue();
5550 }
5551 else
5552 {
5553 if ( vendorOptionElem.firstChild().nodeType() == QDomNode::ElementNode &&
5554 vendorOptionElem.firstChild().localName() == QLatin1String( "Literal" ) )
5555 {
5556 QgsDebugMsgLevel( vendorOptionElem.firstChild().localName(), 2 );
5557 optionValue = vendorOptionElem.firstChild().firstChild().nodeValue();
5558 }
5559 else
5560 {
5561 QgsDebugError( QStringLiteral( "unexpected child of %1 named %2" ).arg( vendorOptionElem.localName(), optionName ) );
5562 }
5563 }
5564
5565 if ( !optionName.isEmpty() && !optionValue.isEmpty() )
5566 {
5567 vendorOptions[ optionName ] = optionValue;
5568 }
5569
5570 vendorOptionElem = vendorOptionElem.nextSiblingElement();
5571 }
5572 if ( !vendorOptions.isEmpty() )
5573 {
5574 for ( QgsStringMap::iterator it = vendorOptions.begin(); it != vendorOptions.end(); ++it )
5575 {
5576 if ( it.key() == QLatin1String( "underlineText" ) && it.value() == QLatin1String( "true" ) )
5577 {
5578 font.setUnderline( true );
5579 format.setFont( font );
5580 }
5581 else if ( it.key() == QLatin1String( "strikethroughText" ) && it.value() == QLatin1String( "true" ) )
5582 {
5583 font.setStrikeOut( true );
5584 format.setFont( font );
5585 }
5586 else if ( it.key() == QLatin1String( "maxDisplacement" ) )
5587 {
5589 }
5590 else if ( it.key() == QLatin1String( "followLine" ) && it.value() == QLatin1String( "true" ) )
5591 {
5593 {
5595 }
5596 else
5597 {
5599 }
5600 }
5601 else if ( it.key() == QLatin1String( "maxAngleDelta" ) )
5602 {
5603 bool ok;
5604 double angle = it.value().toDouble( &ok );
5605 if ( ok )
5606 {
5607 settings.maxCurvedCharAngleIn = angle;
5608 settings.maxCurvedCharAngleOut = angle;
5609 }
5610 }
5611 // miscellaneous options
5612 else if ( it.key() == QLatin1String( "conflictResolution" ) && it.value() == QLatin1String( "false" ) )
5613 {
5615 }
5616 else if ( it.key() == QLatin1String( "forceLeftToRight" ) && it.value() == QLatin1String( "false" ) )
5617 {
5619 }
5620 else if ( it.key() == QLatin1String( "group" ) && it.value() == QLatin1String( "yes" ) )
5621 {
5622 settings.lineSettings().setMergeLines( true );
5623 }
5624 else if ( it.key() == QLatin1String( "labelAllGroup" ) && it.value() == QLatin1String( "true" ) )
5625 {
5626 settings.lineSettings().setMergeLines( true );
5627 }
5628 }
5629 }
5630
5631 format.setBuffer( bufferSettings );
5632 settings.setFormat( format );
5633 return true;
5634}
5635
5637{
5639
5640 return mEditFormConfig;
5641}
5642
5644{
5646
5647 if ( mEditFormConfig == editFormConfig )
5648 return;
5649
5650 mEditFormConfig = editFormConfig;
5651 mEditFormConfig.onRelationsLoaded();
5652 emit editFormConfigChanged();
5653}
5654
5656{
5658
5659 QgsAttributeTableConfig config = mAttributeTableConfig;
5660
5661 if ( config.isEmpty() )
5662 config.update( fields() );
5663
5664 return config;
5665}
5666
5668{
5670
5671 if ( mAttributeTableConfig != attributeTableConfig )
5672 {
5673 mAttributeTableConfig = attributeTableConfig;
5674 emit configChanged();
5675 }
5676}
5677
5679{
5680 // called in a non-thread-safe way in some cases when calculating aggregates in a different thread
5682
5684}
5685
5692
5694{
5696
5697 if ( !mDiagramLayerSettings )
5698 mDiagramLayerSettings = new QgsDiagramLayerSettings();
5699 *mDiagramLayerSettings = s;
5700}
5701
5703{
5705
5706 QgsLayerMetadataFormatter htmlFormatter( metadata() );
5707 QString myMetadata = QStringLiteral( "<html><head></head>\n<body>\n" );
5708
5709 myMetadata += generalHtmlMetadata();
5710
5711 // Begin Provider section
5712 myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" );
5713 myMetadata += QLatin1String( "<table class=\"list-view\">\n" );
5714
5715 // storage type
5716 if ( !storageType().isEmpty() )
5717 {
5718 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Storage" ) + QStringLiteral( "</td><td>" ) + storageType() + QStringLiteral( "</td></tr>\n" );
5719 }
5720
5721 // comment
5722 if ( !dataComment().isEmpty() )
5723 {
5724 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Comment" ) + QStringLiteral( "</td><td>" ) + dataComment() + QStringLiteral( "</td></tr>\n" );
5725 }
5726
5727 // encoding
5728 if ( const QgsVectorDataProvider *provider = dataProvider() )
5729 {
5730 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Encoding" ) + QStringLiteral( "</td><td>" ) + provider->encoding() + QStringLiteral( "</td></tr>\n" );
5731 myMetadata += provider->htmlMetadata();
5732 }
5733
5734 if ( isSpatial() )
5735 {
5736 // geom type
5738 if ( static_cast<int>( type ) < 0 || static_cast< int >( type ) > static_cast< int >( Qgis::GeometryType::Null ) )
5739 {
5740 QgsDebugMsgLevel( QStringLiteral( "Invalid vector type" ), 2 );
5741 }
5742 else
5743 {
5744 QString typeString( QStringLiteral( "%1 (%2)" ).arg( QgsWkbTypes::geometryDisplayString( geometryType() ),
5746 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Geometry" ) + QStringLiteral( "</td><td>" ) + typeString + QStringLiteral( "</td></tr>\n" );
5747 }
5748
5749 // Extent
5750 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Extent" ) + QStringLiteral( "</td><td>" ) + extent().toString() + QStringLiteral( "</td></tr>\n" );
5751 }
5752
5753 // feature count
5754 QLocale locale = QLocale();
5755 locale.setNumberOptions( locale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
5756 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" )
5757 + tr( "Feature count" ) + QStringLiteral( "</td><td>" )
5758 + ( featureCount() == -1 ? tr( "unknown" ) : locale.toString( static_cast<qlonglong>( featureCount() ) ) )
5759 + QStringLiteral( "</td></tr>\n" );
5760
5761 // End Provider section
5762 myMetadata += QLatin1String( "</table>\n<br><br>" );
5763
5764 if ( isSpatial() )
5765 {
5766 // CRS
5767 myMetadata += crsHtmlMetadata();
5768 }
5769
5770 // identification section
5771 myMetadata += QStringLiteral( "<h1>" ) + tr( "Identification" ) + QStringLiteral( "</h1>\n<hr>\n" );
5772 myMetadata += htmlFormatter.identificationSectionHtml( );
5773 myMetadata += QLatin1String( "<br><br>\n" );
5774
5775 // extent section
5776 myMetadata += QStringLiteral( "<h1>" ) + tr( "Extent" ) + QStringLiteral( "</h1>\n<hr>\n" );
5777 myMetadata += htmlFormatter.extentSectionHtml( isSpatial() );
5778 myMetadata += QLatin1String( "<br><br>\n" );
5779
5780 // Start the Access section
5781 myMetadata += QStringLiteral( "<h1>" ) + tr( "Access" ) + QStringLiteral( "</h1>\n<hr>\n" );
5782 myMetadata += htmlFormatter.accessSectionHtml( );
5783 myMetadata += QLatin1String( "<br><br>\n" );
5784
5785 // Fields section
5786 myMetadata += QStringLiteral( "<h1>" ) + tr( "Fields" ) + QStringLiteral( "</h1>\n<hr>\n<table class=\"list-view\">\n" );
5787
5788 // primary key
5790 if ( !pkAttrList.isEmpty() )
5791 {
5792 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Primary key attributes" ) + QStringLiteral( "</td><td>" );
5793 const auto constPkAttrList = pkAttrList;
5794 for ( int idx : constPkAttrList )
5795 {
5796 myMetadata += fields().at( idx ).name() + ' ';
5797 }
5798 myMetadata += QLatin1String( "</td></tr>\n" );
5799 }
5800
5801 const QgsFields myFields = fields();
5802
5803 // count fields
5804 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Count" ) + QStringLiteral( "</td><td>" ) + QString::number( myFields.size() ) + QStringLiteral( "</td></tr>\n" );
5805
5806 myMetadata += QLatin1String( "</table>\n<br><table width=\"100%\" class=\"tabular-view\">\n" );
5807 myMetadata += QLatin1String( "<tr><th>" ) + tr( "Field" ) + QLatin1String( "</th><th>" ) + tr( "Type" ) + QLatin1String( "</th><th>" ) + tr( "Length" ) + QLatin1String( "</th><th>" ) + tr( "Precision" ) + QLatin1String( "</th><th>" ) + tr( "Comment" ) + QLatin1String( "</th></tr>\n" );
5808
5809 for ( int i = 0; i < myFields.size(); ++i )
5810 {
5811 QgsField myField = myFields.at( i );
5812 QString rowClass;
5813 if ( i % 2 )
5814 rowClass = QStringLiteral( "class=\"odd-row\"" );
5815 myMetadata += QLatin1String( "<tr " ) + rowClass + QLatin1String( "><td>" ) + myField.displayNameWithAlias() + QLatin1String( "</td><td>" ) + myField.typeName() + QLatin1String( "</td><td>" ) + QString::number( myField.length() ) + QLatin1String( "</td><td>" ) + QString::number( myField.precision() ) + QLatin1String( "</td><td>" ) + myField.comment() + QLatin1String( "</td></tr>\n" );
5816 }
5817
5818 //close field list
5819 myMetadata += QLatin1String( "</table>\n<br><br>" );
5820
5821 // Start the contacts section
5822 myMetadata += QStringLiteral( "<h1>" ) + tr( "Contacts" ) + QStringLiteral( "</h1>\n<hr>\n" );
5823 myMetadata += htmlFormatter.contactsSectionHtml( );
5824 myMetadata += QLatin1String( "<br><br>\n" );
5825
5826 // Start the links section
5827 myMetadata += QStringLiteral( "<h1>" ) + tr( "Links" ) + QStringLiteral( "</h1>\n<hr>\n" );
5828 myMetadata += htmlFormatter.linksSectionHtml( );
5829 myMetadata += QLatin1String( "<br><br>\n" );
5830
5831 // Start the history section
5832 myMetadata += QStringLiteral( "<h1>" ) + tr( "History" ) + QStringLiteral( "</h1>\n<hr>\n" );
5833 myMetadata += htmlFormatter.historySectionHtml( );
5834 myMetadata += QLatin1String( "<br><br>\n" );
5835
5836 myMetadata += QLatin1String( "\n</body>\n</html>\n" );
5837 return myMetadata;
5838}
5839
5840void QgsVectorLayer::invalidateSymbolCountedFlag()
5841{
5843
5844 mSymbolFeatureCounted = false;
5845}
5846
5847void QgsVectorLayer::onFeatureCounterCompleted()
5848{
5850
5851 onSymbolsCounted();
5852 mFeatureCounter = nullptr;
5853}
5854
5855void QgsVectorLayer::onFeatureCounterTerminated()
5856{
5858
5859 mFeatureCounter = nullptr;
5860}
5861
5862void QgsVectorLayer::onJoinedFieldsChanged()
5863{
5865
5866 // some of the fields of joined layers have changed -> we need to update this layer's fields too
5867 updateFields();
5868}
5869
5870void QgsVectorLayer::onFeatureDeleted( QgsFeatureId fid )
5871{
5873
5874 if ( mEditCommandActive || mCommitChangesActive )
5875 {
5876 mDeletedFids << fid;
5877 }
5878 else
5879 {
5880 mSelectedFeatureIds.remove( fid );
5881 emit featuresDeleted( QgsFeatureIds() << fid );
5882 }
5883
5884 emit featureDeleted( fid );
5885}
5886
5887void QgsVectorLayer::onRelationsLoaded()
5888{
5890
5891 mEditFormConfig.onRelationsLoaded();
5892}
5893
5894void QgsVectorLayer::onSymbolsCounted()
5895{
5897
5898 if ( mFeatureCounter )
5899 {
5900 mSymbolFeatureCounted = true;
5901 mSymbolFeatureCountMap = mFeatureCounter->symbolFeatureCountMap();
5902 mSymbolFeatureIdMap = mFeatureCounter->symbolFeatureIdMap();
5904 }
5905}
5906
5907QList<QgsRelation> QgsVectorLayer::referencingRelations( int idx ) const
5908{
5910
5912}
5913
5914QList<QgsWeakRelation> QgsVectorLayer::weakRelations() const
5915{
5917
5918 return mWeakRelations;
5919}
5920
5921void QgsVectorLayer::setWeakRelations( const QList<QgsWeakRelation> &relations )
5922{
5924
5925 mWeakRelations = relations;
5926}
5927
5928bool QgsVectorLayer::loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage, const QString &key )
5929{
5931
5932 bool rc = false;
5933
5934 QString joinKey = mAuxiliaryLayerKey;
5935 if ( !key.isEmpty() )
5936 joinKey = key;
5937
5938 if ( storage.isValid() && !joinKey.isEmpty() )
5939 {
5940 QgsAuxiliaryLayer *alayer = nullptr;
5941
5942 int idx = fields().lookupField( joinKey );
5943
5944 if ( idx >= 0 )
5945 {
5946 alayer = storage.createAuxiliaryLayer( fields().field( idx ), this );
5947
5948 if ( alayer )
5949 {
5950 setAuxiliaryLayer( alayer );
5951 rc = true;
5952 }
5953 }
5954 }
5955
5956 return rc;
5957}
5958
5960{
5962
5963 mAuxiliaryLayerKey.clear();
5964
5965 if ( mAuxiliaryLayer )
5966 removeJoin( mAuxiliaryLayer->id() );
5967
5968 if ( alayer )
5969 {
5970 addJoin( alayer->joinInfo() );
5971
5972 if ( !alayer->isEditable() )
5973 alayer->startEditing();
5974
5975 mAuxiliaryLayerKey = alayer->joinInfo().targetFieldName();
5976 }
5977
5978 mAuxiliaryLayer.reset( alayer );
5979 if ( mAuxiliaryLayer )
5980 mAuxiliaryLayer->setParent( this );
5981 updateFields();
5982}
5983
5985{
5987
5988 return mAuxiliaryLayer.get();
5989}
5990
5992{
5994
5995 return mAuxiliaryLayer.get();
5996}
5997
5998QSet<QgsMapLayerDependency> QgsVectorLayer::dependencies() const
5999{
6001
6002 if ( mDataProvider )
6003 return mDataProvider->dependencies() + mDependencies;
6004 return mDependencies;
6005}
6006
6007void QgsVectorLayer::emitDataChanged()
6008{
6010
6011 if ( mDataChangedFired )
6012 return;
6013
6014 updateExtents(); // reset cached extent to reflect data changes
6015
6016 mDataChangedFired = true;
6017 emit dataChanged();
6018 mDataChangedFired = false;
6019}
6020
6021void QgsVectorLayer::onAfterCommitChangesDependency()
6022{
6024
6025 mDataChangedFired = true;
6026 reload();
6027 mDataChangedFired = false;
6028}
6029
6030bool QgsVectorLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
6031{
6033
6034 QSet<QgsMapLayerDependency> deps;
6035 const auto constODeps = oDeps;
6036 for ( const QgsMapLayerDependency &dep : constODeps )
6037 {
6038 if ( dep.origin() == QgsMapLayerDependency::FromUser )
6039 deps << dep;
6040 }
6041
6042 QSet<QgsMapLayerDependency> toAdd = deps - dependencies();
6043
6044 // disconnect layers that are not present in the list of dependencies anymore
6045 for ( const QgsMapLayerDependency &dep : std::as_const( mDependencies ) )
6046 {
6047 QgsVectorLayer *lyr = static_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( dep.layerId() ) );
6048 if ( !lyr )
6049 continue;
6050 disconnect( lyr, &QgsVectorLayer::featureAdded, this, &QgsVectorLayer::emitDataChanged );
6051 disconnect( lyr, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayer::emitDataChanged );
6052 disconnect( lyr, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayer::emitDataChanged );
6053 disconnect( lyr, &QgsVectorLayer::dataChanged, this, &QgsVectorLayer::emitDataChanged );
6055 disconnect( lyr, &QgsVectorLayer::afterCommitChanges, this, &QgsVectorLayer::onAfterCommitChangesDependency );
6056 }
6057
6058 // assign new dependencies
6059 if ( mDataProvider )
6060 mDependencies = mDataProvider->dependencies() + deps;
6061 else
6062 mDependencies = deps;
6063 emit dependenciesChanged();
6064
6065 // connect to new layers
6066 for ( const QgsMapLayerDependency &dep : std::as_const( mDependencies ) )
6067 {
6068 QgsVectorLayer *lyr = static_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( dep.layerId() ) );
6069 if ( !lyr )
6070 continue;
6071 connect( lyr, &QgsVectorLayer::featureAdded, this, &QgsVectorLayer::emitDataChanged );
6072 connect( lyr, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayer::emitDataChanged );
6073 connect( lyr, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayer::emitDataChanged );
6074 connect( lyr, &QgsVectorLayer::dataChanged, this, &QgsVectorLayer::emitDataChanged );
6076 connect( lyr, &QgsVectorLayer::afterCommitChanges, this, &QgsVectorLayer::onAfterCommitChangesDependency );
6077 }
6078
6079 // if new layers are present, emit a data change
6080 if ( ! toAdd.isEmpty() )
6081 emitDataChanged();
6082
6083 return true;
6084}
6085
6086QgsFieldConstraints::Constraints QgsVectorLayer::fieldConstraints( int fieldIndex ) const
6087{
6089
6090 if ( fieldIndex < 0 || fieldIndex >= mFields.count() || !mDataProvider )
6091 return QgsFieldConstraints::Constraints();
6092
6093 QgsFieldConstraints::Constraints constraints = mFields.at( fieldIndex ).constraints().constraints();
6094
6095 // make sure provider constraints are always present!
6096 if ( mFields.fieldOrigin( fieldIndex ) == QgsFields::OriginProvider )
6097 {
6098 constraints |= mDataProvider->fieldConstraints( mFields.fieldOriginIndex( fieldIndex ) );
6099 }
6100
6101 return constraints;
6102}
6103
6104QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength> QgsVectorLayer::fieldConstraintsAndStrength( int fieldIndex ) const
6105{
6107
6108 QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength > m;
6109
6110 if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
6111 return m;
6112
6113 QString name = mFields.at( fieldIndex ).name();
6114
6115 QMap< QPair< QString, QgsFieldConstraints::Constraint >, QgsFieldConstraints::ConstraintStrength >::const_iterator conIt = mFieldConstraintStrength.constBegin();
6116 for ( ; conIt != mFieldConstraintStrength.constEnd(); ++conIt )
6117 {
6118 if ( conIt.key().first == name )
6119 {
6120 m[ conIt.key().second ] = mFieldConstraintStrength.value( conIt.key() );
6121 }
6122 }
6123
6124 return m;
6125}
6126
6128{
6130
6131 if ( index < 0 || index >= mFields.count() )
6132 return;
6133
6134 QString name = mFields.at( index ).name();
6135
6136 // add constraint to existing constraints
6137 QgsFieldConstraints::Constraints constraints = mFieldConstraints.value( name, QgsFieldConstraints::Constraints() );
6138 constraints |= constraint;
6139 mFieldConstraints.insert( name, constraints );
6140
6141 mFieldConstraintStrength.insert( qMakePair( name, constraint ), strength );
6142
6143 updateFields();
6144}
6145
6147{
6149
6150 if ( index < 0 || index >= mFields.count() )
6151 return;
6152
6153 QString name = mFields.at( index ).name();
6154
6155 // remove constraint from existing constraints
6156 QgsFieldConstraints::Constraints constraints = mFieldConstraints.value( name, QgsFieldConstraints::Constraints() );
6157 constraints &= ~constraint;
6158 mFieldConstraints.insert( name, constraints );
6159
6160 mFieldConstraintStrength.remove( qMakePair( name, constraint ) );
6161
6162 updateFields();
6163}
6164
6166{
6168
6169 if ( index < 0 || index >= mFields.count() )
6170 return QString();
6171
6172 return mFields.at( index ).constraints().constraintExpression();
6173}
6174
6176{
6178
6179 if ( index < 0 || index >= mFields.count() )
6180 return QString();
6181
6182 return mFields.at( index ).constraints().constraintDescription();
6183}
6184
6185void QgsVectorLayer::setConstraintExpression( int index, const QString &expression, const QString &description )
6186{
6188
6189 if ( index < 0 || index >= mFields.count() )
6190 return;
6191
6192 if ( expression.isEmpty() )
6193 {
6194 mFieldConstraintExpressions.remove( mFields.at( index ).name() );
6195 }
6196 else
6197 {
6198 mFieldConstraintExpressions.insert( mFields.at( index ).name(), qMakePair( expression, description ) );
6199 }
6200 updateFields();
6201}
6202
6203void QgsVectorLayer::setFieldConfigurationFlags( int index, Qgis::FieldConfigurationFlags flags )
6204{
6206
6207 if ( index < 0 || index >= mFields.count() )
6208 return;
6209
6210 mFieldConfigurationFlags.insert( mFields.at( index ).name(), flags );
6211 updateFields();
6212}
6213
6215{
6217
6218 if ( index < 0 || index >= mFields.count() )
6219 return;
6220 Qgis::FieldConfigurationFlags flags = mFields.at( index ).configurationFlags();
6221 flags.setFlag( flag, active );
6223}
6224
6225Qgis::FieldConfigurationFlags QgsVectorLayer::fieldConfigurationFlags( int index ) const
6226{
6228
6229 if ( index < 0 || index >= mFields.count() )
6231
6232 return mFields.at( index ).configurationFlags();
6233}
6234
6236{
6238
6239 if ( index < 0 || index >= mFields.count() )
6240 return;
6241
6242 if ( setup.isNull() )
6243 mFieldWidgetSetups.remove( mFields.at( index ).name() );
6244 else
6245 mFieldWidgetSetups.insert( mFields.at( index ).name(), setup );
6246 updateFields();
6247}
6248
6250{
6252
6253 if ( index < 0 || index >= mFields.count() )
6254 return QgsEditorWidgetSetup();
6255
6256 return mFields.at( index ).editorWidgetSetup();
6257}
6258
6259QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties()
6260{
6262
6264 if ( customProperty( QStringLiteral( "labeling" ) ).toString() == QLatin1String( "pal" ) )
6265 {
6266 if ( customProperty( QStringLiteral( "labeling/enabled" ), QVariant( false ) ).toBool() )
6267 {
6268 // try to load from custom properties
6269 QgsPalLayerSettings settings;
6270 settings.readFromLayerCustomProperties( this );
6271 labeling = new QgsVectorLayerSimpleLabeling( settings );
6272 }
6273
6274 // also clear old-style labeling config
6275 removeCustomProperty( QStringLiteral( "labeling" ) );
6276 const auto constCustomPropertyKeys = customPropertyKeys();
6277 for ( const QString &key : constCustomPropertyKeys )
6278 {
6279 if ( key.startsWith( QLatin1String( "labeling/" ) ) )
6280 removeCustomProperty( key );
6281 }
6282 }
6283
6284 return labeling;
6285}
6286
6288{
6290
6291 return mAllowCommit;
6292}
6293
6294void QgsVectorLayer::setAllowCommit( bool allowCommit )
6295{
6297
6298 if ( mAllowCommit == allowCommit )
6299 return;
6300
6301 mAllowCommit = allowCommit;
6302 emit allowCommitChanged();
6303}
6304
6306{
6308
6309 return mGeometryOptions.get();
6310}
6311
6312void QgsVectorLayer::setReadExtentFromXml( bool readExtentFromXml )
6313{
6315
6316 mReadExtentFromXml = readExtentFromXml;
6317}
6318
6320{
6322
6323 return mReadExtentFromXml;
6324}
6325
6326void QgsVectorLayer::onDirtyTransaction( const QString &sql, const QString &name )
6327{
6329
6331 if ( tr && mEditBuffer )
6332 {
6333 qobject_cast<QgsVectorLayerEditPassthrough *>( mEditBuffer )->update( tr, sql, name );
6334 }
6335}
6336
6337QList<QgsVectorLayer *> QgsVectorLayer::DeleteContext::handledLayers( bool includeAuxiliaryLayers ) const
6338{
6339 QList<QgsVectorLayer *> layers;
6340 QMap<QgsVectorLayer *, QgsFeatureIds>::const_iterator i;
6341 for ( i = mHandledFeatures.begin(); i != mHandledFeatures.end(); ++i )
6342 {
6343 if ( includeAuxiliaryLayers || !qobject_cast< QgsAuxiliaryLayer * >( i.key() ) )
6344 layers.append( i.key() );
6345 }
6346 return layers;
6347}
6348
6350{
6351 return mHandledFeatures[layer];
6352}
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
@ Composition
Fix relation, related elements are part of the parent and a parent copy will copy any children or del...
@ Association
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
GeometryOperationResult
Success or failure of a geometry operation.
Definition qgis.h:1596
@ InvalidInputGeometryType
The input geometry (ring, part, split line, etc.) has not the correct geometry type.
@ Success
Operation succeeded.
@ SelectionIsEmpty
No features were selected.
@ AddRingNotInExistingFeature
The input ring doesn't have any existing ring to fit into.
@ AddRingNotClosed
The input ring is not closed.
@ SelectionIsGreaterThanOne
More than one features were selected.
@ LayerNotEditable
Cannot edit layer.
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition qgis.h:349
@ Unknown
Spatial index presence cannot be determined, index may or may not exist.
@ OverPoint
Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point....
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
@ AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygo...
@ Line
Arranges candidates parallel to a generalised line representing the feature or parallel to a polygon'...
@ Horizontal
Arranges horizontal candidates scattered throughout a polygon feature. Applies to polygon layers only...
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
@ ExactIntersect
Use exact geometry intersection (slower) instead of bounding boxes.
@ SubsetOfAttributes
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
@ BufferedGroups
Buffered transactional editing means that all editable layers in the buffered transaction group are t...
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:3160
@ Duplicate
Duplicate original value.
BlendMode
Blending modes defining the available composition modes that can be used when painting.
Definition qgis.h:3979
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:255
@ Polygon
Polygons.
@ Unknown
Unknown types.
@ Null
No geometry.
@ Generated
A generated relation is a child of a polymorphic relation.
@ Normal
A normal relation.
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition qgis.h:4822
FeatureAvailability
Possible return value for QgsFeatureSource::hasFeatures() to determine if a source is empty.
Definition qgis.h:368
@ FeaturesMaybeAvailable
There may be features available in this source.
@ FeaturesAvailable
There is at least one feature available in this source.
@ NoFeaturesAvailable
There are certainly no features available in this source.
@ Vector
Vector layer.
RenderUnit
Rendering size units.
Definition qgis.h:4193
@ Points
Points (e.g., for font sizes)
Aggregate
Available aggregates to calculate.
Definition qgis.h:4653
VertexMarkerType
Editing vertex markers, used for showing vertices during a edit operation.
Definition qgis.h:1401
@ SemiTransparentCircle
Semi-transparent circle marker.
@ Cross
Cross marker.
VectorEditResult
Flags which control feature selection behavior.
Definition qgis.h:1386
@ Success
Edit operation was successful.
@ InvalidLayer
Edit failed due to invalid layer.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:182
@ Unknown
Unknown.
FieldConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1289
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
@ NoFlag
No flag is defined.
@ HideFromWms
Field is not available if layer is served as WMS from QGIS server.
@ AllowOverlapIfRequired
Avoids overlapping labels when possible, but permit overlaps if labels for features cannot otherwise ...
@ AlwaysAllowUpsideDown
Show upside down for all labels, including dynamic ones.
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1339
@ SetSelection
Set selection, removing any existing selection.
@ AddToSelection
Add selection to current selection.
@ IntersectSelection
Modify current selection to include only select features which match.
@ RemoveFromSelection
Remove from current selection.
Abstract base class for objects which generate elevation profiles.
virtual bool writeXml(QDomElement &collectionElem, const QgsPropertiesDefinition &definitions) const
Writes the current state of the property collection into an XML element.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the labeling...
virtual void toSld(QDomNode &parent, const QVariantMap &props) const
Writes the SE 1.1 TextSymbolizer element based on the current layer labeling settings.
static QgsAbstractVectorLayerLabeling * create(const QDomElement &element, const QgsReadWriteContext &context)
Try to create instance of an implementation based on the XML data.
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const =0
Returns labeling configuration as XML element.
Storage and management of actions associated with a layer.
bool writeXml(QDomNode &layer_node) const
Writes the actions out in XML format.
QList< QgsAction > actions(const QString &actionScope=QString()) const
Returns a list of actions that are available in the given action scope.
QUuid addAction(Qgis::AttributeActionType type, const QString &name, const QString &command, bool capture=false)
Add an action with the given name and action details.
bool readXml(const QDomNode &layer_node)
Reads the actions in in XML format.
Utility class that encapsulates an action based on vector attributes.
Definition qgsaction.h:37
Utility class for calculating aggregates for a field (or expression) over the features from a vector ...
static QgsRuntimeProfiler * profiler()
Returns the application runtime profiler.
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
This is a container for configuration of the attribute table.
void readXml(const QDomNode &node)
Deserialize to XML on layer load.
void update(const QgsFields &fields)
Update the configuration with the given fields.
void writeXml(QDomNode &node) const
Serialize to XML on layer save.
A vector of attributes.
Class allowing to manage the auxiliary storage for a vector layer.
QgsVectorLayerJoinInfo joinInfo() const
Returns information to use for joining with primary key and so on.
Class providing some utility methods to manage auxiliary storage.
QgsAuxiliaryLayer * createAuxiliaryLayer(const QgsField &field, QgsVectorLayer *layer) const
Creates an auxiliary layer for a vector layer.
bool isValid() const
Returns the status of the auxiliary storage currently defined.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
bool isNull() const
Test if the box is null (holding no spatial information).
Definition qgsbox3d.cpp:289
The QgsConditionalLayerStyles class holds conditional style information for a layer.
bool readXml(const QDomNode &node, const QgsReadWriteContext &context)
Reads the condition styles state from a DOM node.
bool writeXml(QDomNode &node, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes the condition styles state to a DOM node.
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Contains information about the context in which a coordinate transform is executed.
Abstract base class for curved geometry type.
Definition qgscurve.h:35
virtual bool isClosed() const
Returns true if the curve is closed.
Definition qgscurve.cpp:53
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
virtual bool containsElevationData() const
Returns true if the data provider definitely contains elevation related data.
virtual bool leaveUpdateMode()
Leave update mode.
@ FlagLoadDefaultStyle
Reset the layer's style to the default for the datasource.
@ ForceReadOnly
Open layer in a read-only mode (since QGIS 3.28)
QgsCoordinateTransformContext transformContext() const
Returns data provider coordinate transform context.
virtual QgsCoordinateReferenceSystem crs() const =0
Returns the coordinate system for the data source.
void dataChanged()
Emitted whenever a change is made to the data provider which may have caused changes in the provider'...
void fullExtentCalculated()
Emitted whenever a deferred extent calculation is completed by the provider.
virtual Qgis::ProviderStyleStorageCapabilities styleStorageCapabilities() const
Returns the style storage capabilities.
virtual QgsBox3D extent3D() const
Returns the 3D extent of the layer.
virtual QString subsetString() const
Returns the subset definition string (typically sql) currently in use by the layer and used by the pr...
virtual QgsLayerMetadata layerMetadata() const
Returns layer metadata collected from the provider's source.
virtual bool isValid() const =0
Returns true if this is a valid layer.
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
virtual void updateExtents()
Update the extents of the layer.
virtual bool setSubsetString(const QString &subset, bool updateFeatureCount=true)
Set the subset string used to create a subset of features in the layer.
virtual void reloadData()
Reloads the data from the source for providers with data caches to synchronize, changes in the data s...
virtual bool enterUpdateMode()
Enter update mode.
virtual QgsRectangle extent() const =0
Returns the extent of the layer.
virtual void setTransformContext(const QgsCoordinateTransformContext &transformContext)
Sets data coordinate transform context to transformContext.
Class for storing the component parts of a RDBMS data source URI (e.g.
bool useEstimatedMetadata() const
Returns true if estimated metadata should be used for the connection.
The QgsDefaultValue class provides a container for managing client side default values for fields.
bool isValid() const
Returns if this default value should be applied.
Stores the settings for rendering of all diagrams for a layer.
@ PositionX
X-coordinate data defined diagram position.
@ PositionY
Y-coordinate data defined diagram position.
@ Show
Whether to show the diagram.
void readXml(const QDomElement &elem)
Reads the diagram settings from a DOM element.
void writeXml(QDomElement &layerElem, QDomDocument &doc) const
Writes the diagram settings to a DOM element.
Evaluates and returns the diagram settings relating to a diagram for a specific feature.
virtual void writeXml(QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context) const =0
Writes diagram state to a DOM element.
virtual QList< QgsDiagramSettings > diagramSettings() const =0
Returns list with all diagram settings in the renderer.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)=0
Reads diagram state from a DOM element.
Contains configuration settings for an editor form.
void readXml(const QDomNode &node, QgsReadWriteContext &context)
Read XML information Deserialize on project load.
void writeXml(QDomNode &node, const QgsReadWriteContext &context) const
Write XML information Serialize on project save.
Holder for the widget type and its configuration for a field.
QVariantMap config() const
void clear()
Clear error messages.
Definition qgserror.h:127
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Buffers information about expression fields for a vector layer.
void removeExpression(int index)
Remove an expression from the buffer.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves expressions to xml under the layer node.
void readXml(const QDomNode &layer_node)
Reads expressions from project file.
void updateFields(QgsFields &flds) const
Adds fields with the expressions buffered in this object to a QgsFields object.
void addExpression(const QString &exp, const QgsField &fld)
Add an expression to the buffer.
QList< QgsExpressionFieldBuffer::ExpressionField > expressions() const
void updateExpression(int index, const QString &exp)
Changes the expression at a given index.
void renameExpression(int index, const QString &name)
Renames an expression field at a given index.
An expression node which takes it value from a feature's field.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
QString expression() const
Returns the original, unmodified expression string.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QString evalErrorString() const
Returns evaluation error.
QString parserErrorString() const
Returns parser error.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
static int expressionToLayerFieldIndex(const QString &expression, const QgsVectorLayer *layer)
Attempts to resolve an expression to a field index from the given layer.
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
QVariant evaluate()
Evaluate the feature and return the result.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
bool close()
Call to end the iteration.
An interface for objects which generate feature renderers for vector layers.
static QgsFeatureRenderer * defaultRenderer(Qgis::GeometryType geomType)
Returns a new renderer - used by default in vector layers.
virtual void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
virtual QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context)
Stores renderer properties to an XML element.
double referenceScale() const
Returns the symbology reference scale.
void setReferenceScale(double scale)
Sets the symbology reference scale.
static QgsFeatureRenderer * load(QDomElement &symbologyElem, const QgsReadWriteContext &context)
create a renderer from XML element
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
static QgsFeatureRenderer * loadSld(const QDomNode &node, Qgis::GeometryType geomType, QString &errorMessage)
Create a new renderer according to the information contained in the UserStyle element of a SLD style ...
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Sets the feature ID that should be fetched.
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
virtual QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const
Returns the set of unique values contained within the specified fieldIndex from this source.
virtual Qgis::SpatialIndexPresence hasSpatialIndex() const
Returns an enum value representing the presence of a valid spatial index on the source,...
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
QgsAttributes attributes
Definition qgsfeature.h:65
QgsFields fields
Definition qgsfeature.h:66
QgsFeatureId id
Definition qgsfeature.h:64
QgsGeometry geometry
Definition qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isValid() const
Returns the validity of this feature.
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
Stores information about constraints which may be present on a field.
ConstraintStrength
Strength of constraints.
void setConstraintStrength(Constraint constraint, ConstraintStrength strength)
Sets the strength of a constraint.
void setConstraintExpression(const QString &expression, const QString &description=QString())
Set the constraint expression for the field.
@ ConstraintOriginProvider
Constraint was set at data provider.
@ ConstraintOriginLayer
Constraint was set by layer.
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
QString constraintExpression() const
Returns the constraint expression for the field, if set.
Constraint
Constraints which may be present on a field.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
@ ConstraintExpression
Field has an expression constraint set. See constraintExpression().
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:150
QString name
Definition qgsfield.h:62
int precision
Definition qgsfield.h:59
int length
Definition qgsfield.h:58
QString displayNameWithAlias() const
Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined...
Definition qgsfield.cpp:96
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:88
Qgis::FieldConfigurationFlags configurationFlags
Definition qgsfield.h:66
QString alias
Definition qgsfield.h:63
QgsDefaultValue defaultValueDefinition
Definition qgsfield.h:64
QString comment
Definition qgsfield.h:61
QgsFieldConstraints constraints
Definition qgsfield.h:65
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition qgsfield.cpp:714
Container of fields for a vector layer.
Definition qgsfields.h:45
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
@ OriginExpression
Field is calculated from an expression.
Definition qgsfields.h:54
@ OriginEdit
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition qgsfields.h:53
@ OriginUnknown
It has not been specified where the field comes from.
Definition qgsfields.h:50
@ OriginJoin
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition qgsfields.h:52
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition qgsfields.h:51
int count() const
Returns number of items.
FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
bool isEmpty() const
Checks whether the container is empty.
int size() const
Returns number of items.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QStringList names() const
Returns a list with field names.
The QgsGeometryOptions class contains options to automatically adjust geometries to constraints on a ...
A geometry is the spatial representation of a feature.
QgsBox3D boundingBox3D() const
Returns the 3D bounding box of the geometry.
bool equals(const QgsGeometry &geometry) const
Test if this geometry is exactly equal to another geometry.
Qgis::GeometryType type
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
void setMergeLines(bool merge)
Sets whether connected line features with identical label text should be merged prior to generating l...
void setOverlapHandling(Qgis::LabelOverlapHandling handling)
Sets the technique used to handle overlapping labels.
Class for metadata formatter.
A structured metadata store for a map layer.
void combine(const QgsAbstractMetadataBase *other) override
Combines the metadata from this object with the metadata from an other object.
Line string geometry type, with support for z-dimension and m-values.
static void warning(const QString &msg)
Goes to qWarning.
This class models dependencies with or between map layers.
Base class for storage of map layer elevation properties.
static QString typeToString(Qgis::LayerType type)
Converts a map layer type to a string value.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from a DOM element previously written by writeXml()
virtual QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Writes configuration to a DOM element, to be used later with readXml()
static QgsMapLayerLegend * defaultVectorLegend(QgsVectorLayer *vl)
Create new legend implementation for vector layer.
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Base class for storage of map layer selection properties.
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer.
Base class for storage of map layer temporal properties.
Base class for all map layer types.
Definition qgsmaplayer.h:75
QString name
Definition qgsmaplayer.h:78
void readStyleManager(const QDomNode &layerNode)
Read style manager's configuration (if any). To be called by subclasses.
void dependenciesChanged()
Emitted when dependencies are 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.
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
void recalculateExtents() const
This is used to send a request that any mapcanvas using this layer update its extents.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QString source() const
Returns the source for the layer.
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
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.
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
void configChanged()
Emitted whenever the configuration is changed.
void setMinimumScale(double scale)
Sets the minimum map scale (i.e.
QgsMapLayer::LayerFlags flags() const
Returns the flags for this layer.
void editingStarted()
Emitted when editing on this layer has started.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:81
friend class QgsVectorLayer
void writeCustomProperties(QDomNode &layerNode, QDomDocument &doc) const
Write custom properties to project file.
virtual int listStylesInDatabase(QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError)
Lists all the style in db split into related to the layer and not related to.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
virtual QString loadDefaultStyle(bool &resultFlag)
Retrieve the default style for this layer if one exists (either as a .qml file on disk or as a record...
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
QString crsHtmlMetadata() const
Returns a HTML fragment containing the layer's CRS metadata, for use in the htmlMetadata() method.
void setMaximumScale(double scale)
Sets the maximum map scale (i.e.
QgsLayerMetadata metadata
Definition qgsmaplayer.h:80
Qgis::LayerType type
Definition qgsmaplayer.h:82
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
void setFlags(QgsMapLayer::LayerFlags flags)
Returns the flags for this layer.
QString publicSource(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
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 void setMetadata(const QgsLayerMetadata &metadata)
Sets the layer's metadata store.
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.
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.
void rendererChanged()
Signal emitted when renderer is changed.
virtual QgsError error() const
Gets current status error.
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.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
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.
void dataChanged()
Data of layer changed.
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
virtual QgsBox3D extent3D() const
Returns the 3D extent of the layer.
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
void setName(const QString &name)
Set the display name of the layer.
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 mDataSource
Data source description string, varies by layer type.
void setMapTipsEnabled(bool enabled)
Enable or disable map tips for this layer.
@ FlagReadExtentFromXml
Read extent from xml and skip get extent from provider.
@ FlagForceReadOnly
Force open as read only.
@ FlagDontResolveLayers
Don't resolve layer paths or create data providers for layers.
void setValid(bool valid)
Sets whether layer is valid or not.
void readCommonStyle(const QDomElement &layerElement, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read style data common to all layer types.
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.
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
void setMapTipTemplate(const QString &mapTipTemplate)
The mapTip is a pretty, html representation for feature information.
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.
bool mapTipsEnabled
Definition qgsmaplayer.h:86
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
double opacity
Definition qgsmaplayer.h:84
bool mValid
Indicates if the layer is valid and can be drawn.
@ GeometryOptions
Geometry validation configuration.
@ AttributeTable
Attribute table settings: choice and order of columns, conditional styling.
@ LayerConfiguration
General configuration: identifiable, removable, searchable, display expression, read-only.
@ Symbology
Symbology.
@ MapTips
Map tips.
@ Rendering
Rendering: scale visibility, simplify method, opacity.
@ Relations
Relations.
@ CustomProperties
Custom properties (by plugins for instance)
@ Actions
Actions.
@ Forms
Feature form.
@ Fields
Aliases, widgets, WMS/WFS, expressions, constraints, virtual fields.
@ Legend
Legend settings (since QGIS 3.16)
@ Diagrams
Diagrams.
@ Labeling
Labeling.
void layerModified()
Emitted when modifications has been done on layer.
void setProviderType(const QString &providerType)
Sets the providerType (provider key)
QString generalHtmlMetadata() const
Returns an HTML fragment containing general metadata information, for use in the htmlMetadata() metho...
void writeCommonStyle(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write style data common to all layer types.
double maximumScale() const
Returns the maximum map scale (i.e.
QString mapTipTemplate
Definition qgsmaplayer.h:85
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).
static QgsExpression * expressionFromOgcFilter(const QDomElement &element, QgsVectorLayer *layer=nullptr)
Parse XML with OGC filter into QGIS expression.
static Qgis::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a Qgis::BlendMode corresponding to a QPainter::CompositionMode.
static QPainter::CompositionMode getCompositionMode(Qgis::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a Qgis::BlendMode.
Contains settings for how a map layer will be labeled.
double yOffset
Vertical offset of label.
const QgsLabelPlacementSettings & placementSettings() const
Returns the label placement settings.
double maxCurvedCharAngleIn
Maximum angle between inside curved label characters (valid range 20.0 to 60.0).
void setFormat(const QgsTextFormat &format)
Sets the label text formatting settings, e.g., font settings, buffer settings, etc.
double xOffset
Horizontal offset of label.
Qgis::LabelPlacement placement
Label placement mode.
double angleOffset
Label rotation, in degrees clockwise.
double maxCurvedCharAngleOut
Maximum angle between outside curved label characters (valid range -20.0 to -95.0)
Qgis::RenderUnit offsetUnits
Units for offsets of label.
bool isExpression
true if this label is made from a expression string, e.g., FieldName || 'mm'
const QgsLabelLineSettings & lineSettings() const
Returns the label line settings, which contain settings related to how the label engine places and fo...
Qgis::UpsideDownLabelHandling upsidedownLabels
Controls whether upside down labels are displayed and how they are handled.
QString fieldName
Name of field (or an expression) to use for label text.
Resolves relative paths into absolute paths and vice versa.
A class to represent a 2D point.
Definition qgspointxy.h:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
Encapsulates properties and constraints relating to fetching elevation profiles from different source...
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.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
QgsRelationManager * relationManager
Definition qgsproject.h:117
bool commitChanges(QStringList &commitErrors, bool stopEditing=true, QgsVectorLayer *vectorLayer=nullptr)
Attempts to commit to the underlying data provider any buffered changes made since the last to call t...
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
bool rollBack(QStringList &rollbackErrors, bool stopEditing=true, QgsVectorLayer *vectorLayer=nullptr)
Stops a current editing operation on vectorLayer and discards any uncommitted edits.
QgsPathResolver pathResolver() const
Returns path resolver object with considering whether the project uses absolute or relative paths and...
bool startEditing(QgsVectorLayer *vectorLayer=nullptr)
Makes the layer editable.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
Definition for a property.
Definition qgsproperty.h:45
@ Double
Double value (including negative values)
Definition qgsproperty.h:55
@ Boolean
Boolean value.
Definition qgsproperty.h:51
static QgsProperty fromField(const QString &fieldName, bool isActive=true)
Returns a new FieldBasedProperty created from the specified field name.
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.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
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.
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.
QgsCoordinateTransformContext transformContext() const
Returns data provider coordinate transform context.
A rectangle specified with double values.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double width() const
Returns the width of the rectangle.
double xMaximum() const
Returns the x maximum value (right side of rectangle).
bool isNull() const
Test if the rectangle is null (holding no spatial information).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
void set(const QgsPointXY &p1, const QgsPointXY &p2, bool normalize=true)
Sets the rectangle from two QgsPoints.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
void normalize()
Normalize the rectangle so it has non-negative width/height.
double height() const
Returns the height of the rectangle.
void setNull()
Mark a rectangle as being null (holding no spatial information).
QList< QgsRelation > referencedRelations(const QgsVectorLayer *layer=nullptr) const
Gets all relations where this layer is the referenced part (i.e.
QList< QgsRelation > referencingRelations(const QgsVectorLayer *layer=nullptr, int fieldIdx=-2) const
Gets all relations where the specified layer (and field) is the referencing part (i....
void relationsLoaded()
Emitted when the relations were loaded after reading a project.
Contains information about the context of a rendering operation.
double rendererScale() const
Returns the renderer map scale.
bool useRenderingOptimization() const
Returns true if the rendering optimization (geometry simplification) can be executed.
A child rule for QgsRuleBasedLabeling.
void appendChild(QgsRuleBasedLabeling::Rule *rule)
add child rule, take ownership, sets this as parent
Rule based labeling for a vector layer.
A boolean settings entry.
A double settings entry.
A template class for enum and flag settings entry.
static QgsSettingsTreeNode * sTreeQgis
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
Renders the diagrams for all features with the same settings.
Manages stored expressions regarding creation, modification and storing in the project.
bool writeXml(QDomNode &layerNode) const
Writes the stored expressions out in XML format.
bool readXml(const QDomNode &layerNode)
Reads the stored expressions in in XML format.
An interface for classes which can visit style entity (e.g.
static double rendererFrameRate(const QgsFeatureRenderer *renderer)
Calculates the frame rate (in frames per second) at which the given renderer must be redrawn.
static QgsStringMap getSvgParameterList(QDomElement &element)
static void mergeScaleDependencies(double mScaleMinDenom, double mScaleMaxDenom, QVariantMap &props)
Merges the local scale limits, if any, with the ones already in the map, if any.
static bool fillFromSld(QDomElement &element, Qt::BrushStyle &brushStyle, QColor &color)
static Qgis::RenderUnit decodeSldUom(const QString &str, double *scaleFactor=nullptr)
Decodes a SLD unit of measure string to a render unit.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
void taskCompleted()
Will be emitted by task to indicate its successful completion.
void taskTerminated()
Will be emitted by task if it has terminated for any reason other then completion (e....
bool isActive() const
Returns true if the temporal property is active.
Container for settings relating to a text buffer.
void setColor(const QColor &color)
Sets the color for the buffer.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units used for the buffer size.
void setEnabled(bool enabled)
Sets whether the text buffer will be drawn.
void setSize(double size)
Sets the size of the buffer.
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
void setFont(const QFont &font)
Sets the font used for rendering text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
void setBuffer(const QgsTextBufferSettings &bufferSettings)
Sets the text's buffer settings.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
QString createSavepoint(QString &error)
creates a save point returns empty string on error returns the last created savepoint if it's not dir...
void dirtied(const QString &sql, const QString &name)
Emitted if a sql query is executed and the underlying data is modified.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
This is the base class for vector data providers.
virtual QString dataComment() const override
Returns a short comment for the data that this provider is providing access to (e....
@ ReadLayerMetadata
Provider can read layer metadata from data store. Since QGIS 3.0. See QgsDataProvider::layerMetadata(...
@ SelectAtId
Fast access to features using their ID.
@ DeleteFeatures
Allows deletion of features.
@ CreateLabeling
Provider can set labeling settings using backend-specific formatting information. Since QGIS 3....
@ CreateRenderer
Provider can create feature renderers using backend-specific formatting information....
virtual QVariant aggregate(Qgis::Aggregate aggregate, int index, const QgsAggregateCalculator::AggregateParameters &parameters, QgsExpressionContext *context, bool &ok, QgsFeatureIds *fids=nullptr) const
Calculates an aggregated value from the layer's features.
static const int EditingCapabilities
Bitmask of all provider's editing capabilities.
long long featureCount() const override=0
Number of features in the layer.
virtual QgsFeatureRenderer * createRenderer(const QVariantMap &configuration=QVariantMap()) const
Creates a new vector layer feature renderer, using provider backend specific information.
virtual QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
virtual QStringList uniqueStringsMatching(int index, const QString &substring, int limit=-1, QgsFeedback *feedback=nullptr) const
Returns unique string values of an attribute which contain a specified subset string.
void raiseError(const QString &msg) const
Signals an error in this provider.
virtual bool isSqlQuery() const
Returns true if the layer is a query (SQL) layer.
virtual bool empty() const
Returns true if the layer does not contain any feature.
virtual QgsAttributeList pkAttributeIndexes() const
Returns list of indexes of fields that make up the primary key.
virtual void handlePostCloneOperations(QgsVectorDataProvider *source)
Handles any post-clone operations required after this vector data provider was cloned from the source...
virtual QSet< QgsMapLayerDependency > dependencies() const
Gets the list of layer ids on which this layer depends.
virtual void setEncoding(const QString &e)
Set encoding used for accessing data from layer.
virtual Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const
Returns the vector layer type flags.
QVariant maximumValue(int index) const override
Returns the maximum value of an attribute.
QgsDataProviderElevationProperties * elevationProperties() override
Returns the provider's elevation properties.
QgsFields fields() const override=0
Returns the fields associated with this data provider.
Qgis::WkbType wkbType() const override=0
Returns the geometry type which is returned by this layer.
QVariant minimumValue(int index) const override
Returns the minimum value of an attribute.
QString encoding() const
Returns the encoding which is used for accessing data.
virtual QVariant defaultValue(int fieldIndex) const
Returns any literal default values which are present at the provider for a specified field index.
QgsFieldConstraints::Constraints fieldConstraints(int fieldIndex) const
Returns any constraints which are present at the provider for a specified field index.
virtual QgsTransaction * transaction() const
Returns the transaction this data provider is included in, if any.
virtual QgsAbstractVectorLayerLabeling * createLabeling(const QVariantMap &configuration=QVariantMap()) const
Creates labeling settings, using provider backend specific information.
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
QgsVectorDataProviderTemporalCapabilities * temporalCapabilities() override
Returns the provider's temporal capabilities.
QString capabilitiesString() const
Returns the above in friendly format.
bool commitChanges(QStringList &commitErrors, bool stopEditing=true)
Attempts to commit any changes to disk.
void committedAttributesDeleted(const QString &layerId, const QgsAttributeList &deletedAttributes)
Signals emitted after committing changes.
virtual bool deleteFeature(QgsFeatureId fid)
Delete a feature from the layer (but does not commit it)
QgsFeatureIds deletedFeatureIds() const
Returns a list of deleted feature IDs which are not committed.
QgsChangedAttributesMap changedAttributeValues() const
Returns a map of features with changed attributes values which are not committed.
void committedAttributeValuesChanges(const QString &layerId, const QgsChangedAttributesMap &changedAttributesValues)
virtual bool renameAttribute(int attr, const QString &newName)
Renames an attribute field (but does not commit it)
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geom)
Emitted when a feature's geometry is changed.
virtual bool deleteFeatures(const QgsFeatureIds &fid)
Deletes a set of features from the layer (but does not commit it)
virtual bool addAttribute(const QgsField &field)
Adds an attribute field (but does not commit it) returns true if the field was added.
void committedAttributesAdded(const QString &layerId, const QList< QgsField > &addedAttributes)
virtual bool addFeatures(QgsFeatureList &features)
Insert a copy of the given features into the layer (but does not commit it)
virtual bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues)
Changes values of attributes (but does not commit it).
QgsFeatureMap addedFeatures() const
Returns a map of new features which are not committed.
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
void updateFields(QgsFields &fields)
Updates fields.
void committedFeaturesAdded(const QString &layerId, const QgsFeatureList &addedFeatures)
void featureDeleted(QgsFeatureId fid)
QgsGeometryMap changedGeometries() const
Returns a map of features with changed geometries which are not committed.
QgsVectorLayerEditBufferGroup * editBufferGroup() const
Returns the parent edit buffer group for this edit buffer, or nullptr if not part of a group.
QgsAttributeList deletedAttributeIds() const
Returns a list of deleted attributes fields which are not committed.
void attributeAdded(int idx)
void committedGeometriesChanges(const QString &layerId, const QgsGeometryMap &changedGeometries)
virtual bool addFeature(QgsFeature &f)
Adds a feature.
virtual void rollBack()
Stop editing and discard the edits.
void attributeDeleted(int idx)
void featureAdded(QgsFeatureId fid)
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &)
virtual bool commitChanges(QStringList &commitErrors)
Attempts to commit any changes to disk.
virtual bool deleteAttribute(int attr)
Deletes an attribute field (but does not commit it)
virtual bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant())
Changed an attribute value (but does not commit it)
virtual bool changeGeometry(QgsFeatureId fid, const QgsGeometry &geom)
Change feature's geometry.
void layerModified()
Emitted when modifications has been done on layer.
void committedFeaturesRemoved(const QString &layerId, const QgsFeatureIds &deletedFeatureIds)
int translateFeature(QgsFeatureId featureId, double dx, double dy)
Translates feature by dx, dy.
bool insertVertex(double x, double y, QgsFeatureId atFeatureId, int beforeVertex)
Insert a new vertex before the given vertex number, in the given ring, item (first number is index 0)...
Q_DECL_DEPRECATED Qgis::GeometryOperationResult addPart(const QVector< QgsPointXY > &ring, QgsFeatureId featureId)
Adds a new part polygon to a multipart feature.
Qgis::VectorEditResult deleteVertex(QgsFeatureId featureId, int vertex)
Deletes a vertex from a feature.
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
Q_DECL_DEPRECATED Qgis::GeometryOperationResult splitParts(const QVector< QgsPointXY > &splitLine, bool topologicalEditing=false)
Splits parts cut by the given line.
Q_DECL_DEPRECATED Qgis::GeometryOperationResult splitFeatures(const QVector< QgsPointXY > &splitLine, bool topologicalEditing=false)
Splits features cut by the given line.
bool moveVertex(double x, double y, QgsFeatureId atFeatureId, int atVertex)
Moves the vertex at the given position number, ring and item (first number is index 0),...
Q_DECL_DEPRECATED Qgis::GeometryOperationResult addRing(const QVector< QgsPointXY > &ring, const QgsFeatureIds &targetFeatureIds=QgsFeatureIds(), QgsFeatureId *modifiedFeatureId=nullptr)
Adds a ring to polygon/multipolygon features.
Vector layer specific subclass of QgsMapLayerElevationProperties.
void setDefaultsFromLayer(QgsMapLayer *layer) override
Sets default properties based on sensible choices for the given map layer.
QgsVectorLayerElevationProperties * clone() const override
Creates a clone of the properties.
Counts the features in a QgsVectorLayer in task.
QHash< QString, long long > symbolFeatureCountMap() const
Returns the count for each symbol.
void cancel() override
Notifies the task that it should terminate.
QHash< QString, QgsFeatureIds > symbolFeatureIdMap() const
Returns the QgsFeatureIds for each symbol.
Manages joined fields for a vector layer.
void resolveReferences(QgsProject *project)
Resolves layer IDs of joined layers using given project's available layers.
bool addJoin(const QgsVectorLayerJoinInfo &joinInfo)
Joins another vector layer to this layer.
void readXml(const QDomNode &layer_node)
Reads joins from project file.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves mVectorJoins to xml under the layer node.
const QgsVectorLayerJoinInfo * joinForFieldIndex(int index, const QgsFields &fields, int &sourceFieldIndex) const
Finds the vector join for a layer field index.
bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant())
Changes attribute value in joined layers.
bool removeJoin(const QString &joinLayerId)
Removes a vector layer join.
bool containsJoins() const
Quick way to test if there is any join at all.
bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues=QgsAttributeMap())
Changes attributes' values in joined layers.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features in joined layers.
void joinedFieldsChanged()
Emitted whenever the list of joined fields changes (e.g.
void updateFields(QgsFields &fields)
Updates field map with joined attributes.
bool deleteFeature(QgsFeatureId fid, QgsVectorLayer::DeleteContext *context=nullptr) const
Deletes a feature from joined layers.
const QgsVectorJoinList & vectorJoins() const
Defines left outer join from our vector layer to some other vector layer.
QString targetFieldName() const
Returns name of the field of our layer that will be used for join.
QString joinLayerId() const
ID of the joined layer - may be used to resolve reference to the joined layer.
Implementation of QgsAbstractProfileGenerator for vector layers.
Implementation of threaded rendering for vector layers.
Implementation of layer selection properties for vector layers.
QgsVectorLayerSelectionProperties * clone() const override
Creates a clone of the properties.
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads temporal properties from a DOM element previously written by writeXml().
Basic implementation of the labeling interface.
Implementation of map layer temporal properties for vector layers.
void guessDefaultsFromFields(const QgsFields &fields)
Attempts to setup the temporal properties by scanning a set of fields and looking for standard naming...
void setDefaultsFromDataProviderTemporalCapabilities(const QgsDataProviderTemporalCapabilities *capabilities) override
Sets the layers temporal settings to appropriate defaults based on a provider's temporal capabilities...
static QString guessFriendlyIdentifierField(const QgsFields &fields, bool *foundFriendly=nullptr)
Given a set of fields, attempts to pick the "most useful" field for user-friendly identification of f...
Represents a vector layer which manages a vector based data sets.
void setLabeling(QgsAbstractVectorLayerLabeling *labeling)
Sets labeling configuration.
QString attributeDisplayName(int index) const
Convenience function that returns the attribute alias if defined or the field name else.
QVariant maximumValue(int index) const FINAL
Returns the maximum value for an attribute column or an invalid variant in case of error.
int addExpressionField(const QString &exp, const QgsField &fld)
Add a new field which is calculated by the expression specified.
void committedFeaturesAdded(const QString &layerId, const QgsFeatureList &addedFeatures)
Emitted when features are added to the provider if not in transaction mode.
void setExtent(const QgsRectangle &rect) FINAL
Sets the extent.
Q_DECL_DEPRECATED Qgis::GeometryOperationResult addPart(const QList< QgsPointXY > &ring)
Adds a new part polygon to a multipart feature.
QgsRectangle sourceExtent() const FINAL
Returns the extent of all geometries from the source.
void featureBlendModeChanged(QPainter::CompositionMode blendMode)
Signal emitted when setFeatureBlendMode() is called.
bool labelsEnabled() const
Returns whether the layer contains labels which are enabled and should be drawn.
bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) const FINAL
bool isModified() const override
Returns true if the provider has been modified since the last commit.
bool writeStyle(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) const FINAL
void addFeatureRendererGenerator(QgsFeatureRendererGenerator *generator)
Adds a new feature renderer generator to the layer.
Q_DECL_DEPRECATED void setExcludeAttributesWfs(const QSet< QString > &att)
A set of attributes that are not advertised in WFS requests with QGIS server.
Q_INVOKABLE bool deleteSelectedFeatures(int *deletedCount=nullptr, QgsVectorLayer::DeleteContext *context=nullptr)
Deletes the selected features.
Q_INVOKABLE void selectByRect(QgsRectangle &rect, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
Selects features found within the search rectangle (in layer's coordinates)
void removeFieldAlias(int index)
Removes an alias (a display name) for attributes to display in dialogs.
void setAuxiliaryLayer(QgsAuxiliaryLayer *layer=nullptr)
Sets the current auxiliary layer.
void beforeRemovingExpressionField(int idx)
Will be emitted, when an expression field is going to be deleted from this vector layer.
QString loadDefaultStyle(bool &resultFlag) FINAL
Retrieve the default style for this layer if one exists (either as a .qml file on disk or as a record...
void committedGeometriesChanges(const QString &layerId, const QgsGeometryMap &changedGeometries)
Emitted when geometry changes are saved to the provider if not in transaction mode.
void beforeCommitChanges(bool stopEditing)
Emitted before changes are committed to the data provider.
Q_INVOKABLE bool startEditing()
Makes the layer editable.
void setFieldConfigurationFlags(int index, Qgis::FieldConfigurationFlags flags)
Sets the configuration flags of the field at given index.
QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength > fieldConstraintsAndStrength(int fieldIndex) const
Returns a map of constraint with their strength for a specific field of the layer.
bool addJoin(const QgsVectorLayerJoinInfo &joinInfo)
Joins another vector layer to this layer.
QSet< QgsMapLayerDependency > dependencies() const FINAL
Gets the list of dependencies.
QgsMapLayerTemporalProperties * temporalProperties() override
Returns the layer's temporal properties.
Q_DECL_DEPRECATED Qgis::GeometryOperationResult splitFeatures(const QVector< QgsPointXY > &splitLine, bool topologicalEditing=false)
Splits features cut by the given line.
QgsDefaultValue defaultValueDefinition(int index) const
Returns the definition of the expression used when calculating the default value for a field.
QgsExpressionContextScope * createExpressionContextScope() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsMapLayerRenderer * createMapRenderer(QgsRenderContext &rendererContext) FINAL
Returns new instance of QgsMapLayerRenderer that will be used for rendering of given context.
QgsVectorLayerFeatureCounter * countSymbolFeatures(bool storeSymbolFids=false)
Count features for symbols.
QPainter::CompositionMode featureBlendMode() const
Returns the current blending mode for features.
bool hasMapTips() const FINAL
Returns true if the layer contains map tips.
QString constraintExpression(int index) const
Returns the constraint expression for for a specified field index, if set.
bool addAttribute(const QgsField &field)
Add an attribute field (but does not commit it) returns true if the field was added.
void attributeAdded(int idx)
Will be emitted, when a new attribute has been added to this vector layer.
QString capabilitiesString() const
Capabilities for this layer, comma separated and translated.
static const QgsSettingsEntryEnumFlag< QgsVectorSimplifyMethod::SimplifyHints > * settingsSimplifyDrawingHints
void deselect(QgsFeatureId featureId)
Deselects feature by its ID.
void allowCommitChanged()
Emitted whenever the allowCommitChanged() property of this layer changes.
friend class QgsVectorLayerEditBuffer
void editCommandStarted(const QString &text)
Signal emitted when a new edit command has been started.
void updateFields()
Will regenerate the fields property of this layer by obtaining all fields from the dataProvider,...
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
const QgsDiagramLayerSettings * diagramLayerSettings() const
void setFieldConstraint(int index, QgsFieldConstraints::Constraint constraint, QgsFieldConstraints::ConstraintStrength strength=QgsFieldConstraints::ConstraintStrengthHard)
Sets a constraint for a specified field index.
bool loadAuxiliaryLayer(const QgsAuxiliaryStorage &storage, const QString &key=QString())
Loads the auxiliary layer for this vector layer.
bool deleteFeature(QgsFeatureId fid, DeleteContext *context=nullptr)
Deletes a feature from the layer (but does not commit it).
bool insertVertex(double x, double y, QgsFeatureId atFeatureId, int beforeVertex)
Inserts a new vertex before the given vertex number, in the given ring, item (first number is index 0...
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsAbstractProfileGenerator * createProfileGenerator(const QgsProfileRequest &request) override
Given a profile request, returns a new profile generator ready for generating elevation profiles.
QString htmlMetadata() const FINAL
Obtain a formatted HTML string containing assorted metadata for this layer.
Q_INVOKABLE QgsRectangle boundingBoxOfSelected() const
Returns the bounding box of the selected features. If there is no selection, QgsRectangle(0,...
QgsFields fields() const FINAL
Returns the list of fields of this layer.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a list of features to the sink.
Q_INVOKABLE QgsFeatureList selectedFeatures() const
Returns a copy of the user-selected features.
QString expressionField(int index) const
Returns the expression used for a given expression field.
bool readSymbology(const QDomNode &layerNode, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) FINAL
void removeFeatureRendererGenerator(const QString &id)
Removes the feature renderer with matching id from the layer.
friend class QgsVectorLayerEditPassthrough
void setSimplifyMethod(const QgsVectorSimplifyMethod &simplifyMethod)
Sets the simplification settings for fast rendering of features.
void editCommandDestroyed()
Signal emitted, when an edit command is destroyed.
QVariant aggregate(Qgis::Aggregate aggregate, const QString &fieldOrExpression, const QgsAggregateCalculator::AggregateParameters &parameters=QgsAggregateCalculator::AggregateParameters(), QgsExpressionContext *context=nullptr, bool *ok=nullptr, QgsFeatureIds *fids=nullptr, QgsFeedback *feedback=nullptr, QString *error=nullptr) const
Calculates an aggregated value from the layer's features.
QgsFieldConstraints::Constraints fieldConstraints(int fieldIndex) const
Returns any constraints which are present for a specified field index.
bool deleteFeatures(const QgsFeatureIds &fids, DeleteContext *context=nullptr)
Deletes a set of features from the layer (but does not commit it)
Q_DECL_DEPRECATED QSet< QString > excludeAttributesWms() const
A set of attributes that are not advertised in WMS requests with QGIS server.
QgsBox3D sourceExtent3D() const FINAL
Returns the 3D extent of all geometries from the source.
QgsFeatureIds symbolFeatureIds(const QString &legendKey) const
Ids of features rendered with specified legend key.
void removeFieldConstraint(int index, QgsFieldConstraints::Constraint constraint)
Removes a constraint for a specified field index.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
void featuresDeleted(const QgsFeatureIds &fids)
Emitted when features have been deleted.
Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const
Returns the vector layer type flags.
void setLabelsEnabled(bool enabled)
Sets whether labels should be enabled for the layer.
void subsetStringChanged()
Emitted when the layer's subset string has changed.
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.
void setCoordinateSystem()
Setup the coordinate system transformation for the layer.
void committedFeaturesRemoved(const QString &layerId, const QgsFeatureIds &deletedFeatureIds)
Emitted when features are deleted from the provider if not in transaction mode.
void updateExpressionField(int index, const QString &exp)
Changes the expression used to define an expression based (virtual) field.
Q_INVOKABLE void selectByExpression(const QString &expression, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection, QgsExpressionContext *context=nullptr)
Selects matching features using an expression.
static const QgsSettingsEntryDouble * settingsSimplifyMaxScale
~QgsVectorLayer() override
QgsCoordinateReferenceSystem sourceCrs() const FINAL
Returns the coordinate reference system for features in the source.
void endEditCommand()
Finish edit command and add it to undo/redo stack.
void destroyEditCommand()
Destroy active command and reverts all changes in it.
bool isAuxiliaryField(int index, int &srcIndex) const
Returns true if the field comes from the auxiliary layer, false otherwise.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QList< QgsRelation > referencingRelations(int idx) const
Returns the layer's relations, where the foreign key is on this layer.
Q_DECL_DEPRECATED QSet< QString > excludeAttributesWfs() const
A set of attributes that are not advertised in WFS requests with QGIS server.
Q_DECL_DEPRECATED Qgis::GeometryOperationResult splitParts(const QVector< QgsPointXY > &splitLine, bool topologicalEditing=false)
Splits parts cut by the given line.
void setDefaultValueDefinition(int index, const QgsDefaultValue &definition)
Sets the definition of the expression to use when calculating the default value for a field.
bool diagramsEnabled() const
Returns whether the layer contains diagrams which are enabled and should be drawn.
void setAllowCommit(bool allowCommit)
Controls, if the layer is allowed to commit changes.
bool setDependencies(const QSet< QgsMapLayerDependency > &layers) FINAL
Sets the list of dependencies.
void symbolFeatureCountMapChanged()
Emitted when the feature count for symbols on this layer has been recalculated.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Qgis::VectorEditResult deleteVertex(QgsFeatureId featureId, int vertex)
Deletes a vertex from a feature.
void setFeatureBlendMode(QPainter::CompositionMode blendMode)
Sets the blending mode used for rendering each feature.
QString constraintDescription(int index) const
Returns the descriptive name for the constraint expression for a specified field index.
void writeCustomSymbology(QDomElement &element, QDomDocument &doc, QString &errorMessage) const
Signal emitted whenever the symbology (QML-file) for this layer is being written.
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
void setProviderEncoding(const QString &encoding)
Sets the text encoding of the data provider.
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.
void setDisplayExpression(const QString &displayExpression)
Set the preview expression, used to create a human readable preview string.
virtual bool deleteAttribute(int attr)
Deletes an attribute field (but does not commit it).
static const QgsSettingsEntryBool * settingsSimplifyLocal
void resolveReferences(QgsProject *project) FINAL
Resolves references to other layers (kept as layer IDs after reading XML) into layer objects.
bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues=QgsAttributeMap(), bool skipDefaultValues=false)
Changes attributes' values for a feature (but does not immediately commit the changes).
QgsMapLayerElevationProperties * elevationProperties() override
Returns the layer's elevation properties.
bool removeJoin(const QString &joinLayerId)
Removes a vector layer join.
Q_INVOKABLE void invertSelectionInRectangle(QgsRectangle &rect)
Inverts selection of features found within the search rectangle (in layer's coordinates)
void setRenderer(QgsFeatureRenderer *r)
Sets the feature renderer which will be invoked to represent this layer in 2D map views.
Q_INVOKABLE void selectAll()
Select all the features.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
QStringList commitErrors() const
Returns a list containing any error messages generated when attempting to commit changes to the layer...
QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
bool readExtentFromXml() const
Returns true if the extent is read from the XML document when data source has no metadata,...
QString dataComment() const
Returns a description for this layer as defined in the data provider.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
QgsGeometryOptions * geometryOptions() const
Configuration and logic to apply automatically on any edit happening on this layer.
QgsStringMap attributeAliases() const
Returns a map of field name to attribute alias.
Q_INVOKABLE int translateFeature(QgsFeatureId featureId, double dx, double dy)
Translates feature by dx, dy.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
virtual void updateExtents(bool force=false)
Update the extents for the layer.
void attributeDeleted(int idx)
Will be emitted, when an attribute has been deleted from this vector layer.
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
void beforeEditingStarted()
Emitted before editing on this layer is started.
void committedAttributeValuesChanges(const QString &layerId, const QgsChangedAttributesMap &changedAttributesValues)
Emitted when attribute value changes are saved to the provider if not in transaction mode.
void committedAttributesAdded(const QString &layerId, const QList< QgsField > &addedAttributes)
Emitted when attributes are added to the provider if not in transaction mode.
void setEditFormConfig(const QgsEditFormConfig &editFormConfig)
Sets the editFormConfig (configuration) of the form used to represent this vector layer.
Qgis::FieldConfigurationFlags fieldConfigurationFlags(int index) const
Returns the configuration flags of the field at given index.
void committedAttributesDeleted(const QString &layerId, const QgsAttributeList &deletedAttributes)
Emitted when attributes are deleted from the provider if not in transaction mode.
QString displayExpression
void displayExpressionChanged()
Emitted when the display expression changes.
QVariant minimumValue(int index) const FINAL
Returns the minimum value for an attribute column or an invalid variant in case of error.
QgsFeatureIterator getSelectedFeatures(QgsFeatureRequest request=QgsFeatureRequest()) const
Returns an iterator of the selected features.
void setEditorWidgetSetup(int index, const QgsEditorWidgetSetup &setup)
The editor widget setup defines which QgsFieldFormatter and editor widget will be used for the field ...
void setConstraintExpression(int index, const QString &expression, const QString &description=QString())
Sets the constraint expression for the specified field index.
Q_INVOKABLE bool rollBack(bool deleteBuffer=true)
Stops a current editing operation and discards any uncommitted edits.
bool readStyle(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) FINAL
bool updateFeature(QgsFeature &feature, bool skipDefaultValues=false)
Updates an existing feature in the layer, replacing the attributes and geometry for the feature with ...
Q_INVOKABLE bool commitChanges(bool stopEditing=true)
Attempts to commit to the underlying data provider any buffered changes made since the last to call t...
void setFieldConfigurationFlag(int index, Qgis::FieldConfigurationFlag flag, bool active)
Sets the given configuration flag for the field at given index to be active or not.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
bool setReadOnly(bool readonly=true)
Makes layer read-only (editing disabled) or not.
void editFormConfigChanged()
Will be emitted whenever the edit form configuration of this layer changes.
Q_INVOKABLE void modifySelection(const QgsFeatureIds &selectIds, const QgsFeatureIds &deselectIds)
Modifies the current selection on this layer.
void setWeakRelations(const QList< QgsWeakRelation > &relations)
Sets the layer's weak relations.
void reselect()
Reselects the previous set of selected features.
void select(QgsFeatureId featureId)
Selects feature by its ID.
QgsEditorWidgetSetup editorWidgetSetup(int index) const
The editor widget setup defines which QgsFieldFormatter and editor widget will be used for the field ...
long long featureCount() const FINAL
Returns feature count including changes which have not yet been committed If you need only the count ...
void setReadExtentFromXml(bool readExtentFromXml)
Flag allowing to indicate if the extent has to be read from the XML document when data source has no ...
void afterCommitChanges()
Emitted after changes are committed to the data provider.
QgsVectorLayer * clone() const override
Returns a new instance equivalent to this one.
QgsAttributeTableConfig attributeTableConfig() const
Returns the attribute table configuration object.
QgsActionManager * actions()
Returns all layer actions defined on this layer.
bool readSld(const QDomNode &node, QString &errorMessage) FINAL
QgsFeature getFeature(QgsFeatureId fid) const
Queries the layer for the feature with the given id.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
Selects matching features using a list of feature IDs.
QStringList uniqueStringsMatching(int index, const QString &substring, int limit=-1, QgsFeedback *feedback=nullptr) const
Returns unique string values of an attribute which contain a specified subset string.
void raiseError(const QString &msg)
Signals an error related to this vector layer.
void editCommandEnded()
Signal emitted, when an edit command successfully ended.
void supportsEditingChanged()
Emitted when the read only state or the data provider of this layer is changed.
void readOnlyChanged()
Emitted when the read only state of this layer is changed.
void removeExpressionField(int index)
Removes an expression field.
virtual void setTransformContext(const QgsCoordinateTransformContext &transformContext) override
Sets the coordinate transform context to transformContext.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Emitted whenever an attribute value change is done in the edit buffer.
static Q_DECL_DEPRECATED void drawVertexMarker(double x, double y, QPainter &p, Qgis::VertexMarkerType type, int vertexSize)
Draws a vertex symbol at (screen) coordinates x, y.
bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant(), bool skipDefaultValues=false)
Changes an attribute value for a feature (but does not immediately commit the changes).
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a single feature to the sink.
void setFieldAlias(int index, const QString &aliasString)
Sets an alias (a display name) for attributes to display in dialogs.
friend class QgsVectorLayerFeatureSource
void minimumAndMaximumValue(int index, QVariant &minimum, QVariant &maximum) const
Calculates both the minimum and maximum value for an attribute column.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
QgsRectangle extent() const FINAL
Returns the extent of the layer.
Q_DECL_DEPRECATED void setExcludeAttributesWms(const QSet< QString > &att)
A set of attributes that are not advertised in WMS requests with QGIS server.
void setAttributeTableConfig(const QgsAttributeTableConfig &attributeTableConfig)
Sets the attribute table configuration object.
virtual bool setSubsetString(const QString &subset)
Sets the string (typically sql) used to define a subset of the layer.
bool readXml(const QDomNode &layer_node, QgsReadWriteContext &context) FINAL
Reads vector layer specific state from project file Dom node.
void afterRollBack()
Emitted after changes are rolled back.
QString decodedSource(const QString &source, const QString &provider, const QgsReadWriteContext &context) const FINAL
Called by readLayerXML(), used by derived classes to decode provider's specific data source from proj...
void setDiagramLayerSettings(const QgsDiagramLayerSettings &s)
QList< QgsWeakRelation > weakRelations() const
Returns the layer's weak relations as specified in the layer's style.
const QgsVectorSimplifyMethod & simplifyMethod() const
Returns the simplification settings for fast rendering of features.
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
Emitted when selection was changed.
void beforeAddingExpressionField(const QString &fieldName)
Will be emitted, when an expression field is going to be added to this vector layer.
bool deleteAttributes(const QList< int > &attrs)
Deletes a list of attribute fields (but does not commit it)
void updatedFields()
Emitted whenever the fields available from this layer have been changed.
QVariant defaultValue(int index, const QgsFeature &feature=QgsFeature(), QgsExpressionContext *context=nullptr) const
Returns the calculated default value for the specified field index.
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer.
QString sourceName() const FINAL
Returns a friendly display name for the source.
QString attributeAlias(int index) const
Returns the alias of an attribute name or a null string if there is no alias.
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted.
QgsBox3D extent3D() const FINAL
Returns the 3D extent of the layer.
Q_INVOKABLE void removeSelection()
Clear selection.
bool allowCommit() const
Controls, if the layer is allowed to commit changes.
QgsConditionalLayerStyles * conditionalStyles() const
Returns the conditional styles that are set for this layer.
void readCustomSymbology(const QDomElement &element, QString &errorMessage)
Signal emitted whenever the symbology (QML-file) for this layer is being read.
void reload() FINAL
Synchronises with changes in the datasource.
const QList< QgsVectorLayerJoinInfo > vectorJoins() const
bool renameAttribute(int index, const QString &newName)
Renames an attribute field (but does not commit it).
bool isSqlQuery() const
Returns true if the layer is a query (SQL) layer.
bool simplifyDrawingCanbeApplied(const QgsRenderContext &renderContext, QgsVectorSimplifyMethod::SimplifyHint simplifyHint) const
Returns whether the VectorLayer can apply the specified simplification hint.
void beforeRollBack()
Emitted before changes are rolled back.
QgsAttributeList primaryKeyAttributes() const
Returns the list of attributes which make up the layer's primary keys.
bool writeXml(QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context) const FINAL
Writes vector layer specific state to project file Dom node.
QString encodedSource(const QString &source, const QgsReadWriteContext &context) const FINAL
Called by writeLayerXML(), used by derived classes to encode provider's specific data source to proje...
static const QgsSettingsEntryEnumFlag< QgsVectorSimplifyMethod::SimplifyAlgorithm > * settingsSimplifyAlgorithm
void beginEditCommand(const QString &text)
Create edit command for undo/redo operations.
QString displayField() const
This is a shorthand for accessing the displayExpression if it is a simple field.
Q_DECL_DEPRECATED Qgis::GeometryOperationResult addRing(const QVector< QgsPointXY > &ring, QgsFeatureId *featureId=nullptr)
Adds a ring to polygon/multipolygon features.
void setDiagramRenderer(QgsDiagramRenderer *r)
Sets diagram rendering object (takes ownership)
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Emitted whenever a geometry change is done in the edit buffer.
QgsEditFormConfig editFormConfig
QList< const QgsFeatureRendererGenerator * > featureRendererGenerators() const
Returns a list of the feature renderer generators owned by the layer.
Qgis::FeatureAvailability hasFeatures() const FINAL
Determines if this vector layer has features.
bool moveVertex(double x, double y, QgsFeatureId atFeatureId, int atVertex)
Moves the vertex at the given position number, ring and item (first number is index 0),...
QgsGeometry getGeometry(QgsFeatureId fid) const
Queries the layer for the geometry at the given id.
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
void beforeModifiedCheck() const
Emitted when the layer is checked for modifications. Use for last-minute additions.
Q_INVOKABLE void invertSelection()
Selects not selected features and deselects selected ones.
const QgsDiagramRenderer * diagramRenderer() const
void setExtent3D(const QgsBox3D &rect) FINAL
Sets the extent.
QgsMapLayerSelectionProperties * selectionProperties() override
Returns the layer's selection properties.
bool changeGeometry(QgsFeatureId fid, QgsGeometry &geometry, bool skipDefaultValue=false)
Changes a feature's geometry within the layer's edit buffer (but does not immediately commit the chan...
static const QgsSettingsEntryDouble * settingsSimplifyDrawingTol
Qgis::SpatialIndexPresence hasSpatialIndex() const override
QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const FINAL
Calculates a list of unique values contained within an attribute in the layer.
void setFieldSplitPolicy(int index, Qgis::FieldDomainSplitPolicy policy)
Sets a split policy for the field with the specified index.
bool forceLocalOptimization() const
Gets where the simplification executes, after fetch the geometries from provider, or when supported,...
void setSimplifyHints(SimplifyHints simplifyHints)
Sets the simplification hints of the vector layer managed.
float maximumScale() const
Gets the maximum scale at which the layer should be simplified.
void setThreshold(float threshold)
Sets the simplification threshold of the vector layer managed.
void setSimplifyAlgorithm(SimplifyAlgorithm simplifyAlgorithm)
Sets the local simplification algorithm of the vector layer managed.
void setForceLocalOptimization(bool localOptimization)
Sets where the simplification executes, after fetch the geometries from provider, or when supported,...
SimplifyHints simplifyHints() const
Gets the simplification hints of the vector layer managed.
SimplifyAlgorithm simplifyAlgorithm() const
Gets the local simplification algorithm of the vector layer managed.
float threshold() const
Gets the simplification threshold of the vector layer managed.
SimplifyHint
Simplification flags for fast rendering of features.
@ NoSimplification
No simplification can be applied.
SimplifyAlgorithm
Types of local simplification algorithms that can be used.
@ Distance
The simplification uses the distance between points to remove duplicate points.
void setMaximumScale(float maximumScale)
Sets the maximum scale at which the layer should be simplified.
@ Referencing
The layer is referencing (or the "child" / "right" layer in the relationship)
@ Referenced
The layer is referenced (or the "parent" / "left" left in the relationship)
static void writeXml(const QgsVectorLayer *layer, WeakRelationType type, const QgsRelation &relation, QDomNode &node, QDomDocument &doc)
Writes a weak relation infoto an XML structure.
static QgsWeakRelation readXml(const QgsVectorLayer *layer, WeakRelationType type, const QDomNode &node, const QgsPathResolver resolver)
Returns a weak relation for the given layer.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
static QString geometryDisplayString(Qgis::GeometryType type)
Returns a display string for a geometry type.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QgsBox3D readBox3D(const QDomElement &element)
Decodes a DOM element to a 3D box.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
static QgsRectangle readRectangle(const QDomElement &element)
@ UnknownCount
Provider returned an unknown feature count.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
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
bool qgsVariantEqual(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether they are equal, two NULL values are always treated a...
Definition qgis.cpp:247
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
Definition qgis.cpp:120
bool qgsVariantGreaterThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is greater than the second.
Definition qgis.cpp:188
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:5354
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:5335
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:5393
T qgsFlagKeysToValue(const QString &keys, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given keys of a flag.
Definition qgis.h:5415
QMap< QString, QString > QgsStringMap
Definition qgis.h:5674
QVector< QgsPoint > QgsPointSequence
QMap< int, QVariant > QgsAttributeMap
QList< QgsFeature > QgsFeatureList
Definition qgsfeature.h:917
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList
Definition qgsfield.h:27
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
#define RENDERER_TAG_NAME
Definition qgsrenderer.h:50
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
bool saveStyle_t(const QString &uri, const QString &qmlStyle, const QString &sldStyle, const QString &styleName, const QString &styleDescription, const QString &uiFileContent, bool useAsDefault, QString &errCause)
int listStyles_t(const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause)
QString getStyleById_t(const QString &uri, QString styleID, QString &errCause)
bool deleteStyleById_t(const QString &uri, QString styleID, QString &errCause)
QString loadStyle_t(const QString &uri, QString &errCause)
QList< int > QgsAttributeList
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
A bundle of parameters controlling aggregate calculation.
Setting options for creating vector data providers.
Context for cascade delete features.
QList< QgsVectorLayer * > handledLayers(bool includeAuxiliaryLayers=true) const
Returns a list of all layers affected by the delete operation.
QMap< QgsVectorLayer *, QgsFeatureIds > mHandledFeatures
QgsFeatureIds handledFeatures(QgsVectorLayer *layer) const
Returns a list of feature IDs from the specified layer affected by the delete operation.
Setting options for loading vector layers.
bool skipCrsValidation
Controls whether the layer is allowed to have an invalid/unknown CRS.
bool forceReadOnly
Controls whether the layer is forced to be load as Read Only.
bool loadDefaultStyle
Set to true if the default layer style should be loaded.
QgsCoordinateTransformContext transformContext
Coordinate transform context.
QgsCoordinateReferenceSystem fallbackCrs
Fallback layer coordinate reference system.
Qgis::WkbType fallbackWkbType
Fallback geometry type.
bool loadAllStoredStyles
Controls whether the stored styles will be all loaded.