QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgssqlcomposerdialog.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssqlcomposerdialog.cpp
3  Dialog to compose SQL queries
4 
5 begin : Apr 2016
6 copyright : (C) 2016 Even Rouault
7 email : even.rouault at spatialys.com
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #ifndef QGSSQLCOMPOSERDIALOG_H
20 #define QGSSQLCOMPOSERDIALOG_H
21 
22 #include "ui_qgssqlcomposerdialogbase.h"
23 #include <qgis.h>
24 #include <qgisgui.h>
25 #include "qgscontexthelp.h"
26 
27 #include <QPair>
28 #include <QStringList>
29 #include <QSet>
30 
35 class GUI_EXPORT QgsSQLComposerDialog : public QDialog, private Ui::QgsSQLComposerDialogBase
36 {
37  Q_OBJECT
38 
39  public:
40 
43 
46 
51  class GUI_EXPORT TableSelectedCallback
52  {
53  public:
54  virtual ~TableSelectedCallback();
56  virtual void tableSelected( const QString& name ) = 0;
57  };
58 
63  class GUI_EXPORT SQLValidatorCallback
64  {
65  public:
66  virtual ~SQLValidatorCallback();
68  virtual bool isValid( const QString& sql, QString& errorReason, QString& warningMsg ) = 0;
69  };
70 
72  struct Argument
73  {
78 
80  Argument( const QString& nameIn = QString(), const QString& typeIn = QString() ) : name( nameIn ), type( typeIn ) {}
81  };
82 
84  struct Function
85  {
91  int minArgs;
93  int maxArgs;
96 
98  Function( const QString& nameIn, int args ) : name( nameIn ), minArgs( args ), maxArgs( args ) {}
100  Function( const QString& nameIn, int minArgs, int maxArgsIn ) : name( nameIn ), minArgs( minArgs ), maxArgs( maxArgsIn ) {}
102  Function() : minArgs( -1 ), maxArgs( -1 ) {}
103  };
104 
106  explicit QgsSQLComposerDialog( QWidget * parent = nullptr, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
107  virtual ~QgsSQLComposerDialog();
108 
110  void setSql( const QString& sql );
111 
113  QString sql() const;
114 
116  void addTableNames( const QStringList& list );
118  void addTableNames( const QList<PairNameTitle> & listNameTitle );
120  void addColumnNames( const QStringList& list, const QString& tableName );
122  void addColumnNames( const QList<PairNameType>& list, const QString& tableName );
124  void addOperators( const QStringList& list );
126  void addSpatialPredicates( const QStringList& list );
128  void addSpatialPredicates( const QList<Function>& list );
130  void addFunctions( const QStringList& list );
132  void addFunctions( const QList<Function>& list );
134  void addApis( const QStringList& list );
135 
137  void setSupportMultipleTables( bool bMultipleTables, QString mainTypename = QString() );
138 
142  void setTableSelectedCallback( TableSelectedCallback* tableSelectedCallback );
143 
146  void setSQLValidatorCallback( SQLValidatorCallback* sqlValidatorCallback );
147 
148  protected:
149  bool eventFilter( QObject *obj, QEvent *event ) override;
150 
151  private slots:
152  void accept() override;
153 
154  void on_mTablesCombo_currentIndexChanged( int );
155  void on_mColumnsCombo_currentIndexChanged( int );
156  void on_mSpatialPredicatesCombo_currentIndexChanged( int );
157  void on_mFunctionsCombo_currentIndexChanged( int );
158  void on_mOperatorsCombo_currentIndexChanged( int );
159  void on_mAddJoinButton_clicked();
160  void on_mRemoveJoinButton_clicked();
161  void on_mTableJoins_itemSelectionChanged();
162 
163  void on_mButtonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
164 
165  void reset();
166  void buildSQLFromFields();
167  void splitSQLIntoFields();
168 
169  private:
170  QStringList mApiList;
171  QSet<QString> mAlreadySelectedTables;
172  TableSelectedCallback* mTableSelectedCallback;
173  SQLValidatorCallback* mSQLValidatorCallback;
174  QObject* mFocusedObject;
175  bool mAlreadyModifyingFields;
176  bool mDistinct;
177  QString mResetSql;
178  QMap<QString, QString> mapTableEntryTextToName;
179  QMap<QString, QString> mapColumnEntryTextToName;
180  QMap<QString, QString> mapSpatialPredicateEntryTextToName;
181  QMap<QString, QString> mapFunctionEntryTextToName;
182  QString lastSearchedText;
183 
184 
185  void loadTableColumns( const QString& table );
186  void functionCurrentIndexChanged( QComboBox* combo,
187  const QMap<QString, QString>& mapEntryTextToName );
188  void getFunctionList( const QList<Function>& list,
189  QStringList& listApi,
190  QStringList& listCombo,
191  QMap<QString, QString>& mapEntryTextToName );
192 };
193 
194 #endif
static const Qt::WindowFlags ModalDialogFlags
Definition: qgisgui.h:50
Function(const QString &nameIn, int args)
constructor with name and fixed number of arguments
Callback to do actions on table selection.
Function(const QString &nameIn, int minArgs, int maxArgsIn)
constructor with name and min,max number of arguments
QList< Argument > argumentList
list of arguments. May be empty despite minArgs > 0
SQL composer dialog.
int maxArgs
maximum number of argument (or -1 if unknown)
QPair< QString, QString > PairNameTitle
pair (name, title)
static void run(const QString &context)
QPair< QString, QString > PairNameType
pair (name, type)
description of server functions
Callback to do validation check on dialog validation.
QString returnType
return type, or empty if unknown
typedef WindowFlags
Argument(const QString &nameIn=QString(), const QString &typeIn=QString())
constructor
int minArgs
minimum number of argument (or -1 if unknown)
QString type
type, or empty if unknown