QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsfontbutton.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfontbutton.h
3  ---------------
4  Date : May 2017
5  Copyright : (C) 2017 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSFONTBUTTON_H
16 #define QGSFONTBUTTON_H
17 
18 #include "qgis_gui.h"
19 #include "qgis_sip.h"
20 #include "qgstextformat.h"
21 
22 #include <QToolButton>
23 
25 class QgsMapCanvas;
26 class QgsMessageBar;
28 
29 
46 class GUI_EXPORT QgsFontButton : public QToolButton
47 {
48  Q_OBJECT
49 
50  Q_PROPERTY( Mode mode READ mode WRITE setMode )
51  Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
52  Q_PROPERTY( QFont currentFont READ currentFont WRITE setCurrentFont NOTIFY changed )
53  Q_PROPERTY( QgsTextFormat textFormat READ textFormat WRITE setTextFormat NOTIFY changed )
54 
55  public:
56 
58  enum Mode
59  {
62  };
63 
64  Q_ENUM( Mode )
65 
66 
71  QgsFontButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &dialogTitle = QString() );
72 
73  QSize minimumSizeHint() const override;
74  QSize sizeHint() const override;
75 
80  QgsFontButton::Mode mode() const;
81 
89  void setMode( Mode mode );
90 
95  void setDialogTitle( const QString &title );
96 
101  QString dialogTitle() const;
102 
107  QgsMapCanvas *mapCanvas() const;
108 
114  void setMapCanvas( QgsMapCanvas *canvas );
115 
122  void setMessageBar( QgsMessageBar *bar );
123 
129  QgsMessageBar *messageBar() const;
130 
136  QgsTextFormat textFormat() const { return mFormat; }
137 
143  QFont currentFont() const;
144 
150  QgsVectorLayer *layer() const;
151 
159  void setLayer( QgsVectorLayer *layer );
160 
166  void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
167 
181  void setShowNullFormat( const bool show ) { mShowNoFormat = show; }
182 
191  void setNoFormatString( const QString &string ) { mNullFormatString = string; }
192 
205  bool showNullFormat() const { return mShowNoFormat; }
206 
207  public slots:
208 
214  void setTextFormat( const QgsTextFormat &format );
215 
223  void setToNullFormat();
224 
230  void setCurrentFont( const QFont &font );
231 
237  void setColor( const QColor &color );
238 
243  void copyFormat();
244 
250  void pasteFormat();
251 
257  void copyColor();
258 
265  void pasteColor();
266 
267  signals:
268 
272  void changed();
273 
274  protected:
275 
276  bool event( QEvent *e ) override;
277  void changeEvent( QEvent *e ) override;
278  void showEvent( QShowEvent *e ) override;
279  void resizeEvent( QResizeEvent *event ) override;
280 
281  // Reimplemented to detect right mouse button clicks on the color button and allow dragging colors
282  void mousePressEvent( QMouseEvent *e ) override;
283  // Reimplemented to allow dragging fonts from button
284  void mouseMoveEvent( QMouseEvent *e ) override;
285 
286  // Reimplemented to accept dragged colors
287  void dragEnterEvent( QDragEnterEvent *e ) override;
288 
289  // Reimplemented to reset button appearance after drag leave
290  void dragLeaveEvent( QDragLeaveEvent *e ) override;
291 
292  // Reimplemented to accept dropped colors
293  void dropEvent( QDropEvent *e ) override;
294 
295  void wheelEvent( QWheelEvent *event ) override;
296 
297  private slots:
298 
299  void showSettingsDialog();
300 
304  void prepareMenu();
305 
306  void addRecentColor( const QColor &color );
307 
308  private:
309 
310  QSize mSizeHint;
311 
312  Mode mMode = ModeTextRenderer;
313 
314  QString mDialogTitle;
315  QgsTextFormat mFormat;
316  QFont mFont;
317 
318  QgsMapCanvas *mMapCanvas = nullptr;
319  QgsMessageBar *mMessageBar = nullptr;
320 
321  QPoint mDragStartPosition;
322 
323  QMenu *mMenu = nullptr;
324 
325  QPointer< QgsVectorLayer > mLayer;
326 
327  QSize mIconSize;
328 
329  QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
330 
331  bool mShowNoFormat = false;
332  QString mNullFormatString;
333  QPointer< QAction > mNullFormatAction;
334  QPointer< QgsTextFormatPanelWidget > mActivePanel;
335 
343  bool formatFromMimeData( const QMimeData *mimeData, QgsTextFormat &resultFormat ) const;
344 
345 
353  bool fontFromMimeData( const QMimeData *mimeData, QFont &resultFont ) const;
354 
364  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha );
365 
369  QPixmap createColorIcon( const QColor &color ) const;
370 
374  QPixmap createDragIcon( QSize size = QSize( 50, 50 ), const QgsTextFormat *tempFormat = nullptr, const QFont *tempFont = nullptr ) const;
375 
380  void updatePreview( const QColor &color = QColor(), QgsTextFormat *tempFormat = nullptr, QFont *tempFont = nullptr );
381 };
382 
383 #endif // QGSFONTBUTTON_H
QgsFontButton::textFormat
QgsTextFormat textFormat() const
Returns the current text formatting set by the widget.
Definition: qgsfontbutton.h:136
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsFontButton::setShowNullFormat
void setShowNullFormat(const bool show)
Sets whether the "null format" option should be shown in the button's drop-down menu.
Definition: qgsfontbutton.h:181
QgsFontButton::Mode
Mode
Available button modes.
Definition: qgsfontbutton.h:59
qgis_sip.h
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
QgsFontButton::setNoFormatString
void setNoFormatString(const QString &string)
Sets the string to use for the "null format" option in the button's drop-down menu.
Definition: qgsfontbutton.h:191
QgsTextFormatPanelWidget
A panel widget for customizing text formatting settings.
Definition: qgstextformatwidget.h:383
QgsFontButton::ModeQFont
@ ModeQFont
Configure font settings for use with QFont objects.
Definition: qgsfontbutton.h:61
QgsFontButton
A button for customizing QgsTextFormat settings.
Definition: qgsfontbutton.h:47
QgsFontButton::ModeTextRenderer
@ ModeTextRenderer
Configure font settings for use with QgsTextRenderer.
Definition: qgsfontbutton.h:60
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
qgstextformat.h
QgsExpressionContextGenerator
Abstract interface for generating an expression context.
Definition: qgsexpressioncontextgenerator.h:37
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsFontButton::showNullFormat
bool showNullFormat() const
Returns whether the "null format" option will be shown in the button's drop-down menu.
Definition: qgsfontbutton.h:205
QgsFontButton::changed
void changed()
Emitted when the widget's text format settings are changed.