Skip to content

Commit 8b36ad5

Browse files
committed
Updated Qt connections to the new style qgis/QGIS-Enhancement-Proposals#77
Changed isDeleteStyleFromDBSupported to isDeleteStyleFromDbSupported
1 parent 738fbd4 commit 8b36ad5

10 files changed

+56
-52
lines changed

python/core/qgsvectordataprovider.sip

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,12 @@ class QgsVectorDataProvider : QgsDataProvider
408408
virtual bool isSaveAndLoadStyleToDBSupported() const;
409409

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

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

python/core/qgsvectorlayer.sip

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,11 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
685685
virtual QString getStyleFromDatabase( const QString& styleId, QString &msgError /Out/ );
686686

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

src/app/qgsloadstylefromdbdialog.cpp

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
2727
{
2828
setupUi( this );
2929
setWindowTitle( QStringLiteral( "Database styles manager" ) );
30-
mSelectedStyleId = QLatin1String( "" );
31-
mSelectedStyleName = QLatin1String( "" );
3230

3331
mLoadButton->setDisabled( true );
3432
mDeleteButton->setDisabled( true );
@@ -42,13 +40,13 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
4240
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
4341
mOthersTable->verticalHeader()->setVisible( false );
4442

45-
connect( mRelatedTable, SIGNAL( itemSelectionChanged() ), this, SLOT( relatedTableSelectionChanged() ) );
46-
connect( mOthersTable, SIGNAL( itemSelectionChanged() ), this, SLOT( otherTableSelectionChanged() ) );
47-
connect( mRelatedTable, SIGNAL( doubleClicked( QModelIndex ) ), this, SLOT( accept() ) );
48-
connect( mOthersTable, SIGNAL( doubleClicked( QModelIndex ) ), this, SLOT( accept() ) );
49-
connect( mCancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
50-
connect( mDeleteButton, SIGNAL( clicked() ), this, SLOT( deleteStyleFromDB() ) );
51-
connect( mLoadButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
43+
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged );
44+
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged );
45+
connect( mRelatedTable, &QTableWidget::doubleClicked, this, &QDialog::accept );
46+
connect( mOthersTable, &QTableWidget::doubleClicked, this, &QDialog::accept );
47+
connect( mCancelButton, &QPushButton::clicked, this, &QDialog::reject );
48+
connect( mLoadButton, &QPushButton::clicked, this, &QDialog::accept );
49+
connect( mDeleteButton, &QPushButton::clicked, this, &QgsLoadStyleFromDBDialog::deleteStyleFromDB );
5250

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

5856
QSettings settings;
5957
restoreGeometry( settings.value( QStringLiteral( "/Windows/loadStyleFromDb/geometry" ) ).toByteArray() );
60-
6158
}
6259

6360
QgsLoadStyleFromDBDialog::~QgsLoadStyleFromDBDialog()
@@ -110,40 +107,45 @@ QString QgsLoadStyleFromDBDialog::getSelectedStyleId()
110107
void QgsLoadStyleFromDBDialog::setLayer( QgsVectorLayer *l )
111108
{
112109
mLayer = l;
113-
if ( mLayer->dataProvider()->isDeleteStyleFromDBSupported() )
114-
{
115-
//QgsDebugMsg( "QgsLoadStyleFromDBDialog::setLayer → The dataProvider supports isDeleteStyleFromDBSupported" );
116-
mDeleteButton->setVisible( true );
117-
}
118-
else
119-
{
120-
// QgsDebugMsg( "QgsLoadStyleFromDBDialog::setLayer → The dataProvider does not supports isDeleteStyleFromDBSupported" );
121-
mDeleteButton->setVisible( false );
122-
}
110+
mDeleteButton->setVisible( mLayer->dataProvider()->isDeleteStyleFromDbSupported() );
123111
}
124112

125-
void QgsLoadStyleFromDBDialog::relatedTableSelectionChanged()
113+
void QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged()
126114
{
127115
selectionChanged( mRelatedTable );
128-
//deselect any other row on the other table widget
129-
QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
130-
mOthersTable->setRangeSelected( range, false );
116+
if ( mRelatedTable->selectionModel()->hasSelection() )
117+
{
118+
if ( mOthersTable->selectionModel()->hasSelection() )
119+
{
120+
disconnect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged );
121+
QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
122+
mOthersTable->setRangeSelected( range, false );
123+
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged );
124+
}
125+
}
131126
}
132127

133-
void QgsLoadStyleFromDBDialog::otherTableSelectionChanged()
128+
void QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged()
134129
{
135130
selectionChanged( mOthersTable );
136-
//deselect any other row on the other table widget
137-
QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
138-
mRelatedTable->setRangeSelected( range, false );
131+
if ( mOthersTable->selectionModel()->hasSelection() )
132+
{
133+
if ( mRelatedTable->selectionModel()->hasSelection() )
134+
{
135+
disconnect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged );
136+
QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
137+
mRelatedTable->setRangeSelected( range, false );
138+
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged );
139+
}
140+
}
139141
}
140142

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

