QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 "moc_qgsmaplayercombobox.cpp"
18#include "qgsmaplayermodel.h"
19#include "qgsmimedatautils.h"
20#include <QDragEnterEvent>
21#include <QPainter>
22
23
25 : QComboBox( parent )
26{
27 mProxyModel = new QgsMapLayerProxyModel( this );
28 setModel( mProxyModel );
29
30 connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsMapLayerComboBox::indexChanged );
31 connect( mProxyModel, &QAbstractItemModel::rowsInserted, this, &QgsMapLayerComboBox::rowsChanged );
32 connect( mProxyModel, &QAbstractItemModel::rowsRemoved, this, &QgsMapLayerComboBox::rowsChanged );
33
34 setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon );
35
36 setAcceptDrops( true );
37}
38
39void QgsMapLayerComboBox::setExcludedProviders( const QStringList &providers )
40{
41 mProxyModel->setExcludedProviders( providers );
42}
43
45{
46 mProxyModel->setProject( project );
47}
48
49
51{
52 return mProxyModel->excludedProviders();
53}
54
55void QgsMapLayerComboBox::setAllowEmptyLayer( bool allowEmpty, const QString &text, const QIcon &icon )
56{
57 mProxyModel->sourceLayerModel()->setAllowEmptyLayer( allowEmpty, text, icon );
58}
59
61{
62 return mProxyModel->sourceLayerModel()->allowEmptyLayer();
63}
64
66{
67 mProxyModel->sourceLayerModel()->setShowCrs( showCrs );
68}
69
71{
72 return mProxyModel->sourceLayerModel()->showCrs();
73}
74
75void QgsMapLayerComboBox::setAdditionalItems( const QStringList &items )
76{
77 mProxyModel->sourceLayerModel()->setAdditionalItems( items );
78}
79
81{
82 return mProxyModel->sourceLayerModel()->additionalItems();
83}
84
85void QgsMapLayerComboBox::setAdditionalLayers( const QList<QgsMapLayer *> &layers )
86{
87 mProxyModel->sourceLayerModel()->setAdditionalLayers( layers );
88}
89
90QList<QgsMapLayer *> QgsMapLayerComboBox::additionalLayers() const
91{
92 return mProxyModel->sourceLayerModel()->additionalLayers();
93}
94
96{
97 if ( layer == currentLayer() && ( layer || !isEditable() || currentText().isEmpty() ) )
98 return;
99
100 if ( !layer )
101 {
102 setCurrentIndex( -1 );
103 emit layerChanged( nullptr );
104 return;
105 }
106
107 const QModelIndex idx = mProxyModel->sourceLayerModel()->indexFromLayer( layer );
108 if ( idx.isValid() )
109 {
110 const QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
111 if ( proxyIdx.isValid() )
112 {
113 setCurrentIndex( proxyIdx.row() );
114 emit layerChanged( currentLayer() );
115 return;
116 }
117 }
118 setCurrentIndex( -1 );
119 emit layerChanged( currentLayer() );
120}
121
123{
124 return layer( currentIndex() );
125}
126
128{
129 const QModelIndex proxyIndex = mProxyModel->index( layerIndex, 0 );
130 if ( !proxyIndex.isValid() )
131 {
132 return nullptr;
133 }
134
135 const QModelIndex index = mProxyModel->mapToSource( proxyIndex );
136 if ( !index.isValid() )
137 {
138 return nullptr;
139 }
140
141 QgsMapLayer *layer = static_cast<QgsMapLayer *>( index.internalPointer() );
142 if ( layer )
143 {
144 return layer;
145 }
146 return nullptr;
147}
148
150{
151 Q_UNUSED( i )
153 emit layerChanged( layer );
154}
155
157{
158 if ( count() == 1 )
159 {
160 //currently selected layer item has changed
161 emit layerChanged( currentLayer() );
162 }
163 else if ( count() == 0 )
164 {
165 emit layerChanged( nullptr );
166 }
167}
168
169QgsMapLayer *QgsMapLayerComboBox::compatibleMapLayerFromMimeData( const QMimeData *data ) const
170{
172 for ( const QgsMimeDataUtils::Uri &u : uriList )
173 {
174 // is this uri from the current project?
175 if ( QgsMapLayer *layer = u.mapLayer() )
176 {
177 if ( mProxyModel->acceptsLayer( layer ) )
178 return layer;
179 }
180 }
181 return nullptr;
182}
183
184void QgsMapLayerComboBox::dragEnterEvent( QDragEnterEvent *event )
185{
186 if ( !( event->possibleActions() & Qt::CopyAction ) )
187 {
188 event->ignore();
189 return;
190 }
191
192 if ( compatibleMapLayerFromMimeData( event->mimeData() ) )
193 {
194 // dragged an acceptable layer, phew
195 event->setDropAction( Qt::CopyAction );
196 event->accept();
197 mDragActive = true;
198 update();
199 }
200 else
201 {
202 event->ignore();
203 }
204}
205
206void QgsMapLayerComboBox::dragLeaveEvent( QDragLeaveEvent *event )
207{
208 if ( mDragActive )
209 {
210 event->accept();
211 mDragActive = false;
212 update();
213 }
214 else
215 {
216 event->ignore();
217 }
218}
219
220void QgsMapLayerComboBox::dropEvent( QDropEvent *event )
221{
222 if ( !( event->possibleActions() & Qt::CopyAction ) )
223 {
224 event->ignore();
225 return;
226 }
227
228 if ( QgsMapLayer *layer = compatibleMapLayerFromMimeData( event->mimeData() ) )
229 {
230 // dropped an acceptable layer, phew
231 setFocus( Qt::MouseFocusReason );
232 event->setDropAction( Qt::CopyAction );
233 event->accept();
234
235 setLayer( layer );
236 }
237 else
238 {
239 event->ignore();
240 }
241 mDragActive = false;
242 update();
243}
244
246{
247 QComboBox::paintEvent( e );
248 if ( mDragActive || mHighlight )
249 {
250 QPainter p( this );
251 const int width = 2; // width of highlight rectangle inside frame
252 p.setPen( QPen( palette().highlight(), width ) );
253 const QRect r = rect().adjusted( width, width, -width, -width );
254 p.drawRect( r );
255 }
256}
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)
Sets the current layer selected in the combo box.
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:76
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