QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 "qgstextrenderer.h"
21 
22 #include <QToolButton>
23 
25 class QgsMapCanvas;
26 class QgsMessageBar;
27 
44 class GUI_EXPORT QgsFontButton : public QToolButton
45 {
46  Q_OBJECT
47 
48  Q_PROPERTY( Mode mode READ mode WRITE setMode )
49  Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
50  Q_PROPERTY( QFont currentFont READ currentFont WRITE setCurrentFont NOTIFY changed )
51  Q_PROPERTY( QgsTextFormat textFormat READ textFormat WRITE setTextFormat NOTIFY changed )
52 
53  public:
54 
56  enum Mode
57  {
60  };
61 
62  Q_ENUM( Mode )
63 
64 
69  QgsFontButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &dialogTitle = QString() );
70 
71  QSize minimumSizeHint() const override;
72  QSize sizeHint() const override;
73 
78  QgsFontButton::Mode mode() const;
79 
87  void setMode( Mode mode );
88 
93  void setDialogTitle( const QString &title );
94 
99  QString dialogTitle() const;
100 
105  QgsMapCanvas *mapCanvas() const;
106 
112  void setMapCanvas( QgsMapCanvas *canvas );
113 
120  void setMessageBar( QgsMessageBar *bar );
121 
127  QgsMessageBar *messageBar() const;
128 
134  QgsTextFormat textFormat() const { return mFormat; }
135 
141  QFont currentFont() const;
142 
148  QgsVectorLayer *layer() const;
149 
157  void setLayer( QgsVectorLayer *layer );
158 
164  void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
165 
166  public slots:
167 
173  void setTextFormat( const QgsTextFormat &format );
174 
180  void setCurrentFont( const QFont &font );
181 
187  void setColor( const QColor &color );
188 
193  void copyFormat();
194 
200  void pasteFormat();
201 
207  void copyColor();
208 
215  void pasteColor();
216 
217  signals:
218 
222  void changed();
223 
224  protected:
225 
226  bool event( QEvent *e ) override;
227  void changeEvent( QEvent *e ) override;
228  void showEvent( QShowEvent *e ) override;
229  void resizeEvent( QResizeEvent *event ) override;
230 
231  // Reimplemented to detect right mouse button clicks on the color button and allow dragging colors
232  void mousePressEvent( QMouseEvent *e ) override;
233  // Reimplemented to allow dragging fonts from button
234  void mouseMoveEvent( QMouseEvent *e ) override;
235 
236  // Reimplemented to accept dragged colors
237  void dragEnterEvent( QDragEnterEvent *e ) override;
238 
239  // Reimplemented to reset button appearance after drag leave
240  void dragLeaveEvent( QDragLeaveEvent *e ) override;
241 
242  // Reimplemented to accept dropped colors
243  void dropEvent( QDropEvent *e ) override;
244 
245  void wheelEvent( QWheelEvent *event ) override;
246 
247  private slots:
248 
249  void showSettingsDialog();
250 
254  void prepareMenu();
255 
256  void addRecentColor( const QColor &color );
257 
258  private:
259 
260  QSize mSizeHint;
261 
262  Mode mMode = ModeTextRenderer;
263 
264  QString mDialogTitle;
265  QgsTextFormat mFormat;
266  QFont mFont;
267 
268  QgsMapCanvas *mMapCanvas = nullptr;
269  QgsMessageBar *mMessageBar = nullptr;
270 
271  QPoint mDragStartPosition;
272 
273  QMenu *mMenu = nullptr;
274 
275  QPointer< QgsVectorLayer > mLayer;
276 
277  QSize mIconSize;
278 
279  QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
280 
288  bool formatFromMimeData( const QMimeData *mimeData, QgsTextFormat &resultFormat ) const;
289 
290 
298  bool fontFromMimeData( const QMimeData *mimeData, QFont &resultFont ) const;
299 
309  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha );
310 
314  QPixmap createColorIcon( const QColor &color ) const;
315 
319  QPixmap createDragIcon( QSize size = QSize( 50, 50 ), const QgsTextFormat *tempFormat = nullptr, const QFont *tempFont = nullptr ) const;
320 
325  void updatePreview( const QColor &color = QColor(), QgsTextFormat *tempFormat = nullptr, QFont *tempFont = nullptr );
326 };
327 
328 #endif // QGSFONTBUTTON_H
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
Mode
Available button modes.
Definition: qgsfontbutton.h:56
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:75
Configure font settings for use with QgsTextRenderer.
Definition: qgsfontbutton.h:58
QgsTextFormat textFormat() const
Returns the current text formatting set by the widget.
Abstract interface for generating an expression context.
A button for customizing QgsTextFormat settings.
Definition: qgsfontbutton.h:44
Configure font settings for use with QFont objects.
Definition: qgsfontbutton.h:59
Container for all settings relating to text rendering.
Represents a vector layer which manages a vector based data sets.