Skip to content

Commit 6b96b41

Browse files
author
timlinux
committed
Committed patch from ticket #897 - port pg and attribute search dialogs to qt4
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7933 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c59d09f commit 6b96b41

5 files changed

+235
-94
lines changed

src/app/qgspgquerybuilder.cpp

+63-30
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
***************************************************************************/
1515
/* $Id$ */
1616
#include <iostream>
17-
#include <q3listbox.h>
1817
#include <QMessageBox>
18+
#include <QListView>
1919
#include "qgspgquerybuilder.h"
2020
#include <qgslogger.h>
2121
#include <QRegExp>
@@ -24,6 +24,7 @@ QgsPgQueryBuilder::QgsPgQueryBuilder(QWidget *parent, Qt::WFlags fl)
2424
: QDialog(parent, fl)
2525
{
2626
setupUi(this);
27+
setupListViews();
2728
}
2829
// constructor used when the query builder must make its own
2930
// connection to the database
@@ -32,6 +33,7 @@ QgsPgQueryBuilder::QgsPgQueryBuilder(QgsDataSourceURI *uri,
3233
: QDialog(parent, fl), mUri(uri)
3334
{
3435
setupUi(this);
36+
setupListViews();
3537
// The query builder must make its own connection to the database when
3638
// using this constructor
3739
QString connInfo = mUri->connInfo();
@@ -69,6 +71,7 @@ QgsPgQueryBuilder::QgsPgQueryBuilder(QString tableName, PGconn *con,
6971
: QDialog(parent, fl), mPgConnection(con)
7072
{
7173
setupUi(this);
74+
setupListViews();
7275
mOwnConnection = false; // we don't own this connection since it was passed to us
7376
mUri = new QgsDataSourceURI( "table=" + tableName);
7477
QString datasource = QString(tr("Table <b>%1</b> in database <b>%2</b> on host <b>%3</b>, user <b>%4</b>"))
@@ -133,7 +136,9 @@ void QgsPgQueryBuilder::populateFields()
133136
#endif
134137
QVariant::Type type = QVariant::String; // TODO: should be set correctly [MD]
135138
mFieldMap[fieldName] = QgsField(fieldName, type, fieldType);
136-
lstFields->insertItem(fieldName);
139+
QStandardItem *myItem = new QStandardItem(fieldName);
140+
myItem->setEditable(false);
141+
mModelFields->insertRow(mModelFields->rowCount(),myItem);
137142
}
138143
}
139144
else
@@ -143,21 +148,39 @@ void QgsPgQueryBuilder::populateFields()
143148
PQclear(result);
144149
}
145150

151+
void QgsPgQueryBuilder::setupListViews()
152+
{
153+
//Models
154+
mModelFields = new QStandardItemModel();
155+
mModelValues = new QStandardItemModel();
156+
lstFields->setModel(mModelFields);
157+
lstValues->setModel(mModelValues);
158+
// Modes
159+
lstFields->setViewMode(QListView::ListMode);
160+
lstValues->setViewMode(QListView::ListMode);
161+
lstFields->setSelectionBehavior(QAbstractItemView::SelectRows);
162+
lstValues->setSelectionBehavior(QAbstractItemView::SelectRows);
163+
// Performance tip since Qt 4.1
164+
lstFields->setUniformItemSizes(true);
165+
lstValues->setUniformItemSizes(true);
166+
}
167+
146168
void QgsPgQueryBuilder::on_btnSampleValues_clicked()
147169
{
148-
if (lstFields->currentText().isEmpty())
170+
QString myFieldName = mModelFields->data(lstFields->currentIndex()).toString();
171+
if (myFieldName.isEmpty())
149172
return;
150173

151-
QString sql = "SELECT DISTINCT \"" + lstFields->currentText() + "\" " +
152-
"FROM (SELECT \"" + lstFields->currentText() + "\" " +
174+
QString sql = "SELECT DISTINCT \"" + myFieldName + "\" " +
175+
"FROM (SELECT \"" + myFieldName + "\" " +
153176
"FROM " + mUri->quotedTablename() + " " +
154177
"LIMIT 5000) AS foo " +
155-
"ORDER BY \"" + lstFields->currentText() + "\" "+
178+
"ORDER BY \"" + myFieldName + "\" "+
156179
"LIMIT 25";
157180
// clear the values list
158-
lstValues->clear();
181+
mModelValues->clear();
159182
// determine the field type
160-
QgsField field = mFieldMap[lstFields->currentText()];
183+
QgsField field = mFieldMap[myFieldName];
161184
bool isCharField = field.typeName().find("char") > -1;
162185
PGresult *result = PQexec(mPgConnection, (const char *) (sql.utf8()));
163186

@@ -169,14 +192,12 @@ void QgsPgQueryBuilder::on_btnSampleValues_clicked()
169192
QString value = QString::fromUtf8(PQgetvalue(result, i, 0));
170193
if(isCharField)
171194
{
172-
lstValues->insertItem("'" + value + "'");
173-
}
174-
else
175-
{
176-
lstValues->insertItem(value);
195+
value = "'" + value + "'";
177196
}
197+
QStandardItem *myItem = new QStandardItem(value);
198+
myItem->setEditable(false);
199+
mModelValues->insertRow(mModelValues->rowCount(),myItem);
178200
}
179-
180201
}else
181202
{
182203
QMessageBox::warning(this, tr("Database error"), tr("<p>Failed to get sample of field values using SQL:</p><p>") + sql + "</p><p>Error message was: "+ QString(PQerrorMessage(mPgConnection)) + "</p>");
@@ -187,36 +208,48 @@ void QgsPgQueryBuilder::on_btnSampleValues_clicked()
187208

188209
void QgsPgQueryBuilder::on_btnGetAllValues_clicked()
189210
{
190-
if (lstFields->currentText().isEmpty())
211+
QString myFieldName = mModelFields->data(lstFields->currentIndex()).toString();
212+
if (myFieldName.isEmpty())
191213
return;
192214

193-
QString sql = "select distinct \"" + lstFields->currentText()
194-
+ "\" from " + mUri->quotedTablename() + " order by \"" + lstFields->currentText() + "\"";
215+
QString sql = "select distinct \"" + myFieldName
216+
+ "\" from " + mUri->quotedTablename() + " order by \"" + myFieldName + "\"";
195217
// clear the values list
196-
lstValues->clear();
218+
mModelValues->clear();
197219
// determine the field type
198-
QgsField field = mFieldMap[lstFields->currentText()];
220+
QgsField field = mFieldMap[myFieldName];
199221
bool isCharField = field.typeName().find("char") > -1;
200222

201223
PGresult *result = PQexec(mPgConnection, (const char *) (sql.utf8()));
202224

203225
if (PQresultStatus(result) == PGRES_TUPLES_OK)
204226
{
205227
int rowCount = PQntuples(result);
228+
229+
lstValues->setCursor(Qt::WaitCursor);
230+
// Block for better performance
231+
mModelValues->blockSignals(true);
232+
lstValues->setUpdatesEnabled(false);
233+
206234
for(int i=0; i < rowCount; i++)
207235
{
208236
QString value = QString::fromUtf8(PQgetvalue(result, i, 0));
209-
210237
if(isCharField)
211238
{
212-
lstValues->insertItem("'" + value + "'");
213-
}
214-
else
215-
{
216-
lstValues->insertItem(value);
239+
value = "'" + value + "'";
217240
}
241+
QStandardItem *myItem = new QStandardItem(value);
242+
myItem->setEditable(false);
243+
mModelValues->insertRow(mModelValues->rowCount(),myItem);
218244
}
219-
245+
246+
// Unblock for normal use
247+
mModelValues->blockSignals(false);
248+
lstValues->setUpdatesEnabled(true);
249+
// TODO: already sorted, signal emit to refresh model
250+
mModelValues->sort(0);
251+
lstValues->setCursor(Qt::ArrowCursor);
252+
220253
}else
221254
{
222255
QMessageBox::warning(this, tr("Database error"), tr("Failed to get sample of field values") + QString(PQerrorMessage(mPgConnection)) );
@@ -365,14 +398,14 @@ void QgsPgQueryBuilder::setSql( QString sqlStatement)
365398
txtSQL->setText(sqlStatement);
366399
}
367400

368-
void QgsPgQueryBuilder::on_lstFields_doubleClicked( Q3ListBoxItem *item )
401+
void QgsPgQueryBuilder::on_lstFields_doubleClicked( const QModelIndex &index )
369402
{
370-
txtSQL->insert("\"" + item->text() + "\"");
403+
txtSQL->insert("\"" + mModelFields->data(index).toString() + "\"");
371404
}
372405

373-
void QgsPgQueryBuilder::on_lstValues_doubleClicked( Q3ListBoxItem *item )
406+
void QgsPgQueryBuilder::on_lstValues_doubleClicked( const QModelIndex &index )
374407
{
375-
txtSQL->insert(item->text());
408+
txtSQL->insert(mModelValues->data(index).toString());
376409
}
377410

378411
void QgsPgQueryBuilder::on_btnLessEqual_clicked()

src/app/qgspgquerybuilder.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ extern "C"
2020
{
2121
#include <libpq-fe.h>
2222
}
23-
23+
#include <QStandardItemModel>
24+
#include <QStandardItem>
25+
#include <QModelIndex>
2426
#include "ui_qgspgquerybuilderbase.h"
2527
#include "qgisgui.h"
2628
#include "qgsfield.h"
@@ -85,8 +87,8 @@ class QgsPgQueryBuilder : public QDialog, private Ui::QgsPgQueryBuilderBase {
8587
void on_btnILike_clicked();
8688
QString sql();
8789
void setSql( QString sqlStatement);
88-
void on_lstFields_doubleClicked( Q3ListBoxItem *item );
89-
void on_lstValues_doubleClicked( Q3ListBoxItem *item );
90+
void on_lstFields_doubleClicked( const QModelIndex &index );
91+
void on_lstValues_doubleClicked( const QModelIndex &index );
9092
void on_btnLessEqual_clicked();
9193
void on_btnGreaterEqual_clicked();
9294
void on_btnNotEqual_clicked();
@@ -118,6 +120,10 @@ class QgsPgQueryBuilder : public QDialog, private Ui::QgsPgQueryBuilderBase {
118120
* Populate the field list for the selected table
119121
*/
120122
void populateFields();
123+
/*!
124+
* Setup models for listviews
125+
*/
126+
void setupListViews();
121127

122128
/*! Get the number of records that would be returned by the current SQL
123129
* @return Number of records or -1 if an error was encountered
@@ -139,6 +145,9 @@ class QgsPgQueryBuilder : public QDialog, private Ui::QgsPgQueryBuilderBase {
139145
QString mPgErrorMessage;
140146
//! Flag to indicate if the class owns the connection to the pg database
141147
bool mOwnConnection;
142-
148+
//! Model for fields ListView
149+
QStandardItemModel *mModelFields;
150+
//! Model for values ListView
151+
QStandardItemModel *mModelValues;
143152
};
144153
#endif //QGSPGQUERYBUILDER_H

src/app/qgssearchquerybuilder.cpp

+54-13
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
/* $Id$ */
1616

1717
#include <iostream>
18-
#include <q3listbox.h>
18+
#include <QListView>
1919
#include <QMessageBox>
20+
#include <QStandardItem>
2021
#include "qgsfeature.h"
2122
#include "qgsfield.h"
2223
#include "qgssearchquerybuilder.h"
@@ -31,6 +32,7 @@ QgsSearchQueryBuilder::QgsSearchQueryBuilder(QgsVectorLayer* layer,
3132
: QDialog(parent, fl), mLayer(layer)
3233
{
3334
setupUi(this);
35+
setupListViews();
3436

3537
setWindowTitle(tr("Search query builder"));
3638

@@ -53,25 +55,49 @@ QgsSearchQueryBuilder::~QgsSearchQueryBuilder()
5355

5456
void QgsSearchQueryBuilder::populateFields()
5557
{
58+
#ifdef QGISDEBUG
59+
std::cout << "QgsSearchQueryBuilder::populateFields" << std::endl;
60+
#endif
5661
const QgsFieldMap& fields = mLayer->getDataProvider()->fields();
5762
for (QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it)
5863
{
5964
QString fieldName = it->name();
60-
6165
mFieldMap[fieldName] = it.key();
62-
lstFields->insertItem(fieldName);
66+
QStandardItem *myItem = new QStandardItem(fieldName);
67+
myItem->setEditable(false);
68+
mModelFields->insertRow(mModelFields->rowCount(),myItem);
6369
}
6470
}
6571

72+
void QgsSearchQueryBuilder::setupListViews()
73+
{
74+
#ifdef QGISDEBUG
75+
std::cout << "QgsSearchQueryBuilder::setupListViews" << std::endl;
76+
#endif
77+
//Models
78+
mModelFields = new QStandardItemModel();
79+
mModelValues = new QStandardItemModel();
80+
lstFields->setModel(mModelFields);
81+
lstValues->setModel(mModelValues);
82+
// Modes
83+
lstFields->setViewMode(QListView::ListMode);
84+
lstValues->setViewMode(QListView::ListMode);
85+
lstFields->setSelectionBehavior(QAbstractItemView::SelectRows);
86+
lstValues->setSelectionBehavior(QAbstractItemView::SelectRows);
87+
// Performance tip since Qt 4.1
88+
lstFields->setUniformItemSizes(true);
89+
lstValues->setUniformItemSizes(true);
90+
}
91+
6692
void QgsSearchQueryBuilder::getFieldValues(uint limit)
6793
{
6894
// clear the values list
69-
lstValues->clear();
95+
mModelValues->clear();
7096

7197
QgsVectorDataProvider* provider = mLayer->getDataProvider();
7298

7399
// determine the field type
74-
QString fieldName = lstFields->currentText();
100+
QString fieldName = mModelFields->data(lstFields->currentIndex()).toString();
75101
int fieldIndex = mFieldMap[fieldName];
76102
QgsField field = provider->fields()[fieldIndex];
77103
bool numeric = (field.type() == QVariant::Int || field.type() == QVariant::Double);
@@ -84,8 +110,13 @@ void QgsSearchQueryBuilder::getFieldValues(uint limit)
84110

85111
provider->select(attrs, QgsRect(), false);
86112

113+
lstValues->setCursor(Qt::WaitCursor);
114+
// Block for better performance
115+
mModelValues->blockSignals(true);
116+
lstValues->setUpdatesEnabled(false);
117+
87118
while (provider->getNextFeature(feat) &&
88-
(limit == 0 || lstValues->count() != limit))
119+
(limit == 0 || mModelValues->rowCount() != limit))
89120
{
90121
const QgsAttributeMap& attributes = feat.attributeMap();
91122
value = attributes[fieldIndex].toString();
@@ -97,10 +128,20 @@ void QgsSearchQueryBuilder::getFieldValues(uint limit)
97128
}
98129

99130
// add item only if it's not there already
100-
if (lstValues->findItem(value) == 0)
101-
lstValues->insertItem(value);
102-
131+
QList<QStandardItem *> items = mModelValues->findItems(value);
132+
if (items.isEmpty())
133+
{
134+
QStandardItem *myItem = new QStandardItem(value);
135+
myItem->setEditable(false);
136+
mModelValues->insertRow(mModelValues->rowCount(),myItem);
137+
}
103138
}
139+
// Unblock for normal use
140+
mModelValues->blockSignals(false);
141+
lstValues->setUpdatesEnabled(true);
142+
// TODO: already sorted, signal emit to refresh model
143+
mModelValues->sort(0);
144+
lstValues->setCursor(Qt::ArrowCursor);
104145
}
105146

106147
void QgsSearchQueryBuilder::on_btnSampleValues_clicked()
@@ -245,14 +286,14 @@ void QgsSearchQueryBuilder::setSearchString(QString searchString)
245286
txtSQL->setText(searchString);
246287
}
247288

248-
void QgsSearchQueryBuilder::on_lstFields_doubleClicked( Q3ListBoxItem *item )
289+
void QgsSearchQueryBuilder::on_lstFields_doubleClicked( const QModelIndex &index )
249290
{
250-
txtSQL->insert(item->text());
291+
txtSQL->insert(mModelFields->data(index).toString());
251292
}
252293

253-
void QgsSearchQueryBuilder::on_lstValues_doubleClicked( Q3ListBoxItem *item )
294+
void QgsSearchQueryBuilder::on_lstValues_doubleClicked( const QModelIndex &index )
254295
{
255-
txtSQL->insert(item->text());
296+
txtSQL->insert(mModelValues->data(index).toString());
256297
}
257298

258299
void QgsSearchQueryBuilder::on_btnLessEqual_clicked()

0 commit comments

Comments
 (0)