Skip to content

Commit 20d8244

Browse files
authored
Merge pull request #5094 from pblottiere/trust2
[FEATURE] Trust project option
2 parents a0e6b7f + 437aefa commit 20d8244

16 files changed

+432
-26
lines changed

python/core/qgsproject.sip

+25
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,31 @@ Returns the number of registered layers.
785785
:rtype: QgsCoordinateReferenceSystem
786786
%End
787787

788+
void setTrustLayerMetadata( bool trust );
789+
%Docstring
790+
Sets the trust option allowing to indicate if the extent has to be
791+
read from the XML document when data source has no metadata or if the
792+
data provider has to determine it. Moreover, when this option is
793+
activated, primary key unicity is not checked for views and
794+
materialized views with Postgres provider.
795+
796+
\param trust True to trust the project, false otherwise
797+
798+
.. versionadded:: 3.0
799+
%End
800+
801+
bool trustLayerMetadata() const;
802+
%Docstring
803+
Returns true if the trust option is activated, false otherwise. This
804+
option allows indicateing if the extent has to be read from the XML
805+
document when data source has no metadata or if the data provider has
806+
to determine it. Moreover, when this option is activated, primary key
807+
unicity is not checked for views and materialized views with Postgres
808+
provider.
809+
810+
.. versionadded:: 3.0
811+
:rtype: bool
812+
%End
788813

789814
signals:
790815
void readProject( const QDomDocument & );

python/core/qgsvectordataprovider.sip

+9
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,15 @@ Returns a list of available encodings
524524
:rtype: str
525525
%End
526526

527+
virtual bool hasMetadata() const;
528+
%Docstring
529+
Returns true if the data source has metadata, false otherwise.
530+
531+
:return: true if data source has metadata, false otherwise.
532+
533+
.. versionadded:: 3.0
534+
:rtype: bool
535+
%End
527536
signals:
528537

529538
void raiseError( const QString &msg ) const;

python/core/qgsvectorlayer.sip

+25-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
315315
};
316316

317317
QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
318-
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true );
318+
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true,
319+
bool readExtentFromXml = false );
319320
%Docstring
320321
Constructor - creates a vector layer
321322

@@ -328,6 +329,7 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
328329
\param baseName The name used to represent the layer in the legend
329330
\param providerLib The name of the data provider, e.g., "memory", "postgres"
330331
\param loadDefaultStyleFlag whether to load the default style
332+
\param readExtentFromXml Read extent from XML if true or let provider determine it if false
331333
%End
332334

333335

@@ -1727,6 +1729,25 @@ Returns the current blending mode for features
17271729
.. versionadded:: 3.0
17281730
%End
17291731

1732+
void setReadExtentFromXml( bool readExtentFromXml );
1733+
%Docstring
1734+
Flag allowing to indicate if the extent has to be read from the XML
1735+
document when data source has no metadata or if the data provider has
1736+
to determine it.
1737+
1738+
.. versionadded:: 3.0
1739+
%End
1740+
1741+
bool readExtentFromXml() const;
1742+
%Docstring
1743+
Returns true if the extent is read from the XML document when data
1744+
source has no metadata, false if it's the data provider which determines
1745+
it.
1746+
1747+
.. versionadded:: 3.0
1748+
:rtype: bool
1749+
%End
1750+
17301751
public slots:
17311752

17321753
void select( QgsFeatureId featureId );
@@ -1772,10 +1793,12 @@ Returns the current blending mode for features
17721793
.. seealso:: selectByIds()
17731794
%End
17741795

1775-
virtual void updateExtents();
1796+
virtual void updateExtents( bool force = false );
17761797
%Docstring
17771798
Update the extents for the layer. This is necessary if features are
17781799
added/deleted or the layer has been subsetted.
1800+
1801+
\param force true to update layer extent even if it's read from xml by default, false otherwise
17791802
%End
17801803

17811804
bool startEditing();

src/app/qgsprojectproperties.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
708708

709709
mAutoTransaction->setChecked( QgsProject::instance()->autoTransaction() );
710710
mEvaluateDefaultValues->setChecked( QgsProject::instance()->evaluateDefaultValues() );
711+
mTrustProjectCheckBox->setChecked( QgsProject::instance()->trustLayerMetadata() );
711712

712713
// Variables editor
713714
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::globalScope() );
@@ -764,6 +765,7 @@ void QgsProjectProperties::apply()
764765
QgsProject::instance()->setTitle( title() );
765766
QgsProject::instance()->setAutoTransaction( mAutoTransaction->isChecked() );
766767
QgsProject::instance()->setEvaluateDefaultValues( mEvaluateDefaultValues->isChecked() );
768+
QgsProject::instance()->setTrustLayerMetadata( mTrustProjectCheckBox->isChecked() );
767769

768770
// set the mouse display precision method and the
769771
// number of decimal places for the manual option

src/app/qgsvectorlayerproperties.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ void QgsVectorLayerProperties::setPbnQueryBuilderEnabled()
13871387

13881388
void QgsVectorLayerProperties::on_pbnUpdateExtents_clicked()
13891389
{
1390-
mLayer->updateExtents();
1390+
mLayer->updateExtents( true ); // force update whatever options activated
13911391
mMetadataFilled = false;
13921392
}
13931393

src/core/qgsmaplayer.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,6 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, const QgsReadWr
474474
setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), 0 ).toInt() );
475475
setAutoRefreshEnabled( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() );
476476

