QGIS API Documentation 3.99.0-Master (26c88405ac0)
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:
62
67 {
70 };
71 Q_ENUM( Behavior )
72
73
80 QgsColorButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &cdt = QString(), QgsColorSchemeRegistry *registry = nullptr );
81
82 QSize minimumSizeHint() const override;
83 QSize sizeHint() const override;
84
90 QColor color() const;
91
98 void setAllowOpacity( bool allowOpacity );
99
106 bool allowOpacity() const { return mAllowOpacity; }
107
113 void setColorDialogTitle( const QString &title );
114
120 QString colorDialogTitle() const;
121
128 void setShowMenu( bool showMenu );
129
135 bool showMenu() const { return menu() ? true : false; }
136
143 void setBehavior( Behavior behavior );
144
150 Behavior behavior() const { return mBehavior; }
151
159 void setDefaultColor( const QColor &color );
160
168 QColor defaultColor() const { return mDefaultColor; }
169
179 void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
180
190 bool showNoColor() const { return mShowNoColorOption; }
191
200 void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
201
209 void setShowNull( bool showNull, const QString &nullString = QString() );
210
216 bool showNull() const;
217
223 bool isNull() const;
224
233 QString noColorString() const { return mNoColorString; }
234
242 void setContext( const QString &context ) { mContext = context; }
243
251 QString context() const { return mContext; }
252
260 void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
261
269 QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
270
285 void linkToProjectColor( const QString &name );
286
293 QString linkedProjectColorName() const { return mLinkedColorName; }
294
303 static QPixmap createMenuIcon( const QColor &color, bool showChecks = true );
304
305 public slots:
306
313 void setColor( const QColor &color );
314
323 void setButtonBackground( const QColor &color = QColor() );
324
329 void copyColor();
330
336 void pasteColor();
337
341 void activatePicker();
342
349 void setToNoColor();
350
357 void setToDefaultColor();
358
365 void setToNull();
366
375 void unlink();
376
377 signals:
378
384 void colorChanged( const QColor &color );
385
392 void colorClicked( const QColor &color );
393
400 void cleared();
401
411 void unlinked();
412
413 protected:
414 bool event( QEvent *e ) override;
415 void changeEvent( QEvent *e ) override;
416 void showEvent( QShowEvent *e ) override;
417 void resizeEvent( QResizeEvent *event ) override;
418
422 static const QPixmap &transparentBackground();
423
427 void mousePressEvent( QMouseEvent *e ) override;
428
432 void mouseMoveEvent( QMouseEvent *e ) override;
433
437 void mouseReleaseEvent( QMouseEvent *e ) override;
438
442 void keyPressEvent( QKeyEvent *e ) override;
443
447 void dragEnterEvent( QDragEnterEvent *e ) override;
448
452 void dragLeaveEvent( QDragLeaveEvent *e ) override;
453
457 void dropEvent( QDropEvent *e ) override;
458
459 void wheelEvent( QWheelEvent *event ) override;
460
461 private:
463 QString mColorDialogTitle;
464 QColor mColor;
465 QSize mMinimumSize;
466
467 QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
468
469 QColor mDefaultColor;
470
472 QColor mCurrentColor;
473
474 QString mContext;
475 bool mAllowOpacity = false;
476 bool mColorSet = false;
477
478 bool mShowNoColorOption = false;
479 QString mNoColorString;
480 bool mShowNull = false;
481 QString mNullColorString;
482
483 QPoint mDragStartPosition;
484 bool mPickingColor = false;
485
486 QMenu *mMenu = nullptr;
487
488 QSize mIconSize;
489 QString mLinkedColorName;
490 bool mShowMenu = true;
491
500 bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
501
508 void stopPicking( QPoint eventPos, bool samplingColor = true );
509
510 QColor linkedProjectColor() const;
511
512 private slots:
513
514 void buttonClicked();
515
516 void showColorDialog();
517
521 void setValidColor( const QColor &newColor );
522
527 void setValidTemporaryColor( const QColor &newColor );
528
533 void addRecentColor( const QColor &color );
534
538 void prepareMenu();
539
540 friend class QgsColorTooltip;
541};
542
543#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