Skip to content
Permalink
Browse files
Create QgsRichTextEditor widget, as a straightforward port from
https://github.com/Anchakor/MRichTextEditor, with some QGIS
formatting and practices applied
  • Loading branch information
nyalldawson committed Apr 21, 2021
1 parent ea4131a commit 7ad31ca1dfa1e98476e432a9a3ae7c05b667eab8
@@ -0,0 +1,89 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsrichtexteditor.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsRichTextEditor : QWidget, protected Ui::QgsRichTextEditorBase
{
%Docstring(signature="appended")
Originally ported from https://github.com/Anchakor/MRichTextEditor, courtesy of Hobrasoft.
%End

%TypeHeaderCode
#include "qgsrichtexteditor.h"
%End
public:
QgsRichTextEditor( QWidget *parent = 0 );

QString toPlainText() const;
QString toHtml() const;
QTextDocument *document();
QTextCursor textCursor() const;
void setTextCursor( const QTextCursor &cursor );

public slots:
void setText( const QString &text );
void clearSource();

protected slots:
void setPlainText( const QString &text );
void setHtml( const QString &text );
void textRemoveFormat();
void textRemoveAllFormat();
void textBold();
void textUnderline();
void textStrikeout();
void textItalic();
void textSize( const QString &p );
void textLink( bool checked );
void textStyle( int index );
void textFgColor();
void textBgColor();
void listBullet( bool checked );
void listOrdered( bool checked );
void slotCurrentCharFormatChanged( const QTextCharFormat &format );
void slotCursorPositionChanged();
void slotClipboardDataChanged();
void increaseIndentation();
void decreaseIndentation();
void insertImage();
void textSource();

protected:
void mergeFormatOnWordOrSelection( const QTextCharFormat &format );
void fontChanged( const QFont &f );
void fgColorChanged( const QColor &c );
void bgColorChanged( const QColor &c );
void list( bool checked, QTextListFormat::Style style );
void indent( int delta );
void focusInEvent( QFocusEvent *event );


enum ParagraphItems
{
ParagraphStandard,
ParagraphHeading1,
ParagraphHeading2,
ParagraphHeading3,
ParagraphHeading4,
ParagraphMonospace
};

};



