QGIS API Documentation  2.14.0-Essen
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 mousePressEvent( QMouseEvent *event ) override;
519  virtual void keyPressEvent( QKeyEvent * event ) override;
520 
521  private:
522 
523  /*Orientation for ramp*/
524  Orientation mOrientation;
525 
526  /*Margin around ramp*/
527  int mMargin;
528 
529  /*Whether to draw a frame around the ramp*/
530  bool mShowFrame;
531 
532  /*Polygon for upper triangle marker*/
533  QPolygonF mTopTriangle;
534 
535  /*Polygon for lower triangle marker*/
536  QPolygonF mBottomTriangle;
537 
541  void setColorFromPoint( QPointF point );
542 
543 };
544 
545 
552 class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
553 {
554  Q_OBJECT
555 
556  public:
557 
562  QgsColorSliderWidget( QWidget* parent = nullptr, const ColorComponent component = QgsColorWidget::Red );
563 
564  virtual ~QgsColorSliderWidget();
565 
566  virtual void setComponent( const ColorComponent component ) override;
567  virtual void setComponentValue( const int value ) override;
568  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
569 
570  private:
571 
572  /*Color ramp widget*/
573  QgsColorRampWidget* mRampWidget;
574 
575  /*Spin box widget*/
576  QSpinBox* mSpinBox;
577 
584  int convertRealToDisplay( const int realValue ) const;
585 
591  int convertDisplayToReal( const int displayValue ) const;
592 
593  private slots:
594 
597  void rampColorChanged( const QColor &color );
598 
601  void spinChanged( int value );
602 
605  void rampChanged( int value );
606 
607 };
608 
609 
617 class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
618 {
619  Q_OBJECT
620 
621  public:
622 
626  QgsColorTextWidget( QWidget* parent = nullptr );
627 
628  virtual ~QgsColorTextWidget();
629 
630  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
631 
632  protected:
633  void resizeEvent( QResizeEvent * event ) override;
634 
635  private:
636 
639  enum ColorTextFormat
640  {
641  HexRgb = 0,
642  HexRgbA,
643  Rgb,
644  Rgba
645  };
646 
647  QLineEdit* mLineEdit;
648 
649  /*Dropdown menu button*/
650  QToolButton* mMenuButton;
651 
652  /*Display format for colors*/
653  ColorTextFormat mFormat;
654 
657  void updateText();
658 
659  private slots:
660 
663  void textChanged();
664 
667  void showMenu();
668 };
669 
670 
677 class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
678 {
679  Q_OBJECT
680 
681  public:
682 
686  QgsColorPreviewWidget( QWidget* parent = nullptr );
687 
688  virtual ~QgsColorPreviewWidget();
689 
690  void paintEvent( QPaintEvent* event ) override;
691  virtual QSize sizeHint() const override;
692 
699  QColor color2() const { return mColor2; }
700 
701  public slots:
702 
709  virtual void setColor2( const QColor& color );
710 
711  protected:
712 
713  //reimplemented to allow dragging colors
714  void mousePressEvent( QMouseEvent* e ) override;
715 
716  //reimplemented to click colors
717  void mouseReleaseEvent( QMouseEvent* e ) override;
718 
719  //reimplemented to allow dragging colors
720  void mouseMoveEvent( QMouseEvent *e ) override;
721 
722  private:
723 
724  /*Secondary color for widget*/
725  QColor mColor2;
726 
727  QPoint mDragStartPosition;
728 
729  /*Draws a color preview within the specified rect.
730  * @param color color to draw
731  * @param rect rect to draw color in
732  * @param painter destination painter
733  */
734  void drawColor( const QColor& color, QRect rect, QPainter &painter );
735 };
736 
737 #endif // #ifndef QGSCOLORWIDGETS_H
bool dismissOnColorSelection() const
Returns whether the parent menu will be dismissed after a color is selected from the action&#39;s color w...
Orientation orientation() const
Fetches the orientation for the color ramp.
bool showFrame() const
Fetches whether the ramp is drawn within a frame.
QColor color2() const
Returns the secondary color for the widget.
A base class for interactive color widgets.
virtual void dropEvent(QDropEvent *event)
Orientation
Specifies the orientation of a color ramp.
int interiorMargin() const
Fetches the margin between the edge of the widget and the ramp.
virtual void dragEnterEvent(QDragEnterEvent *event)
void mousePressEvent(QMouseEvent *e) override
virtual void mouseReleaseEvent(QMouseEvent *event)
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 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)
int mExplicitHue
QColor wipes the hue information when it is ambiguous (eg, for saturation = 0).
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.
ColorComponent component() const
Returns the color component which the widget controls.
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.