Skip to content
Permalink
Browse files
Truncating localized delegate + optimization
  • Loading branch information
elpaso committed Jul 6, 2021
1 parent c131c81 commit 2c9662bcf1620877a8b687071afaf09dc6bfe154
@@ -18,7 +18,7 @@ class QgsQueryResultModel : QAbstractTableModel
%Docstring(signature="appended")
The :py:class:`QgsQueryResultModel` class is a model for :py:class:`QgsAbstractDatabaseProviderConnection`.QueryResult

.. versionadded:: 3.20
.. versionadded:: 3.18
%End

%TypeHeaderCode
@@ -47,6 +47,13 @@ Constructs a QgsQueryResultModel from a ``queryResult`` with optional ``parent``
QStringList columns() const;
%Docstring
Returns the column names
%End

QgsAbstractDatabaseProviderConnection::QueryResult queryResult() const;
%Docstring
Returns the query result

.. versionadded:: 3.22
%End

public slots:
@@ -28,6 +28,10 @@ QgsQueryResultModel::QgsQueryResultModel( const QgsAbstractDatabaseProviderConne
connect( mWorker.get(), &QgsQueryResultFetcher::rowsReady, this, &QgsQueryResultModel::rowsReady );
connect( mWorker.get(), &QgsQueryResultFetcher::fetchingComplete, this, &QgsQueryResultModel::fetchingComplete );
mWorkerThread.start();
if ( mQueryResult.rowCount() > 0 )
{
mRows.reserve( mQueryResult.rowCount() );
}
}

void QgsQueryResultModel::rowsReady( const QList<QList<QVariant>> &rows )
@@ -45,6 +49,11 @@ void QgsQueryResultModel::cancel()
}
}

QgsAbstractDatabaseProviderConnection::QueryResult QgsQueryResultModel::queryResult() const
{
return mQueryResult;
}

