QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgsmaplayercombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayercombobox.cpp
3 --------------------------------------
4 Date : 01.04.2014
5 Copyright : (C) 2014 Denis Rouzaud
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#include "qgsmaplayercombobox.h"
17#include "qgsmaplayermodel.h"
18#include "qgsmimedatautils.h"
19#include <QDragEnterEvent>
20#include <QPainter>
21
22
24 : QComboBox( parent )
25{
26 mProxyModel = new QgsMapLayerProxyModel( this );
27 setModel( mProxyModel );
28
29 connect( this, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this, &QgsMapLayerComboBox::indexChanged );
30 connect( mProxyModel, &QAbstractItemModel::rowsInserted, this, &QgsMapLayerComboBox::rowsChanged );
31 connect( mProxyModel, &QAbstractItemModel::rowsRemoved, this, &QgsMapLayerComboBox::rowsChanged );
32
33 setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon );
34
35 setAcceptDrops( true );
36}
37
38void QgsMapLayerComboBox::setExcludedProviders( const QStringList &providers )
39{
40 mProxyModel->setExcludedProviders( providers );
41}
42
44{
45 mProxyModel->setProject( project );
46}
47
48
50{
51 return mProxyModel->excludedProviders();
52}
53
54void QgsMapLayerComboBox::setAllowEmptyLayer( bool allowEmpty, const QString &text, const QIcon &icon )
55{
56 mProxyModel->sourceLayerModel()->setAllowEmptyLayer( allowEmpty, text, icon );
57}
58
60{
61 return mProxyModel->sourceLayerModel()->allowEmptyLayer();
62}
63
65{
66 mProxyModel->sourceLayerModel()->setShowCrs( showCrs );
67}
68
70{
71 return mProxyModel->sourceLayerModel()->showCrs();
72}
73
74void QgsMapLayerComboBox::setAdditionalItems( const QStringList &items )
75{
76 mProxyModel->sourceLayerModel()->setAdditionalItems( items );
77}
78
80{
81 return mProxyModel->sourceLayerModel()->additionalItems();
82}
83
84void QgsMapLayerComboBox::setAdditionalLayers( const QList<QgsMapLayer *> &layers )
85{
86 mProxyModel->sourceLayerModel()->setAdditionalLayers( layers );
87}
88
89QList<QgsMapLayer *> QgsMapLayerComboBox::additionalLayers() const
90{
91 return mProxyModel->sourceLayerModel()->additionalLayers();
92}
93
95{
96 if ( layer == currentLayer() && ( layer || !isEditable() || currentText().isEmpty() ) )
97 return;
98
99 if ( !layer )
100 {
101 setCurrentIndex( -1 );
102 emit layerChanged( nullptr );
103 return;
104 }
105
106 const QModelIndex idx = mProxyModel->sourceLayerModel()->indexFromLayer( layer );
107 if ( idx.isValid() )
108 {
109 const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
110 if ( proxyIdx.isValid() )
111 {
112 setCurrentIndex( proxyIdx.row() );
113 emit layerChanged( currentLayer() );
114 return;
115 }
116 }
117 setCurrentIndex( -1 );
118 emit layerChanged( currentLayer() );
119}
120
122{
123 return layer( currentIndex() );
124}
125
127{
128 const QModelIndex proxyIndex = mProxyModel->index( layerIndex, 0 );
129 if ( !proxyIndex.isValid() )
130 {
131 return nullptr;
132 }
133
134 const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
135 if ( !index.isValid() )
136 {
137 return nullptr;
138 }
139
140 QgsMapLayer *layer = static_cast<QgsMapLayer *>( index.internalPointer() );
141 if ( layer )
142 {
143 return layer;
144 }
145 return nullptr;
146}
147
149{
150 Q_UNUSED( i )
152 emit layerChanged( layer );
153}
154
156{
157 if ( count() == 1 )
158 {
159 //currently selected layer item has changed
160 emit layerChanged( currentLayer() );
161 }
162 else if ( count() == 0 )
163 {
164 emit layerChanged( nullptr );
165 }
166}
167
168QgsMapLayer *QgsMapLayerComboBox::compatibleMapLayerFromMimeData( const QMimeData *data ) const
169{
171 for ( const QgsMimeDataUtils::Uri &u : uriList )
172 {
173 // is this uri from the current project?
174 if ( QgsMapLayer *layer = u.mapLayer() )
175 {
176 if ( mProxyModel->acceptsLayer( layer ) )
177 return layer;
178 }
179 }
180 return nullptr;
181}
182
183void QgsMapLayerComboBox::dragEnterEvent( QDragEnterEvent *event )
184{
185 if ( !( event->possibleActions() & Qt::CopyAction ) )
186 {
187 event->ignore();
188 return;
189 }
190
191 if ( compatibleMapLayerFromMimeData( event->mimeData() ) )
192 {
193 // dragged an acceptable layer, phew
194 event->setDropAction( Qt::CopyAction );
195 event->accept();
196 mDragActive = true;
197 update();
198 }
199 else
200 {
201 event->ignore();
202 }
203}
204
205void QgsMapLayerComboBox::dragLeaveEvent( QDragLeaveEvent *event )
206{
207 if ( mDragActive )
208 {
209 event->accept();
210 mDragActive = false;
211 update();
212 }
213 else
214 {
215 event->ignore();
216 }
217}
218
219void QgsMapLayerComboBox::dropEvent( QDropEvent *event )
220{
221 if ( !( event->possibleActions() & Qt::CopyAction ) )
222 {
223 event->ignore();
224 return;
225 }
226
227 if ( QgsMapLayer *layer = compatibleMapLayerFromMimeData( event->mimeData() ) )
228 {
229 // dropped an acceptable layer, phew
230 setFocus( Qt::MouseFocusReason );
231 event->setDropAction( Qt::CopyAction );
232 event->accept();
233
234 setLayer( layer );
235 }
236 else
237 {
238 event->ignore();
239 }
240 mDragActive = false;
241 update();
242}
243
245{
246 QComboBox::paintEvent( e );
247 if ( mDragActive || mHighlight )
248 {
249 QPainter p( this );
250 const int width = 2; // width of highlight rectangle inside frame
251 p.setPen( QPen( palette().highlight(), width ) );
252 const QRect r = rect().adjusted( width, width, -width, -width );
253 p.drawRect( r );
254 }
255}
void dragLeaveEvent(QDragLeaveEvent *event) override
QStringList additionalItems() const
Returns the list of additional (non map layer) items included at the end of the combo box.
void setAdditionalLayers(const QList< QgsMapLayer * > &layers)
Sets a list of additional layers to include in the combobox.
QgsMapLayerComboBox(QWidget *parent=nullptr)
QgsMapLayerComboBox creates a combo box to display the list of layers (currently in the registry).
void paintEvent(QPaintEvent *e) override
void setLayer(QgsMapLayer *layer)
setLayer set the current layer selected in the combo
void setProject(QgsProject *project)
Sets the project from which map layers are shown.
void setExcludedProviders(const QStringList &providers)
Sets a list of data providers which should be excluded from the combobox.
void dropEvent(QDropEvent *event) override
QgsMapLayer * layer(int layerIndex) const
Returns the layer currently shown at the specified index within the combo box.
void setShowCrs(bool showCrs)
Sets whether the CRS of layers is also included in the combo box text.
void setAllowEmptyLayer(bool allowEmpty, const QString &text=QString(), const QIcon &icon=QIcon())
Sets whether an optional empty layer ("not set") option is shown in the combo box.
void setAdditionalItems(const QStringList &items)
Sets a list of additional (non map layer) items to include at the end of the combobox.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
QList< QgsMapLayer * > additionalLayers() const
Returns the list of additional layers added to the combobox.
void dragEnterEvent(QDragEnterEvent *event) override
QgsMapLayer * currentLayer() const
Returns the current layer selected in the combo box.
void setShowCrs(bool showCrs)
Sets whether the CRS of layers is also included in the model's display role.
QModelIndex indexFromLayer(QgsMapLayer *layer) const
indexFromLayer returns the model index for a given layer
void setAllowEmptyLayer(bool allowEmpty, const QString &text=QString(), const QIcon &icon=QIcon())
Sets whether an optional empty layer ("not set") option is present in the model.
void setAdditionalItems(const QStringList &items)
Sets a list of additional (non map layer) items to include at the end of the model.
QList< QgsMapLayer * > additionalLayers() const
Returns the list of additional layers added to the model.
QStringList additionalItems
void setAdditionalLayers(const QList< QgsMapLayer * > &layers)
Sets a list of additional layers to include in the model.
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
bool acceptsLayer(QgsMapLayer *layer) const
Returns true if the proxy model accepts the specified map layer.
void setExcludedProviders(const QStringList &providers)
Sets a blocklist of data providers which should be excluded from the model.
QgsMapLayerModel * sourceLayerModel() const
layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel
void setProject(QgsProject *project)
Sets the project from which map layers are shown.
QStringList excludedProviders() const
Returns the blocklist of data providers which are excluded from the model.
Base class for all map layer types.
Definition qgsmaplayer.h:74
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the layer.
QList< QgsMimeDataUtils::Uri > UriList
static UriList decodeUriList(const QMimeData *data)
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107