Skip to content

Commit 4a2f93c

Browse files
author
jef
committed
updated postgis layer selection, fixed #218, again
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7618 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 2c45a16 commit 4a2f93c

File tree

3 files changed

+122
-130
lines changed

3 files changed

+122
-130
lines changed

src/app/qgsdbsourceselect.cpp

+106-124
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ QgsDbSourceSelect::QgsDbSourceSelect(QgisApp *app, Qt::WFlags fl)
108108
lstTables->horizontalHeader()->setStretchLastSection(true);
109109
// Set the column count
110110
lstTables->setColumnCount(dbssColumns);
111-
mColumnLabels += "";
112111
mColumnLabels += tr("Type");
113-
mColumnLabels += tr("Import As");
114112
mColumnLabels += tr("Name");
115113
mColumnLabels += tr("Sql");
116114
lstTables->setHorizontalHeaderLabels(mColumnLabels);
@@ -158,33 +156,42 @@ void QgsDbSourceSelect::on_cmbConnections_activated(int)
158156
dbChanged();
159157
}
160158

161-
void QgsDbSourceSelect::updateImportAsInfo(int row, const QString &type)
159+
void QgsDbSourceSelect::updateTypeInfo(int row, QString type)
162160
{
163-
if(type=="WAITING")
164-
return;
165-
166-
QComboBox *cb = static_cast<QComboBox *>(lstTables->cellWidget(row,dbssImport));
161+
QComboBox *cb = static_cast<QComboBox *>(lstTables->cellWidget(row,dbssType));
167162
if(cb)
168-
lstTables->removeCellWidget(row, dbssImportAs);
163+
lstTables->removeCellWidget(row, dbssType);
169164
else
170165
{
171-
QTableWidgetItem *item = lstTables->takeItem(row,dbssImportAs);
166+
QTableWidgetItem *item = lstTables->takeItem(row,dbssType);
172167
delete item;
173168
}
174-
175-
if( type=="GEOMETRY")
169+
170+
if( type.contains(",") )
176171
{
172+
QStringList types = type.split(",");
173+
177174
cb = new QComboBox(lstTables);
178-
cb->addItem( mLayerIcons.value("POINT").second, mLayerIcons.value("POINT").first);
179-
cb->addItem( mLayerIcons.value("LINESTRING").second, mLayerIcons.value("LINESTRING").first);
180-
cb->addItem( mLayerIcons.value("POLYGON").second, mLayerIcons.value("POLYGON").first);
175+
for(int i=0; i<types.size(); i++) {
176+
cb->addItem( mLayerIcons.value(types[i]).second, mLayerIcons.value(types[i]).first);
177+
}
181178
cb->setCurrentIndex(0);
182179
cb->setToolTip( tr("select import type for multi type layer") );
183-
lstTables->setCellWidget(row, dbssImportAs, cb);
180+
cb->setMinimumContentsLength(mCbMinLength);
181+
cb->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
182+
lstTables->setCellWidget(row, dbssType, cb);
184183
}
185184
else
186185
{
187-
lstTables->setItem(row, dbssImportAs, new QTableWidgetItem(*lstTables->item(row,dbssType)));
186+
if (!mLayerIcons.contains(type))
187+
type="UNKNOWN";
188+
189+
QTableWidgetItem *iconItem = new QTableWidgetItem();
190+
iconItem->setIcon( mLayerIcons.value(type).second );
191+
iconItem->setToolTip( mLayerIcons.value(type).first );
192+
lstTables->setItem(row, dbssType, iconItem);
193+
194+
lstTables->setItem(row, dbssType, new QTableWidgetItem(*lstTables->item(row,dbssType)));
188195
}
189196
}
190197

