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