QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
209  void setShowNull( bool showNull );
210 
217  bool showNull() const;
218 
225  bool isNull() const;
226 
235  QString noColorString() const { return mNoColorString; }
236 
244  void setContext( const QString &context ) { mContext = context; }
245 
253  QString context() const { return mContext; }
254 
262  void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
263 
271  QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
272 
287  void linkToProjectColor( const QString &name );
288 
295  QString linkedProjectColorName() const { return mLinkedColorName; }
296 
305  static QPixmap createMenuIcon( const QColor &color, bool showChecks = true );
306 
307  public slots:
308 
315  void setColor( const QColor &color );
316 
325  void setButtonBackground( const QColor &color = QColor() );
326 
331  void copyColor();
332 
338  void pasteColor();
339 
343  void activatePicker();
344 
351  void setToNoColor();
352 
359  void setToDefaultColor();
360 
367  void setToNull();
368 
377  void unlink();
378 
379  signals:
380 
386  void colorChanged( const QColor &color );
387 
394  void colorClicked( const QColor &color );
395 
405  void unlinked();
406 
407  protected:
408 
409  bool event( QEvent *e ) override;
410  void changeEvent( QEvent *e ) override;
411  void showEvent( QShowEvent *e ) override;
412  void resizeEvent( QResizeEvent *event ) override;
413 
417  static const QPixmap &transparentBackground();
418 
422  void mousePressEvent( QMouseEvent *e ) override;
423 
427  void mouseMoveEvent( QMouseEvent *e ) override;
428 
432  void mouseReleaseEvent( QMouseEvent *e ) override;
433 
437  void keyPressEvent( QKeyEvent *e ) override;
438 
442  void dragEnterEvent( QDragEnterEvent *e ) override;
443 
447  void dragLeaveEvent( QDragLeaveEvent *e ) override;
448 
452  void dropEvent( QDropEvent *e ) override;
453 
454  private:
455 
456 
457  QColor sampleColor( QPoint point ) const;
458 
459  static QScreen *findScreenAt( QPoint pos );
461  QString mColorDialogTitle;
462  QColor mColor;
463  QSize mMinimumSize;
464 
465  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
466 
467  QColor mDefaultColor;
468 
470  QColor mCurrentColor;
471 
472  QString mContext;
473  bool mAllowOpacity = false;
474  bool mColorSet = false;
475 
476  bool mShowNoColorOption = false;
477  QString mNoColorString;
478  bool mShowNull = false;
479 
480  QPoint mDragStartPosition;
481  bool mPickingColor = false;
482 
483  QMenu *mMenu = nullptr;
484 
485  QSize mIconSize;
486  QString mLinkedColorName;
487  bool mShowMenu = true;
488 
497  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
498 
505  void stopPicking( QPoint eventPos, bool samplingColor = true );
506 
507  QColor linkedProjectColor() const;
508 
509  private slots:
510 
511  void buttonClicked();
512 
513  void showColorDialog();
514 
518  void setValidColor( const QColor &newColor );
519 
524  void setValidTemporaryColor( const QColor &newColor );
525 
530  void addRecentColor( const QColor &color );
531 
535  void prepareMenu();
536 };
537 
538 #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...