Skip to content
Permalink
Browse files

Allow dragging and dropping project layers to QgsExtentWidget to copy…

… extent from layer
  • Loading branch information
nyalldawson committed Mar 25, 2020
1 parent 9e4e32f commit 945eb460fee7d2340a4fce694e1184fbabfb0fed
@@ -205,6 +205,15 @@ Emitted when the widget's extent is changed.
Emitted when the widget's validation state changes.
%End

protected:

virtual void dragEnterEvent( QDragEnterEvent *event );

virtual void dragLeaveEvent( QDragLeaveEvent *event );

virtual void dropEvent( QDropEvent *event );


};

/************************************************************************
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>249</width>
<height>23</height>
<height>27</height>
</rect>
</property>
<property name="windowTitle">
@@ -17,7 +17,16 @@
<property name="spacing">
<number>6</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
@@ -38,6 +38,7 @@ QgsExtentWidget::QgsExtentWidget( QWidget *parent, WidgetStyle style )

mCondensedRe = QRegularExpression( QStringLiteral( "\\s*([\\d\\.]+)\\s*,\\s*([\\d\\.]+)\\s*,\\s*([\\d\\.]+)\\s*,\\s*([\\d\\.]+)\\s*(\\[.*?\\])" ) );
mCondensedLineEdit->setValidator( new QRegularExpressionValidator( mCondensedRe, this ) );
mCondensedLineEdit->setShowClearButton( false );

connect( mCondensedLineEdit, &QLineEdit::textEdited, this, &QgsExtentWidget::setOutputExtentFromCondensedLineEdit );

@@ -84,6 +85,8 @@ QgsExtentWidget::QgsExtentWidget( QWidget *parent, WidgetStyle style )
mCondensedFrame->hide();
break;
}

setAcceptDrops( true );
}

void QgsExtentWidget::setOriginalExtent( const QgsRectangle &originalExtent, const QgsCoordinateReferenceSystem &originalCrs )
@@ -295,6 +298,20 @@ void QgsExtentWidget::setExtentToLayerExtent( const QString &layerId )
setOutputExtentFromLayer( layer );
}

QgsMapLayer *QgsExtentWidget::mapLayerFromMimeData( const QMimeData *data ) const
{
const QgsMimeDataUtils::UriList uriList = QgsMimeDataUtils::decodeUriList( data );
for ( const QgsMimeDataUtils::Uri &u : uriList )
{
// is this uri from the current project?
if ( QgsMapLayer *layer = u.mapLayer() )
{
return layer;
}
}
return nullptr;
}

void QgsExtentWidget::setOutputExtentFromCurrent()
{
if ( mCanvas )
@@ -386,3 +403,64 @@ void QgsExtentWidget::setMapCanvas( QgsMapCanvas *canvas )
mMenu->removeAction( mDrawOnCanvasAction );
}
}

void QgsExtentWidget::dragEnterEvent( QDragEnterEvent *event )
{
if ( !( event->possibleActions() & Qt::CopyAction ) )
{
event->ignore();
return;
}

if ( mapLayerFromMimeData( event->mimeData() ) )
{
// dragged an acceptable layer, phew
event->setDropAction( Qt::CopyAction );
event->accept();
mCondensedLineEdit->setHighlighted( true );
update();
}
else
{
event->ignore();
}
}

void QgsExtentWidget::dragLeaveEvent( QDragLeaveEvent *event )
{
if ( mCondensedLineEdit->isHighlighted() )
{
event->accept();
mCondensedLineEdit->setHighlighted( false );
update();
}
else
{
event->ignore();
}
}

void QgsExtentWidget::dropEvent( QDropEvent *event )
{
if ( !( event->possibleActions() & Qt::CopyAction ) )
{
event->ignore();
return;
}

if ( QgsMapLayer *layer = mapLayerFromMimeData( event->mimeData() ) )
{
// dropped a map layer
setFocus( Qt::MouseFocusReason );
event->setDropAction( Qt::CopyAction );
event->accept();

setOutputExtentFromLayer( layer );
}
else
{
event->ignore();
}
mCondensedLineEdit->setHighlighted( false );
update();
}
@@ -212,6 +212,12 @@ class GUI_EXPORT QgsExtentWidget : public QWidget, private Ui::QgsExtentGroupBox
*/
void validationChanged( bool valid );

protected:

void dragEnterEvent( QDragEnterEvent *event ) override;
void dragLeaveEvent( QDragLeaveEvent *event ) override;
void dropEvent( QDropEvent *event ) override;

private slots:

void layerMenuAboutToShow();
@@ -257,6 +263,9 @@ class GUI_EXPORT QgsExtentWidget : public QWidget, private Ui::QgsExtentGroupBox

void setExtentToLayerExtent( const QString &layerId );

QgsMapLayer *mapLayerFromMimeData( const QMimeData *data ) const;


};

#endif // QGSEXTENTWIDGET_H
@@ -227,7 +227,7 @@
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="mCondensedLineEdit">
<widget class="QgsHighlightableLineEdit" name="mCondensedLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
@@ -254,6 +254,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsHighlightableLineEdit</class>
<extends>QLineEdit</extends>
<header>qgshighlightablelineedit.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mYMaxLineEdit</tabstop>
<tabstop>mXMinLineEdit</tabstop>

0 comments on commit 945eb46

Please sign in to comment.
You can’t perform that action at this time.