QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #include <QPointer>
24 
26 class QgsMapCanvas;
27 class QgsMessageBar;
29 
30 
47 class GUI_EXPORT QgsFontButton : public QToolButton
48 {
49  Q_OBJECT
50 
51  Q_PROPERTY( Mode mode READ mode WRITE setMode )
52  Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
53  Q_PROPERTY( QFont currentFont READ currentFont WRITE setCurrentFont NOTIFY changed )
54  Q_PROPERTY( QgsTextFormat textFormat READ textFormat WRITE setTextFormat NOTIFY changed )
55 
56  public:
57 
59  enum Mode
60  {
63  };
64 
65  Q_ENUM( Mode )
66 
67 
72  QgsFontButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &dialogTitle = QString() );
73 
74  QSize minimumSizeHint() const override;
75  QSize sizeHint() const override;
76 
81  QgsFontButton::Mode mode() const;
82 
90  void setMode( Mode mode );
91 
96  void setDialogTitle( const QString &title );
97 
102  QString dialogTitle() const;
103 
108  QgsMapCanvas *mapCanvas() const;
109 
115  void setMapCanvas( QgsMapCanvas *canvas );
116 
123  void setMessageBar( QgsMessageBar *bar );
124 
130  QgsMessageBar *messageBar() const;
131 
137  QgsTextFormat textFormat() const { return mFormat; }
138 
144  QFont currentFont() const;
145 
151  QgsVectorLayer *layer() const;
152 
160  void setLayer( QgsVectorLayer *layer );
161 
167  void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
168 
182  void setShowNullFormat( const bool show ) { mShowNoFormat = show; }
183 
192  void setNoFormatString( const QString &string ) { mNullFormatString = string; }
193 
206  bool showNullFormat() const { return mShowNoFormat; }
207 
208  public slots:
209 
215  void setTextFormat( const QgsTextFormat &format );
216 
224  void setToNullFormat();
225 
231  void setCurrentFont( const QFont &font );
232 
238  void setColor( const QColor &color );
239 
244  void copyFormat();
245 
251  void pasteFormat();
252 
258  void copyColor();
259 
266  void pasteColor();
267 
268  signals:
269 
273  void changed();
274 
275  protected:
276 
277  bool event( QEvent *e ) override;
278  void changeEvent( QEvent *e ) override;
279  void showEvent( QShowEvent *e ) override;
280  void resizeEvent( QResizeEvent *event ) override;
281 
282  // Reimplemented to detect right mouse button clicks on the color button and allow dragging colors
283  void mousePressEvent( QMouseEvent *e ) override;
284  // Reimplemented to allow dragging fonts from button
285  void mouseMoveEvent( QMouseEvent *e ) override;
286 
287  // Reimplemented to accept dragged colors
288  void dragEnterEvent( QDragEnterEvent *e ) override;
289 
290  // Reimplemented to reset button appearance after drag leave
291  void dragLeaveEvent( QDragLeaveEvent *e ) override;
292 
293  // Reimplemented to accept dropped colors
294  void dropEvent( QDropEvent *e ) override;
295 
296  void wheelEvent( QWheelEvent *event ) override;
297 
298  private slots:
299 
300  void showSettingsDialog();
301 
305  void prepareMenu();
306 
307  void addRecentColor( const QColor &color );
308 
309  private:
310 
311  QSize mSizeHint;
312 
313  Mode mMode = ModeTextRenderer;
314 
315  QString mDialogTitle;
316  QgsTextFormat mFormat;
317  QFont mFont;
318 
319  QgsMapCanvas *mMapCanvas = nullptr;
320  QgsMessageBar *mMessageBar = nullptr;
321 
322  QPoint mDragStartPosition;
323 
324  QMenu *mMenu = nullptr;
325 
326  QPointer< QgsVectorLayer > mLayer;
327 
328  QSize mIconSize;
329 
330  QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
331 
332  bool mShowNoFormat = false;
333  QString mNullFormatString;
334  QPointer< QAction > mNullFormatAction;
335  QPointer< QgsTextFormatPanelWidget > mActivePanel;
336 
344  bool formatFromMimeData( const QMimeData *mimeData, QgsTextFormat &resultFormat ) const;
345 
346 
354  bool fontFromMimeData( const QMimeData *mimeData, QFont &resultFont ) const;
355 
365  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha );
366 
370  QPixmap createColorIcon( const QColor &color ) const;
371 
375  QPixmap createDragIcon( QSize size = QSize( 50, 50 ), const QgsTextFormat *tempFormat = nullptr, const QFont *tempFont = nullptr ) const;
376 
381  void updatePreview( const QColor &color = QColor(), QgsTextFormat *tempFormat = nullptr, QFont *tempFont = nullptr );
382 };
383 
384 #endif // QGSFONTBUTTON_H
Abstract interface for generating an expression context.
A button for customizing QgsTextFormat settings.
Definition: qgsfontbutton.h:48
void setNoFormatString(const QString &string)
Sets the string to use for the "null format" option in the button's drop-down menu.
Mode
Available button modes.
Definition: qgsfontbutton.h:60
@ ModeQFont
Configure font settings for use with QFont objects.
Definition: qgsfontbutton.h:62
@ ModeTextRenderer
Configure font settings for use with QgsTextRenderer.
Definition: qgsfontbutton.h:61
void setShowNullFormat(const bool show)
Sets whether the "null format" option should be shown in the button's drop-down menu.
bool showNullFormat() const
Returns whether the "null format" option will be shown in the button's drop-down menu.
void changed()
Emitted when the widget's text format settings are changed.
QgsTextFormat textFormat() const
Returns the current text formatting set by the widget.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:86
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
A panel widget for customizing text formatting settings.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53