Skip to content

Commit 8a17a60

Browse files
committedSep 6, 2017
Trust is now a project option instead of a global option
1 parent 7ef2e70 commit 8a17a60

File tree

10 files changed

+198
-88
lines changed

10 files changed

+198
-88
lines changed
 

‎python/core/qgsproject.sip

Lines changed: 25 additions & 0 deletions
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 setTrust( 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 trust() 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 & );

‎src/app/qgsoptions.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
635635
cbxAddOracleDC->setChecked( mSettings->value( QStringLiteral( "/qgis/addOracleDC" ), false ).toBool() );
636636
cbxCompileExpressions->setChecked( mSettings->value( QStringLiteral( "/qgis/compileExpressions" ), true ).toBool() );
637637
cbxCreateRasterLegendIcons->setChecked( mSettings->value( QStringLiteral( "/qgis/createRasterLegendIcons" ), false ).toBool() );
638-
cbxTrustProject->setChecked( mSettings->value( QStringLiteral( "/qgis/trustProject" ), false ).toBool() );
639638

640639
mComboCopyFeatureFormat->addItem( tr( "Plain text, no geometry" ), QgsClipboard::AttributesOnly );
641640
mComboCopyFeatureFormat->addItem( tr( "Plain text, WKT geometry" ), QgsClipboard::AttributesWithWKT );
@@ -1226,7 +1225,6 @@ void QgsOptions::saveOptions()
12261225
mSettings->setValue( QStringLiteral( "/qgis/addPostgisDC" ), cbxAddPostgisDC->isChecked() );
12271226
mSettings->setValue( QStringLiteral( "/qgis/addOracleDC" ), cbxAddOracleDC->isChecked() );
12281227
mSettings->setValue( QStringLiteral( "/qgis/compileExpressions" ), cbxCompileExpressions->isChecked() );
1229-
mSettings->setValue( QStringLiteral( "/qgis/trustProject" ), cbxTrustProject->isChecked() );
12301228
mSettings->setValue( QStringLiteral( "/qgis/defaultLegendGraphicResolution" ), mLegendGraphicResolutionSpinBox->value() );
12311229
bool createRasterLegendIcons = mSettings->value( QStringLiteral( "/qgis/createRasterLegendIcons" ), false ).toBool();
12321230
mSettings->setValue( QStringLiteral( "/qgis/createRasterLegendIcons" ), cbxCreateRasterLegendIcons->isChecked() );

‎src/app/qgsprojectproperties.cpp

Lines changed: 2 additions & 0 deletions
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()->trust() );
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()->setTrust( mTrustProjectCheckBox->isChecked() );
767769

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

‎src/core/qgsproject.cpp

Lines changed: 28 additions & 3 deletions
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+
mTrust = false;
483484
mCustomVariables.clear();
484485

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

721722
// apply specific settings to vector layer
722-
QgsSettings s;
723-
bool trustProject = s.value( QStringLiteral( "/qgis/trustProject" ), false ).toBool();
724723
if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mapLayer ) )
725724
{
726-
vl->setReadExtentFromXml( trustProject );
725+
vl->setReadExtentFromXml( mTrust );
727726
}
728727
}
729728
else if ( type == QLatin1String( "raster" ) )
@@ -900,6 +899,14 @@ bool QgsProject::readProjectFile( const QString &filename )
900899
mEvaluateDefaultValues = true;
901900
}
902901

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+
mTrust = true;
908+
}
909+
903910
// read the layer tree from project file
904911

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

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

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