@@ -198,44 +205,25 @@ void QgsDbSourceSelect::setLayerType(QString schema,
198205
// was put into the Name column.
199206
QString full_desc = fullDescription(schema, table, column);
200207

201-
QList<QTableWidgetItem*> ii = lstTables->findItems(full_desc,
202-
Qt::MatchExactly);
208+
QList<QTableWidgetItem*> ii = lstTables->findItems(full_desc, Qt::MatchExactly);
203209

204210
if (ii.count() > 0)
205211
{
206-
int row = lstTables->row(ii.at(0)); // just use the first one
207-
QTableWidgetItem* iconItem = lstTables->item(row, dbssType);
208-
209-
// Get the icon and tooltip text
210-
const QIcon* p;
211-
QString toolTipText;
212-
if (mLayerIcons.contains(type))
213-
{
214-
p = &(mLayerIcons.value(type).second);
215-
toolTipText = mLayerIcons.value(type).first;
216-
}
217-
else
218-
{
219-
QgsDebugMsg("Unknown geometry type of '" + type + "'.");
220-
p = &(mLayerIcons.value("UNKNOWN").second);
221-
toolTipText = mLayerIcons.value("UNKNOWN").first;
222-
}
223-
224-
iconItem->setIcon(*p);
225-
iconItem->setToolTip(toolTipText);
226-
227-
updateImportAsInfo(row, type);
212+
updateTypeInfo( lstTables->row(ii.at(0)), type);
213+
lstTables->resizeColumnToContents(dbssType);
228214
}
229215
}
230216

231217
QString QgsDbSourceSelect::makeGeomQuery(QString schema,
232218
QString table, QString column)
233219
{
234-
QString query = "select GeometryType(\"" + column + "\") from ";
235-
if (schema.length() > 0)
236-
query += "\"" + schema + "\".";
237-
query += "\"" + table + "\" where \"" + column + "\" is not null limit 1";
238-
return query;
220+
return QString("select distinct "
221+
"case"
222+
" when geometrytype(%1) IN ('POINT','MULTIPOINT') THEN 'POINT'"
223+
" when geometrytype(%1) IN ('LINESTRING','MULTILINESTRING') THEN 'LINESTRING'"
224+
" when geometrytype(%1) IN ('POLYGON','MULTIPOLYGON') THEN 'POLYGON'"
225+
" end "
226+
"from \"%2\".\"%3\"").arg(column).arg(schema).arg(table);
239227
}
240228

