Skip to content

Commit

Permalink
Support dropping file links into rich text edit widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 22, 2021
1 parent 076bd2b commit 014ec17
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/gui/qgsimagedroptextedit.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions src/gui/qgsimagedroptextedit.h
Expand Up @@ -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;
Expand Down

0 comments on commit 014ec17

Please sign in to comment.