Make setEditable() return true/false to indicate success/error

This commit is contained in:
Martin Dobias 2017-01-19 12:46:53 +08:00
parent f6f6ebdb44
commit 15cd8331ac
5 changed files with 27 additions and 17 deletions

View File

@ -242,13 +242,14 @@ class QgsRasterDataProvider : QgsDataProvider, QgsRasterInterface
/** Turns on/off editing mode of the provider. When in editing mode, it is possible
* to overwrite data of the provider using writeBlock() calls.
* @note Only some providers support editing mode and even those may fail to turn turn
* the underlying data source into editing mode, so it is necessery to check afterwards
* with isEditable() whether the operation was successful.
* @note Only some providers support editing mode and even those may fail to turn
* the underlying data source into editing mode, so it is necessery to check the return
* value whether the operation was successful.
* @returns true if the switch to/from editing mode was successful
* @see isEditable(), writeBlock()
* @note added in QGIS 3.0
*/
virtual void setEditable( bool enabled );
virtual bool setEditable( bool enabled );
/** Writes into the provider datasource*/
// TODO: add data type (may be defferent from band type)

View File

@ -360,13 +360,14 @@ class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRast
/** Turns on/off editing mode of the provider. When in editing mode, it is possible
* to overwrite data of the provider using writeBlock() calls.
* @note Only some providers support editing mode and even those may fail to turn turn
* the underlying data source into editing mode, so it is necessery to check afterwards
* with isEditable() whether the operation was successful.
* @note Only some providers support editing mode and even those may fail to turn
* the underlying data source into editing mode, so it is necessery to check the return
* value whether the operation was successful.
* @returns true if the switch to/from editing mode was successful
* @see isEditable(), writeBlock()
* @note added in QGIS 3.0
*/
virtual void setEditable( bool enabled ) { Q_UNUSED( enabled ); }
virtual bool setEditable( bool enabled ) { Q_UNUSED( enabled ); return false; }
//! Writes into the provider datasource
// TODO: add data type (may be defferent from band type)

View File

@ -2933,16 +2933,16 @@ bool QgsGdalProvider::isEditable() const
return mUpdate;
}
void QgsGdalProvider::setEditable( bool enabled )
bool QgsGdalProvider::setEditable( bool enabled )
{
if ( enabled == mUpdate )
return;
return false;
if ( !mValid )
return;
return false;
if ( mGdalDataset != mGdalBaseDataset )
return; // ignore the case of warped VRT for now (more complicated setup)
return false; // ignore the case of warped VRT for now (more complicated setup)
closeDataset();
@ -2954,12 +2954,13 @@ void QgsGdalProvider::setEditable( bool enabled )
{
QString msg = QStringLiteral( "Cannot reopen GDAL dataset %1:\n%2" ).arg( dataSourceUri(), QString::fromUtf8( CPLGetLastErrorMsg() ) );
appendError( ERRMSG( msg ) );
return;
return false;
}
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
mGdalDataset = mGdalBaseDataset;
mValid = true;
return true;
}
// pyramids resampling

View File

@ -149,7 +149,7 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase
static QMap<QString, QString> supportedMimes();
bool isEditable() const override;
void setEditable( bool enabled ) override;
bool setEditable( bool enabled ) override;
bool write( void* data, int band, int width, int height, int xOffset, int yOffset ) override;
bool setNoDataValue( int bandNo, double noDataValue ) override;

View File

@ -184,15 +184,22 @@ void TestQgsRasterBlock::testWrite()
res = rlayer->dataProvider()->writeBlock( block4, 1 );
QVERIFY( !res );
// make the provider editable
// some sanity checks
QVERIFY( !rlayer->dataProvider()->isEditable() );
rlayer->dataProvider()->setEditable( true );
res = rlayer->dataProvider()->setEditable( false );
QVERIFY( !res );
// make the provider editable
res = rlayer->dataProvider()->setEditable( true );
QVERIFY( res );
QVERIFY( rlayer->dataProvider()->isEditable() );
res = rlayer->dataProvider()->writeBlock( block4, 1 );
QVERIFY( res );
rlayer->dataProvider()->setEditable( false );
// finish the editing session
res = rlayer->dataProvider()->setEditable( false );
QVERIFY( res );
QVERIFY( !rlayer->dataProvider()->isEditable() );
// verify the change is there