QGIS API Documentation  2.99.0-Master (d55fa22)
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 allowAlpha READ allowAlpha WRITE setAllowAlpha )
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 sizeHint() const override;
80 
85  QColor color() const;
86 
92  void setAllowAlpha( const bool allowAlpha );
93 
99  bool allowAlpha() const { return mAllowAlpha; }
100 
105  void setColorDialogTitle( const QString &title );
106 
111  QString colorDialogTitle() const;
112 
117  bool acceptLiveUpdates() const { return mAcceptLiveUpdates; }
118 
124  void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
125 
131  void setShowMenu( const bool showMenu );
132 
137  bool showMenu() const { return menu() ? true : false; }
138 
144  void setBehavior( const Behavior behavior );
145 
150  Behavior behavior() const { return mBehavior; }
151 
158  void setDefaultColor( const QColor &color );
159 
166  QColor defaultColor() const { return mDefaultColor; }
167 
176  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
177 
186  bool showNoColor() const { return mShowNoColorOption; }
187 
195  void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
196 
203  void setShowNull( bool showNull );
204 
210  bool showNull() const;
211 
217  bool isNull() const;
218 
226  QString noColorString() const { return mNoColorString; }
227 
234  void setContext( const QString &context ) { mContext = context; }
235 
242  QString context() const { return mContext; }
243 
250  void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
251 
258  QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
259 
260  public slots:
261 
267  void setColor( const QColor &color );
268 
276  void setButtonBackground( const QColor &color = QColor() );
277 
281  void copyColor();
282 
287  void pasteColor();
288 
291  void activatePicker();
292 
298  void setToNoColor();
299 
305  void setToDefaultColor();
306 
312  void setToNull();
313 
314  signals:
315 
320  void colorChanged( const QColor &color );
321 
327  void colorClicked( const QColor &color );
328 
329  protected:
330 
331  bool event( QEvent *e ) override;
332  void changeEvent( QEvent *e ) override;
333  void showEvent( QShowEvent *e ) override;
334  void resizeEvent( QResizeEvent *event ) override;
335 
338  static const QPixmap &transparentBackground();
339 
343  void mousePressEvent( QMouseEvent *e ) override;
344 
348  void mouseMoveEvent( QMouseEvent *e ) override;
349 
353  void mouseReleaseEvent( QMouseEvent *e ) override;
354 
358  void keyPressEvent( QKeyEvent *e ) override;
359 
363  void dragEnterEvent( QDragEnterEvent *e ) override;
364 
368  void dragLeaveEvent( QDragLeaveEvent *e ) override;
369 
373  void dropEvent( QDropEvent *e ) override;
374 
375  private:
376 
377  Behavior mBehavior;
378  QString mColorDialogTitle;
379  QColor mColor;
380 
381  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
382 
383  QColor mDefaultColor;
384  QString mContext;
385  bool mAllowAlpha;
386  bool mAcceptLiveUpdates;
387  bool mColorSet;
388 
389  bool mShowNoColorOption;
390  QString mNoColorString;
391  bool mShowNull;
392 
393  QPoint mDragStartPosition;
394  bool mPickingColor;
395 
396  QMenu *mMenu = nullptr;
397 
398  QSize mIconSize;
399 
407  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
408 
414  void stopPicking( QPointF eventPos, bool sampleColor = true );
415 
421  QPixmap createMenuIcon( const QColor &color, const bool showChecks = true );
422 
423  private slots:
424 
425  void buttonClicked();
426 
427  void showColorDialog();
428 
431  void setValidColor( const QColor &newColor );
432 
436  void setValidTemporaryColor( const QColor &newColor );
437 
441  void addRecentColor( const QColor &color );
442 
445  void prepareMenu();
446 };
447 
448 #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:34
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.
bool allowAlpha() const
Returns whether alpha modification (transparency) is permitted for the color.
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:133
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:128
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...