QGIS API Documentation 3.41.0-Master (cea29feecf2)
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:
61
65 enum class ComponentUnit
66 {
67 Scaled0to255,
68 Percent,
69 Degree
70 };
71 Q_ENUM( ComponentUnit )
72
73
78 QgsColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Multiple );
79
85 QColor color() const;
86
92 ColorComponent component() const { return mComponent; }
93
102 Q_DECL_DEPRECATED int componentValue() const SIP_DEPRECATED;
103
112 float componentValueF() const;
113
118 static QPixmap createDragIcon( const QColor &color );
119
123 static ComponentUnit componentUnit( ColorComponent component );
124
125
126 public slots:
127
134 virtual void setColor( const QColor &color, bool emitSignals = false );
135
141 virtual void setComponent( QgsColorWidget::ColorComponent component );
142
153 Q_DECL_DEPRECATED virtual void setComponentValue( int value ) SIP_DEPRECATED;
154
165 virtual void setComponentValueF( float value );
166
167 signals:
168
173 void colorChanged( const QColor &color );
174
178 void hovered();
179
180 protected:
181 QColor mCurrentColor;
182
183 ColorComponent mComponent;
184
189 float mExplicitHue = 0;
190
195 int componentRange() const;
196
201 static int componentRange( ColorComponent component );
202
212 Q_DECL_DEPRECATED int componentValue( ColorComponent component ) const SIP_DEPRECATED;
213
222 float componentValueF( ColorComponent component ) const;
223
230 Q_DECL_DEPRECATED int hue() const SIP_DEPRECATED;
231
238 float hueF() const;
239
248 Q_DECL_DEPRECATED static void alterColor( QColor &color, QgsColorWidget::ColorComponent component, int newValue ) SIP_DEPRECATED;
249
258 static void alterColorF( QColor &color, QgsColorWidget::ColorComponent component, float newValue );
259
263 QColor::Spec colorSpec() const;
264
268 static QColor::Spec colorSpec( QgsColorWidget::ColorComponent component );
269
274 static const QPixmap &transparentBackground();
275
276 //Reimplemented to accept dragged colors
277 void dragEnterEvent( QDragEnterEvent *e ) override;
278
279 //Reimplemented to accept dropped colors
280 void dropEvent( QDropEvent *e ) override;
281
282 void mouseMoveEvent( QMouseEvent *e ) override;
283 void mousePressEvent( QMouseEvent *e ) override;
284 void mouseReleaseEvent( QMouseEvent *e ) override;
285
286 friend class TestQgsCompoundColorWidget;
287};
288
289
297class GUI_EXPORT QgsColorWidgetAction : public QWidgetAction
298{
299 Q_OBJECT
300
301 public:
308 QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
309
313 QgsColorWidget *colorWidget() { return mColorWidget; }
314
323 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
324
330 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
331
332 signals:
333
338 void colorChanged( const QColor &color );
339
340 private:
341 QMenu *mMenu = nullptr;
342 QgsColorWidget *mColorWidget = nullptr;
343
344 //used to suppress recursion with hover events
345 bool mSuppressRecurse;
346
347 bool mDismissOnColorSelection;
348
349 private slots:
350
354 void onHover();
355
359 void setColor( const QColor &color );
360};
361
362
370class GUI_EXPORT QgsColorWheel : public QgsColorWidget
371{
372 Q_OBJECT
373
374 public:
379 QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
380
381 ~QgsColorWheel() override;
382
383 QSize sizeHint() const override;
384 void paintEvent( QPaintEvent *event ) override;
385
386 public slots:
387
388 void setColor( const QColor &color, bool emitSignals = false ) override;
389
390 protected:
391 void resizeEvent( QResizeEvent *event ) override;
392 void mouseMoveEvent( QMouseEvent *event ) override;
393 void mousePressEvent( QMouseEvent *event ) override;
394 void mouseReleaseEvent( QMouseEvent *event ) override;
395
396 private:
397 enum ControlPart
398 {
399 None,
400 Wheel,
401 Triangle
402 };
403
404 bool mIsDragging = false;
405
406 /*Margin between outer ring and edge of widget*/
407 int mMargin = 4;
408
409 /*Thickness of hue ring in pixels*/
410 int mWheelThickness = 18;
411
412 /*Part of the wheel where the mouse was originally depressed*/
413 ControlPart mClickedPart = QgsColorWheel::None;
414
415 /*Cached image of hue wheel*/
416 QImage mWheelImage;
417
418 /*Cached image of inner triangle*/
419 QImage mTriangleImage;
420
421 /*Resuable, temporary image for drawing widget*/
422 QImage mWidgetImage;
423
424 /*Whether the color wheel image requires redrawing*/
425 bool mWheelDirty = true;
426
427 /*Whether the inner triangle image requires redrawing*/
428 bool mTriangleDirty = true;
429
430 /*Conical gradient brush used for drawing hue wheel*/
431 QBrush mWheelBrush;
432
437 void createImages( QSizeF size );
438
440 void createWheel();
441
443 void createTriangle();
444
449 void setColorFromPos( QPointF pos );
450};
451
452
461class GUI_EXPORT QgsColorBox : public QgsColorWidget
462{
463 Q_OBJECT
464
465 public:
473 QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
474
475 ~QgsColorBox() override;
476
477 QSize sizeHint() const override;
478 void paintEvent( QPaintEvent *event ) override;
479
480 void setComponent( ColorComponent component ) override;
481
482 public slots:
483 void setColor( const QColor &color, bool emitSignals = false ) override;
484
485 protected:
486 void resizeEvent( QResizeEvent *event ) override;
487 void mouseMoveEvent( QMouseEvent *event ) override;
488 void mousePressEvent( QMouseEvent *event ) override;
489 void mouseReleaseEvent( QMouseEvent *event ) override;
490
491 private:
492 bool mIsDragging = false;
493
494 /*Margin between outer ring and edge of widget*/
495 static constexpr float mMargin = 2.;
496
497 /*Cached image for color box*/
498 QImage *mBoxImage = nullptr;
499
500 /*Whether the cached image requires redrawing*/
501 bool mDirty = true;
502
506 void createBox();
507
512 float valueRangeX() const;
513
518 float valueRangeY() const;
519
523 QgsColorWidget::ColorComponent yComponent() const;
524
528 float yComponentValue() const;
529
533 QgsColorWidget::ColorComponent xComponent() const;
534
538 float xComponentValue() const;
539
544 void setColorFromPoint( QPoint point );
545};
546
547
555class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
556{
557 Q_OBJECT
558
559 public:
564 {
565 Horizontal = 0,
566 Vertical
567 };
568
575 QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red, Orientation orientation = QgsColorRampWidget::Horizontal );
576
577 QSize sizeHint() const override;
578 void paintEvent( QPaintEvent *event ) override;
579
585 void setOrientation( Orientation orientation );
586
592 Orientation orientation() const { return mOrientation; }
593
599 void setInteriorMargin( int margin );
600
606 int interiorMargin() const { return mMargin; }
607
613 void setShowFrame( bool showFrame );
614
620 bool showFrame() const { return mShowFrame; }
621
626 void setMarkerSize( int markerSize );
627
628 signals:
629
635 Q_DECL_DEPRECATED void valueChanged( int value ) SIP_DEPRECATED;
636
642 void valueChangedF( float value );
643
644 protected:
645 void mouseMoveEvent( QMouseEvent *event ) override;
646 void wheelEvent( QWheelEvent *event ) override;
647 void mousePressEvent( QMouseEvent *event ) override;
648 void mouseReleaseEvent( QMouseEvent *event ) override;
649 void keyPressEvent( QKeyEvent *event ) override;
650
651 private:
652 bool mIsDragging = false;
653
654 /*Orientation for ramp*/
655 Orientation mOrientation;
656
657 /*Margin around ramp*/
658 int mMargin = 4;
659
660 /*Whether to draw a frame around the ramp*/
661 bool mShowFrame = false;
662
663 /*Polygon for upper triangle marker*/
664 QPolygonF mTopTriangle;
665
666 /*Polygon for lower triangle marker*/
667 QPolygonF mBottomTriangle;
668
673 void setColorFromPoint( QPointF point );
674
675 friend class TestQgsCompoundColorWidget;
676};
677
678
685class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
686{
687 Q_OBJECT
688
689 public:
695 QgsColorSliderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red );
696
697 void setComponent( ColorComponent component ) override;
698 void setComponentValueF( float value ) override;
699 void setColor( const QColor &color, bool emitSignals = false ) override;
700
701 private:
702 /*Color ramp widget*/
703 QgsColorRampWidget *mRampWidget = nullptr;
704
705 /*Spin box widget*/
706 QgsDoubleSpinBox *mSpinBox = nullptr;
707
715 float convertRealToDisplay( float realValue ) const;
716
723 float convertDisplayToReal( float displayValue ) const;
724
725 private slots:
726
730 void rampColorChanged( const QColor &color );
731
735 void spinChanged( double value );
736
740 void rampChanged( float value );
741
742 friend class TestQgsCompoundColorWidget;
743};
744
745
753class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
754{
755 Q_OBJECT
756
757 public:
762 {
763 HexRgb = 0,
766 Rgba
767 };
768 Q_ENUM( ColorTextFormat )
769
770
774 QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
775
776 void setColor( const QColor &color, bool emitSignals = false ) override;
777
783 void setAllowOpacity( bool allowOpacity );
784
785 protected:
786 void resizeEvent( QResizeEvent *event ) override;
787
788 private:
789 QLineEdit *mLineEdit = nullptr;
790
791 /*Drop-down menu button*/
792 QToolButton *mMenuButton = nullptr;
793
794 /*Display format for colors*/
795 ColorTextFormat mFormat = QgsColorTextWidget::HexRgb;
796
797 bool mAllowAlpha = true;
798
802 void updateText();
803
804 private slots:
805
809 void textChanged();
810
814 void showMenu();
815};
816
817
824class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
825{
826 Q_OBJECT
827
828 public:
833 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
834
835 void paintEvent( QPaintEvent *event ) override;
836 QSize sizeHint() const override;
837
845 QColor color2() const { return mColor2; }
846
847 public slots:
848
856 virtual void setColor2( const QColor &color );
857
858 protected:
859 //reimplemented to allow dragging colors
860 void mousePressEvent( QMouseEvent *e ) override;
861
862 //reimplemented to click colors
863 void mouseReleaseEvent( QMouseEvent *e ) override;
864
865 //reimplemented to allow dragging colors
866 void mouseMoveEvent( QMouseEvent *e ) override;
867
868 private:
869 /*Secondary color for widget*/
870 QColor mColor2;
871
872 QPoint mDragStartPosition;
873
874 /*Draws a color preview within the specified rect.
875 * \param color color to draw
876 * \param rect rect to draw color in
877 * \param painter destination painter
878 */
879 void drawColor( const QColor &color, QRect rect, QPainter &painter );
880};
881
882#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