QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgscolorbuttonv2.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 QGSCOLORBUTTONV2_H
16 #define QGSCOLORBUTTONV2_H
17 
18 #include <QColorDialog>
19 #include <QToolButton>
20 #include <QTemporaryFile>
21 #include "qgscolorscheme.h"
22 
23 class QMimeData;
25 class QgsPanelWidget;
26 
35 class GUI_EXPORT QgsColorButtonV2 : public QToolButton
36 {
37  Q_OBJECT
38  Q_ENUMS( Behaviour )
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( Behaviour behaviour READ behaviour WRITE setBehaviour )
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 Behaviour
55  {
56  ShowDialog = 0,
57  SignalOnly
58  };
59 
66  QgsColorButtonV2( QWidget *parent = nullptr, const QString& cdt = "", QgsColorSchemeRegistry* registry = nullptr );
67 
68  virtual ~QgsColorButtonV2();
69 
70  virtual QSize sizeHint() const override;
71 
76  QColor color() const;
77 
83  void setAllowAlpha( const bool allowAlpha );
84 
90  bool allowAlpha() const { return mAllowAlpha; }
91 
96  void setColorDialogTitle( const QString& title );
97 
102  QString colorDialogTitle() const;
103 
108  bool acceptLiveUpdates() const { return mAcceptLiveUpdates; }
109 
115  void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
116 
122  void setShowMenu( const bool showMenu );
123 
128  bool showMenu() const { return menu() ? true : false; }
129 
135  void setBehaviour( const Behaviour behaviour );
136 
141  Behaviour behaviour() const { return mBehaviour; }
142 
149  void setDefaultColor( const QColor& color );
150 
157  QColor defaultColor() const { return mDefaultColor; }
158 
167  void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
168 
177  bool showNoColor() const { return mShowNoColorOption; }
178 
186  void setNoColorString( const QString& noColorString ) { mNoColorString = noColorString; }
187 
194  void setShowNull( bool showNull );
195 
201  bool showNull() const;
202 
208  bool isNull() const;
209 
217  QString noColorString() const { return mNoColorString; }
218 
225  void setContext( const QString& context ) { mContext = context; }
226 
233  QString context() const { return mContext; }
234 
241  void setColorSchemeRegistry( QgsColorSchemeRegistry* registry ) { mColorSchemeRegistry = registry; }
242 
249  QgsColorSchemeRegistry* colorSchemeRegistry() { return mColorSchemeRegistry; }
250 
251  public slots:
252 
258  void setColor( const QColor &color );
259 
267  void setButtonBackground( const QColor &color = QColor() );
268 
272  void copyColor();
273 
278  void pasteColor();
279 
282  void activatePicker();
283 
289  void setToNoColor();
290 
296  void setToDefaultColor();
297 
303  void setToNull();
304 
305  signals:
306 
311  void colorChanged( const QColor &color );
312 
318  void colorClicked( const QColor &color );
319 
320  protected:
321 
322  bool event( QEvent *e ) override;
323  void changeEvent( QEvent* e ) override;
324  void showEvent( QShowEvent* e ) override;
325  void resizeEvent( QResizeEvent *event ) override;
326 
329  static const QPixmap& transparentBackground();
330 
334  void mousePressEvent( QMouseEvent* e ) override;
335 
339  void mouseMoveEvent( QMouseEvent *e ) override;
340 
344  void mouseReleaseEvent( QMouseEvent *e ) override;
345 
349  void keyPressEvent( QKeyEvent *e ) override;
350 
354  void dragEnterEvent( QDragEnterEvent * e ) override;
355 
359  void dragLeaveEvent( QDragLeaveEvent *e ) override;
360 
364  void dropEvent( QDropEvent *e ) override;
365 
366  private:
367 
368  Behaviour mBehaviour;
369  QString mColorDialogTitle;
370  QColor mColor;
371 
372  QgsColorSchemeRegistry* mColorSchemeRegistry;
373 
374  QColor mDefaultColor;
375  QString mContext;
376  bool mAllowAlpha;
377  bool mAcceptLiveUpdates;
378  bool mColorSet;
379 
380  bool mShowNoColorOption;
381  QString mNoColorString;
382  bool mShowNull;
383 
384  QPoint mDragStartPosition;
385  bool mPickingColor;
386 
387  QMenu* mMenu;
388 
389  QSize mIconSize;
390 
398  bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
399 
405  void stopPicking( QPointF eventPos, bool sampleColor = true );
406 
412  QPixmap createMenuIcon( const QColor &color, const bool showChecks = true );
413 
414  private slots:
415 
416  void buttonClicked();
417 
418  void showColorDialog();
419 
422  void setValidColor( const QColor& newColor );
423 
427  void setValidTemporaryColor( const QColor& newColor );
428 
430  void panelAccepted( QgsPanelWidget* widget );
431 
435  void addRecentColor( const QColor& color );
436 
439  void prepareMenu();
440 };
441 
442 #endif
bool showMenu() const
Returns whether the drop down menu is shown for the button.
bool showNoColor() const
Returns whether the "no color" option is shown in the button&#39;s drop down menu.
bool acceptLiveUpdates() const
Returns whether the button accepts live updates from QColorDialog.
virtual void dropEvent(QDropEvent *event)
A cross platform button subclass for selecting colors.
Registry of color schemes.
virtual QSize sizeHint() const
void setContext(const QString &context)
Sets the context string for the color button.
virtual void dragLeaveEvent(QDragLeaveEvent *event)
virtual void dragEnterEvent(QDragEnterEvent *event)
virtual void mouseMoveEvent(QMouseEvent *e)
void setNoColorString(const QString &noColorString)
Sets the string to use for the "no color" option in the button&#39;s drop down menu.
Base class for any widget that can be shown as a inline panel.
void setShowNoColor(const bool showNoColorOption)
Sets whether the "no color" option should be shown in the button&#39;s drop down menu.
QColor defaultColor() const
Returns the default color for the button, which is shown in the button&#39;s drop down menu for the "defa...
QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the color scheme registry for the button, which controls the color swatch grids that are show...
virtual void showEvent(QShowEvent *event)
virtual void mousePressEvent(QMouseEvent *e)
Behaviour behaviour() const
Returns the behaviour for when the button is clicked.
virtual void mouseReleaseEvent(QMouseEvent *e)
void setColorSchemeRegistry(QgsColorSchemeRegistry *registry)
Sets the color scheme registry for the button, which controls the color swatch grids that are shown i...
QString context() const
Returns the context string for the color button.
virtual bool event(QEvent *event)
bool allowAlpha() const
Returns whether alpha modification (transparency) is permitted for the color.
virtual void changeEvent(QEvent *e)
QMenu * menu() const
void setAcceptLiveUpdates(const bool accept)
Sets whether the button accepts live updates from QColorDialog.
virtual void keyPressEvent(QKeyEvent *e)
QString noColorString() const
Returns the string used for the "no color" option in the button&#39;s drop down menu. ...
Behaviour
Specifies the behaviour when the button is clicked.
virtual void resizeEvent(QResizeEvent *event)
bool isNull(const QVariant &v)