QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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:
66
70 enum class ComponentUnit
71 {
72 Scaled0to255,
73 Percent,
74 Degree
75 };
76 Q_ENUM( ComponentUnit )
77
78
83 QgsColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Multiple );
84
90 QColor color() const;
91
98
107 Q_DECL_DEPRECATED int componentValue() const SIP_DEPRECATED;
108
117 float componentValueF() const;
118
123 static QPixmap createDragIcon( const QColor &color );
124
128 static ComponentUnit componentUnit( ColorComponent component );
129
130
131 public slots:
132
139 virtual void setColor( const QColor &color, bool emitSignals = false );
140
146 virtual void setComponent( QgsColorWidget::ColorComponent component );
147
158 Q_DECL_DEPRECATED virtual void setComponentValue( int value ) SIP_DEPRECATED;
159
170 virtual void setComponentValueF( float value );
171
172 signals:
173
178 void colorChanged( const QColor &color );
179
183 void hovered();
184
185 protected:
187
189
194 float mExplicitHue = 0;
195
200 int componentRange() const;
201
207
217 Q_DECL_DEPRECATED int componentValue( ColorComponent component ) const SIP_DEPRECATED;
218
228
235 Q_DECL_DEPRECATED int hue() const SIP_DEPRECATED;
236
243 float hueF() const;
244
253 Q_DECL_DEPRECATED static void alterColor( QColor &color, QgsColorWidget::ColorComponent component, int newValue ) SIP_DEPRECATED;
254
263 static void alterColorF( QColor &color, QgsColorWidget::ColorComponent component, float newValue );
264
268 QColor::Spec colorSpec() const;
269
273 static QColor::Spec colorSpec( QgsColorWidget::ColorComponent component );
274
279 static const QPixmap &transparentBackground();
280
281 //Reimplemented to accept dragged colors
282 void dragEnterEvent( QDragEnterEvent *e ) override;
283
284 //Reimplemented to accept dropped colors
285 void dropEvent( QDropEvent *e ) override;
286
287 void mouseMoveEvent( QMouseEvent *e ) override;
288 void mousePressEvent( QMouseEvent *e ) override;
289 void mouseReleaseEvent( QMouseEvent *e ) override;
290
292};
293
294
301
302class GUI_EXPORT QgsColorWidgetAction : public QWidgetAction
303{
304 Q_OBJECT
305
306 public:
313 QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
314
318 QgsColorWidget *colorWidget() { return mColorWidget; }
319
328 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
329
335 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
336
337 signals:
338
343 void colorChanged( const QColor &color );
344
345 private:
346 QMenu *mMenu = nullptr;
347 QgsColorWidget *mColorWidget = nullptr;
348
349 //used to suppress recursion with hover events
350 bool mSuppressRecurse = false;
351
352 bool mDismissOnColorSelection = true;
353
354 private slots:
355
359 void onHover();
360
364 void setColor( const QColor &color );
365};
366
367
376
377class GUI_EXPORT QgsColorWheel : public QgsColorWidget
378{
379 Q_OBJECT
380
381 public:
386 QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
387
388 ~QgsColorWheel() override;
389
390 QSize sizeHint() const override;
391 void paintEvent( QPaintEvent *event ) override;
392
393 public slots:
394
395 void setColor( const QColor &color, bool emitSignals = false ) override;
396
397 protected:
398 void resizeEvent( QResizeEvent *event ) override;
399 void mouseMoveEvent( QMouseEvent *event ) override;
400 void mousePressEvent( QMouseEvent *event ) override;
401 void mouseReleaseEvent( QMouseEvent *event ) override;
402
403 private:
404 enum ControlPart
405 {
406 None,
407 Wheel,
408 Triangle
409 };
410
411 bool mIsDragging = false;
412
413 /*Margin between outer ring and edge of widget*/
414 int mMargin = 4;
415
416 /*Thickness of hue ring in pixels*/
417 int mWheelThickness = 18;
418
419 /*Part of the wheel where the mouse was originally depressed*/
420 ControlPart mClickedPart = QgsColorWheel::None;
421
422 /*Cached image of hue wheel*/
423 QImage mWheelImage;
424
425 /*Cached image of inner triangle*/
426 QImage mTriangleImage;
427
428 /*Resuable, temporary image for drawing widget*/
429 QImage mWidgetImage;
430
431 /*Whether the color wheel image requires redrawing*/
432 bool mWheelDirty = true;
433
434 /*Whether the inner triangle image requires redrawing*/
435 bool mTriangleDirty = true;
436
437 /*Conical gradient brush used for drawing hue wheel*/
438 QBrush mWheelBrush;
439
444 void createImages( QSizeF size );
445
447 void createWheel();
448
450 void createTriangle();
451
456 void setColorFromPos( QPointF pos );
457};
458
459
469
470class GUI_EXPORT QgsColorBox : public QgsColorWidget
471{
472 Q_OBJECT
473
474 public:
482 QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
483
484 ~QgsColorBox() override;
485
486 QSize sizeHint() const override;
487 void paintEvent( QPaintEvent *event ) override;
488
489 void setComponent( ColorComponent component ) override;
490
491 public slots:
492 void setColor( const QColor &color, bool emitSignals = false ) override;
493
494 protected:
495 void resizeEvent( QResizeEvent *event ) override;
496 void mouseMoveEvent( QMouseEvent *event ) override;
497 void mousePressEvent( QMouseEvent *event ) override;
498 void mouseReleaseEvent( QMouseEvent *event ) override;
499
500 private:
501 bool mIsDragging = false;
502
503 /*Margin between outer ring and edge of widget*/
504 static constexpr float mMargin = 2.;
505
506 /*Cached image for color box*/
507 QImage *mBoxImage = nullptr;
508
509 /*Whether the cached image requires redrawing*/
510 bool mDirty = true;
511
515 void createBox();
516
521 float valueRangeX() const;
522
527 float valueRangeY() const;
528
532 QgsColorWidget::ColorComponent yComponent() const;
533
537 float yComponentValue() const;
538
542 QgsColorWidget::ColorComponent xComponent() const;
543
547 float xComponentValue() const;
548
553 void setColorFromPoint( QPoint point );
554};
555
556
565
566class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
567{
568 Q_OBJECT
569
570 public:
579
586 QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red, Orientation orientation = QgsColorRampWidget::Horizontal );
587
588 QSize sizeHint() const override;
589 void paintEvent( QPaintEvent *event ) override;
590
596 void setOrientation( Orientation orientation );
597
603 Orientation orientation() const { return mOrientation; }
604
610 void setInteriorMargin( int margin );
611
617 int interiorMargin() const { return mMargin; }
618
624 void setShowFrame( bool showFrame );
625
631 bool showFrame() const { return mShowFrame; }
632
637 void setMarkerSize( int markerSize );
638
639 signals:
640
646 Q_DECL_DEPRECATED void valueChanged( int value ) SIP_DEPRECATED;
647
653 void valueChangedF( float value );
654
655 protected:
656 void mouseMoveEvent( QMouseEvent *event ) override;
657 void wheelEvent( QWheelEvent *event ) override;
658 void mousePressEvent( QMouseEvent *event ) override;
659 void mouseReleaseEvent( QMouseEvent *event ) override;
660 void keyPressEvent( QKeyEvent *event ) override;
661
662 private:
663 bool mIsDragging = false;
664
665 /*Orientation for ramp*/
666 Orientation mOrientation;
667
668 /*Margin around ramp*/
669 int mMargin = 4;
670
671 /*Whether to draw a frame around the ramp*/
672 bool mShowFrame = false;
673
674 /*Polygon for upper triangle marker*/
675 QPolygonF mTopTriangle;
676
677 /*Polygon for lower triangle marker*/
678 QPolygonF mBottomTriangle;
679
684 void setColorFromPoint( QPointF point );
685
687};
688
689
695
696class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
697{
698 Q_OBJECT
699
700 public:
707
708 void setComponent( ColorComponent component ) override;
709 void setComponentValueF( float value ) override;
710 void setColor( const QColor &color, bool emitSignals = false ) override;
711
712 private:
713 /*Color ramp widget*/
714 QgsColorRampWidget *mRampWidget = nullptr;
715
716 /*Spin box widget*/
717 QgsDoubleSpinBox *mSpinBox = nullptr;
718
726 float convertRealToDisplay( float realValue ) const;
727
734 float convertDisplayToReal( float displayValue ) const;
735
736 private slots:
737
741 void rampColorChanged( const QColor &color );
742
746 void spinChanged( double value );
747
751 void rampChanged( float value );
752
754};
755
756
763
764class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
765{
766 Q_OBJECT
767
768 public:
779 Q_ENUM( ColorTextFormat )
780
781
785 QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
786
787 void setColor( const QColor &color, bool emitSignals = false ) override;
788
794 void setAllowOpacity( bool allowOpacity );
795
796 protected:
797 void resizeEvent( QResizeEvent *event ) override;
798
799 private:
800 QLineEdit *mLineEdit = nullptr;
801
802 /*Drop-down menu button*/
803 QToolButton *mMenuButton = nullptr;
804
805 /*Display format for colors*/
806 ColorTextFormat mFormat = QgsColorTextWidget::HexRgb;
807
808 bool mAllowAlpha = true;
809
813 void updateText();
814
815 private slots:
816
820 void textChanged();
821
825 void showMenu();
826};
827
828
834
835class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
836{
837 Q_OBJECT
838
839 public:
844 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
845
846 void paintEvent( QPaintEvent *event ) override;
847 QSize sizeHint() const override;
848
856 QColor color2() const { return mColor2; }
857
858 public slots:
859
867 virtual void setColor2( const QColor &color );
868
869 protected:
870 //reimplemented to allow dragging colors
871 void mousePressEvent( QMouseEvent *e ) override;
872
873 //reimplemented to click colors
874 void mouseReleaseEvent( QMouseEvent *e ) override;
875
876 //reimplemented to allow dragging colors
877 void mouseMoveEvent( QMouseEvent *e ) override;
878
879 private:
880 /*Secondary color for widget*/
881 QColor mColor2;
882
883 QPoint mDragStartPosition;
884
885 /*Draws a color preview within the specified rect.
886 * \param color color to draw
887 * \param rect rect to draw color in
888 * \param painter destination painter
889 */
890 void drawColor( const QColor &color, QRect rect, QPainter &painter );
891};
892
893#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