241229
QgsDbSourceSelect::~QgsDbSourceSelect()
@@ -303,29 +291,38 @@ void QgsDbSourceSelect::addTables()
303291
{
304292
//store the table info
305293

306-
for (int i = 0; i < lstTables->rowCount(); ++i)
294+
for (int i = 0; i < lstTables->rowCount();)
307295
{
308-
if ( static_cast<QCheckBox *>(lstTables->cellWidget(i, dbssImport))->isChecked() )
296+
if ( lstTables->isItemSelected(lstTables->item(i, dbssDetail)) )
309297
{
310298
QString table = lstTables->item(i,dbssDetail)->text();
311299
QString query = table + " sql=";
312300

313-
QComboBox *cb = static_cast<QComboBox *>( lstTables->cellWidget(i, dbssImportAs) );
301+
QComboBox *cb = static_cast<QComboBox *>( lstTables->cellWidget(i, dbssType) );
314302
if(cb) {
315303
int i = table.find("(");
316304
int j = table.find(")");
317305
QString column = table.mid(i+1,j-i-1);
306+
QString type;
318307

319-
switch(cb->currentIndex() ) {
320-
case 0:
308+
QMapIterator <QString, QPair<QString, QIcon>> it(mLayerIcons);
309+
while( it.hasNext() ) {
310+
it.next();
311+
312+
if( it.value().first == cb->currentText() ) {
313+
type=it.key();
314+
break;
315+
}
316+
}
317+
318+
if( type=="POINT" ) {
321319
query += QString("GeometryType(\"%1\") IN ('POINT','MULTIPOINT')").arg(column);
322-
break;
323-
case 1:
320+
} else if(type=="LINESTRING") {
324321
query += QString("GeometryType(\"%1\") IN ('LINESTRING','MULTILINESTRING')").arg(column);
325-
break;
326-
case 2:
322+
} else if(type=="POLYGON") {
327323
query += QString("GeometryType(\"%1\") IN ('POLYGON','MULTIPOLYGON')").arg(column);
328-
break;
324+
} else {
325+
continue;
329326
}
330327
}
331328

@@ -340,6 +337,7 @@ void QgsDbSourceSelect::addTables()
340337

341338
m_selectedTables += query;
342339
}
340+
i++;
343341
}
344342

345343
// BEGIN CHANGES ECOS
@@ -442,6 +440,14 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
442440
mLayerIcons.insert("UNKNOWN",
443441
qMakePair(tr("Unknown layer type"),
444442
QIcon(myThemePath+"/mIconUnknownLayerType.png")));
443+
444+
mCbMinLength = 0;
445+
QMapIterator <QString, QPair<QString, QIcon>> it(mLayerIcons);
446+
while( it.hasNext() ) {
447+
it.next();
448+
int len = it.value().first.length();;
449+
mCbMinLength = mCbMinLength<len ? len : mCbMinLength;
450+
}
445451
}
446452
//qDebug("Connection succeeded");
447453
// tell the DB that we want text encoded in UTF8
@@ -463,41 +469,14 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
463469
geomCol::const_iterator iter = details.begin();
464470
for (; iter != details.end(); ++iter)
465471
{
466-
QString toolTipText;
467-
const QIcon* p;
468-
469-
if (mLayerIcons.contains(iter->second))
470-
{
471-
p = &(mLayerIcons.value(iter->second).second);
472-
toolTipText = mLayerIcons.value(iter->second).first;
473-
}
474-
else
475-
{
476-
QgsDebugMsg("Unknown geometry type of '" + iter->second + "'.");
477-
p = &(mLayerIcons.value("UNKNOWN").second);
478-
toolTipText = mLayerIcons.value("UNKNOWN").first;
479-
}
480-
481-
if (p != 0)
482-
{
483-
int row = lstTables->rowCount();
484-
lstTables->setRowCount(row+1);
485-
486-
QCheckBox *cb = new QCheckBox();
487-
cb->setToolTip(tr("check to import layer"));
488-
lstTables->setCellWidget(row, dbssImport, cb);
489-
490-
QTableWidgetItem *iconItem = new QTableWidgetItem();
491-
iconItem->setIcon(*p);
492-
iconItem->setToolTip(toolTipText);
493-
lstTables->setItem(row, dbssType, iconItem);
494-
495-
QTableWidgetItem *textItem = new QTableWidgetItem(iter->first);
496-
textItem->setToolTip( tr("double click to open PostgreSQL query builder") );
497-
lstTables->setItem(row, dbssDetail, textItem);
498-
499-
updateImportAsInfo(row, iter->second);
500-
}
472+
int row = lstTables->rowCount();
473+
lstTables->setRowCount(row+1);
474+
475+
QTableWidgetItem *textItem = new QTableWidgetItem(iter->first);
476+
textItem->setToolTip( tr("double click to open PostgreSQL query builder") );
477+
lstTables->setItem(row, dbssDetail, textItem);
478+
479+
updateTypeInfo(row, iter->second);
501480
}
502481

503482
// And tidy up the columns & rows
@@ -532,7 +511,8 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
532511
if (cmbConnections->count() > 0)
533512
btnAdd->setEnabled(true);
534513
// END CHANGES ECOS
535-
} else
514+
}
515+
else
536516
{
537517
QMessageBox::warning(this, tr("Connection failed"),
538518
tr
@@ -586,6 +566,17 @@ void QgsDbSourceSelect::setSql(QTableWidgetItem *item)
586566
delete pgb;
587567
}
588568

569+
void QgsDbSourceSelect::addSearchGeometryColumn(const QString &schema, const QString &table, const QString &column)
570+
{
571+
// store the column details and do the query in a thread
572+
if (mColumnTypeThread == NULL)
573+
{
574+
mColumnTypeThread = new QgsGeomColumnTypeThread();
575+
mColumnTypeThread->setConnInfo(m_connInfo);
576+
}
577+
mColumnTypeThread->addGeometryColumn(schema, table, column);
578+
}
579+
589580
bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
590581
geomCol& details, bool searchGeometryColumnsOnly,
591582
bool searchPublicOnly)
@@ -620,6 +611,8 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
620611
PGresult* exists = PQexec(pg, sql.toLocal8Bit().data());
621612
if (PQntuples(exists) == 1)
622613
{
614+
QString column = PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column"));
615+
QString type = PQgetvalue(result, idx, PQfnumber(result, "type"));
623616
QString v = "";
624617

625618
if (schemaName.length() > 0)
@@ -632,11 +625,16 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
632625
v += '"';
633626
v += tableName;
634627
v += "\" (";
635-
v += PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column"));
628+
v += column;
636629
v += ")";
637630

638-
QString type = PQgetvalue(result, idx, PQfnumber(result, "type"));
639-
details.push_back(geomPair(v, type));
631+
632+
if(type=="GEOMETRY" && !searchGeometryColumnsOnly) {
633+
addSearchGeometryColumn(schemaName, tableName, column);
634+
type="WAITING";
635+
}
636+
637+
details.push_back(geomPair(v, type));
640638
}
641639
PQclear(exists);
642640
}
@@ -682,34 +680,9 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
682680

