QGIS API Documentation 3.39.0-Master (47f7b3a4989)
Loading...
Searching...
No Matches
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
62
68 QgsColorWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Multiple );
69
75 QColor color() const;
76
82 ColorComponent component() const { return mComponent; }
83
91 int componentValue() const;
92
97 static QPixmap createDragIcon( const QColor &color );
98
99 public slots:
100
107 virtual void setColor( const QColor &color, bool emitSignals = false );
108
114 virtual void setComponent( QgsColorWidget::ColorComponent component );
115
125 virtual void setComponentValue( int value );
126
127 signals:
128
133 void colorChanged( const QColor &color );
134
138 void hovered();
139
140 protected:
141
143
145
150 int mExplicitHue = 0;
151
156 int componentRange() const;
157
162 static int componentRange( ColorComponent component );
163
171 int componentValue( ColorComponent component ) const;
172
178 int hue() const;
179
187 static void alterColor( QColor &color, QgsColorWidget::ColorComponent component, int newValue );
188
192 QColor::Spec colorSpec() const;
193
197 static QColor::Spec colorSpec( QgsColorWidget::ColorComponent component );
198
203 static const QPixmap &transparentBackground();
204
205 //Reimplemented to accept dragged colors
206 void dragEnterEvent( QDragEnterEvent *e ) override;
207
208 //Reimplemented to accept dropped colors
209 void dropEvent( QDropEvent *e ) override;
210
211 void mouseMoveEvent( QMouseEvent *e ) override;
212 void mousePressEvent( QMouseEvent *e ) override;
213 void mouseReleaseEvent( QMouseEvent *e ) override;
214
215 friend class TestQgsCompoundColorWidget;
216};
217
218
226class GUI_EXPORT QgsColorWidgetAction: public QWidgetAction
227{
228 Q_OBJECT
229
230 public:
231
238 QgsColorWidgetAction( QgsColorWidget *colorWidget, QMenu *menu = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
239
243 QgsColorWidget *colorWidget() { return mColorWidget; }
244
253 void setDismissOnColorSelection( bool dismiss ) { mDismissOnColorSelection = dismiss; }
254
260 bool dismissOnColorSelection() const { return mDismissOnColorSelection; }
261
262 signals:
263
268 void colorChanged( const QColor &color );
269
270 private:
271 QMenu *mMenu = nullptr;
272 QgsColorWidget *mColorWidget = nullptr;
273
274 //used to suppress recursion with hover events
275 bool mSuppressRecurse;
276
277 bool mDismissOnColorSelection;
278
279 private slots:
280
284 void onHover();
285
289 void setColor( const QColor &color );
290};
291
292
293
301class GUI_EXPORT QgsColorWheel : public QgsColorWidget
302{
303 Q_OBJECT
304
305 public:
306
311 QgsColorWheel( QWidget *parent SIP_TRANSFERTHIS = nullptr );
312
313 ~QgsColorWheel() override;
314
315 QSize sizeHint() const override;
316 void paintEvent( QPaintEvent *event ) override;
317
318 public slots:
319
320 void setColor( const QColor &color, bool emitSignals = false ) override;
321
322 protected:
323
324 void resizeEvent( QResizeEvent *event ) override;
325 void mouseMoveEvent( QMouseEvent *event ) override;
326 void mousePressEvent( QMouseEvent *event ) override;
327 void mouseReleaseEvent( QMouseEvent *event ) override;
328
329 private:
330
331 enum ControlPart
332 {
333 None,
334 Wheel,
335 Triangle
336 };
337
338 bool mIsDragging = false;
339
340 /*Margin between outer ring and edge of widget*/
341 int mMargin = 4;
342
343 /*Thickness of hue ring in pixels*/
344 int mWheelThickness = 18;
345
346 /*Part of the wheel where the mouse was originally depressed*/
347 ControlPart mClickedPart = QgsColorWheel::None;
348
349 /*Cached image of hue wheel*/
350 QImage mWheelImage;
351
352 /*Cached image of inner triangle*/
353 QImage mTriangleImage;
354
355 /*Resuable, temporary image for drawing widget*/
356 QImage mWidgetImage;
357
358 /*Whether the color wheel image requires redrawing*/
359 bool mWheelDirty = true;
360
361 /*Whether the inner triangle image requires redrawing*/
362 bool mTriangleDirty = true;
363
364 /*Conical gradient brush used for drawing hue wheel*/
365 QBrush mWheelBrush;
366
371 void createImages( QSizeF size );
372
374 void createWheel();
375
377 void createTriangle();
378
383 void setColorFromPos( QPointF pos );
384
385};
386
387
396class GUI_EXPORT QgsColorBox : public QgsColorWidget
397{
398 Q_OBJECT
399
400 public:
401
409 QgsColorBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = Value );
410
411 ~QgsColorBox() override;
412
413 QSize sizeHint() const override;
414 void paintEvent( QPaintEvent *event ) override;
415
416 void setComponent( ColorComponent component ) override;
417
418 public slots:
419 void setColor( const QColor &color, bool emitSignals = false ) override;
420
421 protected:
422
423 void resizeEvent( QResizeEvent *event ) override;
424 void mouseMoveEvent( QMouseEvent *event ) override;
425 void mousePressEvent( QMouseEvent *event ) override;
426 void mouseReleaseEvent( QMouseEvent *event ) override;
427
428 private:
429
430 bool mIsDragging = false;
431
432 /*Margin between outer ring and edge of widget*/
433 int mMargin = 2;
434
435 /*Cached image for color box*/
436 QImage *mBoxImage = nullptr;
437
438 /*Whether the cached image requires redrawing*/
439 bool mDirty = true;
440
444 void createBox();
445
450 int valueRangeX() const;
451
456 int valueRangeY() const;
457
461 QgsColorWidget::ColorComponent yComponent() const;
462
466 int yComponentValue() const;
467
471 QgsColorWidget::ColorComponent xComponent() const;
472
476 int xComponentValue() const;
477
482 void setColorFromPoint( QPoint point );
483
484};
485
486
494class GUI_EXPORT QgsColorRampWidget : public QgsColorWidget
495{
496 Q_OBJECT
497
498 public:
499
504 {
505 Horizontal = 0,
506 Vertical
507 };
508
515 QgsColorRampWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr,
516 ColorComponent component = QgsColorWidget::Red,
517 Orientation orientation = QgsColorRampWidget::Horizontal );
518
519 QSize sizeHint() const override;
520 void paintEvent( QPaintEvent *event ) override;
521
527 void setOrientation( Orientation orientation );
528
534 Orientation orientation() const { return mOrientation; }
535
541 void setInteriorMargin( int margin );
542
548 int interiorMargin() const { return mMargin; }
549
555 void setShowFrame( bool showFrame );
556
562 bool showFrame() const { return mShowFrame; }
563
568 void setMarkerSize( int markerSize );
569
570 signals:
571
576 void valueChanged( int value );
577
578 protected:
579
580 void mouseMoveEvent( QMouseEvent *event ) override;
581 void wheelEvent( QWheelEvent *event ) override;
582 void mousePressEvent( QMouseEvent *event ) override;
583 void mouseReleaseEvent( QMouseEvent *event ) override;
584 void keyPressEvent( QKeyEvent *event ) override;
585
586 private:
587
588 bool mIsDragging = false;
589
590 /*Orientation for ramp*/
591 Orientation mOrientation;
592
593 /*Margin around ramp*/
594 int mMargin = 4;
595
596 /*Whether to draw a frame around the ramp*/
597 bool mShowFrame = false;
598
599 /*Polygon for upper triangle marker*/
600 QPolygonF mTopTriangle;
601
602 /*Polygon for lower triangle marker*/
603 QPolygonF mBottomTriangle;
604
609 void setColorFromPoint( QPointF point );
610
611};
612
613
620class GUI_EXPORT QgsColorSliderWidget : public QgsColorWidget
621{
622 Q_OBJECT
623
624 public:
625
631 QgsColorSliderWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, ColorComponent component = QgsColorWidget::Red );
632
633 void setComponent( ColorComponent component ) override;
634 void setComponentValue( int value ) override;
635 void setColor( const QColor &color, bool emitSignals = false ) override;
636
637 private:
638
639 /*Color ramp widget*/
640 QgsColorRampWidget *mRampWidget = nullptr;
641
642 /*Spin box widget*/
643 QSpinBox *mSpinBox = nullptr;
644
652 int convertRealToDisplay( int realValue ) const;
653
660 int convertDisplayToReal( int displayValue ) const;
661
662 private slots:
663
667 void rampColorChanged( const QColor &color );
668
672 void spinChanged( int value );
673
677 void rampChanged( int value );
678
679};
680
681
689class GUI_EXPORT QgsColorTextWidget : public QgsColorWidget
690{
691 Q_OBJECT
692
693 public:
694
699 {
700 HexRgb = 0,
703 Rgba
704 };
705 Q_ENUM( ColorTextFormat )
706
707
711 QgsColorTextWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
712
713 void setColor( const QColor &color, bool emitSignals = false ) override;
714
720 void setAllowOpacity( bool allowOpacity );
721
722 protected:
723 void resizeEvent( QResizeEvent *event ) override;
724
725 private:
726
727 QLineEdit *mLineEdit = nullptr;
728
729 /*Drop-down menu button*/
730 QToolButton *mMenuButton = nullptr;
731
732 /*Display format for colors*/
733 ColorTextFormat mFormat = QgsColorTextWidget::HexRgb;
734
735 bool mAllowAlpha = true;
736
740 void updateText();
741
742 private slots:
743
747 void textChanged();
748
752 void showMenu();
753};
754
755
762class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
763{
764 Q_OBJECT
765
766 public:
767
772 QgsColorPreviewWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );
773
774 void paintEvent( QPaintEvent *event ) override;
775 QSize sizeHint() const override;
776
784 QColor color2() const { return mColor2; }
785
786 public slots:
787
795 virtual void setColor2( const QColor &color );
796
797 protected:
798
799 //reimplemented to allow dragging colors
800 void mousePressEvent( QMouseEvent *e ) override;
801
802 //reimplemented to click colors
803 void mouseReleaseEvent( QMouseEvent *e ) override;
804
805 //reimplemented to allow dragging colors
806 void mouseMoveEvent( QMouseEvent *e ) override;
807
808 private:
809
810 /*Secondary color for widget*/
811 QColor mColor2;
812
813 QPoint mDragStartPosition;
814
815 /*Draws a color preview within the specified rect.
816 * \param color color to draw
817 * \param rect rect to draw color in
818 * \param painter destination painter
819 */
820 void drawColor( const QColor &color, QRect rect, QPainter &painter );
821};
822
823#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)
@ Alpha
Alpha component (opacity) of color.
@ Green
Green component of color.
@ Red
Red component of color.
@ Saturation
Saturation component of color (based on HSV model)
@ Magenta
Magenta component (based on CMYK model) of color.
@ Yellow
Yellow component (based on CMYK model) of color.
@ Cyan
Cyan component (based on CMYK model) of color.
@ Blue
Blue component of color.
@ Value
Value component of color (based on HSV model)
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53