QGIS API Documentation  3.0.2-Girona (307d082)
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.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, const 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, const bool emitSignals = false );
105 
111  virtual void setComponent( const QgsColorWidget::ColorComponent component );
112 
122  virtual void setComponentValue( const 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( const ColorComponent component ) const;
161 
169  int componentValue( const ColorComponent component ) const;
170 
176  int hue() const;
177 
185  void alterColor( QColor &color, const QgsColorWidget::ColorComponent component, const 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, const 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  /*Margin between outer ring and edge of widget*/
327  int mMargin = 4;
328 
329  /*Thickness of hue ring in pixels*/
330  int mWheelThickness = 18;
331 
332  /*Part of the wheel where the mouse was originally depressed*/
333  ControlPart mClickedPart = QgsColorWheel::None;
334 
335  /*Cached image of hue wheel*/
336  QImage *mWheelImage = nullptr;
337 
338  /*Cached image of inner triangle*/
339  QImage *mTriangleImage = nullptr;
340 
341  /*Resuable, temporary image for drawing widget*/
342  QImage *mWidgetImage = nullptr;
343 
344  /*Whether the color wheel image requires redrawing*/
345  bool mWheelDirty = true;
346 
347  /*Whether the inner triangle image requires redrawing*/
348  bool mTriangleDirty = true;
349 
350  /*Conical gradient brush used for drawing hue wheel*/
351  QBrush mWheelBrush;
352 
357  void createImages( const QSizeF size );
358 
360  void createWheel();
361 
363  void createTriangle();
364 
369  void setColorFromPos( const QPointF pos );
370 
371 };
372 
373 
383 class GUI_EXPORT QgsColorBox : public QgsColorWidget
384 {
385  Q_OBJECT
386 
387  public:
388 
396  QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, const ColorComponent component = Value );
397 
398  ~QgsColorBox() override;
399 
400  QSize sizeHint() const override;
401  void paintEvent( QPaintEvent *event ) override;
402 
403  void setComponent( const ColorComponent component ) override;
404 
405  public slots:
406  void setColor( const QColor &color, const bool emitSignals = false ) override;
407 
408  protected:
409 
410  void resizeEvent( QResizeEvent *event ) override;
411  void mouseMoveEvent( QMouseEvent *event ) override;
412  void mousePressEvent( QMouseEvent *event ) override;
413 
414  private:
415 
416  /*Margin between outer ring and edge of widget*/
417  int mMargin = 2;
418 
419  /*Cached image for color box*/
420  QImage *mBoxImage = nullptr;
421 
422  /*Whether the cached image requires redrawing*/
423  bool mDirty = true;
424 
428  void createBox();
429 
434  int valueRangeX() const;
435 
440  int valueRangeY() const;
441 
445  QgsColorWidget::ColorComponent yComponent() const;
446 
450  int yComponentValue() const;
451 
455  QgsColorWidget::ColorComponent xComponent() const;
456 
460  int xComponentValue() const;
461 
466  void setColorFromPoint( QPoint point );
467 
468 };
469 
470 
479 class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
480 {
481  Q_OBJECT
482 
483  public:
484 
489  {
490  Horizontal = 0,
491  Vertical
492  };
493 
500  QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr,
501  const ColorComponent component = QgsColorWidget::Red,
502  const Orientation orientation = QgsColorRampWidget::Horizontal );
503 
504  QSize sizeHint() const override;
505  void paintEvent( QPaintEvent *event ) override;
506 
512  void setOrientation( const Orientation orientation );
513 
519  Orientation orientation() const { return mOrientation; }
520 
526  void setInteriorMargin( const int margin );
527 
533  int interiorMargin() const { return mMargin; }
534 
540  void setShowFrame( const bool showFrame );
541 
547  bool showFrame() const { return mShowFrame; }
548 
553  void setMarkerSize( const int markerSize );
554 
555  signals:
556 
561  void valueChanged( const int value );
562 
563  protected:
564 
565  void mouseMoveEvent( QMouseEvent *event ) override;
566  void wheelEvent( QWheelEvent *event ) override;
567  void mousePressEvent( QMouseEvent *event ) override;
568  void keyPressEvent( QKeyEvent *event ) override;
569 
570  private:
571 
572  /*Orientation for ramp*/
573  Orientation mOrientation;
574 
575  /*Margin around ramp*/
576  int mMargin = 4;
577 
578  /*Whether to draw a frame around the ramp*/
579  bool mShowFrame = false;
580 
581  /*Polygon for upper triangle marker*/
582  QPolygonF mTopTriangle;
583 
584  /*Polygon for lower triangle marker*/
585  QPolygonF mBottomTriangle;
586 
591  void setColorFromPoint( QPointF point );
592 
593 };
594 
595 
603 class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
604 {
605  Q_OBJECT
606 
607  public:
608 
614  QgsColorSliderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, const ColorComponent component = QgsColorWidget::Red );
615 
616  void setComponent( const ColorComponent component ) override;
617  void setComponentValue( const int value ) override;
618  void setColor( const QColor &color, const bool emitSignals = false ) override;
619 
620  private:
621 
622  /*Color ramp widget*/
623  QgsColorRampWidget *mRampWidget = nullptr;
624 
625  /*Spin box widget*/
626  QSpinBox *mSpinBox = nullptr;
627 
635  int convertRealToDisplay( const int realValue ) const;
636 
643  int convertDisplayToReal( const int displayValue ) const;
644 
645  private slots:
646 
650  void rampColorChanged( const QColor &color );
651 
655  void spinChanged( int value );
656 
660  void rampChanged( int value );
661 
662 };
663 
664 
673 class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
674 {
675  Q_OBJECT
676 
677  public:
678 
683  {
684  HexRgb = 0,
686  Rgb,
687  Rgba
688  };
689  Q_ENUM( ColorTextFormat )
690 
691 
695  QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
696 
697  void setColor( const QColor &color, const bool emitSignals = false ) override;
698 
699  protected:
700  void resizeEvent( QResizeEvent *event ) override;
701 
702  private:
703 
704  QLineEdit *mLineEdit = nullptr;
705 
706  /*Drop-down menu button*/
707  QToolButton *mMenuButton = nullptr;
708 
709  /*Display format for colors*/
711 
715  void updateText();
716 
717  private slots:
718 
722  void textChanged();
723 
727  void showMenu();
728 };
729 
730 
738 class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
739 {
740  Q_OBJECT
741 
742  public:
743 
748  QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
749 
750  void paintEvent( QPaintEvent *event ) override;
751  QSize sizeHint() const override;
752 
760  QColor color2() const { return mColor2; }
761 
762  public slots:
763 
771  virtual void setColor2( const QColor &color );
772 
773  protected:
774 
775  //reimplemented to allow dragging colors
776  void mousePressEvent( QMouseEvent *e ) override;
777 
778  //reimplemented to click colors
779  void mouseReleaseEvent( QMouseEvent *e ) override;
780 
781  //reimplemented to allow dragging colors
782  void mouseMoveEvent( QMouseEvent *e ) override;
783 
784  private:
785 
786  /*Secondary color for widget*/
787  QColor mColor2;
788 
789  QPoint mDragStartPosition;
790 
791  /*Draws a color preview within the specified rect.
792  * \param color color to draw
793  * \param rect rect to draw color in
794  * \param painter destination painter
795  */
796  void drawColor( const QColor &color, QRect rect, QPainter &painter );
797 };
798 
799 #endif // #ifndef QGSCOLORWIDGETS_H
bool showFrame() const
Fetches whether the ramp is drawn within a frame.
ColorTextFormat
Specifies the display format for a color.
A base class for interactive color widgets.
int interiorMargin() const
Fetches the margin between the edge of the widget and the ramp.
Orientation
Specifies the orientation of a color ramp.
Value component of color (based on HSV model)
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Rgb( r, g, b ) format.
ColorComponent component() const
Returns the color component which the widget controls.
void mousePressEvent(QMouseEvent *e) override
QColor color2() const
Returns the secondary color for the widget.
Green component of color.
Red component of color.
A composite horizontal color ramp widget and associated spinbox for manual value entry.
A color wheel widget.
A color ramp widget.
Blue component of color.
#RRGGBBAA in hexadecimal, with alpha
void mouseReleaseEvent(QMouseEvent *e) override
Hue component of color (based on HSV model)
#RRGGBB in hexadecimal
virtual void setComponentValue(const int value)
Alters the widget&#39;s color by setting the value for the widget&#39;s color component.
ColorComponent
Specifies the color component which the widget alters.
A preview box which displays one or two colors as swatches.
QList< QPair< QRgb, int > > QgsColorBox
Saturation component of color (based on HSV model)
An action containing a color widget, which can be embedded into a menu.
Orientation orientation() const
Fetches the orientation for the color ramp.
virtual void setComponent(const QgsColorWidget::ColorComponent component)
Sets the color component which the widget controls.
bool dismissOnColorSelection() const
Returns whether the parent menu will be dismissed after a color is selected from the action&#39;s color w...
ColorComponent mComponent
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action&#39;...
A line edit widget which displays colors as text and accepts string representations of colors...
A color box widget.
virtual void setColor(const QColor &color, const bool emitSignals=false)
Sets the color for the widget.
void mouseMoveEvent(QMouseEvent *e) override
QgsColorWidget * colorWidget()
Returns the color widget contained in the widget action.