QGIS API Documentation 3.43.0-Master (8fc5848dca1)
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 "moc_qgsvectorlayer.cpp"
28#include "qgsactionmanager.h"
29#include "qgsapplication.h"
30#include "qgsconditionalstyle.h"
32#include "qgscurve.h"
33#include "qgsdatasourceuri.h"
36#include "qgsfeature.h"
37#include "qgsfeaturerequest.h"
38#include "qgsfields.h"
39#include "qgsmaplayerfactory.h"
41#include "qgsgeometry.h"
43#include "qgslogger.h"
44#include "qgsmaplayerlegend.h"
45#include "qgsmessagelog.h"
46#include "qgsogcutils.h"
47#include "qgspainting.h"
48#include "qgspointxy.h"
49#include "qgsproject.h"
50#include "qgsproviderregistry.h"
51#include "qgsrectangle.h"
52#include "qgsrelationmanager.h"
53#include "qgsweakrelation.h"
54#include "qgsrendercontext.h"
67#include "qgspoint.h"
68#include "qgsrenderer.h"
69#include "qgssymbollayer.h"
70#include "qgsdiagramrenderer.h"
71#include "qgspallabeling.h"
75#include "qgsfeedback.h"
76#include "qgsxmlutils.h"
77#include "qgstaskmanager.h"
78#include "qgstransaction.h"
79#include "qgsauxiliarystorage.h"
80#include "qgsgeometryoptions.h"
82#include "qgsruntimeprofiler.h"
84#include "qgsvectorlayerutils.h"
86#include "qgsprofilerequest.h"
87#include "qgssymbollayerutils.h"
88#include "qgsthreadingutils.h"
89#include "qgssldexportcontext.h"
90
91#include <QDir>
92#include <QFile>
93#include <QImage>
94#include <QPainter>
95#include <QPainterPath>
96#include <QPolygonF>
97#include <QProgressDialog>
98#include <QString>
99#include <QDomNode>
100#include <QVector>
101#include <QStringBuilder>
102#include <QUrl>
103#include <QUndoCommand>
104#include <QUrlQuery>
105#include <QUuid>
106#include <QRegularExpression>
107#include <QTimer>
108
109#include <limits>
110#include <optional>
111
113#include "qgssettingsentryimpl.h"
114#include "qgssettingstree.h"
115
121
122
123#ifdef TESTPROVIDERLIB
124#include <dlfcn.h>
125#endif
126
127typedef bool saveStyle_t(
128 const QString &uri,
129 const QString &qmlStyle,
130 const QString &sldStyle,
131 const QString &styleName,
132 const QString &styleDescription,
133 const QString &uiFileContent,
134 bool useAsDefault,
135 QString &errCause
136);
137
138typedef QString loadStyle_t(
139 const QString &uri,
140 QString &errCause
141);
142
143typedef int listStyles_t(
144 const QString &uri,
145 QStringList &ids,
146 QStringList &names,
147 QStringList &descriptions,
148 QString &errCause
149);
150
151typedef QString getStyleById_t(
152 const QString &uri,
153 QString styleID,
154 QString &errCause
155);
156
157typedef bool deleteStyleById_t(
158 const QString &uri,
159 QString styleID,
160 QString &errCause
161);
162
163
164QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
165 const QString &baseName,
166 const QString &providerKey,
167 const QgsVectorLayer::LayerOptions &options )
168 : QgsMapLayer( Qgis::LayerType::Vector, baseName, vectorLayerPath )
169 , mSelectionProperties( new QgsVectorLayerSelectionProperties( this ) )
170 , mTemporalProperties( new QgsVectorLayerTemporalProperties( this ) )
171 , mElevationProperties( new QgsVectorLayerElevationProperties( this ) )
172 , mAuxiliaryLayer( nullptr )
173 , mAuxiliaryLayerKey( QString() )
174 , mReadExtentFromXml( options.readExtentFromXml )
175 , mRefreshRendererTimer( new QTimer( this ) )
176{
178 mLoadAllStoredStyle = options.loadAllStoredStyles;
179
180 if ( options.fallbackCrs.isValid() )
181 setCrs( options.fallbackCrs, false );
182 mWkbType = options.fallbackWkbType;
183
184 setProviderType( providerKey );
185
186 mGeometryOptions = std::make_unique<QgsGeometryOptions>();
187 mActions = new QgsActionManager( this );
188 mConditionalStyles = new QgsConditionalLayerStyles( this );
189 mStoredExpressionManager = new QgsStoredExpressionManager();
190 mStoredExpressionManager->setParent( this );
191
192 mJoinBuffer = new QgsVectorLayerJoinBuffer( this );
193 mJoinBuffer->setParent( this );
194 connect( mJoinBuffer, &QgsVectorLayerJoinBuffer::joinedFieldsChanged, this, &QgsVectorLayer::onJoinedFieldsChanged );
195
196 mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
197 // if we're given a provider type, try to create and bind one to this layer
198 if ( !vectorLayerPath.isEmpty() && !mProviderKey.isEmpty() )
199 {
200 QgsDataProvider::ProviderOptions providerOptions { options.transformContext };
201 Qgis::DataProviderReadFlags providerFlags;
202 if ( options.loadDefaultStyle )
203 {
205 }
206 if ( options.forceReadOnly )
207 {
209 mDataSourceReadOnly = true;
210 }
211 setDataSource( vectorLayerPath, baseName, providerKey, providerOptions, providerFlags );
212 }
213
214 for ( const QgsField &field : std::as_const( mFields ) )
215 {
216 if ( !mAttributeAliasMap.contains( field.name() ) )
217 mAttributeAliasMap.insert( field.name(), QString() );
218 }
219
220 if ( isValid() )
221 {
222 mTemporalProperties->setDefaultsFromDataProviderTemporalCapabilities( mDataProvider->temporalCapabilities() );
223 if ( !mTemporalProperties->isActive() )
224 {
225 // didn't populate temporal properties from provider metadata, so at least try to setup some initially nice
226 // selections
227 mTemporalProperties->guessDefaultsFromFields( mFields );
228 }
229
230 mElevationProperties->setDefaultsFromLayer( this );
231 }
232
233 connect( this, &QgsVectorLayer::selectionChanged, this, [this] { triggerRepaint(); } );
234 connect( QgsProject::instance()->relationManager(), &QgsRelationManager::relationsLoaded, this, &QgsVectorLayer::onRelationsLoaded ); // skip-keyword-check
235
239
240 // Default simplify drawing settings
241 QgsSettings settings;
242 mSimplifyMethod.setSimplifyHints( QgsVectorLayer::settingsSimplifyDrawingHints->valueWithDefaultOverride( mSimplifyMethod.simplifyHints() ) );
243 mSimplifyMethod.setSimplifyAlgorithm( QgsVectorLayer::settingsSimplifyAlgorithm->valueWithDefaultOverride( mSimplifyMethod.simplifyAlgorithm() ) );
244 mSimplifyMethod.setThreshold( QgsVectorLayer::settingsSimplifyDrawingTol->valueWithDefaultOverride( mSimplifyMethod.threshold() ) );
245 mSimplifyMethod.setForceLocalOptimization( QgsVectorLayer::settingsSimplifyLocal->valueWithDefaultOverride( mSimplifyMethod.forceLocalOptimization() ) );
246 mSimplifyMethod.setMaximumScale( QgsVectorLayer::settingsSimplifyMaxScale->valueWithDefaultOverride( mSimplifyMethod.maximumScale() ) );
247
248 connect( mRefreshRendererTimer, &QTimer::timeout, this, [this] { triggerRepaint( true ); } );
249}
250
252{
253 emit willBeDeleted();
254
255 setValid( false );
256
257 delete mDataProvider;
258 delete mEditBuffer;
259 delete mJoinBuffer;
260 delete mExpressionFieldBuffer;
261 delete mLabeling;
262 delete mDiagramLayerSettings;
263 delete mDiagramRenderer;
264
265 delete mActions;
266
267 delete mRenderer;
268 delete mConditionalStyles;
269 delete mStoredExpressionManager;
270
271 if ( mFeatureCounter )
272 mFeatureCounter->cancel();
273
274 qDeleteAll( mRendererGenerators );
275}
276
278{
280
282 // We get the data source string from the provider when
283 // possible because some providers may have changed it
284 // directly (memory provider does that).
285 QString dataSource;
286 if ( mDataProvider )
287 {
288 dataSource = mDataProvider->dataSourceUri();
289 options.transformContext = mDataProvider->transformContext();
290 }
291 else
292 {
293 dataSource = source();
294 }
295 options.forceReadOnly = mDataSourceReadOnly;
296 QgsVectorLayer *layer = new QgsVectorLayer( dataSource, name(), mProviderKey, options );
297 if ( mDataProvider && layer->dataProvider() )
298 {
299 layer->dataProvider()->handlePostCloneOperations( mDataProvider );
300 }
301 QgsMapLayer::clone( layer );
302 layer->mXmlExtent2D = mXmlExtent2D;
303 layer->mLazyExtent2D = mLazyExtent2D;
304 layer->mValidExtent2D = mValidExtent2D;
305 layer->mXmlExtent3D = mXmlExtent3D;
306 layer->mLazyExtent3D = mLazyExtent3D;
307 layer->mValidExtent3D = mValidExtent3D;
308
309 QList<QgsVectorLayerJoinInfo> joins = vectorJoins();
310 const auto constJoins = joins;
311 for ( const QgsVectorLayerJoinInfo &join : constJoins )
312 {
313 // do not copy join information for auxiliary layer
314 if ( !auxiliaryLayer()
315 || ( auxiliaryLayer() && auxiliaryLayer()->id() != join.joinLayerId() ) )
316 layer->addJoin( join );
317 }
318
319 if ( mDataProvider )
320 layer->setProviderEncoding( mDataProvider->encoding() );
321 layer->setSubsetString( subsetString() );
325 layer->setReadOnly( isReadOnly() );
330
331 const auto constActions = actions()->actions();
332 for ( const QgsAction &action : constActions )
333 {
334 layer->actions()->addAction( action );
335 }
336
337 if ( auto *lRenderer = renderer() )
338 {
339 layer->setRenderer( lRenderer->clone() );
340 }
341
342 if ( auto *lLabeling = labeling() )
343 {
344 layer->setLabeling( lLabeling->clone() );
345 }
347
349
350 if ( auto *lDiagramRenderer = diagramRenderer() )
351 {
352 layer->setDiagramRenderer( lDiagramRenderer->clone() );
353 }
354
355 if ( auto *lDiagramLayerSettings = diagramLayerSettings() )
356 {
357 layer->setDiagramLayerSettings( *lDiagramLayerSettings );
358 }
359
360 for ( int i = 0; i < fields().count(); i++ )
361 {
362 layer->setFieldAlias( i, attributeAlias( i ) );
364 layer->setEditorWidgetSetup( i, editorWidgetSetup( i ) );
367
368 QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength> constraints = fieldConstraintsAndStrength( i );
369 auto constraintIt = constraints.constBegin();
370 for ( ; constraintIt != constraints.constEnd(); ++ constraintIt )
371 {
372 layer->setFieldConstraint( i, constraintIt.key(), constraintIt.value() );
373 }
374
375 if ( fields().fieldOrigin( i ) == Qgis::FieldOrigin::Expression )
376 {
377 layer->addExpressionField( expressionField( i ), fields().at( i ) );
378 }
379 }
380
382
383 if ( auto *lAuxiliaryLayer = auxiliaryLayer() )
384 layer->setAuxiliaryLayer( lAuxiliaryLayer->clone( layer ) );
385
386 layer->mElevationProperties = mElevationProperties->clone();
387 layer->mElevationProperties->setParent( layer );
388
389 layer->mSelectionProperties = mSelectionProperties->clone();
390 layer->mSelectionProperties->setParent( layer );
391
392 return layer;
393}
394
396{
398
399 if ( mDataProvider )
400 {
401 return mDataProvider->storageType();
402 }
403 return QString();
404}
405
406
408{
410
411 if ( mDataProvider )
412 {
413 return mDataProvider->capabilitiesString();
414 }
415 return QString();
416}
417
419{
421
422 return mDataProvider && mDataProvider->isSqlQuery();
423}
424
431
433{
435
436 if ( mDataProvider )
437 {
438 return mDataProvider->dataComment();
439 }
440 return QString();
441}
442
449
451{
453
454 return name();
455}
456
458{
459 // non fatal for now -- the QgsVirtualLayerTask class is not thread safe and calls this
461
462 if ( mDataProvider )
463 {
464 mDataProvider->reloadData();
465 updateFields();
466 }
467}
468
470{
471 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
473
474 return new QgsVectorLayerRenderer( this, rendererContext );
475}
476
477
478void QgsVectorLayer::drawVertexMarker( double x, double y, QPainter &p, Qgis::VertexMarkerType type, int m )
479{
480 switch ( type )
481 {
483 p.setPen( QColor( 50, 100, 120, 200 ) );
484 p.setBrush( QColor( 200, 200, 210, 120 ) );
485 p.drawEllipse( x - m, y - m, m * 2 + 1, m * 2 + 1 );
486 break;
487
489 p.setPen( QColor( 255, 0, 0 ) );
490 p.drawLine( x - m, y + m, x + m, y - m );
491 p.drawLine( x - m, y - m, x + m, y + m );
492 break;
493
495 break;
496 }
497}
498
500{
502
503 mSelectedFeatureIds.insert( fid );
504 mPreviousSelectedFeatureIds.clear();
505
506 emit selectionChanged( QgsFeatureIds() << fid, QgsFeatureIds(), false );
507}
508
509void QgsVectorLayer::select( const QgsFeatureIds &featureIds )
510{
512
513 mSelectedFeatureIds.unite( featureIds );
514 mPreviousSelectedFeatureIds.clear();
515
516 emit selectionChanged( featureIds, QgsFeatureIds(), false );
517}
518
520{
522
523 mSelectedFeatureIds.remove( fid );
524 mPreviousSelectedFeatureIds.clear();
525
526 emit selectionChanged( QgsFeatureIds(), QgsFeatureIds() << fid, false );
527}
528
530{
532
533 mSelectedFeatureIds.subtract( featureIds );
534 mPreviousSelectedFeatureIds.clear();
535
536 emit selectionChanged( QgsFeatureIds(), featureIds, false );
537}
538
540{
542
543 // normalize the rectangle
544 rect.normalize();
545
546 QgsFeatureIds newSelection;
547
549 .setFilterRect( rect )
551 .setNoAttributes() );
552
553 QgsFeature feat;
554 while ( features.nextFeature( feat ) )
555 {
556 newSelection << feat.id();
557 }
558 features.close();
559
560 selectByIds( newSelection, behavior );
561}
562
563void QgsVectorLayer::selectByExpression( const QString &expression, Qgis::SelectBehavior behavior, QgsExpressionContext *context )
564{
566
567 QgsFeatureIds newSelection;
568
569 std::optional< QgsExpressionContext > defaultContext;
570 if ( !context )
571 {
572 defaultContext.emplace( QgsExpressionContextUtils::globalProjectLayerScopes( this ) );
573 context = &defaultContext.value();
574 }
575
577 {
579 .setExpressionContext( *context )
582
583 QgsFeatureIterator features = getFeatures( request );
584
585 if ( behavior == Qgis::SelectBehavior::AddToSelection )
586 {
587 newSelection = selectedFeatureIds();
588 }
589 QgsFeature feat;
590 while ( features.nextFeature( feat ) )
591 {
592 newSelection << feat.id();
593 }
594 features.close();
595 }
597 {
598 QgsExpression exp( expression );
599 exp.prepare( context );
600
601 QgsFeatureIds oldSelection = selectedFeatureIds();
602 QgsFeatureRequest request = QgsFeatureRequest().setFilterFids( oldSelection );
603
604 //refine request
605 if ( !exp.needsGeometry() )
608
609 QgsFeatureIterator features = getFeatures( request );
610 QgsFeature feat;
611 while ( features.nextFeature( feat ) )
612 {
613 context->setFeature( feat );
614 bool matches = exp.evaluate( context ).toBool();
615
616 if ( matches && behavior == Qgis::SelectBehavior::IntersectSelection )
617 {
618 newSelection << feat.id();
619 }
620 else if ( !matches && behavior == Qgis::SelectBehavior::RemoveFromSelection )
621 {
622 newSelection << feat.id();
623 }
624 }
625 }
626
627 selectByIds( newSelection );
628}
629
631{
633
634 QgsFeatureIds newSelection;
635
636 switch ( behavior )
637 {
639 newSelection = ids;
640 break;
641
643 newSelection = mSelectedFeatureIds + ids;
644 break;
645
647 newSelection = mSelectedFeatureIds - ids;
648 break;
649
651 newSelection = mSelectedFeatureIds.intersect( ids );
652 break;
653 }
654
655 QgsFeatureIds deselectedFeatures = mSelectedFeatureIds - newSelection;
656 mSelectedFeatureIds = newSelection;
657 mPreviousSelectedFeatureIds.clear();
658
659 emit selectionChanged( newSelection, deselectedFeatures, true );
660}
661
662void QgsVectorLayer::modifySelection( const QgsFeatureIds &selectIds, const QgsFeatureIds &deselectIds )
663{
665
666 QgsFeatureIds intersectingIds = selectIds & deselectIds;
667 if ( !intersectingIds.isEmpty() )
668 {
669 QgsDebugMsgLevel( QStringLiteral( "Trying to select and deselect the same item at the same time. Unsure what to do. Selecting dubious items." ), 3 );
670 }
671
672 mSelectedFeatureIds -= deselectIds;
673 mSelectedFeatureIds += selectIds;
674 mPreviousSelectedFeatureIds.clear();
675
676 emit selectionChanged( selectIds, deselectIds - intersectingIds, false );
677}
678
680{
682
684 ids.subtract( mSelectedFeatureIds );
685 selectByIds( ids );
686}
687
694
696{
698
699 // normalize the rectangle
700 rect.normalize();
701
703 .setFilterRect( rect )
705 .setNoAttributes() );
706
707 QgsFeatureIds selectIds;
708 QgsFeatureIds deselectIds;
709
710 QgsFeature fet;
711 while ( fit.nextFeature( fet ) )
712 {
713 if ( mSelectedFeatureIds.contains( fet.id() ) )
714 {
715 deselectIds << fet.id();
716 }
717 else
718 {
719 selectIds << fet.id();
720 }
721 }
722
723 modifySelection( selectIds, deselectIds );
724}
725
727{
729
730 if ( mSelectedFeatureIds.isEmpty() )
731 return;
732
733 const QgsFeatureIds previous = mSelectedFeatureIds;
735 mPreviousSelectedFeatureIds = previous;
736}
737
739{
741
742 if ( mPreviousSelectedFeatureIds.isEmpty() || !mSelectedFeatureIds.empty() )
743 return;
744
745 selectByIds( mPreviousSelectedFeatureIds );
746}
747
749{
750 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
752
753 return mDataProvider;
754}
755
757{
758 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
760
761 return mDataProvider;
762}
763
765{
766 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
768
769 return mSelectionProperties;
770}
771
778
785
787{
789
790 QgsProfileRequest modifiedRequest( request );
791 modifiedRequest.expressionContext().appendScope( createExpressionContextScope() );
792 return new QgsVectorLayerProfileGenerator( this, modifiedRequest );
793}
794
795void QgsVectorLayer::setProviderEncoding( const QString &encoding )
796{
798
799 if ( isValid() && mDataProvider && mDataProvider->encoding() != encoding )
800 {
801 mDataProvider->setEncoding( encoding );
802 updateFields();
803 }
804}
805
807{
809
810 delete mDiagramRenderer;
811 mDiagramRenderer = r;
812 emit rendererChanged();
813 emit styleChanged();
814}
815
817{
818 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
820
821 return QgsWkbTypes::geometryType( mWkbType );
822}
823
825{
827
828 return mWkbType;
829}
830
832{
834
835 if ( !isValid() || !isSpatial() || mSelectedFeatureIds.isEmpty() || !mDataProvider ) //no selected features
836 {
837 return QgsRectangle( 0, 0, 0, 0 );
838 }
839
840 QgsRectangle r, retval;
841 retval.setNull();
842
843 QgsFeature fet;
845 {
847 .setFilterFids( mSelectedFeatureIds )
848 .setNoAttributes() );
849
850 while ( fit.nextFeature( fet ) )
851 {
852 if ( !fet.hasGeometry() )
853 continue;
854 r = fet.geometry().boundingBox();
855 retval.combineExtentWith( r );
856 }
857 }
858 else
859 {
861 .setNoAttributes() );
862
863 while ( fit.nextFeature( fet ) )
864 {
865 if ( mSelectedFeatureIds.contains( fet.id() ) )
866 {
867 if ( fet.hasGeometry() )
868 {
869 r = fet.geometry().boundingBox();
870 retval.combineExtentWith( r );
871 }
872 }
873 }
874 }
875
876 if ( retval.width() == 0.0 || retval.height() == 0.0 )
877 {
878 // If all of the features are at the one point, buffer the
879 // rectangle a bit. If they are all at zero, do something a bit
880 // more crude.
881
882 if ( retval.xMinimum() == 0.0 && retval.xMaximum() == 0.0 &&
883 retval.yMinimum() == 0.0 && retval.yMaximum() == 0.0 )
884 {
885 retval.set( -1.0, -1.0, 1.0, 1.0 );
886 }
887 }
888
889 return retval;
890}
891
893{
894 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
896
897 return mLabelsEnabled && static_cast< bool >( mLabeling );
898}
899
901{
903
904 mLabelsEnabled = enabled;
905}
906
908{
909 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
911
912 if ( !mDiagramRenderer || !mDiagramLayerSettings )
913 return false;
914
915 QList<QgsDiagramSettings> settingList = mDiagramRenderer->diagramSettings();
916 if ( !settingList.isEmpty() )
917 {
918 return settingList.at( 0 ).enabled;
919 }
920 return false;
921}
922
923long long QgsVectorLayer::featureCount( const QString &legendKey ) const
924{
926
927 if ( !mSymbolFeatureCounted )
928 return -1;
929
930 return mSymbolFeatureCountMap.value( legendKey, -1 );
931}
932
933QgsFeatureIds QgsVectorLayer::symbolFeatureIds( const QString &legendKey ) const
934{
936
937 if ( !mSymbolFeatureCounted )
938 return QgsFeatureIds();
939
940 return mSymbolFeatureIdMap.value( legendKey, QgsFeatureIds() );
941}
943{
945
946 if ( ( mSymbolFeatureCounted || mFeatureCounter ) && !( storeSymbolFids && mSymbolFeatureIdMap.isEmpty() ) )
947 return mFeatureCounter;
948
949 mSymbolFeatureCountMap.clear();
950 mSymbolFeatureIdMap.clear();
951
952 if ( !isValid() )
953 {
954 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer" ), 3 );
955 return mFeatureCounter;
956 }
957 if ( !mDataProvider )
958 {
959 QgsDebugMsgLevel( QStringLiteral( "invoked with null mDataProvider" ), 3 );
960 return mFeatureCounter;
961 }
962 if ( !mRenderer )
963 {
964 QgsDebugMsgLevel( QStringLiteral( "invoked with null mRenderer" ), 3 );
965 return mFeatureCounter;
966 }
967
968 if ( !mFeatureCounter || ( storeSymbolFids && mSymbolFeatureIdMap.isEmpty() ) )
969 {
970 mFeatureCounter = new QgsVectorLayerFeatureCounter( this, QgsExpressionContext(), storeSymbolFids );
971 connect( mFeatureCounter, &QgsTask::taskCompleted, this, &QgsVectorLayer::onFeatureCounterCompleted, Qt::UniqueConnection );
972 connect( mFeatureCounter, &QgsTask::taskTerminated, this, &QgsVectorLayer::onFeatureCounterTerminated, Qt::UniqueConnection );
973 QgsApplication::taskManager()->addTask( mFeatureCounter );
974 }
975
976 return mFeatureCounter;
977}
978
980{
982
983 // do not update extent by default when trust project option is activated
984 if ( force || !mReadExtentFromXml || ( mReadExtentFromXml && mXmlExtent2D.isNull() && mXmlExtent3D.isNull() ) )
985 {
986 mValidExtent2D = false;
987 mValidExtent3D = false;
988 }
989}
990
992{
994
996 mValidExtent2D = true;
997}
998
1000{
1002
1004 mValidExtent3D = true;
1005}
1006
1007void QgsVectorLayer::updateDefaultValues( QgsFeatureId fid, QgsFeature feature, QgsExpressionContext *context )
1008{
1010
1011 if ( !mDefaultValueOnUpdateFields.isEmpty() )
1012 {
1013 if ( !feature.isValid() )
1014 feature = getFeature( fid );
1015
1016 int size = mFields.size();
1017 for ( int idx : std::as_const( mDefaultValueOnUpdateFields ) )
1018 {
1019 if ( idx < 0 || idx >= size )
1020 continue;
1021 feature.setAttribute( idx, defaultValue( idx, feature, context ) );
1022 updateFeature( feature, true );
1023 }
1024 }
1025}
1026
1028{
1030
1031 QgsRectangle rect;
1032 rect.setNull();
1033
1034 if ( !isSpatial() )
1035 return rect;
1036
1037 // Don't do lazy extent if the layer is currently in edit mode
1038 if ( mLazyExtent2D && isEditable() )
1039 mLazyExtent2D = false;
1040
1041 if ( mDataProvider && mDataProvider->isValid() && ( mDataProvider->flags() & Qgis::DataProviderFlag::FastExtent2D ) )
1042 {
1043 // Provider has a trivial 2D extent calculation => always get extent from provider.
1044 // Things are nice and simple this way, e.g. we can always trust that this extent is
1045 // accurate and up to date.
1046 updateExtent( mDataProvider->extent() );
1047 mValidExtent2D = true;
1048 mLazyExtent2D = false;
1049 }
1050 else
1051 {
1052 if ( !mValidExtent2D && mLazyExtent2D && mReadExtentFromXml && !mXmlExtent2D.isNull() )
1053 {
1054 updateExtent( mXmlExtent2D );
1055 mValidExtent2D = true;
1056 mLazyExtent2D = false;
1057 }
1058
1059 if ( !mValidExtent2D && mLazyExtent2D && mDataProvider && mDataProvider->isValid() )
1060 {
1061 // store the extent
1062 updateExtent( mDataProvider->extent() );
1063 mValidExtent2D = true;
1064 mLazyExtent2D = false;
1065
1066 // show the extent
1067 QgsDebugMsgLevel( QStringLiteral( "2D Extent of layer: %1" ).arg( mExtent2D.toString() ), 3 );
1068 }
1069 }
1070
1071 if ( mValidExtent2D )
1072 return QgsMapLayer::extent();
1073
1074 if ( !isValid() || !mDataProvider )
1075 {
1076 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1077 return rect;
1078 }
1079
1080 if ( !mEditBuffer ||
1081 ( !mDataProvider->transaction() && ( mEditBuffer->deletedFeatureIds().isEmpty() && mEditBuffer->changedGeometries().isEmpty() ) ) ||
1083 {
1084 mDataProvider->updateExtents();
1085
1086 // get the extent of the layer from the provider
1087 // but only when there are some features already
1088 if ( mDataProvider->featureCount() != 0 )
1089 {
1090 const QgsRectangle r = mDataProvider->extent();
1091 rect.combineExtentWith( r );
1092 }
1093
1094 if ( mEditBuffer && !mDataProvider->transaction() )
1095 {
1096 const auto addedFeatures = mEditBuffer->addedFeatures();
1097 for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it )
1098 {
1099 if ( it->hasGeometry() )
1100 {
1101 const QgsRectangle r = it->geometry().boundingBox();
1102 rect.combineExtentWith( r );
1103 }
1104 }
1105 }
1106 }
1107 else
1108 {
1110 .setNoAttributes() );
1111
1112 QgsFeature fet;
1113 while ( fit.nextFeature( fet ) )
1114 {
1115 if ( fet.hasGeometry() && fet.geometry().type() != Qgis::GeometryType::Unknown )
1116 {
1117 const QgsRectangle bb = fet.geometry().boundingBox();
1118 rect.combineExtentWith( bb );
1119 }
1120 }
1121 }
1122
1123 if ( rect.xMinimum() > rect.xMaximum() && rect.yMinimum() > rect.yMaximum() )
1124 {
1125 // special case when there are no features in provider nor any added
1126 rect = QgsRectangle(); // use rectangle with zero coordinates
1127 }
1128
1129 updateExtent( rect );
1130 mValidExtent2D = true;
1131
1132 // Send this (hopefully) up the chain to the map canvas
1133 emit recalculateExtents();
1134
1135 return rect;
1136}
1137
1139{
1141
1142 // if data is 2D, redirect to 2D extend computation, and save it as 2D extent (in 3D bbox)
1143 if ( mDataProvider && mDataProvider->elevationProperties() && !mDataProvider->elevationProperties()->containsElevationData() )
1144 {
1145 return QgsBox3D( extent() );
1146 }
1147
1149 extent.setNull();
1150
1151 if ( !isSpatial() )
1152 return extent;
1153
1154 if ( mDataProvider && mDataProvider->isValid() && ( mDataProvider->flags() & Qgis::DataProviderFlag::FastExtent3D ) )
1155 {
1156 // Provider has a trivial 3D extent calculation => always get extent from provider.
1157 // Things are nice and simple this way, e.g. we can always trust that this extent is
1158 // accurate and up to date.
1159 updateExtent( mDataProvider->extent3D() );
1160 mValidExtent3D = true;
1161 mLazyExtent3D = false;
1162 }
1163 else
1164 {
1165 if ( !mValidExtent3D && mLazyExtent3D && mReadExtentFromXml && !mXmlExtent3D.isNull() )
1166 {
1167 updateExtent( mXmlExtent3D );
1168 mValidExtent3D = true;
1169 mLazyExtent3D = false;
1170 }
1171
1172 if ( !mValidExtent3D && mLazyExtent3D && mDataProvider && mDataProvider->isValid() )
1173 {
1174 // store the extent
1175 updateExtent( mDataProvider->extent3D() );
1176 mValidExtent3D = true;
1177 mLazyExtent3D = false;
1178
1179 // show the extent
1180 QgsDebugMsgLevel( QStringLiteral( "3D Extent of layer: %1" ).arg( mExtent3D.toString() ), 3 );
1181 }
1182 }
1183
1184 if ( mValidExtent3D )
1185 return QgsMapLayer::extent3D();
1186
1187 if ( !isValid() || !mDataProvider )
1188 {
1189 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1190 return extent;
1191 }
1192
1193 if ( !mEditBuffer ||
1194 ( !mDataProvider->transaction() && ( mEditBuffer->deletedFeatureIds().isEmpty() && mEditBuffer->changedGeometries().isEmpty() ) ) ||
1196 {
1197 mDataProvider->updateExtents();
1198
1199 // get the extent of the layer from the provider
1200 // but only when there are some features already
1201 if ( mDataProvider->featureCount() != 0 )
1202 {
1203 const QgsBox3D ext = mDataProvider->extent3D();
1204 extent.combineWith( ext );
1205 }
1206
1207 if ( mEditBuffer && !mDataProvider->transaction() )
1208 {
1209 const auto addedFeatures = mEditBuffer->addedFeatures();
1210 for ( QgsFeatureMap::const_iterator it = addedFeatures.constBegin(); it != addedFeatures.constEnd(); ++it )
1211 {
1212 if ( it->hasGeometry() )
1213 {
1214 const QgsBox3D bbox = it->geometry().boundingBox3D();
1215 extent.combineWith( bbox );
1216 }
1217 }
1218 }
1219 }
1220 else
1221 {
1223 .setNoAttributes() );
1224
1225 QgsFeature fet;
1226 while ( fit.nextFeature( fet ) )
1227 {
1228 if ( fet.hasGeometry() && fet.geometry().type() != Qgis::GeometryType::Unknown )
1229 {
1230 const QgsBox3D bb = fet.geometry().boundingBox3D();
1231 extent.combineWith( bb );
1232 }
1233 }
1234 }
1235
1236 if ( extent.xMinimum() > extent.xMaximum() && extent.yMinimum() > extent.yMaximum() && extent.zMinimum() > extent.zMaximum() )
1237 {
1238 // special case when there are no features in provider nor any added
1239 extent = QgsBox3D(); // use rectangle with zero coordinates
1240 }
1241
1242 updateExtent( extent );
1243 mValidExtent3D = true;
1244
1245 // Send this (hopefully) up the chain to the map canvas
1246 emit recalculateExtents();
1247
1248 return extent;
1249}
1250
1257
1264
1266{
1268
1269 if ( !isValid() || !mDataProvider )
1270 {
1271 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider" ), 3 );
1272 return customProperty( QStringLiteral( "storedSubsetString" ) ).toString();
1273 }
1274 return mDataProvider->subsetString();
1275}
1276
1277bool QgsVectorLayer::setSubsetString( const QString &subset )
1278{
1280
1281 if ( !isValid() || !mDataProvider )
1282 {
1283 QgsDebugMsgLevel( QStringLiteral( "invoked with invalid layer or null mDataProvider or while editing" ), 3 );
1284 setCustomProperty( QStringLiteral( "storedSubsetString" ), subset );
1285 return false;
1286 }
1287 else if ( mEditBuffer )
1288 {
1289 QgsDebugMsgLevel( QStringLiteral( "invoked while editing" ), 3 );
1290 return false;
1291 }
1292
1293 if ( subset == mDataProvider->subsetString() )
1294 return true;
1295
1296 bool res = mDataProvider->setSubsetString( subset );
1297
1298 // get the updated data source string from the provider
1299 mDataSource = mDataProvider->dataSourceUri();
1300 updateExtents();
1301 updateFields();
1302
1303 if ( res )
1304 {
1305 emit subsetStringChanged();
1307 }
1308
1309 return res;
1310}
1311
1313{
1314 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
1316
1317 if ( isValid() && mDataProvider && !mEditBuffer && ( isSpatial() && geometryType() != Qgis::GeometryType::Point ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && renderContext.useRenderingOptimization() )
1318 {
1319 double maximumSimplificationScale = mSimplifyMethod.maximumScale();
1320
1321 // check maximum scale at which generalisation should be carried out
1322 return !( maximumSimplificationScale > 1 && renderContext.rendererScale() <= maximumSimplificationScale );
1323 }
1324 return false;
1325}
1326
1328{
1330
1331 return mConditionalStyles;
1332}
1333
1335{
1336 // non fatal for now -- the aggregate expression functions are not thread safe and call this
1338
1339 if ( !isValid() || !mDataProvider )
1340 return QgsFeatureIterator();
1341
1342 return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
1343}
1344
1346{
1348
1349 QgsFeature feature;
1351 if ( feature.isValid() )
1352 return feature.geometry();
1353 else
1354 return QgsGeometry();
1355}
1356
1358{
1360
1361 if ( !isValid() || !mEditBuffer || !mDataProvider )
1362 return false;
1363
1364
1365 if ( mGeometryOptions->isActive() )
1366 {
1367 QgsGeometry geom = feature.geometry();
1368 mGeometryOptions->apply( geom );
1369 feature.setGeometry( geom );
1370 }
1371
1372 bool success = mEditBuffer->addFeature( feature );
1373
1374 if ( success && mJoinBuffer->containsJoins() )
1375 {
1376 success = mJoinBuffer->addFeature( feature );
1377 }
1378
1379 return success;
1380}
1381
1382bool QgsVectorLayer::updateFeature( QgsFeature &updatedFeature, bool skipDefaultValues )
1383{
1385
1386 if ( !mEditBuffer || !mDataProvider )
1387 {
1388 return false;
1389 }
1390
1391 QgsFeature currentFeature = getFeature( updatedFeature.id() );
1392 if ( currentFeature.isValid() )
1393 {
1394 bool hasChanged = false;
1395 bool hasError = false;
1396
1397 if ( ( updatedFeature.hasGeometry() || currentFeature.hasGeometry() ) && !updatedFeature.geometry().equals( currentFeature.geometry() ) )
1398 {
1399 QgsGeometry geometry = updatedFeature.geometry();
1400 if ( changeGeometry( updatedFeature.id(), geometry, true ) )
1401 {
1402 hasChanged = true;
1403 updatedFeature.setGeometry( geometry );
1404 }
1405 else
1406 {
1407 QgsDebugMsgLevel( QStringLiteral( "geometry of feature %1 could not be changed." ).arg( updatedFeature.id() ), 3 );
1408 }
1409 }
1410
1411 QgsAttributes fa = updatedFeature.attributes();
1412 QgsAttributes ca = currentFeature.attributes();
1413
1414 for ( int attr = 0; attr < fa.count(); ++attr )
1415 {
1416 if ( !qgsVariantEqual( fa.at( attr ), ca.at( attr ) ) )
1417 {
1418 if ( changeAttributeValue( updatedFeature.id(), attr, fa.at( attr ), ca.at( attr ), true ) )
1419 {
1420 hasChanged = true;
1421 }
1422 else
1423 {
1424 QgsDebugMsgLevel( QStringLiteral( "attribute %1 of feature %2 could not be changed." ).arg( attr ).arg( updatedFeature.id() ), 3 );
1425 hasError = true;
1426 }
1427 }
1428 }
1429 if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )
1430 updateDefaultValues( updatedFeature.id(), updatedFeature );
1431
1432 return !hasError;
1433 }
1434 else
1435 {
1436 QgsDebugMsgLevel( QStringLiteral( "feature %1 could not be retrieved" ).arg( updatedFeature.id() ), 3 );
1437 return false;
1438 }
1439}
1440
1441
1442bool QgsVectorLayer::insertVertex( double x, double y, QgsFeatureId atFeatureId, int beforeVertex )
1443{
1445
1446 if ( !isValid() || !mEditBuffer || !mDataProvider )
1447 return false;
1448
1449 QgsVectorLayerEditUtils utils( this );
1450 bool result = utils.insertVertex( x, y, atFeatureId, beforeVertex );
1451 if ( result )
1452 updateExtents();
1453 return result;
1454}
1455
1456
1457bool QgsVectorLayer::insertVertex( const QgsPoint &point, QgsFeatureId atFeatureId, int beforeVertex )
1458{
1460
1461 if ( !isValid() || !mEditBuffer || !mDataProvider )
1462 return false;
1463
1464 QgsVectorLayerEditUtils utils( this );
1465 bool result = utils.insertVertex( point, atFeatureId, beforeVertex );
1466 if ( result )
1467 updateExtents();
1468 return result;
1469}
1470
1471
1472bool QgsVectorLayer::moveVertex( double x, double y, QgsFeatureId atFeatureId, int atVertex )
1473{
1475
1476 if ( !isValid() || !mEditBuffer || !mDataProvider )
1477 return false;
1478
1479 QgsVectorLayerEditUtils utils( this );
1480 bool result = utils.moveVertex( x, y, atFeatureId, atVertex );
1481
1482 if ( result )
1483 updateExtents();
1484 return result;
1485}
1486
1487bool QgsVectorLayer::moveVertex( const QgsPoint &p, QgsFeatureId atFeatureId, int atVertex )
1488{
1490
1491 if ( !isValid() || !mEditBuffer || !mDataProvider )
1492 return false;
1493
1494 QgsVectorLayerEditUtils utils( this );
1495 bool result = utils.moveVertex( p, atFeatureId, atVertex );
1496
1497 if ( result )
1498 updateExtents();
1499 return result;
1500}
1501
1503{
1505
1506 if ( !isValid() || !mEditBuffer || !mDataProvider )
1508
1509 QgsVectorLayerEditUtils utils( this );
1510 Qgis::VectorEditResult result = utils.deleteVertex( featureId, vertex );
1511
1512 if ( result == Qgis::VectorEditResult::Success )
1513 updateExtents();
1514 return result;
1515}
1516
1517
1519{
1521
1522 if ( !isValid() || !mDataProvider || !( mDataProvider->capabilities() & Qgis::VectorProviderCapability::DeleteFeatures ) )
1523 {
1524 return false;
1525 }
1526
1527 if ( !isEditable() )
1528 {
1529 return false;
1530 }
1531
1532 int deleted = 0;
1533 int count = mSelectedFeatureIds.size();
1534 // Make a copy since deleteFeature modifies mSelectedFeatureIds
1535 QgsFeatureIds selectedFeatures( mSelectedFeatureIds );
1536 for ( QgsFeatureId fid : std::as_const( selectedFeatures ) )
1537 {
1538 deleted += deleteFeature( fid, context ); // removes from selection
1539 }
1540
1542 updateExtents();
1543
1544 if ( deletedCount )
1545 {
1546 *deletedCount = deleted;
1547 }
1548
1549 return deleted == count;
1550}
1551
1552static const QgsPointSequence vectorPointXY2pointSequence( const QVector<QgsPointXY> &points )
1553{
1554 QgsPointSequence pts;
1555 pts.reserve( points.size() );
1556 QVector<QgsPointXY>::const_iterator it = points.constBegin();
1557 while ( it != points.constEnd() )
1558 {
1559 pts.append( QgsPoint( *it ) );
1560 ++it;
1561 }
1562 return pts;
1563}
1564Qgis::GeometryOperationResult QgsVectorLayer::addRing( const QVector<QgsPointXY> &ring, QgsFeatureId *featureId )
1565{
1567
1568 return addRing( vectorPointXY2pointSequence( ring ), featureId );
1569}
1570
1572{
1574
1575 if ( !isValid() || !mEditBuffer || !mDataProvider )
1577
1578 QgsVectorLayerEditUtils utils( this );
1580
1581 //first try with selected features
1582 if ( !mSelectedFeatureIds.isEmpty() )
1583 {
1584 result = utils.addRing( ring, mSelectedFeatureIds, featureId );
1585 }
1586
1588 {
1589 //try with all intersecting features
1590 result = utils.addRing( ring, QgsFeatureIds(), featureId );
1591 }
1592
1593 return result;
1594}
1595
1597{
1599
1600 if ( !isValid() || !mEditBuffer || !mDataProvider )
1601 {
1602 delete ring;
1604 }
1605
1606 if ( !ring )
1607 {
1609 }
1610
1611 if ( !ring->isClosed() )
1612 {
1613 delete ring;
1615 }
1616
1617 QgsVectorLayerEditUtils utils( this );
1619
1620 //first try with selected features
1621 if ( !mSelectedFeatureIds.isEmpty() )
1622 {
1623 result = utils.addRing( static_cast< QgsCurve * >( ring->clone() ), mSelectedFeatureIds, featureId );
1624 }
1625
1627 {
1628 //try with all intersecting features
1629 result = utils.addRing( static_cast< QgsCurve * >( ring->clone() ), QgsFeatureIds(), featureId );
1630 }
1631
1632 delete ring;
1633 return result;
1634}
1635
1637{
1639
1640 QgsPointSequence pts;
1641 pts.reserve( points.size() );
1642 for ( QList<QgsPointXY>::const_iterator it = points.constBegin(); it != points.constEnd() ; ++it )
1643 {
1644 pts.append( QgsPoint( *it ) );
1645 }
1646 return addPart( pts );
1647}
1648
1649#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1650Qgis::GeometryOperationResult QgsVectorLayer::addPart( const QVector<QgsPointXY> &points )
1651{
1653
1654 return addPart( vectorPointXY2pointSequence( points ) );
1655}
1656#endif
1657
1659{
1661
1662 if ( !isValid() || !mEditBuffer || !mDataProvider )
1664
1665 //number of selected features must be 1
1666
1667 if ( mSelectedFeatureIds.empty() )
1668 {
1669 QgsDebugMsgLevel( QStringLiteral( "Number of selected features <1" ), 3 );
1671 }
1672 else if ( mSelectedFeatureIds.size() > 1 )
1673 {
1674 QgsDebugMsgLevel( QStringLiteral( "Number of selected features >1" ), 3 );
1676 }
1677
1678 QgsVectorLayerEditUtils utils( this );
1679 Qgis::GeometryOperationResult result = utils.addPart( points, *mSelectedFeatureIds.constBegin() );
1680
1682 updateExtents();
1683 return result;
1684}
1685
1687{
1689
1690 if ( !isValid() || !mEditBuffer || !mDataProvider )
1692
1693 //number of selected features must be 1
1694
1695 if ( mSelectedFeatureIds.empty() )
1696 {
1697 QgsDebugMsgLevel( QStringLiteral( "Number of selected features <1" ), 3 );
1699 }
1700 else if ( mSelectedFeatureIds.size() > 1 )
1701 {
1702 QgsDebugMsgLevel( QStringLiteral( "Number of selected features >1" ), 3 );
1704 }
1705
1706 QgsVectorLayerEditUtils utils( this );
1707 Qgis::GeometryOperationResult result = utils.addPart( ring, *mSelectedFeatureIds.constBegin() );
1708
1710 updateExtents();
1711 return result;
1712}
1713
1714// TODO QGIS 4.0 -- this should return Qgis::GeometryOperationResult, not int
1715int QgsVectorLayer::translateFeature( QgsFeatureId featureId, double dx, double dy )
1716{
1718
1719 if ( !isValid() || !mEditBuffer || !mDataProvider )
1720 return static_cast< int >( Qgis::GeometryOperationResult::LayerNotEditable );
1721
1722 QgsVectorLayerEditUtils utils( this );
1723 int result = utils.translateFeature( featureId, dx, dy );
1724
1725 if ( result == static_cast< int >( Qgis::GeometryOperationResult::Success ) )
1726 updateExtents();
1727 return result;
1728}
1729
1730Qgis::GeometryOperationResult QgsVectorLayer::splitParts( const QVector<QgsPointXY> &splitLine, bool topologicalEditing )
1731{
1733
1734 return splitParts( vectorPointXY2pointSequence( splitLine ), topologicalEditing );
1735}
1736
1738{
1740
1741 if ( !isValid() || !mEditBuffer || !mDataProvider )
1743
1744 QgsVectorLayerEditUtils utils( this );
1745 return utils.splitParts( splitLine, topologicalEditing );
1746}
1747
1748Qgis::GeometryOperationResult QgsVectorLayer::splitFeatures( const QVector<QgsPointXY> &splitLine, bool topologicalEditing )
1749{
1751
1752 return splitFeatures( vectorPointXY2pointSequence( splitLine ), topologicalEditing );
1753}
1754
1756{
1758
1759 QgsLineString splitLineString( splitLine );
1760 QgsPointSequence topologyTestPoints;
1761 bool preserveCircular = false;
1762 return splitFeatures( &splitLineString, topologyTestPoints, preserveCircular, topologicalEditing );
1763}
1764
1765Qgis::GeometryOperationResult QgsVectorLayer::splitFeatures( const QgsCurve *curve, QgsPointSequence &topologyTestPoints, bool preserveCircular, bool topologicalEditing )
1766{
1768
1769 if ( !isValid() || !mEditBuffer || !mDataProvider )
1771
1772 QgsVectorLayerEditUtils utils( this );
1773 return utils.splitFeatures( curve, topologyTestPoints, preserveCircular, topologicalEditing );
1774}
1775
1777{
1779
1780 if ( !isValid() || !mEditBuffer || !mDataProvider )
1781 return -1;
1782
1783 QgsVectorLayerEditUtils utils( this );
1784 return utils.addTopologicalPoints( geom );
1785}
1786
1793
1795{
1797
1798 if ( !isValid() || !mEditBuffer || !mDataProvider )
1799 return -1;
1800
1801 QgsVectorLayerEditUtils utils( this );
1802 return utils.addTopologicalPoints( p );
1803}
1804
1806{
1808
1809 if ( !mValid || !mEditBuffer || !mDataProvider )
1810 return -1;
1811
1812 QgsVectorLayerEditUtils utils( this );
1813 return utils.addTopologicalPoints( ps );
1814}
1815
1817{
1819
1820 if ( mLabeling == labeling )
1821 return;
1822
1823 delete mLabeling;
1824 mLabeling = labeling;
1825}
1826
1828{
1830
1831 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
1832 return project()->startEditing( this );
1833
1834 if ( !isValid() || !mDataProvider )
1835 {
1836 return false;
1837 }
1838
1839 // allow editing if provider supports any of the capabilities
1840 if ( !supportsEditing() )
1841 {
1842 return false;
1843 }
1844
1845 if ( mEditBuffer )
1846 {
1847 // editing already underway
1848 return false;
1849 }
1850
1851 mDataProvider->enterUpdateMode();
1852
1853 emit beforeEditingStarted();
1854
1855 createEditBuffer();
1856
1857 updateFields();
1858
1859 emit editingStarted();
1860
1861 return true;
1862}
1863
1865{
1867
1868 if ( mDataProvider )
1869 mDataProvider->setTransformContext( transformContext );
1870}
1871
1878
1880{
1882
1883 if ( mRenderer )
1884 if ( !mRenderer->accept( visitor ) )
1885 return false;
1886
1887 if ( mLabeling )
1888 if ( !mLabeling->accept( visitor ) )
1889 return false;
1890
1891 return true;
1892}
1893
1894bool QgsVectorLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
1895{
1897
1898 QgsDebugMsgLevel( QStringLiteral( "Datasource in QgsVectorLayer::readXml: %1" ).arg( mDataSource.toLocal8Bit().data() ), 3 );
1899
1900 //process provider key
1901 QDomNode pkeyNode = layer_node.namedItem( QStringLiteral( "provider" ) );
1902
1903 if ( pkeyNode.isNull() )
1904 {
1905 mProviderKey.clear();
1906 }
1907 else
1908 {
1909 QDomElement pkeyElt = pkeyNode.toElement();
1910 mProviderKey = pkeyElt.text();
1911 }
1912
1913 // determine type of vector layer
1914 if ( !mProviderKey.isNull() )
1915 {
1916 // if the provider string isn't empty, then we successfully
1917 // got the stored provider
1918 }
1919 else if ( mDataSource.contains( QLatin1String( "dbname=" ) ) )
1920 {
1921 mProviderKey = QStringLiteral( "postgres" );
1922 }
1923 else
1924 {
1925 mProviderKey = QStringLiteral( "ogr" );
1926 }
1927
1928 const QDomElement elem = layer_node.toElement();
1930
1931 mDataSourceReadOnly = mReadFlags & QgsMapLayer::FlagForceReadOnly;
1933
1934 if ( ( mReadFlags & QgsMapLayer::FlagDontResolveLayers ) || !setDataProvider( mProviderKey, options, flags ) )
1935 {
1937 {
1938 QgsDebugError( QStringLiteral( "Could not set data provider for layer %1" ).arg( publicSource() ) );
1939 }
1940
1941 // for invalid layer sources, we fallback to stored wkbType if available
1942 if ( elem.hasAttribute( QStringLiteral( "wkbType" ) ) )
1943 mWkbType = qgsEnumKeyToValue( elem.attribute( QStringLiteral( "wkbType" ) ), mWkbType );
1944 }
1945
1946 QDomElement pkeyElem = pkeyNode.toElement();
1947 if ( !pkeyElem.isNull() )
1948 {
1949 QString encodingString = pkeyElem.attribute( QStringLiteral( "encoding" ) );
1950 if ( mDataProvider && !encodingString.isEmpty() )
1951 {
1952 mDataProvider->setEncoding( encodingString );
1953 }
1954 }
1955
1956 // load vector joins - does not resolve references to layers yet
1957 mJoinBuffer->readXml( layer_node );
1958
1959 updateFields();
1960
1961 // If style doesn't include a legend, we'll need to make a default one later...
1962 mSetLegendFromStyle = false;
1963
1964 QString errorMsg;
1965 if ( !readSymbology( layer_node, errorMsg, context ) )
1966 {
1967 return false;
1968 }
1969
1970 readStyleManager( layer_node );
1971
1972 QDomNode depsNode = layer_node.namedItem( QStringLiteral( "dataDependencies" ) );
1973 QDomNodeList depsNodes = depsNode.childNodes();
1974 QSet<QgsMapLayerDependency> sources;
1975 for ( int i = 0; i < depsNodes.count(); i++ )
1976 {
1977 QString source = depsNodes.at( i ).toElement().attribute( QStringLiteral( "id" ) );
1978 sources << QgsMapLayerDependency( source );
1979 }
1980 setDependencies( sources );
1981
1982 if ( !mSetLegendFromStyle )
1984
1985 // read extent
1987 {
1988 mReadExtentFromXml = true;
1989 }
1990 if ( mReadExtentFromXml )
1991 {
1992 const QDomNode extentNode = layer_node.namedItem( QStringLiteral( "extent" ) );
1993 if ( !extentNode.isNull() )
1994 {
1995 mXmlExtent2D = QgsXmlUtils::readRectangle( extentNode.toElement() );
1996 }
1997 const QDomNode extent3DNode = layer_node.namedItem( QStringLiteral( "extent3D" ) );
1998 if ( !extent3DNode.isNull() )
1999 {
2000 mXmlExtent3D = QgsXmlUtils::readBox3D( extent3DNode.toElement() );
2001 }
2002 }
2003
2004 // auxiliary layer
2005 const QDomNode asNode = layer_node.namedItem( QStringLiteral( "auxiliaryLayer" ) );
2006 const QDomElement asElem = asNode.toElement();
2007 if ( !asElem.isNull() )
2008 {
2009 mAuxiliaryLayerKey = asElem.attribute( QStringLiteral( "key" ) );
2010 }
2011
2012 // QGIS Server WMS Dimensions
2013 mServerProperties->readXml( layer_node );
2014
2015 return isValid(); // should be true if read successfully
2016
2017} // void QgsVectorLayer::readXml
2018
2019
2020void QgsVectorLayer::setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider,
2022{
2024
2025 Qgis::GeometryType geomType = geometryType();
2026
2027 mDataSource = dataSource;
2028 setName( baseName );
2029 setDataProvider( provider, options, flags );
2030
2031 if ( !isValid() )
2032 {
2033 return;
2034 }
2035
2036 // Always set crs
2038
2039 bool loadDefaultStyleFlag = false;
2041 {
2042 loadDefaultStyleFlag = true;
2043 }
2044
2045 // reset style if loading default style, style is missing, or geometry type is has changed (and layer is valid)
2046 if ( !renderer() || !legend() || ( isValid() && geomType != geometryType() ) || loadDefaultStyleFlag )
2047 {
2048 std::unique_ptr< QgsScopedRuntimeProfile > profile;
2049 if ( QgsApplication::profiler()->groupIsActive( QStringLiteral( "projectload" ) ) )
2050 profile = std::make_unique< QgsScopedRuntimeProfile >( tr( "Load layer style" ), QStringLiteral( "projectload" ) );
2051
2052 bool defaultLoadedFlag = false;
2053
2054 // defer style changed signal until we've set the renderer, labeling, everything.
2055 // we don't want multiple signals!
2056 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
2057
2058 // need to check whether the default style included a legend, and if not, we need to make a default legend
2059 // later...
2060 mSetLegendFromStyle = false;
2061
2062 // first check if there is a default style / propertysheet defined
2063 // for this layer and if so apply it
2064 // this should take precedence over all
2065 if ( !defaultLoadedFlag && loadDefaultStyleFlag )
2066 {
2067 loadDefaultStyle( defaultLoadedFlag );
2068 }
2069
2070 if ( loadDefaultStyleFlag && !defaultLoadedFlag && isSpatial() && mDataProvider->capabilities() & Qgis::VectorProviderCapability::CreateRenderer )
2071 {
2072 // if we didn't load a default style for this layer, try to create a renderer directly from the data provider
2073 std::unique_ptr< QgsFeatureRenderer > defaultRenderer( mDataProvider->createRenderer() );
2074 if ( defaultRenderer )
2075 {
2076 defaultLoadedFlag = true;
2077 setRenderer( defaultRenderer.release() );
2078 }
2079 }
2080
2081 // if the default style failed to load or was disabled use some very basic defaults
2082 if ( !defaultLoadedFlag )
2083 {
2084 // add single symbol renderer for spatial layers
2086 }
2087
2088 if ( !mSetLegendFromStyle )
2090
2092 {
2093 std::unique_ptr< QgsAbstractVectorLayerLabeling > defaultLabeling( mDataProvider->createLabeling() );
2094 if ( defaultLabeling )
2095 {
2096 setLabeling( defaultLabeling.release() );
2097 setLabelsEnabled( true );
2098 }
2099 }
2100
2101 styleChangedSignalBlocker.release();
2103 }
2104}
2105
2106QString QgsVectorLayer::loadDefaultStyle( bool &resultFlag )
2107{
2109
2110 // first try to load a user-defined default style - this should always take precedence
2111 QString styleXml = QgsMapLayer::loadDefaultStyle( resultFlag );
2112
2113 if ( resultFlag )
2114 {
2115 // Try to load all stored styles from DB
2116 if ( mLoadAllStoredStyle && mDataProvider && mDataProvider->styleStorageCapabilities().testFlag( Qgis::ProviderStyleStorageCapability::LoadFromDatabase ) )
2117 {
2118 QStringList ids, names, descriptions;
2119 QString errorMessage;
2120 // Get the number of styles related to current layer.
2121 const int relatedStylesCount { listStylesInDatabase( ids, names, descriptions, errorMessage ) };
2122 Q_ASSERT( ids.count() == names.count() );
2123 const QString currentStyleName { mStyleManager->currentStyle() };
2124 for ( int i = 0; i < relatedStylesCount; ++i )
2125 {
2126 if ( names.at( i ) == currentStyleName )
2127 {
2128 continue;
2129 }
2130 errorMessage.clear();
2131 const QString styleXml { getStyleFromDatabase( ids.at( i ), errorMessage ) };
2132 if ( ! styleXml.isEmpty() && errorMessage.isEmpty() )
2133 {
2134 mStyleManager->addStyle( names.at( i ), QgsMapLayerStyle( styleXml ) );
2135 }
2136 else
2137 {
2138 QgsDebugMsgLevel( QStringLiteral( "Error retrieving style %1 from DB: %2" ).arg( ids.at( i ), errorMessage ), 2 );
2139 }
2140 }
2141 }
2142 return styleXml ;
2143 }
2144
2146 {
2147 // otherwise try to create a renderer directly from the data provider
2148 std::unique_ptr< QgsFeatureRenderer > defaultRenderer( mDataProvider->createRenderer() );
2149 if ( defaultRenderer )
2150 {
2151 resultFlag = true;
2152 setRenderer( defaultRenderer.release() );
2153 return QString();
2154 }
2155 }
2156
2157 return QString();
2158}
2159
2160bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProvider::ProviderOptions &options, Qgis::DataProviderReadFlags flags )
2161{
2163
2164 mProviderKey = provider;
2165 delete mDataProvider;
2166
2167 // For Postgres provider primary key unicity is tested at construction time,
2168 // so it has to be set before initializing the provider,
2169 // this manipulation is necessary to preserve default behavior when
2170 // "trust layer metadata" project level option is set and checkPrimaryKeyUnicity
2171 // was not explicitly passed in the uri
2172 if ( provider.compare( QLatin1String( "postgres" ) ) == 0 )
2173 {
2174 const QString checkUnicityKey { QStringLiteral( "checkPrimaryKeyUnicity" ) };
2176 if ( ! uri.hasParam( checkUnicityKey ) )
2177 {
2178 uri.setParam( checkUnicityKey, mReadExtentFromXml ? "0" : "1" );
2179 mDataSource = uri.uri( false );
2180 }
2181 }
2182
2183 std::unique_ptr< QgsScopedRuntimeProfile > profile;
2184 if ( QgsApplication::profiler()->groupIsActive( QStringLiteral( "projectload" ) ) )
2185 profile = std::make_unique< QgsScopedRuntimeProfile >( tr( "Create %1 provider" ).arg( provider ), QStringLiteral( "projectload" ) );
2186
2187 if ( mPreloadedProvider )
2188 mDataProvider = qobject_cast< QgsVectorDataProvider * >( mPreloadedProvider.release() );
2189 else
2190 mDataProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, mDataSource, options, flags ) );
2191
2192 if ( !mDataProvider )
2193 {
2194 setValid( false );
2195 QgsDebugMsgLevel( QStringLiteral( "Unable to get data provider" ), 2 );
2196 return false;
2197 }
2198
2199 mDataProvider->setParent( this );
2200 connect( mDataProvider, &QgsVectorDataProvider::raiseError, this, &QgsVectorLayer::raiseError );
2201
2202 QgsDebugMsgLevel( QStringLiteral( "Instantiated the data provider plugin" ), 2 );
2203
2204 setValid( mDataProvider->isValid() );
2205 if ( !isValid() )
2206 {
2207 QgsDebugMsgLevel( QStringLiteral( "Invalid provider plugin %1" ).arg( QString( mDataSource.toUtf8() ) ), 2 );
2208 return false;
2209 }
2210
2211 if ( profile )
2212 profile->switchTask( tr( "Read layer metadata" ) );
2214 {
2215 // we combine the provider metadata with the layer's existing metadata, so as not to reset any user customizations to the metadata
2216 // back to the default if a layer's data source is changed
2217 QgsLayerMetadata newMetadata = mDataProvider->layerMetadata();
2218 // this overwrites the provider metadata with any properties which are non-empty from the existing layer metadata
2219 newMetadata.combine( &mMetadata );
2220
2221 setMetadata( newMetadata );
2222 QgsDebugMsgLevel( QStringLiteral( "Set Data provider QgsLayerMetadata identifier[%1]" ).arg( metadata().identifier() ), 4 );
2223 }
2224
2225 // TODO: Check if the provider has the capability to send fullExtentCalculated
2226 connect( mDataProvider, &QgsVectorDataProvider::fullExtentCalculated, this, [this] { updateExtents(); } );
2227
2228 // get and store the feature type
2229 mWkbType = mDataProvider->wkbType();
2230
2231 // before we update the layer fields from the provider, we first copy any default set alias and
2232 // editor widget config from the data provider fields, if present
2233 const QgsFields providerFields = mDataProvider->fields();
2234 for ( const QgsField &field : providerFields )
2235 {
2236 // we only copy defaults from the provider if we aren't overriding any configuration made in the layer
2237 if ( !field.editorWidgetSetup().isNull() && mFieldWidgetSetups.value( field.name() ).isNull() )
2238 {
2239 mFieldWidgetSetups[ field.name() ] = field.editorWidgetSetup();
2240 }
2241 if ( !field.alias().isEmpty() && mAttributeAliasMap.value( field.name() ).isEmpty() )
2242 {
2243 mAttributeAliasMap[ field.name() ] = field.alias();
2244 }
2245 if ( !mAttributeSplitPolicy.contains( field.name() ) )
2246 {
2247 mAttributeSplitPolicy[ field.name() ] = field.splitPolicy();
2248 }
2249 if ( !mAttributeDuplicatePolicy.contains( field.name() ) )
2250 {
2251 mAttributeDuplicatePolicy[ field.name() ] = field.duplicatePolicy();
2252 }
2253 if ( !mAttributeMergePolicy.contains( field.name() ) )
2254 {
2255 mAttributeMergePolicy[ field.name() ] = field.mergePolicy();
2256 }
2257 }
2258
2259 if ( profile )
2260 profile->switchTask( tr( "Read layer fields" ) );
2261 updateFields();
2262
2263 if ( mProviderKey == QLatin1String( "postgres" ) )
2264 {
2265 // update datasource from data provider computed one
2266 mDataSource = mDataProvider->dataSourceUri( false );
2267
2268 QgsDebugMsgLevel( QStringLiteral( "Beautifying layer name %1" ).arg( name() ), 3 );
2269
2270 // adjust the display name for postgres layers
2271 const thread_local QRegularExpression reg( R"lit("[^"]+"\."([^"] + )"( \‍([^)]+\))?)lit" );
2272 const QRegularExpressionMatch match = reg.match( name() );
2273 if ( match.hasMatch() )
2274 {
2275 QStringList stuff = match.capturedTexts();
2276 QString lName = stuff[1];
2277
2278 const QMap<QString, QgsMapLayer *> &layers = QgsProject::instance()->mapLayers(); // skip-keyword-check
2279
2280 QMap<QString, QgsMapLayer *>::const_iterator it;
2281 for ( it = layers.constBegin(); it != layers.constEnd() && ( *it )->name() != lName; ++it )
2282 ;
2283
2284 if ( it != layers.constEnd() && stuff.size() > 2 )
2285 {
2286 lName += '.' + stuff[2].mid( 2, stuff[2].length() - 3 );
2287 }
2288
2289 if ( !lName.isEmpty() )
2290 setName( lName );
2291 }
2292 QgsDebugMsgLevel( QStringLiteral( "Beautified layer name %1" ).arg( name() ), 3 );
2293 }
2294 else if ( mProviderKey == QLatin1String( "osm" ) )
2295 {
2296 // make sure that the "observer" has been removed from URI to avoid crashes
2297 mDataSource = mDataProvider->dataSourceUri();
2298 }
2299 else if ( provider == QLatin1String( "ogr" ) )
2300 {
2301 // make sure that the /vsigzip or /vsizip is added to uri, if applicable
2302 mDataSource = mDataProvider->dataSourceUri();
2303 if ( mDataSource.right( 10 ) == QLatin1String( "|layerid=0" ) )
2304 mDataSource.chop( 10 );
2305 }
2306 else if ( provider == QLatin1String( "memory" ) )
2307 {
2308 // required so that source differs between memory layers
2309 mDataSource = mDataSource + QStringLiteral( "&uid=%1" ).arg( QUuid::createUuid().toString() );
2310 }
2311 else if ( provider == QLatin1String( "hana" ) )
2312 {
2313 // update datasource from data provider computed one
2314 mDataSource = mDataProvider->dataSourceUri( false );
2315 }
2316
2317 connect( mDataProvider, &QgsVectorDataProvider::dataChanged, this, &QgsVectorLayer::emitDataChanged );
2319
2320 return true;
2321} // QgsVectorLayer:: setDataProvider
2322
2323
2324
2325
2326/* virtual */
2327bool QgsVectorLayer::writeXml( QDomNode &layer_node,
2328 QDomDocument &document,
2329 const QgsReadWriteContext &context ) const
2330{
2332
2333 // first get the layer element so that we can append the type attribute
2334
2335 QDomElement mapLayerNode = layer_node.toElement();
2336
2337 if ( mapLayerNode.isNull() || ( "maplayer" != mapLayerNode.nodeName() ) )
2338 {
2339 QgsDebugMsgLevel( QStringLiteral( "can't find <maplayer>" ), 2 );
2340 return false;
2341 }
2342
2343 mapLayerNode.setAttribute( QStringLiteral( "type" ), QgsMapLayerFactory::typeToString( Qgis::LayerType::Vector ) );
2344
2345 // set the geometry type
2346 mapLayerNode.setAttribute( QStringLiteral( "geometry" ), QgsWkbTypes::geometryDisplayString( geometryType() ) );
2347 mapLayerNode.setAttribute( QStringLiteral( "wkbType" ), qgsEnumValueToKey( wkbType() ) );
2348
2349 // add provider node
2350 if ( mDataProvider )
2351 {
2352 QDomElement provider = document.createElement( QStringLiteral( "provider" ) );
2353 provider.setAttribute( QStringLiteral( "encoding" ), mDataProvider->encoding() );
2354 QDomText providerText = document.createTextNode( providerType() );
2355 provider.appendChild( providerText );
2356 layer_node.appendChild( provider );
2357 }
2358
2359 //save joins
2360 mJoinBuffer->writeXml( layer_node, document );
2361
2362 // dependencies
2363 QDomElement dependenciesElement = document.createElement( QStringLiteral( "layerDependencies" ) );
2364 const auto constDependencies = dependencies();
2365 for ( const QgsMapLayerDependency &dep : constDependencies )
2366 {
2368 continue;
2369 QDomElement depElem = document.createElement( QStringLiteral( "layer" ) );
2370 depElem.setAttribute( QStringLiteral( "id" ), dep.layerId() );
2371 dependenciesElement.appendChild( depElem );
2372 }
2373 layer_node.appendChild( dependenciesElement );
2374
2375 // change dependencies
2376 QDomElement dataDependenciesElement = document.createElement( QStringLiteral( "dataDependencies" ) );
2377 for ( const QgsMapLayerDependency &dep : constDependencies )
2378 {
2379 if ( dep.type() != QgsMapLayerDependency::DataDependency )
2380 continue;
2381 QDomElement depElem = document.createElement( QStringLiteral( "layer" ) );
2382 depElem.setAttribute( QStringLiteral( "id" ), dep.layerId() );
2383 dataDependenciesElement.appendChild( depElem );
2384 }
2385 layer_node.appendChild( dataDependenciesElement );
2386
2387 // save expression fields
2388 mExpressionFieldBuffer->writeXml( layer_node, document );
2389
2390 writeStyleManager( layer_node, document );
2391
2392 // auxiliary layer
2393 QDomElement asElem = document.createElement( QStringLiteral( "auxiliaryLayer" ) );
2394 if ( mAuxiliaryLayer )
2395 {
2396 const QString pkField = mAuxiliaryLayer->joinInfo().targetFieldName();
2397 asElem.setAttribute( QStringLiteral( "key" ), pkField );
2398 }
2399 layer_node.appendChild( asElem );
2400
2401 // renderer specific settings
2402 QString errorMsg;
2403 return writeSymbology( layer_node, document, errorMsg, context );
2404}
2405
2406QString QgsVectorLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
2407{
2409
2410 if ( providerType() == QLatin1String( "memory" ) )
2411 {
2412 // Refetch the source from the provider, because adding fields actually changes the source for this provider.
2413 return dataProvider()->dataSourceUri();
2414 }
2415
2417}
2418
2419QString QgsVectorLayer::decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const
2420{
2422
2423 return QgsProviderRegistry::instance()->relativeToAbsoluteUri( provider, source, context );
2424}
2425
2426
2427
2435
2436
2437bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMessage,
2439{
2441
2442 if ( categories.testFlag( Fields ) )
2443 {
2444 if ( !mExpressionFieldBuffer )
2445 mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
2446 mExpressionFieldBuffer->readXml( layerNode );
2447
2448 updateFields();
2449 }
2450
2451 if ( categories.testFlag( Relations ) )
2452 {
2453 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Relations" ) );
2454
2455 // Restore referenced layers: relations where "this" is the child layer (the referencing part, that holds the FK)
2456 QDomNodeList referencedLayersNodeList = layerNode.toElement().elementsByTagName( QStringLiteral( "referencedLayers" ) );
2457 if ( referencedLayersNodeList.size() > 0 )
2458 {
2459 const QDomNodeList relationNodes { referencedLayersNodeList.at( 0 ).childNodes() };
2460 for ( int i = 0; i < relationNodes.length(); ++i )
2461 {
2462 const QDomElement relationElement = relationNodes.at( i ).toElement();
2463
2464 mWeakRelations.push_back( QgsWeakRelation::readXml( this, QgsWeakRelation::Referencing, relationElement, context.pathResolver() ) );
2465 }
2466 }
2467
2468 // Restore referencing layers: relations where "this" is the parent layer (the referenced part where the FK points to)
2469 QDomNodeList referencingLayersNodeList = layerNode.toElement().elementsByTagName( QStringLiteral( "referencingLayers" ) );
2470 if ( referencingLayersNodeList.size() > 0 )
2471 {
2472 const QDomNodeList relationNodes { referencingLayersNodeList.at( 0 ).childNodes() };
2473 for ( int i = 0; i < relationNodes.length(); ++i )
2474 {
2475 const QDomElement relationElement = relationNodes.at( i ).toElement();
2476 mWeakRelations.push_back( QgsWeakRelation::readXml( this, QgsWeakRelation::Referenced, relationElement, context.pathResolver() ) );
2477 }
2478 }
2479 }
2480
2481 QDomElement layerElement = layerNode.toElement();
2482
2483 readCommonStyle( layerElement, context, categories );
2484
2485 readStyle( layerNode, errorMessage, context, categories );
2486
2487 if ( categories.testFlag( MapTips ) )
2488 {
2489 QDomElement mapTipElem = layerNode.namedItem( QStringLiteral( "mapTip" ) ).toElement();
2490 setMapTipTemplate( mapTipElem.text() );
2491 setMapTipsEnabled( mapTipElem.attribute( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt() == 1 );
2492 }
2493
2494 if ( categories.testFlag( LayerConfiguration ) )
2495 mDisplayExpression = layerNode.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text();
2496
2497 // Try to migrate pre QGIS 3.0 display field property
2498 QString displayField = layerNode.namedItem( QStringLiteral( "displayfield" ) ).toElement().text();
2499 if ( mFields.lookupField( displayField ) < 0 )
2500 {
2501 // if it's not a field, it's a maptip
2502 if ( mMapTipTemplate.isEmpty() && categories.testFlag( MapTips ) )
2503 mMapTipTemplate = displayField;
2504 }
2505 else
2506 {
2507 if ( mDisplayExpression.isEmpty() && categories.testFlag( LayerConfiguration ) )
2508 mDisplayExpression = QgsExpression::quotedColumnRef( displayField );
2509 }
2510
2511 // process the attribute actions
2512 if ( categories.testFlag( Actions ) )
2513 mActions->readXml( layerNode );
2514
2515 if ( categories.testFlag( Fields ) )
2516 {
2517 // IMPORTANT - we don't clear mAttributeAliasMap here, as it may contain aliases which are coming direct
2518 // from the data provider. Instead we leave any existing aliases and only overwrite them if the style
2519 // has a specific value for that field's alias
2520 QDomNode aliasesNode = layerNode.namedItem( QStringLiteral( "aliases" ) );
2521 if ( !aliasesNode.isNull() )
2522 {
2523 QDomElement aliasElem;
2524
2525 QDomNodeList aliasNodeList = aliasesNode.toElement().elementsByTagName( QStringLiteral( "alias" ) );
2526 for ( int i = 0; i < aliasNodeList.size(); ++i )
2527 {
2528 aliasElem = aliasNodeList.at( i ).toElement();
2529
2530 QString field;
2531 if ( aliasElem.hasAttribute( QStringLiteral( "field" ) ) )
2532 {
2533 field = aliasElem.attribute( QStringLiteral( "field" ) );
2534 }
2535 else
2536 {
2537 int index = aliasElem.attribute( QStringLiteral( "index" ) ).toInt();
2538
2539 if ( index >= 0 && index < fields().count() )
2540 field = fields().at( index ).name();
2541 }
2542
2543 QString alias;
2544
2545 if ( !aliasElem.attribute( QStringLiteral( "name" ) ).isEmpty() )
2546 {
2547 //if it has alias
2548 alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
2549 QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + aliasElem.attribute( QStringLiteral( "name" ) ), 3 );
2550 }
2551 else
2552 {
2553 //if it has no alias, it should be the fields translation
2554 alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
2555 QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + field, 3 );
2556 //if it gets the exact field value, there has been no translation (or not even translation loaded) - so no alias should be generated;
2557 if ( alias == aliasElem.attribute( QStringLiteral( "field" ) ) )
2558 alias.clear();
2559 }
2560
2561 QgsDebugMsgLevel( "field " + field + " origalias " + aliasElem.attribute( QStringLiteral( "name" ) ) + " trans " + alias, 3 );
2562 mAttributeAliasMap.insert( field, alias );
2563 }
2564 }
2565
2566 // IMPORTANT - we don't clear mAttributeSplitPolicy here, as it may contain policies which are coming direct
2567 // from the data provider. Instead we leave any existing policies and only overwrite them if the style
2568 // has a specific value for that field's policy
2569 const QDomNode splitPoliciesNode = layerNode.namedItem( QStringLiteral( "splitPolicies" ) );
2570 if ( !splitPoliciesNode.isNull() )
2571 {
2572 const QDomNodeList splitPolicyNodeList = splitPoliciesNode.toElement().elementsByTagName( QStringLiteral( "policy" ) );
2573 for ( int i = 0; i < splitPolicyNodeList.size(); ++i )
2574 {
2575 const QDomElement splitPolicyElem = splitPolicyNodeList.at( i ).toElement();
2576 const QString field = splitPolicyElem.attribute( QStringLiteral( "field" ) );
2577 const Qgis::FieldDomainSplitPolicy policy = qgsEnumKeyToValue( splitPolicyElem.attribute( QStringLiteral( "policy" ) ), Qgis::FieldDomainSplitPolicy::Duplicate );
2578 mAttributeSplitPolicy.insert( field, policy );
2579 }
2580 }
2581
2582 // The duplicate policy is - unlike alias and split policy - never defined by the data provider, so we clear the map
2583 mAttributeDuplicatePolicy.clear();
2584 const QDomNode duplicatePoliciesNode = layerNode.namedItem( QStringLiteral( "duplicatePolicies" ) );
2585 if ( !duplicatePoliciesNode.isNull() )
2586 {
2587 const QDomNodeList duplicatePolicyNodeList = duplicatePoliciesNode.toElement().elementsByTagName( QStringLiteral( "policy" ) );
2588 for ( int i = 0; i < duplicatePolicyNodeList.size(); ++i )
2589 {
2590 const QDomElement duplicatePolicyElem = duplicatePolicyNodeList.at( i ).toElement();
2591 const QString field = duplicatePolicyElem.attribute( QStringLiteral( "field" ) );
2592 const Qgis::FieldDuplicatePolicy policy = qgsEnumKeyToValue( duplicatePolicyElem.attribute( QStringLiteral( "policy" ) ), Qgis::FieldDuplicatePolicy::Duplicate );
2593 mAttributeDuplicatePolicy.insert( field, policy );
2594 }
2595 }
2596
2597 const QDomNode mergePoliciesNode = layerNode.namedItem( QStringLiteral( "mergePolicies" ) );
2598 if ( !mergePoliciesNode.isNull() )
2599 {
2600 const QDomNodeList mergePolicyNodeList = mergePoliciesNode.toElement().elementsByTagName( QStringLiteral( "policy" ) );
2601 for ( int i = 0; i < mergePolicyNodeList.size(); ++i )
2602 {
2603 const QDomElement mergePolicyElem = mergePolicyNodeList.at( i ).toElement();
2604 const QString field = mergePolicyElem.attribute( QStringLiteral( "field" ) );
2605 const Qgis::FieldDomainMergePolicy policy = qgsEnumKeyToValue( mergePolicyElem.attribute( QStringLiteral( "policy" ) ), Qgis::FieldDomainMergePolicy::UnsetField );
2606 mAttributeMergePolicy.insert( field, policy );
2607 }
2608 }
2609
2610 // default expressions
2611 mDefaultExpressionMap.clear();
2612 QDomNode defaultsNode = layerNode.namedItem( QStringLiteral( "defaults" ) );
2613 if ( !defaultsNode.isNull() )
2614 {
2615 QDomNodeList defaultNodeList = defaultsNode.toElement().elementsByTagName( QStringLiteral( "default" ) );
2616 for ( int i = 0; i < defaultNodeList.size(); ++i )
2617 {
2618 QDomElement defaultElem = defaultNodeList.at( i ).toElement();
2619
2620 QString field = defaultElem.attribute( QStringLiteral( "field" ), QString() );
2621 QString expression = defaultElem.attribute( QStringLiteral( "expression" ), QString() );
2622 bool applyOnUpdate = defaultElem.attribute( QStringLiteral( "applyOnUpdate" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
2623 if ( field.isEmpty() || expression.isEmpty() )
2624 continue;
2625
2626 mDefaultExpressionMap.insert( field, QgsDefaultValue( expression, applyOnUpdate ) );
2627 }
2628 }
2629
2630 // constraints
2631 mFieldConstraints.clear();
2632 mFieldConstraintStrength.clear();
2633 QDomNode constraintsNode = layerNode.namedItem( QStringLiteral( "constraints" ) );
2634 if ( !constraintsNode.isNull() )
2635 {
2636 QDomNodeList constraintNodeList = constraintsNode.toElement().elementsByTagName( QStringLiteral( "constraint" ) );
2637 for ( int i = 0; i < constraintNodeList.size(); ++i )
2638 {
2639 QDomElement constraintElem = constraintNodeList.at( i ).toElement();
2640
2641 QString field = constraintElem.attribute( QStringLiteral( "field" ), QString() );
2642 int constraints = constraintElem.attribute( QStringLiteral( "constraints" ), QStringLiteral( "0" ) ).toInt();
2643 if ( field.isEmpty() || constraints == 0 )
2644 continue;
2645
2646 mFieldConstraints.insert( field, static_cast< QgsFieldConstraints::Constraints >( constraints ) );
2647
2648 int uniqueStrength = constraintElem.attribute( QStringLiteral( "unique_strength" ), QStringLiteral( "1" ) ).toInt();
2649 int notNullStrength = constraintElem.attribute( QStringLiteral( "notnull_strength" ), QStringLiteral( "1" ) ).toInt();
2650 int expStrength = constraintElem.attribute( QStringLiteral( "exp_strength" ), QStringLiteral( "1" ) ).toInt();
2651
2652 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintUnique ), static_cast< QgsFieldConstraints::ConstraintStrength >( uniqueStrength ) );
2653 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintNotNull ), static_cast< QgsFieldConstraints::ConstraintStrength >( notNullStrength ) );
2654 mFieldConstraintStrength.insert( qMakePair( field, QgsFieldConstraints::ConstraintExpression ), static_cast< QgsFieldConstraints::ConstraintStrength >( expStrength ) );
2655 }
2656 }
2657 mFieldConstraintExpressions.clear();
2658 QDomNode constraintExpressionsNode = layerNode.namedItem( QStringLiteral( "constraintExpressions" ) );
2659 if ( !constraintExpressionsNode.isNull() )
2660 {
2661 QDomNodeList constraintNodeList = constraintExpressionsNode.toElement().elementsByTagName( QStringLiteral( "constraint" ) );
2662 for ( int i = 0; i < constraintNodeList.size(); ++i )
2663 {
2664 QDomElement constraintElem = constraintNodeList.at( i ).toElement();
2665
2666 QString field = constraintElem.attribute( QStringLiteral( "field" ), QString() );
2667 QString exp = constraintElem.attribute( QStringLiteral( "exp" ), QString() );
2668 QString desc = constraintElem.attribute( QStringLiteral( "desc" ), QString() );
2669 if ( field.isEmpty() || exp.isEmpty() )
2670 continue;
2671
2672 mFieldConstraintExpressions.insert( field, qMakePair( exp, desc ) );
2673 }
2674 }
2675
2676 updateFields();
2677 }
2678
2679 // load field configuration
2680 if ( categories.testFlag( Fields ) || categories.testFlag( Forms ) )
2681 {
2682 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Forms" ) );
2683
2684 QDomElement widgetsElem = layerNode.namedItem( QStringLiteral( "fieldConfiguration" ) ).toElement();
2685 QDomNodeList fieldConfigurationElementList = widgetsElem.elementsByTagName( QStringLiteral( "field" ) );
2686 for ( int i = 0; i < fieldConfigurationElementList.size(); ++i )
2687 {
2688 const QDomElement fieldConfigElement = fieldConfigurationElementList.at( i ).toElement();
2689 const QDomElement fieldWidgetElement = fieldConfigElement.elementsByTagName( QStringLiteral( "editWidget" ) ).at( 0 ).toElement();
2690
2691 QString fieldName = fieldConfigElement.attribute( QStringLiteral( "name" ) );
2692
2693 if ( categories.testFlag( Fields ) )
2694 mFieldConfigurationFlags[fieldName] = qgsFlagKeysToValue( fieldConfigElement.attribute( QStringLiteral( "configurationFlags" ) ), Qgis::FieldConfigurationFlag::NoFlag );
2695
2696 // load editor widget configuration
2697 if ( categories.testFlag( Forms ) )
2698 {
2699 const QString widgetType = fieldWidgetElement.attribute( QStringLiteral( "type" ) );
2700 const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
2701 const QDomElement optionsElem = cfgElem.childNodes().at( 0 ).toElement();
2702 QVariantMap optionsMap = QgsXmlUtils::readVariant( optionsElem ).toMap();
2703 // translate widget configuration strings
2704 if ( widgetType == QStringLiteral( "ValueRelation" ) )
2705 {
2706 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() );
2707 }
2708 if ( widgetType == QStringLiteral( "ValueMap" ) )
2709 {
2710 if ( optionsMap[ QStringLiteral( "map" ) ].canConvert<QList<QVariant>>() )
2711 {
2712 QList<QVariant> translatedValueList;
2713 const QList<QVariant> valueList = optionsMap[ QStringLiteral( "map" )].toList();
2714 for ( int i = 0, row = 0; i < valueList.count(); i++, row++ )
2715 {
2716 QMap<QString, QVariant> translatedValueMap;
2717 QString translatedKey = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fields:%2:valuemapdescriptions" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text(), fieldName ), valueList[i].toMap().constBegin().key() );
2718 translatedValueMap.insert( translatedKey, valueList[i].toMap().constBegin().value() );
2719 translatedValueList.append( translatedValueMap );
2720 }
2721 optionsMap.insert( QStringLiteral( "map" ), translatedValueList );
2722 }
2723 }
2724 QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, optionsMap );
2725 mFieldWidgetSetups[fieldName] = setup;
2726 }
2727 }
2728 }
2729
2730 // Legacy reading for QGIS 3.14 and older projects
2731 // Attributes excluded from WMS and WFS
2732 if ( categories.testFlag( Fields ) )
2733 {
2734 const QList<QPair<QString, Qgis::FieldConfigurationFlag>> legacyConfig
2735 {
2736 qMakePair( QStringLiteral( "excludeAttributesWMS" ), Qgis::FieldConfigurationFlag::HideFromWms ),
2737 qMakePair( QStringLiteral( "excludeAttributesWFS" ), Qgis::FieldConfigurationFlag::HideFromWfs )
2738 };
2739 for ( const auto &config : legacyConfig )
2740 {
2741 QDomNode excludeNode = layerNode.namedItem( config.first );
2742 if ( !excludeNode.isNull() )
2743 {
2744 QDomNodeList attributeNodeList = excludeNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
2745 for ( int i = 0; i < attributeNodeList.size(); ++i )
2746 {
2747 QString fieldName = attributeNodeList.at( i ).toElement().text();
2748 if ( !mFieldConfigurationFlags.contains( fieldName ) )
2749 mFieldConfigurationFlags[fieldName] = config.second;
2750 else
2751 mFieldConfigurationFlags[fieldName].setFlag( config.second, true );
2752 }
2753 }
2754 }
2755 }
2756
2757 if ( categories.testFlag( GeometryOptions ) )
2758 mGeometryOptions->readXml( layerNode.namedItem( QStringLiteral( "geometryOptions" ) ) );
2759
2760 if ( categories.testFlag( Forms ) )
2761 mEditFormConfig.readXml( layerNode, context );
2762
2763 if ( categories.testFlag( AttributeTable ) )
2764 {
2765 mAttributeTableConfig.readXml( layerNode );
2766 mConditionalStyles->readXml( layerNode, context );
2767 mStoredExpressionManager->readXml( layerNode );
2768 }
2769
2770 if ( categories.testFlag( CustomProperties ) )
2771 readCustomProperties( layerNode, QStringLiteral( "variable" ) );
2772
2773 QDomElement mapLayerNode = layerNode.toElement();
2774 if ( categories.testFlag( LayerConfiguration )
2775 && mapLayerNode.attribute( QStringLiteral( "readOnly" ), QStringLiteral( "0" ) ).toInt() == 1 )
2776 mReadOnly = true;
2777
2778 updateFields();
2779
2780 if ( categories.testFlag( Legend ) )
2781 {
2782 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Legend" ) );
2783
2784 const QDomElement legendElem = layerNode.firstChildElement( QStringLiteral( "legend" ) );
2785 if ( !legendElem.isNull() )
2786 {
2787 std::unique_ptr< QgsMapLayerLegend > legend( QgsMapLayerLegend::defaultVectorLegend( this ) );
2788 legend->readXml( legendElem, context );
2789 setLegend( legend.release() );
2790 mSetLegendFromStyle = true;
2791 }
2792 }
2793
2794 return true;
2795}
2796
2797bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage,
2799{
2801
2802 bool result = true;
2803 emit readCustomSymbology( node.toElement(), errorMessage );
2804
2805 // we must try to restore a renderer if our geometry type is unknown
2806 // as this allows the renderer to be correctly restored even for layers
2807 // with broken sources
2808 if ( isSpatial() || mWkbType == Qgis::WkbType::Unknown )
2809 {
2810 // defer style changed signal until we've set the renderer, labeling, everything.
2811 // we don't want multiple signals!
2812 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
2813
2814 // try renderer v2 first
2815 if ( categories.testFlag( Symbology ) )
2816 {
2817 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Symbology" ) );
2818
2819 QDomElement rendererElement = node.firstChildElement( RENDERER_TAG_NAME );
2820 if ( !rendererElement.isNull() )
2821 {
2822 QgsFeatureRenderer *r = QgsFeatureRenderer::load( rendererElement, context );
2823 if ( r )
2824 {
2825 setRenderer( r );
2826 }
2827 else
2828 {
2829 result = false;
2830 }
2831 }
2832 // make sure layer has a renderer - if none exists, fallback to a default renderer
2833 if ( isSpatial() && !renderer() )
2834 {
2836 }
2837
2838 if ( mSelectionProperties )
2839 mSelectionProperties->readXml( node.toElement(), context );
2840 }
2841
2842 // read labeling definition
2843 if ( categories.testFlag( Labeling ) )
2844 {
2845 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Labeling" ) );
2846
2847 QDomElement labelingElement = node.firstChildElement( QStringLiteral( "labeling" ) );
2849 if ( labelingElement.isNull() ||
2850 ( labelingElement.attribute( QStringLiteral( "type" ) ) == QLatin1String( "simple" ) && labelingElement.firstChildElement( QStringLiteral( "settings" ) ).isNull() ) )
2851 {
2852 // make sure we have custom properties for labeling for 2.x projects
2853 // (custom properties should be already loaded when reading the whole layer from XML,
2854 // but when reading style, custom properties are not read)
2855 readCustomProperties( node, QStringLiteral( "labeling" ) );
2856
2857 // support for pre-QGIS 3 labeling configurations written in custom properties
2858 labeling = readLabelingFromCustomProperties();
2859 }
2860 else
2861 {
2862 labeling = QgsAbstractVectorLayerLabeling::create( labelingElement, context );
2863 }
2865
2866 if ( node.toElement().hasAttribute( QStringLiteral( "labelsEnabled" ) ) )
2867 mLabelsEnabled = node.toElement().attribute( QStringLiteral( "labelsEnabled" ) ).toInt();
2868 else
2869 mLabelsEnabled = true;
2870 }
2871
2872 if ( categories.testFlag( Symbology ) )
2873 {
2874 // get and set the blend mode if it exists
2875 QDomNode blendModeNode = node.namedItem( QStringLiteral( "blendMode" ) );
2876 if ( !blendModeNode.isNull() )
2877 {
2878 QDomElement e = blendModeNode.toElement();
2879 setBlendMode( QgsPainting::getCompositionMode( static_cast< Qgis::BlendMode >( e.text().toInt() ) ) );
2880 }
2881
2882 // get and set the feature blend mode if it exists
2883 QDomNode featureBlendModeNode = node.namedItem( QStringLiteral( "featureBlendMode" ) );
2884 if ( !featureBlendModeNode.isNull() )
2885 {
2886 QDomElement e = featureBlendModeNode.toElement();
2887 setFeatureBlendMode( QgsPainting::getCompositionMode( static_cast< Qgis::BlendMode >( e.text().toInt() ) ) );
2888 }
2889 }
2890
2891 // get and set the layer transparency and scale visibility if they exists
2892 if ( categories.testFlag( Rendering ) )
2893 {
2894 QDomNode layerTransparencyNode = node.namedItem( QStringLiteral( "layerTransparency" ) );
2895 if ( !layerTransparencyNode.isNull() )
2896 {
2897 QDomElement e = layerTransparencyNode.toElement();
2898 setOpacity( 1.0 - e.text().toInt() / 100.0 );
2899 }
2900 QDomNode layerOpacityNode = node.namedItem( QStringLiteral( "layerOpacity" ) );
2901 if ( !layerOpacityNode.isNull() )
2902 {
2903 QDomElement e = layerOpacityNode.toElement();
2904 setOpacity( e.text().toDouble() );
2905 }
2906
2907 const bool hasScaleBasedVisibiliy { node.attributes().namedItem( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).nodeValue() == '1' };
2908 setScaleBasedVisibility( hasScaleBasedVisibiliy );
2909 bool ok;
2910 const double maxScale { node.attributes().namedItem( QStringLiteral( "maxScale" ) ).nodeValue().toDouble( &ok ) };
2911 if ( ok )
2912 {
2913 setMaximumScale( maxScale );
2914 }
2915 const double minScale { node.attributes().namedItem( QStringLiteral( "minScale" ) ).nodeValue().toDouble( &ok ) };
2916 if ( ok )
2917 {
2918 setMinimumScale( minScale );
2919 }
2920
2921 QDomElement e = node.toElement();
2922
2923 // get the simplification drawing settings
2924 mSimplifyMethod.setSimplifyHints( static_cast< Qgis::VectorRenderingSimplificationFlags >( e.attribute( QStringLiteral( "simplifyDrawingHints" ), QStringLiteral( "1" ) ).toInt() ) );
2925 mSimplifyMethod.setSimplifyAlgorithm( static_cast< Qgis::VectorSimplificationAlgorithm >( e.attribute( QStringLiteral( "simplifyAlgorithm" ), QStringLiteral( "0" ) ).toInt() ) );
2926 mSimplifyMethod.setThreshold( e.attribute( QStringLiteral( "simplifyDrawingTol" ), QStringLiteral( "1" ) ).toFloat() );
2927 mSimplifyMethod.setForceLocalOptimization( e.attribute( QStringLiteral( "simplifyLocal" ), QStringLiteral( "1" ) ).toInt() );
2928 mSimplifyMethod.setMaximumScale( e.attribute( QStringLiteral( "simplifyMaxScale" ), QStringLiteral( "1" ) ).toFloat() );
2929
2930 if ( mRenderer )
2931 mRenderer->setReferenceScale( e.attribute( QStringLiteral( "symbologyReferenceScale" ), QStringLiteral( "-1" ) ).toDouble() );
2932 }
2933
2934 //diagram renderer and diagram layer settings
2935 if ( categories.testFlag( Diagrams ) )
2936 {
2937 QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Diagrams" ) );
2938
2939 delete mDiagramRenderer;
2940 mDiagramRenderer = nullptr;
2941 QDomElement singleCatDiagramElem = node.firstChildElement( QStringLiteral( "SingleCategoryDiagramRenderer" ) );
2942 if ( !singleCatDiagramElem.isNull() )
2943 {
2944 mDiagramRenderer = new QgsSingleCategoryDiagramRenderer();
2945 mDiagramRenderer->readXml( singleCatDiagramElem, context );
2946 }
2947 QDomElement linearDiagramElem = node.firstChildElement( QStringLiteral( "LinearlyInterpolatedDiagramRenderer" ) );
2948 if ( !linearDiagramElem.isNull() )
2949 {
2950 if ( linearDiagramElem.hasAttribute( QStringLiteral( "classificationAttribute" ) ) )
2951 {
2952 // fix project from before QGIS 3.0
2953 int idx = linearDiagramElem.attribute( QStringLiteral( "classificationAttribute" ) ).toInt();
2954 if ( idx >= 0 && idx < mFields.count() )
2955 linearDiagramElem.setAttribute( QStringLiteral( "classificationField" ), mFields.at( idx ).name() );
2956 }
2957
2958 mDiagramRenderer = new QgsLinearlyInterpolatedDiagramRenderer();
2959 mDiagramRenderer->readXml( linearDiagramElem, context );
2960 }
2961 QDomElement stackedDiagramElem = node.firstChildElement( QStringLiteral( "StackedDiagramRenderer" ) );
2962 if ( !stackedDiagramElem.isNull() )
2963 {
2964 mDiagramRenderer = new QgsStackedDiagramRenderer();
2965 mDiagramRenderer->readXml( stackedDiagramElem, context );
2966 }
2967
2968 if ( mDiagramRenderer )
2969 {
2970 QDomElement diagramSettingsElem = node.firstChildElement( QStringLiteral( "DiagramLayerSettings" ) );
2971 if ( !diagramSettingsElem.isNull() )
2972 {
2973 bool oldXPos = diagramSettingsElem.hasAttribute( QStringLiteral( "xPosColumn" ) );
2974 bool oldYPos = diagramSettingsElem.hasAttribute( QStringLiteral( "yPosColumn" ) );
2975 bool oldShow = diagramSettingsElem.hasAttribute( QStringLiteral( "showColumn" ) );
2976 if ( oldXPos || oldYPos || oldShow )
2977 {
2978 // fix project from before QGIS 3.0
2980 if ( oldXPos )
2981 {
2982 int xPosColumn = diagramSettingsElem.attribute( QStringLiteral( "xPosColumn" ) ).toInt();
2983 if ( xPosColumn >= 0 && xPosColumn < mFields.count() )
2985 }
2986 if ( oldYPos )
2987 {
2988 int yPosColumn = diagramSettingsElem.attribute( QStringLiteral( "yPosColumn" ) ).toInt();
2989 if ( yPosColumn >= 0 && yPosColumn < mFields.count() )
2991 }
2992 if ( oldShow )
2993 {
2994 int showColumn = diagramSettingsElem.attribute( QStringLiteral( "showColumn" ) ).toInt();
2995 if ( showColumn >= 0 && showColumn < mFields.count() )
2996 ddp.setProperty( QgsDiagramLayerSettings::Property::Show, QgsProperty::fromField( mFields.at( showColumn ).name(), true ) );
2997 }
2998 QDomElement propertiesElem = diagramSettingsElem.ownerDocument().createElement( QStringLiteral( "properties" ) );
3000 {
3001 { static_cast< int >( QgsDiagramLayerSettings::Property::PositionX ), QgsPropertyDefinition( "positionX", QObject::tr( "Position (X)" ), QgsPropertyDefinition::Double ) },
3002 { static_cast< int >( QgsDiagramLayerSettings::Property::PositionY ), QgsPropertyDefinition( "positionY", QObject::tr( "Position (Y)" ), QgsPropertyDefinition::Double ) },
3003 { static_cast< int >( QgsDiagramLayerSettings::Property::Show ), QgsPropertyDefinition( "show", QObject::tr( "Show diagram" ), QgsPropertyDefinition::Boolean ) },
3004 };
3005 ddp.writeXml( propertiesElem, defs );
3006 diagramSettingsElem.appendChild( propertiesElem );
3007 }
3008
3009 delete mDiagramLayerSettings;
3010 mDiagramLayerSettings = new QgsDiagramLayerSettings();
3011 mDiagramLayerSettings->readXml( diagramSettingsElem );
3012 }
3013 }
3014 }
3015 // end diagram
3016
3017 styleChangedSignalBlocker.release();
3019 }
3020 return result;
3021}
3022
3023
3024bool QgsVectorLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
3025 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
3026{
3028
3029 QDomElement layerElement = node.toElement();
3030 writeCommonStyle( layerElement, doc, context, categories );
3031
3032 ( void )writeStyle( node, doc, errorMessage, context, categories );
3033
3034 if ( categories.testFlag( GeometryOptions ) )
3035 mGeometryOptions->writeXml( node );
3036
3037 if ( categories.testFlag( Legend ) && legend() )
3038 {
3039 QDomElement legendElement = legend()->writeXml( doc, context );
3040 if ( !legendElement.isNull() )
3041 node.appendChild( legendElement );
3042 }
3043
3044 // Relation information for both referenced and referencing sides
3045 if ( categories.testFlag( Relations ) )
3046 {
3047 if ( QgsProject *p = project() )
3048 {
3049 // Store referenced layers: relations where "this" is the child layer (the referencing part, that holds the FK)
3050 QDomElement referencedLayersElement = doc.createElement( QStringLiteral( "referencedLayers" ) );
3051 node.appendChild( referencedLayersElement );
3052
3053 const QList<QgsRelation> referencingRelations { p->relationManager()->referencingRelations( this ) };
3054 for ( const QgsRelation &rel : referencingRelations )
3055 {
3056 switch ( rel.type() )
3057 {
3059 QgsWeakRelation::writeXml( this, QgsWeakRelation::Referencing, rel, referencedLayersElement, doc );
3060 break;
3062 break;
3063 }
3064 }
3065
3066 // Store referencing layers: relations where "this" is the parent layer (the referenced part, that holds the FK)
3067 QDomElement referencingLayersElement = doc.createElement( QStringLiteral( "referencingLayers" ) );
3068 node.appendChild( referencedLayersElement );
3069
3070 const QList<QgsRelation> referencedRelations { p->relationManager()->referencedRelations( this ) };
3071 for ( const QgsRelation &rel : referencedRelations )
3072 {
3073 switch ( rel.type() )
3074 {
3076 QgsWeakRelation::writeXml( this, QgsWeakRelation::Referenced, rel, referencingLayersElement, doc );
3077 break;
3079 break;
3080 }
3081 }
3082 }
3083 }
3084
3085 // write field configurations
3086 if ( categories.testFlag( Fields ) || categories.testFlag( Forms ) )
3087 {
3088 QDomElement fieldConfigurationElement;
3089 // field configuration flag
3090 fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );
3091 node.appendChild( fieldConfigurationElement );
3092
3093 for ( const QgsField &field : std::as_const( mFields ) )
3094 {
3095 QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
3096 fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );
3097 fieldConfigurationElement.appendChild( fieldElement );
3098
3099 if ( categories.testFlag( Fields ) )
3100 {
3101 fieldElement.setAttribute( QStringLiteral( "configurationFlags" ), qgsFlagValueToKeys( field.configurationFlags() ) );
3102 }
3103
3104 if ( categories.testFlag( Forms ) )
3105 {
3106 QgsEditorWidgetSetup widgetSetup = field.editorWidgetSetup();
3107
3108 // TODO : wrap this part in an if to only save if it was user-modified
3109 QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) );
3110 fieldElement.appendChild( editWidgetElement );
3111 editWidgetElement.setAttribute( QStringLiteral( "type" ), field.editorWidgetSetup().type() );
3112 QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) );
3113
3114 editWidgetConfigElement.appendChild( QgsXmlUtils::writeVariant( widgetSetup.config(), doc ) );
3115 editWidgetElement.appendChild( editWidgetConfigElement );
3116 // END TODO : wrap this part in an if to only save if it was user-modified
3117 }
3118 }
3119 }
3120
3121 if ( categories.testFlag( Fields ) )
3122 {
3123 //attribute aliases
3124 QDomElement aliasElem = doc.createElement( QStringLiteral( "aliases" ) );
3125 for ( const QgsField &field : std::as_const( mFields ) )
3126 {
3127 QDomElement aliasEntryElem = doc.createElement( QStringLiteral( "alias" ) );
3128 aliasEntryElem.setAttribute( QStringLiteral( "field" ), field.name() );
3129 aliasEntryElem.setAttribute( QStringLiteral( "index" ), mFields.indexFromName( field.name() ) );
3130 aliasEntryElem.setAttribute( QStringLiteral( "name" ), field.alias() );
3131 aliasElem.appendChild( aliasEntryElem );
3132 }
3133 node.appendChild( aliasElem );
3134
3135 //split policies
3136 {
3137 QDomElement splitPoliciesElement = doc.createElement( QStringLiteral( "splitPolicies" ) );
3138 bool hasNonDefaultSplitPolicies = false;
3139 for ( const QgsField &field : std::as_const( mFields ) )
3140 {
3141 if ( field.splitPolicy() != Qgis::FieldDomainSplitPolicy::Duplicate )
3142 {
3143 QDomElement splitPolicyElem = doc.createElement( QStringLiteral( "policy" ) );
3144 splitPolicyElem.setAttribute( QStringLiteral( "field" ), field.name() );
3145 splitPolicyElem.setAttribute( QStringLiteral( "policy" ), qgsEnumValueToKey( field.splitPolicy() ) );
3146 splitPoliciesElement.appendChild( splitPolicyElem );
3147 hasNonDefaultSplitPolicies = true;
3148 }
3149 }
3150 if ( hasNonDefaultSplitPolicies )
3151 node.appendChild( splitPoliciesElement );
3152 }
3153
3154 //duplicate policies
3155 {
3156 QDomElement duplicatePoliciesElement = doc.createElement( QStringLiteral( "duplicatePolicies" ) );
3157 bool hasNonDefaultDuplicatePolicies = false;
3158 for ( const QgsField &field : std::as_const( mFields ) )
3159 {
3160 if ( field.duplicatePolicy() != Qgis::FieldDuplicatePolicy::Duplicate )
3161 {
3162 QDomElement duplicatePolicyElem = doc.createElement( QStringLiteral( "policy" ) );
3163 duplicatePolicyElem.setAttribute( QStringLiteral( "field" ), field.name() );
3164 duplicatePolicyElem.setAttribute( QStringLiteral( "policy" ), qgsEnumValueToKey( field.duplicatePolicy() ) );
3165 duplicatePoliciesElement.appendChild( duplicatePolicyElem );
3166 hasNonDefaultDuplicatePolicies = true;
3167 }
3168 }
3169 if ( hasNonDefaultDuplicatePolicies )
3170 node.appendChild( duplicatePoliciesElement );
3171 }
3172
3173 //merge policies
3174 {
3175 QDomElement mergePoliciesElement = doc.createElement( QStringLiteral( "mergePolicies" ) );
3176 bool hasNonDefaultMergePolicies = false;
3177 for ( const QgsField &field : std::as_const( mFields ) )
3178 {
3179 if ( field.mergePolicy() != Qgis::FieldDomainMergePolicy::UnsetField )
3180 {
3181 QDomElement mergePolicyElem = doc.createElement( QStringLiteral( "policy" ) );
3182 mergePolicyElem.setAttribute( QStringLiteral( "field" ), field.name() );
3183 mergePolicyElem.setAttribute( QStringLiteral( "policy" ), qgsEnumValueToKey( field.mergePolicy() ) );
3184 mergePoliciesElement.appendChild( mergePolicyElem );
3185 hasNonDefaultMergePolicies = true;
3186 }
3187 }
3188 if ( hasNonDefaultMergePolicies )
3189 node.appendChild( mergePoliciesElement );
3190 }
3191
3192 //default expressions
3193 QDomElement defaultsElem = doc.createElement( QStringLiteral( "defaults" ) );
3194 for ( const QgsField &field : std::as_const( mFields ) )
3195 {
3196 QDomElement defaultElem = doc.createElement( QStringLiteral( "default" ) );
3197 defaultElem.setAttribute( QStringLiteral( "field" ), field.name() );
3198 defaultElem.setAttribute( QStringLiteral( "expression" ), field.defaultValueDefinition().expression() );
3199 defaultElem.setAttribute( QStringLiteral( "applyOnUpdate" ), field.defaultValueDefinition().applyOnUpdate() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
3200 defaultsElem.appendChild( defaultElem );
3201 }
3202 node.appendChild( defaultsElem );
3203
3204 // constraints
3205 QDomElement constraintsElem = doc.createElement( QStringLiteral( "constraints" ) );
3206 for ( const QgsField &field : std::as_const( mFields ) )
3207 {
3208 QDomElement constraintElem = doc.createElement( QStringLiteral( "constraint" ) );
3209 constraintElem.setAttribute( QStringLiteral( "field" ), field.name() );
3210 constraintElem.setAttribute( QStringLiteral( "constraints" ), field.constraints().constraints() );
3211 constraintElem.setAttribute( QStringLiteral( "unique_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
3212 constraintElem.setAttribute( QStringLiteral( "notnull_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
3213 constraintElem.setAttribute( QStringLiteral( "exp_strength" ), field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
3214
3215 constraintsElem.appendChild( constraintElem );
3216 }
3217 node.appendChild( constraintsElem );
3218
3219 // constraint expressions
3220 QDomElement constraintExpressionsElem = doc.createElement( QStringLiteral( "constraintExpressions" ) );
3221 for ( const QgsField &field : std::as_const( mFields ) )
3222 {
3223 QDomElement constraintExpressionElem = doc.createElement( QStringLiteral( "constraint" ) );
3224 constraintExpressionElem.setAttribute( QStringLiteral( "field" ), field.name() );
3225 constraintExpressionElem.setAttribute( QStringLiteral( "exp" ), field.constraints().constraintExpression() );
3226 constraintExpressionElem.setAttribute( QStringLiteral( "desc" ), field.constraints().constraintDescription() );
3227 constraintExpressionsElem.appendChild( constraintExpressionElem );
3228 }
3229 node.appendChild( constraintExpressionsElem );
3230
3231 // save expression fields
3232 if ( !mExpressionFieldBuffer )
3233 {
3234 // can happen when saving style on a invalid layer
3236 dummy.writeXml( node, doc );
3237 }
3238 else
3239 {
3240 mExpressionFieldBuffer->writeXml( node, doc );
3241 }
3242 }
3243
3244 // add attribute actions
3245 if ( categories.testFlag( Actions ) )
3246 mActions->writeXml( node );
3247
3248 if ( categories.testFlag( AttributeTable ) )
3249 {
3250 mAttributeTableConfig.writeXml( node );
3251 mConditionalStyles->writeXml( node, doc, context );
3252 mStoredExpressionManager->writeXml( node );
3253 }
3254
3255 if ( categories.testFlag( Forms ) )
3256 mEditFormConfig.writeXml( node, context );
3257
3258 // save readonly state
3259 if ( categories.testFlag( LayerConfiguration ) )
3260 node.toElement().setAttribute( QStringLiteral( "readOnly" ), mReadOnly );
3261
3262 // save preview expression
3263 if ( categories.testFlag( LayerConfiguration ) )
3264 {
3265 QDomElement prevExpElem = doc.createElement( QStringLiteral( "previewExpression" ) );
3266 QDomText prevExpText = doc.createTextNode( mDisplayExpression );
3267 prevExpElem.appendChild( prevExpText );
3268 node.appendChild( prevExpElem );
3269 }
3270
3271 // save map tip
3272 if ( categories.testFlag( MapTips ) )
3273 {
3274 QDomElement mapTipElem = doc.createElement( QStringLiteral( "mapTip" ) );
3275 mapTipElem.setAttribute( QStringLiteral( "enabled" ), mapTipsEnabled() );
3276 QDomText mapTipText = doc.createTextNode( mMapTipTemplate );
3277 mapTipElem.appendChild( mapTipText );
3278 node.toElement().appendChild( mapTipElem );
3279 }
3280
3281 return true;
3282}
3283
3284bool QgsVectorLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
3285 const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
3286{
3288
3289 QDomElement mapLayerNode = node.toElement();
3290
3291 emit writeCustomSymbology( mapLayerNode, doc, errorMessage );
3292
3293 // we must try to write the renderer if our geometry type is unknown
3294 // as this allows the renderer to be correctly restored even for layers
3295 // with broken sources
3296 if ( isSpatial() || mWkbType == Qgis::WkbType::Unknown )
3297 {
3298 if ( categories.testFlag( Symbology ) )
3299 {
3300 if ( mRenderer )
3301 {
3302 QDomElement rendererElement = mRenderer->save( doc, context );
3303 node.appendChild( rendererElement );
3304 }
3305 if ( mSelectionProperties )
3306 {
3307 mSelectionProperties->writeXml( mapLayerNode, doc, context );
3308 }
3309 }
3310
3311 if ( categories.testFlag( Labeling ) )
3312 {
3313 if ( mLabeling )
3314 {
3315 QDomElement labelingElement = mLabeling->save( doc, context );
3316 node.appendChild( labelingElement );
3317 }
3318 mapLayerNode.setAttribute( QStringLiteral( "labelsEnabled" ), mLabelsEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
3319 }
3320
3321 // save the simplification drawing settings
3322 if ( categories.testFlag( Rendering ) )
3323 {
3324 mapLayerNode.setAttribute( QStringLiteral( "simplifyDrawingHints" ), QString::number( static_cast< int >( mSimplifyMethod.simplifyHints() ) ) );
3325 mapLayerNode.setAttribute( QStringLiteral( "simplifyAlgorithm" ), QString::number( static_cast< int >( mSimplifyMethod.simplifyAlgorithm() ) ) );
3326 mapLayerNode.setAttribute( QStringLiteral( "simplifyDrawingTol" ), QString::number( mSimplifyMethod.threshold() ) );
3327 mapLayerNode.setAttribute( QStringLiteral( "simplifyLocal" ), mSimplifyMethod.forceLocalOptimization() ? 1 : 0 );
3328 mapLayerNode.setAttribute( QStringLiteral( "simplifyMaxScale" ), QString::number( mSimplifyMethod.maximumScale() ) );
3329 }
3330
3331 //save customproperties
3332 if ( categories.testFlag( CustomProperties ) )
3333 {
3334 writeCustomProperties( node, doc );
3335 }
3336
3337 if ( categories.testFlag( Symbology ) )
3338 {
3339 // add the blend mode field
3340 QDomElement blendModeElem = doc.createElement( QStringLiteral( "blendMode" ) );
3341 QDomText blendModeText = doc.createTextNode( QString::number( static_cast< int >( QgsPainting::getBlendModeEnum( blendMode() ) ) ) );
3342 blendModeElem.appendChild( blendModeText );
3343 node.appendChild( blendModeElem );
3344
3345 // add the feature blend mode field
3346 QDomElement featureBlendModeElem = doc.createElement( QStringLiteral( "featureBlendMode" ) );
3347 QDomText featureBlendModeText = doc.createTextNode( QString::number( static_cast< int >( QgsPainting::getBlendModeEnum( featureBlendMode() ) ) ) );
3348 featureBlendModeElem.appendChild( featureBlendModeText );
3349 node.appendChild( featureBlendModeElem );
3350 }
3351
3352 // add the layer opacity and scale visibility
3353 if ( categories.testFlag( Rendering ) )
3354 {
3355 QDomElement layerOpacityElem = doc.createElement( QStringLiteral( "layerOpacity" ) );
3356 QDomText layerOpacityText = doc.createTextNode( QString::number( opacity() ) );
3357 layerOpacityElem.appendChild( layerOpacityText );
3358 node.appendChild( layerOpacityElem );
3359 mapLayerNode.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
3360 mapLayerNode.setAttribute( QStringLiteral( "maxScale" ), maximumScale() );
3361 mapLayerNode.setAttribute( QStringLiteral( "minScale" ), minimumScale() );
3362
3363 mapLayerNode.setAttribute( QStringLiteral( "symbologyReferenceScale" ), mRenderer ? mRenderer->referenceScale() : -1 );
3364 }
3365
3366 if ( categories.testFlag( Diagrams ) && mDiagramRenderer )
3367 {
3368 mDiagramRenderer->writeXml( mapLayerNode, doc, context );
3369 if ( mDiagramLayerSettings )
3370 mDiagramLayerSettings->writeXml( mapLayerNode, doc );
3371 }
3372 }
3373 return true;
3374}
3375
3376bool QgsVectorLayer::readSld( const QDomNode &node, QString &errorMessage )
3377{
3379
3380 // get the Name element
3381 QDomElement nameElem = node.firstChildElement( QStringLiteral( "Name" ) );
3382 if ( nameElem.isNull() )
3383 {
3384 errorMessage = QStringLiteral( "Warning: Name element not found within NamedLayer while it's required." );
3385 }
3386
3387 if ( isSpatial() )
3388 {
3389 QgsFeatureRenderer *r = QgsFeatureRenderer::loadSld( node, geometryType(), errorMessage );
3390 if ( !r )
3391 return false;
3392
3393 // defer style changed signal until we've set the renderer, labeling, everything.
3394 // we don't want multiple signals!
3395 ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );
3396
3397 setRenderer( r );
3398
3399 // labeling
3400 readSldLabeling( node );
3401
3402 styleChangedSignalBlocker.release();
3404 }
3405 return true;
3406}
3407
3408bool QgsVectorLayer::writeSld( QDomNode &node, QDomDocument &doc, QString &, const QVariantMap &props ) const
3409{
3411 QgsSldExportContext context;
3412 context.setExtraProperties( props );
3413 writeSld( node, doc, context );
3414 return true;
3415}
3416
3417bool QgsVectorLayer::writeSld( QDomNode &node, QDomDocument &doc, QgsSldExportContext &context ) const
3418{
3420
3421 QVariantMap localProps = context.extraProperties();
3423 {
3425 }
3426 context.setExtraProperties( localProps );
3427
3428 if ( isSpatial() )
3429 {
3430 // store the Name element
3431 QDomElement nameNode = doc.createElement( QStringLiteral( "se:Name" ) );
3432 nameNode.appendChild( doc.createTextNode( name() ) );
3433 node.appendChild( nameNode );
3434
3435 QDomElement userStyleElem = doc.createElement( QStringLiteral( "UserStyle" ) );
3436 node.appendChild( userStyleElem );
3437
3438 QDomElement nameElem = doc.createElement( QStringLiteral( "se:Name" ) );
3439 nameElem.appendChild( doc.createTextNode( name() ) );
3440
3441 userStyleElem.appendChild( nameElem );
3442
3443 QDomElement featureTypeStyleElem = doc.createElement( QStringLiteral( "se:FeatureTypeStyle" ) );
3444 userStyleElem.appendChild( featureTypeStyleElem );
3445
3446 mRenderer->toSld( doc, featureTypeStyleElem, context );
3447 if ( labelsEnabled() )
3448 {
3449 mLabeling->toSld( featureTypeStyleElem, context );
3450 }
3451 }
3452 return true;
3453}
3454
3455bool QgsVectorLayer::changeGeometry( QgsFeatureId fid, QgsGeometry &geom, bool skipDefaultValue )
3456{
3458
3459 if ( !mEditBuffer || !mDataProvider )
3460 {
3461 return false;
3462 }
3463
3464 if ( mGeometryOptions->isActive() )
3465 mGeometryOptions->apply( geom );
3466
3467 updateExtents();
3468
3469 bool result = mEditBuffer->changeGeometry( fid, geom );
3470
3471 if ( result )
3472 {
3473 updateExtents();
3474 if ( !skipDefaultValue && !mDefaultValueOnUpdateFields.isEmpty() )
3475 updateDefaultValues( fid );
3476 }
3477 return result;
3478}
3479
3480
3481bool QgsVectorLayer::changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue, bool skipDefaultValues, QgsVectorLayerToolsContext *context )
3482{
3484
3485 bool result = false;
3486
3487 switch ( fields().fieldOrigin( field ) )
3488 {
3490 result = mJoinBuffer->changeAttributeValue( fid, field, newValue, oldValue );
3491 if ( result )
3492 emit attributeValueChanged( fid, field, newValue );
3493 break;
3494
3498 {
3499 if ( mEditBuffer && mDataProvider )
3500 result = mEditBuffer->changeAttributeValue( fid, field, newValue, oldValue );
3501 break;
3502 }
3503
3505 break;
3506 }
3507
3508 if ( result && !skipDefaultValues && !mDefaultValueOnUpdateFields.isEmpty() )
3509 updateDefaultValues( fid, QgsFeature(), context ? context->expressionContext() : nullptr );
3510
3511 return result;
3512}
3513
3514bool QgsVectorLayer::changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues, bool skipDefaultValues, QgsVectorLayerToolsContext *context )
3515{
3517
3518 bool result = true;
3519
3520 QgsAttributeMap newValuesJoin;
3521 QgsAttributeMap oldValuesJoin;
3522
3523 QgsAttributeMap newValuesNotJoin;
3524 QgsAttributeMap oldValuesNotJoin;
3525
3526 for ( auto it = newValues.constBegin(); it != newValues.constEnd(); ++it )
3527 {
3528 const int field = it.key();
3529 const QVariant newValue = it.value();
3530 QVariant oldValue;
3531
3532 if ( oldValues.contains( field ) )
3533 oldValue = oldValues[field];
3534
3535 switch ( fields().fieldOrigin( field ) )
3536 {
3538 newValuesJoin[field] = newValue;
3539 oldValuesJoin[field] = oldValue;
3540 break;
3541
3545 {
3546 newValuesNotJoin[field] = newValue;
3547 oldValuesNotJoin[field] = oldValue;
3548 break;
3549 }
3550
3552 break;
3553 }
3554 }
3555
3556 if ( ! newValuesJoin.isEmpty() && mJoinBuffer )
3557 {
3558 result = mJoinBuffer->changeAttributeValues( fid, newValuesJoin, oldValuesJoin );
3559 }
3560
3561 if ( ! newValuesNotJoin.isEmpty() )
3562 {
3563 if ( mEditBuffer && mDataProvider )
3564 result &= mEditBuffer->changeAttributeValues( fid, newValuesNotJoin, oldValues );
3565 else
3566 result = false;
3567 }
3568
3569 if ( result && !skipDefaultValues && !mDefaultValueOnUpdateFields.isEmpty() )
3570 {
3571 updateDefaultValues( fid, QgsFeature(), context ? context->expressionContext() : nullptr );
3572 }
3573
3574 return result;
3575}
3576
3578{
3580
3581 if ( !mEditBuffer || !mDataProvider )
3582 return false;
3583
3584 return mEditBuffer->addAttribute( field );
3585}
3586
3588{
3590
3591 if ( attIndex < 0 || attIndex >= fields().count() )
3592 return;
3593
3594 QString name = fields().at( attIndex ).name();
3595 mFields[ attIndex ].setAlias( QString() );
3596 if ( mAttributeAliasMap.contains( name ) )
3597 {
3598 mAttributeAliasMap.remove( name );
3599 updateFields();
3600 mEditFormConfig.setFields( mFields );
3601 emit layerModified();
3602 }
3603}
3604
3605bool QgsVectorLayer::renameAttribute( int index, const QString &newName )
3606{
3608
3609 if ( index < 0 || index >= fields().count() )
3610 return false;
3611
3612 switch ( mFields.fieldOrigin( index ) )
3613 {
3615 {
3616 if ( mExpressionFieldBuffer )
3617 {
3618 int oi = mFields.fieldOriginIndex( index );
3619 mExpressionFieldBuffer->renameExpression( oi, newName );
3620 updateFields();
3621 return true;
3622 }
3623 else
3624 {
3625 return false;
3626 }
3627 }
3628
3631
3632 if ( !mEditBuffer || !mDataProvider )
3633 return false;
3634
3635 return mEditBuffer->renameAttribute( index, newName );
3636
3639 return false;
3640
3641 }
3642
3643 return false; // avoid warning
3644}
3645
3646void QgsVectorLayer::setFieldAlias( int attIndex, const QString &aliasString )
3647{
3649
3650 if ( attIndex < 0 || attIndex >= fields().count() )
3651 return;
3652
3653 QString name = fields().at( attIndex ).name();
3654
3655 mAttributeAliasMap.insert( name, aliasString );
3656 mFields[ attIndex ].setAlias( aliasString );
3657 mEditFormConfig.setFields( mFields );
3658 emit layerModified(); // TODO[MD]: should have a different signal?
3659}
3660
3661QString QgsVectorLayer::attributeAlias( int index ) const
3662{
3664
3665 if ( index < 0 || index >= fields().count() )
3666 return QString();
3667
3668 return fields().at( index ).alias();
3669}
3670
3672{
3674
3675 if ( index >= 0 && index < mFields.count() )
3676 return mFields.at( index ).displayName();
3677 else
3678 return QString();
3679}
3680
3682{
3684
3685 return mAttributeAliasMap;
3686}
3687
3689{
3691
3692 if ( index < 0 || index >= fields().count() )
3693 return;
3694
3695 const QString name = fields().at( index ).name();
3696
3697 mAttributeSplitPolicy.insert( name, policy );
3698 mFields[ index ].setSplitPolicy( policy );
3699 mEditFormConfig.setFields( mFields );
3700 emit layerModified(); // TODO[MD]: should have a different signal?
3701}
3702
3704{
3706
3707 if ( index < 0 || index >= fields().count() )
3708 return;
3709
3710 const QString name = fields().at( index ).name();
3711
3712 mAttributeDuplicatePolicy.insert( name, policy );
3713 mFields[ index ].setDuplicatePolicy( policy );
3714 mEditFormConfig.setFields( mFields );
3715 emit layerModified(); // TODO[MD]: should have a different signal?
3716}
3717
3719{
3721
3722 if ( index < 0 || index >= fields().count() )
3723 return;
3724
3725 const QString name = fields().at( index ).name();
3726
3727 mAttributeMergePolicy.insert( name, policy );
3728 mFields[ index ].setMergePolicy( policy );
3729 mEditFormConfig.setFields( mFields );
3730 emit layerModified(); // TODO[MD]: should have a different signal?
3731}
3732
3734{
3736
3737 QSet<QString> excludeList;
3738 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
3739 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
3740 {
3741 if ( flagsIt->testFlag( Qgis::FieldConfigurationFlag::HideFromWms ) )
3742 {
3743 excludeList << flagsIt.key();
3744 }
3745 }
3746 return excludeList;
3747}
3748
3749void QgsVectorLayer::setExcludeAttributesWms( const QSet<QString> &att )
3750{
3752
3753 QMap< QString, Qgis::FieldConfigurationFlags >::iterator flagsIt = mFieldConfigurationFlags.begin();
3754 for ( ; flagsIt != mFieldConfigurationFlags.end(); ++flagsIt )
3755 {
3756 flagsIt->setFlag( Qgis::FieldConfigurationFlag::HideFromWms, att.contains( flagsIt.key() ) );
3757 }
3758 updateFields();
3759}
3760
3762{
3764
3765 QSet<QString> excludeList;
3766 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
3767 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
3768 {
3769 if ( flagsIt->testFlag( Qgis::FieldConfigurationFlag::HideFromWfs ) )
3770 {
3771 excludeList << flagsIt.key();
3772 }
3773 }
3774 return excludeList;
3775}
3776
3777void QgsVectorLayer::setExcludeAttributesWfs( const QSet<QString> &att )
3778{
3780
3781 QMap< QString, Qgis::FieldConfigurationFlags >::iterator flagsIt = mFieldConfigurationFlags.begin();
3782 for ( ; flagsIt != mFieldConfigurationFlags.end(); ++flagsIt )
3783 {
3784 flagsIt->setFlag( Qgis::FieldConfigurationFlag::HideFromWfs, att.contains( flagsIt.key() ) );
3785 }
3786 updateFields();
3787}
3788
3790{
3792
3793 if ( index < 0 || index >= fields().count() )
3794 return false;
3795
3796 if ( mFields.fieldOrigin( index ) == Qgis::FieldOrigin::Expression )
3797 {
3798 removeExpressionField( index );
3799 return true;
3800 }
3801
3802 if ( !mEditBuffer || !mDataProvider )
3803 return false;
3804
3805 return mEditBuffer->deleteAttribute( index );
3806}
3807
3808bool QgsVectorLayer::deleteAttributes( const QList<int> &attrs )
3809{
3811
3812 bool deleted = false;
3813
3814 // Remove multiple occurrences of same attribute
3815 QList<int> attrList = qgis::setToList( qgis::listToSet( attrs ) );
3816
3817 std::sort( attrList.begin(), attrList.end(), std::greater<int>() );
3818
3819 for ( int attr : std::as_const( attrList ) )
3820 {
3821 if ( deleteAttribute( attr ) )
3822 {
3823 deleted = true;
3824 }
3825 }
3826
3827 return deleted;
3828}
3829
3830bool QgsVectorLayer::deleteFeatureCascade( QgsFeatureId fid, QgsVectorLayer::DeleteContext *context )
3831{
3833
3834 if ( !mEditBuffer )
3835 return false;
3836
3837 if ( context && context->cascade )
3838 {
3839 const QList<QgsRelation> relations = context->project->relationManager()->referencedRelations( this );
3840 const bool hasRelationsOrJoins = !relations.empty() || mJoinBuffer->containsJoins();
3841 if ( hasRelationsOrJoins )
3842 {
3843 if ( context->mHandledFeatures.contains( this ) )
3844 {
3845 QgsFeatureIds &handledFeatureIds = context->mHandledFeatures[ this ];
3846 if ( handledFeatureIds.contains( fid ) )
3847 {
3848 // avoid endless recursion
3849 return false;
3850 }
3851 else
3852 {
3853 // add feature id
3854 handledFeatureIds << fid;
3855 }
3856 }
3857 else
3858 {
3859 // add layer and feature id
3860 context->mHandledFeatures.insert( this, QgsFeatureIds() << fid );
3861 }
3862
3863 for ( const QgsRelation &relation : relations )
3864 {
3865 //check if composition (and not association)
3866 switch ( relation.strength() )
3867 {
3869 {
3870 //get features connected over this relation
3871 QgsFeatureIterator relatedFeaturesIt = relation.getRelatedFeatures( getFeature( fid ) );
3872 QgsFeatureIds childFeatureIds;
3873 QgsFeature childFeature;
3874 while ( relatedFeaturesIt.nextFeature( childFeature ) )
3875 {
3876 childFeatureIds.insert( childFeature.id() );
3877 }
3878 if ( childFeatureIds.count() > 0 )
3879 {
3880 relation.referencingLayer()->startEditing();
3881 relation.referencingLayer()->deleteFeatures( childFeatureIds, context );
3882 }
3883 break;
3884 }
3885
3887 break;
3888 }
3889 }
3890 }
3891 }
3892
3893 if ( mJoinBuffer->containsJoins() )
3894 mJoinBuffer->deleteFeature( fid, context );
3895
3896 bool res = mEditBuffer->deleteFeature( fid );
3897
3898 return res;
3899}
3900
3902{
3904
3905 if ( !mEditBuffer )
3906 return false;
3907
3908 return deleteFeatureCascade( fid, context );
3909}
3910
3912{
3914
3915 bool res = true;
3916
3917 if ( ( context && context->cascade ) || mJoinBuffer->containsJoins() )
3918 {
3919 // should ideally be "deleteFeaturesCascade" for performance!
3920 for ( QgsFeatureId fid : fids )
3921 res = deleteFeatureCascade( fid, context ) && res;
3922 }
3923 else
3924 {
3925 res = mEditBuffer && mEditBuffer->deleteFeatures( fids );
3926 }
3927
3928 if ( res )
3929 {
3930 mSelectedFeatureIds.subtract( fids ); // remove it from selection
3931 updateExtents();
3932 }
3933
3934 return res;
3935}
3936
3938{
3939 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
3941
3942 return mFields;
3943}
3944
3946{
3948
3949 QgsAttributeList pkAttributesList;
3950 if ( !mDataProvider )
3951 return pkAttributesList;
3952
3953 QgsAttributeList providerIndexes = mDataProvider->pkAttributeIndexes();
3954 for ( int i = 0; i < mFields.count(); ++i )
3955 {
3956 if ( mFields.fieldOrigin( i ) == Qgis::FieldOrigin::Provider &&
3957 providerIndexes.contains( mFields.fieldOriginIndex( i ) ) )
3958 pkAttributesList << i;
3959 }
3960
3961 return pkAttributesList;
3962}
3963
3965{
3967
3968 if ( !mDataProvider )
3969 return static_cast< long long >( Qgis::FeatureCountState::UnknownCount );
3970 return mDataProvider->featureCount() +
3971 ( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures().size() - mEditBuffer->deletedFeatureIds().size() : 0 );
3972}
3973
3975{
3977
3978 const QgsFeatureIds deletedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->deletedFeatureIds() : QgsFeatureIds() );
3979 const QgsFeatureMap addedFeatures( mEditBuffer && ! mDataProvider->transaction() ? mEditBuffer->addedFeatures() : QgsFeatureMap() );
3980
3981 if ( mEditBuffer && !deletedFeatures.empty() )
3982 {
3983 if ( addedFeatures.size() > deletedFeatures.size() )
3985 else
3987 }
3988
3989 if ( ( !mEditBuffer || addedFeatures.empty() ) && mDataProvider && mDataProvider->empty() )
3991 else
3993}
3994
3995bool QgsVectorLayer::commitChanges( bool stopEditing )
3996{
3998
3999 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
4000 return project()->commitChanges( mCommitErrors, stopEditing, this );
4001
4002 mCommitErrors.clear();
4003
4004 if ( !mDataProvider )
4005 {
4006 mCommitErrors << tr( "ERROR: no provider" );
4007 return false;
4008 }
4009
4010 if ( !mEditBuffer )
4011 {
4012 mCommitErrors << tr( "ERROR: layer not editable" );
4013 return false;
4014 }
4015
4016 emit beforeCommitChanges( stopEditing );
4017
4018 if ( !mAllowCommit )
4019 return false;
4020
4021 mCommitChangesActive = true;
4022
4023 bool success = false;
4024 if ( mEditBuffer->editBufferGroup() )
4025 success = mEditBuffer->editBufferGroup()->commitChanges( mCommitErrors, stopEditing );
4026 else
4027 success = mEditBuffer->commitChanges( mCommitErrors );
4028
4029 mCommitChangesActive = false;
4030
4031 if ( !mDeletedFids.empty() )
4032 {
4033 emit featuresDeleted( mDeletedFids );
4034 mDeletedFids.clear();
4035 }
4036
4037 if ( success )
4038 {
4039 if ( stopEditing )
4040 {
4041 clearEditBuffer();
4042 }
4043 undoStack()->clear();
4044 emit afterCommitChanges();
4045 if ( stopEditing )
4046 emit editingStopped();
4047 }
4048 else
4049 {
4050 QgsMessageLog::logMessage( tr( "Commit errors:\n %1" ).arg( mCommitErrors.join( QLatin1String( "\n " ) ) ) );
4051 }
4052
4053 updateFields();
4054
4055 mDataProvider->updateExtents();
4056
4057 if ( stopEditing )
4058 {
4059 mDataProvider->leaveUpdateMode();
4060 }
4061
4062 // This second call is required because OGR provider with JSON
4063 // driver might have changed fields order after the call to
4064 // leaveUpdateMode
4065 if ( mFields.names() != mDataProvider->fields().names() )
4066 {
4067 updateFields();
4068 }
4069
4071
4072 return success;
4073}
4074
4076{
4078
4079 return mCommitErrors;
4080}
4081
4082bool QgsVectorLayer::rollBack( bool deleteBuffer )
4083{
4085
4086 if ( project() && project()->transactionMode() == Qgis::TransactionMode::BufferedGroups )
4087 return project()->rollBack( mCommitErrors, deleteBuffer, this );
4088
4089 if ( !mEditBuffer )
4090 {
4091 return false;
4092 }
4093
4094 if ( !mDataProvider )
4095 {
4096 mCommitErrors << tr( "ERROR: no provider" );
4097 return false;
4098 }
4099
4100 bool rollbackExtent = !mDataProvider->transaction() && ( !mEditBuffer->deletedFeatureIds().isEmpty() ||
4101 !mEditBuffer->addedFeatures().isEmpty() ||
4102 !mEditBuffer->changedGeometries().isEmpty() );
4103
4104 emit beforeRollBack();
4105
4106 mEditBuffer->rollBack();
4107
4108 emit afterRollBack();
4109
4110 if ( isModified() )
4111 {
4112 // new undo stack roll back method
4113 // old method of calling every undo could cause many canvas refreshes
4114 undoStack()->setIndex( 0 );
4115 }
4116
4117 updateFields();
4118
4119 if ( deleteBuffer )
4120 {
4121 delete mEditBuffer;
4122 mEditBuffer = nullptr;
4123 undoStack()->clear();
4124 }
4125 emit editingStopped();
4126
4127 if ( rollbackExtent )
4128 updateExtents();
4129
4130 mDataProvider->leaveUpdateMode();
4131
4133 return true;
4134}
4135
4137{
4139
4140 return mSelectedFeatureIds.size();
4141}
4142
4144{
4145 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
4147
4148 return mSelectedFeatureIds;
4149}
4150
4152{
4154
4155 QgsFeatureList features;
4156 features.reserve( mSelectedFeatureIds.count() );
4157 QgsFeature f;
4158
4160
4161 while ( it.nextFeature( f ) )
4162 {
4163 features.push_back( f );
4164 }
4165
4166 return features;
4167}
4168
4170{
4172
4173 if ( mSelectedFeatureIds.isEmpty() )
4174 return QgsFeatureIterator();
4175
4178
4179 if ( mSelectedFeatureIds.count() == 1 )
4180 request.setFilterFid( *mSelectedFeatureIds.constBegin() );
4181 else
4182 request.setFilterFids( mSelectedFeatureIds );
4183
4184 return getFeatures( request );
4185}
4186
4188{
4190
4191 if ( !mEditBuffer || !mDataProvider )
4192 return false;
4193
4194 if ( mGeometryOptions->isActive() )
4195 {
4196 for ( auto feature = features.begin(); feature != features.end(); ++feature )
4197 {
4198 QgsGeometry geom = feature->geometry();
4199 mGeometryOptions->apply( geom );
4200 feature->setGeometry( geom );
4201 }
4202 }
4203
4204 bool res = mEditBuffer->addFeatures( features );
4205 updateExtents();
4206
4207 if ( res && mJoinBuffer->containsJoins() )
4208 res = mJoinBuffer->addFeatures( features );
4209
4210 return res;
4211}
4212
4214{
4216
4217 // if layer is not spatial, it has not CRS!
4218 setCrs( ( isSpatial() && mDataProvider ) ? mDataProvider->crs() : QgsCoordinateReferenceSystem() );
4219}
4220
4222{
4224
4226 if ( exp.isField() )
4227 {
4228 return static_cast<const QgsExpressionNodeColumnRef *>( exp.rootNode() )->name();
4229 }
4230
4231 return QString();
4232}
4233
4234void QgsVectorLayer::setDisplayExpression( const QString &displayExpression )
4235{
4237
4238 if ( mDisplayExpression == displayExpression )
4239 return;
4240
4241 mDisplayExpression = displayExpression;
4243}
4244
4246{
4248
4249 if ( !mDisplayExpression.isEmpty() || mFields.isEmpty() )
4250 {
4251 return mDisplayExpression;
4252 }
4253 else
4254 {
4255 const QString candidateName = QgsVectorLayerUtils::guessFriendlyIdentifierField( mFields );
4256 if ( !candidateName.isEmpty() )
4257 {
4258 return QgsExpression::quotedColumnRef( candidateName );
4259 }
4260 else
4261 {
4262 return QString();
4263 }
4264 }
4265}
4266
4268{
4270
4271 // display expressions are used as a fallback when no explicit map tip template is set
4272 return mapTipsEnabled() && ( !mapTipTemplate().isEmpty() || !displayExpression().isEmpty() );
4273}
4274
4276{
4278
4279 return ( mEditBuffer && mDataProvider );
4280}
4281
4283{
4284 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
4286
4289}
4290
4291bool QgsVectorLayer::isReadOnly() const
4292{
4294
4295 return mDataSourceReadOnly || mReadOnly;
4296}
4297
4298bool QgsVectorLayer::setReadOnly( bool readonly )
4299{
4301
4302 // exit if the layer is in editing mode
4303 if ( readonly && mEditBuffer )
4304 return false;
4305
4306 // exit if the data source is in read-only mode
4307 if ( !readonly && mDataSourceReadOnly )
4308 return false;
4309
4310 mReadOnly = readonly;
4311 emit readOnlyChanged();
4312 return true;
4313}
4314
4316{
4318
4319 if ( ! mDataProvider )
4320 return false;
4321
4322 if ( mDataSourceReadOnly )
4323 return false;
4324
4325 return mDataProvider->capabilities() & QgsVectorDataProvider::EditingCapabilities && ! mReadOnly;
4326}
4327
4329{
4331
4332 emit beforeModifiedCheck();
4333 return mEditBuffer && mEditBuffer->isModified();
4334}
4335
4336bool QgsVectorLayer::isAuxiliaryField( int index, int &srcIndex ) const
4337{
4339
4340 bool auxiliaryField = false;
4341 srcIndex = -1;
4342
4343 if ( !auxiliaryLayer() )
4344 return auxiliaryField;
4345
4346 if ( index >= 0 && fields().fieldOrigin( index ) == Qgis::FieldOrigin::Join )
4347 {
4348 const QgsVectorLayerJoinInfo *info = mJoinBuffer->joinForFieldIndex( index, fields(), srcIndex );
4349
4350 if ( info && info->joinLayerId() == auxiliaryLayer()->id() )
4351 auxiliaryField = true;
4352 }
4353
4354 return auxiliaryField;
4355}
4356
4358{
4360
4361 // we must allow setting a renderer if our geometry type is unknown
4362 // as this allows the renderer to be correctly set even for layers
4363 // with broken sources
4364 // (note that we allow REMOVING the renderer for non-spatial layers,
4365 // e.g. to permit removing the renderer when the layer changes from
4366 // a spatial layer to a non-spatial one)
4367 if ( r && !isSpatial() && mWkbType != Qgis::WkbType::Unknown )
4368 return;
4369
4370 if ( r != mRenderer )
4371 {
4372 delete mRenderer;
4373 mRenderer = r;
4374 mSymbolFeatureCounted = false;
4375 mSymbolFeatureCountMap.clear();
4376 mSymbolFeatureIdMap.clear();
4377
4378 if ( mRenderer )
4379 {
4380 const double refreshRate = QgsSymbolLayerUtils::rendererFrameRate( mRenderer );
4381 if ( refreshRate <= 0 )
4382 {
4383 mRefreshRendererTimer->stop();
4384 mRefreshRendererTimer->setInterval( 0 );
4385 }
4386 else
4387 {
4388 mRefreshRendererTimer->setInterval( 1000 / refreshRate );
4389 mRefreshRendererTimer->start();
4390 }
4391 }
4392
4393 emit rendererChanged();
4395 }
4396}
4397
4399{
4401
4402 if ( generator )
4403 {
4404 mRendererGenerators << generator;
4405 }
4406}
4407
4409{
4411
4412 for ( int i = mRendererGenerators.count() - 1; i >= 0; --i )
4413 {
4414 if ( mRendererGenerators.at( i )->id() == id )
4415 {
4416 delete mRendererGenerators.at( i );
4417 mRendererGenerators.removeAt( i );
4418 }
4419 }
4420}
4421
4422QList<const QgsFeatureRendererGenerator *> QgsVectorLayer::featureRendererGenerators() const
4423{
4424 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
4426
4427 QList< const QgsFeatureRendererGenerator * > res;
4428 for ( const QgsFeatureRendererGenerator *generator : mRendererGenerators )
4429 res << generator;
4430 return res;
4431}
4432
4433void QgsVectorLayer::beginEditCommand( const QString &text )
4434{
4436
4437 if ( !mDataProvider )
4438 {
4439 return;
4440 }
4441 if ( mDataProvider->transaction() )
4442 {
4443 QString ignoredError;
4444 mDataProvider->transaction()->createSavepoint( ignoredError );
4445 }
4446 undoStack()->beginMacro( text );
4447 mEditCommandActive = true;
4448 emit editCommandStarted( text );
4449}
4450
4452{
4454
4455 if ( !mDataProvider )
4456 {
4457 return;
4458 }
4459 undoStack()->endMacro();
4460 mEditCommandActive = false;
4461 if ( !mDeletedFids.isEmpty() )
4462 {
4463 if ( selectedFeatureCount() > 0 )
4464 {
4465 mSelectedFeatureIds.subtract( mDeletedFids );
4466 }
4467 emit featuresDeleted( mDeletedFids );
4468 mDeletedFids.clear();
4469 }
4470 emit editCommandEnded();
4471}
4472
4474{
4476
4477 if ( !mDataProvider )
4478 {
4479 return;
4480 }
4481 undoStack()->endMacro();
4482 undoStack()->undo();
4483
4484 // it's not directly possible to pop the last command off the stack (the destroyed one)
4485 // and delete, so we add a dummy obsolete command to force this to occur.
4486 // Pushing the new command deletes the destroyed one, and since the new
4487 // command is obsolete it's automatically deleted by the undo stack.
4488 auto command = std::make_unique< QUndoCommand >();
4489 command->setObsolete( true );
4490 undoStack()->push( command.release() );
4491
4492 mEditCommandActive = false;
4493 mDeletedFids.clear();
4494 emit editCommandDestroyed();
4495}
4496
4498{
4500
4501 return mJoinBuffer->addJoin( joinInfo );
4502}
4503
4504bool QgsVectorLayer::removeJoin( const QString &joinLayerId )
4505{
4507
4508 return mJoinBuffer->removeJoin( joinLayerId );
4509}
4510
4511const QList< QgsVectorLayerJoinInfo > QgsVectorLayer::vectorJoins() const
4512{
4514
4515 return mJoinBuffer->vectorJoins();
4516}
4517
4518int QgsVectorLayer::addExpressionField( const QString &exp, const QgsField &fld )
4519{
4521
4522 emit beforeAddingExpressionField( fld.name() );
4523 mExpressionFieldBuffer->addExpression( exp, fld );
4524 updateFields();
4525 int idx = mFields.indexFromName( fld.name() );
4526 emit attributeAdded( idx );
4527 return idx;
4528}
4529
4531{
4533
4534 emit beforeRemovingExpressionField( index );
4535 int oi = mFields.fieldOriginIndex( index );
4536 mExpressionFieldBuffer->removeExpression( oi );
4537 updateFields();
4538 emit attributeDeleted( index );
4539}
4540
4541QString QgsVectorLayer::expressionField( int index ) const
4542{
4544
4545 if ( mFields.fieldOrigin( index ) != Qgis::FieldOrigin::Expression )
4546 return QString();
4547
4548 int oi = mFields.fieldOriginIndex( index );
4549 if ( oi < 0 || oi >= mExpressionFieldBuffer->expressions().size() )
4550 return QString();
4551
4552 return mExpressionFieldBuffer->expressions().at( oi ).cachedExpression.expression();
4553}
4554
4555void QgsVectorLayer::updateExpressionField( int index, const QString &exp )
4556{
4558
4559 int oi = mFields.fieldOriginIndex( index );
4560 mExpressionFieldBuffer->updateExpression( oi, exp );
4561}
4562
4564{
4565 // non fatal for now -- the QgsVirtualLayerTask class is not thread safe and calls this
4567
4568 if ( !mDataProvider )
4569 return;
4570
4571 QgsFields oldFields = mFields;
4572
4573 mFields = mDataProvider->fields();
4574
4575 // added / removed fields
4576 if ( mEditBuffer )
4577 mEditBuffer->updateFields( mFields );
4578
4579 // joined fields
4580 if ( mJoinBuffer->containsJoins() )
4581 mJoinBuffer->updateFields( mFields );
4582
4583 if ( mExpressionFieldBuffer )
4584 mExpressionFieldBuffer->updateFields( mFields );
4585
4586 // set aliases and default values
4587 for ( auto aliasIt = mAttributeAliasMap.constBegin(); aliasIt != mAttributeAliasMap.constEnd(); ++aliasIt )
4588 {
4589 int index = mFields.lookupField( aliasIt.key() );
4590 if ( index < 0 )
4591 continue;
4592
4593 mFields[ index ].setAlias( aliasIt.value() );
4594 }
4595
4596 for ( auto splitPolicyIt = mAttributeSplitPolicy.constBegin(); splitPolicyIt != mAttributeSplitPolicy.constEnd(); ++splitPolicyIt )
4597 {
4598 int index = mFields.lookupField( splitPolicyIt.key() );
4599 if ( index < 0 )
4600 continue;
4601
4602 mFields[ index ].setSplitPolicy( splitPolicyIt.value() );
4603 }
4604
4605 for ( auto duplicatePolicyIt = mAttributeDuplicatePolicy.constBegin(); duplicatePolicyIt != mAttributeDuplicatePolicy.constEnd(); ++duplicatePolicyIt )
4606 {
4607 int index = mFields.lookupField( duplicatePolicyIt.key() );
4608 if ( index < 0 )
4609 continue;
4610
4611 mFields[ index ].setDuplicatePolicy( duplicatePolicyIt.value() );
4612 }
4613
4614 for ( auto mergePolicyIt = mAttributeMergePolicy.constBegin(); mergePolicyIt != mAttributeMergePolicy.constEnd(); ++mergePolicyIt )
4615 {
4616 int index = mFields.lookupField( mergePolicyIt.key() );
4617 if ( index < 0 )
4618 continue;
4619
4620 mFields[ index ].setMergePolicy( mergePolicyIt.value() );
4621 }
4622
4623 // Update configuration flags
4624 QMap< QString, Qgis::FieldConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
4625 for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
4626 {
4627 int index = mFields.lookupField( flagsIt.key() );
4628 if ( index < 0 )
4629 continue;
4630
4631 mFields[index].setConfigurationFlags( flagsIt.value() );
4632 }
4633
4634 // Update default values
4635 mDefaultValueOnUpdateFields.clear();
4636 QMap< QString, QgsDefaultValue >::const_iterator defaultIt = mDefaultExpressionMap.constBegin();
4637 for ( ; defaultIt != mDefaultExpressionMap.constEnd(); ++defaultIt )
4638 {
4639 int index = mFields.lookupField( defaultIt.key() );
4640 if ( index < 0 )
4641 continue;
4642
4643 mFields[ index ].setDefaultValueDefinition( defaultIt.value() );
4644 if ( defaultIt.value().applyOnUpdate() )
4645 mDefaultValueOnUpdateFields.insert( index );
4646 }
4647
4648 QMap< QString, QgsFieldConstraints::Constraints >::const_iterator constraintIt = mFieldConstraints.constBegin();
4649 for ( ; constraintIt != mFieldConstraints.constEnd(); ++constraintIt )
4650 {
4651 int index = mFields.lookupField( constraintIt.key() );
4652 if ( index < 0 )
4653 continue;
4654
4655 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4656
4657 // always keep provider constraints intact
4658 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintNotNull ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintNotNull ) )
4660 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintUnique ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintUnique ) )
4662 if ( !( constraints.constraints() & QgsFieldConstraints::ConstraintExpression ) && ( constraintIt.value() & QgsFieldConstraints::ConstraintExpression ) )
4664 mFields[ index ].setConstraints( constraints );
4665 }
4666
4667 QMap< QString, QPair< QString, QString > >::const_iterator constraintExpIt = mFieldConstraintExpressions.constBegin();
4668 for ( ; constraintExpIt != mFieldConstraintExpressions.constEnd(); ++constraintExpIt )
4669 {
4670 int index = mFields.lookupField( constraintExpIt.key() );
4671 if ( index < 0 )
4672 continue;
4673
4674 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4675
4676 // always keep provider constraints intact
4678 continue;
4679
4680 constraints.setConstraintExpression( constraintExpIt.value().first, constraintExpIt.value().second );
4681 mFields[ index ].setConstraints( constraints );
4682 }
4683
4684 QMap< QPair< QString, QgsFieldConstraints::Constraint >, QgsFieldConstraints::ConstraintStrength >::const_iterator constraintStrengthIt = mFieldConstraintStrength.constBegin();
4685 for ( ; constraintStrengthIt != mFieldConstraintStrength.constEnd(); ++constraintStrengthIt )
4686 {
4687 int index = mFields.lookupField( constraintStrengthIt.key().first );
4688 if ( index < 0 )
4689 continue;
4690
4691 QgsFieldConstraints constraints = mFields.at( index ).constraints();
4692
4693 // always keep provider constraints intact
4695 continue;
4696
4697 constraints.setConstraintStrength( constraintStrengthIt.key().second, constraintStrengthIt.value() );
4698 mFields[ index ].setConstraints( constraints );
4699 }
4700
4701 auto fieldWidgetIterator = mFieldWidgetSetups.constBegin();
4702 for ( ; fieldWidgetIterator != mFieldWidgetSetups.constEnd(); ++ fieldWidgetIterator )
4703 {
4704 int index = mFields.indexOf( fieldWidgetIterator.key() );
4705 if ( index < 0 )
4706 continue;
4707
4708 mFields[index].setEditorWidgetSetup( fieldWidgetIterator.value() );
4709 }
4710
4711 if ( oldFields != mFields )
4712 {
4713 emit updatedFields();
4714 mEditFormConfig.setFields( mFields );
4715 }
4716
4717}
4718
4719QVariant QgsVectorLayer::defaultValue( int index, const QgsFeature &feature, QgsExpressionContext *context ) const
4720{
4722
4723 if ( index < 0 || index >= mFields.count() || !mDataProvider )
4724 return QVariant();
4725
4726 QString expression = mFields.at( index ).defaultValueDefinition().expression();
4727 if ( expression.isEmpty() )
4728 return mDataProvider->defaultValue( index );
4729
4730 QgsExpressionContext *evalContext = context;
4731 std::unique_ptr< QgsExpressionContext > tempContext;
4732 if ( !evalContext )
4733 {
4734 // no context passed, so we create a default one
4736 evalContext = tempContext.get();
4737 }
4738
4739 if ( feature.isValid() )
4740 {
4742 featScope->setFeature( feature );
4743 featScope->setFields( feature.fields() );
4744 evalContext->appendScope( featScope );
4745 }
4746
4747 QVariant val;
4748 QgsExpression exp( expression );
4749 exp.prepare( evalContext );
4750 if ( exp.hasEvalError() )
4751 {
4752 QgsLogger::warning( "Error evaluating default value: " + exp.evalErrorString() );
4753 }
4754 else
4755 {
4756 val = exp.evaluate( evalContext );
4757 }
4758
4759 if ( feature.isValid() )
4760 {
4761 delete evalContext->popScope();
4762 }
4763
4764 return val;
4765}
4766
4768{
4770
4771 if ( index < 0 || index >= mFields.count() )
4772 return;
4773
4774 if ( definition.isValid() )
4775 {
4776 mDefaultExpressionMap.insert( mFields.at( index ).name(), definition );
4777 }
4778 else
4779 {
4780 mDefaultExpressionMap.remove( mFields.at( index ).name() );
4781 }
4782 updateFields();
4783}
4784
4786{
4788
4789 if ( index < 0 || index >= mFields.count() )
4790 return QgsDefaultValue();
4791 else
4792 return mFields.at( index ).defaultValueDefinition();
4793}
4794
4795QSet<QVariant> QgsVectorLayer::uniqueValues( int index, int limit ) const
4796{
4798
4799 QSet<QVariant> uniqueValues;
4800 if ( !mDataProvider )
4801 {
4802 return uniqueValues;
4803 }
4804
4805 Qgis::FieldOrigin origin = mFields.fieldOrigin( index );
4806 switch ( origin )
4807 {
4809 return uniqueValues;
4810
4811 case Qgis::FieldOrigin::Provider: //a provider field
4812 {
4813 uniqueValues = mDataProvider->uniqueValues( index, limit );
4814
4815 if ( mEditBuffer && ! mDataProvider->transaction() )
4816 {
4817 QSet<QString> vals;
4818 const auto constUniqueValues = uniqueValues;
4819 for ( const QVariant &v : constUniqueValues )
4820 {
4821 vals << v.toString();
4822 }
4823
4824 QgsFeatureMap added = mEditBuffer->addedFeatures();
4825 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
4826 while ( addedIt.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
4827 {
4828 addedIt.next();
4829 QVariant v = addedIt.value().attribute( index );
4830 if ( v.isValid() )
4831 {
4832 QString vs = v.toString();
4833 if ( !vals.contains( vs ) )
4834 {
4835 vals << vs;
4836 uniqueValues << v;
4837 }
4838 }
4839 }
4840
4841 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
4842 while ( it.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
4843 {
4844 it.next();
4845 QVariant v = it.value().value( index );
4846 if ( v.isValid() )
4847 {
4848 QString vs = v.toString();
4849 if ( !vals.contains( vs ) )
4850 {
4851 vals << vs;
4852 uniqueValues << v;
4853 }
4854 }
4855 }
4856 }
4857
4858 return uniqueValues;
4859 }
4860
4862 // the layer is editable, but in certain cases it can still be avoided going through all features
4863 if ( mDataProvider->transaction() || (
4864 mEditBuffer->deletedFeatureIds().isEmpty() &&
4865 mEditBuffer->addedFeatures().isEmpty() &&
4866 !mEditBuffer->deletedAttributeIds().contains( index ) &&
4867 mEditBuffer->changedAttributeValues().isEmpty() ) )
4868 {
4869 uniqueValues = mDataProvider->uniqueValues( index, limit );
4870 return uniqueValues;
4871 }
4872 [[fallthrough]];
4873 //we need to go through each feature
4876 {
4877 QgsAttributeList attList;
4878 attList << index;
4879
4882 .setSubsetOfAttributes( attList ) );
4883
4884 QgsFeature f;
4885 QVariant currentValue;
4886 QHash<QString, QVariant> val;
4887 while ( fit.nextFeature( f ) )
4888 {
4889 currentValue = f.attribute( index );
4890 val.insert( currentValue.toString(), currentValue );
4891 if ( limit >= 0 && val.size() >= limit )
4892 {
4893 break;
4894 }
4895 }
4896
4897 return qgis::listToSet( val.values() );
4898 }
4899 }
4900
4901 Q_ASSERT_X( false, "QgsVectorLayer::uniqueValues()", "Unknown source of the field!" );
4902 return uniqueValues;
4903}
4904
4905QStringList QgsVectorLayer::uniqueStringsMatching( int index, const QString &substring, int limit, QgsFeedback *feedback ) const
4906{
4908
4909 QStringList results;
4910 if ( !mDataProvider )
4911 {
4912 return results;
4913 }
4914
4915 Qgis::FieldOrigin origin = mFields.fieldOrigin( index );
4916 switch ( origin )
4917 {
4919 return results;
4920
4921 case Qgis::FieldOrigin::Provider: //a provider field
4922 {
4923 results = mDataProvider->uniqueStringsMatching( index, substring, limit, feedback );
4924
4925 if ( mEditBuffer && ! mDataProvider->transaction() )
4926 {
4927 QgsFeatureMap added = mEditBuffer->addedFeatures();
4928 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
4929 while ( addedIt.hasNext() && ( limit < 0 || results.count() < limit ) && ( !feedback || !feedback->isCanceled() ) )
4930 {
4931 addedIt.next();
4932 QVariant v = addedIt.value().attribute( index );
4933 if ( v.isValid() )
4934 {
4935 QString vs = v.toString();
4936 if ( vs.contains( substring, Qt::CaseInsensitive ) && !results.contains( vs ) )
4937 {
4938 results << vs;
4939 }
4940 }
4941 }
4942
4943 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
4944 while ( it.hasNext() && ( limit < 0 || results.count() < limit ) && ( !feedback || !feedback->isCanceled() ) )
4945 {
4946 it.next();
4947 QVariant v = it.value().value( index );
4948 if ( v.isValid() )
4949 {
4950 QString vs = v.toString();
4951 if ( vs.contains( substring, Qt::CaseInsensitive ) && !results.contains( vs ) )
4952 {
4953 results << vs;
4954 }
4955 }
4956 }
4957 }
4958
4959 return results;
4960 }
4961
4963 // the layer is editable, but in certain cases it can still be avoided going through all features
4964 if ( mDataProvider->transaction() || ( mEditBuffer->deletedFeatureIds().isEmpty() &&
4965 mEditBuffer->addedFeatures().isEmpty() &&
4966 !mEditBuffer->deletedAttributeIds().contains( index ) &&
4967 mEditBuffer->changedAttributeValues().isEmpty() ) )
4968 {
4969 return mDataProvider->uniqueStringsMatching( index, substring, limit, feedback );
4970 }
4971 [[fallthrough]];
4972 //we need to go through each feature
4975 {
4976 QgsAttributeList attList;
4977 attList << index;
4978
4979 QgsFeatureRequest request;
4980 request.setSubsetOfAttributes( attList );
4982 QString fieldName = mFields.at( index ).name();
4983 request.setFilterExpression( QStringLiteral( "\"%1\" ILIKE '%%2%'" ).arg( fieldName, substring ) );
4984 QgsFeatureIterator fit = getFeatures( request );
4985
4986 QgsFeature f;
4987 QString currentValue;
4988 while ( fit.nextFeature( f ) )
4989 {
4990 currentValue = f.attribute( index ).toString();
4991 if ( !results.contains( currentValue ) )
4992 results << currentValue;
4993
4994 if ( ( limit >= 0 && results.size() >= limit ) || ( feedback && feedback->isCanceled() ) )
4995 {
4996 break;
4997 }
4998 }
4999
5000 return results;
5001 }
5002 }
5003
5004 Q_ASSERT_X( false, "QgsVectorLayer::uniqueStringsMatching()", "Unknown source of the field!" );
5005 return results;
5006}
5007
5008QVariant QgsVectorLayer::minimumValue( int index ) const
5009{
5011
5012 QVariant minimum;
5013 minimumOrMaximumValue( index, &minimum, nullptr );
5014 return minimum;
5015}
5016
5017QVariant QgsVectorLayer::maximumValue( int index ) const
5018{
5020
5021 QVariant maximum;
5022 minimumOrMaximumValue( index, nullptr, &maximum );
5023 return maximum;
5024}
5025
5026void QgsVectorLayer::minimumAndMaximumValue( int index, QVariant &minimum, QVariant &maximum ) const
5027{
5029
5030 minimumOrMaximumValue( index, &minimum, &maximum );
5031}
5032
5033void QgsVectorLayer::minimumOrMaximumValue( int index, QVariant *minimum, QVariant *maximum ) const
5034{
5036
5037 if ( minimum )
5038 *minimum = QVariant();
5039 if ( maximum )
5040 *maximum = QVariant();
5041
5042 if ( !mDataProvider )
5043 {
5044 return;
5045 }
5046
5047 Qgis::FieldOrigin origin = mFields.fieldOrigin( index );
5048
5049 switch ( origin )
5050 {
5052 {
5053 return;
5054 }
5055
5056 case Qgis::FieldOrigin::Provider: //a provider field
5057 {
5058 if ( minimum )
5059 *minimum = mDataProvider->minimumValue( index );
5060 if ( maximum )
5061 *maximum = mDataProvider->maximumValue( index );
5062 if ( mEditBuffer && ! mDataProvider->transaction() )
5063 {
5064 const QgsFeatureMap added = mEditBuffer->addedFeatures();
5065 QMapIterator< QgsFeatureId, QgsFeature > addedIt( added );
5066 while ( addedIt.hasNext() )
5067 {
5068 addedIt.next();
5069 const QVariant v = addedIt.value().attribute( index );
5070 if ( minimum && v.isValid() && qgsVariantLessThan( v, *minimum ) )
5071 *minimum = v;
5072 if ( maximum && v.isValid() && qgsVariantGreaterThan( v, *maximum ) )
5073 *maximum = v;
5074 }
5075
5076 QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
5077 while ( it.hasNext() )
5078 {
5079 it.next();
5080 const QVariant v = it.value().value( index );
5081 if ( minimum && v.isValid() && qgsVariantLessThan( v, *minimum ) )
5082 *minimum = v;
5083 if ( maximum && v.isValid() && qgsVariantGreaterThan( v, *maximum ) )
5084 *maximum = v;
5085 }
5086 }
5087 return;
5088 }
5089
5091 {
5092 // the layer is editable, but in certain cases it can still be avoided going through all features
5093 if ( mDataProvider->transaction() || ( mEditBuffer->deletedFeatureIds().isEmpty() &&
5094 mEditBuffer->addedFeatures().isEmpty() &&
5095 !mEditBuffer->deletedAttributeIds().contains( index ) &&
5096 mEditBuffer->changedAttributeValues().isEmpty() ) )
5097 {
5098 if ( minimum )
5099 *minimum = mDataProvider->minimumValue( index );
5100 if ( maximum )
5101 *maximum = mDataProvider->maximumValue( index );
5102 return;
5103 }
5104 }
5105 [[fallthrough]];
5106 // no choice but to go through all features
5109 {
5110 // we need to go through each feature
5111 QgsAttributeList attList;
5112 attList << index;
5113
5116 .setSubsetOfAttributes( attList ) );
5117
5118 QgsFeature f;
5119 bool firstValue = true;
5120 while ( fit.nextFeature( f ) )
5121 {
5122 const QVariant currentValue = f.attribute( index );
5123 if ( QgsVariantUtils::isNull( currentValue ) )
5124 continue;
5125
5126 if ( firstValue )
5127 {
5128 if ( minimum )
5129 *minimum = currentValue;
5130 if ( maximum )
5131 *maximum = currentValue;
5132 firstValue = false;
5133 }
5134 else
5135 {
5136 if ( minimum && currentValue.isValid() && qgsVariantLessThan( currentValue, *minimum ) )
5137 *minimum = currentValue;
5138 if ( maximum && currentValue.isValid() && qgsVariantGreaterThan( currentValue, *maximum ) )
5139 *maximum = currentValue;
5140 }
5141 }
5142 return;
5143 }
5144 }
5145
5146 Q_ASSERT_X( false, "QgsVectorLayer::minimumOrMaximumValue()", "Unknown source of the field!" );
5147}
5148
5149void QgsVectorLayer::createEditBuffer()
5150{
5152
5153 if ( mEditBuffer )
5154 clearEditBuffer();
5155
5156 if ( mDataProvider->transaction() )
5157 {
5158 mEditBuffer = new QgsVectorLayerEditPassthrough( this );
5159
5160 connect( mDataProvider->transaction(), &QgsTransaction::dirtied, this, &QgsVectorLayer::onDirtyTransaction, Qt::UniqueConnection );
5161 }
5162 else
5163 {
5164 mEditBuffer = new QgsVectorLayerEditBuffer( this );
5165 }
5166 // forward signals
5167 connect( mEditBuffer, &QgsVectorLayerEditBuffer::layerModified, this, &QgsVectorLayer::invalidateSymbolCountedFlag );
5168 connect( mEditBuffer, &QgsVectorLayerEditBuffer::layerModified, this, &QgsVectorLayer::layerModified ); // TODO[MD]: necessary?
5169 //connect( mEditBuffer, SIGNAL( layerModified() ), this, SLOT( triggerRepaint() ) ); // TODO[MD]: works well?
5170 connect( mEditBuffer, &QgsVectorLayerEditBuffer::featureAdded, this, &QgsVectorLayer::onFeatureAdded );
5171 connect( mEditBuffer, &QgsVectorLayerEditBuffer::featureDeleted, this, &QgsVectorLayer::onFeatureDeleted );
5182
5183}
5184
5185void QgsVectorLayer::clearEditBuffer()
5186{
5188
5189 delete mEditBuffer;
5190 mEditBuffer = nullptr;
5191}
5192
5193QVariant QgsVectorLayer::aggregate( Qgis::Aggregate aggregate, const QString &fieldOrExpression,
5195 bool *ok, QgsFeatureIds *fids, QgsFeedback *feedback, QString *error ) const
5196{
5197 // non fatal for now -- the aggregate expression functions are not thread safe and call this
5199
5200 if ( ok )
5201 *ok = false;
5202 if ( error )
5203 error->clear();
5204
5205 if ( !mDataProvider )
5206 {
5207 if ( error )
5208 *error = tr( "Layer is invalid" );
5209 return QVariant();
5210 }
5211
5212 // test if we are calculating based on a field
5213 const int attrIndex = QgsExpression::expressionToLayerFieldIndex( fieldOrExpression, this );
5214 if ( attrIndex >= 0 )
5215 {
5216 // aggregate is based on a field - if it's a provider field, we could possibly hand over the calculation
5217 // to the provider itself
5218 Qgis::FieldOrigin origin = mFields.fieldOrigin( attrIndex );
5219 if ( origin == Qgis::FieldOrigin::Provider )
5220 {
5221 bool providerOk = false;
5222 QVariant val = mDataProvider->aggregate( aggregate, attrIndex, parameters, context, providerOk, fids );
5223 if ( providerOk )
5224 {
5225 // provider handled calculation
5226 if ( ok )
5227 *ok = true;
5228 return val;
5229 }
5230 }
5231 }
5232
5233 // fallback to using aggregate calculator to determine aggregate
5234 QgsAggregateCalculator c( this );
5235 if ( fids )
5236 c.setFidsFilter( *fids );
5237 c.setParameters( parameters );
5238 bool aggregateOk = false;
5239 const QVariant result = c.calculate( aggregate, fieldOrExpression, context, &aggregateOk, feedback );
5240 if ( ok )
5241 *ok = aggregateOk;
5242 if ( !aggregateOk && error )
5243 *error = c.lastError();
5244
5245 return result;
5246}
5247
5248void QgsVectorLayer::setFeatureBlendMode( QPainter::CompositionMode featureBlendMode )
5249{
5251
5252 if ( mFeatureBlendMode == featureBlendMode )
5253 return;
5254
5255 mFeatureBlendMode = featureBlendMode;
5258}
5259
5260QPainter::CompositionMode QgsVectorLayer::featureBlendMode() const
5261{
5262 // non fatal for now -- the "rasterize" processing algorithm is not thread safe and calls this
5264
5265 return mFeatureBlendMode;
5266}
5267
5268void QgsVectorLayer::readSldLabeling( const QDomNode &node )
5269{
5271
5272 setLabeling( nullptr ); // start with no labeling
5273 setLabelsEnabled( false );
5274
5275 QDomElement element = node.toElement();
5276 if ( element.isNull() )
5277 return;
5278
5279 QDomElement userStyleElem = element.firstChildElement( QStringLiteral( "UserStyle" ) );
5280 if ( userStyleElem.isNull() )
5281 {
5282 QgsDebugMsgLevel( QStringLiteral( "Info: UserStyle element not found." ), 4 );
5283 return;
5284 }
5285
5286 QDomElement featTypeStyleElem = userStyleElem.firstChildElement( QStringLiteral( "FeatureTypeStyle" ) );
5287 if ( featTypeStyleElem.isNull() )
5288 {
5289 QgsDebugMsgLevel( QStringLiteral( "Info: FeatureTypeStyle element not found." ), 4 );
5290 return;
5291 }
5292
5293 // create empty FeatureTypeStyle element to merge TextSymbolizer's Rule's from all FeatureTypeStyle's
5294 QDomElement mergedFeatTypeStyle = featTypeStyleElem.cloneNode( false ).toElement();
5295
5296 // use the RuleRenderer when more rules are present or the rule
5297 // has filters or min/max scale denominators set,
5298 // otherwise use the Simple labeling
5299 bool needRuleBasedLabeling = false;
5300 int ruleCount = 0;
5301
5302 while ( !featTypeStyleElem.isNull() )
5303 {
5304 QDomElement ruleElem = featTypeStyleElem.firstChildElement( QStringLiteral( "Rule" ) );
5305 while ( !ruleElem.isNull() )
5306 {
5307 // test rule children element to check if we need to create RuleRenderer
5308 // and if the rule has a symbolizer
5309 bool hasTextSymbolizer = false;
5310 bool hasRuleBased = false;
5311 QDomElement ruleChildElem = ruleElem.firstChildElement();
5312 while ( !ruleChildElem.isNull() )
5313 {
5314 // rule has filter or min/max scale denominator, use the RuleRenderer
5315 if ( ruleChildElem.localName() == QLatin1String( "Filter" ) ||
5316 ruleChildElem.localName() == QLatin1String( "MinScaleDenominator" ) ||
5317 ruleChildElem.localName() == QLatin1String( "MaxScaleDenominator" ) )
5318 {
5319 hasRuleBased = true;
5320 }
5321 // rule has a renderer symbolizer, not a text symbolizer
5322 else if ( ruleChildElem.localName() == QLatin1String( "TextSymbolizer" ) )
5323 {
5324 QgsDebugMsgLevel( QStringLiteral( "Info: TextSymbolizer element found" ), 4 );
5325 hasTextSymbolizer = true;
5326 }
5327
5328 ruleChildElem = ruleChildElem.nextSiblingElement();
5329 }
5330
5331 if ( hasTextSymbolizer )
5332 {
5333 ruleCount++;
5334
5335 // append a clone of all Rules to the merged FeatureTypeStyle element
5336 mergedFeatTypeStyle.appendChild( ruleElem.cloneNode().toElement() );
5337
5338 if ( hasRuleBased )
5339 {
5340 QgsDebugMsgLevel( QStringLiteral( "Info: Filter or Min/MaxScaleDenominator element found: need a RuleBasedLabeling" ), 4 );
5341 needRuleBasedLabeling = true;
5342 }
5343 }
5344
5345 // more rules present, use the RuleRenderer
5346 if ( ruleCount > 1 )
5347 {
5348 QgsDebugMsgLevel( QStringLiteral( "Info: More Rule elements found: need a RuleBasedLabeling" ), 4 );
5349 needRuleBasedLabeling = true;
5350 }
5351
5352 // not use the rule based labeling if no rules with textSymbolizer
5353 if ( ruleCount == 0 )
5354 {
5355 needRuleBasedLabeling = false;
5356 }
5357
5358 ruleElem = ruleElem.nextSiblingElement( QStringLiteral( "Rule" ) );
5359 }
5360 featTypeStyleElem = featTypeStyleElem.nextSiblingElement( QStringLiteral( "FeatureTypeStyle" ) );
5361 }
5362
5363 if ( ruleCount == 0 )
5364 {
5365 QgsDebugMsgLevel( QStringLiteral( "Info: No TextSymbolizer element." ), 4 );
5366 return;
5367 }
5368
5369 QDomElement ruleElem = mergedFeatTypeStyle.firstChildElement( QStringLiteral( "Rule" ) );
5370
5371 if ( needRuleBasedLabeling )
5372 {
5373 QgsDebugMsgLevel( QStringLiteral( "Info: rule based labeling" ), 4 );
5374 QgsRuleBasedLabeling::Rule *rootRule = new QgsRuleBasedLabeling::Rule( nullptr );
5375 while ( !ruleElem.isNull() )
5376 {
5377
5378 QString label, description, filterExp;
5379 int scaleMinDenom = 0, scaleMaxDenom = 0;
5380 QgsPalLayerSettings settings;
5381
5382 // retrieve the Rule element child nodes
5383 QDomElement childElem = ruleElem.firstChildElement();
5384 while ( !childElem.isNull() )
5385 {
5386 if ( childElem.localName() == QLatin1String( "Name" ) )
5387 {
5388 // <se:Name> tag contains the rule identifier,
5389 // so prefer title tag for the label property value
5390 if ( label.isEmpty() )
5391 label = childElem.firstChild().nodeValue();
5392 }
5393 else if ( childElem.localName() == QLatin1String( "Description" ) )
5394 {
5395 // <se:Description> can contains a title and an abstract
5396 QDomElement titleElem = childElem.firstChildElement( QStringLiteral( "Title" ) );
5397 if ( !titleElem.isNull() )
5398 {
5399 label = titleElem.firstChild().nodeValue();
5400 }
5401
5402 QDomElement abstractElem = childElem.firstChildElement( QStringLiteral( "Abstract" ) );
5403 if ( !abstractElem.isNull() )
5404 {
5405 description = abstractElem.firstChild().nodeValue();
5406 }
5407 }
5408 else if ( childElem.localName() == QLatin1String( "Abstract" ) )
5409 {
5410 // <sld:Abstract> (v1.0)
5411 description = childElem.firstChild().nodeValue();
5412 }
5413 else if ( childElem.localName() == QLatin1String( "Title" ) )
5414 {
5415 // <sld:Title> (v1.0)
5416 label = childElem.firstChild().nodeValue();
5417 }
5418 else if ( childElem.localName() == QLatin1String( "Filter" ) )
5419 {
5421 if ( filter )
5422 {
5423 if ( filter->hasParserError() )
5424 {
5425 QgsDebugMsgLevel( QStringLiteral( "SLD Filter parsing error: %1" ).arg( filter->parserErrorString() ), 3 );
5426 }
5427 else
5428 {
5429 filterExp = filter->expression();
5430 }
5431 delete filter;
5432 }
5433 }
5434 else if ( childElem.localName() == QLatin1String( "MinScaleDenominator" ) )
5435 {
5436 bool ok;
5437 int v = childElem.firstChild().nodeValue().toInt( &ok );
5438 if ( ok )
5439 scaleMinDenom = v;
5440 }
5441 else if ( childElem.localName() == QLatin1String( "MaxScaleDenominator" ) )
5442 {
5443 bool ok;
5444 int v = childElem.firstChild().nodeValue().toInt( &ok );
5445 if ( ok )
5446 scaleMaxDenom = v;
5447 }
5448 else if ( childElem.localName() == QLatin1String( "TextSymbolizer" ) )
5449 {
5450 readSldTextSymbolizer( childElem, settings );
5451 }
5452
5453 childElem = childElem.nextSiblingElement();
5454 }
5455
5456 QgsRuleBasedLabeling::Rule *ruleLabeling = new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings( settings ), scaleMinDenom, scaleMaxDenom, filterExp, label );
5457 rootRule->appendChild( ruleLabeling );
5458
5459 ruleElem = ruleElem.nextSiblingElement();
5460 }
5461
5462 setLabeling( new QgsRuleBasedLabeling( rootRule ) );
5463 setLabelsEnabled( true );
5464 }
5465 else
5466 {
5467 QgsDebugMsgLevel( QStringLiteral( "Info: simple labeling" ), 4 );
5468 // retrieve the TextSymbolizer element child node
5469 QDomElement textSymbolizerElem = ruleElem.firstChildElement( QStringLiteral( "TextSymbolizer" ) );
5471 if ( readSldTextSymbolizer( textSymbolizerElem, s ) )
5472 {
5474 setLabelsEnabled( true );
5475 }
5476 }
5477}
5478
5479bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSettings &settings ) const
5480{
5482
5483 if ( node.localName() != QLatin1String( "TextSymbolizer" ) )
5484 {
5485 QgsDebugMsgLevel( QStringLiteral( "Not a TextSymbolizer element: %1" ).arg( node.localName() ), 3 );
5486 return false;
5487 }
5488 QDomElement textSymbolizerElem = node.toElement();
5489 // Label
5490 QDomElement labelElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Label" ) );
5491 if ( !labelElem.isNull() )
5492 {
5493 QDomElement propertyNameElem = labelElem.firstChildElement( QStringLiteral( "PropertyName" ) );
5494 if ( !propertyNameElem.isNull() )
5495 {
5496 // set labeling defaults
5497
5498 // label attribute
5499 QString labelAttribute = propertyNameElem.text();
5500 settings.fieldName = labelAttribute;
5501 settings.isExpression = false;
5502
5503 int fieldIndex = mFields.lookupField( labelAttribute );
5504 if ( fieldIndex == -1 )
5505 {
5506 // label attribute is not in columns, check if it is an expression
5507 QgsExpression exp( labelAttribute );
5508 if ( !exp.hasEvalError() )
5509 {
5510 settings.isExpression = true;
5511 }
5512 else
5513 {
5514 QgsDebugMsgLevel( QStringLiteral( "SLD label attribute error: %1" ).arg( exp.evalErrorString() ), 3 );
5515 }
5516 }
5517 }
5518 else
5519 {
5520 QgsDebugMsgLevel( QStringLiteral( "Info: PropertyName element not found." ), 4 );
5521 return false;
5522 }
5523 }
5524 else
5525 {
5526 QgsDebugMsgLevel( QStringLiteral( "Info: Label element not found." ), 4 );
5527 return false;
5528 }
5529
5531 if ( textSymbolizerElem.hasAttribute( QStringLiteral( "uom" ) ) )
5532 {
5533 sldUnitSize = QgsSymbolLayerUtils::decodeSldUom( textSymbolizerElem.attribute( QStringLiteral( "uom" ) ) );
5534 }
5535
5536 QString fontFamily = QStringLiteral( "Sans-Serif" );
5537 int fontPointSize = 10;
5539 int fontWeight = -1;
5540 bool fontItalic = false;
5541 bool fontUnderline = false;
5542
5543 // Font
5544 QDomElement fontElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Font" ) );
5545 if ( !fontElem.isNull() )
5546 {
5547 QgsStringMap fontSvgParams = QgsSymbolLayerUtils::getSvgParameterList( fontElem );
5548 for ( QgsStringMap::iterator it = fontSvgParams.begin(); it != fontSvgParams.end(); ++it )
5549 {
5550 QgsDebugMsgLevel( QStringLiteral( "found fontSvgParams %1: %2" ).arg( it.key(), it.value() ), 4 );
5551
5552 if ( it.key() == QLatin1String( "font-family" ) )
5553 {
5554 fontFamily = it.value();
5555 }
5556 else if ( it.key() == QLatin1String( "font-style" ) )
5557 {
5558 fontItalic = ( it.value() == QLatin1String( "italic" ) ) || ( it.value() == QLatin1String( "Italic" ) );
5559 }
5560 else if ( it.key() == QLatin1String( "font-size" ) )
5561 {
5562 bool ok;
5563 int fontSize = it.value().toInt( &ok );
5564 if ( ok )
5565 {
5566 fontPointSize = fontSize;
5567 fontUnitSize = sldUnitSize;
5568 }
5569 }
5570 else if ( it.key() == QLatin1String( "font-weight" ) )
5571 {
5572 if ( ( it.value() == QLatin1String( "bold" ) ) || ( it.value() == QLatin1String( "Bold" ) ) )
5573 fontWeight = QFont::Bold;
5574 }
5575 else if ( it.key() == QLatin1String( "font-underline" ) )
5576 {
5577 fontUnderline = ( it.value() == QLatin1String( "underline" ) ) || ( it.value() == QLatin1String( "Underline" ) );
5578 }
5579 }
5580 }
5581
5582 QgsTextFormat format;
5583 QFont font( fontFamily, fontPointSize, fontWeight, fontItalic );
5584 font.setUnderline( fontUnderline );
5585 format.setFont( font );
5586 format.setSize( fontPointSize );
5587 format.setSizeUnit( fontUnitSize );
5588
5589 // Fill
5590 QDomElement fillElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Fill" ) );
5591 QColor textColor;
5592 Qt::BrushStyle textBrush = Qt::SolidPattern;
5593 QgsSymbolLayerUtils::fillFromSld( fillElem, textBrush, textColor );
5594 if ( textColor.isValid() )
5595 {
5596 QgsDebugMsgLevel( QStringLiteral( "Info: textColor %1." ).arg( QVariant( textColor ).toString() ), 4 );
5597 format.setColor( textColor );
5598 }
5599
5600 QgsTextBufferSettings bufferSettings;
5601
5602 // Halo
5603 QDomElement haloElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Halo" ) );
5604 if ( !haloElem.isNull() )
5605 {
5606 bufferSettings.setEnabled( true );
5607 bufferSettings.setSize( 1 );
5608
5609 QDomElement radiusElem = haloElem.firstChildElement( QStringLiteral( "Radius" ) );
5610 if ( !radiusElem.isNull() )
5611 {
5612 bool ok;
5613 double bufferSize = radiusElem.text().toDouble( &ok );
5614 if ( ok )
5615 {
5616 bufferSettings.setSize( bufferSize );
5617 bufferSettings.setSizeUnit( sldUnitSize );
5618 }
5619 }
5620
5621 QDomElement haloFillElem = haloElem.firstChildElement( QStringLiteral( "Fill" ) );
5622 QColor bufferColor;
5623 Qt::BrushStyle bufferBrush = Qt::SolidPattern;
5624 QgsSymbolLayerUtils::fillFromSld( haloFillElem, bufferBrush, bufferColor );
5625 if ( bufferColor.isValid() )
5626 {
5627 QgsDebugMsgLevel( QStringLiteral( "Info: bufferColor %1." ).arg( QVariant( bufferColor ).toString() ), 4 );
5628 bufferSettings.setColor( bufferColor );
5629 }
5630 }
5631
5632 // LabelPlacement
5633 QDomElement labelPlacementElem = textSymbolizerElem.firstChildElement( QStringLiteral( "LabelPlacement" ) );
5634 if ( !labelPlacementElem.isNull() )
5635 {
5636 // PointPlacement
5637 QDomElement pointPlacementElem = labelPlacementElem.firstChildElement( QStringLiteral( "PointPlacement" ) );
5638 if ( !pointPlacementElem.isNull() )
5639 {
5642 {
5644 }
5645
5646 QDomElement displacementElem = pointPlacementElem.firstChildElement( QStringLiteral( "Displacement" ) );
5647 if ( !displacementElem.isNull() )
5648 {
5649 QDomElement displacementXElem = displacementElem.firstChildElement( QStringLiteral( "DisplacementX" ) );
5650 if ( !displacementXElem.isNull() )
5651 {
5652 bool ok;
5653 double xOffset = displacementXElem.text().toDouble( &ok );
5654 if ( ok )
5655 {
5656 settings.xOffset = xOffset;
5657 settings.offsetUnits = sldUnitSize;
5658 }
5659 }
5660 QDomElement displacementYElem = displacementElem.firstChildElement( QStringLiteral( "DisplacementY" ) );
5661 if ( !displacementYElem.isNull() )
5662 {
5663 bool ok;
5664 double yOffset = displacementYElem.text().toDouble( &ok );
5665 if ( ok )
5666 {
5667 settings.yOffset = yOffset;
5668 settings.offsetUnits = sldUnitSize;
5669 }
5670 }
5671 }
5672 QDomElement anchorPointElem = pointPlacementElem.firstChildElement( QStringLiteral( "AnchorPoint" ) );
5673 if ( !anchorPointElem.isNull() )
5674 {
5675 QDomElement anchorPointXElem = anchorPointElem.firstChildElement( QStringLiteral( "AnchorPointX" ) );
5676 if ( !anchorPointXElem.isNull() )
5677 {
5678 bool ok;
5679 double xOffset = anchorPointXElem.text().toDouble( &ok );
5680 if ( ok )
5681 {
5682 settings.xOffset = xOffset;
5683 settings.offsetUnits = sldUnitSize;
5684 }
5685 }
5686 QDomElement anchorPointYElem = anchorPointElem.firstChildElement( QStringLiteral( "AnchorPointY" ) );
5687 if ( !anchorPointYElem.isNull() )
5688 {
5689 bool ok;
5690 double yOffset = anchorPointYElem.text().toDouble( &ok );
5691 if ( ok )
5692 {
5693 settings.yOffset = yOffset;
5694 settings.offsetUnits = sldUnitSize;
5695 }
5696 }
5697 }
5698
5699 QDomElement rotationElem = pointPlacementElem.firstChildElement( QStringLiteral( "Rotation" ) );
5700 if ( !rotationElem.isNull() )
5701 {
5702 bool ok;
5703 double rotation = rotationElem.text().toDouble( &ok );
5704 if ( ok )
5705 {
5706 settings.angleOffset = 360 - rotation;
5707 }
5708 }
5709 }
5710 else
5711 {
5712 // PointPlacement
5713 QDomElement linePlacementElem = labelPlacementElem.firstChildElement( QStringLiteral( "LinePlacement" ) );
5714 if ( !linePlacementElem.isNull() )
5715 {
5717 }
5718 }
5719 }
5720
5721 // read vendor options
5722 QgsStringMap vendorOptions;
5723 QDomElement vendorOptionElem = textSymbolizerElem.firstChildElement( QStringLiteral( "VendorOption" ) );
5724 while ( !vendorOptionElem.isNull() && vendorOptionElem.localName() == QLatin1String( "VendorOption" ) )
5725 {
5726 QString optionName = vendorOptionElem.attribute( QStringLiteral( "name" ) );
5727 QString optionValue;
5728 if ( vendorOptionElem.firstChild().nodeType() == QDomNode::TextNode )
5729 {
5730 optionValue = vendorOptionElem.firstChild().nodeValue();
5731 }
5732 else
5733 {
5734 if ( vendorOptionElem.firstChild().nodeType() == QDomNode::ElementNode &&
5735 vendorOptionElem.firstChild().localName() == QLatin1String( "Literal" ) )
5736 {
5737 QgsDebugMsgLevel( vendorOptionElem.firstChild().localName(), 2 );
5738 optionValue = vendorOptionElem.firstChild().firstChild().nodeValue();
5739 }
5740 else
5741 {
5742 QgsDebugError( QStringLiteral( "unexpected child of %1 named %2" ).arg( vendorOptionElem.localName(), optionName ) );
5743 }
5744 }
5745
5746 if ( !optionName.isEmpty() && !optionValue.isEmpty() )
5747 {
5748 vendorOptions[ optionName ] = optionValue;
5749 }
5750
5751 vendorOptionElem = vendorOptionElem.nextSiblingElement();
5752 }
5753 if ( !vendorOptions.isEmpty() )
5754 {
5755 for ( QgsStringMap::iterator it = vendorOptions.begin(); it != vendorOptions.end(); ++it )
5756 {
5757 if ( it.key() == QLatin1String( "underlineText" ) && it.value() == QLatin1String( "true" ) )
5758 {
5759 font.setUnderline( true );
5760 format.setFont( font );
5761 }
5762 else if ( it.key() == QLatin1String( "strikethroughText" ) && it.value() == QLatin1String( "true" ) )
5763 {
5764 font.setStrikeOut( true );
5765 format.setFont( font );
5766 }
5767 else if ( it.key() == QLatin1String( "maxDisplacement" ) )
5768 {
5770 }
5771 else if ( it.key() == QLatin1String( "followLine" ) && it.value() == QLatin1String( "true" ) )
5772 {
5774 {
5776 }
5777 else
5778 {
5780 }
5781 }
5782 else if ( it.key() == QLatin1String( "maxAngleDelta" ) )
5783 {
5784 bool ok;
5785 double angle = it.value().toDouble( &ok );
5786 if ( ok )
5787 {
5788 settings.maxCurvedCharAngleIn = angle;
5789 settings.maxCurvedCharAngleOut = angle;
5790 }
5791 }
5792 // miscellaneous options
5793 else if ( it.key() == QLatin1String( "conflictResolution" ) && it.value() == QLatin1String( "false" ) )
5794 {
5796 }
5797 else if ( it.key() == QLatin1String( "forceLeftToRight" ) && it.value() == QLatin1String( "false" ) )
5798 {
5800 }
5801 else if ( it.key() == QLatin1String( "group" ) && it.value() == QLatin1String( "yes" ) )
5802 {
5803 settings.lineSettings().setMergeLines( true );
5804 }
5805 else if ( it.key() == QLatin1String( "labelAllGroup" ) && it.value() == QLatin1String( "true" ) )
5806 {
5807 settings.lineSettings().setMergeLines( true );
5808 }
5809 }
5810 }
5811
5812 format.setBuffer( bufferSettings );
5813 settings.setFormat( format );
5814 return true;
5815}
5816
5818{
5820
5821 return mEditFormConfig;
5822}
5823
5825{
5827
5828 if ( mEditFormConfig == editFormConfig )
5829 return;
5830
5831 mEditFormConfig = editFormConfig;
5832 mEditFormConfig.onRelationsLoaded();
5833 emit editFormConfigChanged();
5834}
5835
5837{
5839
5840 QgsAttributeTableConfig config = mAttributeTableConfig;
5841
5842 if ( config.isEmpty() )
5843 config.update( fields() );
5844
5845 return config;
5846}
5847
5849{
5851
5852 if ( mAttributeTableConfig != attributeTableConfig )
5853 {
5854 mAttributeTableConfig = attributeTableConfig;
5855 emit configChanged();
5856 }
5857}
5858
5860{
5861 // called in a non-thread-safe way in some cases when calculating aggregates in a different thread
5863
5865}
5866
5873
5875{
5877
5878 if ( !mDiagramLayerSettings )
5879 mDiagramLayerSettings = new QgsDiagramLayerSettings();
5880 *mDiagramLayerSettings = s;
5881}
5882
5884{
5886
5887 QgsLayerMetadataFormatter htmlFormatter( metadata() );
5888 QString myMetadata = QStringLiteral( "<html><head></head>\n<body>\n" );
5889
5890 myMetadata += generalHtmlMetadata();
5891
5892 // Begin Provider section
5893 myMetadata += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" );
5894 myMetadata += QLatin1String( "<table class=\"list-view\">\n" );
5895
5896 // storage type
5897 if ( !storageType().isEmpty() )
5898 {
5899 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Storage" ) + QStringLiteral( "</td><td>" ) + storageType() + QStringLiteral( "</td></tr>\n" );
5900 }
5901
5902 // comment
5903 if ( !dataComment().isEmpty() )
5904 {
5905 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Comment" ) + QStringLiteral( "</td><td>" ) + dataComment() + QStringLiteral( "</td></tr>\n" );
5906 }
5907
5908 // encoding
5909 if ( const QgsVectorDataProvider *provider = dataProvider() )
5910 {
5911 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Encoding" ) + QStringLiteral( "</td><td>" ) + provider->encoding() + QStringLiteral( "</td></tr>\n" );
5912 myMetadata += provider->htmlMetadata();
5913 }
5914
5915 if ( isSpatial() )
5916 {
5917 // geom type
5919 if ( static_cast<int>( type ) < 0 || static_cast< int >( type ) > static_cast< int >( Qgis::GeometryType::Null ) )
5920 {
5921 QgsDebugMsgLevel( QStringLiteral( "Invalid vector type" ), 2 );
5922 }
5923 else
5924 {
5925 QString typeString( QStringLiteral( "%1 (%2)" ).arg( QgsWkbTypes::geometryDisplayString( geometryType() ),
5927 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Geometry" ) + QStringLiteral( "</td><td>" ) + typeString + QStringLiteral( "</td></tr>\n" );
5928 }
5929
5930 // Extent
5931 // Try to display extent 3D by default. If empty (probably because the data is 2D), fallback to the 2D version
5932 const QgsBox3D extentBox3D = extent3D();
5933 const QString extentAsStr = !extentBox3D.isEmpty() ? extentBox3D.toString() : extent().toString();
5934 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Extent" ) + QStringLiteral( "</td><td>" ) + extentAsStr + QStringLiteral( "</td></tr>\n" );
5935 }
5936
5937 // feature count
5938 QLocale locale = QLocale();
5939 locale.setNumberOptions( locale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
5940 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" )
5941 + tr( "Feature count" ) + QStringLiteral( "</td><td>" )
5942 + ( featureCount() == -1 ? tr( "unknown" ) : locale.toString( static_cast<qlonglong>( featureCount() ) ) )
5943 + QStringLiteral( "</td></tr>\n" );
5944
5945 // End Provider section
5946 myMetadata += QLatin1String( "</table>\n<br><br>" );
5947
5948 if ( isSpatial() )
5949 {
5950 // CRS
5951 myMetadata += crsHtmlMetadata();
5952 }
5953
5954 // identification section
5955 myMetadata += QStringLiteral( "<h1>" ) + tr( "Identification" ) + QStringLiteral( "</h1>\n<hr>\n" );
5956 myMetadata += htmlFormatter.identificationSectionHtml( );
5957 myMetadata += QLatin1String( "<br><br>\n" );
5958
5959 // extent section
5960 myMetadata += QStringLiteral( "<h1>" ) + tr( "Extent" ) + QStringLiteral( "</h1>\n<hr>\n" );
5961 myMetadata += htmlFormatter.extentSectionHtml( isSpatial() );
5962 myMetadata += QLatin1String( "<br><br>\n" );
5963
5964 // Start the Access section
5965 myMetadata += QStringLiteral( "<h1>" ) + tr( "Access" ) + QStringLiteral( "</h1>\n<hr>\n" );
5966 myMetadata += htmlFormatter.accessSectionHtml( );
5967 myMetadata += QLatin1String( "<br><br>\n" );
5968
5969 // Fields section
5970 myMetadata += QStringLiteral( "<h1>" ) + tr( "Fields" ) + QStringLiteral( "</h1>\n<hr>\n<table class=\"list-view\">\n" );
5971
5972 // primary key
5974 if ( !pkAttrList.isEmpty() )
5975 {
5976 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Primary key attributes" ) + QStringLiteral( "</td><td>" );
5977 const auto constPkAttrList = pkAttrList;
5978 for ( int idx : constPkAttrList )
5979 {
5980 myMetadata += fields().at( idx ).name() + ' ';
5981 }
5982 myMetadata += QLatin1String( "</td></tr>\n" );
5983 }
5984
5985 const QgsFields myFields = fields();
5986
5987 // count fields
5988 myMetadata += QStringLiteral( "<tr><td class=\"highlight\">" ) + tr( "Count" ) + QStringLiteral( "</td><td>" ) + QString::number( myFields.size() ) + QStringLiteral( "</td></tr>\n" );
5989
5990 myMetadata += QLatin1String( "</table>\n<br><table width=\"100%\" class=\"tabular-view\">\n" );
5991 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" );
5992
5993 for ( int i = 0; i < myFields.size(); ++i )
5994 {
5995 QgsField myField = myFields.at( i );
5996 QString rowClass;
5997 if ( i % 2 )
5998 rowClass = QStringLiteral( "class=\"odd-row\"" );
5999 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" );
6000 }
6001
6002 //close field list
6003 myMetadata += QLatin1String( "</table>\n<br><br>" );
6004
6005 // Start the contacts section
6006 myMetadata += QStringLiteral( "<h1>" ) + tr( "Contacts" ) + QStringLiteral( "</h1>\n<hr>\n" );
6007 myMetadata += htmlFormatter.contactsSectionHtml( );
6008 myMetadata += QLatin1String( "<br><br>\n" );
6009
6010 // Start the links section
6011 myMetadata += QStringLiteral( "<h1>" ) + tr( "Links" ) + QStringLiteral( "</h1>\n<hr>\n" );
6012 myMetadata += htmlFormatter.linksSectionHtml( );
6013 myMetadata += QLatin1String( "<br><br>\n" );
6014
6015 // Start the history section
6016 myMetadata += QStringLiteral( "<h1>" ) + tr( "History" ) + QStringLiteral( "</h1>\n<hr>\n" );
6017 myMetadata += htmlFormatter.historySectionHtml( );
6018 myMetadata += QLatin1String( "<br><br>\n" );
6019
6020 myMetadata += customPropertyHtmlMetadata();
6021
6022 myMetadata += QLatin1String( "\n</body>\n</html>\n" );
6023 return myMetadata;
6024}
6025
6026void QgsVectorLayer::invalidateSymbolCountedFlag()
6027{
6029
6030 mSymbolFeatureCounted = false;
6031}
6032
6033void QgsVectorLayer::onFeatureCounterCompleted()
6034{
6036
6037 onSymbolsCounted();
6038 mFeatureCounter = nullptr;
6039}
6040
6041void QgsVectorLayer::onFeatureCounterTerminated()
6042{
6044
6045 mFeatureCounter = nullptr;
6046}
6047
6048void QgsVectorLayer::onJoinedFieldsChanged()
6049{
6051
6052 // some of the fields of joined layers have changed -> we need to update this layer's fields too
6053 updateFields();
6054}
6055
6056void QgsVectorLayer::onFeatureAdded( QgsFeatureId fid )
6057{
6059
6060 updateExtents();
6061
6062 emit featureAdded( fid );
6063}
6064
6065void QgsVectorLayer::onFeatureDeleted( QgsFeatureId fid )
6066{
6068
6069 updateExtents();
6070
6071 if ( mEditCommandActive || mCommitChangesActive )
6072 {
6073 mDeletedFids << fid;
6074 }
6075 else
6076 {
6077 mSelectedFeatureIds.remove( fid );
6078 emit featuresDeleted( QgsFeatureIds() << fid );
6079 }
6080
6081 emit featureDeleted( fid );
6082}
6083
6084void QgsVectorLayer::onRelationsLoaded()
6085{
6087
6088 mEditFormConfig.onRelationsLoaded();
6089}
6090
6091void QgsVectorLayer::onSymbolsCounted()
6092{
6094
6095 if ( mFeatureCounter )
6096 {
6097 mSymbolFeatureCounted = true;
6098 mSymbolFeatureCountMap = mFeatureCounter->symbolFeatureCountMap();
6099 mSymbolFeatureIdMap = mFeatureCounter->symbolFeatureIdMap();
6101 }
6102}
6103
6104QList<QgsRelation> QgsVectorLayer::referencingRelations( int idx ) const
6105{
6107
6108 if ( QgsProject *p = project() )
6109 return p->relationManager()->referencingRelations( this, idx );
6110 else
6111 return {};
6112}
6113
6114QList<QgsWeakRelation> QgsVectorLayer::weakRelations() const
6115{
6117
6118 return mWeakRelations;
6119}
6120
6121void QgsVectorLayer::setWeakRelations( const QList<QgsWeakRelation> &relations )
6122{
6124
6125 mWeakRelations = relations;
6126}
6127
6128bool QgsVectorLayer::loadAuxiliaryLayer( const QgsAuxiliaryStorage &storage, const QString &key )
6129{
6131
6132 bool rc = false;
6133
6134 QString joinKey = mAuxiliaryLayerKey;
6135 if ( !key.isEmpty() )
6136 joinKey = key;
6137
6138 if ( storage.isValid() && !joinKey.isEmpty() )
6139 {
6140 QgsAuxiliaryLayer *alayer = nullptr;
6141
6142 int idx = fields().lookupField( joinKey );
6143
6144 if ( idx >= 0 )
6145 {
6146 alayer = storage.createAuxiliaryLayer( fields().field( idx ), this );
6147
6148 if ( alayer )
6149 {
6150 setAuxiliaryLayer( alayer );
6151 rc = true;
6152 }
6153 }
6154 }
6155
6156 return rc;
6157}
6158
6160{
6162
6163 mAuxiliaryLayerKey.clear();
6164
6165 if ( mAuxiliaryLayer )
6166 removeJoin( mAuxiliaryLayer->id() );
6167
6168 if ( alayer )
6169 {
6170 addJoin( alayer->joinInfo() );
6171
6172 if ( !alayer->isEditable() )
6173 alayer->startEditing();
6174
6175 mAuxiliaryLayerKey = alayer->joinInfo().targetFieldName();
6176 }
6177
6178 mAuxiliaryLayer.reset( alayer );
6179 if ( mAuxiliaryLayer )
6180 mAuxiliaryLayer->setParent( this );
6181 updateFields();
6182}
6183
6185{
6187
6188 return mAuxiliaryLayer.get();
6189}
6190
6192{
6194
6195 return mAuxiliaryLayer.get();
6196}
6197
6198QSet<QgsMapLayerDependency> QgsVectorLayer::dependencies() const
6199{
6201
6202 if ( mDataProvider )
6203 return mDataProvider->dependencies() + mDependencies;
6204 return mDependencies;
6205}
6206
6207void QgsVectorLayer::emitDataChanged()
6208{
6210
6211 if ( mDataChangedFired )
6212 return;
6213
6214 // If we are asked to fire dataChanged from a layer we depend on,
6215 // be sure that this layer is not in the process of committing its changes, because
6216 // we will be asked to fire dataChanged at the end of his commit, and we don't
6217 // want to fire this signal more than necessary.
6218 if ( QgsVectorLayer *layerWeDependUpon = qobject_cast<QgsVectorLayer *>( sender() );
6219 layerWeDependUpon && layerWeDependUpon->mCommitChangesActive )
6220 return;
6221
6222 updateExtents(); // reset cached extent to reflect data changes
6223
6224 mDataChangedFired = true;
6225 emit dataChanged();
6226 mDataChangedFired = false;
6227}
6228
6229bool QgsVectorLayer::setDependencies( const QSet<QgsMapLayerDependency> &oDeps )
6230{
6232
6233 QSet<QgsMapLayerDependency> deps;
6234 const auto constODeps = oDeps;
6235 for ( const QgsMapLayerDependency &dep : constODeps )
6236 {
6237 if ( dep.origin() == QgsMapLayerDependency::FromUser )
6238 deps << dep;
6239 }
6240
6241 QSet<QgsMapLayerDependency> toAdd = deps - dependencies();
6242
6243 // disconnect layers that are not present in the list of dependencies anymore
6244 if ( QgsProject *p = project() )
6245 {
6246 for ( const QgsMapLayerDependency &dep : std::as_const( mDependencies ) )
6247 {
6248 QgsVectorLayer *lyr = static_cast<QgsVectorLayer *>( p->mapLayer( dep.layerId() ) );
6249 if ( !lyr )
6250 continue;
6251 disconnect( lyr, &QgsVectorLayer::featureAdded, this, &QgsVectorLayer::emitDataChanged );
6252 disconnect( lyr, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayer::emitDataChanged );
6253 disconnect( lyr, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayer::emitDataChanged );
6254 disconnect( lyr, &QgsVectorLayer::dataChanged, this, &QgsVectorLayer::emitDataChanged );
6256 disconnect( lyr, &QgsVectorLayer::afterCommitChanges, this, &QgsVectorLayer::emitDataChanged );
6257 }
6258 }
6259
6260 // assign new dependencies
6261 if ( mDataProvider )
6262 mDependencies = mDataProvider->dependencies() + deps;
6263 else
6264 mDependencies = deps;
6265 emit dependenciesChanged();
6266
6267 // connect to new layers
6268 if ( QgsProject *p = project() )
6269 {
6270 for ( const QgsMapLayerDependency &dep : std::as_const( mDependencies ) )
6271 {
6272 QgsVectorLayer *lyr = static_cast<QgsVectorLayer *>( p->mapLayer( dep.layerId() ) );
6273 if ( !lyr )
6274 continue;
6275 connect( lyr, &QgsVectorLayer::featureAdded, this, &QgsVectorLayer::emitDataChanged );
6276 connect( lyr, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayer::emitDataChanged );
6277 connect( lyr, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayer::emitDataChanged );
6278 connect( lyr, &QgsVectorLayer::dataChanged, this, &QgsVectorLayer::emitDataChanged );
6280 connect( lyr, &QgsVectorLayer::afterCommitChanges, this, &QgsVectorLayer::emitDataChanged );
6281 }
6282 }
6283
6284 // if new layers are present, emit a data change
6285 if ( ! toAdd.isEmpty() )
6286 emitDataChanged();
6287
6288 return true;
6289}
6290
6292{
6294
6295 if ( fieldIndex < 0 || fieldIndex >= mFields.count() || !mDataProvider )
6297
6298 QgsFieldConstraints::Constraints constraints = mFields.at( fieldIndex ).constraints().constraints();
6299
6300 // make sure provider constraints are always present!
6301 if ( mFields.fieldOrigin( fieldIndex ) == Qgis::FieldOrigin::Provider )
6302 {
6303 constraints |= mDataProvider->fieldConstraints( mFields.fieldOriginIndex( fieldIndex ) );
6304 }
6305
6306 return constraints;
6307}
6308
6309QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength> QgsVectorLayer::fieldConstraintsAndStrength( int fieldIndex ) const
6310{
6312
6313 QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength > m;
6314
6315 if ( fieldIndex < 0 || fieldIndex >= mFields.count() )
6316 return m;
6317
6318 QString name = mFields.at( fieldIndex ).name();
6319
6320 QMap< QPair< QString, QgsFieldConstraints::Constraint >, QgsFieldConstraints::ConstraintStrength >::const_iterator conIt = mFieldConstraintStrength.constBegin();
6321 for ( ; conIt != mFieldConstraintStrength.constEnd(); ++conIt )
6322 {
6323 if ( conIt.key().first == name )
6324 {
6325 m[ conIt.key().second ] = mFieldConstraintStrength.value( conIt.key() );
6326 }
6327 }
6328
6329 return m;
6330}
6331
6333{
6335
6336 if ( index < 0 || index >= mFields.count() )
6337 return;
6338
6339 QString name = mFields.at( index ).name();
6340
6341 // add constraint to existing constraints
6342 QgsFieldConstraints::Constraints constraints = mFieldConstraints.value( name, QgsFieldConstraints::Constraints() );
6343 constraints |= constraint;
6344 mFieldConstraints.insert( name, constraints );
6345
6346 mFieldConstraintStrength.insert( qMakePair( name, constraint ), strength );
6347
6348 updateFields();
6349}
6350
6352{
6354
6355 if ( index < 0 || index >= mFields.count() )
6356 return;
6357
6358 QString name = mFields.at( index ).name();
6359
6360 // remove constraint from existing constraints
6361 QgsFieldConstraints::Constraints constraints = mFieldConstraints.value( name, QgsFieldConstraints::Constraints() );
6362 constraints &= ~constraint;
6363 mFieldConstraints.insert( name, constraints );
6364
6365 mFieldConstraintStrength.remove( qMakePair( name, constraint ) );
6366
6367 updateFields();
6368}
6369
6371{
6373
6374 if ( index < 0 || index >= mFields.count() )
6375 return QString();
6376
6377 return mFields.at( index ).constraints().constraintExpression();
6378}
6379
6381{
6383
6384 if ( index < 0 || index >= mFields.count() )
6385 return QString();
6386
6387 return mFields.at( index ).constraints().constraintDescription();
6388}
6389
6390void QgsVectorLayer::setConstraintExpression( int index, const QString &expression, const QString &description )
6391{
6393
6394 if ( index < 0 || index >= mFields.count() )
6395 return;
6396
6397 if ( expression.isEmpty() )
6398 {
6399 mFieldConstraintExpressions.remove( mFields.at( index ).name() );
6400 }
6401 else
6402 {
6403 mFieldConstraintExpressions.insert( mFields.at( index ).name(), qMakePair( expression, description ) );
6404 }
6405 updateFields();
6406}
6407
6409{
6411
6412 if ( index < 0 || index >= mFields.count() )
6413 return;
6414
6415 mFieldConfigurationFlags.insert( mFields.at( index ).name(), flags );
6416 updateFields();
6417}
6418
6420{
6422
6423 if ( index < 0 || index >= mFields.count() )
6424 return;
6426 flags.setFlag( flag, active );
6428}
6429
6431{
6433
6434 if ( index < 0 || index >= mFields.count() )
6436
6437 return mFields.at( index ).configurationFlags();
6438}
6439
6441{
6443
6444 if ( index < 0 || index >= mFields.count() )
6445 return;
6446
6447 if ( setup.isNull() )
6448 mFieldWidgetSetups.remove( mFields.at( index ).name() );
6449 else
6450 mFieldWidgetSetups.insert( mFields.at( index ).name(), setup );
6451 updateFields();
6452}
6453
6455{
6457
6458 if ( index < 0 || index >= mFields.count() )
6459 return QgsEditorWidgetSetup();
6460
6461 return mFields.at( index ).editorWidgetSetup();
6462}
6463
6464QgsAbstractVectorLayerLabeling *QgsVectorLayer::readLabelingFromCustomProperties()
6465{
6467
6469 if ( customProperty( QStringLiteral( "labeling" ) ).toString() == QLatin1String( "pal" ) )
6470 {
6471 if ( customProperty( QStringLiteral( "labeling/enabled" ), QVariant( false ) ).toBool() )
6472 {
6473 // try to load from custom properties
6474 QgsPalLayerSettings settings;
6475 settings.readFromLayerCustomProperties( this );
6476 labeling = new QgsVectorLayerSimpleLabeling( settings );
6477 }
6478
6479 // also clear old-style labeling config
6480 removeCustomProperty( QStringLiteral( "labeling" ) );
6481 const auto constCustomPropertyKeys = customPropertyKeys();
6482 for ( const QString &key : constCustomPropertyKeys )
6483 {
6484 if ( key.startsWith( QLatin1String( "labeling/" ) ) )
6485 removeCustomProperty( key );
6486 }
6487 }
6488
6489 return labeling;
6490}
6491
6493{
6495
6496 return mAllowCommit;
6497}
6498
6499void QgsVectorLayer::setAllowCommit( bool allowCommit )
6500{
6502
6503 if ( mAllowCommit == allowCommit )
6504 return;
6505
6506 mAllowCommit = allowCommit;
6507 emit allowCommitChanged();
6508}
6509
6511{
6513
6514 return mGeometryOptions.get();
6515}
6516
6517void QgsVectorLayer::setReadExtentFromXml( bool readExtentFromXml )
6518{
6520
6521 mReadExtentFromXml = readExtentFromXml;
6522}
6523
6525{
6527
6528 return mReadExtentFromXml;
6529}
6530
6531void QgsVectorLayer::onDirtyTransaction( const QString &sql, const QString &name )
6532{
6534
6536 if ( tr && mEditBuffer )
6537 {
6538 qobject_cast<QgsVectorLayerEditPassthrough *>( mEditBuffer )->update( tr, sql, name );
6539 }
6540}
6541
6542QList<QgsVectorLayer *> QgsVectorLayer::DeleteContext::handledLayers( bool includeAuxiliaryLayers ) const
6543{
6544 QList<QgsVectorLayer *> layers;
6545 QMap<QgsVectorLayer *, QgsFeatureIds>::const_iterator i;
6546 for ( i = mHandledFeatures.begin(); i != mHandledFeatures.end(); ++i )
6547 {
6548 if ( includeAuxiliaryLayers || !qobject_cast< QgsAuxiliaryLayer * >( i.key() ) )
6549 layers.append( i.key() );
6550 }
6551 return layers;
6552}
6553
6555{
6556 return mHandledFeatures[layer];
6557}
Provides global constants and enumerations 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:2954
@ 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:2005
@ 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:2939
@ 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:2923
@ 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...
FieldDomainMergePolicy
Merge policy for field domains.
Definition qgis.h:3805
@ UnsetField
Clears the field value so that the data provider backend will populate using any backend triggers or ...
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:3788
@ Duplicate
Duplicate original value.
BlendMode
Blending modes defining the available composition modes that can be used when painting.
Definition qgis.h:4792
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:3825
@ Duplicate
Duplicate original value.
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition qgis.h:5966
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:1667
@ 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:5045
@ 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:5662
VertexMarkerType
Editing vertex markers, used for showing vertices during a edit operation.
Definition qgis.h:1796
@ SemiTransparentCircle
Semi-transparent circle marker.
@ Cross
Cross marker.
VectorEditResult
Specifies the result of a vector layer edit operation.
Definition qgis.h:1781
@ 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:1684
@ 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:1699
@ AlwaysAllowUpsideDown
Show upside down for all labels, including dynamic ones.
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1734
@ 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 Q_DECL_DEPRECATED 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.
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.
Allows managing the auxiliary storage for a vector layer.
QgsVectorLayerJoinInfo joinInfo() const
Returns information to use for joining with primary key and so on.
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
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin,zmin : xmax,ymax,zmax Coordinates will be truncated...
Definition qgsbox3d.cpp:325
bool isNull() const
Test if the box is null (holding no spatial information).
Definition qgsbox3d.cpp:310
bool isEmpty() const
Returns true if the box is empty.
Definition qgsbox3d.cpp:320
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.
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.
Stores the component parts of a data source URI (e.g.
bool useEstimatedMetadata() const
Returns true if estimated metadata should be used for the connection.
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
Returns the widget configuration.
bool isNull() const
Returns true if there is no widget configured.
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 its value from a feature's field.
Handles 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 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.
virtual Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const
Used from subclasses to create SLD Rule elements following SLD v1.1 specs.
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 ...
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:162
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:104
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:96
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:746
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
Contains options to automatically adjust geometries to constraints on a layer.
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.
Formats layer metadata into HTML.
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 linear scaling.
static void warning(const QString &msg)
Goes to qWarning.
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:77
QString name
Definition qgsmaplayer.h:81
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:84
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:80
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:83
Qgis::LayerType type
Definition qgsmaplayer.h:87
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:91
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
double opacity
Definition qgsmaplayer.h:89
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:90
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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
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.
Represents 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 an 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.
A container for the context for various read/write operations on 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.
Q_INVOKABLE 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
double yMinimum
double xMaximum
void set(const QgsPointXY &p1, const QgsPointXY &p2, bool normalize=true)
Sets the rectangle from two QgsPoints.
double yMaximum
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.
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
Stores settings for use within QGIS.
Definition qgssettings.h:65
Renders the diagrams for all features with the same settings.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
void setExtraProperties(const QVariantMap &properties)
Sets the open ended set of properties that can drive/inform the SLD encoding.
QVariantMap extraProperties() const
Returns the open ended set of properties that can drive/inform the SLD encoding.
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.
Allows creation of a multi-layer database-side transaction.
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.
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 be considere...
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 dataset.
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 setFieldMergePolicy(int index, Qgis::FieldDomainMergePolicy policy)
Sets a merge policy for the field with the specified index.
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.
Q_DECL_DEPRECATED 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 Q_INVOKABLE 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 Q_INVOKABLE 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:351
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:286
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:291
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:6577
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6558
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:6616
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:6638
QMap< QString, QString > QgsStringMap
Definition qgis.h:6881
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:41
#define QgsDebugError(str)
Definition qgslogger.h:40
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
#define RENDERER_TAG_NAME
Definition qgsrenderer.h:54
#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.