Skip to content

Commit

Permalink
Delete styles from postgres provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrocha committed Jan 26, 2017
1 parent b3e8d82 commit 2095b8e
Show file tree
Hide file tree
Showing 8 changed files with 496 additions and 236 deletions.
106 changes: 96 additions & 10 deletions src/app/qgsloadstylefromdbdialog.cpp
Expand Up @@ -25,10 +25,12 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
, mSectionLimit( 0 )
{
setupUi( this );
setWindowTitle( QStringLiteral( "Load style from database" ) );
setWindowTitle( QStringLiteral( "Saved styles manager" ) );
mSelectedStyleId = QLatin1String( "" );
mSelectedStyleName = QLatin1String( "" );

mLoadButton->setDisabled( true );
mDeleteButton->setDisabled( true );
mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
mRelatedTable->horizontalHeader()->setStretchLastSection( true );
mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );
Expand All @@ -39,16 +41,18 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
mOthersTable->verticalHeader()->setVisible( false );

connect( mRelatedTable, SIGNAL( cellClicked( int, int ) ), this, SLOT( cellSelectedRelatedTable( int ) ) );
connect( mOthersTable, SIGNAL( cellClicked( int, int ) ), this, SLOT( cellSelectedOthersTable( int ) ) );
connect( mRelatedTable, SIGNAL( itemSelectionChanged() ), this, SLOT( relatedTableSelectionChanged() ) );
connect( mOthersTable, SIGNAL( itemSelectionChanged() ), this, SLOT( otherTableSelectionChanged() ) );
connect( mRelatedTable, SIGNAL( doubleClicked( QModelIndex ) ), this, SLOT( accept() ) );
connect( mOthersTable, SIGNAL( doubleClicked( QModelIndex ) ), this, SLOT( accept() ) );
connect( mCancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
connect( mDeleteButton, SIGNAL( clicked() ), this, SLOT( deleteStyleFromDB() ) );
connect( mLoadButton, SIGNAL( clicked() ), this, SLOT( accept() ) );

setTabOrder( mRelatedTable, mOthersTable );
setTabOrder( mOthersTable, mCancelButton );
setTabOrder( mCancelButton, mLoadButton );
setTabOrder( mCancelButton, mDeleteButton );
setTabOrder( mDeleteButton, mLoadButton );

QSettings settings;
restoreGeometry( settings.value( QStringLiteral( "/Windows/loadStyleFromDb/geometry" ) ).toByteArray() );
Expand Down Expand Up @@ -102,14 +106,96 @@ QString QgsLoadStyleFromDBDialog::getSelectedStyleId()
return mSelectedStyleId;
}

void QgsLoadStyleFromDBDialog::cellSelectedRelatedTable( int r )
void QgsLoadStyleFromDBDialog::setLayer( QgsVectorLayer *l )
{
mLoadButton->setEnabled( true );
mSelectedStyleId = mRelatedTable->item( r, 0 )->data( Qt::UserRole ).toString();
mLayer = l;
if ( mLayer->dataProvider()->isDeleteStyleFromDBSupported() )
{
//QgsDebugMsg( "QgsLoadStyleFromDBDialog::setLayer → The dataProvider supports isDeleteStyleFromDBSupported" );
mDeleteButton->setVisible( true );
}
else
{
// QgsDebugMsg( "QgsLoadStyleFromDBDialog::setLayer → The dataProvider does not supports isDeleteStyleFromDBSupported" );
mDeleteButton->setVisible( false );
}
}

void QgsLoadStyleFromDBDialog::relatedTableSelectionChanged()
{
selectionChanged( mRelatedTable );
//deselect any other row on the other table widget
QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
mOthersTable->setRangeSelected( range, false );
}

void QgsLoadStyleFromDBDialog::cellSelectedOthersTable( int r )
void QgsLoadStyleFromDBDialog::otherTableSelectionChanged()
{
mLoadButton->setEnabled( true );
mSelectedStyleId = mOthersTable->item( r, 0 )->data( Qt::UserRole ).toString();
selectionChanged( mOthersTable );
//deselect any other row on the other table widget
QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
mRelatedTable->setRangeSelected( range, false );

}

void QgsLoadStyleFromDBDialog::selectionChanged( QTableWidget *styleTable )
{
QTableWidgetItem *item;
QList<QTableWidgetItem *> selected = styleTable->selectedItems();
//QgsDebugMsg( QString( "itemSelectionChanged(): count() = %1" ).arg( selected.count() ) );
if ( selected.count() > 0 )
{
item = selected.at( 0 );
mSelectedStyleName = item->text();
mSelectedStyleId = item->data( Qt::UserRole ).toString();
mLoadButton->setEnabled( true );
mDeleteButton->setEnabled( true );
}
else
{
mSelectedStyleName = "";
mSelectedStyleId = "";
mLoadButton->setEnabled( false );
mDeleteButton->setEnabled( false );
}
}

