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