[QGIS Commit] r8223 - trunk/qgis/src/app
svn_qgis at osgeo.org
svn_qgis at osgeo.org
Fri Mar 14 11:17:30 EDT 2008
Author: jef
Date: 2008-03-14 11:17:29 -0400 (Fri, 14 Mar 2008)
New Revision: 8223
Modified:
trunk/qgis/src/app/qgsdbsourceselect.cpp
Log:
qgsdbsourceselect.cpp reindented
Modified: trunk/qgis/src/app/qgsdbsourceselect.cpp
===================================================================
--- trunk/qgis/src/app/qgsdbsourceselect.cpp 2008-03-14 14:09:18 UTC (rev 8222)
+++ trunk/qgis/src/app/qgsdbsourceselect.cpp 2008-03-14 15:17:29 UTC (rev 8223)
@@ -66,7 +66,7 @@
mSearchGroupBox->hide();
connect(mTablesTreeView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(setSql(const QModelIndex&)));
- //for Qt < 4.3.2, passing -1 to include all model columns
+ //for Qt < 4.3.2, passing -1 to include all model columns
//in search does not seem to work
mSearchColumnComboBox->setCurrentIndex(2);
}
@@ -109,53 +109,53 @@
void QgsDbSourceSelect::on_mSearchOptionsButton_clicked()
{
if(mSearchGroupBox->isVisible())
- {
- mSearchGroupBox->hide();
- }
+ {
+ mSearchGroupBox->hide();
+ }
else
- {
- mSearchGroupBox->show();
- }
+ {
+ mSearchGroupBox->show();
+ }
}
void QgsDbSourceSelect::on_mSearchTableEdit_textChanged(const QString & text)
{
if(mSearchModeComboBox->currentText() == tr("Wildcard"))
- {
- mProxyModel._setFilterWildcard(text);
- }
+ {
+ mProxyModel._setFilterWildcard(text);
+ }
else if(mSearchModeComboBox->currentText() == tr("RegExp"))
- {
- mProxyModel._setFilterRegExp(text);
- }
+ {
+ mProxyModel._setFilterRegExp(text);
+ }
}
void QgsDbSourceSelect::on_mSearchColumnComboBox_currentIndexChanged(const QString & text)
{
if(text == tr("All"))
- {
- mProxyModel.setFilterKeyColumn(-1);
- }
+ {
+ mProxyModel.setFilterKeyColumn(-1);
+ }
else if(text == tr("Schema"))
- {
- mProxyModel.setFilterKeyColumn(0);
- }
+ {
+ mProxyModel.setFilterKeyColumn(0);
+ }
else if(text == tr("Table"))
- {
- mProxyModel.setFilterKeyColumn(1);
- }
+ {
+ mProxyModel.setFilterKeyColumn(1);
+ }
else if(text == tr("Type"))
- {
- mProxyModel.setFilterKeyColumn(2);
- }
+ {
+ mProxyModel.setFilterKeyColumn(2);
+ }
else if(text == tr("Geometry column"))
- {
- mProxyModel.setFilterKeyColumn(3);
- }
+ {
+ mProxyModel.setFilterKeyColumn(3);
+ }
else if(text == tr("Sql"))
- {
- mProxyModel.setFilterKeyColumn(4);
- }
+ {
+ mProxyModel.setFilterKeyColumn(4);
+ }
}
void QgsDbSourceSelect::on_mSearchModeComboBox_currentIndexChanged(const QString & text)
@@ -169,24 +169,24 @@
{
mTableModel.setGeometryTypesForTable(schema, table, column, type);
mTablesTreeView->sortByColumn(1, Qt::AscendingOrder);
- mTablesTreeView->sortByColumn(0, Qt::AscendingOrder);
+ mTablesTreeView->sortByColumn(0, Qt::AscendingOrder);
}
QString QgsDbSourceSelect::makeGeomQuery(QString schema,
QString table, QString column)
{
return QString("select distinct "
- "case"
- " when geometrytype(%1) IN ('POINT','MULTIPOINT') THEN 'POINT'"
- " when geometrytype(%1) IN ('LINESTRING','MULTILINESTRING') THEN 'LINESTRING'"
- " when geometrytype(%1) IN ('POLYGON','MULTIPOLYGON') THEN 'POLYGON'"
- " end "
- "from \"%2\".\"%3\"").arg("\""+column+"\"").arg(schema).arg(table);
+ "case"
+ " when geometrytype(%1) IN ('POINT','MULTIPOINT') THEN 'POINT'"
+ " when geometrytype(%1) IN ('LINESTRING','MULTILINESTRING') THEN 'LINESTRING'"
+ " when geometrytype(%1) IN ('POLYGON','MULTIPOLYGON') THEN 'POLYGON'"
+ " end "
+ "from \"%2\".\"%3\"").arg("\""+column+"\"").arg(schema).arg(table);
}
QgsDbSourceSelect::~QgsDbSourceSelect()
{
- PQfinish(pd);
+ PQfinish(pd);
}
void QgsDbSourceSelect::populateConnectionList()
{
@@ -212,7 +212,6 @@
}
void QgsDbSourceSelect::editConnection()
{
-
QgsNewConnection *nc = new QgsNewConnection(this, cmbConnections->currentText());
if (nc->exec())
@@ -257,87 +256,87 @@
QModelIndexList::const_iterator selected_it = selectedIndices.constBegin();
for(; selected_it != selectedIndices.constEnd(); ++selected_it)
+ {
+ if(!selected_it->parent().isValid())
{
- if(!selected_it->parent().isValid())
- {
- //top level items only contain the schema names
- continue;
- }
- currentItem = mTableModel.itemFromIndex(mProxyModel.mapToSource(*selected_it));
- if(!currentItem)
- {
- continue;
- }
-
- QString currentSchemaName = currentItem->parent()->text();
-
- int currentRow = currentItem->row();
- int currentColumn = currentItem->column();
+ //top level items only contain the schema names
+ continue;
+ }
+ currentItem = mTableModel.itemFromIndex(mProxyModel.mapToSource(*selected_it));
+ if(!currentItem)
+ {
+ continue;
+ }
- if(dbInfo[currentSchemaName][currentRow].size() == 0)
- {
- dbInfo[currentSchemaName][currentRow].resize(5);
- }
+ QString currentSchemaName = currentItem->parent()->text();
- dbInfo[currentSchemaName][currentRow][currentColumn] = currentItem->text();
+ int currentRow = currentItem->row();
+ int currentColumn = currentItem->column();
+
+ if(dbInfo[currentSchemaName][currentRow].size() == 0)
+ {
+ dbInfo[currentSchemaName][currentRow].resize(5);
}
-
+
+ dbInfo[currentSchemaName][currentRow][currentColumn] = currentItem->text();
+ }
+
//now traverse all the schemas and table infos
QString schemaName, tableName, geomColumnName, sql;
QString query;
QMap<QString, schemaInfo>::const_iterator schema_it = dbInfo.constBegin();
for(; schema_it != dbInfo.constEnd(); ++schema_it)
+ {
+ schemaInfo scheme = schema_it.value();
+ schemaInfo::const_iterator entry_it = scheme.constBegin();
+ for(; entry_it != scheme.constEnd(); ++entry_it)
{
- schemaInfo scheme = schema_it.value();
- schemaInfo::const_iterator entry_it = scheme.constBegin();
- for(; entry_it != scheme.constEnd(); ++entry_it)
- {
- schemaName = entry_it->at(0);
- tableName = entry_it->at(1);
- geomColumnName = entry_it->at(3);
- sql = entry_it->at(4);
-
- if(geomColumnName.contains(" AS "))
- {
- int a = geomColumnName.find(" AS ");
- QString typeName = geomColumnName.mid(a+4); //only the type name
- geomColumnName = geomColumnName.mid(0, a); //only the geom column name
-
- if(!sql.isEmpty())
- {
- sql += " AND ";
- }
- if( typeName=="POINT" )
- {
- sql += QString("GeometryType(\"%1\") IN ('POINT','MULTIPOINT')").arg(geomColumnName);
- }
- else if(typeName=="LINESTRING")
- {
- sql += QString("GeometryType(\"%1\") IN ('LINESTRING','MULTILINESTRING')").arg(geomColumnName);
- }
- else if(typeName=="POLYGON")
- {
- sql += QString("GeometryType(\"%1\") IN ('POLYGON','MULTIPOLYGON')").arg(geomColumnName);
- }
- else
- {
- continue;
- }
- }
- query = "\"" + schemaName + "\".\"" + tableName + "\" " + "(" + geomColumnName + ") sql=" + sql;
- m_selectedTables.push_back(query);
- }
+ schemaName = entry_it->at(0);
+ tableName = entry_it->at(1);
+ geomColumnName = entry_it->at(3);
+ sql = entry_it->at(4);
+
+ if(geomColumnName.contains(" AS "))
+ {
+ int a = geomColumnName.find(" AS ");
+ QString typeName = geomColumnName.mid(a+4); //only the type name
+ geomColumnName = geomColumnName.mid(0, a); //only the geom column name
+
+ if(!sql.isEmpty())
+ {
+ sql += " AND ";
+ }
+ if( typeName=="POINT" )
+ {
+ sql += QString("GeometryType(\"%1\") IN ('POINT','MULTIPOINT')").arg(geomColumnName);
+ }
+ else if(typeName=="LINESTRING")
+ {
+ sql += QString("GeometryType(\"%1\") IN ('LINESTRING','MULTILINESTRING')").arg(geomColumnName);
+ }
+ else if(typeName=="POLYGON")
+ {
+ sql += QString("GeometryType(\"%1\") IN ('POLYGON','MULTIPOLYGON')").arg(geomColumnName);
+ }
+ else
+ {
+ continue;
+ }
+ }
+ query = "\"" + schemaName + "\".\"" + tableName + "\" " + "(" + geomColumnName + ") sql=" + sql;
+ m_selectedTables.push_back(query);
}
+ }
if(m_selectedTables.empty())
- {
- QMessageBox::information(this, tr("Select Table"), tr("You must select a table in order to add a Layer."));
- }
+ {
+ QMessageBox::information(this, tr("Select Table"), tr("You must select a table in order to add a Layer."));
+ }
else
- {
- accept();
- }
+ {
+ accept();
+ }
}
void QgsDbSourceSelect::on_btnConnect_clicked()
@@ -366,17 +365,17 @@
// get password from user
makeConnection = false;
password = QInputDialog::getText(tr("Password for ") + username,
- tr("Please enter your password:"),
- QLineEdit::Password, QString::null, &makeConnection, this);
+ tr("Please enter your password:"),
+ QLineEdit::Password, QString::null, &makeConnection, this);
// allow null password entry in case its valid for the database
}
QgsDataSourceURI uri;
uri.setConnection( settings.readEntry(key + "/host"),
- settings.readEntry(key + "/port"),
- database,
- settings.readEntry(key + "/username"),
- password );
+ settings.readEntry(key + "/port"),
+ database,
+ settings.readEntry(key + "/username"),
+ password );
bool searchPublicOnly = settings.readBoolEntry(key + "/publicOnly");
bool searchGeometryColumnsOnly = settings.readBoolEntry(key + "/geometryColumnsOnly");
@@ -410,13 +409,13 @@
// may take a long time to return
if (mColumnTypeThread != NULL)
{
- connect(mColumnTypeThread, SIGNAL(setLayerType(QString,QString,QString,QString)),
- this, SLOT(setLayerType(QString,QString,QString,QString)));
- connect(this, SIGNAL(finished()),
- mColumnTypeThread, SLOT(stop()) );
+ connect(mColumnTypeThread, SIGNAL(setLayerType(QString,QString,QString,QString)),
+ this, SLOT(setLayerType(QString,QString,QString,QString)));
+ connect(this, SIGNAL(finished()),
+ mColumnTypeThread, SLOT(stop()) );
- // Do it in a thread.
- mColumnTypeThread->start();
+ // Do it in a thread.
+ mColumnTypeThread->start();
}
}
else
@@ -432,9 +431,9 @@
else
{
QMessageBox::warning(this, tr("Connection failed"),
- tr
- ("Connection to %1 on %2 failed. Either the database is down or your settings are incorrect.%3Check your username and password and try again.%4The database said:%5%6").
- arg(settings.readEntry(key + "/database")).arg(settings.readEntry(key + "/host")).arg("\n\n").arg("\n\n").arg("\n").arg(QString::fromLocal8Bit(PQerrorMessage(pd))));
+ tr
+ ("Connection to %1 on %2 failed. Either the database is down or your settings are incorrect.%3Check your username and password and try again.%4The database said:%5%6").
+ arg(settings.readEntry(key + "/database")).arg(settings.readEntry(key + "/host")).arg("\n\n").arg("\n\n").arg("\n").arg(QString::fromLocal8Bit(PQerrorMessage(pd))));
}
}
@@ -444,13 +443,13 @@
//if we have only one schema item, expand it by default
int numTopLevelItems = mTableModel.invisibleRootItem()->rowCount();
if(numTopLevelItems < 2 || mTableModel.tableCount() < 20)
+ {
+ //expand all the toplevel items
+ for(int i = 0; i < numTopLevelItems; ++i)
{
- //expand all the toplevel items
- for(int i = 0; i < numTopLevelItems; ++i)
- {
- mTablesTreeView->expand(mProxyModel.mapFromSource(mTableModel.indexFromItem(mTableModel.invisibleRootItem()->child(i))));
- }
+ mTablesTreeView->expand(mProxyModel.mapFromSource(mTableModel.indexFromItem(mTableModel.invisibleRootItem()->child(i))));
}
+ }
}
QStringList QgsDbSourceSelect::selectedTables()
@@ -466,23 +465,23 @@
void QgsDbSourceSelect::setSql(const QModelIndex& index)
{
if(!index.parent().isValid())
- {
- qWarning("schema item found");
- return;
- }
+ {
+ qWarning("schema item found");
+ return;
+ }
if(pd == 0)
- {
- return;
- }
-
+ {
+ return;
+ }
+
//create "Schema"."Table" and find out existing sql string
QModelIndex schemaSibling = index.sibling(index.row(), 0);
QModelIndex tableSibling = index.sibling(index.row(), 1);
if(!schemaSibling.isValid() || !tableSibling.isValid())
- {
- return;
- }
+ {
+ return;
+ }
QString schemaName = mTableModel.itemFromIndex(mProxyModel.mapToSource(schemaSibling))->text();
QString tableName = mTableModel.itemFromIndex(mProxyModel.mapToSource(tableSibling))->text();
@@ -492,9 +491,9 @@
QString currentSql;
QModelIndex sqlSibling = index.sibling(index.row(), 4);
if(sqlSibling.isValid())
- {
- currentSql = mTableModel.itemFromIndex(mProxyModel.mapToSource(sqlSibling))->text();
- }
+ {
+ currentSql = mTableModel.itemFromIndex(mProxyModel.mapToSource(sqlSibling))->text();
+ }
// create a query builder object
QgsPgQueryBuilder * pgb = new QgsPgQueryBuilder(tableString, pd, this);
@@ -525,15 +524,15 @@
{
bool ok = false;
QApplication::setOverrideCursor(Qt::waitCursor);
-
- // The following query returns only tables that exist and the user has SELECT privilege on.
- // Can't use regclass here because table must exist, else error occurs.
- QString sql = "select * from geometry_columns,pg_class,pg_namespace "
- "where relname=f_table_name and f_table_schema=nspname "
- "and pg_namespace.oid = pg_class.relnamespace "
- "and has_table_privilege('\"'||pg_namespace.nspname||'\".\"'||pg_class.relname||'\"','select')" // user has select privilege
- "order by f_table_schema,f_table_name";
+ // The following query returns only tables that exist and the user has SELECT privilege on.
+ // Can't use regclass here because table must exist, else error occurs.
+ QString sql = "select * from geometry_columns,pg_class,pg_namespace "
+ "where relname=f_table_name and f_table_schema=nspname "
+ "and pg_namespace.oid = pg_class.relnamespace "
+ "and has_table_privilege('\"'||pg_namespace.nspname||'\".\"'||pg_class.relname||'\"','select')" // user has select privilege
+ "order by f_table_schema,f_table_name";
+
PGresult *result = PQexec(pg, sql.toUtf8());
if (result)
{
@@ -559,15 +558,15 @@
PQclear(result);
//search for geometry columns in tables that are not in the geometry_columns metatable
-
+
QApplication::restoreOverrideCursor();
if (searchGeometryColumnsOnly)
- {
- return ok;
- }
+ {
+ return ok;
+ }
- // Now have a look for geometry columns that aren't in the
+ // Now have a look for geometry columns that aren't in the
// geometry_columns table. This code is specific to postgresql,
// but an equivalent query should be possible in other
// databases.
@@ -577,14 +576,14 @@
"and pg_attribute.atttypid = regtype('geometry') "
"and pg_attribute.attrelid = pg_class.oid "
"and has_table_privilege('\"'||pg_namespace.nspname||'\".\"'||pg_class.relname||'\"','select') ";
- // user has select privilege
+ // user has select privilege
if (searchPublicOnly)
sql += "and pg_namespace.nspname = 'public' ";
sql += "and pg_namespace.nspname||'.'||pg_class.relname not in " // needs to be table and schema
"(select f_table_schema||'.'||f_table_name from geometry_columns) "
"and pg_class.relkind in ('v', 'r')"; // only from views and relations (tables)
-
+
result = PQexec(pg, sql.toUtf8());
for (int i = 0; i < PQntuples(result); i++)
@@ -613,7 +612,7 @@
#if 0 // this function is never called - smizuno
bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
- geomCol& details, bool searchGeometryColumnsOnly,
+ geomCol& details, bool searchGeometryColumnsOnly,
bool searchPublicOnly)
{
bool ok = false;
@@ -642,22 +641,22 @@
QString schemaName = QString::fromUtf8(PQgetvalue(result, idx, PQfnumber(result, "f_table_schema")));
sql = "select oid from pg_class where relname = '" + tableName + "'";
if (schemaName.length() > 0)
- sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
- schemaName + "')";
+ sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
+ schemaName + "')";
PGresult* exists = PQexec(pg, sql.toUtf8());
if (PQntuples(exists) == 1)
{
QString column = QString::fromUtf8(PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column")));
QString type = QString::fromUtf8(PQgetvalue(result, idx, PQfnumber(result, "type")));
-
- QString as = "";
- if(type=="GEOMETRY" && !searchGeometryColumnsOnly) {
- addSearchGeometryColumn(schemaName, tableName, column);
- as=type="WAITING";
- }
- details.push_back(geomPair(fullDescription(schemaName, tableName, column, as), type));
+ QString as = "";
+ if(type=="GEOMETRY" && !searchGeometryColumnsOnly) {
+ addSearchGeometryColumn(schemaName, tableName, column);
+ as=type="WAITING";
+ }
+
+ details.push_back(geomPair(fullDescription(schemaName, tableName, column, as), type));
}
PQclear(exists);
}
@@ -686,7 +685,7 @@
"(select f_table_name from geometry_columns) "
"and pg_namespace.oid = pg_class.relnamespace "
"and pg_class.relkind in ('v', 'r')"; // only from views and relations (tables)
-
+
result = PQexec(pg, sql.toUtf8());
for (int i = 0; i < PQntuples(result); i++)
@@ -718,8 +717,9 @@
{
QgsContextHelp::run(context_id);
}
+
QString QgsDbSourceSelect::fullDescription(QString schema, QString table,
- QString column, QString type)
+ QString column, QString type)
{
QString full_desc = "";
if (schema.length() > 0)
@@ -727,12 +727,13 @@
full_desc += table + "\" (" + column + ") " + type;
return full_desc;
}
+
void QgsDbSourceSelect::dbChanged()
{
// Remember which database was selected.
QSettings settings;
settings.writeEntry("/PostgreSQL/connections/selected",
- cmbConnections->currentText());
+ cmbConnections->currentText());
}
void QgsDbSourceSelect::setConnectionListPosition()
@@ -749,29 +750,27 @@
set = true;
break;
}
- // If we couldn't find the stored item, but there are some,
- // default to the last item (this makes some sense when deleting
- // items as it allows the user to repeatidly click on delete to
- // remove a whole lot of items).
- if (!set && cmbConnections->count() > 0)
- {
- // If toSelect is null, then the selected connection wasn't found
- // by QSettings, which probably means that this is the first time
- // the user has used qgis with database connections, so default to
- // the first in the list of connetions. Otherwise default to the last.
- if (toSelect.isNull())
- cmbConnections->setCurrentItem(0);
- else
- cmbConnections->setCurrentItem(cmbConnections->count()-1);
- }
+ // If we couldn't find the stored item, but there are some,
+ // default to the last item (this makes some sense when deleting
+ // items as it allows the user to repeatidly click on delete to
+ // remove a whole lot of items).
+ if (!set && cmbConnections->count() > 0)
+ {
+ // If toSelect is null, then the selected connection wasn't found
+ // by QSettings, which probably means that this is the first time
+ // the user has used qgis with database connections, so default to
+ // the first in the list of connetions. Otherwise default to the last.
+ if (toSelect.isNull())
+ cmbConnections->setCurrentItem(0);
+ else
+ cmbConnections->setCurrentItem(cmbConnections->count()-1);
+ }
}
void QgsDbSourceSelect::setSearchExpression(const QString& regexp)
{
-
}
-
void QgsGeomColumnTypeThread::setConnInfo(QString s)
{
mConnInfo = s;
@@ -801,20 +800,20 @@
for (uint i = 0; i<schemas.size(); i++)
{
QString query = QgsDbSourceSelect::makeGeomQuery(schemas[i],
- tables[i],
- columns[i]);
+ tables[i],
+ columns[i]);
PGresult* gresult = PQexec(pd, query.toUtf8());
QString type;
if (PQresultStatus(gresult) == PGRES_TUPLES_OK) {
- QStringList types;
+ QStringList types;
- for(int j=0; j<PQntuples(gresult); j++) {
- QString type = QString::fromUtf8(PQgetvalue(gresult, j, 0));
- if(type!="")
- types += type;
- }
+ for(int j=0; j<PQntuples(gresult); j++) {
+ QString type = QString::fromUtf8(PQgetvalue(gresult, j, 0));
+ if(type!="")
+ types += type;
+ }
- type = types.join(",");
+ type = types.join(",");
}
PQclear(gresult);
More information about the QGIS-commit
mailing list