QGIS API Documentation 4.3.0-Master (91d4a333d5d)
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;
28
39class GUI_EXPORT QgsColorButton : public QToolButton
40{
41#ifdef SIP_RUN
43 if ( qobject_cast<QgsColorButton *>( sipCpp ) )
44 sipType = sipType_QgsColorButton;
45 else
46 sipType = NULL;
48#endif
49
50
51 Q_OBJECT
52 Q_PROPERTY( QString colorDialogTitle READ colorDialogTitle WRITE setColorDialogTitle )
53 Q_PROPERTY( QColor color READ color WRITE setColor )
54 Q_PROPERTY( bool allowOpacity READ allowOpacity WRITE setAllowOpacity )
55 Q_PROPERTY( bool showMenu READ showMenu WRITE setShowMenu )
56 Q_PROPERTY( Behavior behavior READ behavior WRITE setBehavior )
57 Q_PROPERTY( QColor defaultColor READ defaultColor WRITE setDefaultColor )
58 Q_PROPERTY( bool showNoColor READ showNoColor WRITE setShowNoColor )
59 Q_PROPERTY( QString noColorString READ noColorString WRITE setNoColorString )
61 Q_PROPERTY( QString context READ context WRITE setContext )
62
63 public:
68 {
71 };
72 Q_ENUM( Behavior )
73
74
81 QgsColorButton( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &cdt = QString(), QgsColorSchemeRegistry *registry = nullptr );
82
83 QSize minimumSizeHint() const override;
84 QSize sizeHint() const override;
85
91 QColor color() const;
92
99 void setAllowOpacity( bool allowOpacity );
100
107 bool allowOpacity() const { return mAllowOpacity; }
108
114 void setColorDialogTitle( const QString &title );
115
121 QString colorDialogTitle() const;
122
129 void setShowMenu( bool showMenu );
130
136 bool showMenu() const { return menu() ? true : false; }
137
144 void setBehavior( Behavior behavior );
145
151 Behavior behavior() const { return mBehavior; }
152
160 void setDefaultColor( const QColor &color );
161
169 QColor defaultColor() const { return mDefaultColor; }
170
180 void setShowNoColor( const bool showNoColorOption ) { mShowNoColorOption = showNoColorOption; }
181
191 bool showNoColor() const { return mShowNoColorOption; }
192
202 void setNoColorString( const QString &noColorString ) { mNoColorString = noColorString; }
203
218 void setOpaqueColorString( const QString &opaqueColorString ) { mOpaqueColorString = opaqueColorString; }
219
227 void setShowNull( bool showNull, const QString &nullString = QString() );
228
234 bool showNull() const;
235
241 bool isNull() const;
242
251 QString noColorString() const { return mNoColorString; }
252
260 QString opaqueColorString() const { return mOpaqueColorString; }
261
269 void setContext( const QString &context ) { mContext = context; }
270
278 QString context() const { return mContext; }
279
287 void setColorSchemeRegistry( QgsColorSchemeRegistry *registry ) { mColorSchemeRegistry = registry; }
288
296 QgsColorSchemeRegistry *colorSchemeRegistry() { return mColorSchemeRegistry; }
297
312 void linkToProjectColor( const QString &name );
313
320 QString linkedProjectColorName() const { return mLinkedColorName; }
321
330 static QPixmap createMenuIcon( const QColor &color, bool showChecks = true );
331
332 public slots:
333
340 void setColor( const QColor &color );
341
350 void setButtonBackground( const QColor &color = QColor() );
351
356 void copyColor();
357
363 void pasteColor();
364
368 void activatePicker();
369
377 void setToNoColor();
378
385 void setToOpaqueColor();
386
393 void setToDefaultColor();
394
401 void setToNull();
402
411 void unlink();
412
413 signals:
414
420 void colorChanged( const QColor &color );
421
428 void colorClicked( const QColor &color );
429
436 void cleared();
437
447 void unlinked();
448
449 protected:
450 bool event( QEvent *e ) override;
451 void changeEvent( QEvent *e ) override;
452 void showEvent( QShowEvent *e ) override;
453 void resizeEvent( QResizeEvent *event ) override;
454
458 static const QPixmap &transparentBackground();
459
463 void mousePressEvent( QMouseEvent *e ) override;
464
465 void contextMenuEvent( QContextMenuEvent *e ) override;
466
470 void mouseMoveEvent( QMouseEvent *e ) override;
471
475 void dragEnterEvent( QDragEnterEvent *e ) override;
476
480 void dragLeaveEvent( QDragLeaveEvent *e ) override;
481
485 void dropEvent( QDropEvent *e ) override;
486
487 void wheelEvent( QWheelEvent *event ) override;
488
489 private:
491 QString mColorDialogTitle;
492 QColor mColor;
493 QSize mMinimumSize;
494
495 QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;
496
497 QColor mDefaultColor;
498
499 QgsColorPickerHelper *mPickerHelper = nullptr;
500
501 QString mContext;
502 bool mAllowOpacity = false;
503 bool mColorSet = false;
504
505 bool mShowNoColorOption = false;
506 QString mNoColorString;
507 QString mOpaqueColorString;
508 bool mShowNull = false;
509 QString mNullColorString;
510
511 QPoint mDragStartPosition;
512
513 QMenu *mMenu = nullptr;
514
515 QSize mIconSize;
516 QString mLinkedColorName;
517 bool mShowMenu = true;
518
527 bool colorFromMimeData( const QMimeData *mimeData, QColor &resultColor );
528
529 QColor linkedProjectColor() const;
530
531 private slots:
532
533 void buttonClicked();
534
535 void showColorDialog();
536
540 void setValidColor( const QColor &newColor );
541
546 void setValidTemporaryColor( const QColor &newColor );
547
552 void addRecentColor( const QColor &color );
553
557 void prepareMenu();
558
559 void onColorPicked( const QColor &color );
560 void onColorHovered( const QColor &color );
561 void colorPickingCanceled();
562
563 friend class QgsColorTooltip;
564};
565
566#endif
A cross platform button subclass for selecting colors.
void contextMenuEvent(QContextMenuEvent *e) override
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 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 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 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
A utility class for sampling colors from the screen.
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