QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
24 class QColor;
25 class QSpinBox;
26 class QLineEdit;
27 class QToolButton;
28 
39 class GUI_EXPORT QgsColorWidget : public QWidget
40 {
41  Q_OBJECT
42 
43  public:
44 
49  {
50  Multiple = 0,
51  Red,
53  Blue,
54  Hue,
57  Alpha
58  };
59 
65  QgsColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Multiple );
66 
72  QColor color() const;
73 
79  ColorComponent component() const { return mComponent; }
80 
88  int componentValue() const;
89 
94  static QPixmap createDragIcon( const QColor &color );
95 
96  public slots:
97 
104  virtual void setColor( const QColor &color, bool emitSignals = false );
105 
111  virtual void setComponent( QgsColorWidget::ColorComponent component );
112 
122  virtual void setComponentValue( int value );
123 
124  signals:
125 
130  void colorChanged( const QColor &color );
131 
136  void hovered();
137 
138  protected:
139 
141 
143 
148  int mExplicitHue = 0;
149 
154  int componentRange() const;
155 
160  int componentRange( ColorComponent component ) const;
161 
169  int componentValue( ColorComponent component ) const;
170 
176  int hue() const;
177 
185  void alterColor( QColor &color, QgsColorWidget::ColorComponent component, int newValue ) const;
186 
191  static const QPixmap &transparentBackground();
192 
193  //Reimplemented to accept dragged colors
194  void dragEnterEvent( QDragEnterEvent *e ) override;
195 
196  //Reimplemented to accept dropped colors
197  void dropEvent( QDropEvent *e ) override;
198 
199  void mouseMoveEvent( QMouseEvent *e ) override;
200  void mousePressEvent( QMouseEvent *e ) override;
201  void mouseReleaseEvent( QMouseEvent *e ) override;
202 };
203 
204 
213 class GUI_EXPORT QgsColorWidgetAction: public QWidgetAction
214 {
215  Q_OBJECT
216 
217  public:
218 
225  QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
226 
230  QgsColorWidget *colorWidget() { return mColorWidget; }
231 
240  void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
241 
247  bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
248 
249  signals:
250 
255  void colorChanged( const QColor &color );
256 
257  private:
258  QMenu *mMenu = nullptr;
259  QgsColorWidget *mColorWidget = nullptr;
260 
261  //used to suppress recursion with hover events
262  bool mSuppressRecurse;
263 
264  bool mDismissOnColorSelection;
265 
266  private slots:
267 
271  void onHover();
272 
276  void setColor( const QColor &color );
277 };
278 
279 
280 
289 class GUI_EXPORT QgsColorWheel : public QgsColorWidget
290 {
291  Q_OBJECT
292 
293  public:
294 
299  QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
300 
301  ~QgsColorWheel() override;
302 
303  QSize sizeHint() const override;
304  void paintEvent( QPaintEvent *event ) override;
305 
306  public slots:
307 
308  void setColor( const QColor &color, bool emitSignals = false ) override;
309 
310  protected:
311 
312  void resizeEvent( QResizeEvent *event ) override;
313  void mouseMoveEvent( QMouseEvent *event ) override;
314  void mousePressEvent( QMouseEvent *event ) override;
315  void mouseReleaseEvent( QMouseEvent *event ) override;
316 
317  private:
318 
319  enum ControlPart
320  {
321  None,
322  Wheel,
323  Triangle
324  };
325 
326  bool mIsDragging = false;
327 
328  /*Margin between outer ring and edge of widget*/
329  int mMargin = 4;
330 
331  /*Thickness of hue ring in pixels*/
332  int mWheelThickness = 18;
333 
334  /*Part of the wheel where the mouse was originally depressed*/
335  ControlPart mClickedPart = QgsColorWheel::None;
336 
337  /*Cached image of hue wheel*/
338  QImage *mWheelImage = nullptr;
339 
340  /*Cached image of inner triangle*/
341  QImage *mTriangleImage = nullptr;
342 
343  /*Resuable, temporary image for drawing widget*/
344  QImage *mWidgetImage = nullptr;
345 
346  /*Whether the color wheel image requires redrawing*/
347  bool mWheelDirty = true;
348 
349  /*Whether the inner triangle image requires redrawing*/
350  bool mTriangleDirty = true;
351 
352  /*Conical gradient brush used for drawing hue wheel*/
353  QBrush mWheelBrush;
354 
359  void createImages( QSizeF size );
360 
362  void createWheel();
363 
365  void createTriangle();
366 
371  void setColorFromPos( QPointF pos );
372 
373 };
374 
375 
385 class GUI_EXPORT QgsColorBox : public QgsColorWidget
386 {
387  Q_OBJECT
388 
389  public:
390 
398  QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
399 
400  ~QgsColorBox() override;
401 
402  QSize sizeHint() const override;
403  void paintEvent( QPaintEvent *event ) override;
404 
405  void setComponent( ColorComponent component ) override;
406 
407  public slots:
408  void setColor( const QColor &color, bool emitSignals = false ) override;
409 
410  protected:
411 
412  void resizeEvent( QResizeEvent *event ) override;
413  void mouseMoveEvent( QMouseEvent *event ) override;
414  void mousePressEvent( QMouseEvent *event ) override;
415  void mouseReleaseEvent( QMouseEvent *event ) override;
416 
417  private:
418 
419  bool mIsDragging = false;
420 
421  /*Margin between outer ring and edge of widget*/
422  int mMargin = 2;
423 
424  /*Cached image for color box*/
425  QImage *mBoxImage = nullptr;
426 
427  /*Whether the cached image requires redrawing*/
428  bool mDirty = true;
429 
433  void createBox();
434 
439  int valueRangeX() const;
440 
445  int valueRangeY() const;
446 
450  QgsColorWidget::ColorComponent yComponent() const;
451 
455  int yComponentValue() const;
456 
460  QgsColorWidget::ColorComponent xComponent() const;
461 
465  int xComponentValue() const;
466 
471  void setColorFromPoint( QPoint point );
472 
473 };
474 
475 
484 class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
485 {
486  Q_OBJECT
487 
488  public:
489 
494  {
495  Horizontal = 0,
496  Vertical
497  };
498 
505  QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr,
506  ColorComponent component = QgsColorWidget::Red,
507  Orientation orientation = QgsColorRampWidget::Horizontal );
508 
509  QSize sizeHint() const override;
510  void paintEvent( QPaintEvent *event ) override;
511 
517  void setOrientation( Orientation orientation );
518 
524  Orientation orientation() const { return mOrientation; }
525 
531  void setInteriorMargin( int margin );
532 
538  int interiorMargin() const { return mMargin; }
539 
545  void setShowFrame( bool showFrame );
546 
552  bool showFrame() const { return mShowFrame; }
553 
558  void setMarkerSize( int markerSize );
559 
560  signals:
561 
566  void valueChanged( int value );
567 
568  protected:
569 
570  void mouseMoveEvent( QMouseEvent *event ) override;
571  void wheelEvent( QWheelEvent *event ) override;
572  void mousePressEvent( QMouseEvent *event ) override;
573  void mouseReleaseEvent( QMouseEvent *event ) override;
574  void keyPressEvent( QKeyEvent *event ) override;
575 
576  private:
577 
578  bool mIsDragging = false;
579 
580  /*Orientation for ramp*/
581  Orientation mOrientation;
582 
583  /*Margin around ramp*/
584  int mMargin = 4;
585 
586  /*Whether to draw a frame around the ramp*/
587  bool mShowFrame = false;
588 
589  /*Polygon for upper triangle marker*/
590  QPolygonF mTopTriangle;
591 
592  /*Polygon for lower triangle marker*/
593  QPolygonF mBottomTriangle;
594 
599  void setColorFromPoint( QPointF point );
600 
601 };
602 
603 
611 class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
612 {
613  Q_OBJECT
614 
615  public:
616 
622  QgsColorSliderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red );
623 
624  void setComponent( ColorComponent component ) override;
625  void setComponentValue( int value ) override;
626  void setColor( const QColor &color, bool emitSignals = false ) override;
627 
628  private:
629 
630  /*Color ramp widget*/
631  QgsColorRampWidget *mRampWidget = nullptr;
632 
633  /*Spin box widget*/
634  QSpinBox *mSpinBox = nullptr;
635 
643  int convertRealToDisplay( int realValue ) const;
644 
651  int convertDisplayToReal( int displayValue ) const;
652 
653  private slots:
654 
658  void rampColorChanged( const QColor &color );
659 
663  void spinChanged( int value );
664 
668  void rampChanged( int value );
669 
670 };
671 
672 
681 class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
682 {
683  Q_OBJECT
684 
685  public:
686 
691  {
692  HexRgb = 0,
694  Rgb,
695  Rgba
696  };
697  Q_ENUM( ColorTextFormat )
698 
699 
703  QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
704 
705  void setColor( const QColor &color, bool emitSignals = false ) override;
706 
707  protected:
708  void resizeEvent( QResizeEvent *event ) override;
709 
710  private:
711 
712  QLineEdit *mLineEdit = nullptr;
713 
714  /*Drop-down menu button*/
715  QToolButton *mMenuButton = nullptr;
716 
717  /*Display format for colors*/
718  ColorTextFormat mFormat = QgsColorTextWidget::HexRgb;
719 
723  void updateText();
724 
725  private slots:
726 
730  void textChanged();
731 
735  void showMenu();
736 };
737 
738 
746 class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
747 {
748  Q_OBJECT
749 
750  public:
751 
756  QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
757 
758  void paintEvent( QPaintEvent *event ) override;
759  QSize sizeHint() const override;
760 
768  QColor color2() const { return mColor2; }
769 
770  public slots:
771 
779  virtual void setColor2( const QColor &color );
780 
781  protected:
782 
783  //reimplemented to allow dragging colors
784  void mousePressEvent( QMouseEvent *e ) override;
785 
786  //reimplemented to click colors
787  void mouseReleaseEvent( QMouseEvent *e ) override;
788 
789  //reimplemented to allow dragging colors
790  void mouseMoveEvent( QMouseEvent *e ) override;
791 
792  private:
793 
794  /*Secondary color for widget*/
795  QColor mColor2;
796 
797  QPoint mDragStartPosition;
798 
799  /*Draws a color preview within the specified rect.
800  * \param color color to draw
801  * \param rect rect to draw color in
802  * \param painter destination painter
803  */
804  void drawColor( const QColor &color, QRect rect, QPainter &painter );
805 };
806 
807 #endif // #ifndef QGSCOLORWIDGETS_H
QgsColorWidget::Value
@ Value
Value component of color (based on HSV model)
Definition: qgscolorwidgets.h:56
QgsColorTextWidget::Rgb
@ Rgb
Rgb( r, g, b ) format.
Definition: qgscolorwidgets.h:694
QgsColorBox
A color box widget. This widget consists of a two dimensional rectangle filled with color variations,...
Definition: qgscolorwidgets.h:385
QgsColorRampWidget::Horizontal
@ Horizontal
Horizontal ramp.
Definition: qgscolorwidgets.h:495
QgsColorRampWidget::interiorMargin
int interiorMargin() const
Fetches the margin between the edge of the widget and the ramp.
Definition: qgscolorwidgets.h:538
QgsColorWidget::mComponent
ColorComponent mComponent
Definition: qgscolorwidgets.h:142
Direction::None
@ None
QgsColorWidget::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *e) override
Definition: qgscolorwidgets.cpp:243
QgsColorRampWidget
A color ramp widget. This widget consists of an interactive box filled with a color which varies alon...
Definition: qgscolorwidgets.h:484
QgsColorWidget::setComponent
virtual void setComponent(QgsColorWidget::ColorComponent component)
Sets the color component which the widget controls.
Definition: qgscolorwidgets.cpp:254
QgsColorWidget::setComponentValue
virtual void setComponentValue(int value)
Alters the widget's color by setting the value for the widget's color component.
Definition: qgscolorwidgets.cpp:265
QgsColorWidget::mousePressEvent
void mousePressEvent(QMouseEvent *e) override
Definition: qgscolorwidgets.cpp:237
QgsColorRampWidget::orientation
Orientation orientation() const
Fetches the orientation for the color ramp.
Definition: qgscolorwidgets.h:524
QgsColorWidget::Green
@ Green
Green component of color.
Definition: qgscolorwidgets.h:52
QgsColorWidget::setColor
virtual void setColor(const QColor &color, bool emitSignals=false)
Sets the color for the widget.
Definition: qgscolorwidgets.cpp:347
QgsColorWidgetAction::setDismissOnColorSelection
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action'...
Definition: qgscolorwidgets.h:240
QgsColorTextWidget::HexRgb
@ HexRgb
#RRGGBB in hexadecimal
Definition: qgscolorwidgets.h:692
QgsColorWidget::Red
@ Red
Red component of color.
Definition: qgscolorwidgets.h:51
QgsColorWidget::mCurrentColor
QColor mCurrentColor
Definition: qgscolorwidgets.h:140
QgsColorWheel
A color wheel widget. This widget consists of an outer ring which allows for hue selection,...
Definition: qgscolorwidgets.h:289
QgsColorWidget::component
ColorComponent component() const
Returns the color component which the widget controls.
Definition: qgscolorwidgets.h:79
QgsColorWidget
A base class for interactive color widgets. Widgets can either allow setting a single component of a ...
Definition: qgscolorwidgets.h:39
qgis_sip.h
QgsColorPreviewWidget
A preview box which displays one or two colors as swatches.
Definition: qgscolorwidgets.h:746
QgsColorWidgetAction
An action containing a color widget, which can be embedded into a menu.
Definition: qgscolorwidgets.h:213
QgsColorRampWidget::showFrame
bool showFrame() const
Fetches whether the ramp is drawn within a frame.
Definition: qgscolorwidgets.h:552
QgsColorWidgetAction::dismissOnColorSelection
bool dismissOnColorSelection() const
Returns whether the parent menu will be dismissed after a color is selected from the action's color w...
Definition: qgscolorwidgets.h:247
QgsColorRampWidget::Orientation
Orientation
Specifies the orientation of a color ramp.
Definition: qgscolorwidgets.h:493
QgsColorWidget::Blue
@ Blue
Blue component of color.
Definition: qgscolorwidgets.h:53
QgsColorWidget::Hue
@ Hue
Hue component of color (based on HSV model)
Definition: qgscolorwidgets.h:54
QgsColorTextWidget::HexRgbA
@ HexRgbA
#RRGGBBAA in hexadecimal, with alpha
Definition: qgscolorwidgets.h:693
QgsColorWidget::ColorComponent
ColorComponent
Specifies the color component which the widget alters.
Definition: qgscolorwidgets.h:48
QgsColorWidget::Saturation
@ Saturation
Saturation component of color (based on HSV model)
Definition: qgscolorwidgets.h:55
QgsColorWidgetAction::colorWidget
QgsColorWidget * colorWidget()
Returns the color widget contained in the widget action.
Definition: qgscolorwidgets.h:230
QgsColorTextWidget::ColorTextFormat
ColorTextFormat
Specifies the display format for a color.
Definition: qgscolorwidgets.h:690
QgsColorSliderWidget
A composite horizontal color ramp widget and associated spinbox for manual value entry.
Definition: qgscolorwidgets.h:611
QgsColorWidget::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *e) override
Definition: qgscolorwidgets.cpp:230
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsColorTextWidget
A line edit widget which displays colors as text and accepts string representations of colors.
Definition: qgscolorwidgets.h:681
QgsWms::QgsColorBox
QList< QPair< QRgb, int > > QgsColorBox
Definition: qgsmediancut.cpp:48
QgsColorPreviewWidget::color2
QColor color2() const
Returns the secondary color for the widget.
Definition: qgscolorwidgets.h:768