QGIS API Documentation 3.99.0-Master (d270888f95f)
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
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 "ui_qgsqueryresultpanelwidgetbase.h"
20#include "ui_qgsqueryresultwidgetbase.h"
21
22#include "qgis_gui.h"
23#include "qgis_sip.h"
25#include "qgsfeedback.h"
26#include "qgsqueryresultmodel.h"
27#include "qgssettingstree.h"
28
29#include <QDialog>
30#include <QMainWindow>
31#include <QString>
32#include <QStyledItemDelegate>
33#include <QThread>
34#include <QWidget>
35#include <QtConcurrent>
36
37using namespace Qt::StringLiterals;
38
41class QgsQueryResultPanelWidget;
43
45
46#ifndef SIP_RUN
47
51class GUI_EXPORT QgsQueryResultItemDelegate : public QStyledItemDelegate
52{
53 Q_OBJECT
54
55 // QStyledItemDelegate interface
56 public:
57 explicit QgsQueryResultItemDelegate( QObject *parent = nullptr );
58
59 QString displayText( const QVariant &value, const QLocale &locale ) const override;
60};
61
66class GUI_EXPORT QgsConnectionsApiFetcher : public QObject
67{
68 Q_OBJECT
69
70 public:
72 QgsConnectionsApiFetcher( const QString &uri, const QString &providerKey )
73 : mUri( uri )
74 , mProviderKey( providerKey )
75 {}
76
78 void fetchTokens();
79
81 void stopFetching();
82
83 signals:
84
86 void tokensReady( const QStringList &newTokens );
87
89 void fetchingFinished();
90
91 private:
92 QString mUri;
93 QString mProviderKey;
94 QAtomicInt mStopFetching = 0;
95 std::unique_ptr<QgsFeedback> mFeedback;
96};
97
98#endif
99
101
117class GUI_EXPORT QgsQueryResultWidget : public QWidget, private Ui::QgsQueryResultWidgetBase
118{
119 Q_OBJECT
120
121 public:
122#ifndef SIP_RUN
124 static inline QgsSettingsTreeNode *sTreeSqlQueries = QgsSettingsTree::sTreeGui->createChildNode( u"sql-queries"_s );
125 static const QgsSettingsEntryString *settingLastSourceFolder;
127#endif
128
132 enum class QueryWidgetMode : int SIP_ENUM_BASETYPE( IntFlag )
133 {
134 SqlQueryMode = 1 << 0,
135 QueryLayerUpdateMode = 1 << 1,
136 };
137 Q_ENUM( QueryWidgetMode )
138
139
142 QgsQueryResultWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
143
144 ~QgsQueryResultWidget() override;
145
149 void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
150
154 void setWidgetMode( QueryWidgetMode widgetMode );
155
159 void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
160
164 void setQuery( const QString &sql );
165
166 SIP_SKIP bool promptUnsavedChanges();
167
168 public slots:
169
173 void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
174
178 void executeQuery();
179
184 void showError( const QString &title, const QString &message, bool isSqlError = false );
185
191 Q_DECL_DEPRECATED void tokensReady( const QStringList &tokens ) SIP_DEPRECATED;
192
198 void copyResults();
199
205 void copyResults( int fromRow, int toRow, int fromColumn, int toColumn );
206
207 signals:
208
215 void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
216
221 void firstResultBatchFetched();
222
223 SIP_SKIP void requestDialogTitleUpdate( const QString &filename );
224
225 private slots:
226
230 void updateButtons();
231
232 void openQuery();
233 void saveQuery( bool saveAs );
234 void setHasChanged( bool hasChanged );
235 void populatePresetQueryMenu();
236 void showHistoryPanel( bool show );
237
238 private:
239 QgsQueryResultPanelWidget *mQueryWidget = nullptr;
240 QPointer< QgsDatabaseQueryHistoryWidget > mHistoryWidget;
241
242 QMenu *mPresetQueryMenu = nullptr;
243
244 bool mHasChangedFileContents = false;
245
246 void updateDialogTitle();
247 void storeCurrentQuery( Qgis::QueryStorageBackend backend );
248
249 friend class TestQgsQueryResultWidget;
250};
251
252#ifndef SIP_RUN
253
265class GUI_EXPORT QgsQueryResultPanelWidget : public QgsPanelWidget, private Ui::QgsQueryResultPanelWidgetBase
266{
267 Q_OBJECT
268
269 public:
273 QgsQueryResultPanelWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
274
275 ~QgsQueryResultPanelWidget() override;
276
280 QgsCodeEditorSQL *sqlEditor();
281
285 QgsCodeEditorWidget *codeEditorWidget();
286
290 void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
291
295 void setWidgetMode( QgsQueryResultWidget::QueryWidgetMode widgetMode );
296
300 void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
301
305 void setQuery( const QString &sql );
306
307 public slots:
308
312 void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
313
317 void executeQuery();
318
323 void showError( const QString &title, const QString &message, bool isSqlError = false );
324
328 void tokensReady( const QStringList &tokens );
329
335 void copyResults();
336
342 void copyResults( int fromRow, int toRow, int fromColumn, int toColumn );
343
344 signals:
345
352 void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
353
358 void firstResultBatchFetched();
359
360 private slots:
361
365 void updateButtons();
366
367 void showCellContextMenu( QPoint point );
368 void copySelection();
369
370 private:
371 QgsCodeEditorWidget *mCodeEditorWidget = nullptr;
372 QgsCodeEditorSQL *mSqlEditor = nullptr;
373
374 std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
375 std::unique_ptr<QgsQueryResultModel> mModel;
376 std::unique_ptr<QgsFeedback> mFeedback;
377
378 QPointer<QgsConnectionsApiFetcher> mApiFetcher;
379
380 bool mWasCanceled = false;
381 mutable QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions mSqlVectorLayerOptions;
382 bool mFirstRowFetched = false;
383 QFutureWatcher<QgsAbstractDatabaseProviderConnection::QueryResult> mQueryResultWatcher;
384 QString mSqlErrorMessage;
385 long long mActualRowCount = -1;
386 long long mFetchedRowsBatchCount = 0;
387 QgsQueryResultWidget::QueryWidgetMode mQueryWidgetMode = QgsQueryResultWidget::QueryWidgetMode::SqlQueryMode;
388 long long mCurrentHistoryEntryId = -1;
389
393 void updateSqlLayerColumns();
394
398 void cancelRunningQuery();
399
403 void cancelApiFetcher();
404
408 void startFetching();
409
413 QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions sqlVectorLayerOptions() const;
414
415 void storeCurrentQuery( Qgis::QueryStorageBackend backend );
416
417 friend class TestQgsQueryResultWidget;
418 friend class QgsQueryResultWidget;
419};
420#endif
421
433class GUI_EXPORT QgsQueryResultDialog : public QDialog
434{
435 Q_OBJECT
436
437 public:
443 QgsQueryResultDialog( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr, QWidget *parent = nullptr );
444
448 QgsQueryResultWidget *resultWidget() { return mWidget; }
449
450 void closeEvent( QCloseEvent *event ) override;
451
452 private:
453 QgsQueryResultWidget *mWidget = nullptr;
454};
455
467class GUI_EXPORT QgsQueryResultMainWindow : public QMainWindow
468{
469 Q_OBJECT
470
471 public:
477 QgsQueryResultMainWindow( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr, const QString &identifierName = QString() );
478
482 QgsQueryResultWidget *resultWidget() { return mWidget; }
483
484 void closeEvent( QCloseEvent *event ) override;
485
486 private:
487 QgsQueryResultWidget *mWidget = nullptr;
488 QString mIdentifierName;
489
490 void updateWindowTitle( const QString &fileName );
491};
492
493#endif // QGSQUERYRESULTWIDGET_H
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:159
@ Info
Information message.
Definition qgis.h:160
QueryStorageBackend
Stored query storage backends.
Definition qgis.h:3576
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.
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:114
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36