Skip to content

Commit

Permalink
[needs-docs] New gui widget QgsFontButton
Browse files Browse the repository at this point in the history
A standard widget for configuring text format properties for use
with QgsTextRenderer/QgsTextFormat.

It's modelled heavily off QgsColorButton, and supports lots of nice
things like dragging formats between buttons, copying and pasting
format settings, dropping colors from color buttons, dragging colors
from font buttons to color buttons, directly setting font size
and opacity/color without having to open a dialog.
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent a731311 commit 0b9fb5d
Show file tree
Hide file tree
Showing 11 changed files with 1,187 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/core/qgstextrenderer.sip
Expand Up @@ -1231,6 +1231,22 @@ class QgsTextFormat
:rtype: QDomElement
%End

QMimeData *toMimeData() const /Factory/;
%Docstring
Returns new mime data representing the text format settings.
Caller takes responsibility for deleting the returned object.
.. seealso:: fromMimeData()
:rtype: QMimeData
%End

static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok /Out/ = 0 );
%Docstring
Attempts to parse the provided mime ``data`` as a QgsTextFormat.
If data can be parsed as a text format, ``ok`` will be set to true.
.. seealso:: toMimeData()
:rtype: QgsTextFormat
%End

bool containsAdvancedEffects() const;
%Docstring
Returns true if any component of the font format requires advanced effects
Expand Down
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -110,6 +110,7 @@
%Include qgsfilterlineedit.sip
%Include qgsfloatingwidget.sip
%Include qgsfocuswatcher.sip
%Include qgsfontbutton.sip
%Include qgsformannotation.sip
%Include qgsgradientcolorrampdialog.sip
%Include qgsgradientstopeditor.sip
Expand Down
151 changes: 151 additions & 0 deletions python/gui/qgsfontbutton.sip
@@ -0,0 +1,151 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsfontbutton.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsFontButton : QToolButton
{
%Docstring
A button for customising QgsTextFormat settings.

The button will open a detailed text format settings dialog when clicked. An attached drop down
menu allows for copying and pasting text styles, picking colors for the text, and for dropping
colors from other color widgets.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsfontbutton.h"
%End
public:

QgsFontButton( QWidget *parent /TransferThis/ = 0, const QString &dialogTitle = QString() );
%Docstring
Construct a new font button.
Use ``parent`` to attach a parent QWidget to the dialog.
Use ``dialogTitle`` string to define the title to show in the text settings dialog.
%End

virtual QSize sizeHint() const;

void setDialogTitle( const QString &title );
%Docstring
Sets the ``title`` for the text settings dialog window.
.. seealso:: dialogTitle()
%End

QString dialogTitle() const;
%Docstring
Returns the title for the text settings dialog window.
.. seealso:: setDialogTitle()
:rtype: str
%End

QgsMapCanvas *mapCanvas() const;
%Docstring
Returns the map canvas associated with the widget.
.. seealso:: setMapCanvas()
:rtype: QgsMapCanvas
%End

void setMapCanvas( QgsMapCanvas *canvas );
%Docstring
Sets a map ``canvas`` to associate with the widget. This allows the
widget to fetch current settings from the map canvas, such as current scale.
.. seealso:: mapCanvas()
%End

QgsTextFormat textFormat() const;
%Docstring
Returns the current text formatting set by the widget.
.. seealso:: setTextFormat()
:rtype: QgsTextFormat
%End

public slots:

void setTextFormat( const QgsTextFormat &format );
%Docstring
Sets the current text ``format`` to show in the widget.
.. seealso:: textFormat()
%End

void setColor( const QColor &color );
%Docstring
Sets the current ``color`` for the text. Will emit a changed signal if the color is different
to the previous text color.
%End

void copyFormat();
%Docstring
Copies the current text format to the clipboard.
.. seealso:: pasteFormat()
%End

void pasteFormat();
%Docstring
Pastes a format from the clipboard. If clipboard does not contain a valid
format then no change is applied.
.. seealso:: copyFormat()
%End

void copyColor();
%Docstring
Copies the current text color to the clipboard.
.. seealso:: pasteColor()
%End

void pasteColor();
%Docstring
Pastes a color from the clipboard to the text format. If clipboard does not contain a valid
color or string representation of a color, then no change is applied.
.. seealso:: copyColor()
%End

signals:

void changed();
%Docstring
Emitted when the widget's text format settings are changed.
%End

protected:

virtual bool event( QEvent *e );

virtual void changeEvent( QEvent *e );

virtual void showEvent( QShowEvent *e );

virtual void resizeEvent( QResizeEvent *event );


virtual void mousePressEvent( QMouseEvent *e );

virtual void mouseMoveEvent( QMouseEvent *e );


virtual void dragEnterEvent( QDragEnterEvent *e );


virtual void dragLeaveEvent( QDragLeaveEvent *e );


virtual void dropEvent( QDropEvent *e );


};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsfontbutton.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
43 changes: 43 additions & 0 deletions src/core/qgstextrenderer.cpp
Expand Up @@ -1594,6 +1594,49 @@ QDomElement QgsTextFormat::writeXml( QDomDocument &doc, const QgsReadWriteContex
return textStyleElem;
}

QMimeData *QgsTextFormat::toMimeData() const
{
//set both the mime color data, and the text (format settings).
QMimeData *mimeData = new QMimeData;
mimeData->setColorData( QVariant( color() ) );

QgsReadWriteContext rwContext;
QDomDocument textDoc;
QDomElement textElem = writeXml( textDoc, rwContext );
textDoc.appendChild( textElem );
mimeData->setText( textDoc.toString() );

return mimeData;
}

QgsTextFormat QgsTextFormat::fromMimeData( const QMimeData *data, bool *ok )
{
if ( ok )
*ok = false;
QgsTextFormat format;
if ( !data )
return format;

QString text = data->text();
if ( !text.isEmpty() )
{
QDomDocument doc;
QDomElement elem;
QgsReadWriteContext rwContext;

if ( doc.setContent( text ) )
{
elem = doc.documentElement();

format.readXml( elem, rwContext );
if ( ok )
*ok = true;
return format;
}
}
return format;
}

bool QgsTextFormat::containsAdvancedEffects() const
{
if ( d->blendMode != QPainter::CompositionMode_SourceOver )
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgstextrenderer.h
Expand Up @@ -1068,6 +1068,20 @@ class CORE_EXPORT QgsTextFormat
*/
QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;

/**
* Returns new mime data representing the text format settings.
* Caller takes responsibility for deleting the returned object.
* \see fromMimeData()
*/
QMimeData *toMimeData() const SIP_FACTORY;

/**
* Attempts to parse the provided mime \a data as a QgsTextFormat.
* If data can be parsed as a text format, \a ok will be set to true.
* \see toMimeData()
*/
static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );

/** Returns true if any component of the font format requires advanced effects
* such as blend modes, which require output in raster formats to be fully respected.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -227,6 +227,7 @@ SET(QGIS_GUI_SRCS
qgsfilterlineedit.cpp
qgsfloatingwidget.cpp
qgsfocuswatcher.cpp
qgsfontbutton.cpp
qgsformannotation.cpp
qgsgeometryrubberband.cpp
qgsgradientcolorrampdialog.cpp
Expand Down Expand Up @@ -387,6 +388,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsfilterlineedit.h
qgsfloatingwidget.h
qgsfocuswatcher.h
qgsfontbutton.h
qgsformannotation.h
qgsgradientcolorrampdialog.h
qgsgradientstopeditor.h
Expand Down

0 comments on commit 0b9fb5d

Please sign in to comment.