Skip to content

Commit 2095b8e

Browse files
committedJan 26, 2017
Delete styles from postgres provider
1 parent b3e8d82 commit 2095b8e

8 files changed

+496
-236
lines changed
 

‎src/app/qgsloadstylefromdbdialog.cpp

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
2525
, mSectionLimit( 0 )
2626
{
2727
setupUi( this );
28-
setWindowTitle( QStringLiteral( "Load style from database" ) );
28+
setWindowTitle( QStringLiteral( "Saved styles manager" ) );
2929
mSelectedStyleId = QLatin1String( "" );
30+
mSelectedStyleName = QLatin1String( "" );
3031

3132
mLoadButton->setDisabled( true );
33+
mDeleteButton->setDisabled( true );
3234
mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
3335
mRelatedTable->horizontalHeader()->setStretchLastSection( true );
3436
mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );
@@ -39,16 +41,18 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
3941
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
4042
mOthersTable->verticalHeader()->setVisible( false );
4143

42-
connect( mRelatedTable, SIGNAL( cellClicked( int, int ) ), this, SLOT( cellSelectedRelatedTable( int ) ) );
43-
connect( mOthersTable, SIGNAL( cellClicked( int, int ) ), this, SLOT( cellSelectedOthersTable( int ) ) );
44+
connect( mRelatedTable, SIGNAL( itemSelectionChanged() ), this, SLOT( relatedTableSelectionChanged() ) );
45+
connect( mOthersTable, SIGNAL( itemSelectionChanged() ), this, SLOT( otherTableSelectionChanged() ) );
4446
connect( mRelatedTable, SIGNAL( doubleClicked( QModelIndex ) ), this, SLOT( accept() ) );
4547
connect( mOthersTable, SIGNAL( doubleClicked( QModelIndex ) ), this, SLOT( accept() ) );
4648
connect( mCancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
49+
connect( mDeleteButton, SIGNAL( clicked() ), this, SLOT( deleteStyleFromDB() ) );
4750
connect( mLoadButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
4851

4952
setTabOrder( mRelatedTable, mOthersTable );
5053
setTabOrder( mOthersTable, mCancelButton );
51-
setTabOrder( mCancelButton, mLoadButton );
54+
setTabOrder( mCancelButton, mDeleteButton );
55+
setTabOrder( mDeleteButton, mLoadButton );
5256

5357
QSettings settings;
5458
restoreGeometry( settings.value( QStringLiteral( "/Windows/loadStyleFromDb/geometry" ) ).toByteArray() );
@@ -102,14 +106,96 @@ QString QgsLoadStyleFromDBDialog::getSelectedStyleId()
102106
return mSelectedStyleId;
103107
}
104108

105-
void QgsLoadStyleFromDBDialog::cellSelectedRelatedTable( int r )
109+
void QgsLoadStyleFromDBDialog::setLayer( QgsVectorLayer *l )
106110
{
107-
mLoadButton->setEnabled( true );
108-
mSelectedStyleId = mRelatedTable->item( r, 0 )->data( Qt::UserRole ).toString();
111+
mLayer = l;
112+
if ( mLayer->dataProvider()->isDeleteStyleFromDBSupported() )
113+
{
114+
//QgsDebugMsg( "QgsLoadStyleFromDBDialog::setLayer → The dataProvider supports isDeleteStyleFromDBSupported" );
115+
mDeleteButton->setVisible( true );
116+
}
117+
else
118+
{
119+
// QgsDebugMsg( "QgsLoadStyleFromDBDialog::setLayer → The dataProvider does not supports isDeleteStyleFromDBSupported" );
120+
mDeleteButton->setVisible( false );
121+
}
122+
}
123+
124+
void QgsLoadStyleFromDBDialog::relatedTableSelectionChanged()
125+
{
126+
selectionChanged( mRelatedTable );
127+
//deselect any other row on the other table widget
128+
QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
129+
mOthersTable->setRangeSelected( range, false );
109130
}
110131

111-
void QgsLoadStyleFromDBDialog::cellSelectedOthersTable( int r )
132+
void QgsLoadStyleFromDBDialog::otherTableSelectionChanged()
112133
{
113-
mLoadButton->setEnabled( true );
114-
mSelectedStyleId = mOthersTable->item( r, 0 )->data( Qt::UserRole ).toString();
134+
selectionChanged( mOthersTable );
135+
//deselect any other row on the other table widget
136+
QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
137+
mRelatedTable->setRangeSelected( range, false );
138+
139+
}
140+
141+
void QgsLoadStyleFromDBDialog::selectionChanged( QTableWidget *styleTable )
142+
{
143+
QTableWidgetItem *item;
144+
QList<QTableWidgetItem *> selected = styleTable->selectedItems();
145+
//QgsDebugMsg( QString( "itemSelectionChanged(): count() = %1" ).arg( selected.count() ) );
146+
if ( selected.count() > 0 )
147+
{
148+
item = selected.at( 0 );
149+
mSelectedStyleName = item->text();
150+
mSelectedStyleId = item->data( Qt::UserRole ).toString();
151+
mLoadButton->setEnabled( true );
152+
mDeleteButton->setEnabled( true );
153+
}
154+
else
155+
{
156+
mSelectedStyleName = "";
157+
mSelectedStyleId = "";
158+
mLoadButton->setEnabled( false );
159+
mDeleteButton->setEnabled( false );
160+
}
161+
}
162+
163+
void QgsLoadStyleFromDBDialog::deleteStyleFromDB()
164+
{
165+
QString uri, msgError;
166+
QString infoWindowTitle = QObject::tr( "Delete style %1 from %2" ).arg( mSelectedStyleName, mLayer->providerType() );
167+
//QgsDebugMsg( QString( "Delete style: %1 " ).arg( mSelectedStyleName ) );
168+
169+
if ( QMessageBox::question( nullptr, QObject::tr( "Delete style" ),
170+
QObject::tr( "Are you sure you want to delete the style %1?" ).arg( mSelectedStyleName ),
171+
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
172+
return;
173+
174+
uri = mLayer->dataProvider()->dataSourceUri();
175+
mLayer->dataProvider()->deleteStyleById( uri, mSelectedStyleId, msgError );
176+
177+
if ( !msgError.isNull() )
178+
{
179+
QMessageBox::warning( this, infoWindowTitle, msgError );
180+
}
181+
else
182+
{
183+
QMessageBox::information( this, infoWindowTitle, tr( "Style deleted" ) );
184+
185+
//Delete all rows from the UI table widgets
186+
mRelatedTable->setRowCount( 0 );
187+
mOthersTable->setRowCount( 0 );
188+
189+
//Fill UI widgets again from DB. Other users might have change the styles meanwhile.
190+
QString errorMsg;
191+
QStringList ids, names, descriptions;
192+
//get the list of styles in the db
193+
int sectionLimit = mLayer->listStylesInDatabase( ids, names, descriptions, errorMsg );
194+
if ( !errorMsg.isNull() )
195+
{
196+
QMessageBox::warning( this, tr( "Error occurred retrieving styles from database" ), errorMsg );
197+
return;
198+
}
199+
initializeLists( ids, names, descriptions, sectionLimit );
200+
}
115201
}

‎src/app/qgsloadstylefromdbdialog.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
#include "ui_qgsloadstylefromdbdialog.h"
2020
#include "qgisgui.h"
2121
#include "qgis_app.h"
22+
#include "qgsvectorlayer.h"
23+
#include "qgsvectordataprovider.h"
2224

2325
class APP_EXPORT QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadStyleFromDBDialogLayout
2426
{
2527
QString mSelectedStyleId;
28+
QString mSelectedStyleName;
2629
int mSectionLimit;
2730
QString qmlStyle;
2831
Q_OBJECT
@@ -33,12 +36,17 @@ class APP_EXPORT QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadS
3336

3437
void initializeLists( const QStringList& ids, const QStringList& names, const QStringList& descriptions, int sectionLimit );
3538
QString getSelectedStyleId();
39+
void selectionChanged( QTableWidget *styleTable );
40+
41+
void setLayer( QgsVectorLayer *l );
3642

3743
public slots:
38-
void cellSelectedRelatedTable( int r );
39-
void cellSelectedOthersTable( int r );
44+
void relatedTableSelectionChanged();
45+
void otherTableSelectionChanged();
46+
void deleteStyleFromDB();
4047

4148
private:
49+
QgsVectorLayer *mLayer;
4250

4351
};
4452

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
163163
//for loading
164164
mLoadStyleMenu = new QMenu( this );
165165
mLoadStyleMenu->addAction( tr( "Load from file..." ) );
166-
mLoadStyleMenu->addAction( tr( "Load from database" ) );
166+
mLoadStyleMenu->addAction( tr( "Saved styles manager" ) );
167167
//mActionLoadStyle->setContextMenuPolicy( Qt::PreventContextMenu );
168168
mActionLoadStyle->setMenu( mLoadStyleMenu );
169169

@@ -1022,6 +1022,7 @@ void QgsVectorLayerProperties::showListOfStylesFromDatabase()
10221022
}
10231023

10241024
QgsLoadStyleFromDBDialog dialog;
1025+
dialog.setLayer( mLayer );
10251026
dialog.initializeLists( ids, names, descriptions, sectionLimit );
10261027

10271028
if ( dialog.exec() == QDialog::Accepted )

‎src/core/qgsvectordataprovider.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,16 @@ bool QgsVectorDataProvider::isSaveAndLoadStyleToDBSupported() const
695695
return false;
696696
}
697697

698+
bool QgsVectorDataProvider::isDeleteStyleFromDBSupported() const
699+
{
700+
return false;
701+
}
702+
703+
void QgsVectorDataProvider::deleteStyleById( const QString& uri, QString styleId, QString& errCause )
704+
{
705+
return;
706+
}
707+
698708
void QgsVectorDataProvider::pushError( const QString& msg ) const
699709
{
700710
QgsDebugMsg( msg );

‎src/core/qgsvectordataprovider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
469469
* Must be implemented by providers that support saving and loading styles to db returning true
470470
*/
471471
virtual bool isSaveAndLoadStyleToDBSupported() const;
472+
virtual bool isDeleteStyleFromDBSupported() const;
473+
virtual void deleteStyleById( const QString& uri, QString styleId, QString& errCause );
472474

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

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 361 additions & 223 deletions
Large diffs are not rendered by default.

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ 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; }
136+
void deleteStyleById( const QString& uri, QString styleId, QString& errCause );
135137
QgsAttributeList attributeIndexes() const override;
136138
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
137139
QString defaultValueClause( int fieldId ) const override;

‎src/ui/qgsloadstylefromdbdialog.ui

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@
8181
</property>
8282
</widget>
8383
</item>
84+
<item>
85+
<widget class="QPushButton" name="mDeleteButton">
86+
<property name="sizePolicy">
87+
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
88+
<horstretch>0</horstretch>
89+
<verstretch>0</verstretch>
90+
</sizepolicy>
91+
</property>
92+
<property name="text">
93+
<string>Delete Style</string>
94+
</property>
95+
</widget>
96+
</item>
8497
<item>
8598
<widget class="QPushButton" name="mLoadButton">
8699
<property name="sizePolicy">

0 commit comments

Comments
 (0)
Please sign in to comment.