QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgis_gui.h"
21 #include "qgis_sip.h"
22 
23 class QMimeData;
25 class QgsPanelWidget;
26 
35 class GUI_EXPORT QgsColorButton : public QToolButton
36 {
37 
38 #ifdef SIP_RUN
40  if ( qobject_cast<QgsColorButton *>( sipCpp ) )
41  sipType = sipType_QgsColorButton;
42  else
43  sipType = NULL;
44  SIP_END
45 #endif
46 
47 
48  Q_OBJECT
49  Q_PROPERTY( QString colorDialogTitle READ colorDialogTitle WRITE setColorDialogTitle )
50  Q_PROPERTY( QColor color READ color WRITE setColor )
51  Q_PROPERTY( bool allowOpacity READ allowOpacity WRITE setAllowOpacity )
52  Q_PROPERTY( bool showMenu READ showMenu WRITE setShowMenu )
53  Q_PROPERTY( Behavior behavior READ behavior WRITE setBehavior )
54  Q_PROPERTY( QColor defaultColor READ defaultColor WRITE setDefaultColor )
55  Q_PROPERTY( bool showNoColor READ showNoColor WRITE setShowNoColor )
56  Q_PROPERTY( QString noColorString READ noColorString WRITE setNoColorString )
57  Q_PROPERTY( QString context READ context WRITE setContext )
58 
59  public:
60 
64  enum Behavior
65  {
66  ShowDialog = 0,
67  SignalOnly
68  };
69  Q_ENUM( Behavior )
70 
71 
78  QgsColorButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &cdt = QString(), QgsColorSchemeRegistry *registry = nullptr );
79 
80  QSize minimumSizeHint() const override;
81  QSize sizeHint() const override;
82 
88  QColor color() const;
89 
97  void setAllowOpacity( bool allowOpacity );
98 
106  bool allowOpacity() const { return mAllowOpacity; }
107 
113  void setColorDialogTitle( const QString &title );
114 
120  QString colorDialogTitle() const;
121 
128  void setShowMenu( bool showMenu );
129 
135  bool showMenu() const { return menu() ? true : false; }
136 
143  void setBehavior( Behavior behavior );
144 
150  Behavior behavior() const { return mBehavior; }
151 
159  void setDefaultColor( const QColor &color );
160 
168  QColor defaultColor() const { return mDefaultColor; }
169 
179  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
180 
190  bool showNoColor() const { return mShowNoColorOption; }
191 
200  void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
201 
210  void setShowNull( bool showNull, const QString &nullString = QString() );
211 
218  bool showNull() const;
219 
226  bool isNull() const;
227 
236  QString noColorString() const { return mNoColorString; }
237 
245  void setContext( const QString &context ) { mContext = context; }
246 
254  QString context() const { return mContext; }
255 
263  void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
264 
272  QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
273 
288  void linkToProjectColor( const QString &name );
289 
296  QString linkedProjectColorName() const { return mLinkedColorName; }
297 
306  static QPixmap createMenuIcon( const QColor &color, bool showChecks = true );
307 
308  public slots:
309 
316  void setColor( const QColor &color );
317 
326  void setButtonBackground( const QColor &color = QColor() );
327 
332  void copyColor();
333 
339  void pasteColor();
340 
344  void activatePicker();
345 
352  void setToNoColor();
353 
360  void setToDefaultColor();
361 
369  void setToNull();
370 
379  void unlink();
380 
381  signals:
382 
388  void colorChanged( const QColor &color );
389 
396  void colorClicked( const QColor &color );
397 
404  void cleared();
405 
415  void unlinked();
416 
417  protected:
418 
419  bool event( QEvent *e ) override;
420  void changeEvent( QEvent *e ) override;
421  void showEvent( QShowEvent *e ) override;
422  void resizeEvent( QResizeEvent *event ) override;
423 
427  static const QPixmap &transparentBackground();
428 
432  void mousePressEvent( QMouseEvent *e ) override;
433 
437  void mouseMoveEvent( QMouseEvent *e ) override;
438 
442  void mouseReleaseEvent( QMouseEvent *e ) override;
443 
447  void keyPressEvent( QKeyEvent *e ) override;
448 
452  void dragEnterEvent( QDragEnterEvent *e ) override;
453 
457  void dragLeaveEvent( QDragLeaveEvent *e ) override;
458 
462  void dropEvent( QDropEvent *e ) override;
463 
464  private:
465 
466  Behavior mBehavior = QgsColorButton::ShowDialog;
467  QString mColorDialogTitle;
468  QColor mColor;
469  QSize mMinimumSize;
470 
471  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
472 
473  QColor mDefaultColor;
474 
476  QColor mCurrentColor;
477 
478  QString mContext;
479  bool mAllowOpacity = false;
480  bool mColorSet = false;
481 
482  bool mShowNoColorOption = false;
483  QString mNoColorString;
484  bool mShowNull = false;
485  QString mNullColorString;
486 
487  QPoint mDragStartPosition;
488  bool mPickingColor = false;
489 
490  QMenu *mMenu = nullptr;
491 
492  QSize mIconSize;
493  QString mLinkedColorName;
494  bool mShowMenu = true;
495 
504  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
505 
512  void stopPicking( QPoint eventPos, bool samplingColor = true );
513 
514  QColor linkedProjectColor() const;
515 
516  private slots:
517 
518  void buttonClicked();
519 
520  void showColorDialog();
521 
525  void setValidColor( const QColor &newColor );
526 
531  void setValidTemporaryColor( const QColor &newColor );
532 
537  void addRecentColor( const QColor &color );
538 
542  void prepareMenu();
543 };
544 
545 #endif
QgsColorButton::showNoColor
bool showNoColor() const
Returns whether the "no color" option is shown in the button's drop-down menu.
Definition: qgscolorbutton.h:190
QgsColorButton::setNoColorString
void setNoColorString(const QString &noColorString)
Sets the string to use for the "no color" option in the button's drop-down menu.
Definition: qgscolorbutton.h:200
QgsColorButton::setShowNoColor
void setShowNoColor(const bool showNoColorOption)
Sets whether the "no color" option should be shown in the button's drop-down menu.
Definition: qgscolorbutton.h:179
QgsColorButton::setColorSchemeRegistry
void setColorSchemeRegistry(QgsColorSchemeRegistry *registry)
Sets the color scheme registry for the button, which controls the color swatch grids that are shown i...
Definition: qgscolorbutton.h:263
QgsColorButton::behavior
Behavior behavior() const
Returns the behavior for when the button is clicked.
Definition: qgscolorbutton.h:150
QgsColorButton::Behavior
Behavior
Specifies the behavior when the button is clicked.
Definition: qgscolorbutton.h:64
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:29
QgsColorButton::linkedProjectColorName
QString linkedProjectColorName() const
Returns the linked project color name, if set.
Definition: qgscolorbutton.h:296
QgsColorButton::context
QString context() const
Returns the context string for the color button.
Definition: qgscolorbutton.h:254
qgis_sip.h
QgsColorButton::noColorString
QString noColorString() const
Returns the string used for the "no color" option in the button's drop-down menu.
Definition: qgscolorbutton.h:236
QgsColorSchemeRegistry
Registry of color schemes.
Definition: qgscolorschemeregistry.h:34
QgsColorButton::allowOpacity
bool allowOpacity() const
Returns whether opacity modification (transparency) is permitted for the color.
Definition: qgscolorbutton.h:106
QgsColorButton::defaultColor
QColor defaultColor() const
Returns the default color for the button, which is shown in the button's drop-down menu for the "defa...
Definition: qgscolorbutton.h:168
QgsColorButton::showMenu
bool showMenu() const
Returns whether the drop-down menu is shown for the button.
Definition: qgscolorbutton.h:135
QgsColorButton
Definition: qgscolorbutton.h:35
QgsColorButton::setContext
void setContext(const QString &context)
Sets the context string for the color button.
Definition: qgscolorbutton.h:245
QgsColorButton::colorSchemeRegistry
QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the color scheme registry for the button, which controls the color swatch grids that are show...
Definition: qgscolorbutton.h:272
QgsColorButton::ShowDialog
@ ShowDialog
Show a color picker dialog when clicked.
Definition: qgscolorbutton.h:66
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53