QGIS API Documentation  2.99.0-Master (0cba29c)
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 
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_ENUMS( Behavior )
50  Q_PROPERTY( QString colorDialogTitle READ colorDialogTitle WRITE setColorDialogTitle )
51  Q_PROPERTY( bool acceptLiveUpdates READ acceptLiveUpdates WRITE setAcceptLiveUpdates )
52  Q_PROPERTY( QColor color READ color WRITE setColor )
53  Q_PROPERTY( bool allowOpacity READ allowOpacity WRITE setAllowOpacity )
54  Q_PROPERTY( bool showMenu READ showMenu WRITE setShowMenu )
55  Q_PROPERTY( Behavior behavior READ behavior WRITE setBehavior )
56  Q_PROPERTY( QColor defaultColor READ defaultColor WRITE setDefaultColor )
57  Q_PROPERTY( bool showNoColor READ showNoColor WRITE setShowNoColor )
58  Q_PROPERTY( QString noColorString READ noColorString WRITE setNoColorString )
59  Q_PROPERTY( QString context READ context WRITE setContext )
60 
61  public:
62 
65  enum Behavior
66  {
67  ShowDialog = 0,
68  SignalOnly
69  };
70 
77  QgsColorButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &cdt = "", QgsColorSchemeRegistry *registry = nullptr );
78 
79  virtual QSize minimumSizeHint() const override;
80 
85  QColor color() const;
86 
94  void setAllowOpacity( const bool allowOpacity );
95 
103  bool allowOpacity() const { return mAllowOpacity; }
104 
109  void setColorDialogTitle( const QString &title );
110 
115  QString colorDialogTitle() const;
116 
121  bool acceptLiveUpdates() const { return mAcceptLiveUpdates; }
122 
128  void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
129 
135  void setShowMenu( const bool showMenu );
136 
141  bool showMenu() const { return menu() ? true : false; }
142 
148  void setBehavior( const Behavior behavior );
149 
154  Behavior behavior() const { return mBehavior; }
155 
162  void setDefaultColor( const QColor &color );
163 
170  QColor defaultColor() const { return mDefaultColor; }
171 
180  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
181 
190  bool showNoColor() const { return mShowNoColorOption; }
191 
199  void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
200 
207  void setShowNull( bool showNull );
208 
214  bool showNull() const;
215 
221  bool isNull() const;
222 
230  QString noColorString() const { return mNoColorString; }
231 
238  void setContext( const QString &context ) { mContext = context; }
239 
246  QString context() const { return mContext; }
247 
254  void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
255 
262  QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
263 
264  public slots:
265 
271  void setColor( const QColor &color );
272 
280  void setButtonBackground( const QColor &color = QColor() );
281 
285  void copyColor();
286 
291  void pasteColor();
292 
295  void activatePicker();
296 
302  void setToNoColor();
303 
309  void setToDefaultColor();
310 
316  void setToNull();
317 
318  signals:
319 
324  void colorChanged( const QColor &color );
325 
331  void colorClicked( const QColor &color );
332 
333  protected:
334 
335  bool event( QEvent *e ) override;
336  void changeEvent( QEvent *e ) override;
337  void showEvent( QShowEvent *e ) override;
338  void resizeEvent( QResizeEvent *event ) override;
339 
342  static const QPixmap &transparentBackground();
343 
347  void mousePressEvent( QMouseEvent *e ) override;
348 
352  void mouseMoveEvent( QMouseEvent *e ) override;
353 
357  void mouseReleaseEvent( QMouseEvent *e ) override;
358 
362  void keyPressEvent( QKeyEvent *e ) override;
363 
367  void dragEnterEvent( QDragEnterEvent *e ) override;
368 
372  void dragLeaveEvent( QDragLeaveEvent *e ) override;
373 
377  void dropEvent( QDropEvent *e ) override;
378 
379  private:
380 
381  Behavior mBehavior;
382  QString mColorDialogTitle;
383  QColor mColor;
384 
385  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
386 
387  QColor mDefaultColor;
388  QString mContext;
389  bool mAllowOpacity;
390  bool mAcceptLiveUpdates;
391  bool mColorSet;
392 
393  bool mShowNoColorOption;
394  QString mNoColorString;
395  bool mShowNull;
396 
397  QPoint mDragStartPosition;
398  bool mPickingColor;
399 
400  QMenu *mMenu = nullptr;
401 
402  QSize mIconSize;
403 
411  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
412 
418  void stopPicking( QPointF eventPos, bool sampleColor = true );
419 
425  QPixmap createMenuIcon( const QColor &color, const bool showChecks = true );
426 
427  private slots:
428 
429  void buttonClicked();
430 
431  void showColorDialog();
432 
435  void setValidColor( const QColor &newColor );
436 
440  void setValidTemporaryColor( const QColor &newColor );
441 
445  void addRecentColor( const QColor &color );
446 
449  void prepareMenu();
450 };
451 
452 #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.
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.
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...