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