QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgssymbolbutton.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbolbutton.h
3  -----------------
4  Date : July 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 QGSSYMBOLBUTTON_H
16 #define QGSSYMBOLBUTTON_H
17 
18 #include "qgis_gui.h"
19 #include "qgis_sip.h"
20 #include "qgis.h"
21 
22 #include <QToolButton>
23 #include <QPointer>
24 #include <memory>
25 
26 class QgsMapCanvas;
27 class QgsVectorLayer;
29 class QgsPanelWidget;
30 class QgsMessageBar;
31 class QMimeData;
32 class QgsSymbol;
33 
44 class GUI_EXPORT QgsSymbolButton : public QToolButton
45 {
46  Q_OBJECT
47 
48  Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
49 
50  public:
51 
56  QgsSymbolButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &dialogTitle = QString() );
58 
59  QSize minimumSizeHint() const override;
60  QSize sizeHint() const override;
61 
68  void setSymbolType( Qgis::SymbolType type );
69 
74  Qgis::SymbolType symbolType() const { return mType; }
75 
80  void setDialogTitle( const QString &title );
81 
86  QString dialogTitle() const;
87 
93  QgsSymbol *symbol();
94 
101  template <class SymbolType> SymbolType *clonedSymbol() SIP_SKIP
102  {
103  QgsSymbol *tmpSymbol = mSymbol.get();
104  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
105 
106  if ( symbolCastToType )
107  {
108  return symbolCastToType->clone();
109  }
110  else
111  {
112  //could not cast
113  return nullptr;
114  }
115  }
116 
121  QgsMapCanvas *mapCanvas() const;
122 
128  void setMapCanvas( QgsMapCanvas *canvas );
129 
136  void setMessageBar( QgsMessageBar *bar );
137 
143  QgsMessageBar *messageBar() const;
144 
149  QgsVectorLayer *layer() const;
150 
157  void setLayer( QgsVectorLayer *layer );
158 
163  void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
164 
165  public slots:
166 
173  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
174 
179  void setColor( const QColor &color );
180 
185  void copySymbol();
186 
192  void pasteSymbol();
193 
198  void copyColor();
199 
205  void pasteColor();
206 
207  signals:
208 
214  void changed();
215 
216  protected:
217 
218  void changeEvent( QEvent *e ) override;
219  void showEvent( QShowEvent *e ) override;
220  void resizeEvent( QResizeEvent *event ) override;
221 
222  // Reimplemented to detect right mouse button clicks on the color button and allow dragging colors
223  void mousePressEvent( QMouseEvent *e ) override;
224  // Reimplemented to allow dragging colors/symbols from button
225  void mouseMoveEvent( QMouseEvent *e ) override;
226  void mouseReleaseEvent( QMouseEvent *e ) override;
227  void keyPressEvent( QKeyEvent *e ) override;
228  // Reimplemented to accept dragged colors
229  void dragEnterEvent( QDragEnterEvent *e ) override;
230 
231  // Reimplemented to reset button appearance after drag leave
232  void dragLeaveEvent( QDragLeaveEvent *e ) override;
233 
234  // Reimplemented to accept dropped colors
235  void dropEvent( QDropEvent *e ) override;
236 
237  private slots:
238 
239  void showSettingsDialog();
240  void updateSymbolFromWidget();
241  void cleanUpSymbolSelector( QgsPanelWidget *container );
242 
246  void prepareMenu();
247 
248  void addRecentColor( const QColor &color );
249 
253  void activatePicker();
254 
255  private:
256 
257  QSize mSizeHint;
258 
259  QString mDialogTitle;
260 
262 
263  QgsMapCanvas *mMapCanvas = nullptr;
264  QgsMessageBar *mMessageBar = nullptr;
265 
266  QPoint mDragStartPosition;
267 
268  QMenu *mMenu = nullptr;
269 
270  QPointer< QgsVectorLayer > mLayer;
271 
272  QSize mIconSize;
273 
274  std::unique_ptr< QgsSymbol > mSymbol;
275 
276  QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
277 
278  bool mPickingColor = false;
279 
284  void updatePreview( const QColor &color = QColor(), QgsSymbol *tempSymbol = nullptr );
285 
295  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha );
296 
300  QPixmap createColorIcon( const QColor &color ) const;
301 
308  void stopPicking( QPoint eventPos, bool samplingColor = true );
309 
310  void showColorDialog();
311 
312 };
313 
314 #endif // QGSSYMBOLBUTTON_H
SymbolType
Symbol types.
Definition: qgis.h:168
@ Fill
Fill symbol.
Abstract interface for generating an expression context.
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
Base class for any widget that can be shown as a inline panel.
A button for creating and modifying QgsSymbol settings.
SymbolType * clonedSymbol()
Returns a clone of the current symbol (as the specified template type) defined by the button.
void changed()
Emitted when the symbol's settings are changed.
Qgis::SymbolType symbolType() const
Returns the symbol type which the button requires.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36