1 /***************************************************************************
2     qgsabstractdatasourcewidget.h  -  base class for source selector widgets
3                              -------------------
4     begin                : 10 July 2017
5     original             : (C) 2017 by Alessandro Pasotti
6     email                : apasotti at boundlessgeo dot com
7 
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 QGSABSTRACTDATASOURCEWIDGET_H
20 #define QGSABSTRACTDATASOURCEWIDGET_H
21 
22 #include "qgis_sip.h"
23 #include "qgis_gui.h"
24 
25 #include "qgsproviderguimetadata.h"
26 #include "qgsproviderregistry.h"
27 #include "qgsguiutils.h"
28 #include <QDialog>
29 #include <QDialogButtonBox>
30 
31 class QgsMapCanvas;
32 class QgsBrowserModel;
33 
34 
35 /**
36  * \ingroup gui
37  * \brief  Abstract base Data Source Widget to create connections and add layers
38  * This class provides common functionality and the interface for all
39  * source select dialogs used by data providers to configure data sources
40  * and add layers.
41  * \since QGIS 3.0
42  */
43 class GUI_EXPORT QgsAbstractDataSourceWidget : public QDialog
44 {
45     Q_OBJECT
46 
47   public:
48 
49     /**
50      * Store a pointer to the map canvas to retrieve extent and CRS
51      * Used to select an appropriate CRS and possibly to retrieve data only in the current extent
52      */
53     void setMapCanvas( const QgsMapCanvas *mapCanvas );
54 
55     /**
56      * Sets a browser \a model to use with the widget.
57      *
58      * \see browserModel()
59      * \since QGIS 3.18
60      */
61     void setBrowserModel( QgsBrowserModel *model );
62 
63   public slots:
64 
65     /**
66      * Triggered when the provider's connections need to be refreshed
67      * The default implementation does nothing
68      */
refresh()69     virtual void refresh() {}
70 
71     /**
72      * Triggered when the add button is clicked, the add layer signal is emitted
73      * Concrete classes should implement the right behavior depending on the layer
74      * being added.
75      */
76     virtual void addButtonClicked();
77 
78     /**
79      * Called when this source select widget is being shown in a "new and clean" dialog.
80      *
81      * The data source manager recycles existing source select widgets but will call
82      * this method on every reopening.
83      * This should clear any selection that has previously been done.
84      *
85      * \since QGIS 3.10
86      */
87     virtual void reset();
88 
89   signals:
90 
91     /**
92      * Emitted when the provider's connections have changed
93      * This signal is normally forwarded the app and used to refresh browser items
94      */
95     void connectionsChanged();
96 
97     //! Emitted when a DB layer has been selected for addition
98     void addDatabaseLayers( const QStringList &paths, const QString &providerKey );
99 
100     //! Emitted when a raster layer has been selected for addition
101     void addRasterLayer( const QString &rasterLayerPath, const QString &baseName, const QString &providerKey );
102 
103     /**
104      * Emitted when one or more GDAL supported layers are selected for addition
105      * \param layersList list of layers protocol URIs
106      * \since 3.20
107      */
108     void addRasterLayers( const QStringList &layersList );
109 
110     /**
111      * Emitted when a vector layer has been selected for addition.
112      *
113      * If \a providerKey is not specified, the default provider key associated with the source
114      * will be used.
115      */
116     void addVectorLayer( const QString &uri, const QString &layerName, const QString &providerKey = QString() );
117 
118     /**
119      * Emitted when a mesh layer has been selected for addition.
120      * \since QGIS 3.4
121      */
122     void addMeshLayer( const QString &url, const QString &baseName, const QString &providerKey );
123 
124     /**
125      * Emitted when a vector tile layer has been selected for addition.
126      * \since QGIS 3.14
127      */
128     void addVectorTileLayer( const QString &url, const QString &baseName );
129 
130     /**
131      * Emitted when a point cloud layer has been selected for addition.
132      * \since QGIS 3.18
133      */
134     void addPointCloudLayer( const QString &url, const QString &baseName, const QString &providerKey );
135 
136     /**
137      * Emitted when one or more OGR supported layers are selected for addition
138      * \param layerList list of layers protocol URIs
139      * \param encoding encoding
140      * \param dataSourceType string (can be "file" or "database")
141      */
142     void addVectorLayers( const QStringList &layerList, const QString &encoding, const QString &dataSourceType );
143 
144     /**
145      * Emitted when a layer needs to be replaced
146      * \param oldId old layer ID
147      * \param source URI of the layer
148      * \param name of the layer
149      * \param provider key
150      */
151     void replaceVectorLayer( const QString &oldId, const QString &source, const QString &name, const QString &provider );
152 
153     /**
154      * Emitted when a progress dialog is shown by the provider dialog.
155      *
156      * \deprecated Since QGIS 3.4 this signal is no longer used. Use QgsProxyProgressTask instead to show progress reports.
157      */
158     Q_DECL_DEPRECATED void progress( int, int ) SIP_DEPRECATED;
159 
160     //! Emitted when a progress dialog is shown by the provider dialog
161     void progressMessage( QString message );
162 
163     //! Emitted when the ok/add buttons should be enabled/disabled
164     void enableButtons( bool enable );
165 
166     /**
167      * Emitted when a \a message with \a title and \a level must be shown to the user using the parent visible message bar
168      * \since QGIS 3.14
169      */
170     void pushMessage( const QString &title, const QString &message, const Qgis::MessageLevel level = Qgis::MessageLevel::Info );
171 
172 
173   protected:
174 
175     //! Constructor
176     QgsAbstractDataSourceWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::None );
177 
178     //! Returns the widget mode
179     QgsProviderRegistry::WidgetMode widgetMode() const;
180 
181     //! Returns the map canvas (can be NULLPTR)
182     const QgsMapCanvas *mapCanvas() const;
183 
184     /**
185      * Returns the associated browser model (may be NULLPTR).
186      *
187      * \since QGIS 3.18
188      */
189     QgsBrowserModel *browserModel();
190 
191     //! Connect the ok and apply/add buttons to the slots
192     void setupButtons( QDialogButtonBox *buttonBox );
193 
194     //! Returns the add Button
addButton()195     QPushButton *addButton( ) const { return mAddButton; }
196 
197   private:
198     QPushButton *mAddButton  = nullptr;
199     QgsProviderRegistry::WidgetMode mWidgetMode;
200     QgsMapCanvas const *mMapCanvas = nullptr;
201     QgsBrowserModel *mBrowserModel = nullptr;
202 
203 };
204 
205 #endif // QGSABSTRACTDATASOURCEWIDGET_H
206