Index: src/app/qgspgquerybuilder.cpp =================================================================== --- src/app/qgspgquerybuilder.cpp (revisión: 7935) +++ src/app/qgspgquerybuilder.cpp (copia de trabajo) @@ -24,7 +24,7 @@ : QDialog(parent, fl) { setupUi(this); - setupListViews(); + setupGuiViews(); } // constructor used when the query builder must make its own // connection to the database @@ -33,7 +33,7 @@ : QDialog(parent, fl), mUri(uri) { setupUi(this); - setupListViews(); + setupGuiViews(); // The query builder must make its own connection to the database when // using this constructor QString connInfo = mUri->connInfo(); @@ -71,7 +71,7 @@ : QDialog(parent, fl), mPgConnection(con) { setupUi(this); - setupListViews(); + setupGuiViews(); mOwnConnection = false; // we don't own this connection since it was passed to us mUri = new QgsDataSourceURI( "table=" + tableName); QString datasource = QString(tr("Table %1 in database %2 on host %3, user %4")) @@ -138,8 +138,10 @@ mFieldMap[fieldName] = QgsField(fieldName, type, fieldType); QStandardItem *myItem = new QStandardItem(fieldName); myItem->setEditable(false); - mModelFields->insertRow(mModelFields->rowCount(),myItem); + mModelFields->insertRow(mModelFields->rowCount(),myItem); } + // All fields get ... setup + setupLstFieldsModel(); } else { @@ -148,13 +150,16 @@ PQclear(result); } -void QgsPgQueryBuilder::setupListViews() +void QgsPgQueryBuilder::setupLstFieldsModel() { - //Models + lstFields->setModel(mModelFields); +} + +void QgsPgQueryBuilder::setupGuiViews() +{ + //Initialize the models mModelFields = new QStandardItemModel(); mModelValues = new QStandardItemModel(); - lstFields->setModel(mModelFields); - lstValues->setModel(mModelValues); // Modes lstFields->setViewMode(QListView::ListMode); lstValues->setViewMode(QListView::ListMode); @@ -163,26 +168,21 @@ // Performance tip since Qt 4.1 lstFields->setUniformItemSizes(true); lstValues->setUniformItemSizes(true); + // Colored rows + lstFields->setAlternatingRowColors(true); + lstValues->setAlternatingRowColors(true); } -void QgsPgQueryBuilder::on_btnSampleValues_clicked() +void QgsPgQueryBuilder::fillValues(QString theSQL) { - QString myFieldName = mModelFields->data(lstFields->currentIndex()).toString(); - if (myFieldName.isEmpty()) - return; - - QString sql = "SELECT DISTINCT \"" + myFieldName + "\" " + - "FROM (SELECT \"" + myFieldName + "\" " + - "FROM " + mUri->quotedTablename() + " " + - "LIMIT 5000) AS foo " + - "ORDER BY \"" + myFieldName + "\" "+ - "LIMIT 25"; - // clear the values list + // clear the model mModelValues->clear(); + // determine the field type - QgsField field = mFieldMap[myFieldName]; - bool isCharField = field.typeName().find("char") > -1; - PGresult *result = PQexec(mPgConnection, (const char *) (sql.utf8())); + QgsField field = mFieldMap[mModelFields->data(lstFields->currentIndex()).toString()]; + bool mActualFieldIsChar = field.typeName().find("char") > -1; + + PGresult *result = PQexec(mPgConnection, (const char *) (theSQL.utf8())); if (PQresultStatus(result) == PGRES_TUPLES_OK) { @@ -190,72 +190,66 @@ for(int i=0; i < rowCount; i++) { QString value = QString::fromUtf8(PQgetvalue(result, i, 0)); - if(isCharField) - { - value = "'" + value + "'"; - } QStandardItem *myItem = new QStandardItem(value); myItem->setEditable(false); mModelValues->insertRow(mModelValues->rowCount(),myItem); } }else { - QMessageBox::warning(this, tr("Database error"), tr("

