QGIS API Documentation  2.99.0-Master (585a4d3)
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.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 
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( const 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( const bool showMenu );
130 
136  bool showMenu() const { return menu() ? true : false; }
137 
144  void setBehavior( const 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 
274  public slots:
275 
282  void setColor( const QColor &color );
283 
292  void setButtonBackground( const QColor &color = QColor() );
293 
298  void copyColor();
299 
305  void pasteColor();
306 
310  void activatePicker();
311 
318  void setToNoColor();
319 
326  void setToDefaultColor();
327 
334  void setToNull();
335 
336  signals:
337 
343  void colorChanged( const QColor &color );
344 
351  void colorClicked( const QColor &color );
352 
353  protected:
354 
355  bool event( QEvent *e ) override;
356  void changeEvent( QEvent *e ) override;
357  void showEvent( QShowEvent *e ) override;
358  void resizeEvent( QResizeEvent *event ) override;
359 
363  static const QPixmap &transparentBackground();
364 
368  void mousePressEvent( QMouseEvent *e ) override;
369 
373  void mouseMoveEvent( QMouseEvent *e ) override;
374 
378  void mouseReleaseEvent( QMouseEvent *e ) override;
379 
383  void keyPressEvent( QKeyEvent *e ) override;
384 
388  void dragEnterEvent( QDragEnterEvent *e ) override;
389 
393  void dragLeaveEvent( QDragLeaveEvent *e ) override;
394 
398  void dropEvent( QDropEvent *e ) override;
399 
400  private:
401 
402 
403  QColor sampleColor( QPoint point ) const;
404 
405  static QScreen *findScreenAt( QPoint pos );
407  QString mColorDialogTitle;
408  QColor mColor;
409  QSize mMinimumSize;
410 
411  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
412 
413  QColor mDefaultColor;
414 
416  QColor mCurrentColor;
417 
418  QString mContext;
419  bool mAllowOpacity = false;
420  bool mColorSet = false;
421 
422  bool mShowNoColorOption = false;
423  QString mNoColorString;
424  bool mShowNull = false;
425 
426  QPoint mDragStartPosition;
427  bool mPickingColor = false;
428 
429  QMenu *mMenu = nullptr;
430 
431  QSize mIconSize;
432 
441  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
442 
449  void stopPicking( QPoint eventPos, bool samplingColor = true );
450 
457  QPixmap createMenuIcon( const QColor &color, const bool showChecks = true );
458 
459  private slots:
460 
461  void buttonClicked();
462 
463  void showColorDialog();
464 
468  void setValidColor( const QColor &newColor );
469 
474  void setValidTemporaryColor( const QColor &newColor );
475 
480  void addRecentColor( const QColor &color );
481 
485  void prepareMenu();
486 };
487 
488 #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:175
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.
QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the color scheme registry for the button, which controls the color swatch grids that are show...