QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
22 class QColor;
23 class QSpinBox;
24 class QLineEdit;
25 class QToolButton;
26 
36 class GUI_EXPORT QgsColorWidget : public QWidget
37 {
38  Q_OBJECT
39 
40  public:
41 
45  {
46  Multiple = 0,
47  Red,
49  Blue,
50  Hue,
53  Alpha
54  };
55 
60  QgsColorWidget( QWidget* parent = nullptr, const ColorComponent component = Multiple );
61 
62  virtual ~QgsColorWidget();
63 
68  QColor color() const;
69 
74  ColorComponent component() const { return mComponent; }
75 
82  int componentValue() const;
83 
87  static QPixmap createDragIcon( const QColor &color );
88 
89  public slots:
90 
96  virtual void setColor( const QColor &color, const bool emitSignals = false );
97 
102  virtual void setComponent( const ColorComponent component );
103 
112  virtual void setComponentValue( const int value );
113 
114  signals:
115 
119  void colorChanged( const QColor &color );
120 
124  void hovered();
125 
126  protected:
127 
129 
131 
136 
140  int componentRange() const;
141 
145  int componentRange( const ColorComponent component ) const;
146 
153  int componentValue( const ColorComponent component ) const;
154 
159  int hue() const;
160 
167  void alterColor( QColor& color, const QgsColorWidget::ColorComponent component, const int newValue ) const;
168 
172  static const QPixmap& transparentBackground();
173 
174  //Reimplemented to accept dragged colors
175  void dragEnterEvent( QDragEnterEvent * e ) override;
176 
177  //Reimplemented to accept dropped colors
178  void dropEvent( QDropEvent *e ) override;
179 
180  void mouseMoveEvent( QMouseEvent* e ) override;
181  void mousePressEvent( QMouseEvent* e ) override;
182  void mouseReleaseEvent( QMouseEvent* e ) override;
183 };
184 
185 
193 class GUI_EXPORT QgsColorWidgetAction: public QWidgetAction
194 {
195  Q_OBJECT
196 
197  public:
198 
204  QgsColorWidgetAction( QgsColorWidget* colorWidget, QMenu* menu = nullptr, QWidget *parent = nullptr );
205 
206  virtual ~QgsColorWidgetAction();
207 
210  QgsColorWidget* colorWidget() { return mColorWidget; }
211 
219  void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
220 
225  bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
226 
227  signals:
228 
232  void colorChanged( const QColor &color );
233 
234  private:
235  QMenu* mMenu;
236  QgsColorWidget* mColorWidget;
237 
238  //used to suppress recursion with hover events
239  bool mSuppressRecurse;
240 
241  bool mDismissOnColorSelection;
242 
243  private slots:
244 
247  void onHover();
248 
251  void setColor( const QColor &color );
252 };
253 
254 
255 
263 class GUI_EXPORT QgsColorWheel : public QgsColorWidget
264 {
265  Q_OBJECT
266 
267  public:
268 
272  QgsColorWheel( QWidget* parent = nullptr );
273 
274  virtual ~QgsColorWheel();
275 
276  virtual QSize sizeHint() const override;
277  void paintEvent( QPaintEvent* event ) override;
278 
279  public slots:
280 
281  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
282 
283  protected:
284 
285  virtual void resizeEvent( QResizeEvent *event ) override;
286  virtual void mouseMoveEvent( QMouseEvent *event ) override;
287  virtual void mousePressEvent( QMouseEvent *event ) override;
288  virtual void mouseReleaseEvent( QMouseEvent *event ) override;
289 
290  private:
291 
292  enum ControlPart
293  {
294  None,
295  Wheel,
296  Triangle
297  };
298 
299  /*Margin between outer ring and edge of widget*/
300  int mMargin;
301 
302  /*Thickness of hue ring in pixels*/
303  int mWheelThickness;
304 
305  /*Part of the wheel where the mouse was originally depressed*/
306  ControlPart mClickedPart;
307 
308  /*Cached image of hue wheel*/
309  QImage* mWheelImage;
310 
311  /*Cached image of inner triangle*/
312  QImage* mTriangleImage;
313 
314  /*Resuable, temporary image for drawing widget*/
315  QImage* mWidgetImage;
316 
317  /*Whether the color wheel image requires redrawing*/
318  bool mWheelDirty;
319 
320  /*Whether the inner triangle image requires redrawing*/
321  bool mTriangleDirty;
322 
323  /*Conical gradient brush used for drawing hue wheel*/
324  QBrush mWheelBrush;
325 
329  void createImages( const QSizeF size );
330 
332  void createWheel();
333 
335  void createTriangle();
336 
340  void setColorFromPos( const QPointF pos );
341 
342 };
343 
344 
353 class GUI_EXPORT QgsColorBox : public QgsColorWidget
354 {
355  Q_OBJECT
356 
357  public:
358 
365  QgsColorBox( QWidget* parent = nullptr, const ColorComponent component = Value );
366 
367  virtual ~QgsColorBox();
368 
369  virtual QSize sizeHint() const override;
370  void paintEvent( QPaintEvent* event ) override;
371 
372  virtual void setComponent( const ColorComponent component ) override;
373 
374  public slots:
375  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
376 
377  protected:
378 
379  virtual void resizeEvent( QResizeEvent *event ) override;
380  virtual void mouseMoveEvent( QMouseEvent *event ) override;
381  virtual void mousePressEvent( QMouseEvent *event ) override;
382 
383  private:
384 
385  /*Margin between outer ring and edge of widget*/
386  int mMargin;
387 
388  /*Cached image for color box*/
389  QImage* mBoxImage;
390 
391  /*Whether the cached image requires redrawing*/
392  bool mDirty;
393 
396  void createBox();
397 
401  int valueRangeX() const;
402 
406  int valueRangeY() const;
407 
410  QgsColorWidget::ColorComponent yComponent() const;
411 
414  int yComponentValue() const;
415 
418  QgsColorWidget::ColorComponent xComponent() const;
419 
422  int xComponentValue() const;
423 
427  void setColorFromPoint( QPoint point );
428 
429 };
430 
431 
439 class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
440 {
441  Q_OBJECT
442 
443  public:
444 
448  {
449  Horizontal = 0,
450  Vertical
451  };
452 
458  QgsColorRampWidget( QWidget* parent = nullptr,
459  const ColorComponent component = QgsColorWidget::Red,
460  const Orientation orientation = QgsColorRampWidget::Horizontal );
461 
462  virtual ~QgsColorRampWidget();
463 
464  virtual QSize sizeHint() const override;
465  void paintEvent( QPaintEvent* event ) override;
466 
471  void setOrientation( const Orientation orientation );
472 
477  Orientation orientation() const { return mOrientation; }
478 
483  void setInteriorMargin( const int margin );
484 
489  int interiorMargin() const { return mMargin; }
490 
495  void setShowFrame( const bool showFrame );
496 
501  bool showFrame() const { return mShowFrame; }
502 
506  void setMarkerSize( const int markerSize );
507 
508  signals:
509 
513  void valueChanged( const int value );
514 
515  protected:
516 
517  virtual void mouseMoveEvent( QMouseEvent *event ) override;
518  virtual void wheelEvent( QWheelEvent* event ) override;
519  virtual void mousePressEvent( QMouseEvent *event ) override;
520  virtual void keyPressEvent( QKeyEvent * event ) override;
521 
522  private:
523 
524  /*Orientation for ramp*/
525  Orientation mOrientation;
526 
527  /*Margin around ramp*/
528  int mMargin;
529 
530  /*Whether to draw a frame around the ramp*/
531  bool mShowFrame;
532 
533  /*Polygon for upper triangle marker*/
534  QPolygonF mTopTriangle;
535 
536  /*Polygon for lower triangle marker*/
537  QPolygonF mBottomTriangle;
538 
542  void setColorFromPoint( QPointF point );
543 
544 };
545 
546 
553 class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
554 {
555  Q_OBJECT
556 
557  public:
558 
563  QgsColorSliderWidget( QWidget* parent = nullptr, const ColorComponent component = QgsColorWidget::Red );
564 
565  virtual ~QgsColorSliderWidget();
566 
567  virtual void setComponent( const ColorComponent component ) override;
568  virtual void setComponentValue( const int value ) override;
569  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
570 
571  private:
572 
573  /*Color ramp widget*/
574  QgsColorRampWidget* mRampWidget;
575 
576  /*Spin box widget*/
577  QSpinBox* mSpinBox;
578 
585  int convertRealToDisplay( const int realValue ) const;
586 
592  int convertDisplayToReal( const int displayValue ) const;
593 
594  private slots:
595 
598  void rampColorChanged( const QColor &color );
599 
602  void spinChanged( int value );
603 
606  void rampChanged( int value );
607 
608 };
609 
610 
618 class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
619 {
620  Q_OBJECT
621 
622  public:
623 
627  QgsColorTextWidget( QWidget* parent = nullptr );
628 
629  virtual ~QgsColorTextWidget();
630 
631  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
632 
633  protected:
634  void resizeEvent( QResizeEvent * event ) override;
635 
636  private:
637 
640  enum ColorTextFormat
641  {
642  HexRgb = 0,
643  HexRgbA,
644  Rgb,
645  Rgba
646  };
647 
648  QLineEdit* mLineEdit;
649 
650  /*Dropdown menu button*/
651  QToolButton* mMenuButton;
652 
653  /*Display format for colors*/
654  ColorTextFormat mFormat;
655 
658  void updateText();
659 
660  private slots:
661 
664  void textChanged();
665 
668  void showMenu();
669 };
670 
671 
678 class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
679 {
680  Q_OBJECT
681 
682  public:
683 
687  QgsColorPreviewWidget( QWidget* parent = nullptr );
688 
689  virtual ~QgsColorPreviewWidget();
690 
691  void paintEvent( QPaintEvent* event ) override;
692  virtual QSize sizeHint() const override;
693 
700  QColor color2() const { return mColor2; }
701 
702  public slots:
703 
710  virtual void setColor2( const QColor& color );
711 
712  protected:
713 
714  //reimplemented to allow dragging colors
715  void mousePressEvent( QMouseEvent* e ) override;
716 
717  //reimplemented to click colors
718  void mouseReleaseEvent( QMouseEvent* e ) override;
719 
720  //reimplemented to allow dragging colors
721  void mouseMoveEvent( QMouseEvent *e ) override;
722 
723  private:
724 
725  /*Secondary color for widget*/
726  QColor mColor2;
727 
728  QPoint mDragStartPosition;
729 
730  /*Draws a color preview within the specified rect.
731  * @param color color to draw
732  * @param rect rect to draw color in
733  * @param painter destination painter
734  */
735  void drawColor( const QColor& color, QRect rect, QPainter &painter );
736 };
737 
738 #endif // #ifndef QGSCOLORWIDGETS_H
bool showFrame() const
Fetches whether the ramp is drawn within a frame.
A base class for interactive color widgets.
virtual void dropEvent(QDropEvent *event)
int interiorMargin() const
Fetches the margin between the edge of the widget and the ramp.
Orientation
Specifies the orientation of a color ramp.
virtual void dragEnterEvent(QDragEnterEvent *event)
ColorComponent component() const
Returns the color component which the widget controls.
void mousePressEvent(QMouseEvent *e) override
virtual void mouseReleaseEvent(QMouseEvent *event)
QColor color2() const
Returns the secondary color for the widget.
virtual void paintEvent(QPaintEvent *event)
A composite horizontal color ramp widget and associated spinbox for manual value entry.
A color wheel widget.
virtual void mousePressEvent(QMouseEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event)
A color ramp widget.
void mouseReleaseEvent(QMouseEvent *e) override
virtual void wheelEvent(QWheelEvent *event)
virtual QSize sizeHint() const
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.
An action containing a color widget, which can be embedded into a menu.
virtual void keyPressEvent(QKeyEvent *event)
Orientation orientation() const
Fetches the orientation for the color ramp.
int mExplicitHue
QColor wipes the hue information when it is ambiguous (eg, for saturation = 0).
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
virtual void resizeEvent(QResizeEvent *event)
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 setComponent(const ColorComponent component)
Sets the color component which the widget controls.
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.