Skip to content

Commit 0b9fb5d

Browse files
committedJul 6, 2017
[needs-docs] New gui widget QgsFontButton
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.
1 parent a731311 commit 0b9fb5d

File tree

11 files changed

+1187
-0
lines changed

11 files changed

+1187
-0
lines changed
 

‎python/core/qgstextrenderer.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,22 @@ class QgsTextFormat
12311231
:rtype: QDomElement
12321232
%End
12331233

1234+
QMimeData *toMimeData() const /Factory/;
1235+
%Docstring
1236+
Returns new mime data representing the text format settings.
1237+
Caller takes responsibility for deleting the returned object.
1238+
.. seealso:: fromMimeData()
1239+
:rtype: QMimeData
1240+
%End
1241+
1242+
static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok /Out/ = 0 );
1243+
%Docstring
1244+
Attempts to parse the provided mime ``data`` as a QgsTextFormat.
1245+
If data can be parsed as a text format, ``ok`` will be set to true.
1246+
.. seealso:: toMimeData()
1247+
:rtype: QgsTextFormat
1248+
%End
1249+
12341250
bool containsAdvancedEffects() const;
12351251
%Docstring
12361252
Returns true if any component of the font format requires advanced effects

‎python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
%Include qgsfilterlineedit.sip
111111
%Include qgsfloatingwidget.sip
112112
%Include qgsfocuswatcher.sip
113+
%Include qgsfontbutton.sip
113114
%Include qgsformannotation.sip
114115
%Include qgsgradientcolorrampdialog.sip
115116
%Include qgsgradientstopeditor.sip

‎python/gui/qgsfontbutton.sip

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/qgsfontbutton.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsFontButton : QToolButton
13+
{
14+
%Docstring
15+
A button for customising QgsTextFormat settings.
16+
17+
The button will open a detailed text format settings dialog when clicked. An attached drop down
18+
menu allows for copying and pasting text styles, picking colors for the text, and for dropping
19+
colors from other color widgets.
20+
.. versionadded:: 3.0
21+
%End
22+
23+
%TypeHeaderCode
24+
#include "qgsfontbutton.h"
25+
%End
26+
public:
27+
28+
QgsFontButton( QWidget *parent /TransferThis/ = 0, const QString &dialogTitle = QString() );
29+
%Docstring
30+
Construct a new font button.
31+
Use ``parent`` to attach a parent QWidget to the dialog.
32+
Use ``dialogTitle`` string to define the title to show in the text settings dialog.
33+
%End
34+
35+
virtual QSize sizeHint() const;
36+
37+
void setDialogTitle( const QString &title );
38+
%Docstring
39+
Sets the ``title`` for the text settings dialog window.
40+
.. seealso:: dialogTitle()
41+
%End
42+
43+
QString dialogTitle() const;
44+
%Docstring
45+
Returns the title for the text settings dialog window.
46+
.. seealso:: setDialogTitle()
47+
:rtype: str
48+
%End
49+
50+
QgsMapCanvas *mapCanvas() const;
51+
%Docstring
52+
Returns the map canvas associated with the widget.
53+
.. seealso:: setMapCanvas()
54+
:rtype: QgsMapCanvas
55+
%End
56+
57+
void setMapCanvas( QgsMapCanvas *canvas );
58+
%Docstring
59+
Sets a map ``canvas`` to associate with the widget. This allows the
60+
widget to fetch current settings from the map canvas, such as current scale.
61+
.. seealso:: mapCanvas()
62+
%End
63+
64+
QgsTextFormat textFormat() const;
65+
%Docstring
66+
Returns the current text formatting set by the widget.
67+
.. seealso:: setTextFormat()
68+
:rtype: QgsTextFormat
69+
%End
70+
71+
public slots:
72+
73+
void setTextFormat( const QgsTextFormat &format );
74+
%Docstring
75+
Sets the current text ``format`` to show in the widget.
76+
.. seealso:: textFormat()
77+
%End
78+
79+
void setColor( const QColor &color );
80+
%Docstring
81+
Sets the current ``color`` for the text. Will emit a changed signal if the color is different
82+
to the previous text color.
83+
%End
84+
85+
void copyFormat();
86+
%Docstring
87+
Copies the current text format to the clipboard.
88+
.. seealso:: pasteFormat()
89+
%End
90+
91+
void pasteFormat();
92+
%Docstring
93+
Pastes a format from the clipboard. If clipboard does not contain a valid
94+
format then no change is applied.
95+
.. seealso:: copyFormat()
96+
%End
97+
98+
void copyColor();
99+
%Docstring
100+
Copies the current text color to the clipboard.
101+
.. seealso:: pasteColor()
102+
%End
103+
104+
void pasteColor();
105+
%Docstring
106+
Pastes a color from the clipboard to the text format. If clipboard does not contain a valid
107+
color or string representation of a color, then no change is applied.
108+
.. seealso:: copyColor()
109+
%End
110+
111+
signals:
112+
113+
void changed();
114+
%Docstring
115+
Emitted when the widget's text format settings are changed.
116+
%End
117+
118+
protected:
119+
120+
virtual bool event( QEvent *e );
121+
122+
virtual void changeEvent( QEvent *e );
123+
124+
virtual void showEvent( QShowEvent *e );
125+
126+
virtual void resizeEvent( QResizeEvent *event );
127+
128+
129+
virtual void mousePressEvent( QMouseEvent *e );
130+
131+
virtual void mouseMoveEvent( QMouseEvent *e );
132+
133+
134+
virtual void dragEnterEvent( QDragEnterEvent *e );
135+
136+
137+
virtual void dragLeaveEvent( QDragLeaveEvent *e );
138+
139+
140+
virtual void dropEvent( QDropEvent *e );
141+
142+
143+
};
144+
145+
/************************************************************************
146+
* This file has been generated automatically from *
147+
* *
148+
* src/gui/qgsfontbutton.h *
149+
* *
150+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
151+
************************************************************************/

