1 /***************************************************************************
2     qgsprocessingmaplayercombobox.h
3     -----------------------------
4     begin                : June 2019
5     copyright            : (C) 2019 by Nyall Dawson
6     email                : nyall dot dawson at gmail dot 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 QGSPROCESSINGMAPLAYERCOMBOBOX_H
17 #define QGSPROCESSINGMAPLAYERCOMBOBOX_H
18 
19 #include "qgis.h"
20 #include "qgis_gui.h"
21 #include <QTreeView>
22 #include "qgsprocessingtoolboxmodel.h"
23 #include "qgsfeatureid.h"
24 #include "qgsmimedatautils.h"
25 #include "qgsprocessingcontext.h"
26 #include "qgsprocessinggui.h"
27 
28 class QgsMapLayerComboBox;
29 class QToolButton;
30 class QCheckBox;
31 class QgsProcessingParameterDefinition;
32 class QgsBrowserGuiModel;
33 class QgsProcessingParameterWidgetContext;
34 
35 ///@cond PRIVATE
36 
37 /**
38  * Processing map layer combo box.
39  * \ingroup gui
40  * \warning Not part of stable API and may change in future QGIS releases.
41  * \since QGIS 3.8
42  */
43 class GUI_EXPORT QgsProcessingMapLayerComboBox : public QWidget
44 {
45     Q_OBJECT
46 
47   public:
48 
49     /**
50      * Constructor for QgsProcessingMapLayerComboBox, with the specified \a parameter definition.
51      */
52     QgsProcessingMapLayerComboBox( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type = QgsProcessingGui::Standard, QWidget *parent = nullptr );
53 
54     ~QgsProcessingMapLayerComboBox() override;
55 
56     /**
57      * Sets the combo box to the specified \a layer, if \a layer is compatible with the
58      * widget's parameter definition.
59      */
60     void setLayer( QgsMapLayer *layer );
61 
62     /**
63      * Returns the current layer selected in the combobox, or NULLPTR if the selection cannot
64      * be represented as a map layer.
65      *
66      * \warning Prefer calling value() instead, as it correctly encapsulates all valid
67      * values which can be represented by the widget.
68      *
69      * \see currentText()
70      */
71     QgsMapLayer *currentLayer();
72 
73     /**
74      * Returns the current text of the selected item in the combobox.
75      *
76      * \warning Prefer calling value() instead, as it correctly encapsulates all valid
77      * values which can be represented by the widget.
78      *
79      * \see currentLayer()
80      */
81     QString currentText();
82 
83     /**
84      * Sets the \a value shown in the widget.
85      *
86      * \see value()
87      */
88     void setValue( const QVariant &value, QgsProcessingContext &context );
89 
90     /**
91      * Returns the current value of the widget.
92      *
93      * \see setValue()
94      */
95     QVariant value() const;
96 
97     /**
98      * Sets the \a context in which the widget is shown.
99      * \since QGIS 3.14
100      */
101     void setWidgetContext( const QgsProcessingParameterWidgetContext &context );
102 
103     /**
104      * Sets whether the combo box value can be freely edited.
105      *
106      * \see isEditable()
107      * \since QGIS 3.14
108      */
109     void setEditable( bool editable );
110 
111     /**
112      * Returns whether the combo box value can be freely edited.
113      *
114      * \see setEditable()
115      * \since QGIS 3.14
116      */
117     bool isEditable() const;
118 
119   signals:
120 
121     /**
122      * Emitted whenever the value is changed in the widget.
123      */
124     void valueChanged();
125 
126   protected:
127 
128     void dragEnterEvent( QDragEnterEvent *event ) override;
129     void dragLeaveEvent( QDragLeaveEvent *event ) override;
130     void dropEvent( QDropEvent *event ) override;
131 
132   private slots:
133 
134     void onLayerChanged( QgsMapLayer *layer );
135     void selectionChanged( const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect );
136     void showSourceOptions();
137     void selectFromFile();
138     void browseForLayer();
139 
140   private:
141     std::unique_ptr< QgsProcessingParameterDefinition > mParameter;
142     QgsMapLayerComboBox *mCombo = nullptr;
143     QToolButton *mSelectButton = nullptr;
144     QToolButton *mIterateButton = nullptr;
145     QToolButton *mSettingsButton = nullptr;
146     QCheckBox *mUseSelectionCheckBox = nullptr;
147     bool mDragActive = false;
148     long long mFeatureLimit = -1;
149     bool mIsOverridingDefaultGeometryCheck = false;
150     QgsFeatureRequest::InvalidGeometryCheck mGeometryCheck = QgsFeatureRequest::GeometryAbortOnInvalid;
151     QPointer< QgsMapLayer> mPrevLayer;
152     int mBlockChangedSignal = 0;
153 
154     QgsBrowserGuiModel *mBrowserModel = nullptr;
155 
156     QMenu *mFeatureSourceMenu = nullptr;
157     QgsMapLayer *compatibleMapLayerFromMimeData( const QMimeData *data, bool &incompatibleLayerSelected ) const;
158     QString compatibleUriFromMimeData( const QMimeData *data ) const;
159 };
160 
161 ///@endcond
162 #endif // QGSPROCESSINGMAPLAYERCOMBOBOX_H
163