Skip to content
Permalink
Browse files
Merge pull request #5094 from pblottiere/trust2
[FEATURE] Trust project option
  • Loading branch information
pblottiere committed Sep 7, 2017
2 parents a0e6b7f + 437aefa commit 20d8244
Show file tree
Hide file tree
Showing 16 changed files with 432 additions and 26 deletions.
@@ -785,6 +785,31 @@ Returns the number of registered layers.
:rtype: QgsCoordinateReferenceSystem
%End

void setTrustLayerMetadata( bool trust );
%Docstring
Sets the trust option allowing to indicate if the extent has to be
read from the XML document when data source has no metadata or if the
data provider has to determine it. Moreover, when this option is
activated, primary key unicity is not checked for views and
materialized views with Postgres provider.

\param trust True to trust the project, false otherwise

.. versionadded:: 3.0
%End

bool trustLayerMetadata() const;
%Docstring
Returns true if the trust option is activated, false otherwise. This
option allows indicateing if the extent has to be read from the XML
document when data source has no metadata or if the data provider has
to determine it. Moreover, when this option is activated, primary key
unicity is not checked for views and materialized views with Postgres
provider.

.. versionadded:: 3.0
:rtype: bool
%End

signals:
void readProject( const QDomDocument & );
@@ -524,6 +524,15 @@ Returns a list of available encodings
:rtype: str
%End

virtual bool hasMetadata() const;
%Docstring
Returns true if the data source has metadata, false otherwise.

:return: true if data source has metadata, false otherwise.

.. versionadded:: 3.0
:rtype: bool
%End
signals:

void raiseError( const QString &msg ) const;
@@ -315,7 +315,8 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator, QgsFeatureSin
};

QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true );
const QString &providerLib = "ogr", bool loadDefaultStyleFlag = true,
bool readExtentFromXml = false );
%Docstring
Constructor - creates a vector layer

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


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

void setReadExtentFromXml( bool readExtentFromXml );
%Docstring
Flag allowing to indicate if the extent has to be read from the XML
document when data source has no metadata or if the data provider has
to determine it.

.. versionadded:: 3.0
%End

bool readExtentFromXml() const;
%Docstring
Returns true if the extent is read from the XML document when data
source has no metadata, false if it's the data provider which determines
it.

.. versionadded:: 3.0
:rtype: bool
%End

public slots:

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

virtual void updateExtents();
virtual void updateExtents( bool force = false );
%Docstring
Update the extents for the layer. This is necessary if features are
added/deleted or the layer has been subsetted.

\param force true to update layer extent even if it's read from xml by default, false otherwise
%End

bool startEditing();
@@ -708,6 +708,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa

mAutoTransaction->setChecked( QgsProject::instance()->autoTransaction() );
mEvaluateDefaultValues->setChecked( QgsProject::instance()->evaluateDefaultValues() );
mTrustProjectCheckBox->setChecked( QgsProject::instance()->trustLayerMetadata() );

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

// set the mouse display precision method and the
// number of decimal places for the manual option
@@ -1387,7 +1387,7 @@ void QgsVectorLayerProperties::setPbnQueryBuilderEnabled()

void QgsVectorLayerProperties::on_pbnUpdateExtents_clicked()
{
mLayer->updateExtents();
mLayer->updateExtents( true ); // force update whatever options activated
mMetadataFilled = false;
}

@@ -474,12 +474,6 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, const QgsReadWr
setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), 0 ).toInt() );
setAutoRefreshEnabled( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() );

QDomNode extentNode = layerElement.namedItem( QStringLiteral( "extent" ) );
if ( !extentNode.isNull() )
{
setExtent( QgsXmlUtils::readRectangle( extentNode.toElement() ) );
}

// set name
mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
mne = mnl.toElement();
@@ -575,7 +569,7 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume
layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );

if ( !mExtent.isNull() )
if ( !extent().isNull() )
{
layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent, document ) );
}
@@ -480,6 +480,7 @@ void QgsProject::clear()
mAutoTransaction = false;
mEvaluateDefaultValues = false;
mDirty = false;
mTrustLayerMetadata = false;
mCustomVariables.clear();

mEmbeddedLayers.clear();
@@ -717,6 +718,12 @@ bool QgsProject::addLayer( const QDomElement &layerElem, QList<QDomNode> &broken
if ( type == QLatin1String( "vector" ) )
{
mapLayer = new QgsVectorLayer;

// apply specific settings to vector layer
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mapLayer ) )
{
vl->setReadExtentFromXml( mTrustLayerMetadata );
}
}
else if ( type == QLatin1String( "raster" ) )
{
@@ -892,6 +899,14 @@ bool QgsProject::readProjectFile( const QString &filename )
mEvaluateDefaultValues = true;
}

nl = doc->elementsByTagName( QStringLiteral( "trust" ) );
if ( nl.count() )
{
QDomElement trustElement = nl.at( 0 ).toElement();
if ( trustElement.attribute( QStringLiteral( "active" ), QStringLiteral( "0" ) ).toInt() == 1 )
mTrustLayerMetadata = true;
}

// read the layer tree from project file

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

QDomElement trustNode = doc->createElement( QStringLiteral( "trust" ) );
trustNode.setAttribute( QStringLiteral( "active" ), mTrustLayerMetadata ? "1" : "0" );
qgisNode.appendChild( trustNode );

QDomText titleText = doc->createTextNode( title() ); // XXX why have title TWICE?
titleNode.appendChild( titleText );

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

return defaultCrs;
}

void QgsProject::setTrustLayerMetadata( bool trust )
{
mTrustLayerMetadata = trust;

Q_FOREACH ( QgsMapLayer *layer, mapLayers().values() )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
if ( vl )
{
vl->setReadExtentFromXml( trust );
}
}
}
@@ -755,6 +755,30 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QgsCoordinateReferenceSystem defaultCrsForNewLayers() const;

/**
* Sets the trust option allowing to indicate if the extent has to be
* read from the XML document when data source has no metadata or if the
* data provider has to determine it. Moreover, when this option is
* activated, primary key unicity is not checked for views and
* materialized views with Postgres provider.
*
* \param trust True to trust the project, false otherwise
*
* \since QGIS 3.0
*/
void setTrustLayerMetadata( bool trust );

/**
* Returns true if the trust option is activated, false otherwise. This
* option allows indicateing if the extent has to be read from the XML
* document when data source has no metadata or if the data provider has
* to determine it. Moreover, when this option is activated, primary key
* unicity is not checked for views and materialized views with Postgres
* provider.
*
* \since QGIS 3.0
*/
bool trustLayerMetadata() const { return mTrustLayerMetadata; }

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

/** Return the version string found in the given DOM document
@@ -516,6 +516,14 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
*/
virtual QString translateMetadataValue( const QString &mdKey, const QVariant &value ) const { Q_UNUSED( mdKey ); return value.toString(); }

/** Returns true if the data source has metadata, false otherwise.
*
* \returns true if data source has metadata, false otherwise.
*
* \since QGIS 3.0
*/
virtual bool hasMetadata() const { return true; };

signals:

/**

0 comments on commit 20d8244

Please sign in to comment.