QGIS API Documentation  2.6.0-Brighton
 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 );
170 
171  //Reimplemented to accept dropped colors
172  void dropEvent( QDropEvent *e );
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 );
197 
198  public slots:
199 
200  virtual void setColor( const QColor color, const bool emitSignals = false );
201 
202  protected:
203 
204  virtual void resizeEvent( QResizeEvent *event );
205  virtual void mouseMoveEvent( QMouseEvent *event );
206  virtual void mousePressEvent( QMouseEvent *event );
207  virtual void mouseReleaseEvent( QMouseEvent *event );
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;
289  void paintEvent( QPaintEvent* event );
290 
291  virtual void setComponent( const ColorComponent component );
292 
293  public slots:
294 
295  virtual void setColor( const QColor color, const bool emitSignals = false );
296 
297  protected:
298 
299  virtual void resizeEvent( QResizeEvent *event );
300  virtual void mouseMoveEvent( QMouseEvent *event );
301  virtual void mousePressEvent( QMouseEvent *event );
302 
303  private:
304 
305  /*Margin between outer ring and edge of widget*/
306  int mMargin;
307 
308  /*Cached image for color box*/
309  QImage* mBoxImage;
310 
311  /*Whether the cached image requires redrawing*/
312  bool mDirty;
313 
316  void createBox();
317 
321  int valueRangeX() const;
322 
326  int valueRangeY() const;
327 
330  QgsColorWidget::ColorComponent yComponent() const;
331 
334  int yComponentValue() const;
335 
338  QgsColorWidget::ColorComponent xComponent() const;
339 
342  int xComponentValue() const;
343 
347  void setColorFromPoint( const QPoint& point );
348 
349 };
350 
351 
359 class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
360 {
361  Q_OBJECT
362 
363  public:
364 
368  {
369  Horizontal = 0,
370  Vertical
371  };
372 
378  QgsColorRampWidget( QWidget* parent = 0,
379  const ColorComponent component = QgsColorWidget::Red,
380  const Orientation orientation = QgsColorRampWidget::Horizontal );
381 
382  virtual ~QgsColorRampWidget();
383 
384  virtual QSize sizeHint() const;
385  void paintEvent( QPaintEvent* event );
386 
391  void setOrientation( const Orientation orientation );
392 
397  Orientation orientation() const { return mOrientation; }
398 
403  void setInteriorMargin( const int margin );
404 
409  int interiorMargin() const { return mMargin; }
410 
415  void setShowFrame( const bool showFrame );
416 
421  bool showFrame() const { return mShowFrame; }
422 
426  void setMarkerSize( const int markerSize );
427 
428  signals:
429 
433  void valueChanged( const int value );
434 
435  protected:
436 
437  virtual void mouseMoveEvent( QMouseEvent *event );
438  virtual void mousePressEvent( QMouseEvent *event );
439  virtual void keyPressEvent( QKeyEvent * event );
440 
441  private:
442 
443  /*Orientation for ramp*/
444  Orientation mOrientation;
445 
446  /*Margin around ramp*/
447  int mMargin;
448 
449  /*Whether to draw a frame around the ramp*/
450  bool mShowFrame;
451 
452  /*Polygon for upper triangle marker*/
453  QPolygonF mTopTriangle;
454 
455  /*Polygon for lower triangle marker*/
456  QPolygonF mBottomTriangle;
457 
461  void setColorFromPoint( const QPointF &point );
462 
463 };
464 
465 
472 class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
473 {
474  Q_OBJECT
475 
476  public:
477 
482  QgsColorSliderWidget( QWidget* parent = 0, const ColorComponent component = QgsColorWidget::Red );
483 
484  virtual ~QgsColorSliderWidget();
485 
486  virtual void setComponent( const ColorComponent component );
487  virtual void setComponentValue( const int value );
488  virtual void setColor( const QColor color, const bool emitSignals = false );
489 
490  private:
491 
492  /*Color ramp widget*/
493  QgsColorRampWidget* mRampWidget;
494 
495  /*Spin box widget*/
496  QSpinBox* mSpinBox;
497 
504  int convertRealToDisplay( const int realValue ) const;
505 
511  int convertDisplayToReal( const int displayValue ) const;
512 
513  private slots:
514 
517  void rampColorChanged( const QColor color );
518 
521  void spinChanged( int value );
522 
525  void rampChanged( int value );
526 
527 };
528 
529 
537 class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
538 {
539  Q_OBJECT
540 
541  public:
542 
546  QgsColorTextWidget( QWidget* parent = 0 );
547 
548  virtual ~QgsColorTextWidget();
549 
550  virtual void setColor( const QColor color, const bool emitSignals = false );
551 
552  protected:
553  void resizeEvent( QResizeEvent * event );
554 
555  private:
556 
559  enum ColorTextFormat
560  {
561  HexRgb = 0,
562  HexRgbA,
563  Rgb,
564  Rgba
565  };
566 
567  QLineEdit* mLineEdit;
568 
569  /*Dropdown menu button*/
570  QToolButton* mMenuButton;
571 
572  /*Display format for colors*/
573  ColorTextFormat mFormat;
574 
577  void updateText();
578 
579  private slots:
580 
583  void textChanged();
584 
587  void showMenu();
588 };
589 
590 
597 class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
598 {
599  Q_OBJECT
600 
601  public:
602 
606  QgsColorPreviewWidget( QWidget* parent = 0 );
607 
608  virtual ~QgsColorPreviewWidget();
609 
610  void paintEvent( QPaintEvent* event );
611 
618  QColor color2() const { return mColor2; }
619 
620  public slots:
621 
628  virtual void setColor2( const QColor& color );
629 
630  protected:
631 
632  //reimplemented to allow dragging colors
633  void mousePressEvent( QMouseEvent* e );
634 
635  //reimplemented to click colors
636  void mouseReleaseEvent( QMouseEvent* e );
637 
638  //reimplemented to allow dragging colors
639  void mouseMoveEvent( QMouseEvent *e );
640 
641  private:
642 
643  /*Secondary color for widget*/
644  QColor mColor2;
645 
646  QPoint mDragStartPosition;
647 
648  /*Draws a color preview within the specified rect.
649  * @param color color to draw
650  * @param rect rect to draw color in
651  * @param painter destination painter
652  */
653  void drawColor( const QColor& color, const QRect& rect, QPainter &painter );
654 };
655 
656 #endif // #ifndef QGSCOLORWIDGETS_H