QGIS API Documentation  2.99.0-Master (7d4f81d)
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  virtual QSize sizeHint() const override;
81 
86  QColor color() const;
87 
95  void setAllowOpacity( const bool allowOpacity );
96 
104  bool allowOpacity() const { return mAllowOpacity; }
105 
110  void setColorDialogTitle( const QString &title );
111 
116  QString colorDialogTitle() const;
117 
122  bool acceptLiveUpdates() const { return mAcceptLiveUpdates; }
123 
129  void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
130 
136  void setShowMenu( const bool showMenu );
137 
142  bool showMenu() const { return menu() ? true : false; }
143 
149  void setBehavior( const Behavior behavior );
150 
155  Behavior behavior() const { return mBehavior; }
156 
163  void setDefaultColor( const QColor &color );
164 
171  QColor defaultColor() const { return mDefaultColor; }
172 
181  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
182 
191  bool showNoColor() const { return mShowNoColorOption; }
192 
200  void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
201 
208  void setShowNull( bool showNull );
209 
215  bool showNull() const;
216 
222  bool isNull() const;
223 
231  QString noColorString() const { return mNoColorString; }
232 
239  void setContext( const QString &context ) { mContext = context; }
240 
247  QString context() const { return mContext; }
248 
255  void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
256 
263  QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
264 
265  public slots:
266 
272  void setColor( const QColor &color );
273 
281  void setButtonBackground( const QColor &color = QColor() );
282 
286  void copyColor();
287 
292  void pasteColor();
293 
296  void activatePicker();
297 
303  void setToNoColor();
304 
310  void setToDefaultColor();
311 
317  void setToNull();
318 
319  signals:
320 
325  void colorChanged( const QColor &color );
326 
332  void colorClicked( const QColor &color );
333 
334  protected:
335 
336  bool event( QEvent *e ) override;
337  void changeEvent( QEvent *e ) override;
338  void showEvent( QShowEvent *e ) override;
339  void resizeEvent( QResizeEvent *event ) override;
340 
343  static const QPixmap &transparentBackground();
344 
348  void mousePressEvent( QMouseEvent *e ) override;
349 
353  void mouseMoveEvent( QMouseEvent *e ) override;
354 
358  void mouseReleaseEvent( QMouseEvent *e ) override;
359 
363  void keyPressEvent( QKeyEvent *e ) override;
364 
368  void dragEnterEvent( QDragEnterEvent *e ) override;
369 
373  void dragLeaveEvent( QDragLeaveEvent *e ) override;
374 
378  void dropEvent( QDropEvent *e ) override;
379 
380  private:
381 
382  Behavior mBehavior;
383  QString mColorDialogTitle;
384  QColor mColor;
385  QSize mMinimumSize;
386 
387  QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
388 
389  QColor mDefaultColor;
390  QString mContext;
391  bool mAllowOpacity;
392  bool mAcceptLiveUpdates;
393  bool mColorSet;
394 
395  bool mShowNoColorOption;
396  QString mNoColorString;
397  bool mShowNull;
398 
399  QPoint mDragStartPosition;
400  bool mPickingColor;
401 
402  QMenu *mMenu = nullptr;
403 
404  QSize mIconSize;
405 
413  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
414 
420  void stopPicking( QPointF eventPos, bool sampleColor = true );
421 
427  QPixmap createMenuIcon( const QColor &color, const bool showChecks = true );
428 
429  private slots:
430 
431  void buttonClicked();
432 
433  void showColorDialog();
434 
437  void setValidColor( const QColor &newColor );
438 
442  void setValidTemporaryColor( const QColor &newColor );
443 
447  void addRecentColor( const QColor &color );
448 
451  void prepareMenu();
452 };
453 
454 #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...