Skip to content
Permalink
Browse files

Implement delete action

  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent e36c5e2 commit bca8973465974a26deb804717dd7b7993cbafc4f
@@ -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.
@@ -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 );
@@ -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();
@@ -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();
@@ -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 );

@@ -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();
}
}
@@ -157,8 +157,11 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
void updateFieldsPropertiesDialog();

void onAuxiliaryLayerNew();

void onAuxiliaryLayerClear();

void onAuxiliaryLayerDelete();

private:

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

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

private slots:
void openPanel( QgsPanelWidget *panel );
@@ -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 ) )
@@ -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.
*/

0 comments on commit bca8973

Please sign in to comment.
You can’t perform that action at this time.