QGIS API Documentation 4.3.0-Master (7c7bd4d7018)
Loading...
Searching...
No Matches
qgscolorwidgets.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscolorwidgets.h - color selection widgets
3 ---------------------
4 begin : September 2014
5 copyright : (C) 2014 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
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
16#ifndef QGSCOLORWIDGETS_H
17#define QGSCOLORWIDGETS_H
18
19#include "qgis_gui.h"
20#include "qgis_sip.h"
21
22#include <QWidget>
23#include <QWidgetAction>
24
25class QColor;
26class QLineEdit;
27class QToolButton;
29template<class T> class QgsSettingsEntryEnumFlag;
30
43
44class GUI_EXPORT QgsColorWidget : public QWidget
45{
46 Q_OBJECT
47
48 public:
67
71 enum class ComponentUnit
72 {
73 Scaled0to255,
74 Percent,
75 Degree
76 };
77 Q_ENUM( ComponentUnit )
78
79
84 QgsColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Multiple );
85
91 QColor color() const;
92
99
108 Q_DECL_DEPRECATED int componentValue() const SIP_DEPRECATED;
109
118 float componentValueF() const;
119
124 static QPixmap createDragIcon( const QColor &color );
125
129 static ComponentUnit componentUnit( ColorComponent component );
130
131
132 public slots:
133
140 virtual void setColor( const QColor &color, bool emitSignals = false );
141
147 virtual void setComponent( QgsColorWidget::ColorComponent component );
148
159 Q_DECL_DEPRECATED virtual void setComponentValue( int value ) SIP_DEPRECATED;
160
171 virtual void setComponentValueF( float value );
172
173 signals:
174
179 void colorChanged( const QColor &color );
180
184 void hovered();
185
186 protected:
188
190
195 float mExplicitHue = 0;
196
201 int componentRange() const;
202
208
218 Q_DECL_DEPRECATED int componentValue( ColorComponent component ) const SIP_DEPRECATED;
219
229
236 Q_DECL_DEPRECATED int hue() const SIP_DEPRECATED;
237
244 float hueF() const;
245
254 Q_DECL_DEPRECATED static void alterColor( QColor &color, QgsColorWidget::ColorComponent component, int newValue ) SIP_DEPRECATED;
255
264 static void alterColorF( QColor &color, QgsColorWidget::ColorComponent component, float newValue );
265
269 QColor::Spec colorSpec() const;
270
274 static QColor::Spec colorSpec( QgsColorWidget::ColorComponent component );
275
280 static const QPixmap &transparentBackground();
281
282 //Reimplemented to accept dragged colors
283 void dragEnterEvent( QDragEnterEvent *e ) override;
284
285 //Reimplemented to accept dropped colors
286 void dropEvent( QDropEvent *e ) override;
287
288 void mouseMoveEvent( QMouseEvent *e ) override;
289 void mousePressEvent( QMouseEvent *e ) override;
290 void mouseReleaseEvent( QMouseEvent *e ) override;
291
293};
294
295
302
303class GUI_EXPORT QgsColorWidgetAction : public QWidgetAction
304{
305 Q_OBJECT
306
307 public:
314 QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
315
319 QgsColorWidget *colorWidget() { return mColorWidget; }
320
329 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
330
336 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
337
338 signals:
339
344 void colorChanged( const QColor &color );
345
346 private:
347 QMenu *mMenu = nullptr;
348 QgsColorWidget *mColorWidget = nullptr;
349
350 //used to suppress recursion with hover events
351 bool mSuppressRecurse = false;
352
353 bool mDismissOnColorSelection = true;
354
355 private slots:
356
360 void onHover();
361
365 void setColor( const QColor &color );
366};
367
368
377
378class GUI_EXPORT QgsColorWheel : public QgsColorWidget
379{
380 Q_OBJECT
381
382 public:
387 QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
388
389 ~QgsColorWheel() override;
390
391 QSize sizeHint() const override;
392 void paintEvent( QPaintEvent *event ) override;
393
394 public slots:
395
396 void setColor( const QColor &color, bool emitSignals = false ) override;
397
398 protected:
399 void resizeEvent( QResizeEvent *event ) override;
400 void mouseMoveEvent( QMouseEvent *event ) override;
401 void mousePressEvent( QMouseEvent *event ) override;
402 void mouseReleaseEvent( QMouseEvent *event ) override;
403
404 private slots:
405
406 void invalidateImages();
407
408 private:
409 enum ControlPart
410 {
411 None,
412 Wheel,
413 Triangle
414 };
415
416 bool mIsDragging = false;
417
418 /*Margin between outer ring and edge of widget*/
419 int mMargin = 4;
420
421 /*Thickness of hue ring in pixels*/
422 int mWheelThickness = 18;
423
424 /*Part of the wheel where the mouse was originally depressed*/
425 ControlPart mClickedPart = QgsColorWheel::None;
426
427 /*Cached image of hue wheel*/
428 QImage mWheelImage;
429
430 /*Cached image of inner triangle*/
431 QImage mTriangleImage;
432
433 /*Resuable, temporary image for drawing widget*/
434 QImage mWidgetImage;
435
436 /*Whether the color wheel image requires redrawing*/
437 bool mWheelDirty = true;
438
439 /*Whether the inner triangle image requires redrawing*/
440 bool mTriangleDirty = true;
441
442 /*Conical gradient brush used for drawing hue wheel*/
443 QBrush mWheelBrush;
444
449 void createImages( QSizeF size );
450
452 void createWheel();
453
455 void createTriangle();
456
461 void setColorFromPos( QPointF pos );
462};
463
464
474
475class GUI_EXPORT QgsColorBox : public QgsColorWidget
476{
477 Q_OBJECT
478
479 public:
487 QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
488
489 ~QgsColorBox() override;
490
491 QSize sizeHint() const override;
492 void paintEvent( QPaintEvent *event ) override;
493
494 void setComponent( ColorComponent component ) override;
495
496 public slots:
497 void setColor( const QColor &color, bool emitSignals = false ) override;
498
499 protected:
500 void resizeEvent( QResizeEvent *event ) override;
501 void mouseMoveEvent( QMouseEvent *event ) override;
502 void mousePressEvent( QMouseEvent *event ) override;
503 void mouseReleaseEvent( QMouseEvent *event ) override;
504
505 private:
506 bool mIsDragging = false;
507
508 /*Margin between outer ring and edge of widget*/
509 static constexpr float mMargin = 2.;
510
511 /*Cached image for color box*/
512 std::unique_ptr<QImage> mBoxImage;
513
514 /*Whether the cached image requires redrawing*/
515 bool mDirty = true;
516
520 void createBox();
521
526 float valueRangeX() const;
527
532 float valueRangeY() const;
533
537 QgsColorWidget::ColorComponent yComponent() const;
538
542 float yComponentValue() const;
543
547 QgsColorWidget::ColorComponent xComponent() const;
548
552 float xComponentValue() const;
553
558 void setColorFromPoint( QPoint point );
559};
560
561
570
571class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
572{
573 Q_OBJECT
574
575 public:
584
591 QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red, Orientation orientation = QgsColorRampWidget::Horizontal );
592
593 QSize sizeHint() const override;
594 void paintEvent( QPaintEvent *event ) override;
595
601 void setOrientation( Orientation orientation );
602
608 Orientation orientation() const { return mOrientation; }
609
615 void setInteriorMargin( int margin );
616
622 int interiorMargin() const { return mMargin; }
623
629 void setShowFrame( bool showFrame );
630
636 bool showFrame() const { return mShowFrame; }
637
642 void setMarkerSize( int markerSize );
643
644 signals:
645
651 Q_DECL_DEPRECATED void valueChanged( int value ) SIP_DEPRECATED;
652
658 void valueChangedF( float value );
659
660 protected:
661 void mouseMoveEvent( QMouseEvent *event ) override;
662 void wheelEvent( QWheelEvent *event ) override;
663 void mousePressEvent( QMouseEvent *event ) override;
664 void mouseReleaseEvent( QMouseEvent *event ) override;
665 void keyPressEvent( QKeyEvent *event ) override;
666
667 private:
668 bool mIsDragging = false;
669
670 /*Orientation for ramp*/
671 Orientation mOrientation;
672
673 /*Margin around ramp*/
674 int mMargin = 4;
675
676 /*Whether to draw a frame around the ramp*/
677 bool mShowFrame = false;
678
679 /*Polygon for upper triangle marker*/
680 QPolygonF mTopTriangle;
681
682 /*Polygon for lower triangle marker*/
683 QPolygonF mBottomTriangle;
684
689 void setColorFromPoint( QPointF point );
690
692};
693
694
700
701class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
702{
703 Q_OBJECT
704
705 public:
712
713 void setComponent( ColorComponent component ) override;
714 void setComponentValueF( float value ) override;
715 void setColor( const QColor &color, bool emitSignals = false ) override;
716
717 private:
718 /*Color ramp widget*/
719 QgsColorRampWidget *mRampWidget = nullptr;
720
721 /*Spin box widget*/
722 QgsDoubleSpinBox *mSpinBox = nullptr;
723
731 float convertRealToDisplay( float realValue ) const;
732
739 float convertDisplayToReal( float displayValue ) const;
740
741 private slots:
742
746 void rampColorChanged( const QColor &color );
747
751 void spinChanged( double value );
752
756 void rampChanged( float value );
757
759};
760
761
768
769class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
770{
771 Q_OBJECT
772
773 public:
784 Q_ENUM( ColorTextFormat )
785
787
792 QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
793
794 void setColor( const QColor &color, bool emitSignals = false ) override;
795
801 void setAllowOpacity( bool allowOpacity );
802
803 protected:
804 void resizeEvent( QResizeEvent *event ) override;
805
806 private:
807 QLineEdit *mLineEdit = nullptr;
808
809 /*Drop-down menu button*/
810 QToolButton *mMenuButton = nullptr;
811
812 /*Display format for colors*/
814
815 bool mAllowAlpha = true;
816
820 void updateText();
821
822 private slots:
823
827 void textChanged();
828
832 void showMenu();
833};
834
835
841
842class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
843{
844 Q_OBJECT
845
846 public:
851 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
852
853 void paintEvent( QPaintEvent *event ) override;
854 QSize sizeHint() const override;
855
863 QColor color2() const { return mColor2; }
864
865 public slots:
866
874 virtual void setColor2( const QColor &color );
875
876 protected:
877 //reimplemented to allow dragging colors
878 void mousePressEvent( QMouseEvent *e ) override;
879
880 //reimplemented to click colors
881 void mouseReleaseEvent( QMouseEvent *e ) override;
882
883 //reimplemented to allow dragging colors
884 void mouseMoveEvent( QMouseEvent *e ) override;
885
886 private:
887 /*Secondary color for widget*/
888 QColor mColor2;
889
890 QPoint mDragStartPosition;
891
892 /*Draws a color preview within the specified rect.
893 * \param color color to draw
894 * \param rect rect to draw color in
895 * \param painter destination painter
896 */
897 void drawColor( const QColor &color, QRect rect, QPainter &painter );
898};
899
900#endif // #ifndef QGSCOLORWIDGETS_H
QSize sizeHint() const override
void resizeEvent(QResizeEvent *event) override
void mouseReleaseEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
void setComponent(ColorComponent component) override
Sets the color component which the widget controls.
void setColor(const QColor &color, bool emitSignals=false) override
void mouseMoveEvent(QMouseEvent *event) override
void paintEvent(QPaintEvent *event) override
QgsColorBox(QWidget *parent=nullptr, ColorComponent component=Value)
Construct a new color box widget.
QSize sizeHint() const override
QColor color2() const
Returns the secondary color for the widget.
void paintEvent(QPaintEvent *event) override
QgsColorPreviewWidget(QWidget *parent=nullptr)
Construct a new color preview widget.
A color ramp widget.
Q_DECL_DEPRECATED void valueChanged(int value)
Emitted when the widget's color component value changes.
void keyPressEvent(QKeyEvent *event) override
int interiorMargin() const
Fetches the margin between the edge of the widget and the ramp.
void mousePressEvent(QMouseEvent *event) override
Orientation orientation() const
Fetches the orientation for the color ramp.
void valueChangedF(float value)
Emitted when the widget's color component value changes.
void wheelEvent(QWheelEvent *event) override
QgsColorRampWidget(QWidget *parent=nullptr, ColorComponent component=QgsColorWidget::Red, Orientation orientation=QgsColorRampWidget::Horizontal)
Construct a new color ramp widget.
friend class TestQgsCompoundColorWidget
void mouseMoveEvent(QMouseEvent *event) override
void mouseReleaseEvent(QMouseEvent *event) override
Orientation
Specifies the orientation of a color ramp.
@ Horizontal
Horizontal ramp.
@ Vertical
Vertical ramp.
bool showFrame() const
Fetches whether the ramp is drawn within a frame.
void setColor(const QColor &color, bool emitSignals=false) override
Sets the color for the widget.
void setComponent(ColorComponent component) override
Sets the color component which the widget controls.
void setComponentValueF(float value) override
Alters the widget's color by setting the value for the widget's color component.
friend class TestQgsCompoundColorWidget
QgsColorSliderWidget(QWidget *parent=nullptr, ColorComponent component=QgsColorWidget::Red)
Construct a new color slider widget.
QgsColorTextWidget(QWidget *parent=nullptr)
Construct a new color line edit widget.
ColorTextFormat
Specifies the display format for a color.
@ Rgba
Rgba( r, g, b, a ) format, with alpha.
@ Rgb
Rgb( r, g, b ) format.
@ HexRgbA
#RRGGBBAA in hexadecimal, with alpha
@ HexRgb
#RRGGBB in hexadecimal
void setColor(const QColor &color, bool emitSignals=false) override
Sets the color for the widget.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted.
static const QgsSettingsEntryEnumFlag< ColorTextFormat > * settingsTextFormat
void resizeEvent(QResizeEvent *event) override
void paintEvent(QPaintEvent *event) override
QgsColorWheel(QWidget *parent=nullptr)
Constructs a new color wheel widget.
void mousePressEvent(QMouseEvent *event) override
QSize sizeHint() const override
void mouseReleaseEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void resizeEvent(QResizeEvent *event) override
void setColor(const QColor &color, bool emitSignals=false) override
~QgsColorWheel() override
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action'...
QgsColorWidget * colorWidget()
Returns the color widget contained in the widget action.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
QgsColorWidgetAction(QgsColorWidget *colorWidget, QMenu *menu=nullptr, QWidget *parent=nullptr)
Construct a new color widget action.
bool dismissOnColorSelection() const
Returns whether the parent menu will be dismissed after a color is selected from the action's color w...
A base class for interactive color widgets.
static void alterColorF(QColor &color, QgsColorWidget::ColorComponent component, float newValue)
Alters a color by modifying the value of a specific color component.
static Q_DECL_DEPRECATED void alterColor(QColor &color, QgsColorWidget::ColorComponent component, int newValue)
Alters a color by modifying the value of a specific color component.
void mousePressEvent(QMouseEvent *e) override
void hovered()
Emitted when mouse hovers over widget.
QgsColorWidget(QWidget *parent=nullptr, ColorComponent component=Multiple)
Construct a new color widget.
ColorComponent component() const
Returns the color component which the widget controls.
QColor color() const
Returns the current color for the widget.
ComponentUnit
Specified the color component unit.
void colorChanged(const QColor &color)
Emitted when the widget's color changes.
void mouseReleaseEvent(QMouseEvent *e) override
void mouseMoveEvent(QMouseEvent *e) override
Q_DECL_DEPRECATED int componentValue() const
Returns the current value of the widget's color component.
float hueF() const
Returns the hue for the widget.
float mExplicitHue
QColor wipes the hue information when it is ambiguous (e.g., for saturation = 0).
void dropEvent(QDropEvent *e) override
float componentValueF() const
Returns the current value of the widget's color component.
friend class TestQgsCompoundColorWidget
ColorComponent mComponent
int componentRange() const
Returns the range of valid values for the color widget's component.
QColor::Spec colorSpec() const
Returns color widget type of color, either RGB, HSV, CMYK, or Invalid if this component value is Mult...
Q_DECL_DEPRECATED int hue() const
Returns the hue for the widget.
static const QPixmap & transparentBackground()
Generates a checkboard pattern pixmap for use as a background to transparent colors.
ColorComponent
Specifies the color component which the widget alters.
@ Hue
Hue component of color (based on HSV model).
@ Alpha
Alpha component (opacity) of color.
@ Green
Green component of color.
@ Red
Red component of color.
@ Saturation
Saturation component of color (based on HSV model).
@ Magenta
Magenta component (based on CMYK model) of color.
@ Yellow
Yellow component (based on CMYK model) of color.
@ Black
Black component (based on CMYK model) of color.
@ Cyan
Cyan component (based on CMYK model) of color.
@ Blue
Blue component of color.
@ Value
Value component of color (based on HSV model).
@ Multiple
Widget alters multiple color components.
void dragEnterEvent(QDragEnterEvent *e) override
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
A template class for enum and flag settings entry.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_SKIP
Definition qgis_sip.h:133