/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsrichtexteditor.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -183,6 +183,7 @@
%Include auto_generated/qgsrelationeditorwidget.sip
%Include auto_generated/qgsabstractrelationeditorwidget.sip
%Include auto_generated/qgsrelationwidgetregistry.sip
%Include auto_generated/qgsrichtexteditor.sip
%Include auto_generated/qgsrubberband.sip
%Include auto_generated/qgsscalecombobox.sip
%Include auto_generated/qgsscalerangewidget.sip
@@ -477,6 +477,7 @@ set(QGIS_GUI_SRCS
qgshistogramwidget.cpp
qgshelp.cpp
qgsidentifymenu.cpp
qgsimagedroptextedit.cpp
qgsinstallgridshiftdialog.cpp
qgskeyvaluewidget.cpp
qgslistwidget.cpp
@@ -570,6 +571,7 @@ set(QGIS_GUI_SRCS
qgsrelationeditorwidget.cpp
qgsabstractrelationeditorwidget.cpp
qgsrelationwidgetregistry.cpp
qgsrichtexteditor.cpp
qgsrubberband.cpp
qgsscalecombobox.cpp
qgsscalerangewidget.cpp
@@ -722,6 +724,7 @@ set(QGIS_GUI_HDRS
qgshighlightablelineedit.h
qgshistogramwidget.h
qgsidentifymenu.h
qgsimagedroptextedit.h
qgsinstallgridshiftdialog.h
qgskeyvaluewidget.h
qgslegendfilterbutton.h
@@ -818,6 +821,7 @@ set(QGIS_GUI_HDRS
qgsrelationeditorwidget.h
qgsabstractrelationeditorwidget.h
qgsrelationwidgetregistry.h
qgsrichtexteditor.h
qgsrubberband.h
qgsscalecombobox.h
qgsscalerangewidget.h
@@ -0,0 +1,140 @@
/*
** Copyright (C) 2013 Jiří Procházka (Hobrasoft)
** Contact: http://www.hobrasoft.cz/
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file is under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation and appearing
** in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the
** GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
*/

#include "qgsimagedroptextedit.h"
#include <QMimeData>
#include <QBuffer>

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

bool QgsImageDropTextEdit::canInsertFromMimeData( const QMimeData *source ) const
{
return source->hasImage() || QTextEdit::canInsertFromMimeData( source );
}

void QgsImageDropTextEdit::insertFromMimeData( const QMimeData *source )
{
if ( source->hasImage() )
{
const QStringList formats = source->formats();
QString format;
for ( const QString &string : formats )
{
if ( string == QLatin1String( "image/bmp" ) )
{
format = QStringLiteral( "BMP" );
break;
}
if ( string == QLatin1String( "image/jpeg" ) )
{
format = QStringLiteral( "JPG" );
break;
}
if ( string == QLatin1String( "image/jpg" ) )
{
format = QStringLiteral( "JPG" );
break;
}
if ( string == QLatin1String( "image/gif" ) )
{
format = QStringLiteral( "GIF" );
break;
}
if ( string == QLatin1String( "image/png" ) )
{
format = QStringLiteral( "PNG" );
break;
}
if ( string == QLatin1String( "image/pbm" ) )
{
format = QStringLiteral( "PBM" );
break;
}
if ( string == QLatin1String( "image/pgm" ) )
{
format = QStringLiteral( "PGM" );
break;
}
if ( string == QLatin1String( "image/ppm" ) )
{
format = QStringLiteral( "PPM" );
break;
}
if ( string == QLatin1String( "image/tiff" ) )
{
format = QStringLiteral( "TIFF" );
break;
}
if ( string == QLatin1String( "image/xbm" ) )
{
format = QStringLiteral( "XBM" );
break;
}
if ( string == QLatin1String( "image/xpm" ) )
{
format = QStringLiteral( "XPM" );
break;
}
}
if ( !format.isEmpty() )
{
dropImage( qvariant_cast<QImage>( source->imageData() ), format );
return;
}
}
QTextEdit::insertFromMimeData( source );
}

void QgsImageDropTextEdit::dropImage( const QImage &image, const QString &format )
{
QByteArray bytes;
QBuffer buffer( &bytes );
buffer.open( QIODevice::WriteOnly );
image.save( &buffer, format.toLocal8Bit().data() );
buffer.close();
QByteArray base64 = bytes.toBase64();
QByteArray base64l;
for ( int i = 0; i < base64.size(); i++ )
{
base64l.append( base64[i] );
if ( i % 80 == 0 )
{
base64l.append( "\n" );
}
}

QTextCursor cursor = textCursor();
QTextImageFormat imageFormat;
imageFormat.setWidth( image.width() );
imageFormat.setHeight( image.height() );
imageFormat.setName( QStringLiteral( "data:image/%1;base64,%2" )
.arg( QStringLiteral( "%1.%2" ).arg( rand() ).arg( format ),
base64l.data() )
);
cursor.insertImage( imageFormat );
}
///@endcond
@@ -0,0 +1,57 @@
/*
** Copyright (C) 2013 Jiří Procházka (Hobrasoft)
** Contact: http://www.hobrasoft.cz/
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file is under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation and appearing
** in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the
** GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
*/

#ifndef QGSIMAGEDROPTEXTEDIT_H
#define QGSIMAGEDROPTEXTEDIT_H

#include "qgis_gui.h"
#include "qgis_sip.h"
#include <QTextEdit>

#define SIP_NO_FILE

class QImage;

/*
* Originally ported from https://github.com/Anchakor/MRichTextEditor, courtesy of Hobrasoft.
*/

///@cond PRIVATE
class GUI_EXPORT QgsImageDropTextEdit : public QTextEdit
{
Q_OBJECT

public:
QgsImageDropTextEdit( QWidget *parent = nullptr );

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

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

};
///@endcond


#endif // QGSIMAGEDROPTEXTEDIT_H

0 comments on commit 7ad31ca

Please sign in to comment.