QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 : [email protected]
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 
32 
33 #ifndef SIP_RUN
34 
38 class GUI_EXPORT QgsQueryResultItemDelegate: public QStyledItemDelegate
39 {
40  Q_OBJECT
41 
42  // QStyledItemDelegate interface
43  public:
44 
45  explicit QgsQueryResultItemDelegate( QObject *parent = nullptr );
46 
47  QString displayText( const QVariant &value, const QLocale &locale ) const override;
48 };
49 
54 class GUI_EXPORT QgsConnectionsApiFetcher: public QObject
55 {
56  Q_OBJECT
57 
58  public:
59 
61  QgsConnectionsApiFetcher( const QgsAbstractDatabaseProviderConnection *connection )
62  : mConnection( connection )
63  {}
64 
66  void fetchTokens();
67 
69  void stopFetching();
70 
71  signals:
72 
74  void tokensReady( const QStringList &newTokens );
75 
77  void fetchingFinished();
78 
79  private:
80 
81  const QgsAbstractDatabaseProviderConnection *mConnection = nullptr;
82  QAtomicInt mStopFetching = 0;
83 
84 };
85 
86 #endif
87 
89 
105 class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResultWidgetBase
106 {
107  Q_OBJECT
108 
109  public:
110 
111 
115  enum class QueryWidgetMode : int
116  {
117  SqlQueryMode = 1 << 0,
118  QueryLayerUpdateMode = 1 << 1,
119  };
120  Q_ENUM( QueryWidgetMode )
121 
122 
125  QgsQueryResultWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
126 
127  virtual ~QgsQueryResultWidget();
128 
132  void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
133 
137  void setWidgetMode( QueryWidgetMode widgetMode );
138 
142  void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
143 
147  void setQuery( const QString &sql );
148 
149 
150  public slots:
151 
155  void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
156 
160  void executeQuery();
161 
166  void showError( const QString &title, const QString &message, bool isSqlError = false );
167 
171  void tokensReady( const QStringList &tokens );
172 
173  signals:
174 
181  void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
182 
188 
189  private slots:
190 
194  void updateButtons();
195 
196  private:
197 
198  std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
199  std::unique_ptr<QgsQueryResultModel> mModel;
200  std::unique_ptr<QgsFeedback> mFeedback;
201  std::unique_ptr<QgsConnectionsApiFetcher> mApiFetcher;
202  QThread mApiFetcherWorkerThread;
203  bool mWasCanceled = false;
205  bool mFirstRowFetched = false;
206  QFutureWatcher<QgsAbstractDatabaseProviderConnection::QueryResult> mQueryResultWatcher;
207  QString mSqlErrorMessage;
208  long long mActualRowCount = -1;
209  long long mFetchedRowsBatchCount = 0;
210  QueryWidgetMode mQueryWidgetMode = QueryWidgetMode::SqlQueryMode;
211 
215  void updateSqlLayerColumns();
216 
220  void cancelRunningQuery();
221 
225  void cancelApiFetcher();
226 
230  void startFetching();
231 
236 
237 
238  friend class TestQgsQueryResultWidget;
239 
240 };
241 
242 #endif // QGSQUERYRESULTWIDGET_H
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:106
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
The QgsQueryResultWidget class allows users to enter and run an SQL query on a DB connection (an inst...
void firstResultBatchFetched()
Emitted when the first batch of results has been fetched.
QueryWidgetMode
The QueryWidgetMode enum represents various modes for the widget appearance.
void createSqlVectorLayer(const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options)
Emitted when a new vector SQL (query) layer must be created.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
The SqlVectorLayerOptions stores all information required to create a SQL (query) layer.