QGIS API Documentation  3.6.0-Noosa (5873452)
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  // Reimplemented to accept dragged colors
223  void dragEnterEvent( QDragEnterEvent *e ) override;
224 
225  // Reimplemented to reset button appearance after drag leave
226  void dragLeaveEvent( QDragLeaveEvent *e ) override;
227 
228  // Reimplemented to accept dropped colors
229  void dropEvent( QDropEvent *e ) override;
230 
231  private slots:
232 
233  void showSettingsDialog();
234  void updateSymbolFromWidget();
235  void cleanUpSymbolSelector( QgsPanelWidget *container );
236 
240  void prepareMenu();
241 
242  void addRecentColor( const QColor &color );
243 
244  private:
245 
246  QSize mSizeHint;
247 
248  QString mDialogTitle;
249 
251 
252  QgsMapCanvas *mMapCanvas = nullptr;
253  QgsMessageBar *mMessageBar = nullptr;
254 
255  QPoint mDragStartPosition;
256 
257  QMenu *mMenu = nullptr;
258 
259  QPointer< QgsVectorLayer > mLayer;
260 
261  QSize mIconSize;
262 
263  std::unique_ptr< QgsSymbol > mSymbol;
264 
265  QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
266 
271  void updatePreview( const QColor &color = QColor(), QgsSymbol *tempSymbol = nullptr );
272 
282  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor, bool &hasAlpha );
283 
287  QPixmap createColorIcon( const QColor &color ) const;
288 
289 };
290 
291 #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...
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
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:73
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
Returns a deep copy of this symbol.
Represents a vector layer which manages a vector based data sets.