Skip to content

Commit eb298d8

Browse files
committed
Add, edit and remove PG connections in browser dock
1 parent de9fb2c commit eb298d8

6 files changed

+91
-5
lines changed

src/app/qgsbrowserdockwidget.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
187187
}
188188
}
189189

190+
QList<QAction*> actions = item->actions();
191+
if ( !actions.isEmpty() )
192+
{
193+
if ( !menu->actions().isEmpty() )
194+
menu->addSeparator();
195+
// add action to the menu
196+
menu->addActions( actions );
197+
}
198+
190199
if ( menu->actions().count() == 0 )
191200
{
192201
delete menu;

src/core/qgsdataitem.h

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class CORE_EXPORT QgsDataItem : public QObject
7676

7777
virtual QWidget * paramWidget() { return 0; }
7878

79+
// list of actions provided by this item - usually used for popup menu on right-click
80+
virtual QList<QAction*> actions() { return QList<QAction*>(); }
81+
7982
//
8083

8184
enum Capability

src/providers/postgres/qgspgsourceselect.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,21 @@ void QgsPgSourceSelect::on_btnNew_clicked()
144144
// Slot for deleting an existing connection
145145
void QgsPgSourceSelect::on_btnDelete_clicked()
146146
{
147-
QSettings settings;
148-
QString key = "/Postgresql/connections/" + cmbConnections->currentText();
149147
QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
150148
.arg( cmbConnections->currentText() );
151149
if ( QMessageBox::Ok != QMessageBox::information( this, tr( "Confirm Delete" ), msg, QMessageBox::Ok | QMessageBox::Cancel ) )
152150
return;
153151

152+
QgsPgSourceSelect::deleteConnection( cmbConnections->currentText() );
153+
154+
populateConnectionList();
155+
emit connectionsChanged();
156+
}
157+
158+
void QgsPgSourceSelect::deleteConnection( QString name )
159+
{
160+
QString key = "/Postgresql/connections/" + name;
161+
QSettings settings;
154162
settings.remove( key + "/service" );
155163
settings.remove( key + "/host" );
156164
settings.remove( key + "/port" );
@@ -166,9 +174,6 @@ void QgsPgSourceSelect::on_btnDelete_clicked()
166174
settings.remove( key + "/savePassword" );
167175
settings.remove( key + "/save" );
168176
settings.remove( key );
169-
170-
populateConnectionList();
171-
emit connectionsChanged();
172177
}
173178

174179
void QgsPgSourceSelect::on_btnSave_clicked()

src/providers/postgres/qgspgsourceselect.h

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
105105

106106
public:
107107

108+
//! static function to delete a connection
109+
static void deleteConnection( QString key );
110+
111+
108112
//! Constructor
109113
QgsPgSourceSelect( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags, bool managerMode = false, bool embeddedMode = false );
110114
//! Destructor

src/providers/postgres/qgspostgresprovider.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -4455,6 +4455,41 @@ bool QgsPGConnectionItem::equal( const QgsDataItem *other )
44554455
const QgsPGConnectionItem *o = dynamic_cast<const QgsPGConnectionItem *>( other );
44564456
return ( mPath == o->mPath && mName == o->mName && mConnInfo == o->mConnInfo );
44574457
}
4458+
4459+
QList<QAction*> QgsPGConnectionItem::actions()
4460+
{
4461+
QList<QAction*> lst;
4462+
4463+
QAction* actionEdit = new QAction( tr( "Edit..." ), this );
4464+
connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) );
4465+
lst.append( actionEdit );
4466+
4467+
QAction* actionDelete = new QAction( tr( "Delete" ), this );
4468+
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
4469+
lst.append( actionDelete );
4470+
4471+
return lst;
4472+
}
4473+
4474+
#include "qgspgnewconnection.h"
4475+
void QgsPGConnectionItem::editConnection()
4476+
{
4477+
QgsPgNewConnection nc( NULL, mName );
4478+
if ( nc.exec() )
4479+
{
4480+
// the parent should be updated
4481+
mParent->refresh();
4482+
}
4483+
}
4484+
4485+
void QgsPGConnectionItem::deleteConnection()
4486+
{
4487+
QgsPgSourceSelect::deleteConnection( mName );
4488+
// the parent should be updated
4489+
mParent->refresh();
4490+
}
4491+
4492+
44584493
// ---------------------------------------------------------------------------
44594494
QgsPGLayerItem::QgsPGLayerItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperty )
44604495
: QgsLayerItem( parent, name, path, QString(), layerType, "postgres" ),
@@ -4543,6 +4578,17 @@ QVector<QgsDataItem*>QgsPGRootItem::createChildren()
45434578
return connections;
45444579
}
45454580

4581+
QList<QAction*> QgsPGRootItem::actions()
4582+
{
4583+
QList<QAction*> lst;
4584+
4585+
QAction* actionNew = new QAction( tr( "New..." ), this );
4586+
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
4587+
lst.append( actionNew );
4588+
4589+
return lst;
4590+
}
4591+
45464592
QWidget * QgsPGRootItem::paramWidget()
45474593
{
45484594
QgsPgSourceSelect *select = new QgsPgSourceSelect( 0, 0, true, true );
@@ -4554,6 +4600,15 @@ void QgsPGRootItem::connectionsChanged()
45544600
refresh();
45554601
}
45564602

4603+
void QgsPGRootItem::newConnection()
4604+
{
4605+
QgsPgNewConnection nc( NULL );
4606+
if ( nc.exec() )
4607+
{
4608+
refresh();
4609+
}
4610+
}
4611+
45574612
// ---------------------------------------------------------------------------
45584613

45594614
QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )

src/providers/postgres/qgspostgresprovider.h

+10
Original file line numberDiff line numberDiff line change
@@ -775,15 +775,22 @@ class QgsPostgresProvider : public QgsVectorDataProvider
775775

776776
class QgsPGConnectionItem : public QgsDataCollectionItem
777777
{
778+
Q_OBJECT
778779
public:
779780
QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path );
780781
~QgsPGConnectionItem();
781782

782783
QVector<QgsDataItem*> createChildren();
783784
virtual bool equal( const QgsDataItem *other );
784785

786+
virtual QList<QAction*> actions();
787+
785788
QString mConnInfo;
786789
QVector<QgsPostgresLayerProperty> mLayerProperties;
790+
791+
public slots:
792+
void editConnection();
793+
void deleteConnection();
787794
};
788795

789796
// WMS Layers may be nested, so that they may be both QgsDataCollectionItem and QgsLayerItem
@@ -822,8 +829,11 @@ class QgsPGRootItem : public QgsDataCollectionItem
822829

823830
virtual QWidget * paramWidget();
824831

832+
virtual QList<QAction*> actions();
833+
825834
public slots:
826835
void connectionsChanged();
836+
void newConnection();
827837
};
828838

829839
#endif

0 commit comments

Comments
 (0)