QGIS API Documentation 3.43.0-Master (58029bba303)
qgsqueryresultwidget.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsqueryresultwidget.h - QgsQueryResultWidget
3
4 ---------------------
5 begin : 14.1.2021
6 copyright : (C) 2021 by elpaso
7 email : elpaso@itopen.it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#ifndef QGSQUERYRESULTWIDGET_H
17#define QGSQUERYRESULTWIDGET_H
18
19#include "qgis_gui.h"
20#include "qgis_sip.h"
21#include "ui_qgsqueryresultwidgetbase.h"
22#include "ui_qgsqueryresultpanelwidgetbase.h"
24#include "qgsqueryresultmodel.h"
25#include "qgsfeedback.h"
26#include "qgssettingstree.h"
27
28#include <QWidget>
29#include <QThread>
30#include <QtConcurrent>
31#include <QStyledItemDelegate>
32#include <QDialog>
33#include <QMainWindow>
34
37class QgsQueryResultPanelWidget;
39
41
42#ifndef SIP_RUN
43
47class GUI_EXPORT QgsQueryResultItemDelegate : public QStyledItemDelegate
48{
49 Q_OBJECT
50
51 // QStyledItemDelegate interface
52 public:
53 explicit QgsQueryResultItemDelegate( QObject *parent = nullptr );
54
55 QString displayText( const QVariant &value, const QLocale &locale ) const override;
56};
57
62class GUI_EXPORT QgsConnectionsApiFetcher : public QObject
63{
64 Q_OBJECT
65
66 public:
68 QgsConnectionsApiFetcher( const QString &uri, const QString &providerKey )
69 : mUri( uri )
70 , mProviderKey( providerKey )
71 {}
72
74 void fetchTokens();
75
77 void stopFetching();
78
79 signals:
80
82 void tokensReady( const QStringList &newTokens );
83
85 void fetchingFinished();
86
87 private:
88 QString mUri;
89 QString mProviderKey;
90 QAtomicInt mStopFetching = 0;
91 std::unique_ptr<QgsFeedback> mFeedback;
92};
93
94#endif
95
97
113class GUI_EXPORT QgsQueryResultWidget : public QWidget, private Ui::QgsQueryResultWidgetBase
114{
115 Q_OBJECT
116
117 public:
118#ifndef SIP_RUN
120 static inline QgsSettingsTreeNode *sTreeSqlQueries = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "sql-queries" ) );
121 static const QgsSettingsEntryString *settingLastSourceFolder;
123#endif
124
128 enum class QueryWidgetMode : int SIP_ENUM_BASETYPE( IntFlag )
129 {
130 SqlQueryMode = 1 << 0,
131 QueryLayerUpdateMode = 1 << 1,
132 };
133 Q_ENUM( QueryWidgetMode )
134
135
138 QgsQueryResultWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
139
140 virtual ~QgsQueryResultWidget();
141
145 void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
146
150 void setWidgetMode( QueryWidgetMode widgetMode );
151
155 void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
156
160 void setQuery( const QString &sql );
161
162 SIP_SKIP bool promptUnsavedChanges();
163
164 public slots:
165
169 void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
170
174 void executeQuery();
175
180 void showError( const QString &title, const QString &message, bool isSqlError = false );
181
187 Q_DECL_DEPRECATED void tokensReady( const QStringList &tokens ) SIP_DEPRECATED;
188
194 void copyResults();
195
201 void copyResults( int fromRow, int toRow, int fromColumn, int toColumn );
202
203 signals:
204
211 void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
212
217 void firstResultBatchFetched();
218
219 SIP_SKIP void requestDialogTitleUpdate( const QString &filename );
220
221 private slots:
222
226 void updateButtons();
227
228 void openQuery();
229 void saveQuery( bool saveAs );
230 void setHasChanged( bool hasChanged );
231 void populatePresetQueryMenu();
232 void showHistoryPanel( bool show );
233
234 private:
235 QgsQueryResultPanelWidget *mQueryWidget = nullptr;
236 QPointer< QgsDatabaseQueryHistoryWidget > mHistoryWidget;
237
238 QMenu *mPresetQueryMenu = nullptr;
239
240 bool mHasChangedFileContents = false;
241
242 void updateDialogTitle();
243 void storeCurrentQuery( Qgis::QueryStorageBackend backend );
244
245 friend class TestQgsQueryResultWidget;
246};
247
248#ifndef SIP_RUN
249
261class GUI_EXPORT QgsQueryResultPanelWidget : public QgsPanelWidget, private Ui::QgsQueryResultPanelWidgetBase
262{
263 Q_OBJECT
264
265 public:
269 QgsQueryResultPanelWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
270
271 ~QgsQueryResultPanelWidget() override;
272
276 QgsCodeEditorSQL *sqlEditor();
277
281 QgsCodeEditorWidget *codeEditorWidget();
282
286 void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
287
291 void setWidgetMode( QgsQueryResultWidget::QueryWidgetMode widgetMode );
292
296 void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
297
301 void setQuery( const QString &sql );
302
303 public slots:
304
308 void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
309
313 void executeQuery();
314
319 void showError( const QString &title, const QString &message, bool isSqlError = false );
320
324 void tokensReady( const QStringList &tokens );
325
331 void copyResults();
332
338 void copyResults( int fromRow, int toRow, int fromColumn, int toColumn );
339
340 signals:
341
348 void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
349
354 void firstResultBatchFetched();
355
356 private slots:
357
361 void updateButtons();
362
363 void showCellContextMenu( QPoint point );
364 void copySelection();
365
366 private:
367 QgsCodeEditorWidget *mCodeEditorWidget = nullptr;
368 QgsCodeEditorSQL *mSqlEditor = nullptr;
369
370 std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
371 std::unique_ptr<QgsQueryResultModel> mModel;
372 std::unique_ptr<QgsFeedback> mFeedback;
373
374 QPointer<QgsConnectionsApiFetcher> mApiFetcher;
375
376 bool mWasCanceled = false;
378 bool mFirstRowFetched = false;
379 QFutureWatcher<QgsAbstractDatabaseProviderConnection::QueryResult> mQueryResultWatcher;
380 QString mSqlErrorMessage;
381 long long mActualRowCount = -1;
382 long long mFetchedRowsBatchCount = 0;
383 QgsQueryResultWidget::QueryWidgetMode mQueryWidgetMode = QgsQueryResultWidget::QueryWidgetMode::SqlQueryMode;
384 long long mCurrentHistoryEntryId = -1;
385
389 void updateSqlLayerColumns();
390
394 void cancelRunningQuery();
395
399 void cancelApiFetcher();
400
404 void startFetching();
405
410
411 void storeCurrentQuery( Qgis::QueryStorageBackend backend );
412
413 friend class TestQgsQueryResultWidget;
414 friend class QgsQueryResultWidget;
415};
416#endif
417
429class GUI_EXPORT QgsQueryResultDialog : public QDialog
430{
431 Q_OBJECT
432
433 public:
439 QgsQueryResultDialog( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr, QWidget *parent = nullptr );
440
444 QgsQueryResultWidget *resultWidget() { return mWidget; }
445
446 void closeEvent( QCloseEvent *event ) override;
447
448 private:
449 QgsQueryResultWidget *mWidget = nullptr;
450};
451
463class GUI_EXPORT QgsQueryResultMainWindow : public QMainWindow
464{
465 Q_OBJECT
466
467 public:
473 QgsQueryResultMainWindow( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr, const QString &identifierName = QString() );
474
478 QgsQueryResultWidget *resultWidget() { return mWidget; }
479
480 void closeEvent( QCloseEvent *event ) override;
481
482 private:
483 QgsQueryResultWidget *mWidget = nullptr;
484 QString mIdentifierName;
485
486 void updateWindowTitle( const QString &fileName );
487};
488
489#endif // QGSQUERYRESULTWIDGET_H
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ Info
Information message.
Definition qgis.h:155
QueryStorageBackend
Stored query storage backends.
Definition qgis.h:3385
Provides common functionality for database based connections.
A SQL editor based on QScintilla2.
A widget which wraps a QgsCodeEditor in additional functionality.
Custom QgsHistoryWidget for use with the database query provider.
Base class for any widget that can be shown as an inline panel.
A string settings entry.
A tree node for the settings tree to help organizing and introspecting the tree.
QgsSettingsTreeNode * createChildNode(const QString &key)
Creates a normal tree node It will return the existing child node if it exists at the given key.
static QgsSettingsTreeNode * sTreeGui
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_TRANSFER
Definition qgis_sip.h:36
The SqlVectorLayerOptions stores all information required to create a SQL (query) layer.