Skip to content
Permalink
Browse files

Support dropping file links into rich text edit widget

  • Loading branch information
nyalldawson committed Apr 22, 2021
1 parent 076bd2b commit 014ec17ed1ba84acb695b1ea1daa753c81e9c50a
Showing with 27 additions and 5 deletions.
  1. +26 −5 src/gui/qgsimagedroptextedit.cpp
  2. +1 −0 src/gui/qgsimagedroptextedit.h
@@ -154,27 +154,41 @@ void QgsImageDropTextEdit::insertFromMimeData( const QMimeData *source )
files.reserve( urls.size() );
for ( const QUrl &url : urls )
{
QString fileName = url.toLocalFile();
// seems that some drag and drop operations include an empty url
// so we test for length to make sure we have something
if ( !fileName.isEmpty() )
if ( url.isLocalFile() )
{
files << fileName;
QString fileName = url.toLocalFile();
// seems that some drag and drop operations include an empty url
// so we test for length to make sure we have something
if ( !fileName.isEmpty() )
{
files << fileName;
}
}
else
{
dropLink( url );
}
}

for ( const QString &file : std::as_const( files ) )
{
const QFileInfo fi( file );
const QList<QByteArray> formats = QImageReader::supportedImageFormats();
bool isImage = false;
for ( const QByteArray &format : formats )
{
if ( fi.suffix().compare( format, Qt::CaseInsensitive ) == 0 )
{
const QImage image( file );
dropImage( image, format );
isImage = true;
break;
}
}
if ( !isImage )
{
dropLink( QUrl::fromLocalFile( file ) );
}
}
if ( !files.empty() )
return;
@@ -211,4 +225,11 @@ void QgsImageDropTextEdit::dropImage( const QImage &image, const QString &format
);
cursor.insertImage( imageFormat );
}

void QgsImageDropTextEdit::dropLink( const QUrl &url )
{
QTextCursor cursor = textCursor();
cursor.insertHtml( QStringLiteral( "<a href=\"%1\">%1</a>" ).arg( url.toString() ) );
}

///@endcond
@@ -51,6 +51,7 @@ class GUI_EXPORT QgsImageDropTextEdit : public QTextEdit
QgsImageDropTextEdit( QWidget *parent = nullptr );

void dropImage( const QImage &image, const QString &format );
void dropLink( const QUrl &url );

protected:
bool canInsertFromMimeData( const QMimeData *source ) const override;

0 comments on commit 014ec17

Please sign in to comment.