Skip to content
Permalink
Browse files
Add, edit and remove PG connections in browser dock
  • Loading branch information
wonder-sk committed Oct 11, 2011
1 parent de9fb2c commit eb298d8f3b63d4b2fa1fe2538c994547e9b843b6
@@ -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;
@@ -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
@@ -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" );
@@ -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()
@@ -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
@@ -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" ),
@@ -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 );
@@ -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 )
@@ -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
@@ -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.