QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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 Qgis::DataProviderReadFlags providerFlags;
200 if ( options.loadDefaultStyle )
201 {
203 }
204 if ( options.forceReadOnly )
205 {
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 ); // skip-keyword-check
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 ) == Qgis::FieldOrigin::Expression )
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
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;
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, QgsExpressionContext *context )
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, context ) );
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 ( mDataProvider && mDataProvider->isValid() && ( mDataProvider->flags() & Qgis::DataProviderFlag::FastExtent2D ) )
1036 {
1037 // Provider has a trivial 2D extent calculation => always get extent from provider.
1038 // Things are nice and simple this way, e.g. we can always trust that this extent is
1039 // accurate and up to date.
1040 updateExtent( mDataProvider->extent() );
1041 mValidExtent2D = true;
1042 mLazyExtent2D = false;
1043 }
1044 else
1045 {
1046 if ( !mValidExtent2D && mLazyExtent2D && mReadExtentFromXml && !mXmlExtent2D.isNull() )
1047 {
1048 updateExtent( mXmlExtent2D );
1049 mValidExtent2D = true;
1050 mLazyExtent2D = false;
1051 }
1052
1053 if ( !mValidExtent2D && mLazyExtent2D && mDataProvider && mDataProvider->isValid() )
1054 {
1055 // store the extent
1056 updateExtent( mDataProvider->extent() );
1057 mValidExtent2D = true;
1058 mLazyExtent2D = false;
1059
1060 // show the extent
1061 QgsDebugMsgLevel( QStringLiteral( "2D Extent of layer: %1" ).arg( mExtent2D.toString() ), 3 );
1062 }
1063 }
1064
1065 if ( mValidExtent2D )
1066 return QgsMapLayer::extent();
1067
1068 if ( !isValid() || !mDataProvider )
1069 {
1070 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1071 return rect;
1072 }
1073
1074 if ( !mEditBuffer ||
1075 ( !mDataProvider->transaction() && ( mEditBuffer->deletedFeatureIds().isEmpty() && mEditBuffer->changedGeometries().isEmpty() ) ) ||
1077 {
1078 mDataProvider->updateExtents();
1079
1080 // get the extent of the layer from the provider
1081 // but only when there are some features already
1082 if ( mDataProvider->featureCount() != 0 )
1083 {
1084 const QgsRectangle r = mDataProvider->extent();
1085 rect.combineExtentWith( r );
1086 }
1087
1088 if ( mEditBuffer && !mDataProvider->transaction() )
1089 {
1090 const auto addedFeatures = mEditBuffer->addedFeatures();
1091 for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it )
1092 {
1093 if ( it->hasGeometry() )
1094 {
1095 const QgsRectangle r = it->geometry().boundingBox();
1096 rect.combineExtentWith( r );
1097 }
1098 }
1099 }
1100 }
1101 else
1102 {
1104 .setNoAttributes() );
1105
1106 QgsFeature fet;
1107 while ( fit.nextFeature( fet ) )
1108 {
1109 if ( fet.hasGeometry() && fet.geometry().type() != Qgis::GeometryType::Unknown )
1110 {
1111 const QgsRectangle bb = fet.geometry().boundingBox();
1112 rect.combineExtentWith( bb );
1113 }
1114 }
1115 }
1116
1117 if ( rect.xMinimum() > rect.xMaximum() && rect.yMinimum() > rect.yMaximum() )
1118 {
1119 // special case when there are no features in provider nor any added
1120 rect = QgsRectangle(); // use rectangle with zero coordinates
1121 }
1122
1123 updateExtent( rect );
1124 mValidExtent2D = true;
1125
1126 // Send this (hopefully) up the chain to the map canvas
1127 emit recalculateExtents();
1128
1129 return rect;
1130}
1131
1133{
1135
1136 // if data is 2D, redirect to 2D extend computation, and save it as 2D extent (in 3D bbox)
1137 if ( mDataProvider && mDataProvider->elevationProperties() && !mDataProvider->elevationProperties()->containsElevationData() )
1138 {
1139 return QgsBox3D( extent() );
1140 }
1141
1143 extent.setNull();
1144
1145 if ( !isSpatial() )
1146 return extent;
1147
1148 if ( mDataProvider && mDataProvider->isValid() && ( mDataProvider->flags() & Qgis::DataProviderFlag::FastExtent3D ) )
1149 {
1150 // Provider has a trivial 3D extent calculation => always get extent from provider.
1151 // Things are nice and simple this way, e.g. we can always trust that this extent is
1152 // accurate and up to date.
1153 updateExtent( mDataProvider->extent3D() );
1154 mValidExtent3D = true;
1155 mLazyExtent3D = false;
1156 }
1157 else
1158 {
1159 if ( !mValidExtent3D && mLazyExtent3D && mReadExtentFromXml && !mXmlExtent3D.isNull() )
1160 {
1161 updateExtent( mXmlExtent3D );
1162 mValidExtent3D = true;
1163 mLazyExtent3D = false;
1164 }
1165
1166 if ( !mValidExtent3D && mLazyExtent3D && mDataProvider && mDataProvider->isValid() )
1167 {
1168 // store the extent
1169 updateExtent( mDataProvider->extent3D() );
1170 mValidExtent3D = true;
1171 mLazyExtent3D = false;
1172
1173 // show the extent
1174 QgsDebugMsgLevel( QStringLiteral( "3D Extent of layer: %1" ).arg( mExtent3D.toString() ), 3 );
1175 }
1176 }
1177
1178 if ( mValidExtent3D )
1179 return QgsMapLayer::extent3D();
1180
1181 if ( !isValid() || !mDataProvider )
1182 {
1183 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1184 return extent;
1185 }
1186
1187 if ( !mEditBuffer ||
1188 ( !mDataProvider->transaction() && ( mEditBuffer->deletedFeatureIds().isEmpty() && mEditBuffer->changedGeometries().isEmpty() ) ) ||
1190 {
1191 mDataProvider->updateExtents();
1192
1193 // get the extent of the layer from the provider
1194 // but only when there are some features already
1195 if ( mDataProvider->featureCount() != 0 )
1196 {
1197 const QgsBox3D ext = mDataProvider->extent3D();
1198 extent.combineWith( ext );
1199 }
1200
1201 if ( mEditBuffer && !mDataProvider->transaction() )
1202 {
1203 const auto addedFeatures = mEditBuffer->addedFeatures();
1204 for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it )
1205 {
1206 if ( it->hasGeometry() )
1207 {
1208 const QgsBox3D bbox = it->geometry().boundingBox3D();
1209 extent.combineWith( bbox );
1210 }
1211 }
1212 }
1213 }
1214 else
1215 {
1217 .setNoAttributes() );
1218
1219 QgsFeature fet;
1220 while ( fit.nextFeature( fet ) )
1221 {
1222 if ( fet.hasGeometry() && fet.geometry().type() != Qgis::GeometryType::Unknown )
1223 {
1224 const QgsBox3D bb = fet.geometry().boundingBox3D();
1225 extent.combineWith( bb );
1226 }
1227 }
1228 }
1229
1230 if ( extent.xMinimum() > extent.xMaximum() && extent.yMinimum() > extent.yMaximum() && extent.zMinimum() > extent.zMaximum() )
1231 {
1232 // special case when there are no features in provider nor any added
1233 extent = QgsBox3D(); // use rectangle with zero coordinates
1234 }
1235
1236 updateExtent( extent );
1237 mValidExtent3D = true;
1238
1239 // Send this (hopefully) up the chain to the map canvas
1240 emit recalculateExtents();
1241
1242 return extent;
1243}
1244
1251
1258
1260{
1262
1263 if ( !isValid() || !mDataProvider )
1264 {
1265 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1266 return customProperty( QStringLiteral( "storedSubsetString" ) ).toString();
1267 }
1268 return mDataProvider->subsetString();
1269}
1270
1271bool QgsVectorLayer::setSubsetString( const QString &subset )
1272{
1274
1275 if ( !isValid() || !mDataProvider )
1276 {
1277 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider or while editing" ), 3 );
1278 setCustomProperty( QStringLiteral( "storedSubsetString" ), subset );
1279 return false;
1280 }
1281 else if ( mEditBuffer )
1282 {
1283 QgsDebugMsgLevel( QStringLiteral( "invoked while editing" ), 3 );
1284 return false;
1285 }
1286
1287 if ( subset == mDataProvider->subsetString() )
1288 return true;
1289
1290 bool res = mDataProvider->setSubsetString( subset );
1291
1292 // get the updated data source string from the provider
1293 mDataSource = mDataProvider->dataSourceUri();
1294 updateExtents();
1295 updateFields();
1296
1297 if ( res )
1298 {
1299 emit subsetStringChanged();
1301 }
1302
1303 return res;
1304}
1305
1307{
1308 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1310
1311 if ( isValid() && mDataProvider && !mEditBuffer && ( isSpatial() && geometryType() != Qgis::GeometryType::Point ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && renderContext.useRenderingOptimization() )
1312 {
1313 double maximumSimplificationScale = mSimplifyMethod.maximumScale();
1314
1315 // check maximum scale at which generalisation should be carried out
1316 return !( maximumSimplificationScale > 1 && renderContext.rendererScale() <= maximumSimplificationScale );
1317 }
1318 return false;
1319}
1320
1322{
1324
1325 return mConditionalStyles;
1326}
1327
1329{
1330 // non fatal for now -- the aggregate expression functions are not thread safe and call this
1332
1333 if ( !isValid() || !mDataProvider )
1334 return QgsFeatureIterator();
1335
1336 return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
1337}
1338
1340{
1342
1343 QgsFeature feature;
1345 if ( feature.isValid() )
1346 return feature.geometry();
1347 else
1348 return QgsGeometry();
1349}
1350
1352{
1354
1355 if ( !isValid() || !mEditBuffer || !mDataProvider )
1356 return false;
1357
1358
1359 if ( mGeometryOptions->isActive() )
1360 {
1361 QgsGeometry geom = feature.geometry();
1362 mGeometryOptions->apply( geom );
1363 feature.setGeometry( geom );
1364 }
1365
1366 bool success = mEditBuffer->addFeature( feature );
1367
1368 if ( success )
1369 {
1370 updateExtents();
1371
1372 if ( mJoinBuffer->containsJoins() )
1373 success = mJoinBuffer->addFeature( feature );
1374 }
1375
1376 return success;
1377}
1378
1379bool QgsVectorLayer::updateFeature( QgsFeature &updatedFeature, bool skipDefaultValues )
1380{
1382
1383 if ( !mEditBuffer || !mDataProvider )
1384 {
1385 return false;
1386 }
1387
1388 QgsFeature currentFeature = getFeature( updatedFeature.id() );
1389 if ( currentFeature.isValid() )
1390 {
1391 bool hasChanged = false;
1392 bool hasError = false;
1393
1394 if ( ( updatedFeature.hasGeometry() || currentFeature.hasGeometry() ) && !updatedFeature.geometry().equals( currentFeature.geometry() ) )
1395 {
1396 QgsGeometry geometry = updatedFeature.geometry();
1397 if ( changeGeometry( updatedFeature.id(), geometry, true ) )
1398 {
1399 hasChanged = true;
1400 updatedFeature.setGeometry( geometry );
1401 }
1402 else
1403 {
1404 QgsDebugMsgLevel( QStringLiteral( "geometry of feature %1 could not be changed." ).arg( updatedFeature.id() ), 3 );
1405 }
1406 }
1407
1408 QgsAttributes fa = updatedFeature.attributes();
1409 QgsAttributes ca = currentFeature.attributes();
1410
1411 for ( int attr = 0; attr < fa.count(); ++attr )
1412 {
1413 if ( !qgsVariantEqual( fa.at( attr ), ca.at( attr ) ) )
1414 {
1415 if ( changeAttributeValue( updatedFeature.id(), attr, fa.at( attr ), ca.at( attr ), true ) )
1416 {
1417 hasChanged = true;
1418 }
1419 else
1420 {
1421 QgsDebugMsgLevel( QStringLiteral( "attribute %1 of feature %2 could not be changed." ).arg( attr ).arg( updatedFeature.id() ), 3 );
1422 hasError = true;
1423 }
1424 }
1425 }
1426 if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )
1427 updateDefaultValues( updatedFeature.id(), updatedFeature );
1428
1429 return !hasError;
1430 }
1431 else
1432 {
1433 QgsDebugMsgLevel( QStringLiteral( "feature %1 could not be retrieved" ).arg( updatedFeature.id() ), 3 );
1434 return false;
1435 }
1436}
1437
1438
1439bool QgsVectorLayer::insertVertex( double x, double y, QgsFeatureId atFeatureId, int beforeVertex )
1440{
1442
1443 if ( !isValid() || !mEditBuffer || !mDataProvider )
1444 return false;
1445
1446 QgsVectorLayerEditUtils utils( this );
1447 bool result = utils.insertVertex( x, y, atFeatureId, beforeVertex );
1448 if ( result )
1449 updateExtents();
1450 return result;
1451}
1452
1453
1454bool QgsVectorLayer::insertVertex( const QgsPoint &point, QgsFeatureId atFeatureId, int beforeVertex )
1455{
1457
1458 if ( !isValid() || !mEditBuffer || !mDataProvider )
1459 return false;
1460
1461 QgsVectorLayerEditUtils utils( this );
1462 bool result = utils.insertVertex( point, atFeatureId, beforeVertex );
1463 if ( result )
1464 updateExtents();
1465 return result;
1466}
1467
1468
1469bool QgsVectorLayer::moveVertex( double x, double y, QgsFeatureId atFeatureId, int atVertex )
1470{
1472
1473 if ( !isValid() || !mEditBuffer || !mDataProvider )
1474 return false;
1475
1476 QgsVectorLayerEditUtils utils( this );
1477 bool result = utils.moveVertex( x, y, atFeatureId, atVertex );
1478
1479 if ( result )
1480 updateExtents();
1481 return result;
1482}
1483
1484bool QgsVectorLayer::moveVertex( const QgsPoint &p, QgsFeatureId atFeatureId, int atVertex )
1485{
1487
1488 if ( !isValid() || !mEditBuffer || !mDataProvider )
1489 return false;
1490
1491 QgsVectorLayerEditUtils utils( this );
1492 bool result = utils.moveVertex( p, atFeatureId, atVertex );
1493
1494 if ( result )
1495 updateExtents();
1496 return result;
1497}
1498
1500{
1502
1503 if ( !isValid() || !mEditBuffer || !mDataProvider )
1505
1506 QgsVectorLayerEditUtils utils( this );
1507 Qgis::VectorEditResult result = utils.deleteVertex( featureId, vertex );
1508
1509 if ( result == Qgis::VectorEditResult::Success )
1510 updateExtents();
1511 return result;
1512}
1513
1514
1516{
1518
1519 if ( !isValid() || !mDataProvider || !( mDataProvider->capabilities() & Qgis::VectorProviderCapability::DeleteFeatures ) )
1520 {
1521 return false;
1522 }
1523
1524 if ( !isEditable() )
1525 {
1526 return false;
1527 }
1528
1529 int deleted = 0;
1530 int count = mSelectedFeatureIds.size();
1531 // Make a copy since deleteFeature modifies mSelectedFeatureIds
1532 QgsFeatureIds selectedFeatures( mSelectedFeatureIds );
1533 for ( QgsFeatureId fid : std::as_const( selectedFeatures ) )
1534 {
1535 deleted += deleteFeature( fid, context ); // removes from selection
1536 }
1537
1539 updateExtents();
1540
1541 if ( deletedCount )
1542 {
1543 *deletedCount = deleted;
1544 }
1545
1546 return deleted == count;
1547}
1548
1549static const QgsPointSequence vectorPointXY2pointSequence( const QVector<QgsPointXY> &points )
1550{
1551 QgsPointSequence pts;
1552 pts.reserve( points.size() );
1553 QVector<QgsPointXY>::const_iterator it = points.constBegin();
1554 while ( it != points.constEnd() )
1555 {
1556 pts.append( QgsPoint( *it ) );
1557 ++it;
1558 }
1559 return pts;
1560}
1561Qgis::GeometryOperationResult QgsVectorLayer::addRing( const QVector<QgsPointXY> &ring, QgsFeatureId *featureId )
1562{
1564
1565 return addRing( vectorPointXY2pointSequence( ring ), featureId );
1566}
1567
1569{
1571
1572 if ( !isValid() || !mEditBuffer || !mDataProvider )
1574
1575 QgsVectorLayerEditUtils utils( this );
1577
1578 //first try with selected features
1579 if ( !mSelectedFeatureIds.isEmpty() )
1580 {
1581 result = utils.addRing( ring, mSelectedFeatureIds, featureId );
1582 }
1583
1585 {
1586 //try with all intersecting features
1587 result = utils.addRing( ring, QgsFeatureIds(), featureId );
1588 }
1589
1590 return result;
1591}
1592
1594{
1596
1597 if ( !isValid() || !mEditBuffer || !mDataProvider )
1598 {
1599 delete ring;
1601 }
1602
1603 if ( !ring )
1604 {
1606 }
1607
1608 if ( !ring->isClosed() )
1609 {
1610 delete ring;
1612 }
1613
1614 QgsVectorLayerEditUtils utils( this );
1616
1617 //first try with selected features
1618 if ( !mSelectedFeatureIds.isEmpty() )
1619 {
1620 result = utils.addRing( static_cast< QgsCurve * >( ring->clone() ), mSelectedFeatureIds, featureId );
1621 }
1622
1624 {
1625 //try with all intersecting features
1626 result = utils.addRing( static_cast< QgsCurve * >( ring->clone() ), QgsFeatureIds(), featureId );
1627 }
1628
1629 delete ring;
1630 return result;
1631}
1632
1634{
1636
1637 QgsPointSequence pts;
1638 pts.reserve( points.size() );
1639 for ( QList<QgsPointXY>::const_iterator it = points.constBegin(); it != points.constEnd() ; ++it )
1640 {
1641 pts.append( QgsPoint( *it ) );
1642 }
1643 return addPart( pts );
1644}
1645
1646#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1647Qgis::GeometryOperationResult QgsVectorLayer::addPart( const QVector<QgsPointXY> &points )
1648{
1650
1651 return addPart( vectorPointXY2pointSequence( points ) );
1652}
1653#endif
1654
1656{
1658
1659 if ( !isValid() || !mEditBuffer || !mDataProvider )
1661
1662 //number of selected features must be 1
1663
1664 if ( mSelectedFeatureIds.empty() )
1665 {
1666 QgsDebugMsgLevel( QStringLiteral( "Number of selected features <1" ), 3 );
1668 }
1669 else if ( mSelectedFeatureIds.size() > 1 )
1670 {
1671 QgsDebugMsgLevel( QStringLiteral( "Number of selected features >1" ), 3 );
1673 }
1674
1675 QgsVectorLayerEditUtils utils( this );
1676 Qgis::GeometryOperationResult result = utils.addPart( points, *mSelectedFeatureIds.constBegin() );
1677
1679 updateExtents();
1680 return result;
1681}
1682
1684{
1686
1687 if ( !isValid() || !mEditBuffer || !mDataProvider )
1689
1690 //number of selected features must be 1
1691
1692 if ( mSelectedFeatureIds.empty() )
1693 {
1694 QgsDebugMsgLevel( QStringLiteral( "Number of selected features <1" ), 3 );
1696 }
1697 else if ( mSelectedFeatureIds.size() > 1 )
1698 {
1699 QgsDebugMsgLevel( QStringLiteral( "Number of selected features >1" ), 3 );
1701 }
1702
1703 QgsVectorLayerEditUtils utils( this );
1704 Qgis::GeometryOperationResult result = utils.addPart( ring, *mSelectedFeatureIds.constBegin() );
1705
1707 updateExtents();
1708 return result;
1709}
1710
1711// TODO QGIS 4.0 -- this should return Qgis::GeometryOperationResult, not int
1712int QgsVectorLayer::translateFeature( QgsFeatureId featureId, double dx, double dy )
1713{
1715
1716 if ( !isValid() || !mEditBuffer || !mDataProvider )
1717 return static_cast< int >( Qgis::GeometryOperationResult::LayerNotEditable );
1718
1719 QgsVectorLayerEditUtils utils( this );
1720 int result = utils.translateFeature( featureId, dx, dy );
1721
1722 if ( result == static_cast< int >( Qgis::GeometryOperationResult::Success ) )
1723 updateExtents();
1724 return result;
1725}
1726
1727Qgis::GeometryOperationResult QgsVectorLayer::splitParts( const QVector<QgsPointXY> &splitLine, bool topologicalEditing )
1728{
1730
1731 return splitParts( vectorPointXY2pointSequence( splitLine ), topologicalEditing );
1732}
1733
1735{
1737
1738 if ( !isValid() || !mEditBuffer || !mDataProvider )
1740
1741 QgsVectorLayerEditUtils utils( this );
1742 return utils.splitParts( splitLine, topologicalEditing );
1743}
1744
1745Qgis::GeometryOperationResult QgsVectorLayer::splitFeatures( const QVector<QgsPointXY> &splitLine, bool topologicalEditing )
1746{
1748
1749 return splitFeatures( vectorPointXY2pointSequence( splitLine ), topologicalEditing );
1750}
1751
1753{
1755
1756 QgsLineString splitLineString( splitLine );
1757 QgsPointSequence topologyTestPoints;
1758 bool preserveCircular = false;
1759 return splitFeatures( &splitLineString, topologyTestPoints, preserveCircular, topologicalEditing );
1760}
1761
1762Qgis::GeometryOperationResult QgsVectorLayer::splitFeatures( const QgsCurve *curve, QgsPointSequence &topologyTestPoints, bool preserveCircular, bool topologicalEditing )
1763{
1765
1766 if ( !isValid() || !mEditBuffer || !mDataProvider )
1768
1769 QgsVectorLayerEditUtils utils( this );
1770 return utils.splitFeatures( curve, topologyTestPoints, preserveCircular, topologicalEditing );
1771}
1772
1774{
1776
1777 if ( !isValid() || !mEditBuffer || !mDataProvider )
1778 return -1;
1779
1780 QgsVectorLayerEditUtils utils( this );
1781 return utils.addTopologicalPoints( geom );
1782}
1783
1790
1792{
1794
1795 if ( !isValid() || !mEditBuffer || !mDataProvider )
1796 return -1;
1797
1798 QgsVectorLayerEditUtils utils( this );
1799 return utils.addTopologicalPoints( p );
1800}
1801
1803{
1805
1806 if ( !mValid || !mEditBuffer || !mDataProvider )
1807 return -1;
1808
1809 QgsVectorLayerEditUtils utils( this );
1810 return utils.addTopologicalPoints( ps );
1811}
1812
1814{
1816
1817 if ( mLabeling == labeling )
1818 return;
1819
1820 delete mLabeling;
1821 mLabeling = labeling;
1822}
1823
1825{
1827
1828 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
1829 return project()->startEditing( this );
1830
1831 if ( !isValid() || !mDataProvider )
1832 {
1833 return false;
1834 }
1835
1836 // allow editing if provider supports any of the capabilities
1837 if ( !supportsEditing() )
1838 {
1839 return false;
1840 }
1841
1842 if ( mEditBuffer )
1843 {
1844 // editing already underway
1845 return false;
1846 }
1847
1848 mDataProvider->enterUpdateMode();
1849
1850 emit beforeEditingStarted();
1851
1852 createEditBuffer();
1853
1854 updateFields();
1855
1856 emit editingStarted();
1857
1858 return true;
1859}
1860
1862{
1864
1865 if ( mDataProvider )
1866 mDataProvider->setTransformContext( transformContext );
1867}
1868
1875
1877{
1879
1880 if ( mRenderer )
1881 if ( !mRenderer->accept( visitor ) )
1882 return false;
1883
1884 if ( mLabeling )
1885 if ( !mLabeling->accept( visitor ) )
1886 return false;
1887
1888 return true;
1889}
1890
1891bool QgsVectorLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
1892{
1894
1895 QgsDebugMsgLevel( QStringLiteral( "Datasource in QgsVectorLayer::readXml: %1" ).arg( mDataSource.toLocal8Bit().data() ), 3 );
1896
1897 //process provider key
1898 QDomNode pkeyNode = layer_node.namedItem( QStringLiteral( "provider" ) );
1899
1900 if ( pkeyNode.isNull() )
1901 {
1902 mProviderKey.clear();
1903 }
1904 else
1905 {
1906 QDomElement pkeyElt = pkeyNode.toElement();
1907 mProviderKey = pkeyElt.text();
1908 }
1909
1910 // determine type of vector layer
1911 if ( !mProviderKey.isNull() )
1912 {
1913 // if the provider string isn't empty, then we successfully
1914 // got the stored provider
1915 }
1916 else if ( mDataSource.contains( QLatin1String( "dbname=" ) ) )
1917 {
1918 mProviderKey = QStringLiteral( "postgres" );
1919 }
1920 else
1921 {
1922 mProviderKey = QStringLiteral( "ogr" );
1923 }
1924
1925 const QDomElement elem = layer_node.toElement();
1927
1928 mDataSourceReadOnly = mReadFlags & QgsMapLayer::FlagForceReadOnly;
1930
1931 if ( ( mReadFlags & QgsMapLayer::FlagDontResolveLayers ) || !setDataProvider( mProviderKey, options, flags ) )
1932 {
1934 {
1935 QgsDebugError( QStringLiteral( "Could not set data provider for layer %1" ).arg( publicSource() ) );
1936 }
1937
1938 // for invalid layer sources, we fallback to stored wkbType if available
1939 if ( elem.hasAttribute( QStringLiteral( "wkbType" ) ) )
1940 mWkbType = qgsEnumKeyToValue( elem.attribute( QStringLiteral( "wkbType" ) ), mWkbType );
1941 }
1942
1943 QDomElement pkeyElem = pkeyNode.toElement();
1944 if ( !pkeyElem.isNull() )
1945 {
1946 QString encodingString = pkeyElem.attribute( QStringLiteral( "encoding" ) );
1947 if ( mDataProvider && !encodingString.isEmpty() )
1948 {
1949 mDataProvider->setEncoding( encodingString );
1950 }
1951 }
1952
1953 // load vector joins - does not resolve references to layers yet
1954 mJoinBuffer->readXml( layer_node );
1955
1956 updateFields();
1957
1958 // If style doesn't include a legend, we'll need to make a default one later...
1959 mSetLegendFromStyle = false;
1960
1961 QString errorMsg;
1962 if ( !readSymbology( layer_node, errorMsg, context ) )
1963 {
1964 return false;
1965 }
1966
1967 readStyleManager( layer_node );
1968
1969 QDomNode depsNode = layer_node.namedItem( QStringLiteral( "dataDependencies" ) );
1970 QDomNodeList depsNodes = depsNode.childNodes();
1971 QSet<QgsMapLayerDependency> sources;
1972 for ( int i = 0; i < depsNodes.count(); i++ )
1973 {
1974 QString source = depsNodes.at( i ).toElement().attribute( QStringLiteral( "id" ) );
1975 sources << QgsMapLayerDependency( source );
1976 }
1977 setDependencies( sources );
1978
1979 if ( !mSetLegendFromStyle )
1981
1982 // read extent
1984 {
1985 mReadExtentFromXml = true;
1986 }
1987 if ( mReadExtentFromXml )
1988 {
1989 const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
1990 if ( !extentNode.isNull() )
1991 {
1992 mXmlExtent2D = QgsXmlUtils::readRectangle( extentNode.toElement() );
1993 }
1994 const QDomNode extent3DNode = layer_node.namedItem( QStringLiteral( "extent3D" ) );
1995 if ( !extent3DNode.isNull() )
1996 {
1997 mXmlExtent3D = QgsXmlUtils::readBox3D( extent3DNode.toElement() );
1998 }
1999 }
2000
2001 // auxiliary layer
2002 const QDomNode asNode = layer_node.namedItem( QStringLiteral( "auxiliaryLayer" ) );
2003 const QDomElement asElem = asNode.toElement();
2004 if ( !asElem.isNull() )
2005 {
2006 mAuxiliaryLayerKey = asElem.attribute( QStringLiteral( "key" ) );
2007 }
2008
2009 // QGIS Server WMS Dimensions
2010 mServerProperties->readXml( layer_node );
2011
2012 return isValid(); // should be true if read successfully
2013
2014} // void QgsVectorLayer::readXml
2015
2016
2017void QgsVectorLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
2019{
2021
2022 Qgis::GeometryType geomType = geometryType();
2023
2024 mDataSource = dataSource;
2025 setName( baseName );
2026 setDataProvider( provider, options, flags );
2027
2028 if ( !isValid() )
2029 {
2030 return;
2031 }
2032
2033 // Always set crs
2035
2036 bool loadDefaultStyleFlag = false;
2038 {
2039 loadDefaultStyleFlag = true;
2040 }
2041
2042 // reset style if loading default style, style is missing, or geometry type is has changed (and layer is valid)
2043 if ( !renderer() || !legend() || ( isValid() && geomType != geometryType() ) || loadDefaultStyleFlag )
2044 {
2045 std::unique_ptr< QgsScopedRuntimeProfile > profile;
2046 if ( QgsApplication::profiler()->groupIsActive( QStringLiteral( "projectload" ) ) )
2047 profile = std::make_unique< QgsScopedRuntimeProfile >( tr( "Load layer style" ), QStringLiteral( "projectload" ) );
2048
2049 bool defaultLoadedFlag = false;
2050
2051 // defer style changed signal until we've set the renderer, labeling, everything.
2052 // we don't want multiple signals!
2053 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
2054
2055 // need to check whether the default style included a legend, and if not, we need to make a default legend
2056 // later...
2057 mSetLegendFromStyle = false;
2058
2059 // first check if there is a default style / propertysheet defined
2060 // for this layer and if so apply it
2061 // this should take precedence over all
2062 if ( !defaultLoadedFlag && loadDefaultStyleFlag )
2063 {
2064 loadDefaultStyle( defaultLoadedFlag );
2065 }
2066
2067 if ( loadDefaultStyleFlag && !defaultLoadedFlag && isSpatial() && mDataProvider->capabilities() & Qgis::VectorProviderCapability::CreateRenderer )
2068 {
2069 // if we didn't load a default style for this layer, try to create a renderer directly from the data provider
2070 std::unique_ptr< QgsFeatureRenderer > defaultRenderer( mDataProvider->createRenderer() );
2071 if ( defaultRenderer )
2072 {
2073 defaultLoadedFlag = true;
2074 setRenderer( defaultRenderer.release() );
2075 }
2076 }
2077
2078 // if the default style failed to load or was disabled use some very basic defaults
2079 if ( !defaultLoadedFlag )
2080 {
2081 // add single symbol renderer for spatial layers
2083 }
2084
2085 if ( !mSetLegendFromStyle )
2087
2089 {
2090 std::unique_ptr< QgsAbstractVectorLayerLabeling > defaultLabeling( mDataProvider->createLabeling() );
2091 if ( defaultLabeling )
2092 {
2093 setLabeling( defaultLabeling.release() );
2094 setLabelsEnabled( true );
2095 }
2096 }
2097
2098 styleChangedSignalBlocker.release();
2100 }
2101}
2102
2103QString QgsVectorLayer::loadDefaultStyle( bool &resultFlag )
2104{
2106
2107 // first try to load a user-defined default style - this should always take precedence
2108 QString styleXml = QgsMapLayer::loadDefaultStyle( resultFlag );
2109
2110 if ( resultFlag )
2111 {
2112 // Try to load all stored styles from DB
2113 if ( mLoadAllStoredStyle && mDataProvider && mDataProvider->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2114 {
2115 QStringList ids, names, descriptions;
2116 QString errorMessage;
2117 // Get the number of styles related to current layer.
2118 const int relatedStylesCount { listStylesInDatabase( ids, names, descriptions, errorMessage ) };
2119 Q_ASSERT( ids.count() == names.count() );
2120 const QString currentStyleName { mStyleManager->currentStyle() };
2121 for ( int i = 0; i < relatedStylesCount; ++i )
2122 {
2123 if ( names.at( i ) == currentStyleName )
2124 {
2125 continue;
2126 }
2127 errorMessage.clear();
2128 const QString styleXml { getStyleFromDatabase( ids.at( i ), errorMessage ) };
2129 if ( ! styleXml.isEmpty() && errorMessage.isEmpty() )
2130 {
2131 mStyleManager->addStyle( names.at( i ), QgsMapLayerStyle( styleXml ) );
2132 }
2133 else
2134 {
2135 QgsDebugMsgLevel( QStringLiteral( "Error retrieving style %1 from DB: %2" ).arg( ids.at( i ), errorMessage ), 2 );
2136 }
2137 }
2138 }
2139 return styleXml ;
2140 }
2141
2143 {
2144 // otherwise try to create a renderer directly from the data provider
2145 std::unique_ptr< QgsFeatureRenderer > defaultRenderer( mDataProvider->createRenderer() );
2146 if ( defaultRenderer )
2147 {
2148 resultFlag = true;
2149 setRenderer( defaultRenderer.release() );
2150 return QString();
2151 }
2152 }
2153
2154 return QString();
2155}
2156
2157bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProvider::ProviderOptions &options, Qgis::DataProviderReadFlags flags )
2158{
2160
2161 mProviderKey = provider;
2162 delete mDataProvider;
2163
2164 // For Postgres provider primary key unicity is tested at construction time,
2165 // so it has to be set before initializing the provider,
2166 // this manipulation is necessary to preserve default behavior when
2167 // "trust layer metadata" project level option is set and checkPrimaryKeyUnicity
2168 // was not explicitly passed in the uri
2169 if ( provider.compare( QLatin1String( "postgres" ) ) == 0 )
2170 {
2171 const QString checkUnicityKey { QStringLiteral( "checkPrimaryKeyUnicity" ) };
2173 if ( ! uri.hasParam( checkUnicityKey ) )
2174 {
2175 uri.setParam( checkUnicityKey, mReadExtentFromXml ? "0" : "1" );
2176 mDataSource = uri.uri( false );
2177 }
2178 }
2179
2180 std::unique_ptr< QgsScopedRuntimeProfile > profile;
2181 if ( QgsApplication::profiler()->groupIsActive( QStringLiteral( "projectload" ) ) )
2182 profile = std::make_unique< QgsScopedRuntimeProfile >( tr( "Create %1 provider" ).arg( provider ), QStringLiteral( "projectload" ) );
2183
2184 if ( mPreloadedProvider )
2185 mDataProvider = qobject_cast< QgsVectorDataProvider * >( mPreloadedProvider.release() );
2186 else
2187 mDataProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, mDataSource, options, flags ) );
2188
2189 if ( !mDataProvider )
2190 {
2191 setValid( false );
2192 QgsDebugMsgLevel( QStringLiteral( "Unable to get data provider" ), 2 );
2193 return false;
2194 }
2195
2196 mDataProvider->setParent( this );
2197 connect( mDataProvider, &QgsVectorDataProvider::raiseError, this, &QgsVectorLayer::raiseError );
2198
2199 QgsDebugMsgLevel( QStringLiteral( "Instantiated the data provider plugin" ), 2 );
2200
2201 setValid( mDataProvider->isValid() );
2202 if ( !isValid() )
2203 {
2204 QgsDebugMsgLevel( QStringLiteral( "Invalid provider plugin %1" ).arg( QString( mDataSource.toUtf8() ) ), 2 );
2205 return false;
2206 }
2207
2208 if ( profile )
2209 profile->switchTask( tr( "Read layer metadata" ) );
2211 {
2212 // we combine the provider metadata with the layer's existing metadata, so as not to reset any user customizations to the metadata
2213 // back to the default if a layer's data source is changed
2214 QgsLayerMetadata newMetadata = mDataProvider->layerMetadata();
2215 // this overwrites the provider metadata with any properties which are non-empty from the existing layer metadata
2216 newMetadata.combine( &mMetadata );
2217
2218 setMetadata( newMetadata );
2219 QgsDebugMsgLevel( QStringLiteral( "Set Data provider QgsLayerMetadata identifier[%1]" ).arg( metadata().identifier() ), 4 );
2220 }
2221
2222 // TODO: Check if the provider has the capability to send fullExtentCalculated
2223 connect( mDataProvider, &QgsVectorDataProvider::fullExtentCalculated, this, [this] { updateExtents(); } );
2224
2225 // get and store the feature type
2226 mWkbType = mDataProvider->wkbType();
2227
2228 // before we update the layer fields from the provider, we first copy any default set alias and
2229 // editor widget config from the data provider fields, if present
2230 const QgsFields providerFields = mDataProvider->fields();
2231 for ( const QgsField &field : providerFields )
2232 {
2233 // we only copy defaults from the provider if we aren't overriding any configuration made in the layer
2234 if ( !field.editorWidgetSetup().isNull() && mFieldWidgetSetups.value( field.name() ).isNull() )
2235 {
2236 mFieldWidgetSetups[ field.name() ] = field.editorWidgetSetup();
2237 }
2238 if ( !field.alias().isEmpty() && mAttributeAliasMap.value( field.name() ).isEmpty() )
2239 {
2240 mAttributeAliasMap[ field.name() ] = field.alias();
2241 }
2242 if ( !mAttributeSplitPolicy.contains( field.name() ) )
2243 {
2244 mAttributeSplitPolicy[ field.name() ] = field.splitPolicy();
2245 }
2246 if ( !mAttributeDuplicatePolicy.contains( field.name() ) )
2247 {
2248 mAttributeDuplicatePolicy[ field.name() ] = field.duplicatePolicy();
2249 }
2250 }
2251
2252 if ( profile )
2253 profile->switchTask( tr( "Read layer fields" ) );
2254 updateFields();
2255
2256 if ( mProviderKey == QLatin1String( "postgres" ) )
2257 {
2258 // update datasource from data provider computed one
2259 mDataSource = mDataProvider->dataSourceUri( false );
2260
2261 QgsDebugMsgLevel( QStringLiteral( "Beautifying layer name %1" ).arg( name() ), 3 );
2262
2263 // adjust the display name for postgres layers
2264 const thread_local QRegularExpression reg( R"lit("[^"]+"\."([^"] + )"( \‍([^)]+\))?)lit" );
2265 const QRegularExpressionMatch match = reg.match( name() );
2266 if ( match.hasMatch() )
2267 {
2268 QStringList stuff = match.capturedTexts();
2269 QString lName = stuff[1];
2270
2271 const QMap<QString, QgsMapLayer *> &layers = QgsProject::instance()->mapLayers(); // skip-keyword-check
2272
2273 QMap<QString, QgsMapLayer *>::const_iterator it;
2274 for ( it = layers.constBegin(); it != layers.constEnd() && ( *it )->name() != lName; ++it )
2275 ;
2276
2277 if ( it != layers.constEnd() && stuff.size() > 2 )
2278 {
2279 lName += '.' + stuff[2].mid( 2, stuff[2].length() - 3 );
2280 }
2281
2282 if ( !lName.isEmpty() )
2283 setName( lName );
2284 }
2285 QgsDebugMsgLevel( QStringLiteral( "Beautified layer name %1" ).arg( name() ), 3 );
2286 }
2287 else if ( mProviderKey == QLatin1String( "osm" ) )
2288 {
2289 // make sure that the "observer" has been removed from URI to avoid crashes
2290 mDataSource = mDataProvider->dataSourceUri();
2291 }
2292 else if ( provider == QLatin1String( "ogr" ) )
2293 {
2294 // make sure that the /vsigzip or /vsizip is added to uri, if applicable
2295 mDataSource = mDataProvider->dataSourceUri();
2296 if ( mDataSource.right( 10 ) == QLatin1String( "|layerid=0" ) )
2297 mDataSource.chop( 10 );
2298 }
2299 else if ( provider == QLatin1String( "memory" ) )
2300 {
2301 // required so that source differs between memory layers
2302 mDataSource = mDataSource + QStringLiteral( "&uid=%1" ).arg( QUuid::createUuid().toString() );
2303 }
2304 else if ( provider == QLatin1String( "hana" ) )
2305 {
2306 // update datasource from data provider computed one
2307 mDataSource = mDataProvider->dataSourceUri( false );
2308 }
2309
2310 connect( mDataProvider, &QgsVectorDataProvider::dataChanged, this, &QgsVectorLayer::emitDataChanged );
2312
2313 return true;
2314} // QgsVectorLayer:: setDataProvider
2315
2316
2317
2318
2319/* virtual */
2320bool QgsVectorLayer::writeXml( QDomNode &layer_node,
2321 QDomDocument &document,
2322 const QgsReadWriteContext &context ) const
2323{
2325
2326 // first get the layer element so that we can append the type attribute
2327
2328 QDomElement mapLayerNode = layer_node.toElement();
2329
2330 if ( mapLayerNode.isNull() || ( "maplayer" != mapLayerNode.nodeName() ) )
2331 {
2332 QgsDebugMsgLevel( QStringLiteral( "can't find <maplayer>" ), 2 );
2333 return false;
2334 }
2335
2336 mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( Qgis::LayerType::Vector ) );
2337
2338 // set the geometry type
2339 mapLayerNode.setAttribute( QStringLiteral( "geometry" ), QgsWkbTypes::geometryDisplayString( geometryType() ) );
2340 mapLayerNode.setAttribute( QStringLiteral( "wkbType" ), qgsEnumValueToKey( wkbType() ) );
2341
2342 // add provider node
2343 if ( mDataProvider )
2344 {
2345 QDomElement provider = document.createElement( QStringLiteral( "provider" ) );
2346 provider.setAttribute( QStringLiteral( "encoding" ), mDataProvider->encoding() );
2347 QDomText providerText = document.createTextNode( providerType() );
2348 provider.appendChild( providerText );
2349 layer_node.appendChild( provider );
2350 }
2351
2352 //save joins
2353 mJoinBuffer->writeXml( layer_node, document );
2354
2355 // dependencies
2356 QDomElement dependenciesElement = document.createElement( QStringLiteral( "layerDependencies" ) );
2357 const auto constDependencies = dependencies();
2358 for ( const QgsMapLayerDependency &dep : constDependencies )
2359 {
2361 continue;
2362 QDomElement depElem = document.createElement( QStringLiteral( "layer" ) );
2363 depElem.setAttribute( QStringLiteral( "id" ), dep.layerId() );
2364 dependenciesElement.appendChild( depElem );
2365 }
2366 layer_node.appendChild( dependenciesElement );
2367
2368 // change dependencies
2369 QDomElement dataDependenciesElement = document.createElement( QStringLiteral( "dataDependencies" ) );
2370 for ( const QgsMapLayerDependency &dep : constDependencies )
2371 {
2372 if ( dep.type() != QgsMapLayerDependency::DataDependency )
2373 continue;
2374 QDomElement depElem = document.createElement( QStringLiteral( "layer" ) );
2375 depElem.setAttribute( QStringLiteral( "id" ), dep.layerId() );
2376 dataDependenciesElement.appendChild( depElem );
2377 }
2378 layer_node.appendChild( dataDependenciesElement );
2379
2380 // save expression fields
2381 mExpressionFieldBuffer->writeXml( layer_node, document );
2382
2383 writeStyleManager( layer_node, document );
2384
2385 // auxiliary layer
2386 QDomElement asElem = document.createElement( QStringLiteral( "auxiliaryLayer" ) );
2387 if ( mAuxiliaryLayer )
2388 {
2389 const QString pkField = mAuxiliaryLayer->joinInfo().targetFieldName();
2390 asElem.setAttribute( QStringLiteral( "key" ), pkField );
2391 }
2392 layer_node.appendChild( asElem );
2393
2394 // save QGIS Server properties (WMS Dimension, metadata URLS...)
2395 mServerProperties->writeXml( layer_node, document );
2396
2397 // renderer specific settings
2398 QString errorMsg;
2399 return writeSymbology( layer_node, document, errorMsg, context );
2400}
2401
2402QString QgsVectorLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
2403{
2405
2406 if ( providerType() == QLatin1String( "memory" ) )
2407 {
2408 // Refetch the source from the provider, because adding fields actually changes the source for this provider.
2409 return dataProvider()->dataSourceUri();
2410 }
2411
2413}
2414
2415QString QgsVectorLayer::decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const
2416{
2418
2419 return QgsProviderRegistry::instance()->relativeToAbsoluteUri( provider, source, context );
2420}
2421
2422
2423
2431
2432
2433bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMessage,
2435{
2437
2438 if ( categories.testFlag( Fields ) )
2439 {
2440 if ( !mExpressionFieldBuffer )
2441 mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
2442 mExpressionFieldBuffer->readXml( layerNode );
2443
2444 updateFields();
2445 }
2446
2447 if ( categories.testFlag( Relations ) )
2448 {
2449 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Relations" ) );
2450
2451 // Restore referenced layers: relations where "this" is the child layer (the referencing part, that holds the FK)
2452 QDomNodeList referencedLayersNodeList = layerNode.toElement().elementsByTagName( QStringLiteral( "referencedLayers" ) );
2453 if ( referencedLayersNodeList.size() > 0 )
2454 {
2455 const QDomNodeList relationNodes { referencedLayersNodeList.at( 0 ).childNodes() };
2456 for ( int i = 0; i < relationNodes.length(); ++i )
2457 {
2458 const QDomElement relationElement = relationNodes.at( i ).toElement();
2459
2460 mWeakRelations.push_back( QgsWeakRelation::readXml( this, QgsWeakRelation::Referencing, relationElement, context.pathResolver() ) );
2461 }
2462 }
2463
2464 // Restore referencing layers: relations where "this" is the parent layer (the referenced part where the FK points to)
2465 QDomNodeList referencingLayersNodeList = layerNode.toElement().elementsByTagName( QStringLiteral( "referencingLayers" ) );
2466 if ( referencingLayersNodeList.size() > 0 )
2467 {
2468 const QDomNodeList relationNodes { referencingLayersNodeList.at( 0 ).childNodes() };
2469 for ( int i = 0; i < relationNodes.length(); ++i )
2470 {
2471 const QDomElement relationElement = relationNodes.at( i ).toElement();
2472 mWeakRelations.push_back( QgsWeakRelation::readXml( this, QgsWeakRelation::Referenced, relationElement, context.pathResolver() ) );
2473 }
2474 }
2475 }
2476
2477 QDomElement layerElement = layerNode.toElement();
2478
2479 readCommonStyle( layerElement, context, categories );
2480
2481 readStyle( layerNode, errorMessage, context, categories );
2482
2483 if ( categories.testFlag( MapTips ) )
2484 {
2485 QDomElement mapTipElem = layerNode.namedItem( QStringLiteral( "mapTip" ) ).toElement();
2486 setMapTipTemplate( mapTipElem.text() );
2487 setMapTipsEnabled( mapTipElem.attribute( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt() == 1 );
2488 }
2489
2490 if ( categories.testFlag( LayerConfiguration ) )
2491 mDisplayExpression = layerNode.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text();
2492
2493 // Try to migrate pre QGIS 3.0 display field property
2494 QString displayField = layerNode.namedItem( QStringLiteral( "displayfield" ) ).toElement().text();
2495 if ( mFields.lookupField( displayField ) < 0 )
2496 {
2497 // if it's not a field, it's a maptip
2498 if ( mMapTipTemplate.isEmpty() && categories.testFlag( MapTips ) )
2499 mMapTipTemplate = displayField;
2500 }
2501 else
2502 {
2503 if ( mDisplayExpression.isEmpty() && categories.testFlag( LayerConfiguration ) )
2504 mDisplayExpression = QgsExpression::quotedColumnRef( displayField );
2505 }
2506
2507 // process the attribute actions
2508 if ( categories.testFlag( Actions ) )
2509 mActions->readXml( layerNode );
2510
2511 if ( categories.testFlag( Fields ) )
2512 {
2513 // IMPORTANT - we don't clear mAttributeAliasMap here, as it may contain aliases which are coming direct
2514 // from the data provider. Instead we leave any existing aliases and only overwrite them if the style
2515 // has a specific value for that field's alias
2516 QDomNode aliasesNode = layerNode.namedItem( QStringLiteral( "aliases" ) );
2517 if ( !aliasesNode.isNull() )
2518 {
2519 QDomElement aliasElem;
2520
2521 QDomNodeList aliasNodeList = aliasesNode.toElement().elementsByTagName( QStringLiteral( "alias" ) );
2522 for ( int i = 0; i < aliasNodeList.size(); ++i )
2523 {
2524 aliasElem = aliasNodeList.at( i ).toElement();
2525
2526 QString field;
2527 if ( aliasElem.hasAttribute( QStringLiteral( "field" ) ) )
2528 {
2529 field = aliasElem.attribute( QStringLiteral( "field" ) );
2530 }
2531 else
2532 {
2533 int index = aliasElem.attribute( QStringLiteral( "index" ) ).toInt();
2534
2535 if ( index >= 0 && index < fields().count() )
2536 field = fields().at( index ).name();
2537 }
2538
2539 QString alias;
2540
2541 if ( !aliasElem.attribute( QStringLiteral( "name" ) ).isEmpty() )
2542 {
2543 //if it has alias
2544 alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
2545 QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + aliasElem.attribute( QStringLiteral( "name" ) ), 3 );
2546 }
2547 else
2548 {
2549 //if it has no alias, it should be the fields translation
2550 alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
2551 QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + field, 3 );
2552 //if it gets the exact field value, there has been no translation (or not even translation loaded) - so no alias should be generated;
2553 if ( alias == aliasElem.attribute( QStringLiteral( "field" ) ) )
2554 alias.clear();
2555 }
2556
2557 QgsDebugMsgLevel( "field " + field + " origalias " + aliasElem.attribute( QStringLiteral( "name" ) ) + " trans " + alias, 3 );
2558 mAttributeAliasMap.insert( field, alias );
2559 }
2560 }
2561
2562 // IMPORTANT - we don't clear mAttributeSplitPolicy here, as it may contain policies which are coming direct
2563 // from the data provider. Instead we leave any existing policies and only overwrite them if the style
2564 // has a specific value for that field's policy
2565 const QDomNode splitPoliciesNode = layerNode.namedItem( QStringLiteral( "splitPolicies" ) );
2566 if ( !splitPoliciesNode.isNull() )
2567 {
2568 const QDomNodeList splitPolicyNodeList = splitPoliciesNode.toElement().elementsByTagName( QStringLiteral( "policy" ) );
2569 for ( int i = 0; i < splitPolicyNodeList.size(); ++i )
2570 {
2571 const QDomElement splitPolicyElem = splitPolicyNodeList.at( i ).toElement();
2572 const QString field = splitPolicyElem.attribute( QStringLiteral( "field" ) );
2573 const Qgis::FieldDomainSplitPolicy policy = qgsEnumKeyToValue( splitPolicyElem.attribute( QStringLiteral( "policy" ) ), Qgis::FieldDomainSplitPolicy::Duplicate );
2574 mAttributeSplitPolicy.insert( field, policy );
2575 }
2576 }
2577
2578 // The duplicate policy is - unlike alias and split policy - never defined by the data provider, so we clear the map
2579 mAttributeDuplicatePolicy.clear();
2580 const QDomNode duplicatePoliciesNode = layerNode.namedItem( QStringLiteral( "duplicatePolicies" ) );
2581 if ( !duplicatePoliciesNode.isNull() )
2582 {
2583 const QDomNodeList duplicatePolicyNodeList = duplicatePoliciesNode.toElement().elementsByTagName( QStringLiteral( "policy" ) );
2584 for ( int i = 0; i < duplicatePolicyNodeList.size(); ++i )
2585 {
2586 const QDomElement duplicatePolicyElem = duplicatePolicyNodeList.at( i ).toElement();
2587 const QString field = duplicatePolicyElem.attribute( QStringLiteral( "field" ) );
2588 const Qgis::FieldDuplicatePolicy policy = qgsEnumKeyToValue( duplicatePolicyElem.attribute( QStringLiteral( "policy" ) ), Qgis::FieldDuplicatePolicy::Duplicate );
2589 mAttributeDuplicatePolicy.insert( field, policy );
2590 }
2591 }
2592
2593 // default expressions
2594 mDefaultExpressionMap.clear();
2595 QDomNode defaultsNode = layerNode.namedItem( QStringLiteral( "defaults" ) );
2596 if ( !defaultsNode.isNull() )
2597 {
2598 QDomNodeList defaultNodeList = defaultsNode.toElement().elementsByTagName( QStringLiteral( "default" ) );
2599 for ( int i = 0; i < defaultNodeList.size(); ++i )
2600 {
2601 QDomElement defaultElem = defaultNodeList.at( i ).toElement();
2602
2603 QString field = defaultElem.attribute( QStringLiteral( "field" ), QString() );
2604 QString expression = defaultElem.attribute( QStringLiteral( "expression" ), QString() );
2605 bool applyOnUpdate = defaultElem.attribute( QStringLiteral( "applyOnUpdate" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
2606 if ( field.isEmpty() || expression.isEmpty() )
2607 continue;
2608
2609 mDefaultExpressionMap.insert( field, QgsDefaultValue( expression, applyOnUpdate ) );
2610 }
2611 }
2612
2613 // constraints
2614 mFieldConstraints.clear();
2615 mFieldConstraintStrength.clear();
2616 QDomNode constraintsNode = layerNode.namedItem( QStringLiteral( "constraints" ) );
2617 if ( !constraintsNode.isNull() )
2618 {
2619 QDomNodeList constraintNodeList = constraintsNode.toElement().elementsByTagName( QStringLiteral( "constraint" ) );
2620 for ( int i = 0; i < constraintNodeList.size(); ++i )
2621 {
2622 QDomElement constraintElem = constraintNodeList.at( i ).toElement();
2623
2624 QString field = constraintElem.attribute( QStringLiteral( "field" ), QString() );
2625 int constraints = constraintElem.attribute( QStringLiteral( "constraints" ), QStringLiteral( "0" ) ).toInt();
2626 if ( field.isEmpty() || constraints == 0 )
2627 continue;
2628
2629 mFieldConstraints.insert( field, static_cast< QgsFieldConstraints::Constraints >( constraints ) );
2630
2631 int uniqueStrength = constraintElem.attribute( QStringLiteral( "unique_strength" ), QStringLiteral( "1" ) ).toInt();
2632 int notNullStrength = constraintElem.attribute( QStringLiteral( "notnull_strength" ), QStringLiteral( "1" ) ).toInt();
2633 int expStrength = constraintElem.attribute( QStringLiteral( "exp_strength" ), QStringLiteral( "1" ) ).toInt();
2634
2635 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintUnique ), static_cast< QgsFieldConstraints::ConstraintStrength >( uniqueStrength ) );
2636 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintNotNull ), static_cast< QgsFieldConstraints::ConstraintStrength >( notNullStrength ) );
2637 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintExpression ), static_cast< QgsFieldConstraints::ConstraintStrength >( expStrength ) );
2638 }
2639 }
2640 mFieldConstraintExpressions.clear();
2641 QDomNode constraintExpressionsNode = layerNode.namedItem( QStringLiteral( "constraintExpressions" ) );
2642 if ( !constraintExpressionsNode.isNull() )
2643 {
2644 QDomNodeList constraintNodeList = constraintExpressionsNode.toElement().elementsByTagName( QStringLiteral( "constraint" ) );
2645 for ( int i = 0; i < constraintNodeList.size(); ++i )
2646 {
2647 QDomElement constraintElem = constraintNodeList.at( i ).toElement();
2648
2649 QString field = constraintElem.attribute( QStringLiteral( "field" ), QString() );
2650 QString exp = constraintElem.attribute( QStringLiteral( "exp" ), QString() );
2651 QString desc = constraintElem.attribute( QStringLiteral( "desc" ), QString() );
2652 if ( field.isEmpty() || exp.isEmpty() )
2653 continue;
2654
2655 mFieldConstraintExpressions.insert( field, qMakePair( exp, desc ) );
2656 }
2657 }
2658
2659 updateFields();
2660 }
2661
2662 // load field configuration
2663 if ( categories.testFlag( Fields ) || categories.testFlag( Forms ) )
2664 {
2665 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Forms" ) );
2666
2667 QDomElement widgetsElem = layerNode.namedItem( QStringLiteral( "fieldConfiguration" ) ).toElement();
2668 QDomNodeList fieldConfigurationElementList = widgetsElem.elementsByTagName( QStringLiteral( "field" ) );
2669 for ( int i = 0; i < fieldConfigurationElementList.size(); ++i )
2670 {
2671 const QDomElement fieldConfigElement = fieldConfigurationElementList.at( i ).toElement();
2672 const QDomElement fieldWidgetElement = fieldConfigElement.elementsByTagName( QStringLiteral( "editWidget" ) ).at( 0 ).toElement();
2673
2674 QString fieldName = fieldConfigElement.attribute( QStringLiteral( "name" ) );
2675
2676 if ( categories.testFlag( Fields ) )
2677 mFieldConfigurationFlags[fieldName] = qgsFlagKeysToValue( fieldConfigElement.attribute( QStringLiteral( "configurationFlags" ) ), Qgis::FieldConfigurationFlag::NoFlag );
2678
2679 // Load editor widget configuration
2680 if ( categories.testFlag( Forms ) )
2681 {
2682 const QString widgetType = fieldWidgetElement.attribute( QStringLiteral( "type" ) );
2683 const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
2684 const QDomElement optionsElem = cfgElem.childNodes().at( 0 ).toElement();
2685 QVariantMap optionsMap = QgsXmlUtils::readVariant( optionsElem ).toMap();
2686 if ( widgetType == QLatin1String( "ValueRelation" ) )
2687 {
2688 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() );
2689 }
2690 QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, optionsMap );
2691 mFieldWidgetSetups[fieldName] = setup;
2692 }
2693 }
2694 }
2695
2696 // Legacy reading for QGIS 3.14 and older projects
2697 // Attributes excluded from WMS and WFS
2698 if ( categories.testFlag( Fields ) )
2699 {
2700 const QList<QPair<QString, Qgis::FieldConfigurationFlag>> legacyConfig
2701 {
2702 qMakePair( QStringLiteral( "excludeAttributesWMS" ), Qgis::FieldConfigurationFlag::HideFromWms ),
2703 qMakePair( QStringLiteral( "excludeAttributesWFS" ), Qgis::FieldConfigurationFlag::HideFromWfs )
2704 };
2705 for ( const auto &config : legacyConfig )
2706 {
2707 QDomNode excludeNode = layerNode.namedItem( config.first );
2708 if ( !excludeNode.isNull() )
2709 {
2710 QDomNodeList attributeNodeList = excludeNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
2711 for ( int i = 0; i < attributeNodeList.size(); ++i )
2712 {
2713 QString fieldName = attributeNodeList.at( i ).toElement().text();
2714 if ( !mFieldConfigurationFlags.contains( fieldName ) )
2715 mFieldConfigurationFlags[fieldName] = config.second;
2716 else
2717 mFieldConfigurationFlags[fieldName].setFlag( config.second, true );
2718 }
2719 }
2720 }
2721 }
2722
2723 if ( categories.testFlag( GeometryOptions ) )
2724 mGeometryOptions->readXml( layerNode.namedItem( QStringLiteral( "geometryOptions" ) ) );
2725
2726 if ( categories.testFlag( Forms ) )
2727 mEditFormConfig.readXml( layerNode, context );
2728
2729 if ( categories.testFlag( AttributeTable ) )
2730 {
2731 mAttributeTableConfig.readXml( layerNode );
2732 mConditionalStyles->readXml( layerNode, context );
2733 mStoredExpressionManager->readXml( layerNode );
2734 }
2735
2736 if ( categories.testFlag( CustomProperties ) )
2737 readCustomProperties( layerNode, QStringLiteral( "variable" ) );
2738
2739 QDomElement mapLayerNode = layerNode.toElement();
2740 if ( categories.testFlag( LayerConfiguration )
2741 && mapLayerNode.attribute( QStringLiteral( "readOnly" ), QStringLiteral( "0" ) ).toInt() == 1 )
2742 mReadOnly = true;
2743
2744 updateFields();
2745
2746 if ( categories.testFlag( Legend ) )
2747 {
2748 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Legend" ) );
2749
2750 const QDomElement legendElem = layerNode.firstChildElement( QStringLiteral( "legend" ) );
2751 if ( !legendElem.isNull() )
2752 {
2753 std::unique_ptr< QgsMapLayerLegend > legend( QgsMapLayerLegend::defaultVectorLegend( this ) );
2754 legend->readXml( legendElem, context );
2755 setLegend( legend.release() );
2756 mSetLegendFromStyle = true;
2757 }
2758 }
2759
2760 return true;
2761}
2762
2763bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage,
2765{
2767
2768 bool result = true;
2769 emit readCustomSymbology( node.toElement(), errorMessage );
2770
2771 // we must try to restore a renderer if our geometry type is unknown
2772 // as this allows the renderer to be correctly restored even for layers
2773 // with broken sources
2774 if ( isSpatial() || mWkbType == Qgis::WkbType::Unknown )
2775 {
2776 // defer style changed signal until we've set the renderer, labeling, everything.
2777 // we don't want multiple signals!
2778 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
2779
2780 // try renderer v2 first
2781 if ( categories.testFlag( Symbology ) )
2782 {
2783 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Symbology" ) );
2784
2785 QDomElement rendererElement = node.firstChildElement( RENDERER_TAG_NAME );
2786 if ( !rendererElement.isNull() )
2787 {
2788 QgsFeatureRenderer *r = QgsFeatureRenderer::load( rendererElement, context );
2789 if ( r )
2790 {
2791 setRenderer( r );
2792 }
2793 else
2794 {
2795 result = false;
2796 }
2797 }
2798 // make sure layer has a renderer - if none exists, fallback to a default renderer
2799 if ( isSpatial() && !renderer() )
2800 {
2802 }
2803
2804 if ( mSelectionProperties )
2805 mSelectionProperties->readXml( node.toElement(), context );
2806 }
2807
2808 // read labeling definition
2809 if ( categories.testFlag( Labeling ) )
2810 {
2811 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Labeling" ) );
2812
2813 QDomElement labelingElement = node.firstChildElement( QStringLiteral( "labeling" ) );
2815 if ( labelingElement.isNull() ||
2816 ( labelingElement.attribute( QStringLiteral( "type" ) ) == QLatin1String( "simple" ) && labelingElement.firstChildElement( QStringLiteral( "settings" ) ).isNull() ) )
2817 {
2818 // make sure we have custom properties for labeling for 2.x projects
2819 // (custom properties should be already loaded when reading the whole layer from XML,
2820 // but when reading style, custom properties are not read)
2821 readCustomProperties( node, QStringLiteral( "labeling" ) );
2822
2823 // support for pre-QGIS 3 labeling configurations written in custom properties
2824 labeling = readLabelingFromCustomProperties();
2825 }
2826 else
2827 {
2828 labeling = QgsAbstractVectorLayerLabeling::create( labelingElement, context );
2829 }
2831
2832 if ( node.toElement().hasAttribute( QStringLiteral( "labelsEnabled" ) ) )
2833 mLabelsEnabled = node.toElement().attribute( QStringLiteral( "labelsEnabled" ) ).toInt();
2834 else
2835 mLabelsEnabled = true;
2836 }
2837
2838 if ( categories.testFlag( Symbology ) )
2839 {
2840 // get and set the blend mode if it exists
2841 QDomNode blendModeNode = node.namedItem( QStringLiteral( "blendMode" ) );
2842 if ( !blendModeNode.isNull() )
2843 {
2844 QDomElement e = blendModeNode.toElement();
2845 setBlendMode( QgsPainting::getCompositionMode( static_cast< Qgis::BlendMode >( e.text().toInt() ) ) );
2846 }
2847
2848 // get and set the feature blend mode if it exists
2849 QDomNode featureBlendModeNode = node.namedItem( QStringLiteral( "featureBlendMode" ) );
2850 if ( !featureBlendModeNode.isNull() )
2851 {
2852 QDomElement e = featureBlendModeNode.toElement();
2853 setFeatureBlendMode( QgsPainting::getCompositionMode( static_cast< Qgis::BlendMode >( e.text().toInt() ) ) );
2854 }
2855 }
2856
2857 // get and set the layer transparency and scale visibility if they exists
2858 if ( categories.testFlag( Rendering ) )
2859 {
2860 QDomNode layerTransparencyNode = node.namedItem( QStringLiteral( "layerTransparency" ) );
2861 if ( !layerTransparencyNode.isNull() )
2862 {
2863 QDomElement e = layerTransparencyNode.toElement();
2864 setOpacity( 1.0 - e.text().toInt() / 100.0 );
2865 }
2866 QDomNode layerOpacityNode = node.namedItem( QStringLiteral( "layerOpacity" ) );
2867 if ( !layerOpacityNode.isNull() )
2868 {
2869 QDomElement e = layerOpacityNode.toElement();
2870 setOpacity( e.text().toDouble() );
2871 }
2872
2873 const bool hasScaleBasedVisibiliy { node.attributes().namedItem( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).nodeValue() == '1' };
2874 setScaleBasedVisibility( hasScaleBasedVisibiliy );
2875 bool ok;
2876 const double maxScale { node.attributes().namedItem( QStringLiteral( "maxScale" ) ).nodeValue().toDouble( &ok ) };
2877 if ( ok )
2878 {
2879 setMaximumScale( maxScale );
2880 }
2881 const double minScale { node.attributes().namedItem( QStringLiteral( "minScale" ) ).nodeValue().toDouble( &ok ) };
2882 if ( ok )
2883 {
2884 setMinimumScale( minScale );
2885 }
2886
2887 QDomElement e = node.toElement();
2888
2889 // get the simplification drawing settings
2890 mSimplifyMethod.setSimplifyHints( static_cast< Qgis::VectorRenderingSimplificationFlags >( e.attribute( QStringLiteral( "simplifyDrawingHints" ), QStringLiteral( "1" ) ).toInt() ) );
2891 mSimplifyMethod.setSimplifyAlgorithm( static_cast< Qgis::VectorSimplificationAlgorithm >( e.attribute( QStringLiteral( "simplifyAlgorithm" ), QStringLiteral( "0" ) ).toInt() ) );
2892 mSimplifyMethod.setThreshold( e.attribute( QStringLiteral( "simplifyDrawingTol" ), QStringLiteral( "1" ) ).toFloat() );
2893 mSimplifyMethod.setForceLocalOptimization( e.attribute( QStringLiteral( "simplifyLocal" ), QStringLiteral( "1" ) ).toInt() );
2894 mSimplifyMethod.setMaximumScale( e.attribute( QStringLiteral( "simplifyMaxScale" ), QStringLiteral( "1" ) ).toFloat() );
2895
2896 if ( mRenderer )
2897 mRenderer->setReferenceScale( e.attribute( QStringLiteral( "symbologyReferenceScale" ), QStringLiteral( "-1" ) ).toDouble() );
2898 }
2899
2900 //diagram renderer and diagram layer settings
2901 if ( categories.testFlag( Diagrams ) )
2902 {
2903 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Diagrams" ) );
2904
2905 delete mDiagramRenderer;
2906 mDiagramRenderer = nullptr;
2907 QDomElement singleCatDiagramElem = node.firstChildElement( QStringLiteral( "SingleCategoryDiagramRenderer" ) );
2908 if ( !singleCatDiagramElem.isNull() )
2909 {
2910 mDiagramRenderer = new QgsSingleCategoryDiagramRenderer();
2911 mDiagramRenderer->readXml( singleCatDiagramElem, context );
2912 }
2913 QDomElement linearDiagramElem = node.firstChildElement( QStringLiteral( "LinearlyInterpolatedDiagramRenderer" ) );
2914 if ( !linearDiagramElem.isNull() )
2915 {
2916 if ( linearDiagramElem.hasAttribute( QStringLiteral( "classificationAttribute" ) ) )
2917 {
2918 // fix project from before QGIS 3.0
2919 int idx = linearDiagramElem.attribute( QStringLiteral( "classificationAttribute" ) ).toInt();
2920 if ( idx >= 0 && idx < mFields.count() )
2921 linearDiagramElem.setAttribute( QStringLiteral( "classificationField" ), mFields.at( idx ).name() );
2922 }
2923
2924 mDiagramRenderer = new QgsLinearlyInterpolatedDiagramRenderer();
2925 mDiagramRenderer->readXml( linearDiagramElem, context );
2926 }
2927 QDomElement stackedDiagramElem = node.firstChildElement( QStringLiteral( "StackedDiagramRenderer" ) );
2928 if ( !stackedDiagramElem.isNull() )
2929 {
2930 mDiagramRenderer = new QgsStackedDiagramRenderer();
2931 mDiagramRenderer->readXml( stackedDiagramElem, context );
2932 }
2933
2934 if ( mDiagramRenderer )
2935 {
2936 QDomElement diagramSettingsElem = node.firstChildElement( QStringLiteral( "DiagramLayerSettings" ) );
2937 if ( !diagramSettingsElem.isNull() )
2938 {
2939 bool oldXPos = diagramSettingsElem.hasAttribute( QStringLiteral( "xPosColumn" ) );
2940 bool oldYPos = diagramSettingsElem.hasAttribute( QStringLiteral( "yPosColumn" ) );
2941 bool oldShow = diagramSettingsElem.hasAttribute( QStringLiteral( "showColumn" ) );
2942 if ( oldXPos || oldYPos || oldShow )
2943 {
2944 // fix project from before QGIS 3.0
2946 if ( oldXPos )
2947 {
2948 int xPosColumn = diagramSettingsElem.attribute( QStringLiteral( "xPosColumn" ) ).toInt();
2949 if ( xPosColumn >= 0 && xPosColumn < mFields.count() )
2951 }
2952 if ( oldYPos )
2953 {
2954 int yPosColumn = diagramSettingsElem.attribute( QStringLiteral( "yPosColumn" ) ).toInt();
2955 if ( yPosColumn >= 0 && yPosColumn < mFields.count() )
2957 }
2958 if ( oldShow )
2959 {
2960 int showColumn = diagramSettingsElem.attribute( QStringLiteral( "showColumn" ) ).toInt();
2961 if ( showColumn >= 0 && showColumn < mFields.count() )
2962 ddp.setProperty( QgsDiagramLayerSettings::Property::Show, QgsProperty::fromField( mFields.at( showColumn ).name(), true ) );
2963 }
2964 QDomElement propertiesElem = diagramSettingsElem.ownerDocument().createElement( QStringLiteral( "properties" ) );
2966 {
2967 { static_cast< int >( QgsDiagramLayerSettings::Property::PositionX ), QgsPropertyDefinition( "positionX", QObject::tr( "Position (X)" ), QgsPropertyDefinition::Double ) },
2968 { static_cast< int >( QgsDiagramLayerSettings::Property::PositionY ), QgsPropertyDefinition( "positionY", QObject::tr( "Position (Y)" ), QgsPropertyDefinition::Double ) },
2969 { static_cast< int >( QgsDiagramLayerSettings::Property::Show ), QgsPropertyDefinition( "show", QObject::tr( "Show diagram" ), QgsPropertyDefinition::Boolean ) },
2970 };
2971 ddp.writeXml( propertiesElem, defs );
2972 diagramSettingsElem.appendChild( propertiesElem );
2973 }
2974
2975 delete mDiagramLayerSettings;
2976 mDiagramLayerSettings = new QgsDiagramLayerSettings();
2977 mDiagramLayerSettings->readXml( diagramSettingsElem );
2978 }
2979 }
2980 }
2981 // end diagram
2982
2983 styleChangedSignalBlocker.release();
2985 }
2986 return result;
2987}
2988
2989
2990bool QgsVectorLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
2991 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
2992{
2994
2995 QDomElement layerElement = node.toElement();
2996 writeCommonStyle( layerElement, doc, context, categories );
2997
2998 ( void )writeStyle( node, doc, errorMessage, context, categories );
2999
3000 if ( categories.testFlag( GeometryOptions ) )
3001 mGeometryOptions->writeXml( node );
3002
3003 if ( categories.testFlag( Legend ) && legend() )
3004 {
3005 QDomElement legendElement = legend()->writeXml( doc, context );
3006 if ( !legendElement.isNull() )
3007 node.appendChild( legendElement );
3008 }
3009
3010 // Relation information for both referenced and referencing sides
3011 if ( categories.testFlag( Relations ) )
3012 {
3013 if ( QgsProject *p = project() )
3014 {
3015 // Store referenced layers: relations where "this" is the child layer (the referencing part, that holds the FK)
3016 QDomElement referencedLayersElement = doc.createElement( QStringLiteral( "referencedLayers" ) );
3017 node.appendChild( referencedLayersElement );
3018
3019 const QList<QgsRelation> referencingRelations { p->relationManager()->referencingRelations( this ) };
3020 for ( const QgsRelation &rel : referencingRelations )
3021 {
3022 switch ( rel.type() )
3023 {
3025 QgsWeakRelation::writeXml( this, QgsWeakRelation::Referencing, rel, referencedLayersElement, doc );
3026 break;
3028 break;
3029 }
3030 }
3031
3032 // Store referencing layers: relations where "this" is the parent layer (the referenced part, that holds the FK)
3033 QDomElement referencingLayersElement = doc.createElement( QStringLiteral( "referencingLayers" ) );
3034 node.appendChild( referencedLayersElement );
3035
3036 const QList<QgsRelation> referencedRelations { p->relationManager()->referencedRelations( this ) };
3037 for ( const QgsRelation &rel : referencedRelations )
3038 {
3039 switch ( rel.type() )
3040 {
3042 QgsWeakRelation::writeXml( this, QgsWeakRelation::Referenced, rel, referencingLayersElement, doc );
3043 break;
3045 break;
3046 }
3047 }
3048 }
3049 }
3050
3051 // write field configurations
3052 if ( categories.testFlag( Fields ) || categories.testFlag( Forms ) )
3053 {
3054 QDomElement fieldConfigurationElement;
3055 // field configuration flag
3056 fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );
3057 node.appendChild( fieldConfigurationElement );
3058
3059 for ( const QgsField &field : std::as_const( mFields ) )
3060 {
3061 QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
3062 fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );
3063 fieldConfigurationElement.appendChild( fieldElement );
3064
3065 if ( categories.testFlag( Fields ) )
3066 {
3067 fieldElement.setAttribute( QStringLiteral( "configurationFlags" ), qgsFlagValueToKeys( field.configurationFlags() ) );
3068 }
3069
3070 if ( categories.testFlag( Forms ) )
3071 {
3072 QgsEditorWidgetSetup widgetSetup = field.editorWidgetSetup();
3073
3074 // TODO : wrap this part in an if to only save if it was user-modified
3075 QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) );
3076 fieldElement.appendChild( editWidgetElement );
3077 editWidgetElement.setAttribute( QStringLiteral( "type" ), field.editorWidgetSetup().type() );
3078 QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) );
3079
3080 editWidgetConfigElement.appendChild( QgsXmlUtils::writeVariant( widgetSetup.config(), doc ) );
3081 editWidgetElement.appendChild( editWidgetConfigElement );
3082 // END TODO : wrap this part in an if to only save if it was user-modified
3083 }
3084 }
3085 }
3086
3087 if ( categories.testFlag( Fields ) )
3088 {
3089 //attribute aliases
3090 QDomElement aliasElem = doc.createElement( QStringLiteral( "aliases" ) );
3091 for ( const QgsField &field : std::as_const( mFields ) )
3092 {
3093 QDomElement aliasEntryElem = doc.createElement( QStringLiteral( "alias" ) );
3094 aliasEntryElem.setAttribute( QStringLiteral( "field" ), field.name() );
3095 aliasEntryElem.setAttribute( QStringLiteral( "index" ), mFields.indexFromName( field.name() ) );
3096 aliasEntryElem.setAttribute( QStringLiteral( "name" ), field.alias() );
3097 aliasElem.appendChild( aliasEntryElem );
3098 }
3099 node.appendChild( aliasElem );
3100
3101 //split policies
3102 {
3103 QDomElement splitPoliciesElement = doc.createElement( QStringLiteral( "splitPolicies" ) );
3104 for ( const QgsField &field : std::as_const( mFields ) )
3105 {
3106 QDomElement splitPolicyElem = doc.createElement( QStringLiteral( "policy" ) );
3107 splitPolicyElem.setAttribute( QStringLiteral( "field" ), field.name() );
3108 splitPolicyElem.setAttribute( QStringLiteral( "policy" ), qgsEnumValueToKey( field.splitPolicy() ) );
3109 splitPoliciesElement.appendChild( splitPolicyElem );
3110 }
3111 node.appendChild( splitPoliciesElement );
3112 }
3113
3114 //duplicate policies
3115 {
3116 QDomElement duplicatePoliciesElement = doc.createElement( QStringLiteral( "duplicatePolicies" ) );
3117 for ( const QgsField &field : std::as_const( mFields ) )
3118 {
3119 QDomElement duplicatePolicyElem = doc.createElement( QStringLiteral( "policy" ) );
3120 duplicatePolicyElem.setAttribute( QStringLiteral( "field" ), field.name() );
3121 duplicatePolicyElem.setAttribute( QStringLiteral( "policy" ), qgsEnumValueToKey( field.duplicatePolicy() ) );
3122 duplicatePoliciesElement.appendChild( duplicatePolicyElem );
3123 }
3124 node.appendChild( duplicatePoliciesElement );
3125 }
3126
3127 //default expressions
3128 QDomElement defaultsElem = doc.createElement( QStringLiteral( "defaults" ) );
3129 for ( const QgsField &field : std::as_const( mFields ) )
3130 {
3131 QDomElement defaultElem = doc.createElement( QStringLiteral( "default" ) );
3132 defaultElem.setAttribute( QStringLiteral( "field" ), field.name() );
3133 defaultElem.setAttribute( QStringLiteral( "expression" ), field.defaultValueDefinition().expression() );
3134 defaultElem.setAttribute( QStringLiteral( "applyOnUpdate" ), field.defaultValueDefinition().applyOnUpdate() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
3135 defaultsElem.appendChild( defaultElem );
3136 }
3137 node.appendChild( defaultsElem );
3138
3139 // constraints
3140 QDomElement constraintsElem = doc.createElement( QStringLiteral( "constraints" ) );
3141 for ( const QgsField &field : std::as_const( mFields ) )
3142 {
3143 QDomElement constraintElem = doc.createElement( QStringLiteral( "constraint" ) );
3144 constraintElem.setAttribute( QStringLiteral( "field" ), field.name() );
3145 constraintElem.setAttribute( QStringLiteral( "constraints" ), field.constraints().constraints() );
3146 constraintElem.setAttribute( QStringLiteral( "unique_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
3147 constraintElem.setAttribute( QStringLiteral( "notnull_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
3148 constraintElem.setAttribute( QStringLiteral( "exp_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
3149
3150 constraintsElem.appendChild( constraintElem );
3151 }
3152 node.appendChild( constraintsElem );
3153
3154 // constraint expressions
3155 QDomElement constraintExpressionsElem = doc.createElement( QStringLiteral( "constraintExpressions" ) );
3156 for ( const QgsField &field : std::as_const( mFields ) )
3157 {
3158 QDomElement constraintExpressionElem = doc.createElement( QStringLiteral( "constraint" ) );
3159 constraintExpressionElem.setAttribute( QStringLiteral( "field" ), field.name() );
3160 constraintExpressionElem.setAttribute( QStringLiteral( "exp" ), field.constraints().constraintExpression() );
3161 constraintExpressionElem.setAttribute( QStringLiteral( "desc" ), field.constraints().constraintDescription() );
3162 constraintExpressionsElem.appendChild( constraintExpressionElem );
3163 }
3164 node.appendChild( constraintExpressionsElem );
3165
3166 // save expression fields
3167 if ( !mExpressionFieldBuffer )
3168 {
3169 // can happen when saving style on a invalid layer
3171 dummy.writeXml( node, doc );
3172 }
3173 else
3174 {
3175 mExpressionFieldBuffer->writeXml( node, doc );
3176 }
3177 }
3178
3179 // add attribute actions
3180 if ( categories.testFlag( Actions ) )
3181 mActions->writeXml( node );
3182
3183 if ( categories.testFlag( AttributeTable ) )
3184 {
3185 mAttributeTableConfig.writeXml( node );
3186 mConditionalStyles->writeXml( node, doc, context );
3187 mStoredExpressionManager->writeXml( node );
3188 }
3189
3190 if ( categories.testFlag( Forms ) )
3191 mEditFormConfig.writeXml( node, context );
3192
3193 // save readonly state
3194 if ( categories.testFlag( LayerConfiguration ) )
3195 node.toElement().setAttribute( QStringLiteral( "readOnly" ), mReadOnly );
3196
3197 // save preview expression
3198 if ( categories.testFlag( LayerConfiguration ) )
3199 {
3200 QDomElement prevExpElem = doc.createElement( QStringLiteral( "previewExpression" ) );
3201 QDomText prevExpText = doc.createTextNode( mDisplayExpression );
3202 prevExpElem.appendChild( prevExpText );
3203 node.appendChild( prevExpElem );
3204 }
3205
3206 // save map tip
3207 if ( categories.testFlag( MapTips ) )
3208 {
3209 QDomElement mapTipElem = doc.createElement( QStringLiteral( "mapTip" ) );
3210 mapTipElem.setAttribute( QStringLiteral( "enabled" ), mapTipsEnabled() );
3211 QDomText mapTipText = doc.createTextNode( mMapTipTemplate );
3212 mapTipElem.appendChild( mapTipText );
3213 node.toElement().appendChild( mapTipElem );
3214 }
3215
3216 return true;
3217}
3218
3219bool QgsVectorLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
3220 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
3221{
3223
3224 QDomElement mapLayerNode = node.toElement();
3225
3226 emit writeCustomSymbology( mapLayerNode, doc, errorMessage );
3227
3228 // we must try to write the renderer if our geometry type is unknown
3229 // as this allows the renderer to be correctly restored even for layers
3230 // with broken sources
3231 if ( isSpatial() || mWkbType == Qgis::WkbType::Unknown )
3232 {
3233 if ( categories.testFlag( Symbology ) )
3234 {
3235 if ( mRenderer )
3236 {
3237 QDomElement rendererElement = mRenderer->save( doc, context );
3238 node.appendChild( rendererElement );
3239 }
3240 if ( mSelectionProperties )
3241 {
3242 mSelectionProperties->writeXml( mapLayerNode, doc, context );
3243 }
3244 }
3245
3246 if ( categories.testFlag( Labeling ) )
3247 {
3248 if ( mLabeling )
3249 {
3250 QDomElement labelingElement = mLabeling->save( doc, context );
3251 node.appendChild( labelingElement );
3252 }
3253 mapLayerNode.setAttribute( QStringLiteral( "labelsEnabled" ), mLabelsEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
3254 }
3255
3256 // save the simplification drawing settings
3257 if ( categories.testFlag( Rendering ) )
3258 {
3259 mapLayerNode.setAttribute( QStringLiteral( "simplifyDrawingHints" ), QString::number( static_cast< int >( mSimplifyMethod.simplifyHints() ) ) );
3260 mapLayerNode.setAttribute( QStringLiteral( "simplifyAlgorithm" ), QString::number( static_cast< int >( mSimplifyMethod.simplifyAlgorithm() ) ) );
3261 mapLayerNode.setAttribute( QStringLiteral( "simplifyDrawingTol" ), QString::number( mSimplifyMethod.threshold() ) );
3262 mapLayerNode.setAttribute( QStringLiteral( "simplifyLocal" ), mSimplifyMethod.forceLocalOptimization() ? 1 : 0 );
3263 mapLayerNode.setAttribute( QStringLiteral( "simplifyMaxScale" ), QString::number( mSimplifyMethod.maximumScale() ) );
3264 }
3265
3266 //save customproperties
3267 if ( categories.testFlag( CustomProperties ) )
3268 {
3269 writeCustomProperties( node, doc );
3270 }
3271
3272 if ( categories.testFlag( Symbology ) )
3273 {
3274 // add the blend mode field
3275 QDomElement blendModeElem = doc.createElement( QStringLiteral( "blendMode" ) );
3276 QDomText blendModeText = doc.createTextNode( QString::number( static_cast< int >( QgsPainting::getBlendModeEnum( blendMode() ) ) ) );
3277 blendModeElem.appendChild( blendModeText );
3278 node.appendChild( blendModeElem );
3279
3280 // add the feature blend mode field
3281 QDomElement featureBlendModeElem = doc.createElement( QStringLiteral( "featureBlendMode" ) );
3282 QDomText featureBlendModeText = doc.createTextNode( QString::number( static_cast< int >( QgsPainting::getBlendModeEnum( featureBlendMode() ) ) ) );
3283 featureBlendModeElem.appendChild( featureBlendModeText );
3284 node.appendChild( featureBlendModeElem );
3285 }
3286
3287 // add the layer opacity and scale visibility
3288 if ( categories.testFlag( Rendering ) )
3289 {
3290 QDomElement layerOpacityElem = doc.createElement( QStringLiteral( "layerOpacity" ) );
3291 QDomText layerOpacityText = doc.createTextNode( QString::number( opacity() ) );
3292 layerOpacityElem.appendChild( layerOpacityText );
3293 node.appendChild( layerOpacityElem );
3294 mapLayerNode.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
3295 mapLayerNode.setAttribute( QStringLiteral( "maxScale" ), maximumScale() );
3296 mapLayerNode.setAttribute( QStringLiteral( "minScale" ), minimumScale() );
3297
3298 mapLayerNode.setAttribute( QStringLiteral( "symbologyReferenceScale" ), mRenderer ? mRenderer->referenceScale() : -1 );
3299 }
3300
3301 if ( categories.testFlag( Diagrams ) && mDiagramRenderer )
3302 {
3303 mDiagramRenderer->writeXml( mapLayerNode, doc, context );
3304 if ( mDiagramLayerSettings )
3305 mDiagramLayerSettings->writeXml( mapLayerNode, doc );
3306 }
3307 }
3308 return true;
3309}
3310
3311bool QgsVectorLayer::readSld( const QDomNode &node, QString &errorMessage )
3312{
3314
3315 // get the Name element
3316 QDomElement nameElem = node.firstChildElement( QStringLiteral( "Name" ) );
3317 if ( nameElem.isNull() )
3318 {
3319 errorMessage = QStringLiteral( "Warning: Name element not found within NamedLayer while it's required." );
3320 }
3321
3322 if ( isSpatial() )
3323 {
3324 QgsFeatureRenderer *r = QgsFeatureRenderer::loadSld( node, geometryType(), errorMessage );
3325 if ( !r )
3326 return false;
3327
3328 // defer style changed signal until we've set the renderer, labeling, everything.
3329 // we don't want multiple signals!
3330 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
3331
3332 setRenderer( r );
3333
3334 // labeling
3335 readSldLabeling( node );
3336
3337 styleChangedSignalBlocker.release();
3339 }
3340 return true;
3341}
3342
3343bool QgsVectorLayer::writeSld( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props ) const
3344{
3346
3347 Q_UNUSED( errorMessage )
3348
3349 QVariantMap localProps = QVariantMap( props );
3351 {
3353 }
3354
3355 if ( isSpatial() )
3356 {
3357 // store the Name element
3358 QDomElement nameNode = doc.createElement( QStringLiteral( "se:Name" ) );
3359 nameNode.appendChild( doc.createTextNode( name() ) );
3360 node.appendChild( nameNode );
3361
3362 QDomElement userStyleElem = doc.createElement( QStringLiteral( "UserStyle" ) );
3363 node.appendChild( userStyleElem );
3364
3365 QDomElement nameElem = doc.createElement( QStringLiteral( "se:Name" ) );
3366 nameElem.appendChild( doc.createTextNode( name() ) );
3367
3368 userStyleElem.appendChild( nameElem );
3369
3370 QDomElement featureTypeStyleElem = doc.createElement( QStringLiteral( "se:FeatureTypeStyle" ) );
3371 userStyleElem.appendChild( featureTypeStyleElem );
3372
3373 mRenderer->toSld( doc, featureTypeStyleElem, localProps );
3374 if ( labelsEnabled() )
3375 {
3376 mLabeling->toSld( featureTypeStyleElem, localProps );
3377 }
3378 }
3379 return true;
3380}
3381
3382
3383bool QgsVectorLayer::changeGeometry( QgsFeatureId fid, QgsGeometry &geom, bool skipDefaultValue )
3384{
3386
3387 if ( !mEditBuffer || !mDataProvider )
3388 {
3389 return false;
3390 }
3391
3392 if ( mGeometryOptions->isActive() )
3393 mGeometryOptions->apply( geom );
3394
3395 updateExtents();
3396
3397 bool result = mEditBuffer->changeGeometry( fid, geom );
3398
3399 if ( result )
3400 {
3401 updateExtents();
3402 if ( !skipDefaultValue && !mDefaultValueOnUpdateFields.isEmpty() )
3403 updateDefaultValues( fid );
3404 }
3405 return result;
3406}
3407
3408
3409bool QgsVectorLayer::changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue, bool skipDefaultValues, QgsVectorLayerToolsContext *context )
3410{
3412
3413 bool result = false;
3414
3415 switch ( fields().fieldOrigin( field ) )
3416 {
3418 result = mJoinBuffer->changeAttributeValue( fid, field, newValue, oldValue );
3419 if ( result )
3420 emit attributeValueChanged( fid, field, newValue );
3421 break;
3422
3426 {
3427 if ( mEditBuffer && mDataProvider )
3428 result = mEditBuffer->changeAttributeValue( fid, field, newValue, oldValue );
3429 break;
3430 }
3431
3433 break;
3434 }
3435
3436 if ( result && !skipDefaultValues && !mDefaultValueOnUpdateFields.isEmpty() )
3437 updateDefaultValues( fid, QgsFeature(), context ? context->expressionContext() : nullptr );
3438
3439 return result;
3440}
3441
3442bool QgsVectorLayer::changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues, bool skipDefaultValues, QgsVectorLayerToolsContext *context )
3443{
3445
3446 bool result = true;
3447
3448 QgsAttributeMap newValuesJoin;
3449 QgsAttributeMap oldValuesJoin;
3450
3451 QgsAttributeMap newValuesNotJoin;
3452 QgsAttributeMap oldValuesNotJoin;
3453
3454 for ( auto it = newValues.constBegin(); it != newValues.constEnd(); ++it )
3455 {
3456 const int field = it.key();
3457 const QVariant newValue = it.value();
3458 QVariant oldValue;
3459
3460 if ( oldValues.contains( field ) )
3461 oldValue = oldValues[field];
3462
3463 switch ( fields().fieldOrigin( field ) )
3464 {
3466 newValuesJoin[field] = newValue;
3467 oldValuesJoin[field] = oldValue;
3468 break;
3469
3473 {
3474 newValuesNotJoin[field] = newValue;
3475 oldValuesNotJoin[field] = oldValue;
3476 break;
3477 }
3478
3480 break;
3481 }
3482 }
3483
3484 if ( ! newValuesJoin.isEmpty() && mJoinBuffer )
3485 {
3486 result = mJoinBuffer->changeAttributeValues( fid, newValuesJoin, oldValuesJoin );
3487 }
3488
3489 if ( ! newValuesNotJoin.isEmpty() )
3490 {
3491 if ( mEditBuffer && mDataProvider )
3492 result &= mEditBuffer->changeAttributeValues( fid, newValuesNotJoin, oldValues );
3493 else
3494 result = false;
3495 }
3496
3497 if ( result && !skipDefaultValues && !mDefaultValueOnUpdateFields.isEmpty() )
3498 {
3499 updateDefaultValues( fid, QgsFeature(), context ? context->expressionContext() : nullptr );
3500 }
3501
3502 return result;
3503}
3504
3506{
3508
3509 if ( !mEditBuffer || !mDataProvider )
3510 return false;
3511
3512 return mEditBuffer->addAttribute( field );
3513}
3514
3516{
3518
3519 if ( attIndex < 0 || attIndex >= fields().count() )
3520 return;
3521
3522 QString name = fields().at( attIndex ).name();
3523 mFields[ attIndex ].setAlias( QString() );
3524 if ( mAttributeAliasMap.contains( name ) )
3525 {
3526 mAttributeAliasMap.remove( name );
3527 updateFields();
3528 mEditFormConfig.setFields( mFields );
3529 emit layerModified();
3530 }
3531}
3532
3533bool QgsVectorLayer::renameAttribute( int index, const QString &newName )
3534{
3536
3537 if ( index < 0 || index >= fields().count() )
3538 return false;
3539
3540 switch ( mFields.fieldOrigin( index ) )
3541 {
3543 {
3544 if ( mExpressionFieldBuffer )
3545 {
3546 int oi = mFields.fieldOriginIndex( index );
3547 mExpressionFieldBuffer->renameExpression( oi, newName );
3548 updateFields();
3549 return true;
3550 }
3551 else
3552 {
3553 return false;
3554 }
3555 }
3556
3559
3560 if ( !mEditBuffer || !mDataProvider )
3561 return false;
3562
3563 return mEditBuffer->renameAttribute( index, newName );
3564
3567 return false;
3568
3569 }
3570
3571 return false; // avoid warning
3572}
3573
3574void QgsVectorLayer::setFieldAlias( int attIndex, const QString &aliasString )
3575{
3577
3578 if ( attIndex < 0 || attIndex >= fields().count() )
3579 return;
3580
3581 QString name = fields().at( attIndex ).name();
3582
3583 mAttributeAliasMap.insert( name, aliasString );
3584 mFields[ attIndex ].setAlias( aliasString );
3585 mEditFormConfig.setFields( mFields );
3586 emit layerModified(); // TODO[MD]: should have a different signal?
3587}
3588
3589QString QgsVectorLayer::attributeAlias( int index ) const
3590{
3592
3593 if ( index < 0 || index >= fields().count() )
3594 return QString();
3595
3596 return fields().at( index ).alias();
3597}
3598
3600{
3602
3603 if ( index >= 0 && index < mFields.count() )
3604 return mFields.at( index ).displayName();
3605 else
3606 return QString();
3607}
3608
3610{
3612
3613 return mAttributeAliasMap;
3614}
3615
3617{
3619
3620 if ( index < 0 || index >= fields().count() )
3621 return;
3622
3623 const QString name = fields().at( index ).name();
3624
3625 mAttributeSplitPolicy.insert( name, policy );
3626 mFields[ index ].setSplitPolicy( policy );
3627 mEditFormConfig.setFields( mFields );
3628 emit layerModified(); // TODO[MD]: should have a different signal?
3629}
3630
3632{
3634
3635 if ( index < 0 || index >= fields().count() )
3636 return;
3637
3638 const QString name = fields().at( index ).name();
3639
3640 mAttributeDuplicatePolicy.insert( name, policy );
3641 mFields[ index ].setDuplicatePolicy( policy );
3642 mEditFormConfig.setFields( mFields );
3643 emit layerModified(); // TODO[MD]: should have a different signal?
3644}
3645
3646
3648{
3650
3651 QSet<QString> excludeList;
3652 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
3653 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
3654 {
3655 if ( flagsIt->testFlag( Qgis::FieldConfigurationFlag::HideFromWms ) )
3656 {
3657 excludeList << flagsIt.key();
3658 }
3659 }
3660 return excludeList;
3661}
3662
3663void QgsVectorLayer::setExcludeAttributesWms( const QSet<QString> &att )
3664{
3666
3667 QMap< QString, Qgis::FieldConfigurationFlags >::iterator flagsIt = mFieldConfigurationFlags.begin();
3668 for ( ; flagsIt != mFieldConfigurationFlags.end(); ++flagsIt )
3669 {
3670 flagsIt->setFlag( Qgis::FieldConfigurationFlag::HideFromWms, att.contains( flagsIt.key() ) );
3671 }
3672 updateFields();
3673}
3674
3676{
3678
3679 QSet<QString> excludeList;
3680 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
3681 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
3682 {
3683 if ( flagsIt->testFlag( Qgis::FieldConfigurationFlag::HideFromWfs ) )
3684 {
3685 excludeList << flagsIt.key();
3686 }
3687 }
3688 return excludeList;
3689}
3690
3691void QgsVectorLayer::setExcludeAttributesWfs( const QSet<QString> &att )
3692{
3694
3695 QMap< QString, Qgis::FieldConfigurationFlags >::iterator flagsIt = mFieldConfigurationFlags.begin();
3696 for ( ; flagsIt != mFieldConfigurationFlags.end(); ++flagsIt )
3697 {
3698 flagsIt->setFlag( Qgis::FieldConfigurationFlag::HideFromWfs, att.contains( flagsIt.key() ) );
3699 }
3700 updateFields();
3701}
3702
3704{
3706
3707 if ( index < 0 || index >= fields().count() )
3708 return false;
3709
3710 if ( mFields.fieldOrigin( index ) == Qgis::FieldOrigin::Expression )
3711 {
3712 removeExpressionField( index );
3713 return true;
3714 }
3715
3716 if ( !mEditBuffer || !mDataProvider )
3717 return false;
3718
3719 return mEditBuffer->deleteAttribute( index );
3720}
3721
3722bool QgsVectorLayer::deleteAttributes( const QList<int> &attrs )
3723{
3725
3726 bool deleted = false;
3727
3728 // Remove multiple occurrences of same attribute
3729 QList<int> attrList = qgis::setToList( qgis::listToSet( attrs ) );
3730
3731 std::sort( attrList.begin(), attrList.end(), std::greater<int>() );
3732
3733 for ( int attr : std::as_const( attrList ) )
3734 {
3735 if ( deleteAttribute( attr ) )
3736 {
3737 deleted = true;
3738 }
3739 }
3740
3741 return deleted;
3742}
3743
3744bool QgsVectorLayer::deleteFeatureCascade( QgsFeatureId fid, QgsVectorLayer::DeleteContext *context )
3745{
3747
3748 if ( !mEditBuffer )
3749 return false;
3750
3751 if ( context && context->cascade )
3752 {
3753 const QList<QgsRelation> relations = context->project->relationManager()->referencedRelations( this );
3754 const bool hasRelationsOrJoins = !relations.empty() || mJoinBuffer->containsJoins();
3755 if ( hasRelationsOrJoins )
3756 {
3757 if ( context->mHandledFeatures.contains( this ) )
3758 {
3759 QgsFeatureIds &handledFeatureIds = context->mHandledFeatures[ this ];
3760 if ( handledFeatureIds.contains( fid ) )
3761 {
3762 // avoid endless recursion
3763 return false;
3764 }
3765 else
3766 {
3767 // add feature id
3768 handledFeatureIds << fid;
3769 }
3770 }
3771 else
3772 {
3773 // add layer and feature id
3774 context->mHandledFeatures.insert( this, QgsFeatureIds() << fid );
3775 }
3776
3777 for ( const QgsRelation &relation : relations )
3778 {
3779 //check if composition (and not association)
3780 switch ( relation.strength() )
3781 {
3783 {
3784 //get features connected over this relation
3785 QgsFeatureIterator relatedFeaturesIt = relation.getRelatedFeatures( getFeature( fid ) );
3786 QgsFeatureIds childFeatureIds;
3787 QgsFeature childFeature;
3788 while ( relatedFeaturesIt.nextFeature( childFeature ) )
3789 {
3790 childFeatureIds.insert( childFeature.id() );
3791 }
3792 if ( childFeatureIds.count() > 0 )
3793 {
3794 relation.referencingLayer()->startEditing();
3795 relation.referencingLayer()->deleteFeatures( childFeatureIds, context );
3796 }
3797 break;
3798 }
3799
3801 break;
3802 }
3803 }
3804 }
3805 }
3806
3807 if ( mJoinBuffer->containsJoins() )
3808 mJoinBuffer->deleteFeature( fid, context );
3809
3810 bool res = mEditBuffer->deleteFeature( fid );
3811
3812 return res;
3813}
3814
3816{
3818
3819 if ( !mEditBuffer )
3820 return false;
3821
3822 bool res = deleteFeatureCascade( fid, context );
3823
3824 if ( res )
3825 {
3826 updateExtents();
3827 }
3828
3829 return res;
3830}
3831
3833{
3835
3836 bool res = true;
3837
3838 if ( ( context && context->cascade ) || mJoinBuffer->containsJoins() )
3839 {
3840 // should ideally be "deleteFeaturesCascade" for performance!
3841 for ( QgsFeatureId fid : fids )
3842 res = deleteFeatureCascade( fid, context ) && res;
3843 }
3844 else
3845 {
3846 res = mEditBuffer && mEditBuffer->deleteFeatures( fids );
3847 }
3848
3849 if ( res )
3850 {
3851 mSelectedFeatureIds.subtract( fids ); // remove it from selection
3852 updateExtents();
3853 }
3854
3855 return res;
3856}
3857
3859{
3860 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
3862
3863 return mFields;
3864}
3865
3867{
3869
3870 QgsAttributeList pkAttributesList;
3871 if ( !mDataProvider )
3872 return pkAttributesList;
3873
3874 QgsAttributeList providerIndexes = mDataProvider->pkAttributeIndexes();
3875 for ( int i = 0; i < mFields.count(); ++i )
3876 {
3877 if ( mFields.fieldOrigin( i ) == Qgis::FieldOrigin::Provider &&
3878 providerIndexes.contains( mFields.fieldOriginIndex( i ) ) )
3879 pkAttributesList << i;
3880 }
3881
3882 return pkAttributesList;
3883}
3884
3886{
3888
3889 if ( !mDataProvider )
3890 return static_cast< long long >( Qgis::FeatureCountState::UnknownCount );
3891 return mDataProvider->featureCount() +
3892 ( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures().size() - mEditBuffer->deletedFeatureIds().size() : 0 );
3893}
3894
3896{
3898
3899 const QgsFeatureIds deletedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->deletedFeatureIds() : QgsFeatureIds() );
3900 const QgsFeatureMap addedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures() : QgsFeatureMap() );
3901
3902 if ( mEditBuffer && !deletedFeatures.empty() )
3903 {
3904 if ( addedFeatures.size() > deletedFeatures.size() )
3906 else
3908 }
3909
3910 if ( ( !mEditBuffer || addedFeatures.empty() ) && mDataProvider && mDataProvider->empty() )
3912 else
3914}
3915
3916bool QgsVectorLayer::commitChanges( bool stopEditing )
3917{
3919
3920 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
3921 return project()->commitChanges( mCommitErrors, stopEditing, this );
3922
3923 mCommitErrors.clear();
3924
3925 if ( !mDataProvider )
3926 {
3927 mCommitErrors << tr( "ERROR: no provider" );
3928 return false;
3929 }
3930
3931 if ( !mEditBuffer )
3932 {
3933 mCommitErrors << tr( "ERROR: layer not editable" );
3934 return false;
3935 }
3936
3937 emit beforeCommitChanges( stopEditing );
3938
3939 if ( !mAllowCommit )
3940 return false;
3941
3942 mCommitChangesActive = true;
3943
3944 bool success = false;
3945 if ( mEditBuffer->editBufferGroup() )
3946 success = mEditBuffer->editBufferGroup()->commitChanges( mCommitErrors, stopEditing );
3947 else
3948 success = mEditBuffer->commitChanges( mCommitErrors );
3949
3950 mCommitChangesActive = false;
3951
3952 if ( !mDeletedFids.empty() )
3953 {
3954 emit featuresDeleted( mDeletedFids );
3955 mDeletedFids.clear();
3956 }
3957
3958 if ( success )
3959 {
3960 if ( stopEditing )
3961 {
3962 clearEditBuffer();
3963 }
3964 undoStack()->clear();
3965 emit afterCommitChanges();
3966 if ( stopEditing )
3967 emit editingStopped();
3968 }
3969 else
3970 {
3971 QgsMessageLog::logMessage( tr( "Commit errors:\n %1" ).arg( mCommitErrors.join( QLatin1String( "\n " ) ) ) );
3972 }
3973
3974 updateFields();
3975
3976 mDataProvider->updateExtents();
3977
3978 if ( stopEditing )
3979 {
3980 mDataProvider->leaveUpdateMode();
3981 }
3982
3983 // This second call is required because OGR provider with JSON
3984 // driver might have changed fields order after the call to
3985 // leaveUpdateMode
3986 if ( mFields.names() != mDataProvider->fields().names() )
3987 {
3988 updateFields();
3989 }
3990
3992
3993 return success;
3994}
3995
3997{
3999
4000 return mCommitErrors;
4001}
4002
4003bool QgsVectorLayer::rollBack( bool deleteBuffer )
4004{
4006
4007 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
4008 return project()->rollBack( mCommitErrors, deleteBuffer, this );
4009
4010 if ( !mEditBuffer )
4011 {
4012 return false;
4013 }
4014
4015 if ( !mDataProvider )
4016 {
4017 mCommitErrors << tr( "ERROR: no provider" );
4018 return false;
4019 }
4020
4021 bool rollbackExtent = !mDataProvider->transaction() && ( !mEditBuffer->deletedFeatureIds().isEmpty() ||
4022 !mEditBuffer->addedFeatures().isEmpty() ||
4023 !mEditBuffer->changedGeometries().isEmpty() );
4024
4025 emit beforeRollBack();
4026
4027 mEditBuffer->rollBack();
4028
4029 emit afterRollBack();
4030
4031 if ( isModified() )
4032 {
4033 // new undo stack roll back method
4034 // old method of calling every undo could cause many canvas refreshes
4035 undoStack()->setIndex( 0 );
4036 }
4037
4038 updateFields();
4039
4040 if ( deleteBuffer )
4041 {
4042 delete mEditBuffer;
4043 mEditBuffer = nullptr;
4044 undoStack()->clear();
4045 }
4046 emit editingStopped();
4047
4048 if ( rollbackExtent )
4049 updateExtents();
4050
4051 mDataProvider->leaveUpdateMode();
4052
4054 return true;
4055}
4056
4058{
4060
4061 return mSelectedFeatureIds.size();
4062}
4063
4065{
4066 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
4068
4069 return mSelectedFeatureIds;
4070}
4071
4073{
4075
4076 QgsFeatureList features;
4077 features.reserve( mSelectedFeatureIds.count() );
4078 QgsFeature f;
4079
4081
4082 while ( it.nextFeature( f ) )
4083 {
4084 features.push_back( f );
4085 }
4086
4087 return features;
4088}
4089
4091{
4093
4094 if ( mSelectedFeatureIds.isEmpty() )
4095 return QgsFeatureIterator();
4096
4099
4100 if ( mSelectedFeatureIds.count() == 1 )
4101 request.setFilterFid( *mSelectedFeatureIds.constBegin() );
4102 else
4103 request.setFilterFids( mSelectedFeatureIds );
4104
4105 return getFeatures( request );
4106}
4107
4109{
4111
4112 if ( !mEditBuffer || !mDataProvider )
4113 return false;
4114
4115 if ( mGeometryOptions->isActive() )
4116 {
4117 for ( auto feature = features.begin(); feature != features.end(); ++feature )
4118 {
4119 QgsGeometry geom = feature->geometry();
4120 mGeometryOptions->apply( geom );
4121 feature->setGeometry( geom );
4122 }
4123 }
4124
4125 bool res = mEditBuffer->addFeatures( features );
4126 updateExtents();
4127
4128 if ( res && mJoinBuffer->containsJoins() )
4129 res = mJoinBuffer->addFeatures( features );
4130
4131 return res;
4132}
4133
4135{
4137
4138 // if layer is not spatial, it has not CRS!
4139 setCrs( ( isSpatial() && mDataProvider ) ? mDataProvider->crs() : QgsCoordinateReferenceSystem() );
4140}
4141
4143{
4145
4147 if ( exp.isField() )
4148 {
4149 return static_cast<const QgsExpressionNodeColumnRef *>( exp.rootNode() )->name();
4150 }
4151
4152 return QString();
4153}
4154
4155void QgsVectorLayer::setDisplayExpression( const QString &displayExpression )
4156{
4158
4159 if ( mDisplayExpression == displayExpression )
4160 return;
4161
4162 mDisplayExpression = displayExpression;
4164}
4165
4167{
4169
4170 if ( !mDisplayExpression.isEmpty() || mFields.isEmpty() )
4171 {
4172 return mDisplayExpression;
4173 }
4174 else
4175 {
4176 const QString candidateName = QgsVectorLayerUtils::guessFriendlyIdentifierField( mFields );
4177 if ( !candidateName.isEmpty() )
4178 {
4179 return QgsExpression::quotedColumnRef( candidateName );
4180 }
4181 else
4182 {
4183 return QString();
4184 }
4185 }
4186}
4187
4189{
4191
4192 // display expressions are used as a fallback when no explicit map tip template is set
4193 return mapTipsEnabled() && ( !mapTipTemplate().isEmpty() || !displayExpression().isEmpty() );
4194}
4195
4197{
4199
4200 return ( mEditBuffer && mDataProvider );
4201}
4202
4204{
4205 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
4207
4210}
4211
4212bool QgsVectorLayer::isReadOnly() const
4213{
4215
4216 return mDataSourceReadOnly || mReadOnly;
4217}
4218
4219bool QgsVectorLayer::setReadOnly( bool readonly )
4220{
4222
4223 // exit if the layer is in editing mode
4224 if ( readonly && mEditBuffer )
4225 return false;
4226
4227 // exit if the data source is in read-only mode
4228 if ( !readonly && mDataSourceReadOnly )
4229 return false;
4230
4231 mReadOnly = readonly;
4232 emit readOnlyChanged();
4233 return true;
4234}
4235
4237{
4239
4240 if ( ! mDataProvider )
4241 return false;
4242
4243 if ( mDataSourceReadOnly )
4244 return false;
4245
4246 return mDataProvider->capabilities() & QgsVectorDataProvider::EditingCapabilities && ! mReadOnly;
4247}
4248
4250{
4252
4253 emit beforeModifiedCheck();
4254 return mEditBuffer && mEditBuffer->isModified();
4255}
4256
4257bool QgsVectorLayer::isAuxiliaryField( int index, int &srcIndex ) const
4258{
4260
4261 bool auxiliaryField = false;
4262 srcIndex = -1;
4263
4264 if ( !auxiliaryLayer() )
4265 return auxiliaryField;
4266
4267 if ( index >= 0 && fields().fieldOrigin( index ) == Qgis::FieldOrigin::Join )
4268 {
4269 const QgsVectorLayerJoinInfo *info = mJoinBuffer->joinForFieldIndex( index, fields(), srcIndex );
4270
4271 if ( info && info->joinLayerId() == auxiliaryLayer()->id() )
4272 auxiliaryField = true;
4273 }
4274
4275 return auxiliaryField;
4276}
4277
4279{
4281
4282 // we must allow setting a renderer if our geometry type is unknown
4283 // as this allows the renderer to be correctly set even for layers
4284 // with broken sources
4285 // (note that we allow REMOVING the renderer for non-spatial layers,
4286 // e.g. to permit removing the renderer when the layer changes from
4287 // a spatial layer to a non-spatial one)
4288 if ( r && !isSpatial() && mWkbType != Qgis::WkbType::Unknown )
4289 return;
4290
4291 if ( r != mRenderer )
4292 {
4293 delete mRenderer;
4294 mRenderer = r;
4295 mSymbolFeatureCounted = false;
4296 mSymbolFeatureCountMap.clear();
4297 mSymbolFeatureIdMap.clear();
4298
4299 if ( mRenderer )
4300 {
4301 const double refreshRate = QgsSymbolLayerUtils::rendererFrameRate( mRenderer );
4302 if ( refreshRate <= 0 )
4303 {
4304 mRefreshRendererTimer->stop();
4305 mRefreshRendererTimer->setInterval( 0 );
4306 }
4307 else
4308 {
4309 mRefreshRendererTimer->setInterval( 1000 / refreshRate );
4310 mRefreshRendererTimer->start();
4311 }
4312 }
4313
4314 emit rendererChanged();
4316 }
4317}
4318
4320{
4322
4323 if ( generator )
4324 {
4325 mRendererGenerators << generator;
4326 }
4327}
4328
4330{
4332
4333 for ( int i = mRendererGenerators.count() - 1; i >= 0; --i )
4334 {
4335 if ( mRendererGenerators.at( i )->id() == id )
4336 {
4337 delete mRendererGenerators.at( i );
4338 mRendererGenerators.removeAt( i );
4339 }
4340 }
4341}
4342
4343QList<const QgsFeatureRendererGenerator *> QgsVectorLayer::featureRendererGenerators() const
4344{
4345 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
4347
4348 QList< const QgsFeatureRendererGenerator * > res;
4349 for ( const QgsFeatureRendererGenerator *generator : mRendererGenerators )
4350 res << generator;
4351 return res;
4352}
4353
4354void QgsVectorLayer::beginEditCommand( const QString &text )
4355{
4357
4358 if ( !mDataProvider )
4359 {
4360 return;
4361 }
4362 if ( mDataProvider->transaction() )
4363 {
4364 QString ignoredError;
4365 mDataProvider->transaction()->createSavepoint( ignoredError );
4366 }
4367 undoStack()->beginMacro( text );
4368 mEditCommandActive = true;
4369 emit editCommandStarted( text );
4370}
4371
4373{
4375
4376 if ( !mDataProvider )
4377 {
4378 return;
4379 }
4380 undoStack()->endMacro();
4381 mEditCommandActive = false;
4382 if ( !mDeletedFids.isEmpty() )
4383 {
4384 if ( selectedFeatureCount() > 0 )
4385 {
4386 mSelectedFeatureIds.subtract( mDeletedFids );
4387 }
4388 emit featuresDeleted( mDeletedFids );
4389 mDeletedFids.clear();
4390 }
4391 emit editCommandEnded();
4392}
4393
4395{
4397
4398 if ( !mDataProvider )
4399 {
4400 return;
4401 }
4402 undoStack()->endMacro();
4403 undoStack()->undo();
4404
4405 // it's not directly possible to pop the last command off the stack (the destroyed one)
4406 // and delete, so we add a dummy obsolete command to force this to occur.
4407 // Pushing the new command deletes the destroyed one, and since the new
4408 // command is obsolete it's automatically deleted by the undo stack.
4409 std::unique_ptr< QUndoCommand > command = std::make_unique< QUndoCommand >();
4410 command->setObsolete( true );
4411 undoStack()->push( command.release() );
4412
4413 mEditCommandActive = false;
4414 mDeletedFids.clear();
4415 emit editCommandDestroyed();
4416}
4417
4419{
4421
4422 return mJoinBuffer->addJoin( joinInfo );
4423}
4424
4425bool QgsVectorLayer::removeJoin( const QString &joinLayerId )
4426{
4428
4429 return mJoinBuffer->removeJoin( joinLayerId );
4430}
4431
4432const QList< QgsVectorLayerJoinInfo > QgsVectorLayer::vectorJoins() const
4433{
4435
4436 return mJoinBuffer->vectorJoins();
4437}
4438
4439int QgsVectorLayer::addExpressionField( const QString &exp, const QgsField &fld )
4440{
4442
4443 emit beforeAddingExpressionField( fld.name() );
4444 mExpressionFieldBuffer->addExpression( exp, fld );
4445 updateFields();
4446 int idx = mFields.indexFromName( fld.name() );
4447 emit attributeAdded( idx );
4448 return idx;
4449}
4450
4452{
4454
4455 emit beforeRemovingExpressionField( index );
4456 int oi = mFields.fieldOriginIndex( index );
4457 mExpressionFieldBuffer->removeExpression( oi );
4458 updateFields();
4459 emit attributeDeleted( index );
4460}
4461
4462QString QgsVectorLayer::expressionField( int index ) const
4463{
4465
4466 if ( mFields.fieldOrigin( index ) != Qgis::FieldOrigin::Expression )
4467 return QString();
4468
4469 int oi = mFields.fieldOriginIndex( index );
4470 if ( oi < 0 || oi >= mExpressionFieldBuffer->expressions().size() )
4471 return QString();
4472
4473 return mExpressionFieldBuffer->expressions().at( oi ).cachedExpression.expression();
4474}
4475
4476void QgsVectorLayer::updateExpressionField( int index, const QString &exp )
4477{
4479
4480 int oi = mFields.fieldOriginIndex( index );
4481 mExpressionFieldBuffer->updateExpression( oi, exp );
4482}
4483
4485{
4486 // non fatal for now -- the QgsVirtualLayerTask class is not thread safe and calls this
4488
4489 if ( !mDataProvider )
4490 return;
4491
4492 QgsFields oldFields = mFields;
4493
4494 mFields = mDataProvider->fields();
4495
4496 // added / removed fields
4497 if ( mEditBuffer )
4498 mEditBuffer->updateFields( mFields );
4499
4500 // joined fields
4501 if ( mJoinBuffer->containsJoins() )
4502 mJoinBuffer->updateFields( mFields );
4503
4504 if ( mExpressionFieldBuffer )
4505 mExpressionFieldBuffer->updateFields( mFields );
4506
4507 // set aliases and default values
4508 for ( auto aliasIt = mAttributeAliasMap.constBegin(); aliasIt != mAttributeAliasMap.constEnd(); ++aliasIt )
4509 {
4510 int index = mFields.lookupField( aliasIt.key() );
4511 if ( index < 0 )
4512 continue;
4513
4514 mFields[ index ].setAlias( aliasIt.value() );
4515 }
4516
4517 for ( auto splitPolicyIt = mAttributeSplitPolicy.constBegin(); splitPolicyIt != mAttributeSplitPolicy.constEnd(); ++splitPolicyIt )
4518 {
4519 int index = mFields.lookupField( splitPolicyIt.key() );
4520 if ( index < 0 )
4521 continue;
4522
4523 mFields[ index ].setSplitPolicy( splitPolicyIt.value() );
4524 }
4525
4526 for ( auto duplicatePolicyIt = mAttributeDuplicatePolicy.constBegin(); duplicatePolicyIt != mAttributeDuplicatePolicy.constEnd(); ++duplicatePolicyIt )
4527 {
4528 int index = mFields.lookupField( duplicatePolicyIt.key() );
4529 if ( index < 0 )
4530 continue;
4531
4532 mFields[ index ].setDuplicatePolicy( duplicatePolicyIt.value() );
4533 }
4534
4535 // Update configuration flags
4536 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
4537 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
4538 {
4539 int index = mFields.lookupField( flagsIt.key() );
4540 if ( index < 0 )
4541 continue;
4542
4543 mFields[index].setConfigurationFlags( flagsIt.value() );
4544 }
4545
4546 // Update default values
4547 mDefaultValueOnUpdateFields.clear();
4548 QMap< QString, QgsDefaultValue >::const_iterator defaultIt = mDefaultExpressionMap.constBegin();
4549 for ( ; defaultIt != mDefaultExpressionMap.constEnd(); ++defaultIt )
4550 {
4551 int index = mFields.lookupField( defaultIt.key() );
4552 if ( index < 0 )
4553 continue;
4554
4555 mFields[ index ].setDefaultValueDefinition( defaultIt.value() );
4556 if ( defaultIt.value().applyOnUpdate() )
4557 mDefaultValueOnUpdateFields.insert( index );
4558 }
4559
4560 QMap< QString, QgsFieldConstraints::Constraints >::const_iterator constraintIt = mFieldConstraints.constBegin();
4561 for ( ; constraintIt != mFieldConstraints.constEnd(); ++constraintIt )
4562 {
4563 int index = mFields.lookupField( constraintIt.key() );
4564 if ( index < 0 )
4565 continue;
4566
4567 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4568
4569 // always keep provider constraints intact
4570 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintNotNull ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintNotNull ) )
4572 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintUnique ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintUnique ) )
4574 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintExpression ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintExpression ) )
4576 mFields[ index ].setConstraints( constraints );
4577 }
4578
4579 QMap< QString, QPair< QString, QString > >::const_iterator constraintExpIt = mFieldConstraintExpressions.constBegin();
4580 for ( ; constraintExpIt != mFieldConstraintExpressions.constEnd(); ++constraintExpIt )
4581 {
4582 int index = mFields.lookupField( constraintExpIt.key() );
4583 if ( index < 0 )
4584 continue;
4585
4586 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4587
4588 // always keep provider constraints intact
4590 continue;
4591
4592 constraints.setConstraintExpression( constraintExpIt.value().first, constraintExpIt.value().second );
4593 mFields[ index ].setConstraints( constraints );
4594 }
4595
4596 QMap< QPair< QString, QgsFieldConstraints::Constraint >, QgsFieldConstraints::ConstraintStrength >::const_iterator constraintStrengthIt = mFieldConstraintStrength.constBegin();
4597 for ( ; constraintStrengthIt != mFieldConstraintStrength.constEnd(); ++constraintStrengthIt )
4598 {
4599 int index = mFields.lookupField( constraintStrengthIt.key().first );
4600 if ( index < 0 )
4601 continue;
4602
4603 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4604
4605 // always keep provider constraints intact
4607 continue;
4608
4609 constraints.setConstraintStrength( constraintStrengthIt.key().second, constraintStrengthIt.value() );
4610 mFields[ index ].setConstraints( constraints );
4611 }
4612
4613 auto fieldWidgetIterator = mFieldWidgetSetups.constBegin();
4614 for ( ; fieldWidgetIterator != mFieldWidgetSetups.constEnd(); ++ fieldWidgetIterator )
4615 {
4616 int index = mFields.indexOf( fieldWidgetIterator.key() );
4617 if ( index < 0 )
4618 continue;
4619
4620 mFields[index].setEditorWidgetSetup( fieldWidgetIterator.value() );
4621 }
4622
4623 if ( oldFields != mFields )
4624 {
4625 emit updatedFields();
4626 mEditFormConfig.setFields( mFields );
4627 }
4628
4629}
4630
4631QVariant QgsVectorLayer::defaultValue( int index, const QgsFeature &feature, QgsExpressionContext *context ) const
4632{
4634
4635 if ( index < 0 || index >= mFields.count() || !mDataProvider )
4636 return QVariant();
4637
4638 QString expression = mFields.at( index ).defaultValueDefinition().expression();
4639 if ( expression.isEmpty() )
4640 return mDataProvider->defaultValue( index );
4641
4642 QgsExpressionContext *evalContext = context;
4643 std::unique_ptr< QgsExpressionContext > tempContext;
4644 if ( !evalContext )
4645 {
4646 // no context passed, so we create a default one
4648 evalContext = tempContext.get();
4649 }
4650
4651 if ( feature.isValid() )
4652 {
4654 featScope->setFeature( feature );
4655 featScope->setFields( feature.fields() );
4656 evalContext->appendScope( featScope );
4657 }
4658
4659 QVariant val;
4660 QgsExpression exp( expression );
4661 exp.prepare( evalContext );
4662 if ( exp.hasEvalError() )
4663 {
4664 QgsLogger::warning( "Error evaluating default value: " + exp.evalErrorString() );
4665 }
4666 else
4667 {
4668 val = exp.evaluate( evalContext );
4669 }
4670
4671 if ( feature.isValid() )
4672 {
4673 delete evalContext->popScope();
4674 }
4675
4676 return val;
4677}
4678
4680{
4682
4683 if ( index < 0 || index >= mFields.count() )
4684 return;
4685
4686 if ( definition.isValid() )
4687 {
4688 mDefaultExpressionMap.insert( mFields.at( index ).name(), definition );
4689 }
4690 else
4691 {
4692 mDefaultExpressionMap.remove( mFields.at( index ).name() );
4693 }
4694 updateFields();
4695}
4696
4698{
4700
4701 if ( index < 0 || index >= mFields.count() )
4702 return QgsDefaultValue();
4703 else
4704 return mFields.at( index ).defaultValueDefinition();
4705}
4706
4707QSet<QVariant> QgsVectorLayer::uniqueValues( int index, int limit ) const
4708{
4710
4711 QSet<QVariant> uniqueValues;
4712 if ( !mDataProvider )
4713 {
4714 return uniqueValues;
4715 }
4716
4717 Qgis::FieldOrigin origin = mFields.fieldOrigin( index );
4718 switch ( origin )
4719 {
4721 return uniqueValues;
4722
4723 case Qgis::FieldOrigin::Provider: //a provider field
4724 {
4725 uniqueValues = mDataProvider->uniqueValues( index, limit );
4726
4727 if ( mEditBuffer && ! mDataProvider->transaction() )
4728 {
4729 QSet<QString> vals;
4730 const auto constUniqueValues = uniqueValues;
4731 for ( const QVariant &v : constUniqueValues )
4732 {
4733 vals << v.toString();
4734 }
4735
4736 QgsFeatureMap added = mEditBuffer->addedFeatures();
4737 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
4738 while ( addedIt.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
4739 {
4740 addedIt.next();
4741 QVariant v = addedIt.value().attribute( index );
4742 if ( v.isValid() )
4743 {
4744 QString vs = v.toString();
4745 if ( !vals.contains( vs ) )
4746 {
4747 vals << vs;
4748 uniqueValues << v;
4749 }
4750 }
4751 }
4752
4753 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
4754 while ( it.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
4755 {
4756 it.next();
4757 QVariant v = it.value().value( index );
4758 if ( v.isValid() )
4759 {
4760 QString vs = v.toString();
4761 if ( !vals.contains( vs ) )
4762 {
4763 vals << vs;
4764 uniqueValues << v;
4765 }
4766 }
4767 }
4768 }
4769
4770 return uniqueValues;
4771 }
4772
4774 // the layer is editable, but in certain cases it can still be avoided going through all features
4775 if ( mDataProvider->transaction() || (
4776 mEditBuffer->deletedFeatureIds().isEmpty() &&
4777 mEditBuffer->addedFeatures().isEmpty() &&
4778 !mEditBuffer->deletedAttributeIds().contains( index ) &&
4779 mEditBuffer->changedAttributeValues().isEmpty() ) )
4780 {
4781 uniqueValues = mDataProvider->uniqueValues( index, limit );
4782 return uniqueValues;
4783 }
4784 [[fallthrough]];
4785 //we need to go through each feature
4788 {
4789 QgsAttributeList attList;
4790 attList << index;
4791
4794 .setSubsetOfAttributes( attList ) );
4795
4796 QgsFeature f;
4797 QVariant currentValue;
4798 QHash<QString, QVariant> val;
4799 while ( fit.nextFeature( f ) )
4800 {
4801 currentValue = f.attribute( index );
4802 val.insert( currentValue.toString(), currentValue );
4803 if ( limit >= 0 && val.size() >= limit )
4804 {
4805 break;
4806 }
4807 }
4808
4809 return qgis::listToSet( val.values() );
4810 }
4811 }
4812
4813 Q_ASSERT_X( false, "QgsVectorLayer::uniqueValues()", "Unknown source of the field!" );
4814 return uniqueValues;
4815}
4816
4817QStringList QgsVectorLayer::uniqueStringsMatching( int index, const QString &substring, int limit, QgsFeedback *feedback ) const
4818{
4820
4821 QStringList results;
4822 if ( !mDataProvider )
4823 {
4824 return results;
4825 }
4826
4827 Qgis::FieldOrigin origin = mFields.fieldOrigin( index );
4828 switch ( origin )
4829 {
4831 return results;
4832
4833 case Qgis::FieldOrigin::Provider: //a provider field
4834 {
4835 results = mDataProvider->uniqueStringsMatching( index, substring, limit, feedback );
4836
4837 if ( mEditBuffer && ! mDataProvider->transaction() )
4838 {
4839 QgsFeatureMap added = mEditBuffer->addedFeatures();
4840 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
4841 while ( addedIt.hasNext() && ( limit < 0 || results.count() < limit ) && ( !feedback || !feedback->isCanceled() ) )
4842 {
4843 addedIt.next();
4844 QVariant v = addedIt.value().attribute( index );
4845 if ( v.isValid() )
4846 {
4847 QString vs = v.toString();
4848 if ( vs.contains( substring, Qt::CaseInsensitive ) && !results.contains( vs ) )
4849 {
4850 results << vs;
4851 }
4852 }
4853 }
4854
4855 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
4856 while ( it.hasNext() && ( limit < 0 || results.count() < limit ) && ( !feedback || !feedback->isCanceled() ) )
4857 {
4858 it.next();
4859 QVariant v = it.value().value( index );
4860 if ( v.isValid() )
4861 {
4862 QString vs = v.toString();
4863 if ( vs.contains( substring, Qt::CaseInsensitive ) && !results.contains( vs ) )
4864 {
4865 results << vs;
4866 }
4867 }
4868 }
4869 }
4870
4871 return results;
4872 }
4873
4875 // the layer is editable, but in certain cases it can still be avoided going through all features
4876 if ( mDataProvider->transaction() || ( mEditBuffer->deletedFeatureIds().isEmpty() &&
4877 mEditBuffer->addedFeatures().isEmpty() &&
4878 !mEditBuffer->deletedAttributeIds().contains( index ) &&
4879 mEditBuffer->changedAttributeValues().isEmpty() ) )
4880 {
4881 return mDataProvider->uniqueStringsMatching( index, substring, limit, feedback );
4882 }
4883 [[fallthrough]];
4884 //we need to go through each feature
4887 {
4888 QgsAttributeList attList;
4889 attList << index;
4890
4891 QgsFeatureRequest request;
4892 request.setSubsetOfAttributes( attList );
4894 QString fieldName = mFields.at( index ).name();
4895 request.setFilterExpression( QStringLiteral( "\"%1\" ILIKE '%%2%'" ).arg( fieldName, substring ) );
4896 QgsFeatureIterator fit = getFeatures( request );
4897
4898 QgsFeature f;
4899 QString currentValue;
4900 while ( fit.nextFeature( f ) )
4901 {
4902 currentValue = f.attribute( index ).toString();
4903 if ( !results.contains( currentValue ) )
4904 results << currentValue;
4905
4906 if ( ( limit >= 0 && results.size() >= limit ) || ( feedback && feedback->isCanceled() ) )
4907 {
4908 break;
4909 }
4910 }
4911
4912 return results;
4913 }
4914 }
4915
4916 Q_ASSERT_X( false, "QgsVectorLayer::uniqueStringsMatching()", "Unknown source of the field!" );
4917 return results;
4918}
4919
4920QVariant QgsVectorLayer::minimumValue( int index ) const
4921{
4923
4924 QVariant minimum;
4925 minimumOrMaximumValue( index, &minimum, nullptr );
4926 return minimum;
4927}
4928
4929QVariant QgsVectorLayer::maximumValue( int index ) const
4930{
4932
4933 QVariant maximum;
4934 minimumOrMaximumValue( index, nullptr, &maximum );
4935 return maximum;
4936}
4937
4938void QgsVectorLayer::minimumAndMaximumValue( int index, QVariant &minimum, QVariant &maximum ) const
4939{
4941
4942 minimumOrMaximumValue( index, &minimum, &maximum );
4943}
4944
4945void QgsVectorLayer::minimumOrMaximumValue( int index, QVariant *minimum, QVariant *maximum ) const
4946{
4948
4949 if ( minimum )
4950 *minimum = QVariant();
4951 if ( maximum )
4952 *maximum = QVariant();
4953
4954 if ( !mDataProvider )
4955 {
4956 return;
4957 }
4958
4959 Qgis::FieldOrigin origin = mFields.fieldOrigin( index );
4960
4961 switch ( origin )
4962 {
4964 {
4965 return;
4966 }
4967
4968 case Qgis::FieldOrigin::Provider: //a provider field
4969 {
4970 if ( minimum )
4971 *minimum = mDataProvider->minimumValue( index );
4972 if ( maximum )
4973 *maximum = mDataProvider->maximumValue( index );
4974 if ( mEditBuffer && ! mDataProvider->transaction() )
4975 {
4976 const QgsFeatureMap added = mEditBuffer->addedFeatures();
4977 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
4978 while ( addedIt.hasNext() )
4979 {
4980 addedIt.next();
4981 const QVariant v = addedIt.value().attribute( index );
4982 if ( minimum && v.isValid() && qgsVariantLessThan( v, *minimum ) )
4983 *minimum = v;
4984 if ( maximum && v.isValid() && qgsVariantGreaterThan( v, *maximum ) )
4985 *maximum = v;
4986 }
4987
4988 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
4989 while ( it.hasNext() )
4990 {
4991 it.next();
4992 const QVariant v = it.value().value( index );
4993 if ( minimum && v.isValid() && qgsVariantLessThan( v, *minimum ) )
4994 *minimum = v;
4995 if ( maximum && v.isValid() && qgsVariantGreaterThan( v, *maximum ) )
4996 *maximum = v;
4997 }
4998 }
4999 return;
5000 }
5001
5003 {
5004 // the layer is editable, but in certain cases it can still be avoided going through all features
5005 if ( mDataProvider->transaction() || ( mEditBuffer->deletedFeatureIds().isEmpty() &&
5006 mEditBuffer->addedFeatures().isEmpty() &&
5007 !mEditBuffer->deletedAttributeIds().contains( index ) &&
5008 mEditBuffer->changedAttributeValues().isEmpty() ) )
5009 {
5010 if ( minimum )
5011 *minimum = mDataProvider->minimumValue( index );
5012 if ( maximum )
5013 *maximum = mDataProvider->maximumValue( index );
5014 return;
5015 }
5016 }
5017 [[fallthrough]];
5018 // no choice but to go through all features
5021 {
5022 // we need to go through each feature
5023 QgsAttributeList attList;
5024 attList << index;
5025
5028 .setSubsetOfAttributes( attList ) );
5029
5030 QgsFeature f;
5031 bool firstValue = true;
5032 while ( fit.nextFeature( f ) )
5033 {
5034 const QVariant currentValue = f.attribute( index );
5035 if ( QgsVariantUtils::isNull( currentValue ) )
5036 continue;
5037
5038 if ( firstValue )
5039 {
5040 if ( minimum )
5041 *minimum = currentValue;
5042 if ( maximum )
5043 *maximum = currentValue;
5044 firstValue = false;
5045 }
5046 else
5047 {
5048 if ( minimum && currentValue.isValid() && qgsVariantLessThan( currentValue, *minimum ) )
5049 *minimum = currentValue;
5050 if ( maximum && currentValue.isValid() && qgsVariantGreaterThan( currentValue, *maximum ) )
5051 *maximum = currentValue;
5052 }
5053 }
5054 return;
5055 }
5056 }
5057
5058 Q_ASSERT_X( false, "QgsVectorLayer::minimumOrMaximumValue()", "Unknown source of the field!" );
5059}
5060
5061void QgsVectorLayer::createEditBuffer()
5062{
5064
5065 if ( mEditBuffer )
5066 clearEditBuffer();
5067
5068 if ( mDataProvider->transaction() )
5069 {
5070 mEditBuffer = new QgsVectorLayerEditPassthrough( this );
5071
5072 connect( mDataProvider->transaction(), &QgsTransaction::dirtied, this, &QgsVectorLayer::onDirtyTransaction, Qt::UniqueConnection );
5073 }
5074 else
5075 {
5076 mEditBuffer = new QgsVectorLayerEditBuffer( this );
5077 }
5078 // forward signals
5079 connect( mEditBuffer, &QgsVectorLayerEditBuffer::layerModified, this, &QgsVectorLayer::invalidateSymbolCountedFlag );
5080 connect( mEditBuffer, &QgsVectorLayerEditBuffer::layerModified, this, &QgsVectorLayer::layerModified ); // TODO[MD]: necessary?
5081 //connect( mEditBuffer, SIGNAL( layerModified() ), this, SLOT( triggerRepaint() ) ); // TODO[MD]: works well?
5083 connect( mEditBuffer, &QgsVectorLayerEditBuffer::featureDeleted, this, &QgsVectorLayer::onFeatureDeleted );
5094
5095}
5096
5097void QgsVectorLayer::clearEditBuffer()
5098{
5100
5101 delete mEditBuffer;
5102 mEditBuffer = nullptr;
5103}
5104
5105QVariant QgsVectorLayer::aggregate( Qgis::Aggregate aggregate, const QString &fieldOrExpression,
5107 bool *ok, QgsFeatureIds *fids, QgsFeedback *feedback, QString *error ) const
5108{
5109 // non fatal for now -- the aggregate expression functions are not thread safe and call this
5111
5112 if ( ok )
5113 *ok = false;
5114 if ( error )
5115 error->clear();
5116
5117 if ( !mDataProvider )
5118 {
5119 if ( error )
5120 *error = tr( "Layer is invalid" );
5121 return QVariant();
5122 }
5123
5124 // test if we are calculating based on a field
5125 const int attrIndex = QgsExpression::expressionToLayerFieldIndex( fieldOrExpression, this );
5126 if ( attrIndex >= 0 )
5127 {
5128 // aggregate is based on a field - if it's a provider field, we could possibly hand over the calculation
5129 // to the provider itself
5130 Qgis::FieldOrigin origin = mFields.fieldOrigin( attrIndex );
5131 if ( origin == Qgis::FieldOrigin::Provider )
5132 {
5133 bool providerOk = false;
5134 QVariant val = mDataProvider->aggregate( aggregate, attrIndex, parameters, context, providerOk, fids );
5135 if ( providerOk )
5136 {
5137 // provider handled calculation
5138 if ( ok )
5139 *ok = true;
5140 return val;
5141 }
5142 }
5143 }
5144
5145 // fallback to using aggregate calculator to determine aggregate
5146 QgsAggregateCalculator c( this );
5147 if ( fids )
5148 c.setFidsFilter( *fids );
5149 c.setParameters( parameters );
5150 bool aggregateOk = false;
5151 const QVariant result = c.calculate( aggregate, fieldOrExpression, context, &aggregateOk, feedback );
5152 if ( ok )
5153 *ok = aggregateOk;
5154 if ( !aggregateOk && error )
5155 *error = c.lastError();
5156
5157 return result;
5158}
5159
5160void QgsVectorLayer::setFeatureBlendMode( QPainter::CompositionMode featureBlendMode )
5161{
5163
5164 if ( mFeatureBlendMode == featureBlendMode )
5165 return;
5166
5167 mFeatureBlendMode = featureBlendMode;
5170}
5171
5172QPainter::CompositionMode QgsVectorLayer::featureBlendMode() const
5173{
5174 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
5176
5177 return mFeatureBlendMode;
5178}
5179
5180void QgsVectorLayer::readSldLabeling( const QDomNode &node )
5181{
5183
5184 setLabeling( nullptr ); // start with no labeling
5185 setLabelsEnabled( false );
5186
5187 QDomElement element = node.toElement();
5188 if ( element.isNull() )
5189 return;
5190
5191 QDomElement userStyleElem = element.firstChildElement( QStringLiteral( "UserStyle" ) );
5192 if ( userStyleElem.isNull() )
5193 {
5194 QgsDebugMsgLevel( QStringLiteral( "Info: UserStyle element not found." ), 4 );
5195 return;
5196 }
5197
5198 QDomElement featTypeStyleElem = userStyleElem.firstChildElement( QStringLiteral( "FeatureTypeStyle" ) );
5199 if ( featTypeStyleElem.isNull() )
5200 {
5201 QgsDebugMsgLevel( QStringLiteral( "Info: FeatureTypeStyle element not found." ), 4 );
5202 return;
5203 }
5204
5205 // create empty FeatureTypeStyle element to merge TextSymbolizer's Rule's from all FeatureTypeStyle's
5206 QDomElement mergedFeatTypeStyle = featTypeStyleElem.cloneNode( false ).toElement();
5207
5208 // use the RuleRenderer when more rules are present or the rule
5209 // has filters or min/max scale denominators set,
5210 // otherwise use the Simple labeling
5211 bool needRuleBasedLabeling = false;
5212 int ruleCount = 0;
5213
5214 while ( !featTypeStyleElem.isNull() )
5215 {
5216 QDomElement ruleElem = featTypeStyleElem.firstChildElement( QStringLiteral( "Rule" ) );
5217 while ( !ruleElem.isNull() )
5218 {
5219 // test rule children element to check if we need to create RuleRenderer
5220 // and if the rule has a symbolizer
5221 bool hasTextSymbolizer = false;
5222 bool hasRuleBased = false;
5223 QDomElement ruleChildElem = ruleElem.firstChildElement();
5224 while ( !ruleChildElem.isNull() )
5225 {
5226 // rule has filter or min/max scale denominator, use the RuleRenderer
5227 if ( ruleChildElem.localName() == QLatin1String( "Filter" ) ||
5228 ruleChildElem.localName() == QLatin1String( "MinScaleDenominator" ) ||
5229 ruleChildElem.localName() == QLatin1String( "MaxScaleDenominator" ) )
5230 {
5231 hasRuleBased = true;
5232 }
5233 // rule has a renderer symbolizer, not a text symbolizer
5234 else if ( ruleChildElem.localName() == QLatin1String( "TextSymbolizer" ) )
5235 {
5236 QgsDebugMsgLevel( QStringLiteral( "Info: TextSymbolizer element found" ), 4 );
5237 hasTextSymbolizer = true;
5238 }
5239
5240 ruleChildElem = ruleChildElem.nextSiblingElement();
5241 }
5242
5243 if ( hasTextSymbolizer )
5244 {
5245 ruleCount++;
5246
5247 // append a clone of all Rules to the merged FeatureTypeStyle element
5248 mergedFeatTypeStyle.appendChild( ruleElem.cloneNode().toElement() );
5249
5250 if ( hasRuleBased )
5251 {
5252 QgsDebugMsgLevel( QStringLiteral( "Info: Filter or Min/MaxScaleDenominator element found: need a RuleBasedLabeling" ), 4 );
5253 needRuleBasedLabeling = true;
5254 }
5255 }
5256
5257 // more rules present, use the RuleRenderer
5258 if ( ruleCount > 1 )
5259 {
5260 QgsDebugMsgLevel( QStringLiteral( "Info: More Rule elements found: need a RuleBasedLabeling" ), 4 );
5261 needRuleBasedLabeling = true;
5262 }
5263
5264 // not use the rule based labeling if no rules with textSymbolizer
5265 if ( ruleCount == 0 )
5266 {
5267 needRuleBasedLabeling = false;
5268 }
5269
5270 ruleElem = ruleElem.nextSiblingElement( QStringLiteral( "Rule" ) );
5271 }
5272 featTypeStyleElem = featTypeStyleElem.nextSiblingElement( QStringLiteral( "FeatureTypeStyle" ) );
5273 }
5274
5275 if ( ruleCount == 0 )
5276 {
5277 QgsDebugMsgLevel( QStringLiteral( "Info: No TextSymbolizer element." ), 4 );
5278 return;
5279 }
5280
5281 QDomElement ruleElem = mergedFeatTypeStyle.firstChildElement( QStringLiteral( "Rule" ) );
5282
5283 if ( needRuleBasedLabeling )
5284 {
5285 QgsDebugMsgLevel( QStringLiteral( "Info: rule based labeling" ), 4 );
5286 QgsRuleBasedLabeling::Rule *rootRule = new QgsRuleBasedLabeling::Rule( nullptr );
5287 while ( !ruleElem.isNull() )
5288 {
5289
5290 QString label, description, filterExp;
5291 int scaleMinDenom = 0, scaleMaxDenom = 0;
5292 QgsPalLayerSettings settings;
5293
5294 // retrieve the Rule element child nodes
5295 QDomElement childElem = ruleElem.firstChildElement();
5296 while ( !childElem.isNull() )
5297 {
5298 if ( childElem.localName() == QLatin1String( "Name" ) )
5299 {
5300 // <se:Name> tag contains the rule identifier,
5301 // so prefer title tag for the label property value
5302 if ( label.isEmpty() )
5303 label = childElem.firstChild().nodeValue();
5304 }
5305 else if ( childElem.localName() == QLatin1String( "Description" ) )
5306 {
5307 // <se:Description> can contains a title and an abstract
5308 QDomElement titleElem = childElem.firstChildElement( QStringLiteral( "Title" ) );
5309 if ( !titleElem.isNull() )
5310 {
5311 label = titleElem.firstChild().nodeValue();
5312 }
5313
5314 QDomElement abstractElem = childElem.firstChildElement( QStringLiteral( "Abstract" ) );
5315 if ( !abstractElem.isNull() )
5316 {
5317 description = abstractElem.firstChild().nodeValue();
5318 }
5319 }
5320 else if ( childElem.localName() == QLatin1String( "Abstract" ) )
5321 {
5322 // <sld:Abstract> (v1.0)
5323 description = childElem.firstChild().nodeValue();
5324 }
5325 else if ( childElem.localName() == QLatin1String( "Title" ) )
5326 {
5327 // <sld:Title> (v1.0)
5328 label = childElem.firstChild().nodeValue();
5329 }
5330 else if ( childElem.localName() == QLatin1String( "Filter" ) )
5331 {
5333 if ( filter )
5334 {
5335 if ( filter->hasParserError() )
5336 {
5337 QgsDebugMsgLevel( QStringLiteral( "SLD Filter parsing error: %1" ).arg( filter->parserErrorString() ), 3 );
5338 }
5339 else
5340 {
5341 filterExp = filter->expression();
5342 }
5343 delete filter;
5344 }
5345 }
5346 else if ( childElem.localName() == QLatin1String( "MinScaleDenominator" ) )
5347 {
5348 bool ok;
5349 int v = childElem.firstChild().nodeValue().toInt( &ok );
5350 if ( ok )
5351 scaleMinDenom = v;
5352 }
5353 else if ( childElem.localName() == QLatin1String( "MaxScaleDenominator" ) )
5354 {
5355 bool ok;
5356 int v = childElem.firstChild().nodeValue().toInt( &ok );
5357 if ( ok )
5358 scaleMaxDenom = v;
5359 }
5360 else if ( childElem.localName() == QLatin1String( "TextSymbolizer" ) )
5361 {
5362 readSldTextSymbolizer( childElem, settings );
5363 }
5364
5365 childElem = childElem.nextSiblingElement();
5366 }
5367
5368 QgsRuleBasedLabeling::Rule *ruleLabeling = new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings( settings ), scaleMinDenom, scaleMaxDenom, filterExp, label );
5369 rootRule->appendChild( ruleLabeling );
5370
5371 ruleElem = ruleElem.nextSiblingElement();
5372 }
5373
5374 setLabeling( new QgsRuleBasedLabeling( rootRule ) );
5375 setLabelsEnabled( true );
5376 }
5377 else
5378 {
5379 QgsDebugMsgLevel( QStringLiteral( "Info: simple labeling" ), 4 );
5380 // retrieve the TextSymbolizer element child node
5381 QDomElement textSymbolizerElem = ruleElem.firstChildElement( QStringLiteral( "TextSymbolizer" ) );
5383 if ( readSldTextSymbolizer( textSymbolizerElem, s ) )
5384 {
5386 setLabelsEnabled( true );
5387 }
5388 }
5389}
5390
5391bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSettings &settings ) const
5392{
5394
5395 if ( node.localName() != QLatin1String( "TextSymbolizer" ) )
5396 {
5397 QgsDebugMsgLevel( QStringLiteral( "Not a TextSymbolizer element: %1" ).arg( node.localName() ), 3 );
5398 return false;
5399 }
5400 QDomElement textSymbolizerElem = node.toElement();
5401 // Label
5402 QDomElement labelElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Label" ) );
5403 if ( !labelElem.isNull() )
5404 {
5405 QDomElement propertyNameElem = labelElem.firstChildElement( QStringLiteral( "PropertyName" ) );
5406 if ( !propertyNameElem.isNull() )
5407 {
5408 // set labeling defaults
5409
5410 // label attribute
5411 QString labelAttribute = propertyNameElem.text();
5412 settings.fieldName = labelAttribute;
5413 settings.isExpression = false;
5414
5415 int fieldIndex = mFields.lookupField( labelAttribute );
5416 if ( fieldIndex == -1 )
5417 {
5418 // label attribute is not in columns, check if it is an expression
5419 QgsExpression exp( labelAttribute );
5420 if ( !exp.hasEvalError() )
5421 {
5422 settings.isExpression = true;
5423 }
5424 else
5425 {
5426 QgsDebugMsgLevel( QStringLiteral( "SLD label attribute error: %1" ).arg( exp.evalErrorString() ), 3 );
5427 }
5428 }
5429 }
5430 else
5431 {
5432 QgsDebugMsgLevel( QStringLiteral( "Info: PropertyName element not found." ), 4 );
5433 return false;
5434 }
5435 }
5436 else
5437 {
5438 QgsDebugMsgLevel( QStringLiteral( "Info: Label element not found." ), 4 );
5439 return false;
5440 }
5441
5443 if ( textSymbolizerElem.hasAttribute( QStringLiteral( "uom" ) ) )
5444 {
5445 sldUnitSize = QgsSymbolLayerUtils::decodeSldUom( textSymbolizerElem.attribute( QStringLiteral( "uom" ) ) );
5446 }
5447
5448 QString fontFamily = QStringLiteral( "Sans-Serif" );
5449 int fontPointSize = 10;
5451 int fontWeight = -1;
5452 bool fontItalic = false;
5453 bool fontUnderline = false;
5454
5455 // Font
5456 QDomElement fontElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Font" ) );
5457 if ( !fontElem.isNull() )
5458 {
5459 QgsStringMap fontSvgParams = QgsSymbolLayerUtils::getSvgParameterList( fontElem );
5460 for ( QgsStringMap::iterator it = fontSvgParams.begin(); it != fontSvgParams.end(); ++it )
5461 {
5462 QgsDebugMsgLevel( QStringLiteral( "found fontSvgParams %1: %2" ).arg( it.key(), it.value() ), 4 );
5463
5464 if ( it.key() == QLatin1String( "font-family" ) )
5465 {
5466 fontFamily = it.value();
5467 }
5468 else if ( it.key() == QLatin1String( "font-style" ) )
5469 {
5470 fontItalic = ( it.value() == QLatin1String( "italic" ) ) || ( it.value() == QLatin1String( "Italic" ) );
5471 }
5472 else if ( it.key() == QLatin1String( "font-size" ) )
5473 {
5474 bool ok;
5475 int fontSize = it.value().toInt( &ok );
5476 if ( ok )
5477 {
5478 fontPointSize = fontSize;
5479 fontUnitSize = sldUnitSize;
5480 }
5481 }
5482 else if ( it.key() == QLatin1String( "font-weight" ) )
5483 {
5484 if ( ( it.value() == QLatin1String( "bold" ) ) || ( it.value() == QLatin1String( "Bold" ) ) )
5485 fontWeight = QFont::Bold;
5486 }
5487 else if ( it.key() == QLatin1String( "font-underline" ) )
5488 {
5489 fontUnderline = ( it.value() == QLatin1String( "underline" ) ) || ( it.value() == QLatin1String( "Underline" ) );
5490 }
5491 }
5492 }
5493
5494 QgsTextFormat format;
5495 QFont font( fontFamily, fontPointSize, fontWeight, fontItalic );
5496 font.setUnderline( fontUnderline );
5497 format.setFont( font );
5498 format.setSize( fontPointSize );
5499 format.setSizeUnit( fontUnitSize );
5500
5501 // Fill
5502 QDomElement fillElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Fill" ) );
5503 QColor textColor;
5504 Qt::BrushStyle textBrush = Qt::SolidPattern;
5505 QgsSymbolLayerUtils::fillFromSld( fillElem, textBrush, textColor );
5506 if ( textColor.isValid() )
5507 {
5508 QgsDebugMsgLevel( QStringLiteral( "Info: textColor %1." ).arg( QVariant( textColor ).toString() ), 4 );
5509 format.setColor( textColor );
5510 }
5511
5512 QgsTextBufferSettings bufferSettings;
5513
5514 // Halo
5515 QDomElement haloElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Halo" ) );
5516 if ( !haloElem.isNull() )
5517 {
5518 bufferSettings.setEnabled( true );
5519 bufferSettings.setSize( 1 );
5520
5521 QDomElement radiusElem = haloElem.firstChildElement( QStringLiteral( "Radius" ) );
5522 if ( !radiusElem.isNull() )
5523 {
5524 bool ok;
5525 double bufferSize = radiusElem.text().toDouble( &ok );
5526 if ( ok )
5527 {
5528 bufferSettings.setSize( bufferSize );
5529 bufferSettings.setSizeUnit( sldUnitSize );
5530 }
5531 }
5532
5533 QDomElement haloFillElem = haloElem.firstChildElement( QStringLiteral( "Fill" ) );
5534 QColor bufferColor;
5535 Qt::BrushStyle bufferBrush = Qt::SolidPattern;
5536 QgsSymbolLayerUtils::fillFromSld( haloFillElem, bufferBrush, bufferColor );
5537 if ( bufferColor.isValid() )
5538 {
5539 QgsDebugMsgLevel( QStringLiteral( "Info: bufferColor %1." ).arg( QVariant( bufferColor ).toString() ), 4 );
5540 bufferSettings.setColor( bufferColor );
5541 }
5542 }
5543
5544 // LabelPlacement
5545 QDomElement labelPlacementElem = textSymbolizerElem.firstChildElement( QStringLiteral( "LabelPlacement" ) );
5546 if ( !labelPlacementElem.isNull() )
5547 {
5548 // PointPlacement
5549 QDomElement pointPlacementElem = labelPlacementElem.firstChildElement( QStringLiteral( "PointPlacement" ) );
5550 if ( !pointPlacementElem.isNull() )
5551 {
5554 {
5556 }
5557
5558 QDomElement displacementElem = pointPlacementElem.firstChildElement( QStringLiteral( "Displacement" ) );
5559 if ( !displacementElem.isNull() )
5560 {
5561 QDomElement displacementXElem = displacementElem.firstChildElement( QStringLiteral( "DisplacementX" ) );
5562 if ( !displacementXElem.isNull() )
5563 {
5564 bool ok;
5565 double xOffset = displacementXElem.text().toDouble( &ok );
5566 if ( ok )
5567 {
5568 settings.xOffset = xOffset;
5569 settings.offsetUnits = sldUnitSize;
5570 }
5571 }
5572 QDomElement displacementYElem = displacementElem.firstChildElement( QStringLiteral( "DisplacementY" ) );
5573 if ( !displacementYElem.isNull() )
5574 {
5575 bool ok;
5576 double yOffset = displacementYElem.text().toDouble( &ok );
5577 if ( ok )
5578 {
5579 settings.yOffset = yOffset;
5580 settings.offsetUnits = sldUnitSize;
5581 }
5582 }
5583 }
5584 QDomElement anchorPointElem = pointPlacementElem.firstChildElement( QStringLiteral( "AnchorPoint" ) );
5585 if ( !anchorPointElem.isNull() )
5586 {
5587 QDomElement anchorPointXElem = anchorPointElem.firstChildElement( QStringLiteral( "AnchorPointX" ) );
5588 if ( !anchorPointXElem.isNull() )
5589 {
5590 bool ok;
5591 double xOffset = anchorPointXElem.text().toDouble( &ok );
5592 if ( ok )
5593 {
5594 settings.xOffset = xOffset;
5595 settings.offsetUnits = sldUnitSize;
5596 }
5597 }
5598 QDomElement anchorPointYElem = anchorPointElem.firstChildElement( QStringLiteral( "AnchorPointY" ) );
5599 if ( !anchorPointYElem.isNull() )
5600 {
5601 bool ok;
5602 double yOffset = anchorPointYElem.text().toDouble( &ok );
5603 if ( ok )
5604 {
5605 settings.yOffset = yOffset;
5606 settings.offsetUnits = sldUnitSize;
5607 }
5608 }
5609 }
5610
5611 QDomElement rotationElem = pointPlacementElem.firstChildElement( QStringLiteral( "Rotation" ) );
5612 if ( !rotationElem.isNull() )
5613 {
5614 bool ok;
5615 double rotation = rotationElem.text().toDouble( &ok );
5616 if ( ok )
5617 {
5618 settings.angleOffset = 360 - rotation;
5619 }
5620 }
5621 }
5622 else
5623 {
5624 // PointPlacement
5625 QDomElement linePlacementElem = labelPlacementElem.firstChildElement( QStringLiteral( "LinePlacement" ) );
5626 if ( !linePlacementElem.isNull() )
5627 {
5629 }
5630 }
5631 }
5632
5633 // read vendor options
5634 QgsStringMap vendorOptions;
5635 QDomElement vendorOptionElem = textSymbolizerElem.firstChildElement( QStringLiteral( "VendorOption" ) );
5636 while ( !vendorOptionElem.isNull() && vendorOptionElem.localName() == QLatin1String( "VendorOption" ) )
5637 {
5638 QString optionName = vendorOptionElem.attribute( QStringLiteral( "name" ) );
5639 QString optionValue;
5640 if ( vendorOptionElem.firstChild().nodeType() == QDomNode::TextNode )
5641 {
5642 optionValue = vendorOptionElem.firstChild().nodeValue();
5643 }
5644 else
5645 {
5646 if ( vendorOptionElem.firstChild().nodeType() == QDomNode::ElementNode &&
5647 vendorOptionElem.firstChild().localName() == QLatin1String( "Literal" ) )
5648 {
5649 QgsDebugMsgLevel( vendorOptionElem.firstChild().localName(), 2 );
5650 optionValue = vendorOptionElem.firstChild().firstChild().nodeValue();
5651 }
5652 else
5653 {
5654 QgsDebugError( QStringLiteral( "unexpected child of %1 named %2" ).arg( vendorOptionElem.localName(), optionName ) );
5655 }
5656 }
5657
5658 if ( !optionName.isEmpty() && !optionValue.isEmpty() )
5659 {
5660 vendorOptions[ optionName ] = optionValue;
5661 }
5662
5663 vendorOptionElem = vendorOptionElem.nextSiblingElement();
5664 }
5665 if ( !vendorOptions.isEmpty() )
5666 {
5667 for ( QgsStringMap::iterator it = vendorOptions.begin(); it != vendorOptions.end(); ++it )
5668 {
5669 if ( it.key() == QLatin1String( "underlineText" ) && it.value() == QLatin1String( "true" ) )
5670 {
5671 font.setUnderline( true );
5672 format.setFont( font );
5673 }
5674 else if ( it.key() == QLatin1String( "strikethroughText" ) && it.value() == QLatin1String( "true" ) )
5675 {
5676 font.setStrikeOut( true );
5677 format.setFont( font );
5678 }
5679 else if ( it.key() == QLatin1String( "maxDisplacement" ) )
5680 {
5682 }
5683 else if ( it.key() == QLatin1String( "followLine" ) && it.value() == QLatin1String( "true" ) )
5684 {
5686 {
5688 }
5689 else
5690 {
5692 }
5693 }
5694 else if ( it.key() == QLatin1String( "maxAngleDelta" ) )
5695 {
5696 bool ok;
5697 double angle = it.value().toDouble( &ok );
5698 if ( ok )
5699 {
5700 settings.maxCurvedCharAngleIn = angle;
5701 settings.maxCurvedCharAngleOut = angle;
5702 }
5703 }
5704 // miscellaneous options
5705 else if ( it.key() == QLatin1String( "conflictResolution" ) && it.value() == QLatin1String( "false" ) )
5706 {
5708 }
5709 else if ( it.key() == QLatin1String( "forceLeftToRight" ) && it.value() == QLatin1String( "false" ) )
5710 {
5712 }
5713 else if ( it.key() == QLatin1String( "group" ) && it.value() == QLatin1String( "yes" ) )
5714 {
5715 settings.lineSettings().setMergeLines( true );
5716 }
5717 else if ( it.key() == QLatin1String( "labelAllGroup" ) && it.value() == QLatin1String( "true" ) )
5718 {
5719 settings.lineSettings().setMergeLines( true );
5720 }
5721 }
5722 }
5723
5724 format.setBuffer( bufferSettings );
5725 settings.setFormat( format );
5726 return true;
5727}
5728
5730{
5732
5733 return mEditFormConfig;
5734}
5735
5737{
5739
5740 if ( mEditFormConfig == editFormConfig )
5741 return;
5742
5743 mEditFormConfig = editFormConfig;
5744 mEditFormConfig.onRelationsLoaded();
5745 emit editFormConfigChanged();
5746}
5747
5749{
5751
5752 QgsAttributeTableConfig config = mAttributeTableConfig;
5753
5754 if ( config.isEmpty() )
5755 config.update( fields() );
5756
5757 return config;
5758}
5759
5761{
5763
5764 if ( mAttributeTableConfig != attributeTableConfig )
5765 {
5766 mAttributeTableConfig = attributeTableConfig;
5767 emit configChanged();
5768 }
5769}
5770
5772{
5773 // called in a non-thread-safe way in some cases when calculating aggregates in a different thread
5775
5777}
5778
5785
5787{
5789
5790 if ( !mDiagramLayerSettings )
5791 mDiagramLayerSettings = new QgsDiagramLayerSettings();
5792 *mDiagramLayerSettings = s;
5793}
5794
5796{
5798
5799 QgsLayerMetadataFormatter htmlFormatter( metadata() );
5800 QString myMetadata = QStringLiteral( "<html><head></head>\n<body>\n" );
5801
5802 myMetadata += generalHtmlMetadata();
5803
5804 // Begin Provider section
5805 myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" );
5806 myMetadata += QLatin1String( "<table class=\"list-view\">\n" );
5807
5808 // storage type
5809 if ( !storageType().isEmpty() )
5810 {
5811 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Storage" ) + QStringLiteral( "</td><td>" ) + storageType() + QStringLiteral( "</td></tr>\n" );
5812 }
5813
5814 // comment
5815 if ( !dataComment().isEmpty() )
5816 {
5817 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Comment" ) + QStringLiteral( "</td><td>" ) + dataComment() + QStringLiteral( "</td></tr>\n" );
5818 }
5819
5820 // encoding
5821 if ( const QgsVectorDataProvider *provider = dataProvider() )
5822 {
5823 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Encoding" ) + QStringLiteral( "</td><td>" ) + provider->encoding() + QStringLiteral( "</td></tr>\n" );
5824 myMetadata += provider->htmlMetadata();
5825 }
5826
5827 if ( isSpatial() )
5828 {
5829 // geom type
5831 if ( static_cast<int>( type ) < 0 || static_cast< int >( type ) > static_cast< int >( Qgis::GeometryType::Null ) )
5832 {
5833 QgsDebugMsgLevel( QStringLiteral( "Invalid vector type" ), 2 );
5834 }
5835 else
5836 {
5837 QString typeString( QStringLiteral( "%1 (%2)" ).arg( QgsWkbTypes::geometryDisplayString( geometryType() ),
5839 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Geometry" ) + QStringLiteral( "</td><td>" ) + typeString + QStringLiteral( "</td></tr>\n" );
5840 }
5841
5842 // Extent
5843 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Extent" ) + QStringLiteral( "</td><td>" ) + extent().toString() + QStringLiteral( "</td></tr>\n" );
5844 }
5845
5846 // feature count
5847 QLocale locale = QLocale();
5848 locale.setNumberOptions( locale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
5849 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" )
5850 + tr( "Feature count" ) + QStringLiteral( "</td><td>" )
5851 + ( featureCount() == -1 ? tr( "unknown" ) : locale.toString( static_cast<qlonglong>( featureCount() ) ) )
5852 + QStringLiteral( "</td></tr>\n" );
5853
5854 // End Provider section
5855 myMetadata += QLatin1String( "</table>\n<br><br>" );
5856
5857 if ( isSpatial() )
5858 {
5859 // CRS
5860 myMetadata += crsHtmlMetadata();
5861 }
5862
5863 // identification section
5864 myMetadata += QStringLiteral( "<h1>" ) + tr( "Identification" ) + QStringLiteral( "</h1>\n<hr>\n" );
5865 myMetadata += htmlFormatter.identificationSectionHtml( );
5866 myMetadata += QLatin1String( "<br><br>\n" );
5867
5868 // extent section
5869 myMetadata += QStringLiteral( "<h1>" ) + tr( "Extent" ) + QStringLiteral( "</h1>\n<hr>\n" );
5870 myMetadata += htmlFormatter.extentSectionHtml( isSpatial() );
5871 myMetadata += QLatin1String( "<br><br>\n" );
5872
5873 // Start the Access section
5874 myMetadata += QStringLiteral( "<h1>" ) + tr( "Access" ) + QStringLiteral( "</h1>\n<hr>\n" );
5875 myMetadata += htmlFormatter.accessSectionHtml( );
5876 myMetadata += QLatin1String( "<br><br>\n" );
5877
5878 // Fields section
5879 myMetadata += QStringLiteral( "<h1>" ) + tr( "Fields" ) + QStringLiteral( "</h1>\n<hr>\n<table class=\"list-view\">\n" );
5880
5881 // primary key
5883 if ( !pkAttrList.isEmpty() )
5884 {
5885 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Primary key attributes" ) + QStringLiteral( "</td><td>" );
5886 const auto constPkAttrList = pkAttrList;
5887 for ( int idx : constPkAttrList )
5888 {
5889 myMetadata += fields().at( idx ).name() + ' ';
5890 }
5891 myMetadata += QLatin1String( "</td></tr>\n" );
5892 }
5893
5894 const QgsFields myFields = fields();
5895
5896 // count fields
5897 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Count" ) + QStringLiteral( "</td><td>" ) + QString::number( myFields.size() ) + QStringLiteral( "</td></tr>\n" );
5898
5899 myMetadata += QLatin1String( "</table>\n<br><table width=\"100%\" class=\"tabular-view\">\n" );
5900 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" );
5901
5902 for ( int i = 0; i < myFields.size(); ++i )
5903 {
5904 QgsField myField = myFields.at( i );
5905 QString rowClass;
5906 if ( i % 2 )
5907 rowClass = QStringLiteral( "class=\"odd-row\"" );
5908 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" );
5909 }
5910
5911 //close field list
5912 myMetadata += QLatin1String( "</table>\n<br><br>" );
5913
5914 // Start the contacts section
5915 myMetadata += QStringLiteral( "<h1>" ) + tr( "Contacts" ) + QStringLiteral( "</h1>\n<hr>\n" );
5916 myMetadata += htmlFormatter.contactsSectionHtml( );
5917 myMetadata += QLatin1String( "<br><br>\n" );
5918
5919 // Start the links section
5920 myMetadata += QStringLiteral( "<h1>" ) + tr( "Links" ) + QStringLiteral( "</h1>\n<hr>\n" );
5921 myMetadata += htmlFormatter.linksSectionHtml( );
5922 myMetadata += QLatin1String( "<br><br>\n" );
5923
5924 // Start the history section
5925 myMetadata += QStringLiteral( "<h1>" ) + tr( "History" ) + QStringLiteral( "</h1>\n<hr>\n" );
5926 myMetadata += htmlFormatter.historySectionHtml( );
5927 myMetadata += QLatin1String( "<br><br>\n" );
5928
5929 myMetadata += customPropertyHtmlMetadata();
5930
5931 myMetadata += QLatin1String( "\n</body>\n</html>\n" );
5932 return myMetadata;
5933}
5934
5935void QgsVectorLayer::invalidateSymbolCountedFlag()
5936{
5938
5939 mSymbolFeatureCounted = false;
5940}
5941
5942void QgsVectorLayer::onFeatureCounterCompleted()
5943{
5945
5946 onSymbolsCounted();
5947 mFeatureCounter = nullptr;
5948}
5949
5950void QgsVectorLayer::onFeatureCounterTerminated()
5951{
5953
5954 mFeatureCounter = nullptr;
5955}
5956
5957void QgsVectorLayer::onJoinedFieldsChanged()
5958{
5960
5961 // some of the fields of joined layers have changed -> we need to update this layer's fields too
5962 updateFields();
5963}
5964
5965void QgsVectorLayer::onFeatureDeleted( QgsFeatureId fid )
5966{
5968
5969 if ( mEditCommandActive || mCommitChangesActive )
5970 {
5971 mDeletedFids << fid;
5972 }
5973 else
5974 {
5975 mSelectedFeatureIds.remove( fid );
5976 emit featuresDeleted( QgsFeatureIds() << fid );
5977 }
5978
5979 emit featureDeleted( fid );
5980}
5981
5982void QgsVectorLayer::onRelationsLoaded()
5983{
5985
5986 mEditFormConfig.onRelationsLoaded();
5987}
5988
5989void QgsVectorLayer::onSymbolsCounted()
5990{
5992
5993 if ( mFeatureCounter )
5994 {
5995 mSymbolFeatureCounted = true;
5996 mSymbolFeatureCountMap = mFeatureCounter->symbolFeatureCountMap();
5997 mSymbolFeatureIdMap = mFeatureCounter->symbolFeatureIdMap();
5999 }
6000}
6001
6002QList<QgsRelation> QgsVectorLayer::referencingRelations( int idx ) const
6003{
6005
6006 if ( QgsProject *p = project() )
6007 return p->relationManager()->referencingRelations( this, idx );
6008 else
6009 return {};
6010}
6011
6012QList<QgsWeakRelation> QgsVectorLayer::weakRelations() const
6013{
6015
6016 return mWeakRelations;
6017}
6018
6019void QgsVectorLayer::setWeakRelations( const QList<QgsWeakRelation> &relations )
6020{
6022
6023 mWeakRelations = relations;
6024}
6025
6026bool QgsVectorLayer::loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage, const QString &key )
6027{
6029
6030 bool rc = false;
6031
6032 QString joinKey = mAuxiliaryLayerKey;
6033 if ( !key.isEmpty() )
6034 joinKey = key;
6035
6036 if ( storage.isValid() && !joinKey.isEmpty() )
6037 {
6038 QgsAuxiliaryLayer *alayer = nullptr;
6039
6040 int idx = fields().lookupField( joinKey );
6041
6042 if ( idx >= 0 )
6043 {
6044 alayer = storage.createAuxiliaryLayer( fields().field( idx ), this );
6045
6046 if ( alayer )
6047 {
6048 setAuxiliaryLayer( alayer );
6049 rc = true;
6050 }
6051 }
6052 }
6053
6054 return rc;
6055}
6056
6058{
6060
6061 mAuxiliaryLayerKey.clear();
6062
6063 if ( mAuxiliaryLayer )
6064 removeJoin( mAuxiliaryLayer->id() );
6065
6066 if ( alayer )
6067 {
6068 addJoin( alayer->joinInfo() );
6069
6070 if ( !alayer->isEditable() )
6071 alayer->startEditing();
6072
6073 mAuxiliaryLayerKey = alayer->joinInfo().targetFieldName();
6074 }
6075
6076 mAuxiliaryLayer.reset( alayer );
6077 if ( mAuxiliaryLayer )
6078 mAuxiliaryLayer->setParent( this );
6079 updateFields();
6080}
6081
6083{
6085
6086 return mAuxiliaryLayer.get();
6087}
6088
6090{
6092
6093 return mAuxiliaryLayer.get();
6094}
6095
6096QSet<QgsMapLayerDependency> QgsVectorLayer::dependencies() const
6097{
6099
6100 if ( mDataProvider )
6101 return mDataProvider->dependencies() + mDependencies;
6102 return mDependencies;
6103}
6104
6105void QgsVectorLayer::emitDataChanged()
6106{
6108
6109 if ( mDataChangedFired )
6110 return;
6111
6112 updateExtents(); // reset cached extent to reflect data changes
6113
6114 mDataChangedFired = true;
6115 emit dataChanged();
6116 mDataChangedFired = false;
6117}
6118
6119void QgsVectorLayer::onAfterCommitChangesDependency()
6120{
6122
6123 mDataChangedFired = true;
6124 reload();
6125 mDataChangedFired = false;
6126}
6127
6128bool QgsVectorLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
6129{
6131
6132 QSet<QgsMapLayerDependency> deps;
6133 const auto constODeps = oDeps;
6134 for ( const QgsMapLayerDependency &dep : constODeps )
6135 {
6136 if ( dep.origin() == QgsMapLayerDependency::FromUser )
6137 deps << dep;
6138 }
6139
6140 QSet<QgsMapLayerDependency> toAdd = deps - dependencies();
6141
6142 // disconnect layers that are not present in the list of dependencies anymore
6143 if ( QgsProject *p = project() )
6144 {
6145 for ( const QgsMapLayerDependency &dep : std::as_const( mDependencies ) )
6146 {
6147 QgsVectorLayer *lyr = static_cast<QgsVectorLayer *>( p->mapLayer( dep.layerId() ) );
6148 if ( !lyr )
6149 continue;
6150 disconnect( lyr, &QgsVectorLayer::featureAdded, this, &QgsVectorLayer::emitDataChanged );
6151 disconnect( lyr, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayer::emitDataChanged );
6152 disconnect( lyr, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayer::emitDataChanged );
6153 disconnect( lyr, &QgsVectorLayer::dataChanged, this, &QgsVectorLayer::emitDataChanged );
6155 disconnect( lyr, &QgsVectorLayer::afterCommitChanges, this, &QgsVectorLayer::onAfterCommitChangesDependency );
6156 }
6157 }
6158
6159 // assign new dependencies
6160 if ( mDataProvider )
6161 mDependencies = mDataProvider->dependencies() + deps;
6162 else
6163 mDependencies = deps;
6164 emit dependenciesChanged();
6165
6166 // connect to new layers
6167 if ( QgsProject *p = project() )
6168 {
6169 for ( const QgsMapLayerDependency &dep : std::as_const( mDependencies ) )
6170 {
6171 QgsVectorLayer *lyr = static_cast<QgsVectorLayer *>( p->mapLayer( dep.layerId() ) );
6172 if ( !lyr )
6173 continue;
6174 connect( lyr, &QgsVectorLayer::featureAdded, this, &QgsVectorLayer::emitDataChanged );
6175 connect( lyr, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayer::emitDataChanged );
6176 connect( lyr, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayer::emitDataChanged );
6177 connect( lyr, &QgsVectorLayer::dataChanged, this, &QgsVectorLayer::emitDataChanged );
6179 connect( lyr, &QgsVectorLayer::afterCommitChanges, this, &QgsVectorLayer::onAfterCommitChangesDependency );
6180 }
6181 }
6182
6183 // if new layers are present, emit a data change
6184 if ( ! toAdd.isEmpty() )
6185 emitDataChanged();
6186
6187 return true;
6188}
6189
6191{
6193
6194 if ( fieldIndex < 0 || fieldIndex >= mFields.count() || !mDataProvider )
6196
6197 QgsFieldConstraints::Constraints constraints = mFields.at( fieldIndex ).constraints().constraints();
6198
6199 // make sure provider constraints are always present!
6200 if ( mFields.fieldOrigin( fieldIndex ) == Qgis::FieldOrigin::Provider )
6201 {
6202 constraints |= mDataProvider->fieldConstraints( mFields.fieldOriginIndex( fieldIndex ) );
6203 }
6204
6205 return constraints;
6206}
6207
6208QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength> QgsVectorLayer::fieldConstraintsAndStrength( int fieldIndex ) const
6209{
6211
6212 QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength > m;
6213
6214 if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
6215 return m;
6216
6217 QString name = mFields.at( fieldIndex ).name();
6218
6219 QMap< QPair< QString, QgsFieldConstraints::Constraint >, QgsFieldConstraints::ConstraintStrength >::const_iterator conIt = mFieldConstraintStrength.constBegin();
6220 for ( ; conIt != mFieldConstraintStrength.constEnd(); ++conIt )
6221 {
6222 if ( conIt.key().first == name )
6223 {
6224 m[ conIt.key().second ] = mFieldConstraintStrength.value( conIt.key() );
6225 }
6226 }
6227
6228 return m;
6229}
6230
6232{
6234
6235 if ( index < 0 || index >= mFields.count() )
6236 return;
6237
6238 QString name = mFields.at( index ).name();
6239
6240 // add constraint to existing constraints
6241 QgsFieldConstraints::Constraints constraints = mFieldConstraints.value( name, QgsFieldConstraints::Constraints() );
6242 constraints |= constraint;
6243 mFieldConstraints.insert( name, constraints );
6244
6245 mFieldConstraintStrength.insert( qMakePair( name, constraint ), strength );
6246
6247 updateFields();
6248}
6249
6251{
6253
6254 if ( index < 0 || index >= mFields.count() )
6255 return;
6256
6257 QString name = mFields.at( index ).name();
6258
6259 // remove constraint from existing constraints
6260 QgsFieldConstraints::Constraints constraints = mFieldConstraints.value( name, QgsFieldConstraints::Constraints() );
6261 constraints &= ~constraint;
6262 mFieldConstraints.insert( name, constraints );
6263
6264 mFieldConstraintStrength.remove( qMakePair( name, constraint ) );
6265
6266 updateFields();
6267}
6268
6270{
6272
6273 if ( index < 0 || index >= mFields.count() )
6274 return QString();
6275
6276 return mFields.at( index ).constraints().constraintExpression();
6277}
6278
6280{
6282
6283 if ( index < 0 || index >= mFields.count() )
6284 return QString();
6285
6286 return mFields.at( index ).constraints().constraintDescription();
6287}
6288
6289void QgsVectorLayer::setConstraintExpression( int index, const QString &expression, const QString &description )
6290{
6292
6293 if ( index < 0 || index >= mFields.count() )
6294 return;
6295
6296 if ( expression.isEmpty() )
6297 {
6298 mFieldConstraintExpressions.remove( mFields.at( index ).name() );
6299 }
6300 else
6301 {
6302 mFieldConstraintExpressions.insert( mFields.at( index ).name(), qMakePair( expression, description ) );
6303 }
6304 updateFields();
6305}
6306
6308{
6310
6311 if ( index < 0 || index >= mFields.count() )
6312 return;
6313
6314 mFieldConfigurationFlags.insert( mFields.at( index ).name(), flags );
6315 updateFields();
6316}
6317
6319{
6321
6322 if ( index < 0 || index >= mFields.count() )
6323 return;
6325 flags.setFlag( flag, active );
6327}
6328
6330{
6332
6333 if ( index < 0 || index >= mFields.count() )
6335
6336 return mFields.at( index ).configurationFlags();
6337}
6338
6340{
6342
6343 if ( index < 0 || index >= mFields.count() )
6344 return;
6345
6346 if ( setup.isNull() )
6347 mFieldWidgetSetups.remove( mFields.at( index ).name() );
6348 else
6349 mFieldWidgetSetups.insert( mFields.at( index ).name(), setup );
6350 updateFields();
6351}
6352
6354{
6356
6357 if ( index < 0 || index >= mFields.count() )
6358 return QgsEditorWidgetSetup();
6359
6360 return mFields.at( index ).editorWidgetSetup();
6361}
6362
6363QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties()
6364{
6366
6368 if ( customProperty( QStringLiteral( "labeling" ) ).toString() == QLatin1String( "pal" ) )
6369 {
6370 if ( customProperty( QStringLiteral( "labeling/enabled" ), QVariant( false ) ).toBool() )
6371 {
6372 // try to load from custom properties
6373 QgsPalLayerSettings settings;
6374 settings.readFromLayerCustomProperties( this );
6375 labeling = new QgsVectorLayerSimpleLabeling( settings );
6376 }
6377
6378 // also clear old-style labeling config
6379 removeCustomProperty( QStringLiteral( "labeling" ) );
6380 const auto constCustomPropertyKeys = customPropertyKeys();
6381 for ( const QString &key : constCustomPropertyKeys )
6382 {
6383 if ( key.startsWith( QLatin1String( "labeling/" ) ) )
6384 removeCustomProperty( key );
6385 }
6386 }
6387
6388 return labeling;
6389}
6390
6392{
6394
6395 return mAllowCommit;
6396}
6397
6398void QgsVectorLayer::setAllowCommit( bool allowCommit )
6399{
6401
6402 if ( mAllowCommit == allowCommit )
6403 return;
6404
6405 mAllowCommit = allowCommit;
6406 emit allowCommitChanged();
6407}
6408
6410{
6412
6413 return mGeometryOptions.get();
6414}
6415
6416void QgsVectorLayer::setReadExtentFromXml( bool readExtentFromXml )
6417{
6419
6420 mReadExtentFromXml = readExtentFromXml;
6421}
6422
6424{
6426
6427 return mReadExtentFromXml;
6428}
6429
6430void QgsVectorLayer::onDirtyTransaction( const QString &sql, const QString &name )
6431{
6433
6435 if ( tr && mEditBuffer )
6436 {
6437 qobject_cast<QgsVectorLayerEditPassthrough *>( mEditBuffer )->update( tr, sql, name );
6438 }
6439}
6440
6441QList<QgsVectorLayer *> QgsVectorLayer::DeleteContext::handledLayers( bool includeAuxiliaryLayers ) const
6442{
6443 QList<QgsVectorLayer *> layers;
6444 QMap<QgsVectorLayer *, QgsFeatureIds>::const_iterator i;
6445 for ( i = mHandledFeatures.begin(); i != mHandledFeatures.end(); ++i )
6446 {
6447 if ( includeAuxiliaryLayers || !qobject_cast< QgsAuxiliaryLayer * >( i.key() ) )
6448 layers.append( i.key() );
6449 }
6450 return layers;
6451}
6452
6454{
6455 return mHandledFeatures[layer];
6456}
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
@ SelectAtId
Fast access to features using their ID.
@ CreateRenderer
Provider can create feature renderers using backend-specific formatting information....
@ CreateLabeling
Provider can set labeling settings using backend-specific formatting information. Since QGIS 3....
@ ReadLayerMetadata
Provider can read layer metadata from data store. Since QGIS 3.0. See QgsDataProvider::layerMetadata(...
@ DeleteFeatures
Allows deletion of features.
QFlags< VectorRenderingSimplificationFlag > VectorRenderingSimplificationFlags
Simplification flags for vector feature rendering.
Definition qgis.h:2813
@ 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:1889
@ 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:522
@ Unknown
Spatial index presence cannot be determined, index may or may not exist.
VectorRenderingSimplificationFlag
Simplification flags for vector feature rendering.
Definition qgis.h:2798
@ NoSimplification
No simplification can be applied.
@ 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.
QFlags< VectorLayerTypeFlag > VectorLayerTypeFlags
Vector layer type flags.
Definition qgis.h:395
VectorSimplificationAlgorithm
Simplification algorithms for vector features.
Definition qgis.h:2782
@ Distance
The simplification uses the distance between points to remove duplicate points.
@ 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.
@ FastExtent3D
Provider's 3D extent retrieval via QgsDataProvider::extent3D() is always guaranteed to be trivial/fas...
@ FastExtent2D
Provider's 2D extent retrieval via QgsDataProvider::extent() is always guaranteed to be trivial/fast ...
@ 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:3620
@ Duplicate
Duplicate original value.
BlendMode
Blending modes defining the available composition modes that can be used when painting.
Definition qgis.h:4586
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ Polygon
Polygons.
@ Unknown
Unknown types.
@ Null
No geometry.
@ Generated
A generated relation is a child of a polymorphic relation.
@ Normal
A normal relation.
FieldDuplicatePolicy
Duplicate policy for fields.
Definition qgis.h:3652
@ Duplicate
Duplicate original value.
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition qgis.h:5582
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
Definition qgis.h:450
FeatureAvailability
Possible return value for QgsFeatureSource::hasFeatures() to determine if a source is empty.
Definition qgis.h:541
@ 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.
FieldOrigin
Field origin.
Definition qgis.h:1551
@ Provider
Field originates from the underlying data provider of the vector layer.
@ Edit
Field has been temporarily added in editing mode.
@ Unknown
The field origin has not been specified.
@ Expression
Field is calculated from an expression.
@ Join
Field originates from a joined layer.
RenderUnit
Rendering size units.
Definition qgis.h:4839
@ Points
Points (e.g., for font sizes)
@ LoadDefaultStyle
Reset the layer's style to the default for the datasource.
@ ForceReadOnly
Open layer in a read-only mode.
Aggregate
Available aggregates to calculate.
Definition qgis.h:5400
VertexMarkerType
Editing vertex markers, used for showing vertices during a edit operation.
Definition qgis.h:1680
@ SemiTransparentCircle
Semi-transparent circle marker.
@ Cross
Cross marker.
VectorEditResult
Specifies the result of a vector layer edit operation.
Definition qgis.h:1665
@ 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:256
@ Unknown
Unknown.
FieldConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1568
@ 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 ...
QFlags< FieldConfigurationFlag > FieldConfigurationFlags
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1583
@ AlwaysAllowUpsideDown
Show upside down for all labels, including dynamic ones.
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1618
@ 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.
virtual QString subsetString() const
Returns the subset definition string currently in use by the layer and used by the provider to limit ...
QgsCoordinateTransformContext transformContext() const
Returns data provider coordinate transform context.
virtual Qgis::DataProviderFlags flags() const
Returns the generic data provider flags.
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 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 bool setSubsetString(const QString &subset, bool updateFeatureCount=true)
Set the subset string used to create a subset of features in the layer.
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Gets the data source specification.
virtual void updateExtents()
Update the extents of 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:126
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.
Abstract base class for all 2D vector feature renderers.
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.
QFlags< Flag > Flags
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:58
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
QgsAttributes attributes
Definition qgsfeature.h:67
QgsFields fields
Definition qgsfeature.h:68
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isValid() const
Returns the validity of this feature.
Q_INVOKABLE 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.
QFlags< Constraint > Constraints
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:160
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:102
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:94
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:739
Container of fields for a vector layer.
Definition qgsfields.h:46
int count
Definition qgsfields.h:50
bool isEmpty
Definition qgsfields.h:49
Q_INVOKABLE int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
Q_INVOKABLE int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Qgis::FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
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).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QStringList names
Definition qgsfields.h:51
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.
Alters the size of rendered diagrams using a linear scaling.
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:76
QString name
Definition qgsmaplayer.h:80
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.
static Qgis::DataProviderReadFlags providerReadFlags(const QDomNode &layerNode, QgsMapLayer::ReadFlags layerReadFlags)
Returns provider read flag deduced from layer read flags layerReadFlags and a dom node layerNode that...
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:83
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.
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 setDataSource(const QString &dataSource, const QString &baseName=QString(), const QString &provider=QString(), bool loadDefaultStyleFlag=false)
Updates the data source of the layer.
QString id
Definition qgsmaplayer.h:79
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:82
Qgis::LayerType type
Definition qgsmaplayer.h:86
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.
QFlags< StyleCategory > StyleCategories
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.
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:90
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
double opacity
Definition qgsmaplayer.h:88
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.
@ 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 customPropertyHtmlMetadata() const
Returns an HTML fragment containing custom property information, for use in the htmlMetadata() method...
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:89
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 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.
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
Translates a string using the Qt QTranslator mechanism.
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.
bool rollBack(QStringList &rollbackErrors, bool stopEditing=true, QgsVectorLayer *vectorLayer=nullptr)
Stops a current editing operation on vectorLayer and discards any uncommitted edits.
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.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
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.
void relationsLoaded()
Emitted when the relations were loaded after reading a project.
Represents a relationship between two vector layers.
Definition qgsrelation.h:44
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.
Renders diagrams using mixed diagram render types.
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....
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 Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
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.
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)
Emitted after attribute deletion has been committed to the layer.
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)
Emitted after feature attribute value changes have been committed to the layer.
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)
Emitted after attribute addition has been committed to the layer.
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)
Emitted after feature addition has been committed to the layer.
void featureDeleted(QgsFeatureId fid)
Emitted when a feature was deleted from the buffer.
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)
Emitted when an attribute was added to the buffer.
void committedGeometriesChanges(const QString &layerId, const QgsGeometryMap &changedGeometries)
Emitted after feature geometry changes have been committed to the layer.
virtual bool addFeature(QgsFeature &f)
Adds a feature.
virtual void rollBack()
Stop editing and discard the edits.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Emitted when a feature's attribute value has been changed.
void attributeDeleted(int idx)
Emitted when an attribute was deleted from the buffer.
void featureAdded(QgsFeatureId fid)
Emitted when a feature has been added to the buffer.
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)
Emitted after feature removal has been committed to the layer.
Contains utility functions for editing vector layers.
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.
A feature iterator which iterates over features from a QgsVectorLayer.
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...
Contains settings which reflect the context in which vector layer tool operations should consider.
QgsExpressionContext * expressionContext() const
Returns the optional expression context used by the vector layer tools.
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.
static const QgsSettingsEntryEnumFlag< Qgis::VectorRenderingSimplificationFlags > * settingsSimplifyDrawingHints
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
Write the style for the layer into the document provided.
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
Write just the symbology information for the layer into the document.
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.
Q_INVOKABLE bool deleteFeatures(const QgsFeatureIds &fids, QgsVectorLayer::DeleteContext *context=nullptr)
Deletes a set of features from the layer (but does not commit it)
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_INVOKABLE bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant(), bool skipDefaultValues=false, QgsVectorLayerToolsContext *context=nullptr)
Changes an attribute value for a feature (but does not immediately commit the changes).
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.
void deselect(QgsFeatureId featureId)
Deselects feature by its ID.
void allowCommitChanged()
Emitted whenever the allowCommit() 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 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,...
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
Read the symbology for the current layer from the DOM node supplied.
void removeFeatureRendererGenerator(const QString &id)
Removes the feature renderer with matching id from the layer.
Q_INVOKABLE bool deleteFeature(QgsFeatureId fid, QgsVectorLayer::DeleteContext *context=nullptr)
Deletes a feature from the layer (but does not commit it).
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.
static const QgsSettingsEntryEnumFlag< Qgis::VectorSimplificationAlgorithm > * settingsSimplifyAlgorithm
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 simplifyDrawingCanbeApplied(const QgsRenderContext &renderContext, Qgis::VectorRenderingSimplificationFlag simplifyHint) const
Returns whether the VectorLayer can apply the specified simplification hint.
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)
Sets the editor widget setup for the field at the specified index.
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
Read the style for the current layer from the DOM node supplied.
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.
void setFieldDuplicatePolicy(int index, Qgis::FieldDuplicatePolicy policy)
Sets a duplicate policy for the field with the specified index.
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
Returns the editor widget setup for the field at the specified index.
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
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 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.
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...
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 QgsFeature getFeature(QgsFeatureId fid) const
Queries the layer for the feature with the given id.
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.
Q_INVOKABLE bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues=QgsAttributeMap(), bool skipDefaultValues=false, QgsVectorLayerToolsContext *context=nullptr)
Changes attributes' values for a feature (but does not immediately commit the changes).
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,...
Qgis::VectorRenderingSimplificationFlags simplifyHints() const
Gets the simplification hints of the vector layer managed.
float maximumScale() const
Gets the maximum scale at which the layer should be simplified.
Qgis::VectorSimplificationAlgorithm simplifyAlgorithm() const
Gets the local simplification algorithm of the vector layer managed.
void setThreshold(float threshold)
Sets the simplification threshold of the vector layer managed.
void setForceLocalOptimization(bool localOptimization)
Sets where the simplification executes, after fetch the geometries from provider, or when supported,...
void setSimplifyHints(Qgis::VectorRenderingSimplificationFlags simplifyHints)
Sets the simplification hints of the vector layer managed.
float threshold() const
Gets the simplification threshold of the vector layer managed.
void setMaximumScale(float maximumScale)
Sets the maximum scale at which the layer should be simplified.
void setSimplifyAlgorithm(Qgis::VectorSimplificationAlgorithm simplifyAlgorithm)
Sets the local simplification algorithm of the vector layer managed.
@ 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:6127
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6108
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:6166
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:6188
QMap< QString, QString > QgsStringMap
Definition qgis.h:6455
QVector< QgsPoint > QgsPointSequence
QMap< int, QVariant > QgsAttributeMap
QList< QgsFeature > QgsFeatureList
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:53
#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.