23 #include <QResizeEvent>
25 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
26 #include <QStyleOptionFrameV3>
28 #include <QStyleOptionFrame>
31 #include <QHBoxLayout>
34 #include <QFontMetrics>
35 #include <QToolButton>
50 , mCurrentColor( Qt::red )
51 , mComponent( component )
53 setAcceptDrops(
true );
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 ) );
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 );
394 delete mTriangleImage;
401 return QSize( size, size );
407 QPainter painter(
this );
409 if ( !mWidgetImage || !mWheelImage || !mTriangleImage )
411 createImages( size() );
415 mWidgetImage->fill( Qt::transparent );
416 QPainter imagePainter( mWidgetImage );
417 imagePainter.setRenderHint( QPainter::Antialiasing );
426 const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
427 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage );
431 const double length = mWheelImage->width() / 2.0;
432 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
433 hueMarkerLine.setAngle( h );
436 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
440 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
441 imagePainter.setPen( pen );
442 imagePainter.drawLine( hueMarkerLine );
443 imagePainter.restore();
446 if ( mTriangleDirty )
450 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage );
453 const double triangleRadius = length - mWheelThickness - 1;
457 const double hueRadians = ( h * M_PI / 180.0 );
458 const double hx = std::cos( hueRadians ) * triangleRadius;
459 const double hy = -std::sin( hueRadians ) * triangleRadius;
460 const double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
461 const double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
462 const double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
463 const double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
464 const double mx = ( sx + vx ) / 2.0;
465 const double my = ( sy + vy ) / 2.0;
467 const double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
468 const double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
469 const double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
472 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
473 imagePainter.setPen( pen );
474 imagePainter.setBrush( Qt::NoBrush );
475 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0, 4.0 );
479 painter.drawImage( QPoint( 0, 0 ), *mWidgetImage );
488 mTriangleDirty =
true;
494 void QgsColorWheel::createImages(
const QSizeF size )
496 const double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
497 mWheelThickness = wheelSize / 15.0;
501 mWheelImage =
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
502 delete mTriangleImage;
503 mTriangleImage =
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
505 mWidgetImage =
new QImage( size.width(), size.height(), QImage::Format_ARGB32 );
509 mTriangleDirty =
true;
515 createImages( event->size() );
516 QgsColorWidget::resizeEvent( event );
519 void QgsColorWheel::setColorFromPos(
const QPointF pos )
521 const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
523 const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
525 QColor newColor = QColor();
532 if ( mClickedPart == QgsColorWheel::Triangle )
537 const double x = pos.x() - center.x();
538 const double y = pos.y() - center.y();
540 double eventAngleRadians = line.angle() * M_PI / 180.0;
541 const double hueRadians = h * M_PI / 180.0;
542 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
543 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
544 const double length = mWheelImage->width() / 2.0;
545 const double triangleLength = length - mWheelThickness - 1;
547 const double a = 0.5 * triangleLength;
548 double b = std::tan( rad1 ) * a;
549 double r = std::sqrt( x * x + y * y );
550 const double maxR = std::sqrt( a * a + b * b );
554 const double dx = std::tan( rad1 ) * r;
555 double rad2 = std::atan( dx / maxR );
556 rad2 = std::min( rad2, M_PI / 3.0 );
557 rad2 = std::max( rad2, -M_PI / 3.0 );
558 eventAngleRadians += rad2 - rad1;
559 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
560 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
561 b = std::tan( rad1 ) * a;
562 r = std::sqrt( a * a + b * b );
565 const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
566 const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
567 const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
568 const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
569 s = std::min(
static_cast< int >( std::round( std::max( 0.0, newS ) * 255.0 ) ), 255 );
570 l = std::min(
static_cast< int >( std::round( std::max( 0.0, newL ) * 255.0 ) ), 255 );
571 newColor = QColor::fromHsl( h, s, l );
573 newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
575 else if ( mClickedPart == QgsColorWheel::Wheel )
580 const int newHue = line.angle();
581 newColor = QColor::fromHsv( newHue, s, v, alpha );
583 mTriangleDirty =
true;
605 setColorFromPos( event->pos() );
612 if ( event->button() == Qt::LeftButton )
618 const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
620 const double innerLength = mWheelImage->width() / 2.0 - mWheelThickness;
621 if ( line.length() < innerLength )
623 mClickedPart = QgsColorWheel::Triangle;
627 mClickedPart = QgsColorWheel::Wheel;
629 setColorFromPos( event->pos() );
639 if ( event->button() == Qt::LeftButton )
642 mClickedPart = QgsColorWheel::None;
650 void QgsColorWheel::createWheel()
657 const int maxSize = std::min( mWheelImage->width(), mWheelImage->height() );
658 const double wheelRadius = maxSize / 2.0;
660 mWheelImage->fill( Qt::transparent );
661 QPainter p( mWheelImage );
662 p.setRenderHint( QPainter::Antialiasing );
663 p.setBrush( mWheelBrush );
664 p.setPen( Qt::NoPen );
667 p.translate( wheelRadius, wheelRadius );
668 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
671 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
672 p.setBrush( QBrush( Qt::black ) );
673 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius - mWheelThickness, wheelRadius - mWheelThickness );
679 void QgsColorWheel::createTriangle()
681 if ( !mWheelImage || !mTriangleImage )
686 const QPointF center = QPointF( mWheelImage->width() / 2.0, mWheelImage->height() / 2.0 );
687 mTriangleImage->fill( Qt::transparent );
689 QPainter imagePainter( mTriangleImage );
690 imagePainter.setRenderHint( QPainter::Antialiasing );
693 const double wheelRadius = mWheelImage->width() / 2.0;
694 const double triangleRadius = wheelRadius - mWheelThickness - 1;
697 const QColor pureColor = QColor::fromHsv(
angle, 255, 255 );
699 QColor alphaColor = QColor( pureColor );
700 alphaColor.setAlpha( 0 );
703 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 ) );
704 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
705 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 ) );
706 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
707 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
708 line1.setAngle( line1.angle() +
angle );
709 line2.setAngle( line2.angle() +
angle );
710 line3.setAngle( line3.angle() +
angle );
711 line4.setAngle( line4.angle() +
angle );
712 line5.setAngle( line5.angle() +
angle );
713 const QPointF p1 = line1.p2();
714 const QPointF p2 = line2.p2();
715 const QPointF p3 = line3.p2();
716 const QPointF p4 = line4.p2();
717 const QPointF p5 = line5.p2();
720 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
721 colorGrad.setColorAt( 0, alphaColor );
722 colorGrad.setColorAt( 1, pureColor );
723 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
724 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
725 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
728 triangle << p2 << p1 << p3 << p2;
729 imagePainter.setPen( Qt::NoPen );
731 imagePainter.setBrush( QBrush( Qt::black ) );
732 imagePainter.drawPolygon( triangle );
734 imagePainter.setBrush( QBrush( colorGrad ) );
735 imagePainter.drawPolygon( triangle );
737 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
738 imagePainter.setBrush( QBrush( whiteGrad ) );
739 imagePainter.drawPolygon( triangle );
744 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
745 imagePainter.setBrush( Qt::NoBrush );
746 imagePainter.setPen( QPen( Qt::transparent ) );
747 imagePainter.drawPolygon( triangle );
750 mTriangleDirty =
false;
762 setFocusPolicy( Qt::StrongFocus );
763 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
765 mBoxImage =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
776 return QSize( size, size );
782 QPainter painter(
this );
784 QStyleOptionFrame option;
785 option.initFrom(
this );
786 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
787 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
795 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
798 const double xPos = mMargin + ( width() - 2 * mMargin - 1 ) *
static_cast<double>( xComponentValue() ) /
static_cast<double>( valueRangeX() );
799 const double yPos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) *
static_cast<double>( yComponentValue() ) /
static_cast<double>( valueRangeY() );
801 painter.setBrush( Qt::white );
802 painter.setPen( Qt::NoPen );
804 painter.drawRect( xPos - 1, mMargin, 3, height() - 2 * mMargin - 1 );
805 painter.drawRect( mMargin, yPos - 1, width() - 2 * mMargin - 1, 3 );
806 painter.setPen( Qt::black );
807 painter.drawLine( xPos, mMargin, xPos, height() - mMargin - 1 );
808 painter.drawLine( mMargin, yPos, width() - mMargin - 1, yPos );
857 mBoxImage =
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
858 QgsColorWidget::resizeEvent( event );
865 setColorFromPoint( event->pos() );
872 if ( event->button() == Qt::LeftButton )
875 setColorFromPoint( event->pos() );
885 if ( event->button() == Qt::LeftButton )
895 void QgsColorBox::createBox()
897 const int maxValueX = mBoxImage->width();
898 const int maxValueY = mBoxImage->height();
902 int colorComponentValue;
904 for (
int y = 0; y < maxValueY; ++y )
906 QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
908 colorComponentValue = int( valueRangeY() - valueRangeY() * (
double( y ) / maxValueY ) );
909 alterColor( currentColor, yComponent(), colorComponentValue );
910 for (
int x = 0; x < maxValueX; ++x )
912 colorComponentValue = int( valueRangeX() * (
double( x ) / maxValueX ) );
913 alterColor( currentColor, xComponent(), colorComponentValue );
914 scanLine[x] = currentColor.rgb();
920 int QgsColorBox::valueRangeX()
const
925 int QgsColorBox::valueRangeY()
const
950 int QgsColorBox::yComponentValue()
const
975 int QgsColorBox::xComponentValue()
const
980 void QgsColorBox::setColorFromPoint( QPoint point )
982 int valX = valueRangeX() * ( point.x() - mMargin ) / ( width() - 2 * mMargin - 1 );
983 valX = std::min( std::max( valX, 0 ), valueRangeX() );
985 int valY = valueRangeY() - valueRangeY() * ( point.y() - mMargin ) / ( height() - 2 * mMargin - 1 );
986 valY = std::min( std::max( valY, 0 ), valueRangeY() );
997 if (
color.hue() >= 0 )
1017 setFocusPolicy( Qt::StrongFocus );
1041 QPainter painter(
this );
1046 QStyleOptionFrame option;
1047 option.initFrom(
this );
1048 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1049 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1055 QStyleOptionFocusRect option;
1056 option.initFrom(
this );
1057 option.state = QStyle::State_KeyboardFocusChange;
1058 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1065 color.setAlpha( 255 );
1072 painter.setPen( pen );
1073 painter.setBrush( Qt::NoBrush );
1076 for (
int c = 0;
c <= maxValue; ++
c )
1078 int colorVal =
static_cast<int>(
componentRange() *
static_cast<double>(
c ) / maxValue );
1085 if (
color.hue() < 0 )
1089 pen.setColor(
color );
1090 painter.setPen( pen );
1094 painter.drawLine( QLineF(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 ) );
1099 painter.drawLine( QLineF( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin ) );
1108 painter.setBrush( checkBrush );
1109 painter.setPen( Qt::NoPen );
1110 painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
1111 QLinearGradient colorGrad;
1115 colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 );
1120 colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 );
1123 transparent.setAlpha( 0 );
1124 colorGrad.setColorAt( 0, transparent );
1126 opaque.setAlpha( 255 );
1127 colorGrad.setColorAt( 1, opaque );
1128 const QBrush colorBrush = QBrush( colorGrad );
1129 painter.setBrush( colorBrush );
1130 painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
1136 painter.setRenderHint( QPainter::Antialiasing );
1137 painter.setBrush( QBrush( Qt::black ) );
1138 painter.setPen( Qt::NoPen );
1140 painter.drawPolygon( mTopTriangle );
1141 painter.translate( 0, height() - mMargin - 2 );
1142 painter.setBrush( QBrush( Qt::white ) );
1143 painter.drawPolygon( mBottomTriangle );
1149 const double ypos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) *
static_cast<double>(
componentValue() ) /
componentRange();
1150 painter.setBrush( Qt::white );
1151 painter.setPen( Qt::NoPen );
1152 painter.drawRect( QRectF( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 ) );
1153 painter.setPen( Qt::black );
1154 painter.drawLine( QLineF( mMargin, ypos, width() - mMargin - 1, ypos ) );
1164 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1169 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1176 if ( margin == mMargin )
1197 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1198 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1206 setColorFromPoint( event->pos() );
1216 if ( event->angleDelta().y() > 0 )
1237 if ( event->button() == Qt::LeftButton )
1240 setColorFromPoint( event->pos() );
1250 if ( event->button() == Qt::LeftButton )
1252 mIsDragging =
false;
1296 QgsColorWidget::keyPressEvent( event );
1308 void QgsColorRampWidget::setColorFromPoint( QPointF point )
1314 val =
componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin );
1339 QHBoxLayout *hLayout =
new QHBoxLayout();
1340 hLayout->setContentsMargins( 0, 0, 0, 0 );
1341 hLayout->setSpacing( 5 );
1345 hLayout->addWidget( mRampWidget, 1 );
1347 mSpinBox =
new QSpinBox();
1349 const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( QStringLiteral(
"888%" ) );
1350 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1351 mSpinBox->setMinimum( 0 );
1352 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1357 mSpinBox->setSuffix( QChar( 176 ) );
1361 mSpinBox->setSuffix( tr(
"%" ) );
1363 hLayout->addWidget( mSpinBox );
1364 setLayout( hLayout );
1368 connect( mSpinBox,
static_cast < void ( QSpinBox::* )(
int )
> ( &QSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1375 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1379 mSpinBox->setSuffix( QChar( 176 ) );
1384 mSpinBox->setSuffix( tr(
"%" ) );
1389 mSpinBox->setSuffix( QString() );
1396 mRampWidget->blockSignals(
true );
1398 mRampWidget->blockSignals(
false );
1399 mSpinBox->blockSignals(
true );
1400 mSpinBox->setValue( convertRealToDisplay( value ) );
1401 mSpinBox->blockSignals(
false );
1408 mSpinBox->blockSignals(
true );
1410 mSpinBox->blockSignals(
false );
1413 void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1418 void QgsColorSliderWidget::spinChanged(
int value )
1420 const int convertedValue = convertDisplayToReal( value );
1426 void QgsColorSliderWidget::rampChanged(
int value )
1428 mSpinBox->blockSignals(
true );
1429 mSpinBox->setValue( convertRealToDisplay( value ) );
1430 mSpinBox->blockSignals(
false );
1434 int QgsColorSliderWidget::convertRealToDisplay(
const int realValue )
const
1440 return std::round( 100.0 * realValue / 255.0 );
1447 int QgsColorSliderWidget::convertDisplayToReal(
const int displayValue )
const
1452 return std::round( 255.0 * displayValue / 100.0 );
1456 return displayValue;
1466 QHBoxLayout *hLayout =
new QHBoxLayout();
1467 hLayout->setContentsMargins( 0, 0, 0, 0 );
1468 hLayout->setSpacing( 0 );
1470 mLineEdit =
new QLineEdit(
nullptr );
1471 hLayout->addWidget( mLineEdit );
1473 mMenuButton =
new QToolButton( mLineEdit );
1475 mMenuButton->setCursor( Qt::ArrowCursor );
1476 mMenuButton->setFocusPolicy( Qt::NoFocus );
1477 mMenuButton->setStyleSheet( QStringLiteral(
"QToolButton { border: none; padding: 0px; }" ) );
1479 setLayout( hLayout );
1481 const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1482 mLineEdit->setStyleSheet( QStringLiteral(
"QLineEdit { padding-right: %1px; } " )
1483 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1485 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1486 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1490 mFormat = settings.
enumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ),
HexRgb );
1504 const QSize sz = mMenuButton->sizeHint();
1505 const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1506 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
1507 ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1510 void QgsColorTextWidget::updateText()
1529 void QgsColorTextWidget::textChanged()
1531 const QString testString = mLineEdit->text();
1534 if ( !
color.isValid() )
1545 if ( !containsAlpha )
1556 void QgsColorTextWidget::showMenu()
1558 QMenu colorContextMenu;
1560 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1561 colorContextMenu.addAction( hexRgbAction );
1562 QAction *hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1563 colorContextMenu.addAction( hexRgbaAction );
1564 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1565 colorContextMenu.addAction( rgbAction );
1566 QAction *rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1567 colorContextMenu.addAction( rgbaAction );
1569 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1570 if ( selectedAction == hexRgbAction )
1574 else if ( selectedAction == hexRgbaAction )
1578 else if ( selectedAction == rgbAction )
1582 else if ( selectedAction == rgbaAction )
1589 settings.
setEnumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ), mFormat );
1601 , mColor2( QColor() )
1606 void QgsColorPreviewWidget::drawColor(
const QColor &color, QRect rect, QPainter &painter )
1608 painter.setPen( Qt::NoPen );
1610 if (
color.alpha() < 255 )
1613 painter.setBrush( checkBrush );
1614 painter.drawRect( rect );
1619 const QBrush colorBrush = QBrush(
color );
1620 painter.setBrush( colorBrush );
1621 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1623 QColor opaqueColor = QColor(
color );
1624 opaqueColor.setAlpha( 255 );
1625 const QBrush opaqueBrush = QBrush( opaqueColor );
1626 painter.setBrush( opaqueBrush );
1627 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1632 const QBrush brush = QBrush(
color );
1633 painter.setBrush( brush );
1634 painter.drawRect( rect );
1641 QPainter painter(
this );
1643 if ( mColor2.isValid() )
1646 const int verticalSplit = std::round( height() / 2.0 );
1647 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1648 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1652 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1665 if (
color == mColor2 )
1675 if ( e->button() == Qt::LeftButton )
1677 mDragStartPosition = e->pos();
1684 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1693 if ( mColor2.isValid() )
1696 const int verticalSplit = std::round( height() / 2.0 );
1697 if ( mDragStartPosition.y() >= verticalSplit )
1699 clickedColor = mColor2;
1710 if ( !( e->buttons() & Qt::LeftButton ) )
1717 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1728 if ( mColor2.isValid() )
1731 const int verticalSplit = std::round( height() / 2.0 );
1732 if ( mDragStartPosition.y() >= verticalSplit )
1734 dragColor = mColor2;
1738 QDrag *drag =
new QDrag(
this );
1741 drag->exec( Qt::CopyAction );
1750 : QWidgetAction( parent )
1752 , mColorWidget( colorWidget )
1753 , mSuppressRecurse( false )
1754 , mDismissOnColorSelection( true )
1756 setDefaultWidget( mColorWidget );
1759 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1763 void QgsColorWidgetAction::onHover()
1766 if ( mSuppressRecurse )
1773 mSuppressRecurse =
true;
1774 mMenu->setActiveAction(
this );
1775 mSuppressRecurse =
false;
1779 void QgsColorWidgetAction::setColor(
const QColor &color )
1782 if ( mMenu && mDismissOnColorSelection )