Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated Qt connections to the new style qgis/QGIS-Enhancement-Proposa…
…ls#77

Changed isDeleteStyleFromDBSupported to isDeleteStyleFromDbSupported
  • Loading branch information
jgrocha committed Feb 5, 2017
1 parent 738fbd4 commit 8b36ad5
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 52 deletions.
6 changes: 4 additions & 2 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -408,10 +408,12 @@ class QgsVectorDataProvider : QgsDataProvider
virtual bool isSaveAndLoadStyleToDBSupported() const;

/**
* It returns false by default.
* Checks if the provider supports style removal
* Must be implemented by providers that support delete styles from db returning true
* @note added in QGIS 3.0
* @return true if delete operation is supported by the provider
*/
virtual bool isDeleteStyleFromDBSupported() const;
virtual bool isDeleteStyleFromDbSupported() const;

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

Expand Down
6 changes: 5 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -685,7 +685,11 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
virtual QString getStyleFromDatabase( const QString& styleId, QString &msgError /Out/ );

/**
* Will delete the named style corresponding to style id provided from the database
* Delete a style from the database
* @note added in QGIS 3.0
* @param styleId the provider's layer_styles table id of the style to delete
* @param msgError reference to string that will be updated with any error messages
* @return true in case of success
*/
virtual bool deleteStyleFromDatabase( const QString& styleId, QString &msgError /Out/ );

Expand Down
69 changes: 34 additions & 35 deletions src/app/qgsloadstylefromdbdialog.cpp
Expand Up @@ -27,8 +27,6 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
{
setupUi( this );
setWindowTitle( QStringLiteral( "Database styles manager" ) );
mSelectedStyleId = QLatin1String( "" );
mSelectedStyleName = QLatin1String( "" );

mLoadButton->setDisabled( true );
mDeleteButton->setDisabled( true );
Expand All @@ -42,13 +40,13 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
mOthersTable->verticalHeader()->setVisible( false );

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() ) );
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged );
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged );
connect( mRelatedTable, &QTableWidget::doubleClicked, this, &QDialog::accept );
connect( mOthersTable, &QTableWidget::doubleClicked, this, &QDialog::accept );
connect( mCancelButton, &QPushButton::clicked, this, &QDialog::reject );
connect( mLoadButton, &QPushButton::clicked, this, &QDialog::accept );
connect( mDeleteButton, &QPushButton::clicked, this, &QgsLoadStyleFromDBDialog::deleteStyleFromDB );

setTabOrder( mRelatedTable, mOthersTable );
setTabOrder( mOthersTable, mCancelButton );
Expand All @@ -57,7 +55,6 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )

QSettings settings;
restoreGeometry( settings.value( QStringLiteral( "/Windows/loadStyleFromDb/geometry" ) ).toByteArray() );

}

QgsLoadStyleFromDBDialog::~QgsLoadStyleFromDBDialog()
Expand Down Expand Up @@ -110,40 +107,45 @@ QString QgsLoadStyleFromDBDialog::getSelectedStyleId()
void QgsLoadStyleFromDBDialog::setLayer( QgsVectorLayer *l )
{
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 );
}
mDeleteButton->setVisible( mLayer->dataProvider()->isDeleteStyleFromDbSupported() );
}

void QgsLoadStyleFromDBDialog::relatedTableSelectionChanged()
void QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged()
{
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 );
if ( mRelatedTable->selectionModel()->hasSelection() )
{
if ( mOthersTable->selectionModel()->hasSelection() )
{
disconnect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged );
QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
mOthersTable->setRangeSelected( range, false );
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged );
}
}
}

void QgsLoadStyleFromDBDialog::otherTableSelectionChanged()
void QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged()
{
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 );
if ( mOthersTable->selectionModel()->hasSelection() )
{
if ( mRelatedTable->selectionModel()->hasSelection() )
{
disconnect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged );
QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
mRelatedTable->setRangeSelected( range, false );
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged );
}
}
}

