QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 "qgssymbol.h"
21 #include <QToolButton>
22 #include <QPointer>
23 #include <memory>
24 
25 class QgsMapCanvas;
26 class QgsVectorLayer;
28 class QgsPanelWidget;
29 class QgsMessageBar;
30 class QMimeData;
31 
42 class GUI_EXPORT QgsSymbolButton : public QToolButton
43 {
44  Q_OBJECT
45 
46  Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
47 
48  public:
49 
54  QgsSymbolButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &dialogTitle = QString() );
55 
56  QSize minimumSizeHint() const override;
57  QSize sizeHint() const override;
58 
65  void setSymbolType( QgsSymbol::SymbolType type );
66 
71  QgsSymbol::SymbolType symbolType() const { return mType; }
72 
77  void setDialogTitle( const QString &title );
78 
83  QString dialogTitle() const;
84 
90  QgsSymbol *symbol();
91 
98  template <class SymbolType> SymbolType *clonedSymbol() SIP_SKIP
99  {
100  QgsSymbol *tmpSymbol = mSymbol.get();
101  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
102 
103  if ( symbolCastToType )
104  {
105  return symbolCastToType->clone();
106  }
107  else
108  {
109  //could not cast
110  return nullptr;
111  }
112  }
113 
118  QgsMapCanvas *mapCanvas() const;
119 
125  void setMapCanvas( QgsMapCanvas *canvas );
126 
133  void setMessageBar( QgsMessageBar *bar );
134 
140  QgsMessageBar *messageBar() const;
141 
146  QgsVectorLayer *layer() const;
147 
154  void setLayer( QgsVectorLayer *layer );
155 
160  void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
161 
162  public slots:
163 
170  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
171 
176  void setColor( const QColor &color );
177 
182  void copySymbol();
183 
189  void pasteSymbol();
190 
195  void copyColor();
196 
202  void pasteColor();
203 
204  signals:
205 
211  void changed();
212 
213  protected:
214 
215  void changeEvent( QEvent *e ) override;
216  void showEvent( QShowEvent *e ) override;
217  void resizeEvent( QResizeEvent *event ) override;
218 
219  // Reimplemented to detect right mouse button clicks on the color button and allow dragging colors
220  void mousePressEvent( QMouseEvent *e ) override;
221  // Reimplemented to allow dragging colors/symbols from button
222  void mouseMoveEvent( QMouseEvent *e ) override;
223  void mouseReleaseEvent( QMouseEvent *e ) override;
224  void keyPressEvent( QKeyEvent *e ) override;
225  // Reimplemented to accept dragged colors
226  void dragEnterEvent( QDragEnterEvent *e ) override;
227 
228  // Reimplemented to reset button appearance after drag leave
229  void dragLeaveEvent( QDragLeaveEvent *e ) override;
230 
231  // Reimplemented to accept dropped colors
232  void dropEvent( QDropEvent *e ) override;
233 
234  private slots:
235 
236  void showSettingsDialog();
237  void updateSymbolFromWidget();
238  void cleanUpSymbolSelector( QgsPanelWidget *container );
239 
243  void prepareMenu();
244 
245  void addRecentColor( const QColor &color );
246 
250  void activatePicker();
251 
252  private:
253 
254  QSize mSizeHint;
255 
256  QString mDialogTitle;
257 
259 
260  QgsMapCanvas *mMapCanvas = nullptr;
261  QgsMessageBar *mMessageBar = nullptr;
262 
263  QPoint mDragStartPosition;
264 
265  QMenu *mMenu = nullptr;
266 
267  QPointer< QgsVectorLayer > mLayer;
268 
269  QSize mIconSize;
270 
271  std::unique_ptr< QgsSymbol > mSymbol;
272 
273  QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
274 
275  bool mPickingColor = false;
276 
281  void updatePreview( const QColor &color = QColor(), QgsSymbol *tempSymbol = nullptr );
282 
292  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha );
293 
297  QPixmap createColorIcon( const QColor &color ) const;
298 
305  void stopPicking( QPoint eventPos, bool samplingColor = true );
306 
307  void showColorDialog();
308 
309 };
310 
311 #endif // QGSSYMBOLBUTTON_H
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.
QgsSymbol::SymbolType symbolType() const
Returns the symbol type which the button requires.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:65
SymbolType
Type of the symbol.
Definition: qgssymbol.h:87
@ Fill
Fill symbol.
Definition: qgssymbol.h:90
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