2002-07-26 16:48:31 +00:00
|
|
|
/***************************************************************************
|
2004-11-20 06:00:31 +00:00
|
|
|
qgsdbsourceselect.cpp - description
|
|
|
|
-------------------
|
|
|
|
begin : Sat Jun 22 2002
|
|
|
|
copyright : (C) 2002 by Gary E.Sherman
|
|
|
|
email : sherman at mrcc.com
|
2002-07-26 16:48:31 +00:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
2004-11-20 06:00:31 +00:00
|
|
|
/* $Id$ */
|
2002-07-06 00:33:05 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <qsettings.h>
|
2002-07-06 17:22:01 +00:00
|
|
|
#include <qpixmap.h>
|
2002-07-06 00:33:05 +00:00
|
|
|
#include <qlistbox.h>
|
2004-07-10 06:45:34 +00:00
|
|
|
#include <qlistview.h>
|
2002-07-06 00:33:05 +00:00
|
|
|
#include <qstringlist.h>
|
|
|
|
#include <qcombobox.h>
|
2003-06-10 04:46:28 +00:00
|
|
|
#include <qpushbutton.h>
|
2002-10-11 05:11:01 +00:00
|
|
|
#include <qmessagebox.h>
|
2002-10-12 21:17:11 +00:00
|
|
|
#include <qinputdialog.h>
|
2004-06-26 23:47:29 +00:00
|
|
|
#include <qgroupbox.h>
|
2002-07-12 04:04:29 +00:00
|
|
|
#include "xpm/point_layer.xpm"
|
|
|
|
#include "xpm/line_layer.xpm"
|
|
|
|
#include "xpm/polygon_layer.xpm"
|
2002-07-06 00:33:05 +00:00
|
|
|
#include "qgsdbsourceselect.h"
|
|
|
|
#include "qgsnewconnection.h"
|
2004-11-20 06:00:31 +00:00
|
|
|
#include "qgspgquerybuilder.h"
|
2004-01-28 01:58:52 +00:00
|
|
|
#include "qgisapp.h"
|
2004-02-13 06:04:13 +00:00
|
|
|
QgsDbSourceSelect::QgsDbSourceSelect(QgisApp * app, QWidget * parent, const char *name):QgsDbSourceSelectBase(parent, name),
|
2004-11-20 06:00:31 +00:00
|
|
|
qgisApp(app)
|
2002-08-03 04:01:26 +00:00
|
|
|
{
|
2004-02-13 06:04:13 +00:00
|
|
|
btnAdd->setEnabled(false);
|
|
|
|
populateConnectionList();
|
|
|
|
// connect the double-click signal to the addSingleLayer slot in the parent
|
2003-05-30 04:26:35 +00:00
|
|
|
|
2004-06-26 23:47:29 +00:00
|
|
|
//disable the 'where clause' box for 0.4 release
|
2004-11-20 06:00:31 +00:00
|
|
|
// groupBox3->hide();
|
2003-05-30 04:26:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QgsDbSourceSelect::~QgsDbSourceSelect()
|
|
|
|
{
|
2004-11-20 06:00:31 +00:00
|
|
|
PQfinish(pd);
|
2003-05-30 04:26:35 +00:00
|
|
|
}
|
2004-02-13 06:04:13 +00:00
|
|
|
void QgsDbSourceSelect::populateConnectionList()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QStringList keys = settings.subkeyList("/Qgis/connections");
|
|
|
|
QStringList::Iterator it = keys.begin();
|
|
|
|
cmbConnections->clear();
|
|
|
|
while (it != keys.end())
|
2004-11-20 06:00:31 +00:00
|
|
|
{
|
|
|
|
cmbConnections->insertItem(*it);
|
|
|
|
++it;
|
|
|
|
}
|
2002-07-06 00:33:05 +00:00
|
|
|
}
|
2002-08-13 04:47:18 +00:00
|
|
|
void QgsDbSourceSelect::addNewConnection()
|
2002-08-03 04:01:26 +00:00
|
|
|
{
|
|
|
|
|
2004-02-13 06:04:13 +00:00
|
|
|
QgsNewConnection *nc = new QgsNewConnection();
|
2002-08-03 04:01:26 +00:00
|
|
|
|
2004-02-13 06:04:13 +00:00
|
|
|
if (nc->exec())
|
2004-11-20 06:00:31 +00:00
|
|
|
{
|
|
|
|
populateConnectionList();
|
|
|
|
}
|
2002-07-06 00:33:05 +00:00
|
|
|
}
|
2002-08-13 04:47:18 +00:00
|
|
|
void QgsDbSourceSelect::editConnection()
|
2002-08-03 04:01:26 +00:00
|
|
|
{
|
|
|
|
|
2004-02-13 06:04:13 +00:00
|
|
|
QgsNewConnection *nc = new QgsNewConnection(cmbConnections->currentText());
|
2002-07-06 17:22:01 +00:00
|
|
|
|
2004-02-13 06:04:13 +00:00
|
|
|
if (nc->exec())
|
2004-11-20 06:00:31 +00:00
|
|
|
{
|
|
|
|
nc->saveConnection();
|
|
|
|
}
|
2002-07-06 17:22:01 +00:00
|
|
|
}
|
2003-05-30 04:26:35 +00:00
|
|
|
|
2004-02-13 06:04:13 +00:00
|
|
|
void QgsDbSourceSelect::deleteConnection()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QString key = "/Qgis/connections/" + cmbConnections->currentText();
|
|
|
|
QString msg =
|
|
|
|
tr("Are you sure you want to remove the ") + cmbConnections->currentText() + tr(" connection and all associated settings?");
|
|
|
|
int result = QMessageBox::information(this, tr("Confirm Delete"), msg, tr("Yes"), tr("No"));
|
|
|
|
if (result == 0)
|
2004-11-20 06:00:31 +00:00
|
|
|
{
|
|
|
|
settings.removeEntry(key + "/host");
|
|
|
|
settings.removeEntry(key + "/database");
|
|
|
|
settings.removeEntry(key + "/username");
|
|
|
|
settings.removeEntry(key + "/password");
|
|
|
|
//if(!success){
|
|
|
|
// QMessageBox::information(this,"Unable to Remove","Unable to remove the connection " + cmbConnections->currentText());
|
|
|
|
//}
|
|
|
|
cmbConnections->removeItem(cmbConnections->currentItem()); // populateConnectionList();
|
|
|
|
}
|
2003-05-30 04:26:35 +00:00
|
|
|
}
|
|
|
|
|
2002-08-13 04:47:18 +00:00
|
|
|
void QgsDbSourceSelect::addTables()
|
2002-08-03 04:01:26 +00:00
|
|
|
{
|
2004-02-13 06:04:13 +00:00
|
|
|
//store the table info
|
2004-07-10 06:45:34 +00:00
|
|
|
QListViewItemIterator it( lstTables );
|
|
|
|
while ( it.current() )
|
|
|
|
{
|
|
|
|
QListViewItem *item = it.current();
|
|
|
|
++it;
|
|
|
|
|
|
|
|
if ( item->isSelected() )
|
2004-02-13 06:04:13 +00:00
|
|
|
{
|
2004-07-10 06:45:34 +00:00
|
|
|
m_selectedTables += item->text(1) + " sql=" + item->text(2);
|
2004-02-13 06:04:13 +00:00
|
|
|
}
|
2004-07-10 06:45:34 +00:00
|
|
|
}
|
2003-06-10 04:46:28 +00:00
|
|
|
|
2004-07-10 06:45:34 +00:00
|
|
|
// BEGIN CHANGES ECOS
|
2004-02-13 06:04:13 +00:00
|
|
|
if (m_selectedTables.empty() == true)
|
|
|
|
QMessageBox::information(this, tr("Select Table"), tr("You must select a table in order to add a Layer."));
|
|
|
|
else
|
|
|
|
accept();
|
2004-07-10 06:45:34 +00:00
|
|
|
// END CHANGES ECOS
|
2002-07-07 03:48:55 +00:00
|
|
|
}
|
2002-08-03 04:01:26 +00:00
|
|
|
|
2002-08-13 04:47:18 +00:00
|
|
|
void QgsDbSourceSelect::dbConnect()
|
2002-08-03 04:01:26 +00:00
|
|
|
{
|
2004-02-13 06:04:13 +00:00
|
|
|
// populate the table list
|
|
|
|
QSettings settings;
|
|
|
|
|
|
|
|
QString key = "/Qgis/connections/" + cmbConnections->currentText();
|
|
|
|
QString connString = "host=";
|
|
|
|
QString host = settings.readEntry(key + "/host");
|
|
|
|
connString += host;
|
|
|
|
connString += " dbname=";
|
|
|
|
QString database = settings.readEntry(key + "/database");
|
2004-02-19 06:55:25 +00:00
|
|
|
connString += database + " port=";
|
|
|
|
QString port = settings.readEntry(key + "/port");
|
|
|
|
if(port.length() == 0){
|
2004-11-20 06:00:31 +00:00
|
|
|
port = "5432";
|
2004-02-19 06:55:25 +00:00
|
|
|
}
|
|
|
|
connString += port + " user=";
|
2004-02-13 06:04:13 +00:00
|
|
|
QString username = settings.readEntry(key + "/username");
|
|
|
|
connString += username;
|
|
|
|
QString password = settings.readEntry(key + "/password");
|
2002-10-12 21:17:11 +00:00
|
|
|
bool makeConnection = true;
|
2004-02-13 06:04:13 +00:00
|
|
|
if (password == QString::null)
|
2004-11-20 06:00:31 +00:00
|
|
|
{
|
|
|
|
// get password from user
|
|
|
|
makeConnection = false;
|
|
|
|
QString password = QInputDialog::getText(tr("Password for ") + database + "@" + host,
|
|
|
|
tr("Please enter your password:"),
|
|
|
|
QLineEdit::Password, QString::null, &makeConnection, this);
|
2004-02-13 06:04:13 +00:00
|
|
|
|
2004-11-20 06:00:31 +00:00
|
|
|
// allow null password entry in case its valid for the database
|
|
|
|
}
|
2004-02-13 06:04:13 +00:00
|
|
|
connString += " password=" + password;
|
2004-11-20 06:00:31 +00:00
|
|
|
#ifdef QGISDEBUG
|
2004-02-19 06:55:25 +00:00
|
|
|
std::cout << "Connection info: " << connString << std::endl;
|
2004-11-20 06:00:31 +00:00
|
|
|
#endif
|
2004-02-13 06:04:13 +00:00
|
|
|
if (makeConnection)
|
2004-11-20 06:00:31 +00:00
|
|
|
{
|
|
|
|
m_connInfo = connString; //host + " " + database + " " + username + " " + password;
|
|
|
|
//qDebug(m_connInfo);
|
|
|
|
pd = PQconnectdb((const char *) m_connInfo);
|
|
|
|
// std::cout << pd->ErrorMessage();
|
|
|
|
if (PQstatus(pd) == CONNECTION_OK)
|
2004-02-13 06:04:13 +00:00
|
|
|
{
|
2004-11-20 06:00:31 +00:00
|
|
|
// clear the existing entries
|
|
|
|
lstTables->clear();
|
|
|
|
// create the pixmaps for the layer types
|
|
|
|
QPixmap pxPoint;
|
|
|
|
pxPoint = QPixmap(point_layer_xpm);
|
|
|
|
QPixmap pxLine;
|
|
|
|
pxLine = QPixmap(line_layer_xpm);
|
|
|
|
QPixmap pxPoly;
|
|
|
|
pxPoly = QPixmap(polygon_layer_xpm);
|
|
|
|
//qDebug("Connection succeeded");
|
|
|
|
// get the list of tables
|
|
|
|
QString sql = "select * from geometry_columns";
|
|
|
|
// where f_table_schema ='" + settings.readEntry(key + "/database") + "'";
|
|
|
|
sql += " order by f_table_name";
|
|
|
|
//qDebug("Fetching tables using: " + sql);
|
|
|
|
PGresult *result = PQexec(pd, (const char *) sql);
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
QString msg;
|
|
|
|
QTextOStream(&msg) << "Fetched " << PQntuples(result) << " tables from database";
|
|
|
|
//qDebug(msg);
|
|
|
|
for (int idx = 0; idx < PQntuples(result); idx++)
|
2004-02-13 06:04:13 +00:00
|
|
|
{
|
2004-11-20 06:00:31 +00:00
|
|
|
QString v = "";
|
|
|
|
if (strlen(PQgetvalue(result, idx, PQfnumber(result, "f_table_catalog"))))
|
|
|
|
{
|
|
|
|
v += PQgetvalue(result, idx, PQfnumber(result, "f_table_catalog"));
|
|
|
|
v += ".";
|
|
|
|
}
|
|
|
|
if (strlen(PQgetvalue(result, idx, PQfnumber(result, "f_table_schema"))))
|
|
|
|
{
|
|
|
|
v += PQgetvalue(result, idx, PQfnumber(result, "f_table_schema"));
|
|
|
|
v += ".";
|
|
|
|
}
|
|
|
|
v += PQgetvalue(result, idx, PQfnumber(result, "f_table_name"));
|
|
|
|
v += " (";
|
|
|
|
v += PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column"));
|
|
|
|
v += ")";
|
2004-02-13 06:04:13 +00:00
|
|
|
|
2004-11-20 06:00:31 +00:00
|
|
|
QString type = PQgetvalue(result, idx, PQfnumber(result, "type"));
|
|
|
|
QPixmap *p;
|
|
|
|
if (type == "POINT" || type == "MULTIPOINT")
|
|
|
|
p = &pxPoint;
|
|
|
|
else if (type == "MULTIPOLYGON" || type == "POLYGON")
|
|
|
|
p = &pxPoly;
|
|
|
|
else if (type == "LINESTRING" || type == "MULTILINESTRING")
|
|
|
|
p = &pxLine;
|
|
|
|
else
|
|
|
|
p = 0;
|
|
|
|
if (p != 0)
|
|
|
|
{
|
|
|
|
QListViewItem *lItem = new QListViewItem(lstTables);
|
|
|
|
lItem->setText(1,v);
|
|
|
|
lItem->setPixmap(0,*p);
|
|
|
|
lstTables->insertItem(lItem);
|
|
|
|
}
|
2004-02-13 06:04:13 +00:00
|
|
|
}
|
2004-11-20 06:00:31 +00:00
|
|
|
// BEGIN CHANGES ECOS
|
|
|
|
if (cmbConnections->count() > 0)
|
|
|
|
btnAdd->setEnabled(true);
|
|
|
|
// END CHANGES ECOS
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
qDebug("Unable to get list of spatially enabled tables from geometry_columns table");
|
|
|
|
qDebug(PQerrorMessage(pd));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
QMessageBox::warning(this, tr("Connection failed"),
|
|
|
|
tr
|
2004-11-23 03:50:34 +00:00
|
|
|
("Connection to %1 on %2 failed. Either the database is down or your settings are incorrect.%3Check your username and password and try again.%4%5").
|
|
|
|
arg(settings.readEntry(key + "/database")).arg(settings.readEntry(key + "/host")).arg("\n\n").arg("\n\n").arg(PQerrorMessage(pd)));
|
2004-02-13 06:04:13 +00:00
|
|
|
}
|
2004-11-20 06:00:31 +00:00
|
|
|
}
|
2002-07-06 00:33:05 +00:00
|
|
|
}
|
2002-08-03 04:01:26 +00:00
|
|
|
|
2002-08-13 04:47:18 +00:00
|
|
|
QStringList QgsDbSourceSelect::selectedTables()
|
2002-08-03 04:01:26 +00:00
|
|
|
{
|
2004-02-13 06:04:13 +00:00
|
|
|
return m_selectedTables;
|
2002-07-07 03:48:55 +00:00
|
|
|
}
|
2002-08-03 04:01:26 +00:00
|
|
|
|
2002-08-13 04:47:18 +00:00
|
|
|
QString QgsDbSourceSelect::connInfo()
|
2002-08-03 04:01:26 +00:00
|
|
|
{
|
2004-02-13 06:04:13 +00:00
|
|
|
return m_connInfo;
|
2002-07-07 03:48:55 +00:00
|
|
|
}
|
2004-07-10 06:45:34 +00:00
|
|
|
void QgsDbSourceSelect::setSql(QListViewItem *item)
|
|
|
|
{
|
2004-11-20 08:13:15 +00:00
|
|
|
// Parse out the table name
|
|
|
|
QString table = item->text(1).left(item->text(1).find("("));
|
2004-11-20 06:00:31 +00:00
|
|
|
// create a query builder object
|
2004-11-20 08:13:15 +00:00
|
|
|
QgsPgQueryBuilder * pgb = new QgsPgQueryBuilder(table, pd, this);
|
2004-11-20 06:00:31 +00:00
|
|
|
// set the current sql in the query builder sql box
|
|
|
|
pgb->setSql(item->text(2));
|
|
|
|
// set the PG connection object so it can be used to fetch the
|
|
|
|
// fields for the table, get sample values, and test the query
|
|
|
|
pgb->setConnection(pd);
|
|
|
|
// show the dialog
|
|
|
|
if(pgb->exec())
|
|
|
|
{
|
|
|
|
// if user accepts, store the sql for the layer so it can be used
|
|
|
|
// if and when the layer is added to the map
|
|
|
|
item->setText(2, pgb->sql());
|
|
|
|
}
|
|
|
|
// delete the query builder object
|
|
|
|
delete pgb;
|
2004-07-10 06:45:34 +00:00
|
|
|
}
|
2004-02-13 06:04:13 +00:00
|
|
|
void QgsDbSourceSelect::addLayer(QListBoxItem * item)
|
|
|
|
{
|
2004-01-28 01:58:52 +00:00
|
|
|
qgisApp->addVectorLayer(m_connInfo, item->text(), "postgres");
|
2004-11-20 06:00:31 +00:00
|
|
|
// lstTables->setSelected(item, false);
|
2004-01-28 01:58:52 +00:00
|
|
|
}
|