QGIS API Documentation  2.99.0-Master (5753576)
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 
23 class QMimeData;
25 class QgsPanelWidget;
26 
35 class GUI_EXPORT QgsColorButton : public QToolButton
36 {
37  Q_OBJECT
38  Q_ENUMS( Behavior )
39  Q_PROPERTY( QString colorDialogTitle READ colorDialogTitle WRITE setColorDialogTitle )
40  Q_PROPERTY( bool acceptLiveUpdates READ acceptLiveUpdates WRITE setAcceptLiveUpdates )
41  Q_PROPERTY( QColor color READ color WRITE setColor )
42  Q_PROPERTY( bool allowAlpha READ allowAlpha WRITE setAllowAlpha )
43  Q_PROPERTY( bool showMenu READ showMenu WRITE setShowMenu )
44  Q_PROPERTY( Behavior behavior READ behavior WRITE setBehavior )
45  Q_PROPERTY( QColor defaultColor READ defaultColor WRITE setDefaultColor )
46  Q_PROPERTY( bool showNoColor READ showNoColor WRITE setShowNoColor )
47  Q_PROPERTY( QString noColorString READ noColorString WRITE setNoColorString )
48  Q_PROPERTY( QString context READ context WRITE setContext )
49 
50  public:
51 
54  enum Behavior
55  {
56  ShowDialog = 0,
57  SignalOnly
58  };
59 
66  QgsColorButton( QWidget *parent = nullptr, const QString& cdt = "", QgsColorSchemeRegistry* registry = nullptr );
67 
68  virtual QSize sizeHint() const override;
69 
74  QColor color() const;
75 
81  void setAllowAlpha( const bool allowAlpha );
82 
88  bool allowAlpha() const { return mAllowAlpha; }
89 
94  void setColorDialogTitle( const QString& title );
95 
100  QString colorDialogTitle() const;
101 
106  bool acceptLiveUpdates() const { return mAcceptLiveUpdates; }
107 
113  void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
114 
120  void setShowMenu( const bool showMenu );
121 
126  bool showMenu() const { return menu() ? true : false; }
127 
133  void setBehavior( const Behavior behavior );
134 
139  Behavior behavior() const { return mBehavior; }
140 
147  void setDefaultColor( const QColor& color );
148 
155  QColor defaultColor() const { return mDefaultColor; }
156 
165  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
166 
175  bool showNoColor() const { return mShowNoColorOption; }
176 
184  void setNoColorString( const QString& noColorString ) { mNoColorString = noColorString; }
185 
192  void setShowNull( bool showNull );
193 
199  bool showNull() const;
200 
206  bool isNull() const;
207 
215  QString noColorString() const { return mNoColorString; }
216 
223  void setContext( const QString& context ) { mContext = context; }
224 
231  QString context() const { return mContext; }
232 
239  void setColorSchemeRegistry( QgsColorSchemeRegistry* registry ) { mColorSchemeRegistry = registry; }
240 
247  QgsColorSchemeRegistry* colorSchemeRegistry() { return mColorSchemeRegistry; }
248 
249  public slots:
250 
256  void setColor( const QColor &color );
257 
265  void setButtonBackground( const QColor &color = QColor() );
266 
270  void copyColor();
271 
276  void pasteColor();
277 
280  void activatePicker();
281 
287  void setToNoColor();
288 
294  void setToDefaultColor();
295 
301  void setToNull();
302 
303  signals:
304 
309  void colorChanged( const QColor &color );
310 
316  void colorClicked( const QColor &color );
317 
318  protected:
319 
320  bool event( QEvent *e ) override;
321  void changeEvent( QEvent* e ) override;
322  void showEvent( QShowEvent* e ) override;
323  void resizeEvent( QResizeEvent *event ) override;
324 
327  static const QPixmap& transparentBackground();
328 
332  void mousePressEvent( QMouseEvent* e ) override;
333 
337  void mouseMoveEvent( QMouseEvent *e ) override;
338 
342  void mouseReleaseEvent( QMouseEvent *e ) override;
343 
347  void keyPressEvent( QKeyEvent *e ) override;
348 
352  void dragEnterEvent( QDragEnterEvent * e ) override;
353 
357  void dragLeaveEvent( QDragLeaveEvent *e ) override;
358 
362  void dropEvent( QDropEvent *e ) override;
363 
364  private:
365 
366  Behavior mBehavior;
367  QString mColorDialogTitle;
368  QColor mColor;
369 
370  QgsColorSchemeRegistry* mColorSchemeRegistry;
371 
372  QColor mDefaultColor;
373  QString mContext;
374  bool mAllowAlpha;
375  bool mAcceptLiveUpdates;
376  bool mColorSet;
377 
378  bool mShowNoColorOption;
379  QString mNoColorString;
380  bool mShowNull;
381 
382  QPoint mDragStartPosition;
383  bool mPickingColor;
384 
385  QMenu* mMenu;
386 
387  QSize mIconSize;
388 
396  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
397 
403  void stopPicking( QPointF eventPos, bool sampleColor = true );
404 
410  QPixmap createMenuIcon( const QColor &color, const bool showChecks = true );
411 
412  private slots:
413 
414  void buttonClicked();
415 
416  void showColorDialog();
417 
420  void setValidColor( const QColor& newColor );
421 
425  void setValidTemporaryColor( const QColor& newColor );
426 
430  void addRecentColor( const QColor& color );
431 
434  void prepareMenu();
435 };
436 
437 #endif
QString noColorString() const
Returns the string used for the "no color" option in the button&#39;s drop down menu. ...
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.
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...
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...
bool isNull(const QVariant &v)