23#include <QResizeEvent>
25#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
26#include <QStyleOptionFrameV3>
28#include <QStyleOptionFrame>
34#include <QFontMetrics>
50 , mCurrentColor( Qt::red )
51 , mComponent( component )
53 setAcceptDrops(
true );
65 QPixmap pixmap( iconSize, iconSize );
66 pixmap.fill( Qt::transparent );
68 painter.begin( &pixmap );
70 painter.fillRect( QRect( 0, 0, iconSize, iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
72 QColor pixmapColor =
color;
73 pixmapColor.setAlpha( 255 );
74 painter.setBrush( QBrush( pixmapColor ) );
75 painter.setPen( QPen( Qt::white ) );
76 painter.drawRect( QRect( 1, 1, iconSize - 2, iconSize - 2 ) );
150 color.getHsv( &h, &s, &v, &a );
158 color.setRed( clippedValue );
161 color.setGreen( clippedValue );
164 color.setBlue( clippedValue );
167 color.setHsv( clippedValue, s, v, a );
170 color.setHsv( h, clippedValue, v, a );
173 color.setHsv( h, s, clippedValue, a );
176 color.setAlpha( clippedValue );
185 static QPixmap sTranspBkgrd;
187 if ( sTranspBkgrd.isNull() )
199 if ( mimeColor.isValid() )
202 e->acceptProposedAction();
209 bool hasAlpha =
false;
212 if ( mimeColor.isValid() )
215 e->acceptProposedAction();
274 valueClipped = std::max( valueClipped, 0 );
286 if ( r == valueClipped )
293 if ( g == valueClipped )
300 if ( b == valueClipped )
307 if ( h == valueClipped )
314 if ( s == valueClipped )
321 if ( v == valueClipped )
328 if ( a == valueClipped )
357 if (
color.hue() >= 0 )
379 QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
380 const int wheelStops = 20;
381 QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
382 for (
int pos = 0; pos <= wheelStops; ++pos )
384 const double relativePos =
static_cast<double>( pos ) / wheelStops;
385 gradColor.setHsvF( relativePos, 1, 1 );
386 wheelGradient.setColorAt( relativePos, gradColor );
388 mWheelBrush = QBrush( wheelGradient );
396 return QSize( size, size );
402 QPainter painter(
this );
404 if ( mWidgetImage.isNull() || mWheelImage.isNull() || mTriangleImage.isNull() )
406 createImages( size() );
410 mWidgetImage.fill( Qt::transparent );
411 QPainter imagePainter( &mWidgetImage );
412 imagePainter.setRenderHint( QPainter::Antialiasing );
421 const QPointF center = QPointF( mWidgetImage.width() / 2.0, mWidgetImage.height() / 2.0 );
422 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mWheelImage );
426 const double length = mWheelImage.width() / 2.0;
427 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
428 hueMarkerLine.setAngle( h );
431 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
433 pen.setWidthF( 2 * devicePixelRatioF() );
435 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
436 imagePainter.setPen( pen );
437 imagePainter.drawLine( hueMarkerLine );
438 imagePainter.restore();
441 if ( mTriangleDirty )
445 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mTriangleImage );
448 const double triangleRadius = length - mWheelThickness * devicePixelRatioF() - 1;
452 const double hueRadians = ( h * M_PI / 180.0 );
453 const double hx = std::cos( hueRadians ) * triangleRadius;
454 const double hy = -std::sin( hueRadians ) * triangleRadius;
455 const double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
456 const double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
457 const double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
458 const double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
459 const double mx = ( sx + vx ) / 2.0;
460 const double my = ( sy + vy ) / 2.0;
462 const double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
463 const double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
464 const double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
467 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
468 imagePainter.setPen( pen );
469 imagePainter.setBrush( Qt::NoBrush );
470 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0 * devicePixelRatioF(), 4.0 * devicePixelRatioF() );
474 painter.drawImage( QRectF( 0, 0, width(), height() ), mWidgetImage );
483 mTriangleDirty =
true;
489void QgsColorWheel::createImages(
const QSizeF size )
491 const double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
492 mWheelThickness = wheelSize / 15.0;
495 const double pixelRatio = devicePixelRatioF();
496 mWheelImage = QImage( wheelSize * pixelRatio,
497 wheelSize * pixelRatio, QImage::Format_ARGB32 );
498 mTriangleImage = QImage( wheelSize * pixelRatio,
499 wheelSize * pixelRatio, QImage::Format_ARGB32 );
500 mWidgetImage = QImage( size.width() * pixelRatio,
501 size.height() * pixelRatio, QImage::Format_ARGB32 );
505 mTriangleDirty =
true;
510 QgsColorWidget::resizeEvent( event );
513 if ( event->size().width() > parentWidget()->size().width() )
516 std::min( event->size().width(), parentWidget()->size().width() - 2 ),
517 std::min( event->size().height(), parentWidget()->size().height() - 2 )
520 createImages( newSize );
524 createImages( event->size() );
528 createImages( event->size() );
532void QgsColorWheel::setColorFromPos(
const QPointF pos )
534 const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
536 const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
538 QColor newColor = QColor();
545 if ( mClickedPart == QgsColorWheel::Triangle )
550 const double x = pos.x() - center.x();
551 const double y = pos.y() - center.y();
553 double eventAngleRadians = line.angle() * M_PI / 180.0;
554 const double hueRadians = h * M_PI / 180.0;
555 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
556 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
557 const double length = mWheelImage.width() / 2.0 / devicePixelRatioF();
558 const double triangleLength = length - mWheelThickness - 1;
560 const double a = 0.5 * triangleLength;
561 double b = std::tan( rad1 ) * a;
562 double r = std::sqrt( x * x + y * y );
563 const double maxR = std::sqrt( a * a + b * b );
567 const double dx = std::tan( rad1 ) * r;
568 double rad2 = std::atan( dx / maxR );
569 rad2 = std::min( rad2, M_PI / 3.0 );
570 rad2 = std::max( rad2, -M_PI / 3.0 );
571 eventAngleRadians += rad2 - rad1;
572 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
573 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
574 b = std::tan( rad1 ) * a;
575 r = std::sqrt( a * a + b * b );
578 const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
579 const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
580 const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
581 const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
582 s = std::min(
static_cast< int >( std::round( std::max( 0.0, newS ) * 255.0 ) ), 255 );
583 l = std::min(
static_cast< int >( std::round( std::max( 0.0, newL ) * 255.0 ) ), 255 );
584 newColor = QColor::fromHsl( h, s, l );
586 newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
588 else if ( mClickedPart == QgsColorWheel::Wheel )
593 const int newHue = line.angle();
594 newColor = QColor::fromHsv( newHue, s, v, alpha );
596 mTriangleDirty =
true;
618 setColorFromPos( event->pos() );
625 if ( event->button() == Qt::LeftButton )
631 const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
633 const double innerLength = mWheelImage.width() / 2.0 / devicePixelRatioF() - mWheelThickness;
634 if ( line.length() < innerLength )
636 mClickedPart = QgsColorWheel::Triangle;
640 mClickedPart = QgsColorWheel::Wheel;
642 setColorFromPos( event->pos() );
652 if ( event->button() == Qt::LeftButton )
655 mClickedPart = QgsColorWheel::None;
663void QgsColorWheel::createWheel()
665 if ( mWheelImage.isNull() )
670 const int maxSize = std::min( mWheelImage.width(), mWheelImage.height() );
671 const double wheelRadius = maxSize / 2.0;
673 mWheelImage.fill( Qt::transparent );
674 QPainter p( &mWheelImage );
675 p.setRenderHint( QPainter::Antialiasing );
676 p.setBrush( mWheelBrush );
677 p.setPen( Qt::NoPen );
680 p.translate( wheelRadius, wheelRadius );
681 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
684 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
685 p.setBrush( QBrush( Qt::black ) );
686 p.drawEllipse( QPointF( 0,
688 wheelRadius - mWheelThickness * devicePixelRatioF(),
689 wheelRadius - mWheelThickness * devicePixelRatioF() );
695void QgsColorWheel::createTriangle()
697 if ( mWheelImage.isNull() || mTriangleImage.isNull() )
702 const QPointF center = QPointF( mWheelImage.width() / 2.0, mWheelImage.height() / 2.0 );
703 mTriangleImage.fill( Qt::transparent );
705 QPainter imagePainter( &mTriangleImage );
706 imagePainter.setRenderHint( QPainter::Antialiasing );
709 const double wheelRadius = mWheelImage.width() / 2.0;
710 const double triangleRadius = wheelRadius - mWheelThickness * devicePixelRatioF() - 1;
713 const QColor pureColor = QColor::fromHsv( angle, 255, 255 );
715 QColor alphaColor = QColor( pureColor );
716 alphaColor.setAlpha( 0 );
719 QLineF line1 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() - triangleRadius * std::sin( M_PI / 3.0 ) );
720 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
721 QLineF line3 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() + triangleRadius * std::sin( M_PI / 3.0 ) );
722 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
723 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
724 line1.setAngle( line1.angle() + angle );
725 line2.setAngle( line2.angle() + angle );
726 line3.setAngle( line3.angle() + angle );
727 line4.setAngle( line4.angle() + angle );
728 line5.setAngle( line5.angle() + angle );
729 const QPointF p1 = line1.p2();
730 const QPointF p2 = line2.p2();
731 const QPointF p3 = line3.p2();
732 const QPointF p4 = line4.p2();
733 const QPointF p5 = line5.p2();
736 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
737 colorGrad.setColorAt( 0, alphaColor );
738 colorGrad.setColorAt( 1, pureColor );
739 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
740 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
741 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
744 triangle << p2 << p1 << p3 << p2;
745 imagePainter.setPen( Qt::NoPen );
747 imagePainter.setBrush( QBrush( Qt::black ) );
748 imagePainter.drawPolygon( triangle );
750 imagePainter.setBrush( QBrush( colorGrad ) );
751 imagePainter.drawPolygon( triangle );
753 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
754 imagePainter.setBrush( QBrush( whiteGrad ) );
755 imagePainter.drawPolygon( triangle );
760 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
761 imagePainter.setBrush( Qt::NoBrush );
762 imagePainter.setPen( QPen( Qt::transparent ) );
763 imagePainter.drawPolygon( triangle );
766 mTriangleDirty =
false;
778 setFocusPolicy( Qt::StrongFocus );
779 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
781 mBoxImage =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
792 return QSize( size, size );
798 QPainter painter(
this );
800 QStyleOptionFrame option;
801 option.initFrom(
this );
802 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
803 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
811 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
814 const double xPos = mMargin + ( width() - 2 * mMargin - 1 ) *
static_cast<double>( xComponentValue() ) /
static_cast<double>( valueRangeX() );
815 const double yPos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) *
static_cast<double>( yComponentValue() ) /
static_cast<double>( valueRangeY() );
817 painter.setBrush( Qt::white );
818 painter.setPen( Qt::NoPen );
820 painter.drawRect( xPos - 1, mMargin, 3, height() - 2 * mMargin - 1 );
821 painter.drawRect( mMargin, yPos - 1, width() - 2 * mMargin - 1, 3 );
822 painter.setPen( Qt::black );
823 painter.drawLine( xPos, mMargin, xPos, height() - mMargin - 1 );
824 painter.drawLine( mMargin, yPos, width() - mMargin - 1, yPos );
873 mBoxImage =
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
874 QgsColorWidget::resizeEvent( event );
881 setColorFromPoint( event->pos() );
888 if ( event->button() == Qt::LeftButton )
891 setColorFromPoint( event->pos() );
901 if ( event->button() == Qt::LeftButton )
911void QgsColorBox::createBox()
913 const int maxValueX = mBoxImage->width();
914 const int maxValueY = mBoxImage->height();
918 int colorComponentValue;
920 for (
int y = 0; y < maxValueY; ++y )
922 QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
924 colorComponentValue = int( valueRangeY() - valueRangeY() * (
double( y ) / maxValueY ) );
925 alterColor( currentColor, yComponent(), colorComponentValue );
926 for (
int x = 0; x < maxValueX; ++x )
928 colorComponentValue = int( valueRangeX() * (
double( x ) / maxValueX ) );
929 alterColor( currentColor, xComponent(), colorComponentValue );
930 scanLine[x] = currentColor.rgb();
936int QgsColorBox::valueRangeX()
const
941int QgsColorBox::valueRangeY()
const
966int QgsColorBox::yComponentValue()
const
991int QgsColorBox::xComponentValue()
const
996void QgsColorBox::setColorFromPoint( QPoint point )
998 int valX = valueRangeX() * ( point.x() - mMargin ) / ( width() - 2 * mMargin - 1 );
999 valX = std::min( std::max( valX, 0 ), valueRangeX() );
1001 int valY = valueRangeY() - valueRangeY() * ( point.y() - mMargin ) / ( height() - 2 * mMargin - 1 );
1002 valY = std::min( std::max( valY, 0 ), valueRangeY() );
1013 if (
color.hue() >= 0 )
1033 setFocusPolicy( Qt::StrongFocus );
1057 QPainter painter(
this );
1062 QStyleOptionFrame option;
1063 option.initFrom(
this );
1064 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1065 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1071 QStyleOptionFocusRect option;
1072 option.initFrom(
this );
1073 option.state = QStyle::State_KeyboardFocusChange;
1074 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1081 color.setAlpha( 255 );
1088 painter.setPen( pen );
1089 painter.setBrush( Qt::NoBrush );
1092 for (
int c = 0;
c <= maxValue; ++
c )
1094 int colorVal =
static_cast<int>(
componentRange() *
static_cast<double>(
c ) / maxValue );
1101 if (
color.hue() < 0 )
1105 pen.setColor(
color );
1106 painter.setPen( pen );
1110 painter.drawLine( QLineF(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 ) );
1115 painter.drawLine( QLineF( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin ) );
1124 painter.setBrush( checkBrush );
1125 painter.setPen( Qt::NoPen );
1126 painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
1127 QLinearGradient colorGrad;
1131 colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 );
1136 colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 );
1139 transparent.setAlpha( 0 );
1140 colorGrad.setColorAt( 0, transparent );
1142 opaque.setAlpha( 255 );
1143 colorGrad.setColorAt( 1, opaque );
1144 const QBrush colorBrush = QBrush( colorGrad );
1145 painter.setBrush( colorBrush );
1146 painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
1152 painter.setRenderHint( QPainter::Antialiasing );
1153 painter.setBrush( QBrush( Qt::black ) );
1154 painter.setPen( Qt::NoPen );
1156 painter.drawPolygon( mTopTriangle );
1157 painter.translate( 0, height() - mMargin - 2 );
1158 painter.setBrush( QBrush( Qt::white ) );
1159 painter.drawPolygon( mBottomTriangle );
1165 const double ypos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) *
static_cast<double>(
componentValue() ) /
componentRange();
1166 painter.setBrush( Qt::white );
1167 painter.setPen( Qt::NoPen );
1168 painter.drawRect( QRectF( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 ) );
1169 painter.setPen( Qt::black );
1170 painter.drawLine( QLineF( mMargin, ypos, width() - mMargin - 1, ypos ) );
1180 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1185 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1192 if ( margin == mMargin )
1213 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1214 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1222 setColorFromPoint( event->pos() );
1232 if ( event->angleDelta().y() > 0 )
1253 if ( event->button() == Qt::LeftButton )
1256 setColorFromPoint( event->pos() );
1266 if ( event->button() == Qt::LeftButton )
1268 mIsDragging =
false;
1312 QgsColorWidget::keyPressEvent( event );
1324void QgsColorRampWidget::setColorFromPoint( QPointF point )
1330 val =
componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin );
1355 QHBoxLayout *hLayout =
new QHBoxLayout();
1356 hLayout->setContentsMargins( 0, 0, 0, 0 );
1357 hLayout->setSpacing( 5 );
1361 hLayout->addWidget( mRampWidget, 1 );
1363 mSpinBox =
new QSpinBox();
1365 const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( QStringLiteral(
"888%" ) );
1366 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1367 mSpinBox->setMinimum( 0 );
1368 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1373 mSpinBox->setSuffix( QChar( 176 ) );
1377 mSpinBox->setSuffix( tr(
"%" ) );
1379 hLayout->addWidget( mSpinBox );
1380 setLayout( hLayout );
1384 connect( mSpinBox,
static_cast < void ( QSpinBox::* )(
int )
> ( &QSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1391 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1395 mSpinBox->setSuffix( QChar( 176 ) );
1400 mSpinBox->setSuffix( tr(
"%" ) );
1405 mSpinBox->setSuffix( QString() );
1412 mRampWidget->blockSignals(
true );
1414 mRampWidget->blockSignals(
false );
1415 mSpinBox->blockSignals(
true );
1416 mSpinBox->setValue( convertRealToDisplay( value ) );
1417 mSpinBox->blockSignals(
false );
1424 mSpinBox->blockSignals(
true );
1426 mSpinBox->blockSignals(
false );
1429void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1434void QgsColorSliderWidget::spinChanged(
int value )
1436 const int convertedValue = convertDisplayToReal( value );
1442void QgsColorSliderWidget::rampChanged(
int value )
1444 mSpinBox->blockSignals(
true );
1445 mSpinBox->setValue( convertRealToDisplay( value ) );
1446 mSpinBox->blockSignals(
false );
1450int QgsColorSliderWidget::convertRealToDisplay(
const int realValue )
const
1456 return std::round( 100.0 * realValue / 255.0 );
1463int QgsColorSliderWidget::convertDisplayToReal(
const int displayValue )
const
1468 return std::round( 255.0 * displayValue / 100.0 );
1472 return displayValue;
1482 QHBoxLayout *hLayout =
new QHBoxLayout();
1483 hLayout->setContentsMargins( 0, 0, 0, 0 );
1484 hLayout->setSpacing( 0 );
1486 mLineEdit =
new QLineEdit(
nullptr );
1487 hLayout->addWidget( mLineEdit );
1489 mMenuButton =
new QToolButton( mLineEdit );
1491 mMenuButton->setCursor( Qt::ArrowCursor );
1492 mMenuButton->setFocusPolicy( Qt::NoFocus );
1493 mMenuButton->setStyleSheet( QStringLiteral(
"QToolButton { border: none; padding: 0px; }" ) );
1495 setLayout( hLayout );
1497 const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1498 mLineEdit->setStyleSheet( QStringLiteral(
"QLineEdit { padding-right: %1px; } " )
1499 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1501 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1502 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1506 mFormat = settings.
enumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ),
HexRgb );
1520 const QSize sz = mMenuButton->sizeHint();
1521 const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1522 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
1523 ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1526void QgsColorTextWidget::updateText()
1545void QgsColorTextWidget::textChanged()
1547 const QString testString = mLineEdit->text();
1550 if ( !
color.isValid() )
1561 if ( !containsAlpha )
1572void QgsColorTextWidget::showMenu()
1574 QMenu colorContextMenu;
1575 QAction *hexRgbaAction =
nullptr;
1576 QAction *rgbaAction =
nullptr;
1578 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1579 colorContextMenu.addAction( hexRgbAction );
1582 hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1583 colorContextMenu.addAction( hexRgbaAction );
1585 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1586 colorContextMenu.addAction( rgbAction );
1589 rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1590 colorContextMenu.addAction( rgbaAction );
1593 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1594 if ( selectedAction == hexRgbAction )
1598 else if ( hexRgbaAction && selectedAction == hexRgbaAction )
1602 else if ( selectedAction == rgbAction )
1606 else if ( rgbaAction && selectedAction == rgbaAction )
1613 settings.
setEnumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ), mFormat );
1620 mAllowAlpha = allowOpacity;
1629 , mColor2( QColor() )
1634void QgsColorPreviewWidget::drawColor(
const QColor &color, QRect rect, QPainter &painter )
1636 painter.setPen( Qt::NoPen );
1638 if (
color.alpha() < 255 )
1641 painter.setBrush( checkBrush );
1642 painter.drawRect( rect );
1647 const QBrush colorBrush = QBrush(
color );
1648 painter.setBrush( colorBrush );
1649 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1651 QColor opaqueColor = QColor(
color );
1652 opaqueColor.setAlpha( 255 );
1653 const QBrush opaqueBrush = QBrush( opaqueColor );
1654 painter.setBrush( opaqueBrush );
1655 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1660 const QBrush brush = QBrush(
color );
1661 painter.setBrush( brush );
1662 painter.drawRect( rect );
1669 QPainter painter(
this );
1671 if ( mColor2.isValid() )
1674 const int verticalSplit = std::round( height() / 2.0 );
1675 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1676 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1680 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1693 if (
color == mColor2 )
1703 if ( e->button() == Qt::LeftButton )
1705 mDragStartPosition = e->pos();
1712 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1721 if ( mColor2.isValid() )
1724 const int verticalSplit = std::round( height() / 2.0 );
1725 if ( mDragStartPosition.y() >= verticalSplit )
1727 clickedColor = mColor2;
1738 if ( !( e->buttons() & Qt::LeftButton ) )
1745 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1756 if ( mColor2.isValid() )
1759 const int verticalSplit = std::round( height() / 2.0 );
1760 if ( mDragStartPosition.y() >= verticalSplit )
1762 dragColor = mColor2;
1766 QDrag *drag =
new QDrag(
this );
1769 drag->exec( Qt::CopyAction );
1778 : QWidgetAction( parent )
1780 , mColorWidget( colorWidget )
1781 , mSuppressRecurse( false )
1782 , mDismissOnColorSelection( true )
1784 setDefaultWidget( mColorWidget );
1787 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1791void QgsColorWidgetAction::onHover()
1794 if ( mSuppressRecurse )
1801 mSuppressRecurse =
true;
1802 mMenu->setActiveAction(
this );
1803 mSuppressRecurse =
false;
1807void QgsColorWidgetAction::setColor(
const QColor &color )
1810 if ( mMenu && mDismissOnColorSelection )
static const double UI_SCALE_FACTOR
UI scaling factor.
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QSize sizeHint() const override
void resizeEvent(QResizeEvent *event) override
void mouseReleaseEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
void setComponent(ColorComponent component) override
Sets the color component which the widget controls.
void setColor(const QColor &color, bool emitSignals=false) override
void mouseMoveEvent(QMouseEvent *event) override
void paintEvent(QPaintEvent *event) override
QgsColorBox(QWidget *parent=nullptr, ColorComponent component=Value)
Construct a new color box widget.
QgsColorTextWidget(QWidget *parent=nullptr)
Construct a new color line edit widget.
@ Rgba
Rgba( r, g, b, a ) format, with alpha.
@ Rgb
Rgb( r, g, b ) format.
@ HexRgbA
#RRGGBBAA in hexadecimal, with alpha
@ HexRgb
#RRGGBB in hexadecimal
void setColor(const QColor &color, bool emitSignals=false) override
Sets the color for the widget.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted.
void resizeEvent(QResizeEvent *event) override
void paintEvent(QPaintEvent *event) override
QgsColorWheel(QWidget *parent=nullptr)
Constructs a new color wheel widget.
void mousePressEvent(QMouseEvent *event) override
QSize sizeHint() const override
void mouseReleaseEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void resizeEvent(QResizeEvent *event) override
void setColor(const QColor &color, bool emitSignals=false) override
~QgsColorWheel() override
This class is a composition of two QSettings instances:
void setEnumValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on an enum.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
static QColor parseColorWithAlpha(const QString &colorStr, bool &containsAlpha, bool strictEval=false)
Attempts to parse a string as a color using a variety of common formats, including hex codes,...
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c