QGIS API Documentation  3.13.0-Master (740be229cb)
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 
41 class GUI_EXPORT QgsSymbolButton : public QToolButton
42 {
43  Q_OBJECT
44 
45  Q_PROPERTY( QString dialogTitle READ dialogTitle WRITE setDialogTitle )
46 
47  public:
48 
53  QgsSymbolButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &dialogTitle = QString() );
54 
55  QSize minimumSizeHint() const override;
56  QSize sizeHint() const override;
57 
64  void setSymbolType( QgsSymbol::SymbolType type );
65 
70  QgsSymbol::SymbolType symbolType() const { return mType; }
71 
76  void setDialogTitle( const QString &title );
77 
82  QString dialogTitle() const;
83 
89  QgsSymbol *symbol();
90 
97  template <class SymbolType> SymbolType *clonedSymbol() SIP_SKIP
98  {
99  QgsSymbol *tmpSymbol = mSymbol.get();
100  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
101 
102  if ( symbolCastToType )
103  {
104  return symbolCastToType->clone();
105  }
106  else
107  {
108  //could not cast
109  return nullptr;
110  }
111  }
112 
117  QgsMapCanvas *mapCanvas() const;
118 
124  void setMapCanvas( QgsMapCanvas *canvas );
125 
132  void setMessageBar( QgsMessageBar *bar );
133 
139  QgsMessageBar *messageBar() const;
140 
145  QgsVectorLayer *layer() const;
146 
153  void setLayer( QgsVectorLayer *layer );
154 
159  void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
160 
161  public slots:
162 
169  void setSymbol( QgsSymbol *symbol SIP_TRANSFER );
170 
175  void setColor( const QColor &color );
176 
181  void copySymbol();
182 
188  void pasteSymbol();
189 
194  void copyColor();
195 
201  void pasteColor();
202 
203  signals:
204 
210  void changed();
211 
212  protected:
213 
214  void changeEvent( QEvent *e ) override;
215  void showEvent( QShowEvent *e ) override;
216  void resizeEvent( QResizeEvent *event ) override;
217 
218  // Reimplemented to detect right mouse button clicks on the color button and allow dragging colors
219  void mousePressEvent( QMouseEvent *e ) override;
220  // Reimplemented to allow dragging colors/symbols from button
221  void mouseMoveEvent( QMouseEvent *e ) override;
222  void mouseReleaseEvent( QMouseEvent *e ) override;
223  void keyPressEvent( QKeyEvent *e ) override;
224  // Reimplemented to accept dragged colors
225  void dragEnterEvent( QDragEnterEvent *e ) override;
226 
227  // Reimplemented to reset button appearance after drag leave
228  void dragLeaveEvent( QDragLeaveEvent *e ) override;
229 
230  // Reimplemented to accept dropped colors
231  void dropEvent( QDropEvent *e ) override;
232 
233  private slots:
234 
235  void showSettingsDialog();
236  void updateSymbolFromWidget();
237  void cleanUpSymbolSelector( QgsPanelWidget *container );
238 
242  void prepareMenu();
243 
244  void addRecentColor( const QColor &color );
245 
249  void activatePicker();
250 
251  private:
252 
253  QSize mSizeHint;
254 
255  QString mDialogTitle;
256 
258 
259  QgsMapCanvas *mMapCanvas = nullptr;
260  QgsMessageBar *mMessageBar = nullptr;
261 
262  QPoint mDragStartPosition;
263 
264  QMenu *mMenu = nullptr;
265 
266  QPointer< QgsVectorLayer > mLayer;
267 
268  QSize mIconSize;
269 
270  std::unique_ptr< QgsSymbol > mSymbol;
271 
272  QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
273 
274  bool mPickingColor = false;
275 
280  void updatePreview( const QColor &color = QColor(), QgsSymbol *tempSymbol = nullptr );
281 
291  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha );
292 
296  QPixmap createColorIcon( const QColor &color ) const;
297 
304  void stopPicking( QPoint eventPos, bool samplingColor = true );
305 
306  void showColorDialog();
307 
308 };
309 
310 #endif // QGSSYMBOLBUTTON_H
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
SymbolType * clonedSymbol()
Returns a clone of the current symbol (as the specified template type) defined by the button...
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:62
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
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:78
SymbolType
Type of the symbol.
Definition: qgssymbol.h:84
#define SIP_SKIP
Definition: qgis_sip.h:126
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:88
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
Represents a vector layer which manages a vector based data sets.