683681
QString full_desc = fullDescription(schema, table, column);
684682

685-
QString type = "UNKNOWN";
686-
if (relkind == "r")
687-
{
688-
QString query = makeGeomQuery(schema, table, column);
689-
PGresult* gresult = PQexec(pg, query.toLocal8Bit().data());
690-
if (PQresultStatus(gresult) != PGRES_TUPLES_OK)
691-
{
692-
QString myError = (tr("Access to relation ") + table + tr(" using sql;\n") + query +
693-
tr("\nhas failed. The database said:\n"));
694-
qDebug(myError + QString(PQresultErrorMessage(gresult)));
695-
}
696-
else if (PQntuples(gresult) > 0)
697-
type = PQgetvalue(gresult, 0, 0); // GeometryType
698-
PQclear(gresult);
699-
}
700-
else // view
701-
{
702-
// store the column details and do the query in a thread
703-
if (mColumnTypeThread == NULL)
704-
{
705-
mColumnTypeThread = new QgsGeomColumnTypeThread();
706-
mColumnTypeThread->setConnInfo(m_connInfo);
707-
}
708-
mColumnTypeThread->setGeometryColumn(schema, table, column);
709-
type = "WAITING";
710-
}
683+
addSearchGeometryColumn(schema, table, column);
711684

712-
details.push_back(geomPair(full_desc, type));
685+
details.push_back(geomPair(full_desc, "WAITING"));
713686
}
714687
ok = true;
715688

@@ -778,7 +751,7 @@ void QgsGeomColumnTypeThread::setConnInfo(QString s)
778751
mConnInfo = s;
779752
}
780753

781-
void QgsGeomColumnTypeThread::setGeometryColumn(QString schema, QString table, QString column)
754+
void QgsGeomColumnTypeThread::addGeometryColumn(QString schema, QString table, QString column)
782755
{
783756
schemas.push_back(schema);
784757
tables.push_back(table);
@@ -797,10 +770,19 @@ void QgsGeomColumnTypeThread::getLayerTypes()
797770
QString query = QgsDbSourceSelect::makeGeomQuery(schemas[i],
798771
tables[i],
799772
columns[i]);
773+
QgsDebugMsg("Running SQL:" + query);
800774
PGresult* gresult = PQexec(pd, query.toLocal8Bit().data());
801775
QString type;
802-
if (PQresultStatus(gresult) == PGRES_TUPLES_OK)
803-
type = PQgetvalue(gresult, 0, 0);
776+
if (PQresultStatus(gresult) == PGRES_TUPLES_OK) {
777+
QStringList types;
778+
779+
for(int j=0; j<PQntuples(gresult); j++) {
780+
QString type = PQgetvalue(gresult, j, 0);
781+
types += type!="" ? type : "UNKNOWN";
782+
}
783+
784+
type = types.join(",");
785+
}
804786
PQclear(gresult);
805787

806788
// Now tell the layer list dialog box...

0 commit comments

Comments
 (0)