477-
QDomNode extentNode = layerElement.namedItem( QStringLiteral( "extent" ) );
478-
if ( !extentNode.isNull() )
479-
{
480-
setExtent( QgsXmlUtils::readRectangle( extentNode.toElement() ) );
481-
}
482-
483477
// set name
484478
mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
485479
mne = mnl.toElement();
@@ -575,7 +569,7 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume
575569
layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
576570
layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );
577571

578-
if ( !mExtent.isNull() )
572+
if ( !extent().isNull() )
579573
{
580574
layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent, document ) );
581575
}

src/core/qgsproject.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ void QgsProject::clear()
480480
mAutoTransaction = false;
481481
mEvaluateDefaultValues = false;
482482
mDirty = false;
483+
mTrustLayerMetadata = false;
483484
mCustomVariables.clear();
484485

485486
mEmbeddedLayers.clear();
@@ -717,6 +718,12 @@ bool QgsProject::addLayer( const QDomElement &layerElem, QList<QDomNode> &broken
717718
if ( type == QLatin1String( "vector" ) )
718719
{
719720
mapLayer = new QgsVectorLayer;
721+
722+
// apply specific settings to vector layer
723+
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mapLayer ) )
724+
{
725+
vl->setReadExtentFromXml( mTrustLayerMetadata );
726+
}
720727
}
721728
else if ( type == QLatin1String( "raster" ) )
722729
{
@@ -892,6 +899,14 @@ bool QgsProject::readProjectFile( const QString &filename )
892899
mEvaluateDefaultValues = true;
893900
}
894901

902+
nl = doc->elementsByTagName( QStringLiteral( "trust" ) );
903+
if ( nl.count() )
904+
{
905+
QDomElement trustElement = nl.at( 0 ).toElement();
906+
if ( trustElement.attribute( QStringLiteral( "active" ), QStringLiteral( "0" ) ).toInt() == 1 )
907+
mTrustLayerMetadata = true;
908+
}
909+
895910
// read the layer tree from project file
896911

897912
mRootGroup->setCustomProperty( QStringLiteral( "loading" ), 1 );
@@ -1296,6 +1311,10 @@ bool QgsProject::writeProjectFile( const QString &filename )
12961311
evaluateDefaultValuesNode.setAttribute( QStringLiteral( "active" ), mEvaluateDefaultValues ? "1" : "0" );
12971312
qgisNode.appendChild( evaluateDefaultValuesNode );
12981313

1314+
QDomElement trustNode = doc->createElement( QStringLiteral( "trust" ) );
1315+
trustNode.setAttribute( QStringLiteral( "active" ), mTrustLayerMetadata ? "1" : "0" );
1316+
qgisNode.appendChild( trustNode );
1317+
12991318
QDomText titleText = doc->createTextNode( title() ); // XXX why have title TWICE?
13001319
titleNode.appendChild( titleText );
13011320

@@ -2260,3 +2279,17 @@ QgsCoordinateReferenceSystem QgsProject::defaultCrsForNewLayers() const
22602279

22612280
return defaultCrs;
22622281
}
2282+
2283+
void QgsProject::setTrustLayerMetadata( bool trust )
2284+
{
2285+
mTrustLayerMetadata = trust;
2286+
2287+
Q_FOREACH ( QgsMapLayer *layer, mapLayers().values() )
2288+
{
2289+
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
2290+
if ( vl )
2291+
{
2292+
vl->setReadExtentFromXml( trust );
2293+
}
2294+
}
2295+
}

src/core/qgsproject.h

+25
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,30 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
755755
*/
756756
QgsCoordinateReferenceSystem defaultCrsForNewLayers() const;
757757

758+
/**
759+
* Sets the trust option allowing to indicate if the extent has to be
760+
* read from the XML document when data source has no metadata or if the
761+
* data provider has to determine it. Moreover, when this option is
762+
* activated, primary key unicity is not checked for views and
763+
* materialized views with Postgres provider.
764+
*
765+
* \param trust True to trust the project, false otherwise
766+
*
767+
* \since QGIS 3.0
768+
*/
769+
void setTrustLayerMetadata( bool trust );
770+
771+
/**
772+
* Returns true if the trust option is activated, false otherwise. This
773+
* option allows indicateing if the extent has to be read from the XML
774+
* document when data source has no metadata or if the data provider has
775+
* to determine it. Moreover, when this option is activated, primary key
776+
* unicity is not checked for views and materialized views with Postgres
777+
* provider.
778+
*
779+
* \since QGIS 3.0
780+
*/
781+
bool trustLayerMetadata() const { return mTrustLayerMetadata; }
758782

759783
signals:
760784
//! emitted when project is being read
@@ -1083,6 +1107,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
10831107
bool mEvaluateDefaultValues; // evaluate default values immediately
10841108
QgsCoordinateReferenceSystem mCrs;
10851109
bool mDirty; // project has been modified since it has been read or saved
1110+
bool mTrustLayerMetadata = false;
10861111
};
10871112

10881113
/** Return the version string found in the given DOM document

src/core/qgsvectordataprovider.h

+8
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,14 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
516516
*/
517517
virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }
518518

519+
/** Returns true if the data source has metadata, false otherwise.
520+
*
521+
* \returns true if data source has metadata, false otherwise.
522+
*
523+
* \since QGIS 3.0
524+
*/
525+
virtual bool hasMetadata() const { return true; };
526+
519527
signals:
520528

521529
/**

0 commit comments

Comments
 (0)