QGIS API Documentation  2.99.0-Master (37c43df)
qgscolorbutton.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorbutton.h - Color button
3  --------------------------------------
4  Date : 12-Dec-2006
5  Copyright : (C) 2006 by Tom Elwertowski
6  Email : telwertowski at users dot sourceforge dot net
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 QGSCOLORBUTTON_H
16 #define QGSCOLORBUTTON_H
17 
18 #include <QColorDialog>
19 #include <QToolButton>
20 #include <QTemporaryFile>
21 
22 class QMimeData;
24 class QgsPanelWidget;
25 
34 class GUI_EXPORT QgsColorButton : public QToolButton
35 {
36  Q_OBJECT
37  Q_ENUMS( Behaviour )
38  Q_PROPERTY( QString colorDialogTitle READ colorDialogTitle WRITE setColorDialogTitle )
39  Q_PROPERTY( bool acceptLiveUpdates READ acceptLiveUpdates WRITE setAcceptLiveUpdates )
40  Q_PROPERTY( QColor color READ color WRITE setColor )
41  Q_PROPERTY( bool allowAlpha READ allowAlpha WRITE setAllowAlpha )
42  Q_PROPERTY( bool showMenu READ showMenu WRITE setShowMenu )
43  Q_PROPERTY( Behaviour behaviour READ behaviour WRITE setBehaviour )
44  Q_PROPERTY( QColor defaultColor READ defaultColor WRITE setDefaultColor )
45  Q_PROPERTY( bool showNoColor READ showNoColor WRITE setShowNoColor )
46  Q_PROPERTY( QString noColorString READ noColorString WRITE setNoColorString )
47  Q_PROPERTY( QString context READ context WRITE setContext )
48 
49  public:
50 
53  enum Behaviour
54  {
55  ShowDialog = 0,
56  SignalOnly
57  };
58 
65  QgsColorButton( QWidget *parent = nullptr, const QString& cdt = "", QgsColorSchemeRegistry* registry = nullptr );
66 
67  virtual ~QgsColorButton();
68 
69  virtual QSize sizeHint() const override;
70 
75  QColor color() const;
76 
82  void setAllowAlpha( const bool allowAlpha );
83 
89  bool allowAlpha() const { return mAllowAlpha; }
90 
95  void setColorDialogTitle( const QString& title );
96 
101  QString colorDialogTitle() const;
102 
107  bool acceptLiveUpdates() const { return mAcceptLiveUpdates; }
108 
114  void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
115 
121  void setShowMenu( const bool showMenu );
122 
127  bool showMenu() const { return menu() ? true : false; }
128 
134  void setBehaviour( const Behaviour behaviour );
135 
140  Behaviour behaviour() const { return mBehaviour; }
141 
148  void setDefaultColor( const QColor& color );
149 
156  QColor defaultColor() const { return mDefaultColor; }
157 
166  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
167 
176  bool showNoColor() const { return mShowNoColorOption; }
177 
185  void setNoColorString( const QString& noColorString ) { mNoColorString = noColorString; }
186 
193  void setShowNull( bool showNull );
194 
200  bool showNull() const;
201 
207  bool isNull() const;
208 
216  QString noColorString() const { return mNoColorString; }
217 
224  void setContext( const QString& context ) { mContext = context; }
225 
232  QString context() const { return mContext; }
233 
240  void setColorSchemeRegistry( QgsColorSchemeRegistry* registry ) { mColorSchemeRegistry = registry; }
241 
248  QgsColorSchemeRegistry* colorSchemeRegistry() { return mColorSchemeRegistry; }
249 
250  public slots:
251 
257  void setColor( const QColor &color );
258 
266  void setButtonBackground( const QColor &color = QColor() );
267 
271  void copyColor();
272 
277  void pasteColor();
278 
281  void activatePicker();
282 
288  void setToNoColor();
289 
295  void setToDefaultColor();
296 
302  void setToNull();
303 
304  signals:
305 
310  void colorChanged( const QColor &color );
311 
317  void colorClicked( const QColor &color );
318 
319  protected:
320 
321  bool event( QEvent *e ) override;
322  void changeEvent( QEvent* e ) override;
323  void showEvent( QShowEvent* e ) override;
324  void resizeEvent( QResizeEvent *event ) override;
325 
328  static const QPixmap& transparentBackground();
329 
333  void mousePressEvent( QMouseEvent* e ) override;
334 
338  void mouseMoveEvent( QMouseEvent *e ) override;
339 
343  void mouseReleaseEvent( QMouseEvent *e ) override;
344 
348  void keyPressEvent( QKeyEvent *e ) override;
349 
353  void dragEnterEvent( QDragEnterEvent * e ) override;
354 
358  void dragLeaveEvent( QDragLeaveEvent *e ) override;
359 
363  void dropEvent( QDropEvent *e ) override;
364 
365  private:
366 
367  Behaviour mBehaviour;
368  QString mColorDialogTitle;
369  QColor mColor;
370 
371  QgsColorSchemeRegistry* mColorSchemeRegistry;
372 
373  QColor mDefaultColor;
374  QString mContext;
375  bool mAllowAlpha;
376  bool mAcceptLiveUpdates;
377  bool mColorSet;
378 
379  bool mShowNoColorOption;
380  QString mNoColorString;
381  bool mShowNull;
382 
383  QPoint mDragStartPosition;
384  bool mPickingColor;
385 
386  QMenu* mMenu;
387 
388  QSize mIconSize;
389 
397  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
398 
404  void stopPicking( QPointF eventPos, bool sampleColor = true );
405 
411  QPixmap createMenuIcon( const QColor &color, const bool showChecks = true );
412 
413  private slots:
414 
415  void buttonClicked();
416 
417  void showColorDialog();
418 
421  void setValidColor( const QColor& newColor );
422 
426  void setValidTemporaryColor( const QColor& newColor );
427 
431  void addRecentColor( const QColor& color );
432 
435  void prepareMenu();
436 };
437 
438 #endif
QString noColorString() const
Returns the string used for the "no color" option in the button&#39;s drop down menu. ...
Registry of color schemes.
bool showMenu() const
Returns whether the drop down menu is shown for the button.
void setNoColorString(const QString &noColorString)
Sets the string to use for the "no color" option in the button&#39;s drop down menu.
A cross platform button subclass for selecting colors.
bool allowAlpha() const
Returns whether alpha modification (transparency) is permitted for the color.
Base class for any widget that can be shown as a inline panel.
void setContext(const QString &context)
Sets the context string for the color button.
bool showNoColor() const
Returns whether the "no color" option is shown in the button&#39;s drop down menu.
void setShowNoColor(const bool showNoColorOption)
Sets whether the "no color" option should be shown in the button&#39;s drop down menu.
QColor defaultColor() const
Returns the default color for the button, which is shown in the button&#39;s drop down menu for the "defa...
QString context() const
Returns the context string for the color button.
Behaviour
Specifies the behaviour when the button is clicked.
void setColorSchemeRegistry(QgsColorSchemeRegistry *registry)
Sets the color scheme registry for the button, which controls the color swatch grids that are shown i...
void setAcceptLiveUpdates(const bool accept)
Sets whether the button accepts live updates from QColorDialog.
bool acceptLiveUpdates() const
Returns whether the button accepts live updates from QColorDialog.
QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the color scheme registry for the button, which controls the color swatch grids that are show...
bool isNull(const QVariant &v)
Behaviour behaviour() const
Returns the behaviour for when the button is clicked.