QGIS API Documentation 3.39.0-Master (d85f3c2a281)
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 <QWidgetAction>
20#include <QWidget>
21#include "qgis_gui.h"
22#include "qgis_sip.h"
23
24class QColor;
25class QLineEdit;
26class QToolButton;
28
38class GUI_EXPORT QgsColorWidget : public QWidget
39{
40 Q_OBJECT
41
42 public:
43
62
66 enum class ComponentUnit
67 {
68 Scaled0to255,
69 Percent,
70 Degree
71 };
72 Q_ENUM( ComponentUnit )
73
74
79 QgsColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Multiple );
80
86 QColor color() const;
87
93 ColorComponent component() const { return mComponent; }
94
103 Q_DECL_DEPRECATED int componentValue() const SIP_DEPRECATED;
104
113 float componentValueF() const;
114
119 static QPixmap createDragIcon( const QColor &color );
120
124 static ComponentUnit componentUnit( ColorComponent component );
125
126
127 public slots:
128
135 virtual void setColor( const QColor &color, bool emitSignals = false );
136
142 virtual void setComponent( QgsColorWidget::ColorComponent component );
143
154 Q_DECL_DEPRECATED virtual void setComponentValue( int value ) SIP_DEPRECATED;
155
166 virtual void setComponentValueF( float value );
167
168 signals:
169
174 void colorChanged( const QColor &color );
175
179 void hovered();
180
181 protected:
182
183 QColor mCurrentColor;
184
185 ColorComponent mComponent;
186
191 float mExplicitHue = 0;
192
197 int componentRange() const;
198
203 static int componentRange( ColorComponent component );
204
214 Q_DECL_DEPRECATED int componentValue( ColorComponent component ) const SIP_DEPRECATED;
215
224 float componentValueF( ColorComponent component ) const;
225
232 Q_DECL_DEPRECATED int hue() const SIP_DEPRECATED;
233
240 float hueF() const;
241
250 Q_DECL_DEPRECATED static void alterColor( QColor &color, QgsColorWidget::ColorComponent component, int newValue ) SIP_DEPRECATED;
251
260 static void alterColorF( QColor &color, QgsColorWidget::ColorComponent component, float newValue );
261
265 QColor::Spec colorSpec() const;
266
270 static QColor::Spec colorSpec( QgsColorWidget::ColorComponent component );
271
276 static const QPixmap &transparentBackground();
277
278 //Reimplemented to accept dragged colors
279 void dragEnterEvent( QDragEnterEvent *e ) override;
280
281 //Reimplemented to accept dropped colors
282 void dropEvent( QDropEvent *e ) override;
283
284 void mouseMoveEvent( QMouseEvent *e ) override;
285 void mousePressEvent( QMouseEvent *e ) override;
286 void mouseReleaseEvent( QMouseEvent *e ) override;
287
288 friend class TestQgsCompoundColorWidget;
289};
290
291
299class GUI_EXPORT QgsColorWidgetAction: public QWidgetAction
300{
301 Q_OBJECT
302
303 public:
304
311 QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
312
316 QgsColorWidget *colorWidget() { return mColorWidget; }
317
326 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
327
333 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
334
335 signals:
336
341 void colorChanged( const QColor &color );
342
343 private:
344 QMenu *mMenu = nullptr;
345 QgsColorWidget *mColorWidget = nullptr;
346
347 //used to suppress recursion with hover events
348 bool mSuppressRecurse;
349
350 bool mDismissOnColorSelection;
351
352 private slots:
353
357 void onHover();
358
362 void setColor( const QColor &color );
363};
364
365
366
374class GUI_EXPORT QgsColorWheel : public QgsColorWidget
375{
376 Q_OBJECT
377
378 public:
379
384 QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
385
386 ~QgsColorWheel() override;
387
388 QSize sizeHint() const override;
389 void paintEvent( QPaintEvent *event ) override;
390
391 public slots:
392
393 void setColor( const QColor &color, bool emitSignals = false ) override;
394
395 protected:
396
397 void resizeEvent( QResizeEvent *event ) override;
398 void mouseMoveEvent( QMouseEvent *event ) override;
399 void mousePressEvent( QMouseEvent *event ) override;
400 void mouseReleaseEvent( QMouseEvent *event ) override;
401
402 private:
403
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
460
469class GUI_EXPORT QgsColorBox : public QgsColorWidget
470{
471 Q_OBJECT
472
473 public:
474
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
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
503 bool mIsDragging = false;
504
505 /*Margin between outer ring and edge of widget*/
506 static constexpr float mMargin = 2.;
507
508 /*Cached image for color box*/
509 QImage *mBoxImage = nullptr;
510
511 /*Whether the cached image requires redrawing*/
512 bool mDirty = true;
513
517 void createBox();
518
523 float valueRangeX() const;
524
529 float valueRangeY() const;
530
534 QgsColorWidget::ColorComponent yComponent() const;
535
539 float yComponentValue() const;
540
544 QgsColorWidget::ColorComponent xComponent() const;
545
549 float xComponentValue() const;
550
555 void setColorFromPoint( QPoint point );
556
557};
558
559
567class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
568{
569 Q_OBJECT
570
571 public:
572
577 {
578 Horizontal = 0,
579 Vertical
580 };
581
588 QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr,
589 ColorComponent component = QgsColorWidget::Red,
590 Orientation orientation = QgsColorRampWidget::Horizontal );
591
592 QSize sizeHint() const override;
593 void paintEvent( QPaintEvent *event ) override;
594
600 void setOrientation( Orientation orientation );
601
607 Orientation orientation() const { return mOrientation; }
608
614 void setInteriorMargin( int margin );
615
621 int interiorMargin() const { return mMargin; }
622
628 void setShowFrame( bool showFrame );
629
635 bool showFrame() const { return mShowFrame; }
636
641 void setMarkerSize( int markerSize );
642
643 signals:
644
650 Q_DECL_DEPRECATED void valueChanged( int value ) SIP_DEPRECATED;
651
657 void valueChangedF( float value );
658
659 protected:
660
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
669 bool mIsDragging = false;
670
671 /*Orientation for ramp*/
672 Orientation mOrientation;
673
674 /*Margin around ramp*/
675 int mMargin = 4;
676
677 /*Whether to draw a frame around the ramp*/
678 bool mShowFrame = false;
679
680 /*Polygon for upper triangle marker*/
681 QPolygonF mTopTriangle;
682
683 /*Polygon for lower triangle marker*/
684 QPolygonF mBottomTriangle;
685
690 void setColorFromPoint( QPointF point );
691
692 friend class TestQgsCompoundColorWidget;
693};
694
695
702class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
703{
704 Q_OBJECT
705
706 public:
707
713 QgsColorSliderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red );
714
715 void setComponent( ColorComponent component ) override;
716 void setComponentValueF( float value ) override;
717 void setColor( const QColor &color, bool emitSignals = false ) override;
718
719 private:
720
721 /*Color ramp widget*/
722 QgsColorRampWidget *mRampWidget = nullptr;
723
724 /*Spin box widget*/
725 QgsDoubleSpinBox *mSpinBox = nullptr;
726
734 float convertRealToDisplay( float realValue ) const;
735
742 float convertDisplayToReal( float displayValue ) const;
743
744 private slots:
745
749 void rampColorChanged( const QColor &color );
750
754 void spinChanged( double value );
755
759 void rampChanged( float value );
760
761 friend class TestQgsCompoundColorWidget;
762};
763
764
772class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
773{
774 Q_OBJECT
775
776 public:
777
782 {
783 HexRgb = 0,
786 Rgba
787 };
788 Q_ENUM( ColorTextFormat )
789
790
794 QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
795
796 void setColor( const QColor &color, bool emitSignals = false ) override;
797
803 void setAllowOpacity( bool allowOpacity );
804
805 protected:
806 void resizeEvent( QResizeEvent *event ) override;
807
808 private:
809
810 QLineEdit *mLineEdit = nullptr;
811
812 /*Drop-down menu button*/
813 QToolButton *mMenuButton = nullptr;
814
815 /*Display format for colors*/
816 ColorTextFormat mFormat = QgsColorTextWidget::HexRgb;
817
818 bool mAllowAlpha = true;
819
823 void updateText();
824
825 private slots:
826
830 void textChanged();
831
835 void showMenu();
836};
837
838
845class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
846{
847 Q_OBJECT
848
849 public:
850
855 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
856
857 void paintEvent( QPaintEvent *event ) override;
858 QSize sizeHint() const override;
859
867 QColor color2() const { return mColor2; }
868
869 public slots:
870
878 virtual void setColor2( const QColor &color );
879
880 protected:
881
882 //reimplemented to allow dragging colors
883 void mousePressEvent( QMouseEvent *e ) override;
884
885 //reimplemented to click colors
886 void mouseReleaseEvent( QMouseEvent *e ) override;
887
888 //reimplemented to allow dragging colors
889 void mouseMoveEvent( QMouseEvent *e ) override;
890
891 private:
892
893 /*Secondary color for widget*/
894 QColor mColor2;
895
896 QPoint mDragStartPosition;
897
898 /*Draws a color preview within the specified rect.
899 * \param color color to draw
900 * \param rect rect to draw color in
901 * \param painter destination painter
902 */
903 void drawColor( const QColor &color, QRect rect, QPainter &painter );
904};
905
906#endif // #ifndef QGSCOLORWIDGETS_H
A color box widget.
A preview box which displays one or two colors as swatches.
QColor color2() const
Returns the secondary color for the widget.
A color ramp widget.
Q_DECL_DEPRECATED void valueChanged(int value)
Emitted when the widget's color component value changes.
int interiorMargin() const
Fetches the margin between the edge of the widget and the ramp.
Orientation orientation() const
Fetches the orientation for the color ramp.
void valueChangedF(float value)
Emitted when the widget's color component value changes.
Orientation
Specifies the orientation of a color ramp.
@ Horizontal
Horizontal ramp.
bool showFrame() const
Fetches whether the ramp is drawn within a frame.
A composite horizontal color ramp widget and associated spinbox for manual value entry.
A line edit widget which displays colors as text and accepts string representations of colors.
ColorTextFormat
Specifies the display format for a color.
@ Rgb
Rgb( r, g, b ) format.
@ HexRgbA
#RRGGBBAA in hexadecimal, with alpha
@ HexRgb
#RRGGBB in hexadecimal
A color wheel widget.
~QgsColorWheel() override
An action containing a color widget, which can be embedded into a menu.
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.
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.
ColorComponent component() const
Returns the color component which the widget controls.
ComponentUnit
Specified the color component unit.
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.
@ Cyan
Cyan component (based on CMYK model) of color.
@ Blue
Blue component of color.
@ Value
Value component of color (based on HSV model)
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:106
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53