QGIS API Documentation 4.3.0-Master (0ff9723465c)
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 )
60 Q_PROPERTY( QString context READ context WRITE setContext )
61
62 public:
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
201 void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
202
217 void setOpaqueColorString( const QString &opaqueColorString ) { mOpaqueColorString = opaqueColorString; }
218
226 void setShowNull( bool showNull, const QString &nullString = QString() );
227
233 bool showNull() const;
234
240 bool isNull() const;
241
250 QString noColorString() const { return mNoColorString; }
251
259 QString opaqueColorString() const { return mOpaqueColorString; }
260
268 void setContext( const QString &context ) { mContext = context; }
269
277 QString context() const { return mContext; }
278
286 void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
287
295 QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
296
311 void linkToProjectColor( const QString &name );
312
319 QString linkedProjectColorName() const { return mLinkedColorName; }
320
329 static QPixmap createMenuIcon( const QColor &color, bool showChecks = true );
330
331 public slots:
332
339 void setColor( const QColor &color );
340
349 void setButtonBackground( const QColor &color = QColor() );
350
355 void copyColor();
356
362 void pasteColor();
363
367 void activatePicker();
368
376 void setToNoColor();
377
384 void setToOpaqueColor();
385
392 void setToDefaultColor();
393
400 void setToNull();
401
410 void unlink();
411
412 signals:
413
419 void colorChanged( const QColor &color );
420
427 void colorClicked( const QColor &color );
428
435 void cleared();
436
446 void unlinked();
447
448 protected:
449 bool event( QEvent *e ) override;
450 void changeEvent( QEvent *e ) override;
451 void showEvent( QShowEvent *e ) override;
452 void resizeEvent( QResizeEvent *event ) override;
453
457 static const QPixmap &transparentBackground();
458
462 void mousePressEvent( QMouseEvent *e ) override;
463
467 void mouseMoveEvent( QMouseEvent *e ) override;
468
472 void mouseReleaseEvent( QMouseEvent *e ) override;
473
477 void keyPressEvent( QKeyEvent *e ) override;
478
482 void dragEnterEvent( QDragEnterEvent *e ) override;
483
487 void dragLeaveEvent( QDragLeaveEvent *e ) override;
488
492 void dropEvent( QDropEvent *e ) override;
493
494 void wheelEvent( QWheelEvent *event ) override;
495
496 private:
498 QString mColorDialogTitle;
499 QColor mColor;
500 QSize mMinimumSize;
501
502 QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
503
504 QColor mDefaultColor;
505
507 QColor mCurrentColor;
508
509 QString mContext;
510 bool mAllowOpacity = false;
511 bool mColorSet = false;
512
513 bool mShowNoColorOption = false;
514 QString mNoColorString;
515 QString mOpaqueColorString;
516 bool mShowNull = false;
517 QString mNullColorString;
518
519 QPoint mDragStartPosition;
520 bool mPickingColor = false;
521
522 QMenu *mMenu = nullptr;
523
524 QSize mIconSize;
525 QString mLinkedColorName;
526 bool mShowMenu = true;
527
536 bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
537
544 void stopPicking( QPoint eventPos, bool samplingColor = true );
545
546 QColor linkedProjectColor() const;
547
548 private slots:
549
550 void buttonClicked();
551
552 void showColorDialog();
553
557 void setValidColor( const QColor &newColor );
558
563 void setValidTemporaryColor( const QColor &newColor );
564
569 void addRecentColor( const QColor &color );
570
574 void prepareMenu();
575
576 friend class QgsColorTooltip;
577};
578
579#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.
QString opaqueColorString() const
Returns the string used for the "opaque color" option in the button's drop-down menu.
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...
QString opaqueColorString
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 setOpaqueColorString(const QString &opaqueColorString)
Sets the string to use for the "opaque color" option in the button's drop-down menu.
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:203
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_END
Definition qgis_sip.h:220