QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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_sip.h"
23
24class QColor;
25class QSpinBox;
26class QLineEdit;
27class QToolButton;
28
38class GUI_EXPORT QgsColorWidget : public QWidget
39{
40 Q_OBJECT
41
42 public:
43
48 {
49 Multiple = 0,
56 Alpha
57 };
58
64 QgsColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Multiple );
65
71 QColor color() const;
72
78 ColorComponent component() const { return mComponent; }
79
87 int componentValue() const;
88
93 static QPixmap createDragIcon( const QColor &color );
94
95 public slots:
96
103 virtual void setColor( const QColor &color, bool emitSignals = false );
104
110 virtual void setComponent( QgsColorWidget::ColorComponent component );
111
121 virtual void setComponentValue( int value );
122
123 signals:
124
129 void colorChanged( const QColor &color );
130
134 void hovered();
135
136 protected:
137
139
141
146 int mExplicitHue = 0;
147
152 int componentRange() const;
153
158 int componentRange( ColorComponent component ) const;
159
167 int componentValue( ColorComponent component ) const;
168
174 int hue() const;
175
183 void alterColor( QColor &color, QgsColorWidget::ColorComponent component, int newValue ) const;
184
189 static const QPixmap &transparentBackground();
190
191 //Reimplemented to accept dragged colors
192 void dragEnterEvent( QDragEnterEvent *e ) override;
193
194 //Reimplemented to accept dropped colors
195 void dropEvent( QDropEvent *e ) override;
196
197 void mouseMoveEvent( QMouseEvent *e ) override;
198 void mousePressEvent( QMouseEvent *e ) override;
199 void mouseReleaseEvent( QMouseEvent *e ) override;
200};
201
202
210class GUI_EXPORT QgsColorWidgetAction: public QWidgetAction
211{
212 Q_OBJECT
213
214 public:
215
222 QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
223
227 QgsColorWidget *colorWidget() { return mColorWidget; }
228
237 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
238
244 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
245
246 signals:
247
252 void colorChanged( const QColor &color );
253
254 private:
255 QMenu *mMenu = nullptr;
256 QgsColorWidget *mColorWidget = nullptr;
257
258 //used to suppress recursion with hover events
259 bool mSuppressRecurse;
260
261 bool mDismissOnColorSelection;
262
263 private slots:
264
268 void onHover();
269
273 void setColor( const QColor &color );
274};
275
276
277
285class GUI_EXPORT QgsColorWheel : public QgsColorWidget
286{
287 Q_OBJECT
288
289 public:
290
295 QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
296
297 ~QgsColorWheel() override;
298
299 QSize sizeHint() const override;
300 void paintEvent( QPaintEvent *event ) override;
301
302 public slots:
303
304 void setColor( const QColor &color, bool emitSignals = false ) override;
305
306 protected:
307
308 void resizeEvent( QResizeEvent *event ) override;
309 void mouseMoveEvent( QMouseEvent *event ) override;
310 void mousePressEvent( QMouseEvent *event ) override;
311 void mouseReleaseEvent( QMouseEvent *event ) override;
312
313 private:
314
315 enum ControlPart
316 {
317 None,
318 Wheel,
319 Triangle
320 };
321
322 bool mIsDragging = false;
323
324 /*Margin between outer ring and edge of widget*/
325 int mMargin = 4;
326
327 /*Thickness of hue ring in pixels*/
328 int mWheelThickness = 18;
329
330 /*Part of the wheel where the mouse was originally depressed*/
331 ControlPart mClickedPart = QgsColorWheel::None;
332
333 /*Cached image of hue wheel*/
334 QImage mWheelImage;
335
336 /*Cached image of inner triangle*/
337 QImage mTriangleImage;
338
339 /*Resuable, temporary image for drawing widget*/
340 QImage mWidgetImage;
341
342 /*Whether the color wheel image requires redrawing*/
343 bool mWheelDirty = true;
344
345 /*Whether the inner triangle image requires redrawing*/
346 bool mTriangleDirty = true;
347
348 /*Conical gradient brush used for drawing hue wheel*/
349 QBrush mWheelBrush;
350
355 void createImages( QSizeF size );
356
358 void createWheel();
359
361 void createTriangle();
362
367 void setColorFromPos( QPointF pos );
368
369};
370
371
380class GUI_EXPORT QgsColorBox : public QgsColorWidget
381{
382 Q_OBJECT
383
384 public:
385
393 QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
394
395 ~QgsColorBox() override;
396
397 QSize sizeHint() const override;
398 void paintEvent( QPaintEvent *event ) override;
399
400 void setComponent( ColorComponent component ) override;
401
402 public slots:
403 void setColor( const QColor &color, bool emitSignals = false ) override;
404
405 protected:
406
407 void resizeEvent( QResizeEvent *event ) override;
408 void mouseMoveEvent( QMouseEvent *event ) override;
409 void mousePressEvent( QMouseEvent *event ) override;
410 void mouseReleaseEvent( QMouseEvent *event ) override;
411
412 private:
413
414 bool mIsDragging = false;
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
478class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
479{
480 Q_OBJECT
481
482 public:
483
488 {
489 Horizontal = 0,
490 Vertical
491 };
492
499 QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr,
500 ColorComponent component = QgsColorWidget::Red,
501 Orientation orientation = QgsColorRampWidget::Horizontal );
502
503 QSize sizeHint() const override;
504 void paintEvent( QPaintEvent *event ) override;
505
511 void setOrientation( Orientation orientation );
512
518 Orientation orientation() const { return mOrientation; }
519
525 void setInteriorMargin( int margin );
526
532 int interiorMargin() const { return mMargin; }
533
539 void setShowFrame( bool showFrame );
540
546 bool showFrame() const { return mShowFrame; }
547
552 void setMarkerSize( int markerSize );
553
554 signals:
555
560 void valueChanged( int value );
561
562 protected:
563
564 void mouseMoveEvent( QMouseEvent *event ) override;
565 void wheelEvent( QWheelEvent *event ) override;
566 void mousePressEvent( QMouseEvent *event ) override;
567 void mouseReleaseEvent( QMouseEvent *event ) override;
568 void keyPressEvent( QKeyEvent *event ) override;
569
570 private:
571
572 bool mIsDragging = false;
573
574 /*Orientation for ramp*/
575 Orientation mOrientation;
576
577 /*Margin around ramp*/
578 int mMargin = 4;
579
580 /*Whether to draw a frame around the ramp*/
581 bool mShowFrame = false;
582
583 /*Polygon for upper triangle marker*/
584 QPolygonF mTopTriangle;
585
586 /*Polygon for lower triangle marker*/
587 QPolygonF mBottomTriangle;
588
593 void setColorFromPoint( QPointF point );
594
595};
596
597
604class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
605{
606 Q_OBJECT
607
608 public:
609
615 QgsColorSliderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red );
616
617 void setComponent( ColorComponent component ) override;
618 void setComponentValue( int value ) override;
619 void setColor( const QColor &color, bool emitSignals = false ) override;
620
621 private:
622
623 /*Color ramp widget*/
624 QgsColorRampWidget *mRampWidget = nullptr;
625
626 /*Spin box widget*/
627 QSpinBox *mSpinBox = nullptr;
628
636 int convertRealToDisplay( int realValue ) const;
637
644 int convertDisplayToReal( int displayValue ) const;
645
646 private slots:
647
651 void rampColorChanged( const QColor &color );
652
656 void spinChanged( int value );
657
661 void rampChanged( int value );
662
663};
664
665
673class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
674{
675 Q_OBJECT
676
677 public:
678
683 {
684 HexRgb = 0,
687 Rgba
688 };
689 Q_ENUM( ColorTextFormat )
690
691
695 QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
696
697 void setColor( const QColor &color, bool emitSignals = false ) override;
698
704 void setAllowOpacity( bool allowOpacity );
705
706 protected:
707 void resizeEvent( QResizeEvent *event ) override;
708
709 private:
710
711 QLineEdit *mLineEdit = nullptr;
712
713 /*Drop-down menu button*/
714 QToolButton *mMenuButton = nullptr;
715
716 /*Display format for colors*/
717 ColorTextFormat mFormat = QgsColorTextWidget::HexRgb;
718
719 bool mAllowAlpha = true;
720
724 void updateText();
725
726 private slots:
727
731 void textChanged();
732
736 void showMenu();
737};
738
739
746class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
747{
748 Q_OBJECT
749
750 public:
751
756 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
757
758 void paintEvent( QPaintEvent *event ) override;
759 QSize sizeHint() const override;
760
768 QColor color2() const { return mColor2; }
769
770 public slots:
771
779 virtual void setColor2( const QColor &color );
780
781 protected:
782
783 //reimplemented to allow dragging colors
784 void mousePressEvent( QMouseEvent *e ) override;
785
786 //reimplemented to click colors
787 void mouseReleaseEvent( QMouseEvent *e ) override;
788
789 //reimplemented to allow dragging colors
790 void mouseMoveEvent( QMouseEvent *e ) override;
791
792 private:
793
794 /*Secondary color for widget*/
795 QColor mColor2;
796
797 QPoint mDragStartPosition;
798
799 /*Draws a color preview within the specified rect.
800 * \param color color to draw
801 * \param rect rect to draw color in
802 * \param painter destination painter
803 */
804 void drawColor( const QColor &color, QRect rect, QPainter &painter );
805};
806
807#endif // #ifndef QGSCOLORWIDGETS_H
A color box widget.
A preview box which displays one or two colors as swatches.
QColor color2() const
Returns the secondary color for the widget.
A color ramp widget.
int interiorMargin() const
Fetches the margin between the edge of the widget and the ramp.
Orientation orientation() const
Fetches the orientation for the color ramp.
void valueChanged(int value)
Emitted when the widget's color component value changes.
Orientation
Specifies the orientation of a color ramp.
@ Horizontal
Horizontal ramp.
bool showFrame() const
Fetches whether the ramp is drawn within a frame.
A composite horizontal color ramp widget and associated spinbox for manual value entry.
A line edit widget which displays colors as text and accepts string representations of colors.
ColorTextFormat
Specifies the display format for a color.
@ Rgb
Rgb( r, g, b ) format.
@ HexRgbA
#RRGGBBAA in hexadecimal, with alpha
@ HexRgb
#RRGGBB in hexadecimal
A color wheel widget.
~QgsColorWheel() override
An action containing a color widget, which can be embedded into a menu.
void setDismissOnColorSelection(bool dismiss)
Sets whether the parent menu should be dismissed and closed when a color is selected from the action'...
QgsColorWidget * colorWidget()
Returns the color widget contained in the widget action.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
bool dismissOnColorSelection() const
Returns whether the parent menu will be dismissed after a color is selected from the action's color w...
A base class for interactive color widgets.
void hovered()
Emitted when mouse hovers over widget.
ColorComponent component() const
Returns the color component which the widget controls.
void colorChanged(const QColor &color)
Emitted when the widget's color changes.
ColorComponent mComponent
ColorComponent
Specifies the color component which the widget alters.
@ Hue
Hue component of color (based on HSV model)
@ Green
Green component of color.
@ Red
Red component of color.
@ Saturation
Saturation component of color (based on HSV model)
@ Blue
Blue component of color.
@ Value
Value component of color (based on HSV model)
QList< QPair< QRgb, int > > QgsColorBox
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53