Failed to get sample of field values using SQL:

") + sql + "

Error message was: "+ QString(PQerrorMessage(mPgConnection)) + "

"); + QMessageBox::warning(this, tr("Database error"), tr("

Failed to get sample of field values using SQL:

") + theSQL + "

Error message was: "+ QString(PQerrorMessage(mPgConnection)) + "

"); } // free the result set PQclear(result); } +void QgsPgQueryBuilder::on_btnSampleValues_clicked() +{ + lstValues->setCursor(Qt::WaitCursor); + QString myFieldName = mModelFields->data(lstFields->currentIndex()).toString(); + if (myFieldName.isEmpty()) + return; + + QgsField field = mFieldMap[mModelFields->data(lstFields->currentIndex()).toString()]; + bool mActualFieldIsChar = field.typeName().find("char") > -1; + + QString sql = "SELECT DISTINCT \"" + myFieldName + "\" " + + "FROM (SELECT \"" + myFieldName + "\" " + + "FROM " + mUri->quotedTablename() + " " + + "LIMIT 5000) AS foo " + + "ORDER BY \"" + myFieldName + "\" "+ + "LIMIT 25"; + + //delete connection mModelValues and lstValues + QStandardItemModel *tmp = new QStandardItemModel(); + lstValues->setModel(tmp); + //Clear and fill the mModelValues + fillValues(sql); + lstValues->setModel(mModelValues); + lstValues->setCursor(Qt::ArrowCursor); + //delete the tmp + delete tmp; + +} + void QgsPgQueryBuilder::on_btnGetAllValues_clicked() { + lstValues->setCursor(Qt::WaitCursor); QString myFieldName = mModelFields->data(lstFields->currentIndex()).toString(); if (myFieldName.isEmpty()) return; QString sql = "select distinct \"" + myFieldName + "\" from " + mUri->quotedTablename() + " order by \"" + myFieldName + "\""; - // clear the values list - mModelValues->clear(); - // determine the field type - QgsField field = mFieldMap[myFieldName]; - bool isCharField = field.typeName().find("char") > -1; - - PGresult *result = PQexec(mPgConnection, (const char *) (sql.utf8())); - - if (PQresultStatus(result) == PGRES_TUPLES_OK) - { - int rowCount = PQntuples(result); - - lstValues->setCursor(Qt::WaitCursor); - // Block for better performance - mModelValues->blockSignals(true); - lstValues->setUpdatesEnabled(false); - - for(int i=0; i < rowCount; i++) - { - QString value = QString::fromUtf8(PQgetvalue(result, i, 0)); - if(isCharField) - { - value = "'" + value + "'"; - } - QStandardItem *myItem = new QStandardItem(value); - myItem->setEditable(false); - mModelValues->insertRow(mModelValues->rowCount(),myItem); - } - - // Unblock for normal use - mModelValues->blockSignals(false); - lstValues->setUpdatesEnabled(true); - // TODO: already sorted, signal emit to refresh model - mModelValues->sort(0); - lstValues->setCursor(Qt::ArrowCursor); - - }else - { - QMessageBox::warning(this, tr("Database error"), tr("Failed to get sample of field values") + QString(PQerrorMessage(mPgConnection)) ); - } - // free the result set - PQclear(result); + + //delete connection mModelValues and lstValues + QStandardItemModel *tmp = new QStandardItemModel(); + lstValues->setModel(tmp); + //Clear and fill the mModelValues + fillValues(sql); + lstValues->setModel(mModelValues); + lstValues->setCursor(Qt::ArrowCursor); + //delete the tmp + delete tmp; } void QgsPgQueryBuilder::on_btnTest_clicked() @@ -398,6 +392,27 @@ txtSQL->setText(sqlStatement); } +void QgsPgQueryBuilder::on_lstFields_clicked( const QModelIndex &index ) +{ + if (mPreviousFieldRow != index.row()) + { + mPreviousFieldRow = index.row(); + + // If type is gemetry .. normal users don't want to get values? + QgsField field = mFieldMap[mModelFields->data(lstFields->currentIndex()).toString()]; + if (field.typeName().find("geometry") > -1) + { + btnSampleValues->setEnabled(false); + btnGetAllValues->setEnabled(false); + }else + { + btnSampleValues->setEnabled(true); + btnGetAllValues->setEnabled(true); + } + mModelValues->clear(); + } +} + void QgsPgQueryBuilder::on_lstFields_doubleClicked( const QModelIndex &index ) { txtSQL->insert("\"" + mModelFields->data(index).toString() + "\""); @@ -405,7 +420,13 @@ void QgsPgQueryBuilder::on_lstValues_doubleClicked( const QModelIndex &index ) { - txtSQL->insert(mModelValues->data(index).toString()); + if (mActualFieldIsChar) + { + txtSQL->insert("'" + mModelValues->data(index).toString() + "'"); + }else + { + txtSQL->insert(mModelValues->data(index).toString()); + } } void QgsPgQueryBuilder::on_btnLessEqual_clicked() Index: src/app/qgspgquerybuilder.h =================================================================== --- src/app/qgspgquerybuilder.h (revisión: 7935) +++ src/app/qgspgquerybuilder.h (copia de trabajo) @@ -87,6 +87,7 @@ void on_btnILike_clicked(); QString sql(); void setSql( QString sqlStatement); + void on_lstFields_clicked( const QModelIndex &index ); void on_lstFields_doubleClicked( const QModelIndex &index ); void on_lstValues_doubleClicked( const QModelIndex &index ); void on_btnLessEqual_clicked(); @@ -123,7 +124,9 @@ /*! * Setup models for listviews */ - void setupListViews(); + void setupGuiViews(); + void setupLstFieldsModel(); + void fillValues(QString theSQL); /*! Get the number of records that would be returned by the current SQL * @return Number of records or -1 if an error was encountered @@ -149,5 +152,9 @@ QStandardItemModel *mModelFields; //! Model for values ListView QStandardItemModel *mModelValues; + //! Actual field char? + bool mActualFieldIsChar; + //! Previous field row to delete model + int mPreviousFieldRow; }; #endif //QGSPGQUERYBUILDER_H Index: src/ui/qgspgquerybuilderbase.ui =================================================================== --- src/ui/qgspgquerybuilderbase.ui (revisión: 7935) +++ src/ui/qgspgquerybuilderbase.ui (copia de trabajo) @@ -12,9 +12,7 @@ - - 0 - 0 + 0 0 @@ -29,24 +27,48 @@ true - + 11 - + + 11 + + + 11 + + + 11 + + 6 + + 6 + Operators - + 11 - + + 11 + + + 11 + + + 11 + + 6 + + 6 + @@ -150,12 +172,21 @@ - - 11 - 6 + + 11 + + + 11 + + + 11 + + + 11 + @@ -230,9 +261,7 @@ - - 0 - 0 + 0 0 @@ -241,14 +270,32 @@ Values - + 11 - + + 11 + + + 11 + + + 11 + + 6 + + 6 + + + <html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Retrieve <span style=" font-weight:600;">all</span> the record in the vector file (<span style=" font-style:italic;">if the table is big, the operation can consume some time</span>)</p></body></html> + All @@ -256,31 +303,78 @@ + + <html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Take a <span style=" font-weight:600;">sample</span> of records in the vector file</p></body></html> + Sample - + + + <html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">List of values for the current field.</p></body></html> + + + true + + + QAbstractItemView::SelectRows + + + true + + + + + 0 + 0 + + Fields - + 11 - + + 11 + + + 11 + + + 11 + + 6 + + 6 + - + + + <html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">List of fields in this vector file</p></body></html> + + @@ -304,12 +398,24 @@ SQL where clause - + 11 - + + 11 + + + 11 + + + 11 + + 6 + + 6 +