QGIS API Documentation 3.99.0-Master (26c88405ac0)
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;
29
42
43class GUI_EXPORT QgsColorWidget : public QWidget
44{
45 Q_OBJECT
46
47 public:
48
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:
308
315 QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
316
320 QgsColorWidget *colorWidget() { return mColorWidget; }
321
330 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
331
337 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
338
339 signals:
340
345 void colorChanged( const QColor &color );
346
347 private:
348 QMenu *mMenu = nullptr;
349 QgsColorWidget *mColorWidget = nullptr;
350
351 //used to suppress recursion with hover events
352 bool mSuppressRecurse;
353
354 bool mDismissOnColorSelection;
355
356 private slots:
357
361 void onHover();
362
366 void setColor( const QColor &color );
367};
368
369
378
379class GUI_EXPORT QgsColorWheel : public QgsColorWidget
380{
381 Q_OBJECT
382
383 public:
384
389 QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
390
391 ~QgsColorWheel() override;
392
393 QSize sizeHint() const override;
394 void paintEvent( QPaintEvent *event ) override;
395
396 public slots:
397
398 void setColor( const QColor &color, bool emitSignals = false ) override;
399
400 protected:
401 void resizeEvent( QResizeEvent *event ) override;
402 void mouseMoveEvent( QMouseEvent *event ) override;
403 void mousePressEvent( QMouseEvent *event ) override;
404 void mouseReleaseEvent( QMouseEvent *event ) override;
405
406 private:
407 enum ControlPart
408 {
409 None,
410 Wheel,
411 Triangle
412 };
413
414 bool mIsDragging = false;
415
416 /*Margin between outer ring and edge of widget*/
417 int mMargin = 4;
418
419 /*Thickness of hue ring in pixels*/
420 int mWheelThickness = 18;
421
422 /*Part of the wheel where the mouse was originally depressed*/
423 ControlPart mClickedPart = QgsColorWheel::None;
424
425 /*Cached image of hue wheel*/
426 QImage mWheelImage;
427
428 /*Cached image of inner triangle*/
429 QImage mTriangleImage;
430
431 /*Resuable, temporary image for drawing widget*/
432 QImage mWidgetImage;
433
434 /*Whether the color wheel image requires redrawing*/
435 bool mWheelDirty = true;
436
437 /*Whether the inner triangle image requires redrawing*/
438 bool mTriangleDirty = true;
439
440 /*Conical gradient brush used for drawing hue wheel*/
441 QBrush mWheelBrush;
442
447 void createImages( QSizeF size );
448
450 void createWheel();
451
453 void createTriangle();
454
459 void setColorFromPos( QPointF pos );
460};
461
462
472
473class GUI_EXPORT QgsColorBox : public QgsColorWidget
474{
475 Q_OBJECT
476
477 public:
478
486 QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
487
488 ~QgsColorBox() override;
489
490 QSize sizeHint() const override;
491 void paintEvent( QPaintEvent *event ) override;
492
493 void setComponent( ColorComponent component ) override;
494
495 public slots:
496 void setColor( const QColor &color, bool emitSignals = false ) override;
497
498 protected:
499 void resizeEvent( QResizeEvent *event ) override;
500 void mouseMoveEvent( QMouseEvent *event ) override;
501 void mousePressEvent( QMouseEvent *event ) override;
502 void mouseReleaseEvent( QMouseEvent *event ) override;
503
504 private:
505 bool mIsDragging = false;
506
507 /*Margin between outer ring and edge of widget*/
508 static constexpr float mMargin = 2.;
509
510 /*Cached image for color box*/
511 QImage *mBoxImage = nullptr;
512
513 /*Whether the cached image requires redrawing*/
514 bool mDirty = true;
515
519 void createBox();
520
525 float valueRangeX() const;
526
531 float valueRangeY() const;
532
536 QgsColorWidget::ColorComponent yComponent() const;
537
541 float yComponentValue() const;
542
546 QgsColorWidget::ColorComponent xComponent() const;
547
551 float xComponentValue() const;
552
557 void setColorFromPoint( QPoint point );
558};
559
560
569
570class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
571{
572 Q_OBJECT
573
574 public:
575
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:
706
713
714 void setComponent( ColorComponent component ) override;
715 void setComponentValueF( float value ) override;
716 void setColor( const QColor &color, bool emitSignals = false ) override;
717
718 private:
719 /*Color ramp widget*/
720 QgsColorRampWidget *mRampWidget = nullptr;
721
722 /*Spin box widget*/
723 QgsDoubleSpinBox *mSpinBox = nullptr;
724
732 float convertRealToDisplay( float realValue ) const;
733
740 float convertDisplayToReal( float displayValue ) const;
741
742 private slots:
743
747 void rampColorChanged( const QColor &color );
748
752 void spinChanged( double value );
753
757 void rampChanged( float value );
758
760};
761
762
769
770class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
771{
772 Q_OBJECT
773
774 public:
775
786 Q_ENUM( ColorTextFormat )
787
788
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*/
813 ColorTextFormat mFormat = QgsColorTextWidget::HexRgb;
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:
847
852 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
853
854 void paintEvent( QPaintEvent *event ) override;
855 QSize sizeHint() const override;
856
864 QColor color2() const { return mColor2; }
865
866 public slots:
867
875 virtual void setColor2( const QColor &color );
876
877 protected:
878 //reimplemented to allow dragging colors
879 void mousePressEvent( QMouseEvent *e ) override;
880
881 //reimplemented to click colors
882 void mouseReleaseEvent( QMouseEvent *e ) override;
883
884 //reimplemented to allow dragging colors
885 void mouseMoveEvent( QMouseEvent *e ) override;
886
887 private:
888 /*Secondary color for widget*/
889 QColor mColor2;
890
891 QPoint mDragStartPosition;
892
893 /*Draws a color preview within the specified rect.
894 * \param color color to draw
895 * \param rect rect to draw color in
896 * \param painter destination painter
897 */
898 void drawColor( const QColor &color, QRect rect, QPainter &painter );
899};
900
901#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.
A line edit widget which displays colors as text and accepts string representations of colors.
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 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.
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53