From 0ce7f90350bbd1f75b0a63753a9b271eb0106a9a Mon Sep 17 00:00:00 2001 From: Damiano Lombardi Date: Mon, 23 Aug 2021 14:47:22 +0200 Subject: [PATCH] [Attachment widget] Respect relative paths settings by drag&drop (#44798) --- src/gui/qgsfilewidget.cpp | 15 +++++++++++---- src/gui/qgsfilewidget.h | 8 ++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/gui/qgsfilewidget.cpp b/src/gui/qgsfilewidget.cpp index 1caaa42f82c..9ed17f29563 100644 --- a/src/gui/qgsfilewidget.cpp +++ b/src/gui/qgsfilewidget.cpp @@ -59,6 +59,7 @@ QgsFileWidget::QgsFileWidget( QWidget *parent ) mLineEdit->setDragEnabled( true ); mLineEdit->setToolTip( tr( "Full path to the file(s), including name and extension" ) ); connect( mLineEdit, &QLineEdit::textChanged, this, &QgsFileWidget::textEdited ); + connect( mLineEdit, &QgsFileDropEdit::fileDropped, this, &QgsFileWidget::fileDropped ); mLayout->addWidget( mLineEdit ); mLinkEditButton = new QToolButton( this ); @@ -190,6 +191,13 @@ void QgsFileWidget::editLink() updateLayout(); } +void QgsFileWidget::fileDropped( const QString &filePath ) +{ + setSelectedFileNames( QStringList() << filePath ); + mLineEdit->selectAll(); + mLineEdit->setFocus( Qt::MouseFocusReason ); +} + bool QgsFileWidget::useLink() const { return mUseLink; @@ -585,12 +593,11 @@ void QgsFileDropEdit::dropEvent( QDropEvent *event ) QString filePath = acceptableFilePath( event ); if ( !filePath.isEmpty() ) { - setText( filePath ); - selectAll(); - setFocus( Qt::MouseFocusReason ); event->acceptProposedAction(); - setHighlighted( false ); + emit fileDropped( filePath ); } + + setHighlighted( false ); } ///@endcond diff --git a/src/gui/qgsfilewidget.h b/src/gui/qgsfilewidget.h index 99d573bb7ed..1afdcd1f3a0 100644 --- a/src/gui/qgsfilewidget.h +++ b/src/gui/qgsfilewidget.h @@ -298,6 +298,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget void openFileDialog(); void textEdited( const QString &path ); void editLink(); + void fileDropped( const QString &filePath ); protected: @@ -379,6 +380,13 @@ class GUI_EXPORT QgsFileDropEdit: public QgsHighlightableLineEdit void setFilters( const QString &filters ); + signals: + + /** + * Emitted when the file \a filePath is droppen onto the line edit. + */ + void fileDropped( const QString &filePath ); + protected: void dragEnterEvent( QDragEnterEvent *event ) override;