‎src/core/qgstextrenderer.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,49 @@ QDomElement QgsTextFormat::writeXml( QDomDocument &doc, const QgsReadWriteContex
15941594
return textStyleElem;
15951595
}
15961596

1597+
QMimeData *QgsTextFormat::toMimeData() const
1598+
{
1599+
//set both the mime color data, and the text (format settings).
1600+
QMimeData *mimeData = new QMimeData;
1601+
mimeData->setColorData( QVariant( color() ) );
1602+
1603+
QgsReadWriteContext rwContext;
1604+
QDomDocument textDoc;
1605+
QDomElement textElem = writeXml( textDoc, rwContext );
1606+
textDoc.appendChild( textElem );
1607+
mimeData->setText( textDoc.toString() );
1608+
1609+
return mimeData;
1610+
}
1611+
1612+
QgsTextFormat QgsTextFormat::fromMimeData( const QMimeData *data, bool *ok )
1613+
{
1614+
if ( ok )
1615+
*ok = false;
1616+
QgsTextFormat format;
1617+
if ( !data )
1618+
return format;
1619+
1620+
QString text = data->text();
1621+
if ( !text.isEmpty() )
1622+
{
1623+
QDomDocument doc;
1624+
QDomElement elem;
1625+
QgsReadWriteContext rwContext;
1626+
1627+
if ( doc.setContent( text ) )
1628+
{
1629+
elem = doc.documentElement();
1630+
1631+
format.readXml( elem, rwContext );
1632+
if ( ok )
1633+
*ok = true;
1634+
return format;
1635+
}
1636+
}
1637+
return format;
1638+
}
1639+
15971640
bool QgsTextFormat::containsAdvancedEffects() const
15981641
{
15991642
if ( d->blendMode != QPainter::CompositionMode_SourceOver )

‎src/core/qgstextrenderer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,20 @@ class CORE_EXPORT QgsTextFormat
10681068
*/
10691069
QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
10701070

1071+
/**
1072+
* Returns new mime data representing the text format settings.
1073+
* Caller takes responsibility for deleting the returned object.
1074+
* \see fromMimeData()
1075+
*/
1076+
QMimeData *toMimeData() const SIP_FACTORY;
1077+
1078+
/**
1079+
* Attempts to parse the provided mime \a data as a QgsTextFormat.
1080+
* If data can be parsed as a text format, \a ok will be set to true.
1081+
* \see toMimeData()
1082+
*/
1083+
static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );
1084+
10711085
/** Returns true if any component of the font format requires advanced effects
10721086
* such as blend modes, which require output in raster formats to be fully respected.
10731087
*/

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ SET(QGIS_GUI_SRCS
227227
qgsfilterlineedit.cpp
228228
qgsfloatingwidget.cpp
229229
qgsfocuswatcher.cpp
230+
qgsfontbutton.cpp
230231
qgsformannotation.cpp
231232
qgsgeometryrubberband.cpp
232233
qgsgradientcolorrampdialog.cpp
@@ -387,6 +388,7 @@ SET(QGIS_GUI_MOC_HDRS
387388
qgsfilterlineedit.h
388389
qgsfloatingwidget.h
389390
qgsfocuswatcher.h
391+
qgsfontbutton.h
390392
qgsformannotation.h
391393
qgsgradientcolorrampdialog.h
392394
qgsgradientstopeditor.h

0 commit comments

Comments
 (0)