23 #include <QResizeEvent>
24 #include <QStyleOptionFrameV3>
26 #include <QHBoxLayout>
29 #include <QFontMetrics>
30 #include <QToolButton>
45 , mCurrentColor( Qt::red )
46 , mComponent( component )
48 setAcceptDrops(
true );
61 pixmap.fill( Qt::transparent );
63 painter.begin( &pixmap );
65 painter.fillRect( QRect( 0, 0,
iconSize,
iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
67 QColor pixmapColor =
color;
68 pixmapColor.setAlpha( 255 );
69 painter.setBrush( QBrush( pixmapColor ) );
70 painter.setPen( QPen( Qt::white ) );
145 color.getHsv( &h, &s, &v, &a );
153 color.setRed( clippedValue );
156 color.setGreen( clippedValue );
159 color.setBlue( clippedValue );
162 color.setHsv( clippedValue, s, v, a );
165 color.setHsv( h, clippedValue, v, a );
168 color.setHsv( h, s, clippedValue, a );
171 color.setAlpha( clippedValue );
180 static QPixmap sTranspBkgrd;
182 if ( sTranspBkgrd.isNull() )
194 if ( mimeColor.isValid() )
197 e->acceptProposedAction();
204 bool hasAlpha =
false;
207 if ( mimeColor.isValid() )
210 e->acceptProposedAction();
269 valueClipped = std::max( valueClipped, 0 );
281 if ( r == valueClipped )
288 if ( g == valueClipped )
295 if ( b == valueClipped )
302 if ( h == valueClipped )
309 if ( s == valueClipped )
316 if ( v == valueClipped )
323 if ( a == valueClipped )
352 if (
color.hue() >= 0 )
374 QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
376 QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
377 for (
int pos = 0; pos <= wheelStops; ++pos )
379 double relativePos =
static_cast<double>( pos ) / wheelStops;
380 gradColor.setHsvF( relativePos, 1, 1 );
381 wheelGradient.setColorAt( relativePos, gradColor );
383 mWheelBrush = QBrush( wheelGradient );
389 delete mTriangleImage;
395 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
396 int size =
Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral(
"XXXXXXXXXXXXXXXXXXXXXX" ) );
400 return QSize( size, size );
406 QPainter painter(
this );
408 if ( !mWidgetImage || !mWheelImage || !mTriangleImage )
410 createImages( size() );
414 mWidgetImage->fill( Qt::transparent );
415 QPainter imagePainter( mWidgetImage );
416 imagePainter.setRenderHint( QPainter::Antialiasing );
425 QPointF center = QPointF( width() / 2.0, height() / 2.0 );
426 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage );
430 double length = mWheelImage->width() / 2.0;
431 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
432 hueMarkerLine.setAngle( h );
435 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
439 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
440 imagePainter.setPen( pen );
441 imagePainter.drawLine( hueMarkerLine );
442 imagePainter.restore();
445 if ( mTriangleDirty )
449 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage );
452 double triangleRadius = length - mWheelThickness - 1;
456 double hueRadians = ( h * M_PI / 180.0 );
457 double hx = std::cos( hueRadians ) * triangleRadius;
458 double hy = -std::sin( hueRadians ) * triangleRadius;
459 double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
460 double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
461 double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
462 double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
463 double mx = ( sx + vx ) / 2.0;
464 double my = ( sy + vy ) / 2.0;
466 double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
467 double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
468 double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
471 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
472 imagePainter.setPen( pen );
473 imagePainter.setBrush( Qt::NoBrush );
474 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0, 4.0 );
478 painter.drawImage( QPoint( 0, 0 ), *mWidgetImage );
487 mTriangleDirty =
true;
493 void QgsColorWheel::createImages(
const QSizeF size )
495 double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
496 mWheelThickness = wheelSize / 15.0;
500 mWheelImage =
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
501 delete mTriangleImage;
502 mTriangleImage =
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
504 mWidgetImage =
new QImage( size.width(), size.height(), QImage::Format_ARGB32 );
508 mTriangleDirty =
true;
514 createImages( event->size() );
515 QgsColorWidget::resizeEvent( event );
518 void QgsColorWheel::setColorFromPos(
const QPointF pos )
520 QPointF center = QPointF( width() / 2.0, height() / 2.0 );
522 QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
524 QColor newColor = QColor();
531 if ( mClickedPart == QgsColorWheel::Triangle )
536 double x = pos.x() - center.x();
537 double y = pos.y() - center.y();
539 double eventAngleRadians = line.angle() * M_PI / 180.0;
540 double hueRadians = h * M_PI / 180.0;
541 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
542 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
543 double length = mWheelImage->width() / 2.0;
544 double triangleLength = length - mWheelThickness - 1;
546 double a = 0.5 * triangleLength;
547 double b = std::tan( rad1 ) * a;
548 double r = std::sqrt( x * x + y * y );
549 double maxR = std::sqrt( a * a + b * b );
553 double dx = std::tan( rad1 ) * r;
554 double rad2 = std::atan( dx / maxR );
555 rad2 = std::min( rad2, M_PI / 3.0 );
556 rad2 = std::max( rad2, -M_PI / 3.0 );
557 eventAngleRadians += rad2 - rad1;
558 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
559 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
560 b = std::tan( rad1 ) * a;
561 r = std::sqrt( a * a + b * b );
564 double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
565 double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
566 double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
567 double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
568 s = std::min(
static_cast< int >( std::round( std::max( 0.0, newS ) * 255.0 ) ), 255 );
569 l = std::min(
static_cast< int >( std::round( std::max( 0.0, newL ) * 255.0 ) ), 255 );
570 newColor = QColor::fromHsl( h, s, l );
572 newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
574 else if ( mClickedPart == QgsColorWheel::Wheel )
579 int newHue = line.angle();
580 newColor = QColor::fromHsv( newHue, s, v, alpha );
582 mTriangleDirty =
true;
603 setColorFromPos( event->pos() );
612 QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
614 double innerLength = mWheelImage->width() / 2.0 - mWheelThickness;
615 if ( line.length() < innerLength )
617 mClickedPart = QgsColorWheel::Triangle;
621 mClickedPart = QgsColorWheel::Wheel;
623 setColorFromPos( event->pos() );
629 mClickedPart = QgsColorWheel::None;
632 void QgsColorWheel::createWheel()
639 int maxSize = std::min( mWheelImage->width(), mWheelImage->height() );
640 double wheelRadius = maxSize / 2.0;
642 mWheelImage->fill( Qt::transparent );
643 QPainter p( mWheelImage );
644 p.setRenderHint( QPainter::Antialiasing );
645 p.setBrush( mWheelBrush );
646 p.setPen( Qt::NoPen );
649 p.translate( wheelRadius, wheelRadius );
650 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
653 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
654 p.setBrush( QBrush( Qt::black ) );
655 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius - mWheelThickness, wheelRadius - mWheelThickness );
661 void QgsColorWheel::createTriangle()
663 if ( !mWheelImage || !mTriangleImage )
668 QPointF center = QPointF( mWheelImage->width() / 2.0, mWheelImage->height() / 2.0 );
669 mTriangleImage->fill( Qt::transparent );
671 QPainter imagePainter( mTriangleImage );
672 imagePainter.setRenderHint( QPainter::Antialiasing );
675 double wheelRadius = mWheelImage->width() / 2.0;
676 double triangleRadius = wheelRadius - mWheelThickness - 1;
679 QColor pureColor = QColor::fromHsv(
angle, 255, 255 );
681 QColor alphaColor = QColor( pureColor );
682 alphaColor.setAlpha( 0 );
685 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 ) );
686 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
687 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 ) );
688 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
689 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
690 line1.setAngle( line1.angle() +
angle );
691 line2.setAngle( line2.angle() +
angle );
692 line3.setAngle( line3.angle() +
angle );
693 line4.setAngle( line4.angle() +
angle );
694 line5.setAngle( line5.angle() +
angle );
695 QPointF p1 = line1.p2();
696 QPointF p2 = line2.p2();
697 QPointF p3 = line3.p2();
698 QPointF p4 = line4.p2();
699 QPointF p5 = line5.p2();
702 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
703 colorGrad.setColorAt( 0, alphaColor );
704 colorGrad.setColorAt( 1, pureColor );
705 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
706 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
707 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
710 triangle << p2 << p1 << p3 << p2;
711 imagePainter.setPen( Qt::NoPen );
713 imagePainter.setBrush( QBrush( Qt::black ) );
714 imagePainter.drawPolygon( triangle );
716 imagePainter.setBrush( QBrush( colorGrad ) );
717 imagePainter.drawPolygon( triangle );
719 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
720 imagePainter.setBrush( QBrush( whiteGrad ) );
721 imagePainter.drawPolygon( triangle );
726 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
727 imagePainter.setBrush( Qt::NoBrush );
728 imagePainter.setPen( QPen( Qt::transparent ) );
729 imagePainter.drawPolygon( triangle );
732 mTriangleDirty =
false;
744 setFocusPolicy( Qt::StrongFocus );
745 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
747 mBoxImage =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
757 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
758 int size =
Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral(
"XXXXXXXXXXXXXXXXXXXXXX" ) );
762 return QSize( size, size );
768 QPainter painter(
this );
770 QStyleOptionFrame option;
771 option.initFrom(
this );
772 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
773 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
781 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
784 double xPos = mMargin + ( width() - 2 * mMargin - 1 ) *
static_cast<double>( xComponentValue() ) /
static_cast<double>( valueRangeX() );
785 double yPos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) *
static_cast<double>( yComponentValue() ) /
static_cast<double>( valueRangeY() );
787 painter.setBrush( Qt::white );
788 painter.setPen( Qt::NoPen );
790 painter.drawRect( xPos - 1, mMargin, 3, height() - 2 * mMargin - 1 );
791 painter.drawRect( mMargin, yPos - 1, width() - 2 * mMargin - 1, 3 );
792 painter.setPen( Qt::black );
793 painter.drawLine( xPos, mMargin, xPos, height() - mMargin - 1 );
794 painter.drawLine( mMargin, yPos, width() - mMargin - 1, yPos );
843 mBoxImage =
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
844 QgsColorWidget::resizeEvent( event );
849 setColorFromPoint( event->pos() );
855 setColorFromPoint( event->pos() );
858 void QgsColorBox::createBox()
860 int maxValueX = mBoxImage->width();
861 int maxValueY = mBoxImage->height();
865 int colorComponentValue;
867 for (
int y = 0; y < maxValueY; ++y )
869 QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
871 colorComponentValue = int( valueRangeY() - valueRangeY() * (
double( y ) / maxValueY ) );
872 alterColor( currentColor, yComponent(), colorComponentValue );
873 for (
int x = 0; x < maxValueX; ++x )
875 colorComponentValue = int( valueRangeX() * (
double( x ) / maxValueX ) );
876 alterColor( currentColor, xComponent(), colorComponentValue );
877 scanLine[x] = currentColor.rgb();
883 int QgsColorBox::valueRangeX()
const
888 int QgsColorBox::valueRangeY()
const
913 int QgsColorBox::yComponentValue()
const
938 int QgsColorBox::xComponentValue()
const
943 void QgsColorBox::setColorFromPoint( QPoint point )
945 int valX = valueRangeX() * ( point.x() - mMargin ) / ( width() - 2 * mMargin - 1 );
946 valX = std::min( std::max( valX, 0 ), valueRangeX() );
948 int valY = valueRangeY() - valueRangeY() * ( point.y() - mMargin ) / ( height() - 2 * mMargin - 1 );
949 valY = std::min( std::max( valY, 0 ), valueRangeY() );
960 if (
color.hue() >= 0 )
980 setFocusPolicy( Qt::StrongFocus );
992 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
1001 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
1012 QPainter painter(
this );
1017 QStyleOptionFrame option;
1018 option.initFrom(
this );
1019 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1020 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1026 QStyleOptionFocusRect option;
1027 option.initFrom(
this );
1028 option.state = QStyle::State_KeyboardFocusChange;
1029 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1036 color.setAlpha( 255 );
1043 painter.setPen( pen );
1044 painter.setBrush( Qt::NoBrush );
1047 for (
int c = 0;
c <= maxValue; ++
c )
1049 int colorVal =
static_cast<int>(
componentRange() *
static_cast<double>(
c ) / maxValue );
1056 if (
color.hue() < 0 )
1060 pen.setColor(
color );
1061 painter.setPen( pen );
1065 painter.drawLine( QLineF(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 ) );
1070 painter.drawLine( QLineF( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin ) );
1079 painter.setBrush( checkBrush );
1080 painter.setPen( Qt::NoPen );
1081 painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
1082 QLinearGradient colorGrad;
1086 colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 );
1091 colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 );
1094 transparent.setAlpha( 0 );
1095 colorGrad.setColorAt( 0, transparent );
1097 opaque.setAlpha( 255 );
1098 colorGrad.setColorAt( 1, opaque );
1099 QBrush colorBrush = QBrush( colorGrad );
1100 painter.setBrush( colorBrush );
1101 painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
1107 painter.setRenderHint( QPainter::Antialiasing );
1108 painter.setBrush( QBrush( Qt::black ) );
1109 painter.setPen( Qt::NoPen );
1111 painter.drawPolygon( mTopTriangle );
1112 painter.translate( 0, height() - mMargin - 2 );
1113 painter.setBrush( QBrush( Qt::white ) );
1114 painter.drawPolygon( mBottomTriangle );
1120 double ypos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) *
static_cast<double>(
componentValue() ) /
componentRange();
1121 painter.setBrush( Qt::white );
1122 painter.setPen( Qt::NoPen );
1123 painter.drawRect( QRectF( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 ) );
1124 painter.setPen( Qt::black );
1125 painter.drawLine( QLineF( mMargin, ypos, width() - mMargin - 1, ypos ) );
1135 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1140 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1147 if ( margin == mMargin )
1168 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1169 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1175 setColorFromPoint( event->pos() );
1183 if ( event->delta() > 0 )
1204 setColorFromPoint( event->pos() );
1243 QgsColorWidget::keyPressEvent( event );
1255 void QgsColorRampWidget::setColorFromPoint( QPointF point )
1261 val =
componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin );
1286 QHBoxLayout *hLayout =
new QHBoxLayout();
1287 hLayout->setMargin( 0 );
1288 hLayout->setSpacing( 5 );
1292 hLayout->addWidget( mRampWidget, 1 );
1294 mSpinBox =
new QSpinBox();
1296 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
1297 int largestCharWidth = mSpinBox->fontMetrics().width( QStringLiteral(
"888%" ) );
1299 int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( QStringLiteral(
"888%" ) );
1301 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1302 mSpinBox->setMinimum( 0 );
1303 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1308 mSpinBox->setSuffix( QChar( 176 ) );
1312 mSpinBox->setSuffix( tr(
"%" ) );
1314 hLayout->addWidget( mSpinBox );
1315 setLayout( hLayout );
1319 connect( mSpinBox,
static_cast < void ( QSpinBox::* )(
int )
> ( &QSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1326 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1330 mSpinBox->setSuffix( QChar( 176 ) );
1335 mSpinBox->setSuffix( tr(
"%" ) );
1340 mSpinBox->setSuffix( QString() );
1347 mRampWidget->blockSignals(
true );
1349 mRampWidget->blockSignals(
false );
1350 mSpinBox->blockSignals(
true );
1351 mSpinBox->setValue( convertRealToDisplay( value ) );
1352 mSpinBox->blockSignals(
false );
1359 mSpinBox->blockSignals(
true );
1361 mSpinBox->blockSignals(
false );
1364 void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1369 void QgsColorSliderWidget::spinChanged(
int value )
1371 int convertedValue = convertDisplayToReal( value );
1377 void QgsColorSliderWidget::rampChanged(
int value )
1379 mSpinBox->blockSignals(
true );
1380 mSpinBox->setValue( convertRealToDisplay( value ) );
1381 mSpinBox->blockSignals(
false );
1385 int QgsColorSliderWidget::convertRealToDisplay(
const int realValue )
const
1391 return std::round( 100.0 * realValue / 255.0 );
1398 int QgsColorSliderWidget::convertDisplayToReal(
const int displayValue )
const
1403 return std::round( 255.0 * displayValue / 100.0 );
1407 return displayValue;
1417 QHBoxLayout *hLayout =
new QHBoxLayout();
1418 hLayout->setMargin( 0 );
1419 hLayout->setSpacing( 0 );
1421 mLineEdit =
new QLineEdit(
nullptr );
1422 hLayout->addWidget( mLineEdit );
1424 mMenuButton =
new QToolButton( mLineEdit );
1426 mMenuButton->setCursor( Qt::ArrowCursor );
1427 mMenuButton->setFocusPolicy( Qt::NoFocus );
1428 mMenuButton->setStyleSheet( QStringLiteral(
"QToolButton { border: none; padding: 0px; }" ) );
1430 setLayout( hLayout );
1432 int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1433 mLineEdit->setStyleSheet( QStringLiteral(
"QLineEdit { padding-right: %1px; } " )
1434 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1436 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1437 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1441 mFormat = settings.
enumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ),
HexRgb );
1455 QSize sz = mMenuButton->sizeHint();
1456 int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1457 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
1458 ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1461 void QgsColorTextWidget::updateText()
1480 void QgsColorTextWidget::textChanged()
1482 QString testString = mLineEdit->text();
1485 if ( !
color.isValid() )
1496 if ( !containsAlpha )
1507 void QgsColorTextWidget::showMenu()
1509 QMenu colorContextMenu;
1511 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1512 colorContextMenu.addAction( hexRgbAction );
1513 QAction *hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1514 colorContextMenu.addAction( hexRgbaAction );
1515 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1516 colorContextMenu.addAction( rgbAction );
1517 QAction *rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1518 colorContextMenu.addAction( rgbaAction );
1520 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1521 if ( selectedAction == hexRgbAction )
1525 else if ( selectedAction == hexRgbaAction )
1529 else if ( selectedAction == rgbAction )
1533 else if ( selectedAction == rgbaAction )
1540 settings.
setEnumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ), mFormat );
1552 , mColor2( QColor() )
1557 void QgsColorPreviewWidget::drawColor(
const QColor &color, QRect rect, QPainter &painter )
1559 painter.setPen( Qt::NoPen );
1561 if (
color.alpha() < 255 )
1564 painter.setBrush( checkBrush );
1565 painter.drawRect( rect );
1570 QBrush colorBrush = QBrush(
color );
1571 painter.setBrush( colorBrush );
1572 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1574 QColor opaqueColor = QColor(
color );
1575 opaqueColor.setAlpha( 255 );
1576 QBrush opaqueBrush = QBrush( opaqueColor );
1577 painter.setBrush( opaqueBrush );
1578 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1583 QBrush brush = QBrush(
color );
1584 painter.setBrush( brush );
1585 painter.drawRect( rect );
1592 QPainter painter(
this );
1594 if ( mColor2.isValid() )
1597 int verticalSplit = std::round( height() / 2.0 );
1598 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1599 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1603 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1611 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
1620 if (
color == mColor2 )
1630 if ( e->button() == Qt::LeftButton )
1632 mDragStartPosition = e->pos();
1639 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1648 if ( mColor2.isValid() )
1651 int verticalSplit = std::round( height() / 2.0 );
1652 if ( mDragStartPosition.y() >= verticalSplit )
1654 clickedColor = mColor2;
1665 if ( !( e->buttons() & Qt::LeftButton ) )
1672 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1683 if ( mColor2.isValid() )
1686 int verticalSplit = std::round( height() / 2.0 );
1687 if ( mDragStartPosition.y() >= verticalSplit )
1689 dragColor = mColor2;
1693 QDrag *drag =
new QDrag(
this );
1696 drag->exec( Qt::CopyAction );
1705 : QWidgetAction( parent )
1707 , mColorWidget( colorWidget )
1708 , mSuppressRecurse( false )
1709 , mDismissOnColorSelection( true )
1711 setDefaultWidget( mColorWidget );
1714 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1718 void QgsColorWidgetAction::onHover()
1721 if ( mSuppressRecurse )
1728 mSuppressRecurse =
true;
1729 mMenu->setActiveAction(
this );
1730 mSuppressRecurse =
false;
1734 void QgsColorWidgetAction::setColor(
const QColor &color )
1738 if ( mMenu && mDismissOnColorSelection )