QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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 "qgis_gui.h"
19#include "qgis_sip.h"
20
21#include <QColorDialog>
22#include <QToolButton>
23
24class QMimeData;
26class QgsPanelWidget;
27
38class GUI_EXPORT QgsColorButton : public QToolButton
39{
40#ifdef SIP_RUN
42 if ( qobject_cast<QgsColorButton *>( sipCpp ) )
43 sipType = sipType_QgsColorButton;
44 else
45 sipType = NULL;
47#endif
48
49
50 Q_OBJECT
51 Q_PROPERTY( QString colorDialogTitle READ colorDialogTitle WRITE setColorDialogTitle )
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:
66 {
69 };
70 Q_ENUM( Behavior )
71
72
79 QgsColorButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &cdt = QString(), QgsColorSchemeRegistry *registry = nullptr );
80
81 QSize minimumSizeHint() const override;
82 QSize sizeHint() const override;
83
89 QColor color() const;
90
97 void setAllowOpacity( bool allowOpacity );
98
105 bool allowOpacity() const { return mAllowOpacity; }
106
112 void setColorDialogTitle( const QString &title );
113
119 QString colorDialogTitle() const;
120
127 void setShowMenu( bool showMenu );
128
134 bool showMenu() const { return menu() ? true : false; }
135
142 void setBehavior( Behavior behavior );
143
149 Behavior behavior() const { return mBehavior; }
150
158 void setDefaultColor( const QColor &color );
159
167 QColor defaultColor() const { return mDefaultColor; }
168
178 void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
179
189 bool showNoColor() const { return mShowNoColorOption; }
190
199 void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
200
208 void setShowNull( bool showNull, const QString &nullString = QString() );
209
215 bool showNull() const;
216
222 bool isNull() const;
223
232 QString noColorString() const { return mNoColorString; }
233
241 void setContext( const QString &context ) { mContext = context; }
242
250 QString context() const { return mContext; }
251
259 void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
260
268 QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
269
284 void linkToProjectColor( const QString &name );
285
292 QString linkedProjectColorName() const { return mLinkedColorName; }
293
302 static QPixmap createMenuIcon( const QColor &color, bool showChecks = true );
303
304 public slots:
305
312 void setColor( const QColor &color );
313
322 void setButtonBackground( const QColor &color = QColor() );
323
328 void copyColor();
329
335 void pasteColor();
336
340 void activatePicker();
341
348 void setToNoColor();
349
356 void setToDefaultColor();
357
364 void setToNull();
365
374 void unlink();
375
376 signals:
377
383 void colorChanged( const QColor &color );
384
391 void colorClicked( const QColor &color );
392
399 void cleared();
400
410 void unlinked();
411
412 protected:
413 bool event( QEvent *e ) override;
414 void changeEvent( QEvent *e ) override;
415 void showEvent( QShowEvent *e ) override;
416 void resizeEvent( QResizeEvent *event ) override;
417
421 static const QPixmap &transparentBackground();
422
426 void mousePressEvent( QMouseEvent *e ) override;
427
431 void mouseMoveEvent( QMouseEvent *e ) override;
432
436 void mouseReleaseEvent( QMouseEvent *e ) override;
437
441 void keyPressEvent( QKeyEvent *e ) override;
442
446 void dragEnterEvent( QDragEnterEvent *e ) override;
447
451 void dragLeaveEvent( QDragLeaveEvent *e ) override;
452
456 void dropEvent( QDropEvent *e ) override;
457
458 void wheelEvent( QWheelEvent *event ) override;
459
460 private:
462 QString mColorDialogTitle;
463 QColor mColor;
464 QSize mMinimumSize;
465
466 QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
467
468 QColor mDefaultColor;
469
471 QColor mCurrentColor;
472
473 QString mContext;
474 bool mAllowOpacity = false;
475 bool mColorSet = false;
476
477 bool mShowNoColorOption = false;
478 QString mNoColorString;
479 bool mShowNull = false;
480 QString mNullColorString;
481
482 QPoint mDragStartPosition;
483 bool mPickingColor = false;
484
485 QMenu *mMenu = nullptr;
486
487 QSize mIconSize;
488 QString mLinkedColorName;
489 bool mShowMenu = true;
490
499 bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
500
507 void stopPicking( QPoint eventPos, bool samplingColor = true );
508
509 QColor linkedProjectColor() const;
510
511 private slots:
512
513 void buttonClicked();
514
515 void showColorDialog();
516
520 void setValidColor( const QColor &newColor );
521
526 void setValidTemporaryColor( const QColor &newColor );
527
532 void addRecentColor( const QColor &color );
533
537 void prepareMenu();
538
539 friend class QgsColorTooltip;
540};
541
542#endif
A cross platform button subclass for selecting colors.
bool showNoColor() const
Returns whether the "no color" option is shown in the button's drop-down menu.
bool showMenu() const
Returns whether the drop-down menu is shown for the button.
void setDefaultColor(const QColor &color)
Sets the default color for the button, which is shown in the button's drop-down menu for the "default...
void mouseReleaseEvent(QMouseEvent *e) override
Reimplemented to allow color picking.
void dragLeaveEvent(QDragLeaveEvent *e) override
Reimplemented to reset button appearance after drag leave.
void setColorSchemeRegistry(QgsColorSchemeRegistry *registry)
Sets the color scheme registry for the button, which controls the color swatch grids that are shown i...
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
void setNoColorString(const QString &noColorString)
Sets the string to use for the "no color" option in the button's drop-down menu.
void mousePressEvent(QMouseEvent *e) override
Reimplemented to detect right mouse button clicks on the color button and allow dragging colors.
QString noColorString
bool allowOpacity() const
Returns whether opacity modification (transparency) is permitted for the color.
bool event(QEvent *e) override
void dropEvent(QDropEvent *e) override
Reimplemented to accept dropped colors.
static const QPixmap & transparentBackground()
Returns a checkboard pattern pixmap for use as a background to transparent colors.
Behavior
Specifies the behavior when the button is clicked.
@ ShowDialog
Show a color picker dialog when clicked.
@ SignalOnly
Emit colorClicked signal only, no dialog.
void setShowMenu(bool showMenu)
Sets whether the drop-down menu should be shown for the button.
QgsColorButton(QWidget *parent=nullptr, const QString &cdt=QString(), QgsColorSchemeRegistry *registry=nullptr)
Construct a new color ramp button.
void setBehavior(Behavior behavior)
Sets the behavior for when the button is clicked.
void setColorDialogTitle(const QString &title)
Set the title for the color chooser dialog window.
void unlinked()
Emitted when the color is unlinked, e.g.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color.
void setContext(const QString &context)
Sets the context string for the color button.
void mouseMoveEvent(QMouseEvent *e) override
Reimplemented to allow dragging colors from button.
QString context() const
Returns the context string for the color button.
void showEvent(QShowEvent *e) override
QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the color scheme registry for the button, which controls the color swatch grids that are show...
Behavior behavior() const
Returns the behavior for when the button is clicked.
void colorClicked(const QColor &color)
Emitted when the button is clicked, if the button's behavior is set to SignalOnly.
void setShowNoColor(const bool showNoColorOption)
Sets whether the "no color" option should be shown in the button's drop-down menu.
void cleared()
Emitted when the color is cleared (set to null).
QString colorDialogTitle
QColor defaultColor() const
Returns the default color for the button, which is shown in the button's drop-down menu for the "defa...
void dragEnterEvent(QDragEnterEvent *e) override
Reimplemented to accept dragged colors.
void wheelEvent(QWheelEvent *event) override
QString linkedProjectColorName() const
Returns the linked project color name, if set.
friend class QgsColorTooltip
void keyPressEvent(QKeyEvent *e) override
Reimplemented to allow canceling color pick via keypress, and sample via space bar press.
void resizeEvent(QResizeEvent *event) override
QString noColorString() const
Returns the string used for the "no color" option in the button's drop-down menu.
void setColor(const QColor &color)
Sets the current color for the button.
void changeEvent(QEvent *e) override
Registry of color schemes.
Base class for any widget that can be shown as an inline panel.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:199
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_END
Definition qgis_sip.h:216