15
15
16
16
#include " qgsmaplayercombobox.h"
17
17
#include " qgsmaplayermodel.h"
18
-
18
+ #include " qgsmimedatautils.h"
19
+ #include < QDragEnterEvent>
20
+ #include < QPainter>
19
21
20
22
QgsMapLayerComboBox::QgsMapLayerComboBox ( QWidget *parent )
21
23
: QComboBox( parent )
@@ -26,6 +28,8 @@ QgsMapLayerComboBox::QgsMapLayerComboBox( QWidget *parent )
26
28
connect ( this , static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::activated ), this , &QgsMapLayerComboBox::indexChanged );
27
29
connect ( mProxyModel , &QAbstractItemModel::rowsInserted, this , &QgsMapLayerComboBox::rowsChanged );
28
30
connect ( mProxyModel , &QAbstractItemModel::rowsRemoved, this , &QgsMapLayerComboBox::rowsChanged );
31
+
32
+ setAcceptDrops ( true );
29
33
}
30
34
31
35
void QgsMapLayerComboBox::setExcludedProviders ( const QStringList &providers )
@@ -138,3 +142,74 @@ void QgsMapLayerComboBox::rowsChanged()
138
142
}
139
143
}
140
144
145
+ QgsMapLayer *QgsMapLayerComboBox::compatibleMapLayerFromMimeData ( const QMimeData *data ) const
146
+ {
147
+ const QgsMimeDataUtils::UriList uriList = QgsMimeDataUtils::decodeUriList ( data );
148
+ for ( const QgsMimeDataUtils::Uri &u : uriList )
149
+ {
150
+ // is this uri from the current project?
151
+ if ( QgsMapLayer *layer = u.mapLayer () )
152
+ {
153
+ if ( mProxyModel ->acceptsLayer ( layer ) )
154
+ return layer;
155
+ }
156
+ }
157
+ return nullptr ;
158
+ }
159
+
160
+ void QgsMapLayerComboBox::dragEnterEvent ( QDragEnterEvent *event )
161
+ {
162
+ if ( !( event->possibleActions () & Qt::CopyAction ) )
163
+ return ;
164
+
165
+ if ( compatibleMapLayerFromMimeData ( event->mimeData () ) )
166
+ {
167
+ // dragged an acceptable layer, phew
168
+ event->setDropAction ( Qt::CopyAction );
169
+ event->accept ();
170
+ mDragActive = true ;
171
+ update ();
172
+ }
173
+ }
174
+
175
+ void QgsMapLayerComboBox::dragLeaveEvent ( QDragLeaveEvent *event )
176
+ {
177
+ QComboBox::dragLeaveEvent ( event );
178
+ if ( mDragActive )
179
+ {
180
+ event->accept ();
181
+ mDragActive = false ;
182
+ update ();
183
+ }
184
+ }
185
+
186
+ void QgsMapLayerComboBox::dropEvent ( QDropEvent *event )
187
+ {
188
+ if ( !( event->possibleActions () & Qt::CopyAction ) )
189
+ return ;
190
+
191
+ if ( QgsMapLayer *layer = compatibleMapLayerFromMimeData ( event->mimeData () ) )
192
+ {
193
+ // dropped an acceptable layer, phew
194
+ setFocus ( Qt::MouseFocusReason );
195
+ event->setDropAction ( Qt::CopyAction );
196
+ event->accept ();
197
+
198
+ setLayer ( layer );
199
+ }
200
+ mDragActive = false ;
201
+ update ();
202
+ }
203
+
204
+ void QgsMapLayerComboBox::paintEvent ( QPaintEvent *e )
205
+ {
206
+ QComboBox::paintEvent ( e );
207
+ if ( mDragActive )
208
+ {
209
+ QPainter p ( this );
210
+ int width = 2 ; // width of highlight rectangle inside frame
211
+ p.setPen ( QPen ( palette ().highlight (), width ) );
212
+ QRect r = rect ().adjusted ( width, width, -width, -width );
213
+ p.drawRect ( r );
214
+ }
215
+ }
0 commit comments