void QgsLoadStyleFromDBDialog::deleteStyleFromDB()
{
QString uri, msgError;
QString infoWindowTitle = QObject::tr( "Delete style %1 from %2" ).arg( mSelectedStyleName, mLayer->providerType() );
//QgsDebugMsg( QString( "Delete style: %1 " ).arg( mSelectedStyleName ) );

if ( QMessageBox::question( nullptr, QObject::tr( "Delete style" ),
QObject::tr( "Are you sure you want to delete the style %1?" ).arg( mSelectedStyleName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

uri = mLayer->dataProvider()->dataSourceUri();
mLayer->dataProvider()->deleteStyleById( uri, mSelectedStyleId, msgError );

if ( !msgError.isNull() )
{
QMessageBox::warning( this, infoWindowTitle, msgError );
}
else
{
QMessageBox::information( this, infoWindowTitle, tr( "Style deleted" ) );

//Delete all rows from the UI table widgets
mRelatedTable->setRowCount( 0 );
mOthersTable->setRowCount( 0 );

//Fill UI widgets again from DB. Other users might have change the styles meanwhile.
QString errorMsg;
QStringList ids, names, descriptions;
//get the list of styles in the db
int sectionLimit = mLayer->listStylesInDatabase( ids, names, descriptions, errorMsg );
if ( !errorMsg.isNull() )
{
QMessageBox::warning( this, tr( "Error occurred retrieving styles from database" ), errorMsg );
return;
}
initializeLists( ids, names, descriptions, sectionLimit );
}
}
12 changes: 10 additions & 2 deletions src/app/qgsloadstylefromdbdialog.h
Expand Up @@ -19,10 +19,13 @@
#include "ui_qgsloadstylefromdbdialog.h"
#include "qgisgui.h"
#include "qgis_app.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"

class APP_EXPORT QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadStyleFromDBDialogLayout
{
QString mSelectedStyleId;
QString mSelectedStyleName;
int mSectionLimit;
QString qmlStyle;
Q_OBJECT
Expand All @@ -33,12 +36,17 @@ class APP_EXPORT QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadS

void initializeLists( const QStringList& ids, const QStringList& names, const QStringList& descriptions, int sectionLimit );
QString getSelectedStyleId();
void selectionChanged( QTableWidget *styleTable );

void setLayer( QgsVectorLayer *l );

public slots:
void cellSelectedRelatedTable( int r );
void cellSelectedOthersTable( int r );
void relatedTableSelectionChanged();
void otherTableSelectionChanged();
void deleteStyleFromDB();

private:
QgsVectorLayer *mLayer;

};

Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -163,7 +163,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
//for loading
mLoadStyleMenu = new QMenu( this );
mLoadStyleMenu->addAction( tr( "Load from file..." ) );
mLoadStyleMenu->addAction( tr( "Load from database" ) );
mLoadStyleMenu->addAction( tr( "Saved styles manager" ) );
//mActionLoadStyle->setContextMenuPolicy( Qt::PreventContextMenu );
mActionLoadStyle->setMenu( mLoadStyleMenu );

Expand Down Expand Up @@ -1022,6 +1022,7 @@ void QgsVectorLayerProperties::showListOfStylesFromDatabase()
}

QgsLoadStyleFromDBDialog dialog;
dialog.setLayer( mLayer );
dialog.initializeLists( ids, names, descriptions, sectionLimit );

if ( dialog.exec() == QDialog::Accepted )
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -695,6 +695,16 @@ bool QgsVectorDataProvider::isSaveAndLoadStyleToDBSupported() const
return false;
}

bool QgsVectorDataProvider::isDeleteStyleFromDBSupported() const
{
return false;
}

void QgsVectorDataProvider::deleteStyleById( const QString& uri, QString styleId, QString& errCause )
{
return;
}

void QgsVectorDataProvider::pushError( const QString& msg ) const
{
QgsDebugMsg( msg );
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -469,6 +469,8 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
* Must be implemented by providers that support saving and loading styles to db returning true
*/
virtual bool isSaveAndLoadStyleToDBSupported() const;
virtual bool isDeleteStyleFromDBSupported() const;
virtual void deleteStyleById( const QString& uri, QString styleId, QString& errCause );

static QVariant convertValue( QVariant::Type type, const QString& value );

Expand Down

0 comments on commit 2095b8e

Please sign in to comment.