Skip to content

Commit 357fa9c

Browse files
committedJul 6, 2021
Add progress bar and minor UX changes
1 parent 537670a commit 357fa9c

File tree

4 files changed

+48
-35
lines changed

4 files changed

+48
-35
lines changed
 

‎python/gui/auto_generated/qgsqueryresultwidget.sip.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ Emitted when the first batch of results has been fetched.
8888
If the query returns no results this signal is not emitted.
8989
%End
9090

91-
9291
};
9392

9493
/************************************************************************

‎src/gui/qgsqueryresultwidget.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "qgsmessagelog.h"
2020
#include "qgsquerybuilder.h"
2121
#include "qgsvectorlayer.h"
22-
#include <QStandardItem>
23-
2422

2523

2624
QgsQueryResultWidget::QgsQueryResultWidget( QWidget *parent, QgsAbstractDatabaseProviderConnection *connection )
@@ -31,6 +29,9 @@ QgsQueryResultWidget::QgsQueryResultWidget( QWidget *parent, QgsAbstractDatabase
3129
// Unsure :/
3230
// mSqlEditor->setLineNumbersVisible( true );
3331

32+
mQueryResultsTableView->hide();
33+
mProgressBar->hide();
34+
3435
connect( mExecuteButton, &QPushButton::pressed, this, &QgsQueryResultWidget::executeQuery );
3536
connect( mClearButton, &QPushButton::pressed, this, [ = ]
3637
{
@@ -98,13 +99,7 @@ QgsQueryResultWidget::QgsQueryResultWidget( QWidget *parent, QgsAbstractDatabase
9899

99100
QgsQueryResultWidget::~QgsQueryResultWidget()
100101
{
101-
if ( mApiFetcher )
102-
{
103-
mApiFetcher->stopFetching();
104-
mWorkerThread.quit();
105-
mWorkerThread.wait();
106-
mWorkerThread.deleteLater();
107-
}
102+
cancelApiFetcher();
108103
cancelRunningQuery();
109104
}
110105

@@ -125,12 +120,15 @@ void QgsQueryResultWidget::executeQuery()
125120
mStopButton->setEnabled( true );
126121
mStatusLabel->show();
127122
mStatusLabel->setText( tr( "Running⋯" ) );
123+
mProgressBar->show();
124+
mProgressBar->setRange( 0, 0 );
128125
mSqlErrorMessage.clear();
129126

130127
connect( mStopButton, &QPushButton::pressed, mFeedback.get(), [ = ]
131128
{
132129
mStatusLabel->setText( tr( "Stopped" ) );
133130
mFeedback->cancel();
131+
mProgressBar->hide();
134132
mWasCanceled = true;
135133
} );
136134

@@ -214,6 +212,17 @@ void QgsQueryResultWidget::cancelRunningQuery()
214212
}
215213
}
216214

215+
void QgsQueryResultWidget::cancelApiFetcher()
216+
{
217+
if ( mApiFetcher )
218+
{
219+
mApiFetcher->stopFetching();
220+
mApiFetcherWorkerThread.quit();
221+
mApiFetcherWorkerThread.wait();
222+
mApiFetcherWorkerThread.deleteLater();
223+
}
224+
}
225+
217226
void QgsQueryResultWidget::startFetching()
218227
{
219228
if ( ! mWasCanceled )
@@ -242,7 +251,7 @@ void QgsQueryResultWidget::startFetching()
242251
updateButtons();
243252
updateSqlLayerColumns( );
244253
}
245-
mStatusLabel->setText( tr( "Fetched rows: %1 %2." )
254+
mStatusLabel->setText( tr( "Fetched rows: %1 %2" )
246255
.arg( mModel->rowCount( mModel->index( -1, -1 ) ) )
247256
.arg( mWasCanceled ? tr( "(stopped)" ) : QString() ) );
248257
} );
@@ -256,20 +265,23 @@ void QgsQueryResultWidget::startFetching()
256265
if ( ! mWasCanceled )
257266
{
258267
mStatusLabel->setText( "Query executed successfully." );
268+
mProgressBar->hide();
259269
}
260270
} );
261271
}
262272
}
263273
else
264274
{
265275
mStatusLabel->setText( tr( "SQL command aborted." ) );
276+
mProgressBar->hide();
266277
}
267278
}
268279

269280
void QgsQueryResultWidget::showError( const QString &title, const QString &message, bool isSqlError )
270281
{
271282
mStatusLabel->show();
272283
mStatusLabel->setText( tr( "There was an error executing the query." ) );
284+
mProgressBar->hide();
273285
mQueryResultsTableView->hide();
274286
if ( isSqlError )
275287
{
@@ -317,14 +329,7 @@ void QgsQueryResultWidget::setConnection( QgsAbstractDatabaseProviderConnection
317329
{
318330
mConnection.reset( connection );
319331

320-
if ( mApiFetcher )
321-
{
322-
mApiFetcher->stopFetching();
323-
mWorkerThread.quit();
324-
mWorkerThread.wait();
325-
mApiFetcher->deleteLater();
326-
mApiFetcher = nullptr;
327-
}
332+
cancelApiFetcher();
328333

329334
if ( connection )
330335
{
@@ -343,15 +348,15 @@ void QgsQueryResultWidget::setConnection( QgsAbstractDatabaseProviderConnection
343348

344349
// Add dynamic keywords in a separate thread
345350
mApiFetcher = new QgsConnectionsApiFetcher( connection );
346-
mApiFetcher->moveToThread( &mWorkerThread );
347-
connect( &mWorkerThread, &QThread::started, mApiFetcher, &QgsConnectionsApiFetcher::fetchTokens );
348-
connect( &mWorkerThread, &QThread::finished, mApiFetcher, [ = ]
351+
mApiFetcher->moveToThread( &mApiFetcherWorkerThread );
352+
connect( &mApiFetcherWorkerThread, &QThread::started, mApiFetcher, &QgsConnectionsApiFetcher::fetchTokens );
353+
connect( &mApiFetcherWorkerThread, &QThread::finished, mApiFetcher, [ = ]
349354
{
350355
mApiFetcher->deleteLater();
351356
mApiFetcher = nullptr;
352357
} );
353358
connect( mApiFetcher, &QgsConnectionsApiFetcher::tokensReady, this, &QgsQueryResultWidget::tokensReady );
354-
mWorkerThread.start();
359+
mApiFetcherWorkerThread.start();
355360
}
356361

357362
updateButtons();
@@ -380,7 +385,7 @@ void QgsConnectionsApiFetcher::fetchTokens()
380385
}
381386
catch ( QgsProviderConnectionException &ex )
382387
{
383-
QgsMessageLog::logMessage( tr( "Error retrieving schemas: %1" ).arg( ex.what() ) );
388+
QgsMessageLog::logMessage( tr( "Error retrieving schemas: %1" ).arg( ex.what() ), QStringLiteral( "QGIS" ), Qgis::MessageLevel::Warning );
384389
}
385390
}
386391
else
@@ -404,7 +409,7 @@ void QgsConnectionsApiFetcher::fetchTokens()
404409
}
405410
catch ( QgsProviderConnectionException &ex )
406411
{
407-
QgsMessageLog::logMessage( tr( "Error retrieving tables: %1" ).arg( ex.what() ) );
412+
QgsMessageLog::logMessage( tr( "Error retrieving tables: %1" ).arg( ex.what() ), QStringLiteral( "QGIS" ), Qgis::MessageLevel::Warning );
408413
}
409414

410415
// Get fields
@@ -425,7 +430,7 @@ void QgsConnectionsApiFetcher::fetchTokens()
425430
}
426431
catch ( QgsProviderConnectionException &ex )
427432
{
428-
QgsMessageLog::logMessage( tr( "Error retrieving fields for table %1: %2" ).arg( table, ex.what() ) );
433+
QgsMessageLog::logMessage( tr( "Error retrieving fields for table %1: %2" ).arg( table, ex.what() ), QStringLiteral( "QGIS" ), Qgis::MessageLevel::Warning );
429434
}
430435
}
431436
}

‎src/gui/qgsqueryresultwidget.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#ifndef SIP_RUN
3333

3434
/**
35-
* \brief The QgsConnectionsApiFetcher class fetches tokens (table and field names) of a connection from a separate thread.
35+
* \brief The QgsConnectionsApiFetcher class fetches tokens (schema, table and field names) of a connection from a separate thread.
3636
*/
3737
class GUI_EXPORT QgsConnectionsApiFetcher: public QObject
3838
{
@@ -123,10 +123,6 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul
123123
*/
124124
void tokensReady( const QStringList &tokens );
125125

126-
private slots:
127-
128-
void syncSqlOptions();
129-
130126
signals:
131127

132128
/**
@@ -143,14 +139,17 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul
143139
*/
144140
void firstResultBatchFetched();
145141

142+
private slots:
143+
144+
void syncSqlOptions();
146145

147146
private:
148147

149148
std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
150149
std::unique_ptr<QgsQueryResultModel> mModel;
151150
std::unique_ptr<QgsFeedback> mFeedback;
152151
QgsConnectionsApiFetcher *mApiFetcher = nullptr;
153-
QThread mWorkerThread;
152+
QThread mApiFetcherWorkerThread;
154153
bool mWasCanceled = false;
155154
QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions mSqlVectorLayerOptions;
156155
bool mFirstRowFetched = false;
@@ -172,6 +171,10 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul
172171
*/
173172
void cancelRunningQuery();
174173

174+
/**
175+
* Cancel API fetching.
176+
*/
177+
void cancelApiFetcher();
175178

176179
/**
177180
* Starts the model population after initial query run.
@@ -183,7 +186,6 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul
183186
*/
184187
QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions sqlVectorLayerOptions() const;
185188

186-
187189
friend class TestQgsQueryResultWidget;
188190

189191
};

‎src/ui/qgsqueryresultwidgetbase.ui

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<widget class="QgsCodeEditorSQL" name="mSqlEditor" native="true"/>
2222
</item>
2323
<item>
24-
<layout class="QHBoxLayout" name="horizontalLayout">
24+
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,1,2,0,1,1">
2525
<item>
2626
<widget class="QPushButton" name="mClearButton">
2727
<property name="text">
@@ -36,6 +36,13 @@
3636
</property>
3737
</widget>
3838
</item>
39+
<item>
40+
<widget class="QProgressBar" name="mProgressBar">
41+
<property name="value">
42+
<number>24</number>
43+
</property>
44+
</widget>
45+
</item>
3946
<item>
4047
<spacer name="horizontalSpacer">
4148
<property name="orientation">
@@ -127,7 +134,7 @@
127134
<string/>
128135
</property>
129136
<property name="placeholderText">
130-
<string>Enter the optional SQL filter ro click on the button to open the query builder tool</string>
137+
<string>Enter the optional SQL filter or click on the button to open the query builder tool</string>
131138
</property>
132139
</widget>
133140
</item>

0 commit comments

Comments
 (0)
Please sign in to comment.