void QgsLoadStyleFromDBDialog::selectionChanged( QTableWidget *styleTable )
{
QTableWidgetItem *item;
QList<QTableWidgetItem *> selected = styleTable->selectedItems();

if ( selected.count() > 0 )
if ( !selected.isEmpty() )
{
item = selected.at( 0 );
mSelectedStyleName = item->text();
Expand All @@ -153,27 +155,24 @@ void QgsLoadStyleFromDBDialog::selectionChanged( QTableWidget *styleTable )
}
else
{
mSelectedStyleName = "";
mSelectedStyleId = "";
mSelectedStyleName.clear();
mSelectedStyleId.clear();
mLoadButton->setEnabled( false );
mDeleteButton->setEnabled( false );
}
}

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

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 );
mLayer->deleteStyleFromDatabase( mSelectedStyleId, msgError );

if ( !msgError.isNull() )
{
QgsDebugMsg( opInfo + " failed." );
Expand Down
8 changes: 3 additions & 5 deletions src/app/qgsloadstylefromdbdialog.h
Expand Up @@ -27,7 +27,6 @@ class APP_EXPORT QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadS
QString mSelectedStyleId;
QString mSelectedStyleName;
int mSectionLimit;
QString qmlStyle;
Q_OBJECT
public:
explicit QgsLoadStyleFromDBDialog( QWidget *parent = nullptr );
Expand All @@ -37,16 +36,15 @@ 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 relatedTableSelectionChanged();
void otherTableSelectionChanged();
void onRelatedTableSelectionChanged();
void onOthersTableSelectionChanged();
void deleteStyleFromDB();

private:
QgsVectorLayer *mLayer;
QgsVectorLayer *mLayer = nullptr;

};

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectordataprovider.cpp
Expand Up @@ -695,7 +695,7 @@ bool QgsVectorDataProvider::isSaveAndLoadStyleToDBSupported() const
return false;
}

bool QgsVectorDataProvider::isDeleteStyleFromDBSupported() const
bool QgsVectorDataProvider::isDeleteStyleFromDbSupported() const
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectordataprovider.h
Expand Up @@ -474,7 +474,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
* It returns false by default.
* Must be implemented by providers that support delete styles from db returning true
*/
virtual bool isDeleteStyleFromDBSupported() const;
virtual bool isDeleteStyleFromDbSupported() const;

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

Expand Down
5 changes: 1 addition & 4 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -4343,22 +4343,19 @@ QString QgsVectorLayer::getStyleFromDatabase( const QString& styleId, QString &m

bool QgsVectorLayer::deleteStyleFromDatabase( const QString& styleId, QString &msgError )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
QLibrary *myLib = QgsProviderRegistry::instance()->providerLibrary( mProviderKey );
if ( !myLib )
{
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
return false;
}
deleteStyleById_t* deleteStyleByIdMethod = reinterpret_cast< deleteStyleById_t * >( cast_to_fptr( myLib->resolve( "deleteStyleById" ) ) );

if ( !deleteStyleByIdMethod )
{
delete myLib;
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey, QStringLiteral( "deleteStyleById" ) );
return false;
}

return deleteStyleByIdMethod( mDataSource, styleId, msgError );
}

Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -786,7 +786,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
virtual QString getStyleFromDatabase( const QString& styleId, QString &msgError );

/**
* Will delete the named style corresponding to style id provided from the database
* Delete a style from the database
* @note added in QGIS 3.0
* @param styleId the provider's layer_styles table id of the style to delete
* @param msgError reference to string that will be updated with any error messages
* @return true in case of success
*/
virtual bool deleteStyleFromDatabase( const QString& styleId, QString &msgError );

Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -132,7 +132,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
virtual void enumValues( int index, QStringList& enumList ) const override;
bool isValid() const override;
virtual bool isSaveAndLoadStyleToDBSupported() const override { return true; }
virtual bool isDeleteStyleFromDBSupported() const override { return true; }
virtual bool isDeleteStyleFromDbSupported() const override { return true; }
QgsAttributeList attributeIndexes() const override;
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
QString defaultValueClause( int fieldId ) const override;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_provider_postgres.py
Expand Up @@ -645,7 +645,7 @@ def testStyle(self):
vl = self.getEditableLayer()
self.assertTrue(vl.isValid())
self.assertTrue(vl.dataProvider().isSaveAndLoadStyleToDBSupported())
self.assertTrue(vl.dataProvider().isDeleteStyleFromDBSupported())
self.assertTrue(vl.dataProvider().isDeleteStyleFromDbSupported())

# table layer_styles does not exit
related_count, idlist, namelist, desclist, errmsg = vl.listStylesInDatabase()
Expand Down

0 comments on commit 8b36ad5

Please sign in to comment.