Skip to content

Commit bca8973

Browse files
committed
Implement delete action
1 parent e36c5e2 commit bca8973

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

python/core/qgsauxiliarystorage.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ class QgsAuxiliaryStorage
241241
:rtype: QgsAuxiliaryLayer
242242
%End
243243

244+
static bool deleteTable( const QgsDataSourceUri &uri );
245+
%Docstring
246+
Removes a table from the auxiliary storage.
247+
248+
:return: true if the table is well deleted, false otherwise
249+
:rtype: bool
250+
%End
251+
244252
static QString extension();
245253
%Docstring
246254
Returns the extension used for auxiliary databases.

src/app/qgsvectorlayerproperties.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
8585
, mOriginalSubsetSQL( lyr->subsetString() )
8686
, mAuxiliaryLayerActionNew( nullptr )
8787
, mAuxiliaryLayerActionClear( nullptr )
88+
, mAuxiliaryLayerActionDelete( nullptr )
8889
{
8990
setupUi( this );
9091
connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited );
@@ -366,6 +367,10 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
366367
menu->addAction( mAuxiliaryLayerActionClear );
367368
connect( mAuxiliaryLayerActionClear, &QAction::triggered, this, &QgsVectorLayerProperties::onAuxiliaryLayerClear );
368369

370+
mAuxiliaryLayerActionDelete = new QAction( tr( "Delete" ), this );
371+
menu->addAction( mAuxiliaryLayerActionDelete );
372+
connect( mAuxiliaryLayerActionDelete, &QAction::triggered, this, &QgsVectorLayerProperties::onAuxiliaryLayerDelete );
373+
369374
mAuxiliaryStorageActions->setMenu( menu );
370375

371376
updateAuxiliaryStoragePage();
@@ -1497,6 +1502,8 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )
14971502
mAuxiliaryStorageFeaturesLineEdit->setText( QString::number( features ) );
14981503

14991504
// update actions
1505+
mAuxiliaryLayerActionClear->setEnabled( true );
1506+
mAuxiliaryLayerActionDelete->setEnabled( true );
15001507
mAuxiliaryLayerActionNew->setEnabled( false );
15011508

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

1541+
mAuxiliaryLayerActionClear->setEnabled( false );
1542+
mAuxiliaryLayerActionDelete->setEnabled( false );
1543+
15341544
if ( mLayer->isSpatial() )
15351545
mAuxiliaryLayerActionNew->setEnabled( true );
15361546

@@ -1580,3 +1590,25 @@ void QgsVectorLayerProperties::onAuxiliaryLayerClear()
15801590
mLayer->triggerRepaint();
15811591
}
15821592
}
1593+
1594+
void QgsVectorLayerProperties::onAuxiliaryLayerDelete()
1595+
{
1596+
QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
1597+
if ( !alayer )
1598+
return;
1599+
1600+
const QString msg = tr( "Are you sure you want to delete auxiliary storage for %1" ).arg( mLayer->name() );
1601+
QMessageBox::StandardButton reply;
1602+
reply = QMessageBox::question( this, "Delete auxiliary storage", msg, QMessageBox::Yes | QMessageBox::No );
1603+
1604+
if ( reply == QMessageBox::Yes )
1605+
{
1606+
QApplication::setOverrideCursor( Qt::WaitCursor );
1607+
QgsDataSourceUri uri( alayer->source() );
1608+
mLayer->setAuxiliaryLayer(); // remove auxiliary layer
1609+
QgsAuxiliaryStorage::deleteTable( uri );
1610+
QApplication::restoreOverrideCursor();
1611+
updateAuxiliaryStoragePage( true );
1612+
mLayer->triggerRepaint();
1613+
}
1614+
}

src/app/qgsvectorlayerproperties.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,11 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
157157
void updateFieldsPropertiesDialog();
158158

159159
void onAuxiliaryLayerNew();
160+
160161
void onAuxiliaryLayerClear();
161162

163+
void onAuxiliaryLayerDelete();
164+
162165
private:
163166

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

225228
QAction *mAuxiliaryLayerActionNew;
226229
QAction *mAuxiliaryLayerActionClear;
230+
QAction *mAuxiliaryLayerActionDelete;
227231

228232
private slots:
229233
void openPanel( QgsPanelWidget *panel );

src/core/qgsauxiliarystorage.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,45 @@ QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &fi
311311
return alayer;
312312
}
313313

314+
bool QgsAuxiliaryStorage::deleteTable( const QgsDataSourceUri &uri )
315+
{
316+
bool rc = false;
317+
318+
// parsing for ogr style uri :
319+
// " filePath|layername='tableName' table="" sql="
320+
QStringList uriParts = uri.uri().split( '|' );
321+
if ( uriParts.count() < 2 )
322+
return false;
323+
324+
const QString databasePath = uriParts[0].replace( ' ', "" );
325+
326+
const QString table = uriParts[1];
327+
QStringList tableParts = table.split( ' ' );
328+
329+
if ( tableParts.count() < 1 )
330+
return false;
331+
332+
const QString tableName = tableParts[0].replace( "layername=", "" );
333+
334+
if ( !databasePath.isEmpty() && !tableName.isEmpty() )
335+
{
336+
sqlite3 *handler = openDB( databasePath );
337+
338+
if ( handler )
339+
{
340+
QString sql = QString( "DROP TABLE %1" ).arg( tableName );
341+
rc = exec( sql, handler );
342+
343+
sql = QString( "VACUUM" );
344+
rc = exec( sql, handler );
345+
346+
close( handler );
347+
}
348+
}
349+
350+
return rc;
351+
}
352+
314353
bool QgsAuxiliaryStorage::saveAs( const QString &filename ) const
315354
{
316355
if ( QFile::exists( filename ) )

src/core/qgsauxiliarystorage.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ class CORE_EXPORT QgsAuxiliaryStorage
259259
*/
260260
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, const QgsVectorLayer *layer ) const;
261261

262+
/**
263+
* Removes a table from the auxiliary storage.
264+
*
265+
* \returns true if the table is well deleted, false otherwise
266+
*/
267+
static bool deleteTable( const QgsDataSourceUri &uri );
268+
262269
/**
263270
* Returns the extension used for auxiliary databases.
264271
*/

0 commit comments

Comments
 (0)