22692280
return defaultCrs;
22702281
}
2282+
2283+
void QgsProject::setTrust( bool trust )
2284+
{
2285+
mTrust = 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

Lines changed: 25 additions & 0 deletions
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 setTrust( 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 trust() const { return mTrust; }
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 mTrust = false;
10861111
};
10871112

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

‎src/core/qgsvectorlayer.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ QgsRectangle QgsVectorLayer::extent() const
803803
if ( !isSpatial() )
804804
return rect;
805805

806-
if ( !mValidExtent && mDataProvider && !mDataProvider->hasMetadata() && mReadExtentFromXml && !mXmlExtent.isNull() )
806+
807+
if ( !mValidExtent && mLazyExtent && mDataProvider && !mDataProvider->hasMetadata() && mReadExtentFromXml && !mXmlExtent.isNull() )
807808
{
808809
mExtent = mXmlExtent;
809810
mValidExtent = true;
@@ -1498,12 +1499,28 @@ void QgsVectorLayer::setDataSource( const QString &dataSource, const QString &ba
14981499
bool QgsVectorLayer::setDataProvider( QString const &provider )
14991500
{
15001501
mProviderKey = provider; // XXX is this necessary? Usually already set
1502+
1503+
// primary key unicity is tested at construction time, so it has to be set
1504+
// before initializing postgres provider
1505+
QString checkUnicityKey = QStringLiteral( "checkPrimaryKeyUnicity" );
1506+
QString dataSource = mDataSource;
1507+
if ( provider.compare( QLatin1String( "postgres" ) ) == 0 )
1508+
{
1509+
QgsDataSourceUri uri( dataSource );
1510+
1511+
if ( uri.hasParam( checkUnicityKey ) )
1512+
uri.removeParam( checkUnicityKey );
1513+
1514+
uri.setParam( checkUnicityKey, mReadExtentFromXml ? "0" : "1" );
1515+
dataSource = uri.uri( false );
1516+
}
1517+
15011518
// XXX when execution gets here.
15021519

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

15611578
// deal with unnecessary schema qualification to make v.in.ogr happy
1562-
mDataSource = mDataProvider->dataSourceUri();
1579+
// and remove unnecessary key
1580+
QgsDataSourceUri dataProviderUri( mDataProvider->dataSourceUri() );
1581+
if ( dataProviderUri.hasParam( checkUnicityKey ) )
1582+
dataProviderUri.removeParam( checkUnicityKey );
1583+
mDataSource = dataProviderUri.uri( false );
15631584
}
15641585
else if ( mProviderKey == QLatin1String( "osm" ) )
15651586
{

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri )
115115
mRequestedSrid = mUri.srid();
116116
mRequestedGeomType = mUri.wkbType();
117117

118+
if ( mUri.hasParam( QStringLiteral( "checkPrimaryKeyUnicity" ) ) )
119+
{
120+
if ( mUri.param( QStringLiteral( "checkPrimaryKeyUnicity" ) ).compare( "0" ) == 0 )
121+
{
122+
mCheckPrimaryKeyUnicity = false;
123+
}
124+
else
125+
{
126+
mCheckPrimaryKeyUnicity = true;
127+
}
128+
}
129+
118130
if ( mSchemaName.isEmpty() && mTableName.startsWith( '(' ) && mTableName.endsWith( ')' ) )
119131
{
120132
mIsQuery = true;
@@ -1325,9 +1337,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
13251337
}
13261338
else if ( type == Relkind::View || type == Relkind::MaterializedView )
13271339
{
1328-
QgsSettings settings;
1329-
bool checkPrimaryKeyUnicity = !settings.value( QStringLiteral( "/qgis/trustProject" ), false ).toBool();
1330-
determinePrimaryKeyFromUriKeyColumn( checkPrimaryKeyUnicity );
1340+
determinePrimaryKeyFromUriKeyColumn();
13311341
}
13321342
else
13331343
{
@@ -1456,7 +1466,7 @@ QStringList QgsPostgresProvider::parseUriKey( const QString &key )
14561466
return cols;
14571467
}
14581468

1459-
void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn( bool checkPrimaryKeyUnicity )
1469+
void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn()
14601470
{
14611471
QString primaryKey = mUri.keyColumn();
14621472
mPrimaryKeyType = PktUnknown;
@@ -1489,8 +1499,10 @@ void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn( bool checkPrimary
14891499
if ( !mPrimaryKeyAttrs.isEmpty() )
14901500
{
14911501
bool unique = true;
1492-
if ( checkPrimaryKeyUnicity )
1502+
if ( mCheckPrimaryKeyUnicity )
1503+
{
14931504
unique = uniqueData( primaryKey );
1505+
}
14941506

14951507
if ( mUseEstimatedMetadata || unique )
14961508
{

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
133133
* Fills mPrimaryKeyType and mPrimaryKeyAttrs
134134
* from mUri
135135
*/
136-
void determinePrimaryKeyFromUriKeyColumn( bool checkPrimaryKeyUnicity = true );
136+
void determinePrimaryKeyFromUriKeyColumn();
137137

138138
QgsFields fields() const override;
139139
QString dataComment() const override;
@@ -432,6 +432,8 @@ class QgsPostgresProvider : public QgsVectorDataProvider
432432
void setTransaction( QgsTransaction *transaction ) override;
433433

434434
QHash<int, QString> mDefaultValues;
435+
436+
bool mCheckPrimaryKeyUnicity = true;
435437
};
436438

437439

0 commit comments

Comments
 (0)
Please sign in to comment.