QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
39class GUI_EXPORT QgsColorWidget : public QWidget
40{
41 Q_OBJECT
42
43 public:
44
49 {
50 Multiple = 0,
57 Alpha
58 };
59
65 QgsColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Multiple );
66
72 QColor color() const;
73
79 ColorComponent component() const { return mComponent; }
80
88 int componentValue() const;
89
94 static QPixmap createDragIcon( const QColor &color );
95
96 public slots:
97
104 virtual void setColor( const QColor &color, bool emitSignals = false );
105
111 virtual void setComponent( QgsColorWidget::ColorComponent component );
112
122 virtual void setComponentValue( int value );
123
124 signals:
125
130 void colorChanged( const QColor &color );
131
136 void hovered();
137
138 protected:
139
141
143
148 int mExplicitHue = 0;
149
154 int componentRange() const;
155
160 int componentRange( ColorComponent component ) const;
161
169 int componentValue( ColorComponent component ) const;
170
176 int hue() const;
177
185 void alterColor( QColor &color, QgsColorWidget::ColorComponent component, int newValue ) const;
186
191 static const QPixmap &transparentBackground();
192
193 //Reimplemented to accept dragged colors
194 void dragEnterEvent( QDragEnterEvent *e ) override;
195
196 //Reimplemented to accept dropped colors
197 void dropEvent( QDropEvent *e ) override;
198
199 void mouseMoveEvent( QMouseEvent *e ) override;
200 void mousePressEvent( QMouseEvent *e ) override;
201 void mouseReleaseEvent( QMouseEvent *e ) override;
202};
203
204
213class GUI_EXPORT QgsColorWidgetAction: public QWidgetAction
214{
215 Q_OBJECT
216
217 public:
218
225 QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
226
230 QgsColorWidget *colorWidget() { return mColorWidget; }
231
240 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
241
247 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
248
249 signals:
250
255 void colorChanged( const QColor &color );
256
257 private:
258 QMenu *mMenu = nullptr;
259 QgsColorWidget *mColorWidget = nullptr;
260
261 //used to suppress recursion with hover events
262 bool mSuppressRecurse;
263
264 bool mDismissOnColorSelection;
265
266 private slots:
267
271 void onHover();
272
276 void setColor( const QColor &color );
277};
278
279
280
289class GUI_EXPORT QgsColorWheel : public QgsColorWidget
290{
291 Q_OBJECT
292
293 public:
294
299 QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
300
301 ~QgsColorWheel() override;
302
303 QSize sizeHint() const override;
304 void paintEvent( QPaintEvent *event ) override;
305
306 public slots:
307
308 void setColor( const QColor &color, bool emitSignals = false ) override;
309
310 protected:
311
312 void resizeEvent( QResizeEvent *event ) override;
313 void mouseMoveEvent( QMouseEvent *event ) override;
314 void mousePressEvent( QMouseEvent *event ) override;
315 void mouseReleaseEvent( QMouseEvent *event ) override;
316
317 private:
318
319 enum ControlPart
320 {
321 None,
322 Wheel,
323 Triangle
324 };
325
326 bool mIsDragging = false;
327
328 /*Margin between outer ring and edge of widget*/
329 int mMargin = 4;
330
331 /*Thickness of hue ring in pixels*/
332 int mWheelThickness = 18;
333
334 /*Part of the wheel where the mouse was originally depressed*/
335 ControlPart mClickedPart = QgsColorWheel::None;
336
337 /*Cached image of hue wheel*/
338 QImage *mWheelImage = nullptr;
339
340 /*Cached image of inner triangle*/
341 QImage *mTriangleImage = nullptr;
342
343 /*Resuable, temporary image for drawing widget*/
344 QImage *mWidgetImage = nullptr;
345
346 /*Whether the color wheel image requires redrawing*/
347 bool mWheelDirty = true;
348
349 /*Whether the inner triangle image requires redrawing*/
350 bool mTriangleDirty = true;
351
352 /*Conical gradient brush used for drawing hue wheel*/
353 QBrush mWheelBrush;
354
359 void createImages( QSizeF size );
360
362 void createWheel();
363
365 void createTriangle();
366
371 void setColorFromPos( QPointF pos );
372
373};
374
375
385class GUI_EXPORT QgsColorBox : public QgsColorWidget
386{
387 Q_OBJECT
388
389 public:
390
398 QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
399
400 ~QgsColorBox() override;
401
402 QSize sizeHint() const override;
403 void paintEvent( QPaintEvent *event ) override;
404
405 void setComponent( ColorComponent component ) override;
406
407 public slots:
408 void setColor( const QColor &color, bool emitSignals = false ) override;
409
410 protected:
411
412 void resizeEvent( QResizeEvent *event ) override;
413 void mouseMoveEvent( QMouseEvent *event ) override;
414 void mousePressEvent( QMouseEvent *event ) override;
415 void mouseReleaseEvent( QMouseEvent *event ) override;
416
417 private:
418
419 bool mIsDragging = false;
420
421 /*Margin between outer ring and edge of widget*/
422 int mMargin = 2;
423
424 /*Cached image for color box*/
425 QImage *mBoxImage = nullptr;
426
427 /*Whether the cached image requires redrawing*/
428 bool mDirty = true;
429
433 void createBox();
434
439 int valueRangeX() const;
440
445 int valueRangeY() const;
446
450 QgsColorWidget::ColorComponent yComponent() const;
451
455 int yComponentValue() const;
456
460 QgsColorWidget::ColorComponent xComponent() const;
461
465 int xComponentValue() const;
466
471 void setColorFromPoint( QPoint point );
472
473};
474
475
484class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
485{
486 Q_OBJECT
487
488 public:
489
494 {
495 Horizontal = 0,
496 Vertical
497 };
498
505 QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr,
506 ColorComponent component = QgsColorWidget::Red,
507 Orientation orientation = QgsColorRampWidget::Horizontal );
508
509 QSize sizeHint() const override;
510 void paintEvent( QPaintEvent *event ) override;
511
517 void setOrientation( Orientation orientation );
518
524 Orientation orientation() const { return mOrientation; }
525
531 void setInteriorMargin( int margin );
532
538 int interiorMargin() const { return mMargin; }
539
545 void setShowFrame( bool showFrame );
546
552 bool showFrame() const { return mShowFrame; }
553
558 void setMarkerSize( int markerSize );
559
560 signals:
561
566 void valueChanged( int value );
567
568 protected:
569
570 void mouseMoveEvent( QMouseEvent *event ) override;
571 void wheelEvent( QWheelEvent *event ) override;
572 void mousePressEvent( QMouseEvent *event ) override;
573 void mouseReleaseEvent( QMouseEvent *event ) override;
574 void keyPressEvent( QKeyEvent *event ) override;
575
576 private:
577
578 bool mIsDragging = false;
579
580 /*Orientation for ramp*/
581 Orientation mOrientation;
582
583 /*Margin around ramp*/
584 int mMargin = 4;
585
586 /*Whether to draw a frame around the ramp*/
587 bool mShowFrame = false;
588
589 /*Polygon for upper triangle marker*/
590 QPolygonF mTopTriangle;
591
592 /*Polygon for lower triangle marker*/
593 QPolygonF mBottomTriangle;
594
599 void setColorFromPoint( QPointF point );
600
601};
602
603
611class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
612{
613 Q_OBJECT
614
615 public:
616
622 QgsColorSliderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red );
623
624 void setComponent( ColorComponent component ) override;
625 void setComponentValue( int value ) override;
626 void setColor( const QColor &color, bool emitSignals = false ) override;
627
628 private:
629
630 /*Color ramp widget*/
631 QgsColorRampWidget *mRampWidget = nullptr;
632
633 /*Spin box widget*/
634 QSpinBox *mSpinBox = nullptr;
635
643 int convertRealToDisplay( int realValue ) const;
644
651 int convertDisplayToReal( int displayValue ) const;
652
653 private slots:
654
658 void rampColorChanged( const QColor &color );
659
663 void spinChanged( int value );
664
668 void rampChanged( int value );
669
670};
671
672
681class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
682{
683 Q_OBJECT
684
685 public:
686
691 {
692 HexRgb = 0,
695 Rgba
696 };
697 Q_ENUM( ColorTextFormat )
698
699
703 QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
704
705 void setColor( const QColor &color, bool emitSignals = false ) override;
706
712 void setAllowOpacity( bool allowOpacity );
713
714 protected:
715 void resizeEvent( QResizeEvent *event ) override;
716
717 private:
718
719 QLineEdit *mLineEdit = nullptr;
720
721 /*Drop-down menu button*/
722 QToolButton *mMenuButton = nullptr;
723
724 /*Display format for colors*/
725 ColorTextFormat mFormat = QgsColorTextWidget::HexRgb;
726
727 bool mAllowAlpha = true;
728
732 void updateText();
733
734 private slots:
735
739 void textChanged();
740
744 void showMenu();
745};
746
747
755class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
756{
757 Q_OBJECT
758
759 public:
760
765 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
766
767 void paintEvent( QPaintEvent *event ) override;
768 QSize sizeHint() const override;
769
777 QColor color2() const { return mColor2; }
778
779 public slots:
780
788 virtual void setColor2( const QColor &color );
789
790 protected:
791
792 //reimplemented to allow dragging colors
793 void mousePressEvent( QMouseEvent *e ) override;
794
795 //reimplemented to click colors
796 void mouseReleaseEvent( QMouseEvent *e ) override;
797
798 //reimplemented to allow dragging colors
799 void mouseMoveEvent( QMouseEvent *e ) override;
800
801 private:
802
803 /*Secondary color for widget*/
804 QColor mColor2;
805
806 QPoint mDragStartPosition;
807
808 /*Draws a color preview within the specified rect.
809 * \param color color to draw
810 * \param rect rect to draw color in
811 * \param painter destination painter
812 */
813 void drawColor( const QColor &color, QRect rect, QPainter &painter );
814};
815
816#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.
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