QStringList QgsQueryResultModel::columns() const
{
return mColumns;
@@ -75,7 +75,7 @@ class QgsQueryResultFetcher: public QObject
* \brief The QgsQueryResultModel class is a model for QgsAbstractDatabaseProviderConnection::QueryResult
*
* \ingroup core
* \since QGIS 3.20
* \since QGIS 3.18
*/
class CORE_EXPORT QgsQueryResultModel : public QAbstractTableModel
{
@@ -100,6 +100,12 @@ class CORE_EXPORT QgsQueryResultModel : public QAbstractTableModel
//! Returns the column names
QStringList columns() const;

/**
* Returns the query result
* \since QGIS 3.22
*/
QgsAbstractDatabaseProviderConnection::QueryResult queryResult() const;

public slots:

/**
@@ -20,7 +20,6 @@
#include "qgsquerybuilder.h"
#include "qgsvectorlayer.h"


QgsQueryResultWidget::QgsQueryResultWidget( QWidget *parent, QgsAbstractDatabaseProviderConnection *connection )
: QWidget( parent )
{
@@ -30,6 +29,7 @@ QgsQueryResultWidget::QgsQueryResultWidget( QWidget *parent, QgsAbstractDatabase
// mSqlEditor->setLineNumbersVisible( true );

mQueryResultsTableView->hide();
mQueryResultsTableView->setItemDelegate( new QgsQueryResultItemDelegate( mQueryResultsTableView ) );
mProgressBar->hide();

connect( mExecuteButton, &QPushButton::pressed, this, &QgsQueryResultWidget::executeQuery );
@@ -164,7 +164,8 @@ void QgsQueryResultWidget::executeQuery()

void QgsQueryResultWidget::updateButtons()
{
mFilterToolButton->setEnabled( false );
mFilterLineEdit->setEnabled( mFirstRowFetched );
mFilterToolButton->setEnabled( mFirstRowFetched );
mExecuteButton->setEnabled( ! mSqlEditor->text().isEmpty() );
mLoadAsNewLayerGroupBox->setVisible( mConnection && mConnection->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::SqlLayers ) );
mLoadAsNewLayerGroupBox->setEnabled(
@@ -179,6 +180,7 @@ void QgsQueryResultWidget::updateSqlLayerColumns( )
Q_ASSERT( mModel );

mFilterToolButton->setEnabled( true );
mFilterLineEdit->setEnabled( true );
mPkColumnsComboBox->clear();
mGeometryColumnComboBox->clear();
const bool hasPkInformation { ! mSqlVectorLayerOptions.primaryKeyColumns.isEmpty() };
@@ -239,8 +241,7 @@ void QgsQueryResultWidget::startFetching()
}
else
{
QgsAbstractDatabaseProviderConnection::QueryResult result { mQueryResultWatcher.result() };
mModel = std::make_unique<QgsQueryResultModel>( result );
mModel = std::make_unique<QgsQueryResultModel>( mQueryResultWatcher.result() );
connect( mFeedback.get(), &QgsFeedback::canceled, mModel.get(), [ = ]
{
mModel->cancel();
@@ -256,7 +257,7 @@ void QgsQueryResultWidget::startFetching()
mQueryResultsTableView->show();
updateButtons();
updateSqlLayerColumns( );
mActualRowCount = result.rowCount();
mActualRowCount = mModel->queryResult().rowCount();
if ( mActualRowCount != -1 )
{
mProgressBar->setRange( 0, mActualRowCount );
@@ -362,16 +363,11 @@ void QgsQueryResultWidget::setConnection( QgsAbstractDatabaseProviderConnection
mSqlErrorText->setExtraKeywords( keywords );

// Add dynamic keywords in a separate thread
mApiFetcher = new QgsConnectionsApiFetcher( connection );
mApiFetcher = std::make_unique<QgsConnectionsApiFetcher>( connection );
mApiFetcher->moveToThread( &mApiFetcherWorkerThread );
connect( &mApiFetcherWorkerThread, &QThread::started, mApiFetcher, &QgsConnectionsApiFetcher::fetchTokens );
connect( &mApiFetcherWorkerThread, &QThread::finished, mApiFetcher, [ = ]
{
mApiFetcher->deleteLater();
mApiFetcher = nullptr;
} );
connect( mApiFetcher, &QgsConnectionsApiFetcher::tokensReady, this, &QgsQueryResultWidget::tokensReady );
connect( mApiFetcher, &QgsConnectionsApiFetcher::fetchingFinished, &mApiFetcherWorkerThread, [ = ]
connect( &mApiFetcherWorkerThread, &QThread::started, mApiFetcher.get(), &QgsConnectionsApiFetcher::fetchTokens );
connect( mApiFetcher.get(), &QgsConnectionsApiFetcher::tokensReady, this, &QgsQueryResultWidget::tokensReady );
connect( mApiFetcher.get(), &QgsConnectionsApiFetcher::fetchingFinished, &mApiFetcherWorkerThread, [ = ]
{
mApiFetcherWorkerThread.quit();
mApiFetcherWorkerThread.wait();
@@ -464,3 +460,23 @@ void QgsConnectionsApiFetcher::stopFetching()
}

///@endcond private

QgsQueryResultItemDelegate::QgsQueryResultItemDelegate( QObject *parent )
: QStyledItemDelegate( parent )
{
}

QString QgsQueryResultItemDelegate::displayText( const QVariant &value, const QLocale &locale ) const
{
Q_UNUSED( locale )
// TODO: use https://github.com/qgis/QGIS/pull/43705/files#diff-03e9f0d1977751f918b16f080b6595ff4f2183944e8aa6ac602d345325d8bcd5R440
// when merged
QString result { QStyledItemDelegate::displayText( value, QLocale() ) };
// Show no more than 255 characters
if ( result.length() > 255 )
{
result.truncate( 255 );
result.append( QStringLiteral( "" ) );
}
return result;
}
@@ -26,11 +26,27 @@
#include <QWidget>
#include <QThread>
#include <QtConcurrent>
#include <QStyledItemDelegate>

///@cond private

#ifndef SIP_RUN

/**
* The QgsQueryResultItemDelegate class shows results truncated to 255 characters and using current locale
*/
class GUI_EXPORT QgsQueryResultItemDelegate: public QStyledItemDelegate
{
Q_OBJECT

// QStyledItemDelegate interface
public:

explicit QgsQueryResultItemDelegate( QObject *parent = nullptr );

QString displayText( const QVariant &value, const QLocale &locale ) const override;
};

/**
* The QgsConnectionsApiFetcher class fetches tokens (schema, table and field names) of a connection from a separate thread.
* WARNING: this class is an implementation detail and it is not part of public API!
@@ -149,7 +165,7 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul
std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
std::unique_ptr<QgsQueryResultModel> mModel;
std::unique_ptr<QgsFeedback> mFeedback;
QgsConnectionsApiFetcher *mApiFetcher = nullptr;
std::unique_ptr<QgsConnectionsApiFetcher> mApiFetcher;
QThread mApiFetcherWorkerThread;
bool mWasCanceled = false;
QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions mSqlVectorLayerOptions;
@@ -392,6 +392,8 @@ QVariantList QgsPostgresProviderResultIterator::nextRowPrivate()
return row;
}

row.reserve( result->PQnfields() );

for ( int colIdx = 0; colIdx < result->PQnfields(); colIdx++ )
{
if ( mResolveTypes )
@@ -137,7 +137,7 @@ void TestQgsQueryResultWidget::testCodeEditorApis()
{
auto w = std::make_unique<QgsQueryResultWidget>( nullptr, makeConn() );
bool exited = false;
connect( w->mApiFetcher, &QgsConnectionsApiFetcher::fetchingFinished, w.get(), [ & ] { exited = true; } );
connect( w->mApiFetcher.get(), &QgsConnectionsApiFetcher::fetchingFinished, w.get(), [ & ] { exited = true; } );
while ( ! exited )
QgsApplication::processEvents();
QVERIFY( w->mSqlEditor->extraKeywords().contains( QStringLiteral( "qgis_test" ) ) );
@@ -150,7 +150,7 @@ void TestQgsQueryResultWidget::testCodeEditorApis()
{
QTest::mousePress( w->mStopButton, Qt::MouseButton::LeftButton );
} );
connect( w->mApiFetcher, &QgsConnectionsApiFetcher::fetchingFinished, w.get(), [ & ] { exited = true; } );
connect( w->mApiFetcher.get(), &QgsConnectionsApiFetcher::fetchingFinished, w.get(), [ & ] { exited = true; } );
while ( ! exited )
QgsApplication::processEvents();

0 comments on commit 2c9662b

Please sign in to comment.