QGIS API Documentation  2.99.0-Master (19b062c)
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( 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 
66  enum Behavior
67  {
68  ShowDialog = 0,
69  SignalOnly
70  };
71  Q_ENUM( Behavior );
72 
80  QgsColorButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &cdt = QString(), QgsColorSchemeRegistry *registry = nullptr );
81 
82  virtual QSize minimumSizeHint() const override;
83  virtual QSize sizeHint() const override;
84 
90  QColor color() const;
91 
99  void setAllowOpacity( const bool allowOpacity );
100 
108  bool allowOpacity() const { return mAllowOpacity; }
109 
115  void setColorDialogTitle( const QString &title );
116 
122  QString colorDialogTitle() const;
123 
129  bool acceptLiveUpdates() const { return mAcceptLiveUpdates; }
130 
137  void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
138 
145  void setShowMenu( const bool showMenu );
146 
152  bool showMenu() const { return menu() ? true : false; }
153 
160  void setBehavior( const Behavior behavior );
161 
167  Behavior behavior() const { return mBehavior; }
168 
176  void setDefaultColor( const QColor &color );
177 
185  QColor defaultColor() const { return mDefaultColor; }
186 
196  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
197 
207  bool showNoColor() const { return mShowNoColorOption; }
208 
217  void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
218 
226  void setShowNull( bool showNull );
227 
234  bool showNull() const;
235 
242  bool isNull() const;
243 
252  QString noColorString() const { return mNoColorString; }
253 
261  void setContext( const QString &context ) { mContext = context; }
262 
270  QString context() const { return mContext; }
271 
279  void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
280 
288  QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
289 
290  public slots:
291 
298  void setColor( const QColor &color );
299 
308  void setButtonBackground( const QColor &color = QColor() );
309 
314  void copyColor();
315 
321  void pasteColor();
322 
326  void activatePicker();
327 
334  void setToNoColor();
335 
342  void setToDefaultColor();
343 
350  void setToNull();
351 
352  signals:
353 
359  void colorChanged( const QColor &color );
360 
367  void colorClicked( const QColor &color );
368 
369  protected:
370 
371  bool event( QEvent *e ) override;
372  void changeEvent( QEvent *e ) override;
373  void showEvent( QShowEvent *e ) override;
374  void resizeEvent( QResizeEvent *event ) override;
375 
379  static const QPixmap &transparentBackground();
380 
384  void mousePressEvent( QMouseEvent *e ) override;
385 
389  void mouseMoveEvent( QMouseEvent *e ) override;
390 
394  void mouseReleaseEvent( QMouseEvent *e ) override;
395 
399  void keyPressEvent( QKeyEvent *e ) override;
400 
404  void dragEnterEvent( QDragEnterEvent *e ) override;
405 
409  void dragLeaveEvent( QDragLeaveEvent *e ) override;
410 
414  void dropEvent( QDropEvent *e ) override;
415 
416  private:
417 
418  static QScreen *findScreenAt( const QPoint &pos );
420  QString mColorDialogTitle;
421  QColor mColor;
422  QSize mMinimumSize;
423 
424  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
425 
426  QColor mDefaultColor;
427  QString mContext;
428  bool mAllowOpacity = false;
429  bool mAcceptLiveUpdates = true;
430  bool mColorSet = false;
431 
432  bool mShowNoColorOption = false;
433  QString mNoColorString;
434  bool mShowNull = false;
435 
436  QPoint mDragStartPosition;
437  bool mPickingColor = false;
438 
439  QMenu *mMenu = nullptr;
440 
441  QSize mIconSize;
442 
451  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
452 
459  void stopPicking( QPointF eventPos, bool sampleColor = true );
460 
467  QPixmap createMenuIcon( const QColor &color, const bool showChecks = true );
468 
469  private slots:
470 
471  void buttonClicked();
472 
473  void showColorDialog();
474 
478  void setValidColor( const QColor &newColor );
479 
484  void setValidTemporaryColor( const QColor &newColor );
485 
490  void addRecentColor( const QColor &color );
491 
495  void prepareMenu();
496 };
497 
498 #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.
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...