Skip to content
Permalink
Browse files
Ensure that external links are clickable in rich text edit
  • Loading branch information
nyalldawson committed Apr 22, 2021
1 parent 014ec17 commit c12a3fdd51666c038ee7ccb29f32ba647c63857e
Showing with 41 additions and 0 deletions.
  1. +32 −0 src/gui/qgsimagedroptextedit.cpp
  2. +9 −0 src/gui/qgsimagedroptextedit.h
@@ -29,17 +29,24 @@
****************************************************************************/

#include "qgsimagedroptextedit.h"
#include "qgsguiutils.h"

#include <QMimeData>
#include <QBuffer>
#include <QFileInfo>
#include <QImageReader>
#include <QMouseEvent>
#include <QApplication>
#include <QDesktopServices>

///@cond PRIVATE
QgsImageDropTextEdit::QgsImageDropTextEdit( QWidget *parent )
: QTextEdit( parent )
{
}

QgsImageDropTextEdit::~QgsImageDropTextEdit() = default;

bool QgsImageDropTextEdit::canInsertFromMimeData( const QMimeData *source ) const
{
if ( source->hasImage() || QTextEdit::canInsertFromMimeData( source ) )
@@ -197,6 +204,31 @@ void QgsImageDropTextEdit::insertFromMimeData( const QMimeData *source )
QTextEdit::insertFromMimeData( source );
}

void QgsImageDropTextEdit::mouseMoveEvent( QMouseEvent *e )
{
QTextEdit::mouseMoveEvent( e );
mActiveAnchor = anchorAt( e->pos() );
if ( !mActiveAnchor.isEmpty() && !mCursorOverride )
mCursorOverride = std::make_unique< QgsTemporaryCursorOverride >( Qt::PointingHandCursor );
else if ( mActiveAnchor.isEmpty() && mCursorOverride )
mCursorOverride.reset();
}

void QgsImageDropTextEdit::mouseReleaseEvent( QMouseEvent *e )
{
if ( e->button() == Qt::LeftButton && !mActiveAnchor.isEmpty() )
{
QDesktopServices::openUrl( QUrl( mActiveAnchor ) );
if ( mCursorOverride )
mCursorOverride.reset();
mActiveAnchor.clear();
}
else
{
QTextEdit::mouseReleaseEvent( e );
}
}

void QgsImageDropTextEdit::dropImage( const QImage &image, const QString &format )
{
QByteArray bytes;
@@ -33,10 +33,12 @@
#include "qgis_gui.h"
#include "qgis_sip.h"
#include <QTextEdit>
#include <memory>

#define SIP_NO_FILE

class QImage;
class QgsTemporaryCursorOverride;

/*
* Originally ported from https://github.com/Anchakor/MRichTextEditor, courtesy of Hobrasoft.
@@ -49,13 +51,20 @@ class GUI_EXPORT QgsImageDropTextEdit : public QTextEdit

public:
QgsImageDropTextEdit( QWidget *parent = nullptr );
~QgsImageDropTextEdit() override;

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

protected:
bool canInsertFromMimeData( const QMimeData *source ) const override;
void insertFromMimeData( const QMimeData *source ) override;
void mouseMoveEvent( QMouseEvent *e ) override;
void mouseReleaseEvent( QMouseEvent *e ) override;

private:
QString mActiveAnchor;
std::unique_ptr< QgsTemporaryCursorOverride > mCursorOverride;

};
///@endcond

0 comments on commit c12a3fd

Please sign in to comment.