Skip to content

Commit

Permalink
Make setEditable() return true/false to indicate success/error
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 19, 2017
1 parent f6f6ebd commit 15cd833
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
9 changes: 5 additions & 4 deletions python/core/raster/qgsrasterdataprovider.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions src/core/raster/qgsrasterdataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/providers/gdal/qgsgdalprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 10 additions & 3 deletions tests/src/core/testqgsrasterblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 15cd833

Please sign in to comment.