1 /***************************************************************************
2    qgsmaplayercombobox.h
3     --------------------------------------
4    Date                 : 01.04.2014
5    Copyright            : (C) 2014 Denis Rouzaud
6    Email                : denis.rouzaud@gmail.com
7 ***************************************************************************
8 *                                                                         *
9 *   This program is free software; you can redistribute it and/or modify  *
10 *   it under the terms of the GNU General Public License as published by  *
11 *   the Free Software Foundation; either version 2 of the License, or     *
12 *   (at your option) any later version.                                   *
13 *                                                                         *
14 ***************************************************************************/
15 
16 #ifndef QGSMAPLAYERCOMBOBOX_H
17 #define QGSMAPLAYERCOMBOBOX_H
18 
19 #include <QComboBox>
20 
21 #include "qgsmaplayerproxymodel.h"
22 #include "qgis_gui.h"
23 
24 #include "qgis_sip.h"
25 
26 class QgsMapLayer;
27 class QgsVectorLayer;
28 
29 /**
30  * \ingroup gui
31  * \brief The QgsMapLayerComboBox class is a combo box which displays the list of layers
32  * \since QGIS 2.3
33  */
34 class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
35 {
36     Q_OBJECT
37     Q_PROPERTY( QgsMapLayerProxyModel::Filters filters READ filters WRITE setFilters )
38     Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
39     Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
40     Q_PROPERTY( QStringList excludedProviders READ excludedProviders WRITE setExcludedProviders )
41 
42   public:
43 
44     /**
45      * \brief QgsMapLayerComboBox creates a combo box to display the list of layers (currently in the registry).
46      * The layers can be filtered and/or ordered.
47      */
48     explicit QgsMapLayerComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );
49 
50     //! setFilters allows filtering according to layer type and/or geometry type.
setFilters(QgsMapLayerProxyModel::Filters filters)51     void setFilters( QgsMapLayerProxyModel::Filters filters ) { mProxyModel->setFilters( filters ); }
52 
53     //! currently used filter on list layers
filters()54     QgsMapLayerProxyModel::Filters filters() const { return mProxyModel->filters(); }
55 
56     //! except a list of layers not to be listed
setExceptedLayerList(const QList<QgsMapLayer * > & layerList)57     void setExceptedLayerList( const QList<QgsMapLayer *> &layerList ) { mProxyModel->setExceptedLayerList( layerList );}
58 
59     //! returns the list of excepted layers
exceptedLayerList()60     QList<QgsMapLayer *> exceptedLayerList() const {return mProxyModel->exceptedLayerList();}
61 
62     /**
63      * Sets a list of data providers which should be excluded from the combobox.
64      * \see excludedProviders()
65      * \since QGIS 3.0
66      */
67     void setExcludedProviders( const QStringList &providers );
68 
69     /**
70      * Returns the list of data providers which are excluded from the combobox.
71      * \see setExcludedProviders()
72      * \since QGIS 3.0
73      */
74     QStringList excludedProviders() const;
75 
76     /**
77      * Sets whether an optional empty layer ("not set") option is shown in the combo box.
78      * \see allowEmptyLayer()
79      * \since QGIS 3.0
80      */
81     void setAllowEmptyLayer( bool allowEmpty );
82 
83     /**
84      * Returns TRUE if the combo box allows the empty layer ("not set") choice.
85      * \see setAllowEmptyLayer()
86      * \since QGIS 3.0
87      */
88     bool allowEmptyLayer() const;
89 
90     /**
91      * Sets whether the CRS of layers is also included in the combo box text.
92      * \see showCrs()
93      * \since QGIS 3.0
94      */
95     void setShowCrs( bool showCrs );
96 
97     /**
98      * Returns TRUE if the combo box shows the layer's CRS.
99      * \see setShowCrs()
100      * \since QGIS 3.0
101      */
102     bool showCrs() const;
103 
104     /**
105      * Sets a list of additional (non map layer) items to include at the end of the combobox.
106      * These may represent additional layers such as layers which are not included in the map
107      * layer registry, or paths to layers which have not yet been loaded into QGIS.
108      * \see additionalItems()
109      * \since QGIS 3.0
110      */
111     void setAdditionalItems( const QStringList &items );
112 
113     /**
114      * Returns the list of additional (non map layer) items included at the end of the combo box.
115      * \see setAdditionalItems()
116      * \since QGIS 3.0
117      */
118     QStringList additionalItems() const;
119 
120     /**
121      * Returns the current layer selected in the combo box.
122      * \see layer
123      */
124     QgsMapLayer *currentLayer() const;
125 
126     /**
127      * Returns the layer currently shown at the specified index within the combo box.
128      * \param layerIndex position of layer to return
129      * \see currentLayer
130      * \since QGIS 2.10
131      */
132     QgsMapLayer *layer( int layerIndex ) const;
133 
134   public slots:
135     //! setLayer set the current layer selected in the combo
136     void setLayer( QgsMapLayer *layer );
137 
138   signals:
139     //! Emitted whenever the currently selected layer changes.
140     void layerChanged( QgsMapLayer *layer );
141 
142   protected:
143 
144     void dragEnterEvent( QDragEnterEvent *event ) override;
145     void dragLeaveEvent( QDragLeaveEvent *event ) override;
146     void dropEvent( QDropEvent *event ) override;
147     void paintEvent( QPaintEvent *e ) override;
148 
149   protected slots:
150     void indexChanged( int i );
151     void rowsChanged();
152 
153   private:
154     QgsMapLayerProxyModel *mProxyModel = nullptr;
155     bool mDragActive = false;
156     bool mHighlight = false;
157 
158     /**
159      * Returns a map layer, compatible with the filters set for the combo box, from
160      * the specified mime \a data (if possible!).
161      */
162     QgsMapLayer *compatibleMapLayerFromMimeData( const QMimeData *data ) const;
163 
164     friend class QgsProcessingMapLayerComboBox;
165 };
166 
167 #endif // QGSMAPLAYERCOMBOBOX_H
168