QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 <QStyledItemDelegate>
32#include <QThread>
33#include <QWidget>
34#include <QtConcurrent>
35
38class QgsQueryResultPanelWidget;
40
42
43#ifndef SIP_RUN
44
48class GUI_EXPORT QgsQueryResultItemDelegate : public QStyledItemDelegate
49{
50 Q_OBJECT
51
52 // QStyledItemDelegate interface
53 public:
54 explicit QgsQueryResultItemDelegate( QObject *parent = nullptr );
55
56 QString displayText( const QVariant &value, const QLocale &locale ) const override;
57};
58
63class GUI_EXPORT QgsConnectionsApiFetcher : public QObject
64{
65 Q_OBJECT
66
67 public:
69 QgsConnectionsApiFetcher( const QString &uri, const QString &providerKey )
70 : mUri( uri )
71 , mProviderKey( providerKey )
72 {}
73
75 void fetchTokens();
76
78 void stopFetching();
79
80 signals:
81
83 void tokensReady( const QStringList &newTokens );
84
86 void fetchingFinished();
87
88 private:
89 QString mUri;
90 QString mProviderKey;
91 QAtomicInt mStopFetching = 0;
92 std::unique_ptr<QgsFeedback> mFeedback;
93};
94
95#endif
96
98
114class GUI_EXPORT QgsQueryResultWidget : public QWidget, private Ui::QgsQueryResultWidgetBase
115{
116 Q_OBJECT
117
118 public:
119#ifndef SIP_RUN
121 static inline QgsSettingsTreeNode *sTreeSqlQueries = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "sql-queries" ) );
122 static const QgsSettingsEntryString *settingLastSourceFolder;
124#endif
125
129 enum class QueryWidgetMode : int SIP_ENUM_BASETYPE( IntFlag )
130 {
131 SqlQueryMode = 1 << 0,
132 QueryLayerUpdateMode = 1 << 1,
133 };
134 Q_ENUM( QueryWidgetMode )
135
136
139 QgsQueryResultWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
140
141 ~QgsQueryResultWidget() override;
142
146 void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
147
151 void setWidgetMode( QueryWidgetMode widgetMode );
152
156 void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
157
161 void setQuery( const QString &sql );
162
163 SIP_SKIP bool promptUnsavedChanges();
164
165 public slots:
166
170 void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
171
175 void executeQuery();
176
181 void showError( const QString &title, const QString &message, bool isSqlError = false );
182
188 Q_DECL_DEPRECATED void tokensReady( const QStringList &tokens ) SIP_DEPRECATED;
189
195 void copyResults();
196
202 void copyResults( int fromRow, int toRow, int fromColumn, int toColumn );
203
204 signals:
205
212 void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
213
218 void firstResultBatchFetched();
219
220 SIP_SKIP void requestDialogTitleUpdate( const QString &filename );
221
222 private slots:
223
227 void updateButtons();
228
229 void openQuery();
230 void saveQuery( bool saveAs );
231 void setHasChanged( bool hasChanged );
232 void populatePresetQueryMenu();
233 void showHistoryPanel( bool show );
234
235 private:
236 QgsQueryResultPanelWidget *mQueryWidget = nullptr;
237 QPointer< QgsDatabaseQueryHistoryWidget > mHistoryWidget;
238
239 QMenu *mPresetQueryMenu = nullptr;
240
241 bool mHasChangedFileContents = false;
242
243 void updateDialogTitle();
244 void storeCurrentQuery( Qgis::QueryStorageBackend backend );
245
246 friend class TestQgsQueryResultWidget;
247};
248
249#ifndef SIP_RUN
250
262class GUI_EXPORT QgsQueryResultPanelWidget : public QgsPanelWidget, private Ui::QgsQueryResultPanelWidgetBase
263{
264 Q_OBJECT
265
266 public:
270 QgsQueryResultPanelWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
271
272 ~QgsQueryResultPanelWidget() override;
273
277 QgsCodeEditorSQL *sqlEditor();
278
282 QgsCodeEditorWidget *codeEditorWidget();
283
287 void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
288
292 void setWidgetMode( QgsQueryResultWidget::QueryWidgetMode widgetMode );
293
297 void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
298
302 void setQuery( const QString &sql );
303
304 public slots:
305
309 void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
310
314 void executeQuery();
315
320 void showError( const QString &title, const QString &message, bool isSqlError = false );
321
325 void tokensReady( const QStringList &tokens );
326
332 void copyResults();
333
339 void copyResults( int fromRow, int toRow, int fromColumn, int toColumn );
340
341 signals:
342
349 void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
350
355 void firstResultBatchFetched();
356
357 private slots:
358
362 void updateButtons();
363
364 void showCellContextMenu( QPoint point );
365 void copySelection();
366
367 private:
368 QgsCodeEditorWidget *mCodeEditorWidget = nullptr;
369 QgsCodeEditorSQL *mSqlEditor = nullptr;
370
371 std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
372 std::unique_ptr<QgsQueryResultModel> mModel;
373 std::unique_ptr<QgsFeedback> mFeedback;
374
375 QPointer<QgsConnectionsApiFetcher> mApiFetcher;
376
377 bool mWasCanceled = false;
378 mutable QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions mSqlVectorLayerOptions;
379 bool mFirstRowFetched = false;
380 QFutureWatcher<QgsAbstractDatabaseProviderConnection::QueryResult> mQueryResultWatcher;
381 QString mSqlErrorMessage;
382 long long mActualRowCount = -1;
383 long long mFetchedRowsBatchCount = 0;
384 QgsQueryResultWidget::QueryWidgetMode mQueryWidgetMode = QgsQueryResultWidget::QueryWidgetMode::SqlQueryMode;
385 long long mCurrentHistoryEntryId = -1;
386
390 void updateSqlLayerColumns();
391
395 void cancelRunningQuery();
396
400 void cancelApiFetcher();
401
405 void startFetching();
406
410 QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions sqlVectorLayerOptions() const;
411
412 void storeCurrentQuery( Qgis::QueryStorageBackend backend );
413
414 friend class TestQgsQueryResultWidget;
415 friend class QgsQueryResultWidget;
416};
417#endif
418
430class GUI_EXPORT QgsQueryResultDialog : public QDialog
431{
432 Q_OBJECT
433
434 public:
440 QgsQueryResultDialog( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr, QWidget *parent = nullptr );
441
445 QgsQueryResultWidget *resultWidget() { return mWidget; }
446
447 void closeEvent( QCloseEvent *event ) override;
448
449 private:
450 QgsQueryResultWidget *mWidget = nullptr;
451};
452
464class GUI_EXPORT QgsQueryResultMainWindow : public QMainWindow
465{
466 Q_OBJECT
467
468 public:
474 QgsQueryResultMainWindow( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr, const QString &identifierName = QString() );
475
479 QgsQueryResultWidget *resultWidget() { return mWidget; }
480
481 void closeEvent( QCloseEvent *event ) override;
482
483 private:
484 QgsQueryResultWidget *mWidget = nullptr;
485 QString mIdentifierName;
486
487 void updateWindowTitle( const QString &fileName );
488};
489
490#endif // QGSQUERYRESULTWIDGET_H
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:156
@ Info
Information message.
Definition qgis.h:157
QueryStorageBackend
Stored query storage backends.
Definition qgis.h:3517
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