Skip to content

Commit

Permalink
Add, edit and remove PG connections in browser dock
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Oct 11, 2011
1 parent de9fb2c commit eb298d8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/app/qgsbrowserdockwidget.cpp
Expand Up @@ -187,6 +187,15 @@ void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
}
}

QList<QAction*> actions = item->actions();
if ( !actions.isEmpty() )
{
if ( !menu->actions().isEmpty() )
menu->addSeparator();
// add action to the menu
menu->addActions( actions );
}

if ( menu->actions().count() == 0 )
{
delete menu;
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsdataitem.h
Expand Up @@ -76,6 +76,9 @@ class CORE_EXPORT QgsDataItem : public QObject

virtual QWidget * paramWidget() { return 0; }

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

//

enum Capability
Expand Down
15 changes: 10 additions & 5 deletions src/providers/postgres/qgspgsourceselect.cpp
Expand Up @@ -144,13 +144,21 @@ void QgsPgSourceSelect::on_btnNew_clicked()
// Slot for deleting an existing connection
void QgsPgSourceSelect::on_btnDelete_clicked()
{
QSettings settings;
QString key = "/Postgresql/connections/" + cmbConnections->currentText();
QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
.arg( cmbConnections->currentText() );
if ( QMessageBox::Ok != QMessageBox::information( this, tr( "Confirm Delete" ), msg, QMessageBox::Ok | QMessageBox::Cancel ) )
return;

QgsPgSourceSelect::deleteConnection( cmbConnections->currentText() );

populateConnectionList();
emit connectionsChanged();
}

void QgsPgSourceSelect::deleteConnection( QString name )
{
QString key = "/Postgresql/connections/" + name;
QSettings settings;
settings.remove( key + "/service" );
settings.remove( key + "/host" );
settings.remove( key + "/port" );
Expand All @@ -166,9 +174,6 @@ void QgsPgSourceSelect::on_btnDelete_clicked()
settings.remove( key + "/savePassword" );
settings.remove( key + "/save" );
settings.remove( key );

populateConnectionList();
emit connectionsChanged();
}

void QgsPgSourceSelect::on_btnSave_clicked()
Expand Down
4 changes: 4 additions & 0 deletions src/providers/postgres/qgspgsourceselect.h
Expand Up @@ -105,6 +105,10 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase

public:

//! static function to delete a connection
static void deleteConnection( QString key );


//! Constructor
QgsPgSourceSelect( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags, bool managerMode = false, bool embeddedMode = false );
//! Destructor
Expand Down
55 changes: 55 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -4455,6 +4455,41 @@ bool QgsPGConnectionItem::equal( const QgsDataItem *other )
const QgsPGConnectionItem *o = dynamic_cast<const QgsPGConnectionItem *>( other );
return ( mPath == o->mPath && mName == o->mName && mConnInfo == o->mConnInfo );
}

QList<QAction*> QgsPGConnectionItem::actions()
{
QList<QAction*> lst;

QAction* actionEdit = new QAction( tr( "Edit..." ), this );
connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) );
lst.append( actionEdit );

QAction* actionDelete = new QAction( tr( "Delete" ), this );
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
lst.append( actionDelete );

return lst;
}

#include "qgspgnewconnection.h"
void QgsPGConnectionItem::editConnection()
{
QgsPgNewConnection nc( NULL, mName );
if ( nc.exec() )
{
// the parent should be updated
mParent->refresh();
}
}

void QgsPGConnectionItem::deleteConnection()
{
QgsPgSourceSelect::deleteConnection( mName );
// the parent should be updated
mParent->refresh();
}


// ---------------------------------------------------------------------------
QgsPGLayerItem::QgsPGLayerItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperty )
: QgsLayerItem( parent, name, path, QString(), layerType, "postgres" ),
Expand Down Expand Up @@ -4543,6 +4578,17 @@ QVector<QgsDataItem*>QgsPGRootItem::createChildren()
return connections;
}

QList<QAction*> QgsPGRootItem::actions()
{
QList<QAction*> lst;

QAction* actionNew = new QAction( tr( "New..." ), this );
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
lst.append( actionNew );

return lst;
}

QWidget * QgsPGRootItem::paramWidget()
{
QgsPgSourceSelect *select = new QgsPgSourceSelect( 0, 0, true, true );
Expand All @@ -4554,6 +4600,15 @@ void QgsPGRootItem::connectionsChanged()
refresh();
}

void QgsPGRootItem::newConnection()
{
QgsPgNewConnection nc( NULL );
if ( nc.exec() )
{
refresh();
}
}

// ---------------------------------------------------------------------------

QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
Expand Down
10 changes: 10 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -775,15 +775,22 @@ class QgsPostgresProvider : public QgsVectorDataProvider

class QgsPGConnectionItem : public QgsDataCollectionItem
{
Q_OBJECT
public:
QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path );
~QgsPGConnectionItem();

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

virtual QList<QAction*> actions();

QString mConnInfo;
QVector<QgsPostgresLayerProperty> mLayerProperties;

public slots:
void editConnection();
void deleteConnection();
};

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

virtual QWidget * paramWidget();

virtual QList<QAction*> actions();

public slots:
void connectionsChanged();
void newConnection();
};

#endif

0 comments on commit eb298d8

Please sign in to comment.