QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <QWidget>
20 
21 class QColor;
22 class QSpinBox;
23 class QLineEdit;
24 class QToolButton;
25 
35 class GUI_EXPORT QgsColorWidget : public QWidget
36 {
37  Q_OBJECT
38 
39  public:
40 
44  {
45  Multiple = 0,
46  Red,
48  Blue,
49  Hue,
52  Alpha
53  };
54 
59  QgsColorWidget( QWidget* parent = 0, const ColorComponent component = Multiple );
60 
61  virtual ~QgsColorWidget();
62 
67  QColor color() const;
68 
73  ColorComponent component() const { return mComponent; }
74 
81  int componentValue() const;
82 
86  static QPixmap createDragIcon( const QColor &color );
87 
88  public slots:
89 
95  virtual void setColor( const QColor &color, const bool emitSignals = false );
96 
101  virtual void setComponent( const ColorComponent component );
102 
111  virtual void setComponentValue( const int value );
112 
113  signals:
114 
118  void colorChanged( const QColor &color );
119 
120  protected:
121 
123 
125 
130 
134  int componentRange() const;
135 
139  int componentRange( const ColorComponent component ) const;
140 
147  int componentValue( const ColorComponent component ) const;
148 
153  int hue() const;
154 
161  void alterColor( QColor& color, const QgsColorWidget::ColorComponent component, const int newValue ) const;
162 
166  static const QPixmap& transparentBackground();
167 
168  //Reimplemented to accept dragged colors
169  void dragEnterEvent( QDragEnterEvent * e ) override;
170 
171  //Reimplemented to accept dropped colors
172  void dropEvent( QDropEvent *e ) override;
173 };
174 
175 
183 class GUI_EXPORT QgsColorWheel : public QgsColorWidget
184 {
185  Q_OBJECT
186 
187  public:
188 
192  QgsColorWheel( QWidget* parent = 0 );
193 
194  virtual ~QgsColorWheel();
195 
196  void paintEvent( QPaintEvent* event ) override;
197 
198  public slots:
199 
200  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
201 
202  protected:
203 
204  virtual void resizeEvent( QResizeEvent *event ) override;
205  virtual void mouseMoveEvent( QMouseEvent *event ) override;
206  virtual void mousePressEvent( QMouseEvent *event ) override;
207  virtual void mouseReleaseEvent( QMouseEvent *event ) override;
208 
209  private:
210 
211  enum ControlPart
212  {
213  None,
214  Wheel,
215  Triangle
216  };
217 
218  /*Margin between outer ring and edge of widget*/
219  int mMargin;
220 
221  /*Thickness of hue ring in pixels*/
222  int mWheelThickness;
223 
224  /*Part of the wheel where the mouse was originally depressed*/
225  ControlPart mClickedPart;
226 
227  /*Cached image of hue wheel*/
228  QImage* mWheelImage;
229 
230  /*Cached image of inner triangle*/
231  QImage* mTriangleImage;
232 
233  /*Resuable, temporary image for drawing widget*/
234  QImage* mWidgetImage;
235 
236  /*Whether the color wheel image requires redrawing*/
237  bool mWheelDirty;
238 
239  /*Whether the inner triangle image requires redrawing*/
240  bool mTriangleDirty;
241 
242  /*Conical gradient brush used for drawing hue wheel*/
243  QBrush mWheelBrush;
244 
248  void createImages( const QSizeF size );
249 
251  void createWheel();
252 
254  void createTriangle();
255 
259  void setColorFromPos( const QPointF pos );
260 
261 };
262 
263 
272 class GUI_EXPORT QgsColorBox : public QgsColorWidget
273 {
274  Q_OBJECT
275 
276  public:
277 
284  QgsColorBox( QWidget* parent = 0, const ColorComponent component = Value );
285 
286  virtual ~QgsColorBox();
287 
288  virtual QSize sizeHint() const override;
289  void paintEvent( QPaintEvent* event ) override;
290 
291  virtual void setComponent( const ColorComponent component ) override;
292 
293  public slots:
294  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
295 
296  protected:
297 
298  virtual void resizeEvent( QResizeEvent *event ) override;
299  virtual void mouseMoveEvent( QMouseEvent *event ) override;
300  virtual void mousePressEvent( QMouseEvent *event ) override;
301 
302  private:
303 
304  /*Margin between outer ring and edge of widget*/
305  int mMargin;
306 
307  /*Cached image for color box*/
308  QImage* mBoxImage;
309 
310  /*Whether the cached image requires redrawing*/
311  bool mDirty;
312 
315  void createBox();
316 
320  int valueRangeX() const;
321 
325  int valueRangeY() const;
326 
329  QgsColorWidget::ColorComponent yComponent() const;
330 
333  int yComponentValue() const;
334 
337  QgsColorWidget::ColorComponent xComponent() const;
338 
341  int xComponentValue() const;
342 
346  void setColorFromPoint( const QPoint& point );
347 
348 };
349 
350 
358 class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
359 {
360  Q_OBJECT
361 
362  public:
363 
367  {
368  Horizontal = 0,
369  Vertical
370  };
371 
377  QgsColorRampWidget( QWidget* parent = 0,
378  const ColorComponent component = QgsColorWidget::Red,
379  const Orientation orientation = QgsColorRampWidget::Horizontal );
380 
381  virtual ~QgsColorRampWidget();
382 
383  virtual QSize sizeHint() const override;
384  void paintEvent( QPaintEvent* event ) override;
385 
390  void setOrientation( const Orientation orientation );
391 
396  Orientation orientation() const { return mOrientation; }
397 
402  void setInteriorMargin( const int margin );
403 
408  int interiorMargin() const { return mMargin; }
409 
414  void setShowFrame( const bool showFrame );
415 
420  bool showFrame() const { return mShowFrame; }
421 
425  void setMarkerSize( const int markerSize );
426 
427  signals:
428 
432  void valueChanged( const int value );
433 
434  protected:
435 
436  virtual void mouseMoveEvent( QMouseEvent *event ) override;
437  virtual void mousePressEvent( QMouseEvent *event ) override;
438  virtual void keyPressEvent( QKeyEvent * event ) override;
439 
440  private:
441 
442  /*Orientation for ramp*/
443  Orientation mOrientation;
444 
445  /*Margin around ramp*/
446  int mMargin;
447 
448  /*Whether to draw a frame around the ramp*/
449  bool mShowFrame;
450 
451  /*Polygon for upper triangle marker*/
452  QPolygonF mTopTriangle;
453 
454  /*Polygon for lower triangle marker*/
455  QPolygonF mBottomTriangle;
456 
460  void setColorFromPoint( const QPointF &point );
461 
462 };
463 
464 
471 class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
472 {
473  Q_OBJECT
474 
475  public:
476 
481  QgsColorSliderWidget( QWidget* parent = 0, const ColorComponent component = QgsColorWidget::Red );
482 
483  virtual ~QgsColorSliderWidget();
484 
485  virtual void setComponent( const ColorComponent component ) override;
486  virtual void setComponentValue( const int value ) override;
487  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
488 
489  private:
490 
491  /*Color ramp widget*/
492  QgsColorRampWidget* mRampWidget;
493 
494  /*Spin box widget*/
495  QSpinBox* mSpinBox;
496 
503  int convertRealToDisplay( const int realValue ) const;
504 
510  int convertDisplayToReal( const int displayValue ) const;
511 
512  private slots:
513 
516  void rampColorChanged( const QColor &color );
517 
520  void spinChanged( int value );
521 
524  void rampChanged( int value );
525 
526 };
527 
528 
536 class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
537 {
538  Q_OBJECT
539 
540  public:
541 
545  QgsColorTextWidget( QWidget* parent = 0 );
546 
547  virtual ~QgsColorTextWidget();
548 
549  virtual void setColor( const QColor &color, const bool emitSignals = false ) override;
550 
551  protected:
552  void resizeEvent( QResizeEvent * event ) override;
553 
554  private:
555 
558  enum ColorTextFormat
559  {
560  HexRgb = 0,
561  HexRgbA,
562  Rgb,
563  Rgba
564  };
565 
566  QLineEdit* mLineEdit;
567 
568  /*Dropdown menu button*/
569  QToolButton* mMenuButton;
570 
571  /*Display format for colors*/
572  ColorTextFormat mFormat;
573 
576  void updateText();
577 
578  private slots:
579 
582  void textChanged();
583 
586  void showMenu();
587 };
588 
589 
596 class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
597 {
598  Q_OBJECT
599 
600  public:
601 
605  QgsColorPreviewWidget( QWidget* parent = 0 );
606 
607  virtual ~QgsColorPreviewWidget();
608 
609  void paintEvent( QPaintEvent* event ) override;
610 
617  QColor color2() const { return mColor2; }
618 
619  public slots:
620 
627  virtual void setColor2( const QColor& color );
628 
629  protected:
630 
631  //reimplemented to allow dragging colors
632  void mousePressEvent( QMouseEvent* e ) override;
633 
634  //reimplemented to click colors
635  void mouseReleaseEvent( QMouseEvent* e ) override;
636 
637  //reimplemented to allow dragging colors
638  void mouseMoveEvent( QMouseEvent *e ) override;
639 
640  private:
641 
642  /*Secondary color for widget*/
643  QColor mColor2;
644 
645  QPoint mDragStartPosition;
646 
647  /*Draws a color preview within the specified rect.
648  * @param color color to draw
649  * @param rect rect to draw color in
650  * @param painter destination painter
651  */
652  void drawColor( const QColor& color, const QRect& rect, QPainter &painter );
653 };
654 
655 #endif // #ifndef QGSCOLORWIDGETS_H