Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement delete action
  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent e36c5e2 commit bca8973
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsauxiliarystorage.sip
Expand Up @@ -241,6 +241,14 @@ class QgsAuxiliaryStorage
:rtype: QgsAuxiliaryLayer
%End

static bool deleteTable( const QgsDataSourceUri &uri );
%Docstring
Removes a table from the auxiliary storage.

:return: true if the table is well deleted, false otherwise
:rtype: bool
%End

static QString extension();
%Docstring
Returns the extension used for auxiliary databases.
Expand Down
32 changes: 32 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -85,6 +85,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
, mOriginalSubsetSQL( lyr->subsetString() )
, mAuxiliaryLayerActionNew( nullptr )
, mAuxiliaryLayerActionClear( nullptr )
, mAuxiliaryLayerActionDelete( nullptr )
{
setupUi( this );
connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited );
Expand Down Expand Up @@ -366,6 +367,10 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
menu->addAction( mAuxiliaryLayerActionClear );
connect( mAuxiliaryLayerActionClear, &QAction::triggered, this, &QgsVectorLayerProperties::onAuxiliaryLayerClear );

mAuxiliaryLayerActionDelete = new QAction( tr( "Delete" ), this );
menu->addAction( mAuxiliaryLayerActionDelete );
connect( mAuxiliaryLayerActionDelete, &QAction::triggered, this, &QgsVectorLayerProperties::onAuxiliaryLayerDelete );

mAuxiliaryStorageActions->setMenu( menu );

updateAuxiliaryStoragePage();
Expand Down Expand Up @@ -1497,6 +1502,8 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )
mAuxiliaryStorageFeaturesLineEdit->setText( QString::number( features ) );

// update actions
mAuxiliaryLayerActionClear->setEnabled( true );
mAuxiliaryLayerActionDelete->setEnabled( true );
mAuxiliaryLayerActionNew->setEnabled( false );

const QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
Expand Down Expand Up @@ -1531,6 +1538,9 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )
mAuxiliaryStorageInformationGrpBox->setEnabled( false );
mAuxiliaryStorageFieldsGrpBox->setEnabled( false );

mAuxiliaryLayerActionClear->setEnabled( false );
mAuxiliaryLayerActionDelete->setEnabled( false );

if ( mLayer->isSpatial() )
mAuxiliaryLayerActionNew->setEnabled( true );

Expand Down Expand Up @@ -1580,3 +1590,25 @@ void QgsVectorLayerProperties::onAuxiliaryLayerClear()
mLayer->triggerRepaint();
}
}

void QgsVectorLayerProperties::onAuxiliaryLayerDelete()
{
QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
if ( !alayer )
return;

const QString msg = tr( "Are you sure you want to delete auxiliary storage for %1" ).arg( mLayer->name() );
QMessageBox::StandardButton reply;
reply = QMessageBox::question( this, "Delete auxiliary storage", msg, QMessageBox::Yes | QMessageBox::No );

if ( reply == QMessageBox::Yes )
{
QApplication::setOverrideCursor( Qt::WaitCursor );
QgsDataSourceUri uri( alayer->source() );
mLayer->setAuxiliaryLayer(); // remove auxiliary layer
QgsAuxiliaryStorage::deleteTable( uri );
QApplication::restoreOverrideCursor();
updateAuxiliaryStoragePage( true );
mLayer->triggerRepaint();
}
}
4 changes: 4 additions & 0 deletions src/app/qgsvectorlayerproperties.h
Expand Up @@ -157,8 +157,11 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
void updateFieldsPropertiesDialog();

void onAuxiliaryLayerNew();

void onAuxiliaryLayerClear();

void onAuxiliaryLayerDelete();

private:

void saveStyleAs( StyleType styleType );
Expand Down Expand Up @@ -224,6 +227,7 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private

QAction *mAuxiliaryLayerActionNew;
QAction *mAuxiliaryLayerActionClear;
QAction *mAuxiliaryLayerActionDelete;

private slots:
void openPanel( QgsPanelWidget *panel );
Expand Down
39 changes: 39 additions & 0 deletions src/core/qgsauxiliarystorage.cpp
Expand Up @@ -311,6 +311,45 @@ QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &fi
return alayer;
}

bool QgsAuxiliaryStorage::deleteTable( const QgsDataSourceUri &uri )
{
bool rc = false;

// parsing for ogr style uri :
// " filePath|layername='tableName' table="" sql="
QStringList uriParts = uri.uri().split( '|' );
if ( uriParts.count() < 2 )
return false;

const QString databasePath = uriParts[0].replace( ' ', "" );

const QString table = uriParts[1];
QStringList tableParts = table.split( ' ' );

if ( tableParts.count() < 1 )
return false;

const QString tableName = tableParts[0].replace( "layername=", "" );

if ( !databasePath.isEmpty() && !tableName.isEmpty() )
{
sqlite3 *handler = openDB( databasePath );

if ( handler )
{
QString sql = QString( "DROP TABLE %1" ).arg( tableName );
rc = exec( sql, handler );

sql = QString( "VACUUM" );
rc = exec( sql, handler );

close( handler );
}
}

return rc;
}

bool QgsAuxiliaryStorage::saveAs( const QString &filename ) const
{
if ( QFile::exists( filename ) )
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsauxiliarystorage.h
Expand Up @@ -259,6 +259,13 @@ class CORE_EXPORT QgsAuxiliaryStorage
*/
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, const QgsVectorLayer *layer ) const;

/**
* Removes a table from the auxiliary storage.
*
* \returns true if the table is well deleted, false otherwise
*/
static bool deleteTable( const QgsDataSourceUri &uri );

/**
* Returns the extension used for auxiliary databases.
*/
Expand Down

0 comments on commit bca8973

Please sign in to comment.