QGIS API Documentation  3.6.0-Noosa (5873452)
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 #include "qgis_gui.h"
22 #include "qgis_sip.h"
23 
24 class QMimeData;
26 class QgsPanelWidget;
27 
36 class GUI_EXPORT QgsColorButton : public QToolButton
37 {
38 
39 #ifdef SIP_RUN
41  if ( qobject_cast<QgsColorButton *>( sipCpp ) )
42  sipType = sipType_QgsColorButton;
43  else
44  sipType = NULL;
45  SIP_END
46 #endif
47 
48 
49  Q_OBJECT
50  Q_PROPERTY( QString colorDialogTitle READ colorDialogTitle WRITE setColorDialogTitle )
51  Q_PROPERTY( QColor color READ color WRITE setColor )
52  Q_PROPERTY( bool allowOpacity READ allowOpacity WRITE setAllowOpacity )
53  Q_PROPERTY( bool showMenu READ showMenu WRITE setShowMenu )
54  Q_PROPERTY( Behavior behavior READ behavior WRITE setBehavior )
55  Q_PROPERTY( QColor defaultColor READ defaultColor WRITE setDefaultColor )
56  Q_PROPERTY( bool showNoColor READ showNoColor WRITE setShowNoColor )
57  Q_PROPERTY( QString noColorString READ noColorString WRITE setNoColorString )
58  Q_PROPERTY( QString context READ context WRITE setContext )
59 
60  public:
61 
65  enum Behavior
66  {
67  ShowDialog = 0,
68  SignalOnly
69  };
70  Q_ENUM( Behavior )
71 
72 
79  QgsColorButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &cdt = QString(), QgsColorSchemeRegistry *registry = nullptr );
80 
81  QSize minimumSizeHint() const override;
82  QSize sizeHint() const override;
83 
89  QColor color() const;
90 
98  void setAllowOpacity( bool allowOpacity );
99 
107  bool allowOpacity() const { return mAllowOpacity; }
108 
114  void setColorDialogTitle( const QString &title );
115 
121  QString colorDialogTitle() const;
122 
129  void setShowMenu( bool showMenu );
130 
136  bool showMenu() const { return menu() ? true : false; }
137 
144  void setBehavior( Behavior behavior );
145 
151  Behavior behavior() const { return mBehavior; }
152 
160  void setDefaultColor( const QColor &color );
161 
169  QColor defaultColor() const { return mDefaultColor; }
170 
180  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
181 
191  bool showNoColor() const { return mShowNoColorOption; }
192 
201  void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
202 
210  void setShowNull( bool showNull );
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 
368  void setToNull();
369 
378  void unlink();
379 
380  signals:
381 
387  void colorChanged( const QColor &color );
388 
395  void colorClicked( const QColor &color );
396 
406  void unlinked();
407 
408  protected:
409 
410  bool event( QEvent *e ) override;
411  void changeEvent( QEvent *e ) override;
412  void showEvent( QShowEvent *e ) override;
413  void resizeEvent( QResizeEvent *event ) override;
414 
418  static const QPixmap &transparentBackground();
419 
423  void mousePressEvent( QMouseEvent *e ) override;
424 
428  void mouseMoveEvent( QMouseEvent *e ) override;
429 
433  void mouseReleaseEvent( QMouseEvent *e ) override;
434 
438  void keyPressEvent( QKeyEvent *e ) override;
439 
443  void dragEnterEvent( QDragEnterEvent *e ) override;
444 
448  void dragLeaveEvent( QDragLeaveEvent *e ) override;
449 
453  void dropEvent( QDropEvent *e ) override;
454 
455  private:
456 
457 
458  QColor sampleColor( QPoint point ) const;
459 
460  static QScreen *findScreenAt( QPoint pos );
462  QString mColorDialogTitle;
463  QColor mColor;
464  QSize mMinimumSize;
465 
466  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
467 
468  QColor mDefaultColor;
469 
471  QColor mCurrentColor;
472 
473  QString mContext;
474  bool mAllowOpacity = false;
475  bool mColorSet = false;
476 
477  bool mShowNoColorOption = false;
478  QString mNoColorString;
479  bool mShowNull = false;
480 
481  QPoint mDragStartPosition;
482  bool mPickingColor = false;
483 
484  QMenu *mMenu = nullptr;
485 
486  QSize mIconSize;
487  QString mLinkedColorName;
488  bool mShowMenu = true;
489 
498  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
499 
506  void stopPicking( QPoint eventPos, bool samplingColor = true );
507 
508  QColor linkedProjectColor() const;
509 
510  private slots:
511 
512  void buttonClicked();
513 
514  void showColorDialog();
515 
519  void setValidColor( const QColor &newColor );
520 
525  void setValidTemporaryColor( const QColor &newColor );
526 
531  void addRecentColor( const QColor &color );
532 
536  void prepareMenu();
537 };
538 
539 #endif
QString noColorString() const
Returns the string used for the "no color" option in the button&#39;s drop-down menu. ...
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
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.
Show a color picker dialog when clicked.
A cross platform button subclass for selecting colors.
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.
Behavior
Specifies the behavior when the button is clicked.
void setShowNoColor(const bool showNoColorOption)
Sets whether the "no color" option should be shown in the button&#39;s drop-down menu.
#define SIP_END
Definition: qgis_sip.h:182
Behavior behavior() const
Returns the behavior for when the button is clicked.
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.
void setColorSchemeRegistry(QgsColorSchemeRegistry *registry)
Sets the color scheme registry for the button, which controls the color swatch grids that are shown i...
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
bool allowOpacity() const
Returns whether opacity modification (transparency) is permitted for the color.
QString linkedProjectColorName() const
Returns the linked project color name, if set.
QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the color scheme registry for the button, which controls the color swatch grids that are show...