QGIS API Documentation  3.2.0-Bonn (bc43194)
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.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 
40 class GUI_EXPORT QgsSymbolButton : public QToolButton
41 {
42  Q_OBJECT
43 
44  Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
45 
46  public:
47 
52  QgsSymbolButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &dialogTitle = QString() );
53 
54  QSize minimumSizeHint() const override;
55  QSize sizeHint() const override;
56 
63  void setSymbolType( QgsSymbol::SymbolType type );
64 
69  QgsSymbol::SymbolType symbolType() const { return mType; }
70 
75  void setDialogTitle( const QString &title );
76 
81  QString dialogTitle() const;
82 
88  QgsSymbol *symbol();
89 
96  template <class SymbolType> SymbolType *clonedSymbol() SIP_SKIP
97  {
98  QgsSymbol *tmpSymbol = mSymbol.get();
99  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
100 
101  if ( symbolCastToType )
102  {
103  return symbolCastToType->clone();
104  }
105  else
106  {
107  //could not cast
108  return nullptr;
109  }
110  }
111 
116  QgsMapCanvas *mapCanvas() const;
117 
123  void setMapCanvas( QgsMapCanvas *canvas );
124 
129  QgsVectorLayer *layer() const;
130 
137  void setLayer( QgsVectorLayer *layer );
138 
143  void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
144 
145  public slots:
146 
153  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
154 
159  void setColor( const QColor &color );
160 
165  void copySymbol();
166 
172  void pasteSymbol();
173 
178  void copyColor();
179 
185  void pasteColor();
186 
187  signals:
188 
194  void changed();
195 
196  protected:
197 
198  void changeEvent( QEvent *e ) override;
199  void showEvent( QShowEvent *e ) override;
200  void resizeEvent( QResizeEvent *event ) override;
201 
202  // Reimplemented to detect right mouse button clicks on the color button and allow dragging colors
203  void mousePressEvent( QMouseEvent *e ) override;
204  // Reimplemented to allow dragging colors/symbols from button
205  void mouseMoveEvent( QMouseEvent *e ) override;
206  // Reimplemented to accept dragged colors
207  void dragEnterEvent( QDragEnterEvent *e ) override;
208 
209  // Reimplemented to reset button appearance after drag leave
210  void dragLeaveEvent( QDragLeaveEvent *e ) override;
211 
212  // Reimplemented to accept dropped colors
213  void dropEvent( QDropEvent *e ) override;
214 
215  private slots:
216 
217  void showSettingsDialog();
218  void updateSymbolFromWidget();
219  void cleanUpSymbolSelector( QgsPanelWidget *container );
220 
224  void prepareMenu();
225 
226  void addRecentColor( const QColor &color );
227 
228  private:
229 
230  QSize mSizeHint;
231 
232  QString mDialogTitle;
233 
235 
236  QgsMapCanvas *mMapCanvas = nullptr;
237 
238  QPoint mDragStartPosition;
239 
240  QMenu *mMenu = nullptr;
241 
242  QPointer< QgsVectorLayer > mLayer;
243 
244  QSize mIconSize;
245 
246  std::unique_ptr< QgsSymbol > mSymbol;
247 
248  QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
249 
254  void updatePreview( const QColor &color = QColor(), QgsSymbol *tempSymbol = nullptr );
255 
265  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha );
266 
270  QPixmap createColorIcon( const QColor &color ) const;
271 
272 };
273 
274 #endif // QGSSYMBOLBUTTON_H
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
SymbolType * clonedSymbol()
Returns a clone of the current symbol (as the specified template type) defined by the button...
A button for creating and modifying QgsSymbol settings.
Base class for any widget that can be shown as a inline panel.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
SymbolType
Type of the symbol.
Definition: qgssymbol.h:83
#define SIP_SKIP
Definition: qgis_sip.h:119
QgsSymbol::SymbolType symbolType() const
Returns the symbol type which the button requires.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Abstract interface for generating an expression context.
Fill symbol.
Definition: qgssymbol.h:87
virtual QgsSymbol * clone() const =0
Gets a deep copy of this symbol.
Represents a vector layer which manages a vector based data sets.