QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
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:
405 enum ControlPart
406 {
407 None,
408 Wheel,
409 Triangle
410 };
411
412 bool mIsDragging = false;
413
414 /*Margin between outer ring and edge of widget*/
415 int mMargin = 4;
416
417 /*Thickness of hue ring in pixels*/
418 int mWheelThickness = 18;
419
420 /*Part of the wheel where the mouse was originally depressed*/
421 ControlPart mClickedPart = QgsColorWheel::None;
422
423 /*Cached image of hue wheel*/
424 QImage mWheelImage;
425
426 /*Cached image of inner triangle*/
427 QImage mTriangleImage;
428
429 /*Resuable, temporary image for drawing widget*/
430 QImage mWidgetImage;
431
432 /*Whether the color wheel image requires redrawing*/
433 bool mWheelDirty = true;
434
435 /*Whether the inner triangle image requires redrawing*/
436 bool mTriangleDirty = true;
437
438 /*Conical gradient brush used for drawing hue wheel*/
439 QBrush mWheelBrush;
440
445 void createImages( QSizeF size );
446
448 void createWheel();
449
451 void createTriangle();
452
457 void setColorFromPos( QPointF pos );
458};
459
460
470
471class GUI_EXPORT QgsColorBox : public QgsColorWidget
472{
473 Q_OBJECT
474
475 public:
483 QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
484
485 ~QgsColorBox() override;
486
487 QSize sizeHint() const override;
488 void paintEvent( QPaintEvent *event ) override;
489
490 void setComponent( ColorComponent component ) override;
491
492 public slots:
493 void setColor( const QColor &color, bool emitSignals = false ) override;
494
495 protected:
496 void resizeEvent( QResizeEvent *event ) override;
497 void mouseMoveEvent( QMouseEvent *event ) override;
498 void mousePressEvent( QMouseEvent *event ) override;
499 void mouseReleaseEvent( QMouseEvent *event ) override;
500
501 private:
502 bool mIsDragging = false;
503
504 /*Margin between outer ring and edge of widget*/
505 static constexpr float mMargin = 2.;
506
507 /*Cached image for color box*/
508 std::unique_ptr<QImage> mBoxImage;
509
510 /*Whether the cached image requires redrawing*/
511 bool mDirty = true;
512
516 void createBox();
517
522 float valueRangeX() const;
523
528 float valueRangeY() const;
529
533 QgsColorWidget::ColorComponent yComponent() const;
534
538 float yComponentValue() const;
539
543 QgsColorWidget::ColorComponent xComponent() const;
544
548 float xComponentValue() const;
549
554 void setColorFromPoint( QPoint point );
555};
556
557
566
567class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
568{
569 Q_OBJECT
570
571 public:
580
587 QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red, Orientation orientation = QgsColorRampWidget::Horizontal );
588
589 QSize sizeHint() const override;
590 void paintEvent( QPaintEvent *event ) override;
591
597 void setOrientation( Orientation orientation );
598
604 Orientation orientation() const { return mOrientation; }
605
611 void setInteriorMargin( int margin );
612
618 int interiorMargin() const { return mMargin; }
619
625 void setShowFrame( bool showFrame );
626
632 bool showFrame() const { return mShowFrame; }
633
638 void setMarkerSize( int markerSize );
639
640 signals:
641
647 Q_DECL_DEPRECATED void valueChanged( int value ) SIP_DEPRECATED;
648
654 void valueChangedF( float value );
655
656 protected:
657 void mouseMoveEvent( QMouseEvent *event ) override;
658 void wheelEvent( QWheelEvent *event ) override;
659 void mousePressEvent( QMouseEvent *event ) override;
660 void mouseReleaseEvent( QMouseEvent *event ) override;
661 void keyPressEvent( QKeyEvent *event ) override;
662
663 private:
664 bool mIsDragging = false;
665
666 /*Orientation for ramp*/
667 Orientation mOrientation;
668
669 /*Margin around ramp*/
670 int mMargin = 4;
671
672 /*Whether to draw a frame around the ramp*/
673 bool mShowFrame = false;
674
675 /*Polygon for upper triangle marker*/
676 QPolygonF mTopTriangle;
677
678 /*Polygon for lower triangle marker*/
679 QPolygonF mBottomTriangle;
680
685 void setColorFromPoint( QPointF point );
686
688};
689
690
696
697class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
698{
699 Q_OBJECT
700
701 public:
708
709 void setComponent( ColorComponent component ) override;
710 void setComponentValueF( float value ) override;
711 void setColor( const QColor &color, bool emitSignals = false ) override;
712
713 private:
714 /*Color ramp widget*/
715 QgsColorRampWidget *mRampWidget = nullptr;
716
717 /*Spin box widget*/
718 QgsDoubleSpinBox *mSpinBox = nullptr;
719
727 float convertRealToDisplay( float realValue ) const;
728
735 float convertDisplayToReal( float displayValue ) const;
736
737 private slots:
738
742 void rampColorChanged( const QColor &color );
743
747 void spinChanged( double value );
748
752 void rampChanged( float value );
753
755};
756
757
764
765class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
766{
767 Q_OBJECT
768
769 public:
780 Q_ENUM( ColorTextFormat )
781
783
788 QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
789
790 void setColor( const QColor &color, bool emitSignals = false ) override;
791
797 void setAllowOpacity( bool allowOpacity );
798
799 protected:
800 void resizeEvent( QResizeEvent *event ) override;
801
802 private:
803 QLineEdit *mLineEdit = nullptr;
804
805 /*Drop-down menu button*/
806 QToolButton *mMenuButton = nullptr;
807
808 /*Display format for colors*/
810
811 bool mAllowAlpha = true;
812
816 void updateText();
817
818 private slots:
819
823 void textChanged();
824
828 void showMenu();
829};
830
831
837
838class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
839{
840 Q_OBJECT
841
842 public:
847 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
848
849 void paintEvent( QPaintEvent *event ) override;
850 QSize sizeHint() const override;
851
859 QColor color2() const { return mColor2; }
860
861 public slots:
862
870 virtual void setColor2( const QColor &color );
871
872 protected:
873 //reimplemented to allow dragging colors
874 void mousePressEvent( QMouseEvent *e ) override;
875
876 //reimplemented to click colors
877 void mouseReleaseEvent( QMouseEvent *e ) override;
878
879 //reimplemented to allow dragging colors
880 void mouseMoveEvent( QMouseEvent *e ) override;
881
882 private:
883 /*Secondary color for widget*/
884 QColor mColor2;
885
886 QPoint mDragStartPosition;
887
888 /*Draws a color preview within the specified rect.
889 * \param color color to draw
890 * \param rect rect to draw color in
891 * \param painter destination painter
892 */
893 void drawColor( const QColor &color, QRect rect, QPainter &painter );
894};
895
896#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