QGIS API Documentation 3.43.0-Master (9e873c7bc91)
Loading...
Searching...
No Matches
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"
23#include "qgsqueryresultmodel.h"
24#include "qgsfeedback.h"
25
26#include <QWidget>
27#include <QThread>
28#include <QtConcurrent>
29#include <QStyledItemDelegate>
30#include <QDialog>
31#include <QMainWindow>
32
34
36
37#ifndef SIP_RUN
38
42class GUI_EXPORT QgsQueryResultItemDelegate : public QStyledItemDelegate
43{
44 Q_OBJECT
45
46 // QStyledItemDelegate interface
47 public:
48 explicit QgsQueryResultItemDelegate( QObject *parent = nullptr );
49
50 QString displayText( const QVariant &value, const QLocale &locale ) const override;
51};
52
57class GUI_EXPORT QgsConnectionsApiFetcher : public QObject
58{
59 Q_OBJECT
60
61 public:
63 QgsConnectionsApiFetcher( const QString &uri, const QString &providerKey )
64 : mUri( uri )
65 , mProviderKey( providerKey )
66 {}
67
69 void fetchTokens();
70
72 void stopFetching();
73
74 signals:
75
77 void tokensReady( const QStringList &newTokens );
78
80 void fetchingFinished();
81
82 private:
83 QString mUri;
84 QString mProviderKey;
85 QAtomicInt mStopFetching = 0;
86 std::unique_ptr<QgsFeedback> mFeedback;
87};
88
89#endif
90
92
108class GUI_EXPORT QgsQueryResultWidget : public QWidget, private Ui::QgsQueryResultWidgetBase
109{
110 Q_OBJECT
111
112 public:
113#ifndef SIP_RUN
115 static inline QgsSettingsTreeNode *sTreeSqlQueries = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "sql-queries" ) );
116 static const QgsSettingsEntryString *settingLastSourceFolder;
118#endif
119
123 enum class QueryWidgetMode : int SIP_ENUM_BASETYPE( IntFlag )
124 {
125 SqlQueryMode = 1 << 0,
126 QueryLayerUpdateMode = 1 << 1,
127 };
128 Q_ENUM( QueryWidgetMode )
129
130
133 QgsQueryResultWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
134
135 virtual ~QgsQueryResultWidget();
136
140 void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
141
145 void setWidgetMode( QueryWidgetMode widgetMode );
146
150 void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
151
155 void setQuery( const QString &sql );
156
157 SIP_SKIP bool promptUnsavedChanges();
158
159 public slots:
160
164 void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
165
169 void executeQuery();
170
175 void showError( const QString &title, const QString &message, bool isSqlError = false );
176
180 void tokensReady( const QStringList &tokens );
181
187 void copyResults();
188
194 void copyResults( int fromRow, int toRow, int fromColumn, int toColumn );
195
196 signals:
197
204 void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
205
210 void firstResultBatchFetched();
211
212 SIP_SKIP void requestDialogTitleUpdate( const QString &filename );
213
214 private slots:
215
219 void updateButtons();
220
221 void showCellContextMenu( QPoint point );
222 void copySelection();
223 void openQuery();
224 void saveQuery( bool saveAs );
225 void setHasChanged( bool hasChanged );
226 void populatePresetQueryMenu();
227
228 private:
229 QgsCodeEditorWidget *mCodeEditorWidget = nullptr;
230 QgsCodeEditorSQL *mSqlEditor = nullptr;
231 QMenu *mPresetQueryMenu = nullptr;
232
233 std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
234 std::unique_ptr<QgsQueryResultModel> mModel;
235 std::unique_ptr<QgsFeedback> mFeedback;
236
237 QPointer<QgsConnectionsApiFetcher> mApiFetcher;
238
239 bool mWasCanceled = false;
241 bool mFirstRowFetched = false;
242 QFutureWatcher<QgsAbstractDatabaseProviderConnection::QueryResult> mQueryResultWatcher;
243 QString mSqlErrorMessage;
244 long long mActualRowCount = -1;
245 long long mFetchedRowsBatchCount = 0;
246 QueryWidgetMode mQueryWidgetMode = QueryWidgetMode::SqlQueryMode;
247 long long mCurrentHistoryEntryId = -1;
248
249 bool mHasChangedFileContents = false;
250
254 void updateSqlLayerColumns();
255
259 void cancelRunningQuery();
260
264 void cancelApiFetcher();
265
269 void startFetching();
270
275
276 void updateDialogTitle();
277 void storeCurrentQuery( Qgis::QueryStorageBackend backend );
278
279 friend class TestQgsQueryResultWidget;
280};
281
293class GUI_EXPORT QgsQueryResultDialog : public QDialog
294{
295 Q_OBJECT
296
297 public:
303 QgsQueryResultDialog( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr, QWidget *parent = nullptr );
304
308 QgsQueryResultWidget *resultWidget() { return mWidget; }
309
310 void closeEvent( QCloseEvent *event ) override;
311
312 private:
313 QgsQueryResultWidget *mWidget = nullptr;
314};
315
327class GUI_EXPORT QgsQueryResultMainWindow : public QMainWindow
328{
329 Q_OBJECT
330
331 public:
337 QgsQueryResultMainWindow( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr, const QString &identifierName = QString() );
338
342 QgsQueryResultWidget *resultWidget() { return mWidget; }
343
344 void closeEvent( QCloseEvent *event ) override;
345
346 private:
347 QgsQueryResultWidget *mWidget = nullptr;
348 QString mIdentifierName;
349
350 void updateWindowTitle( const QString &fileName );
351};
352
353#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:3381
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
A SQL editor based on QScintilla2.
A widget which wraps a QgsCodeEditor in additional functionality.
A string settings entry.
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
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_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.