Skip to content

Commit

Permalink
Trust is now a project option instead of a global option
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Sep 6, 2017
1 parent 7ef2e70 commit 8a17a60
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 88 deletions.
25 changes: 25 additions & 0 deletions python/core/qgsproject.sip
Expand Up @@ -785,6 +785,31 @@ Returns the number of registered layers.
:rtype: QgsCoordinateReferenceSystem
%End

void setTrust( 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 trust() 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 & );
Expand Down
2 changes: 0 additions & 2 deletions src/app/qgsoptions.cpp
Expand Up @@ -635,7 +635,6 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
cbxAddOracleDC->setChecked( mSettings->value( QStringLiteral( "/qgis/addOracleDC" ), false ).toBool() );
cbxCompileExpressions->setChecked( mSettings->value( QStringLiteral( "/qgis/compileExpressions" ), true ).toBool() );
cbxCreateRasterLegendIcons->setChecked( mSettings->value( QStringLiteral( "/qgis/createRasterLegendIcons" ), false ).toBool() );
cbxTrustProject->setChecked( mSettings->value( QStringLiteral( "/qgis/trustProject" ), false ).toBool() );

mComboCopyFeatureFormat->addItem( tr( "Plain text, no geometry" ), QgsClipboard::AttributesOnly );
mComboCopyFeatureFormat->addItem( tr( "Plain text, WKT geometry" ), QgsClipboard::AttributesWithWKT );
Expand Down Expand Up @@ -1226,7 +1225,6 @@ void QgsOptions::saveOptions()
mSettings->setValue( QStringLiteral( "/qgis/addPostgisDC" ), cbxAddPostgisDC->isChecked() );
mSettings->setValue( QStringLiteral( "/qgis/addOracleDC" ), cbxAddOracleDC->isChecked() );
mSettings->setValue( QStringLiteral( "/qgis/compileExpressions" ), cbxCompileExpressions->isChecked() );
mSettings->setValue( QStringLiteral( "/qgis/trustProject" ), cbxTrustProject->isChecked() );
mSettings->setValue( QStringLiteral( "/qgis/defaultLegendGraphicResolution" ), mLegendGraphicResolutionSpinBox->value() );
bool createRasterLegendIcons = mSettings->value( QStringLiteral( "/qgis/createRasterLegendIcons" ), false ).toBool();
mSettings->setValue( QStringLiteral( "/qgis/createRasterLegendIcons" ), cbxCreateRasterLegendIcons->isChecked() );
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -708,6 +708,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa

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

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

// set the mouse display precision method and the
// number of decimal places for the manual option
Expand Down
31 changes: 28 additions & 3 deletions src/core/qgsproject.cpp
Expand Up @@ -480,6 +480,7 @@ void QgsProject::clear()
mAutoTransaction = false;
mEvaluateDefaultValues = false;
mDirty = false;
mTrust = false;
mCustomVariables.clear();

mEmbeddedLayers.clear();
Expand Down Expand Up @@ -719,11 +720,9 @@ bool QgsProject::addLayer( const QDomElement &layerElem, QList<QDomNode> &broken
mapLayer = new QgsVectorLayer;

// apply specific settings to vector layer
QgsSettings s;
bool trustProject = s.value( QStringLiteral( "/qgis/trustProject" ), false ).toBool();
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mapLayer ) )
{
vl->setReadExtentFromXml( trustProject );
vl->setReadExtentFromXml( mTrust );
}
}
else if ( type == QLatin1String( "raster" ) )
Expand Down Expand Up @@ -900,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 )
mTrust = true;
}

// read the layer tree from project file

mRootGroup->setCustomProperty( QStringLiteral( "loading" ), 1 );
Expand Down Expand Up @@ -1304,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" ), mTrust ? "1" : "0" );
qgisNode.appendChild( trustNode );

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

Expand Down Expand Up @@ -2268,3 +2279,17 @@ QgsCoordinateReferenceSystem QgsProject::defaultCrsForNewLayers() const

return defaultCrs;
}

void QgsProject::setTrust( bool trust )
{
mTrust = trust;

Q_FOREACH ( QgsMapLayer *layer, mapLayers().values() )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
if ( vl )
{
vl->setReadExtentFromXml( trust );
}
}
}
25 changes: 25 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -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 setTrust( 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 trust() const { return mTrust; }

signals:
//! emitted when project is being read
Expand Down Expand Up @@ -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 mTrust = false;
};