146-
if ( selected.count() > 0 )
148+
if ( !selected.isEmpty() )
147149
{
148150
item = selected.at( 0 );
149151
mSelectedStyleName = item->text();
@@ -153,27 +155,24 @@ void QgsLoadStyleFromDBDialog::selectionChanged( QTableWidget *styleTable )
153155
}
154156
else
155157
{
156-
mSelectedStyleName = "";
157-
mSelectedStyleId = "";
158+
mSelectedStyleName.clear();
159+
mSelectedStyleId.clear();
158160
mLoadButton->setEnabled( false );
159161
mDeleteButton->setEnabled( false );
160162
}
161163
}
162164

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

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

173-
uri = mLayer->dataProvider()->dataSourceUri();
174-
// mLayer->dataProvider()->deleteStyleById( uri, mSelectedStyleId, msgError );
175175
mLayer->deleteStyleFromDatabase( mSelectedStyleId, msgError );
176-
177176
if ( !msgError.isNull() )
178177
{
179178
QgsDebugMsg( opInfo + " failed." );

src/app/qgsloadstylefromdbdialog.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class APP_EXPORT QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadS
2727
QString mSelectedStyleId;
2828
QString mSelectedStyleName;
2929
int mSectionLimit;
30-
QString qmlStyle;
3130
Q_OBJECT
3231
public:
3332
explicit QgsLoadStyleFromDBDialog( QWidget *parent = nullptr );
@@ -37,16 +36,15 @@ class APP_EXPORT QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadS
3736
void initializeLists( const QStringList& ids, const QStringList& names, const QStringList& descriptions, int sectionLimit );
3837
QString getSelectedStyleId();
3938
void selectionChanged( QTableWidget *styleTable );
40-
4139
void setLayer( QgsVectorLayer *l );
4240

4341
public slots:
44-
void relatedTableSelectionChanged();
45-
void otherTableSelectionChanged();
42+
void onRelatedTableSelectionChanged();
43+
void onOthersTableSelectionChanged();
4644
void deleteStyleFromDB();
4745

4846
private:
49-
QgsVectorLayer *mLayer;
47+
QgsVectorLayer *mLayer = nullptr;
5048

5149
};
5250

src/core/qgsvectordataprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ bool QgsVectorDataProvider::isSaveAndLoadStyleToDBSupported() const
695695
return false;
696696
}
697697

698-
bool QgsVectorDataProvider::isDeleteStyleFromDBSupported() const
698+
bool QgsVectorDataProvider::isDeleteStyleFromDbSupported() const
699699
{
700700
return false;
701701
}

src/core/qgsvectordataprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
474474
* It returns false by default.
475475
* Must be implemented by providers that support delete styles from db returning true
476476
*/
477-
virtual bool isDeleteStyleFromDBSupported() const;
477+
virtual bool isDeleteStyleFromDbSupported() const;
478478

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

src/core/qgsvectorlayer.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4343,22 +4343,19 @@ QString QgsVectorLayer::getStyleFromDatabase( const QString& styleId, QString &m
43434343

43444344
bool QgsVectorLayer::deleteStyleFromDatabase( const QString& styleId, QString &msgError )
43454345
{
4346-
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
4347-
QLibrary *myLib = pReg->providerLibrary( mProviderKey );
4346+
QLibrary *myLib = QgsProviderRegistry::instance()->providerLibrary( mProviderKey );
43484347
if ( !myLib )
43494348
{
43504349
msgError = QObject::tr( "Unable to load %1 provider" ).arg( mProviderKey );
43514350
return false;
43524351
}
43534352
deleteStyleById_t* deleteStyleByIdMethod = reinterpret_cast< deleteStyleById_t * >( cast_to_fptr( myLib->resolve( "deleteStyleById" ) ) );
4354-
43554353
if ( !deleteStyleByIdMethod )
43564354
{
43574355
delete myLib;
43584356
msgError = QObject::tr( "Provider %1 has no %2 method" ).arg( mProviderKey, QStringLiteral( "deleteStyleById" ) );
43594357
return false;
43604358
}
4361-
43624359
return deleteStyleByIdMethod( mDataSource, styleId, msgError );
43634360
}
43644361

src/core/qgsvectorlayer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
786786
virtual QString getStyleFromDatabase( const QString& styleId, QString &msgError );
787787

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

src/providers/postgres/qgspostgresprovider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
132132
virtual void enumValues( int index, QStringList& enumList ) const override;
133133
bool isValid() const override;
134134
virtual bool isSaveAndLoadStyleToDBSupported() const override { return true; }
135-
virtual bool isDeleteStyleFromDBSupported() const override { return true; }
135+
virtual bool isDeleteStyleFromDbSupported() const override { return true; }
136136
QgsAttributeList attributeIndexes() const override;
137137
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
138138
QString defaultValueClause( int fieldId ) const override;

tests/src/python/test_provider_postgres.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ def testStyle(self):
645645
vl = self.getEditableLayer()
646646
self.assertTrue(vl.isValid())
647647
self.assertTrue(vl.dataProvider().isSaveAndLoadStyleToDBSupported())
648-
self.assertTrue(vl.dataProvider().isDeleteStyleFromDBSupported())
648+
self.assertTrue(vl.dataProvider().isDeleteStyleFromDbSupported())
649649

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

0 commit comments

Comments
 (0)