/** Return the version string found in the given DOM document
Expand Down
27 changes: 24 additions & 3 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -803,7 +803,8 @@ QgsRectangle QgsVectorLayer::extent() const
if ( !isSpatial() )
return rect;

if ( !mValidExtent && mDataProvider && !mDataProvider->hasMetadata() && mReadExtentFromXml && !mXmlExtent.isNull() )

if ( !mValidExtent && mLazyExtent && mDataProvider && !mDataProvider->hasMetadata() && mReadExtentFromXml && !mXmlExtent.isNull() )
{
mExtent = mXmlExtent;
mValidExtent = true;
Expand Down Expand Up @@ -1498,12 +1499,28 @@ void QgsVectorLayer::setDataSource( const QString &dataSource, const QString &ba
bool QgsVectorLayer::setDataProvider( QString const &provider )
{
mProviderKey = provider; // XXX is this necessary? Usually already set

// primary key unicity is tested at construction time, so it has to be set
// before initializing postgres provider
QString checkUnicityKey = QStringLiteral( "checkPrimaryKeyUnicity" );
QString dataSource = mDataSource;
if ( provider.compare( QLatin1String( "postgres" ) ) == 0 )
{
QgsDataSourceUri uri( dataSource );

if ( uri.hasParam( checkUnicityKey ) )
uri.removeParam( checkUnicityKey );

uri.setParam( checkUnicityKey, mReadExtentFromXml ? "0" : "1" );
dataSource = uri.uri( false );
}

// XXX when execution gets here.

//XXX - This was a dynamic cast but that kills the Windows
// version big-time with an abnormal termination error
delete mDataProvider;
mDataProvider = ( QgsVectorDataProvider * )( QgsProviderRegistry::instance()->createProvider( provider, mDataSource ) );
mDataProvider = ( QgsVectorDataProvider * )( QgsProviderRegistry::instance()->createProvider( provider, dataSource ) );
if ( !mDataProvider )
{
QgsDebugMsg( " unable to get data provider" );
Expand Down Expand Up @@ -1559,7 +1576,11 @@ bool QgsVectorLayer::setDataProvider( QString const &provider )
QgsDebugMsg( "Beautified layer name " + name() );

// deal with unnecessary schema qualification to make v.in.ogr happy
mDataSource = mDataProvider->dataSourceUri();
// and remove unnecessary key
QgsDataSourceUri dataProviderUri( mDataProvider->dataSourceUri() );
if ( dataProviderUri.hasParam( checkUnicityKey ) )
dataProviderUri.removeParam( checkUnicityKey );
mDataSource = dataProviderUri.uri( false );
}
else if ( mProviderKey == QLatin1String( "osm" ) )
{
Expand Down
22 changes: 17 additions & 5 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -115,6 +115,18 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri )
mRequestedSrid = mUri.srid();
mRequestedGeomType = mUri.wkbType();

if ( mUri.hasParam( QStringLiteral( "checkPrimaryKeyUnicity" ) ) )
{
if ( mUri.param( QStringLiteral( "checkPrimaryKeyUnicity" ) ).compare( "0" ) == 0 )
{
mCheckPrimaryKeyUnicity = false;
}
else
{
mCheckPrimaryKeyUnicity = true;
}
}

if ( mSchemaName.isEmpty() && mTableName.startsWith( '(' ) && mTableName.endsWith( ')' ) )
{
mIsQuery = true;
Expand Down Expand Up @@ -1325,9 +1337,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
}
else if ( type == Relkind::View || type == Relkind::MaterializedView )
{
QgsSettings settings;
bool checkPrimaryKeyUnicity = !settings.value( QStringLiteral( "/qgis/trustProject" ), false ).toBool();
determinePrimaryKeyFromUriKeyColumn( checkPrimaryKeyUnicity );
determinePrimaryKeyFromUriKeyColumn();
}
else
{
Expand Down Expand Up @@ -1456,7 +1466,7 @@ QStringList QgsPostgresProvider::parseUriKey( const QString &key )
return cols;
}

void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn( bool checkPrimaryKeyUnicity )
void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn()
{
QString primaryKey = mUri.keyColumn();
mPrimaryKeyType = PktUnknown;
Expand Down Expand Up @@ -1489,8 +1499,10 @@ void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn( bool checkPrimary
if ( !mPrimaryKeyAttrs.isEmpty() )
{
bool unique = true;
if ( checkPrimaryKeyUnicity )
if ( mCheckPrimaryKeyUnicity )
{
unique = uniqueData( primaryKey );
}

if ( mUseEstimatedMetadata || unique )
{
Expand Down
4 changes: 3 additions & 1 deletion src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -133,7 +133,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
* Fills mPrimaryKeyType and mPrimaryKeyAttrs
* from mUri
*/
void determinePrimaryKeyFromUriKeyColumn( bool checkPrimaryKeyUnicity = true );
void determinePrimaryKeyFromUriKeyColumn();

QgsFields fields() const override;
QString dataComment() const override;
Expand Down Expand Up @@ -432,6 +432,8 @@ class QgsPostgresProvider : public QgsVectorDataProvider
void setTransaction( QgsTransaction *transaction ) override;

QHash<int, QString> mDefaultValues;

bool mCheckPrimaryKeyUnicity = true;
};


Expand Down

0 comments on commit 8a17a60

Please sign in to comment.