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 );
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;
494void 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;
514 QgsColorWidget::resizeEvent( event );
517 if ( event->size().width() > parentWidget()->size().width() )
520 std::min( event->size().width(), parentWidget()->size().width() - 2 ),
521 std::min( event->size().height(), parentWidget()->size().height() - 2 )
524 createImages( newSize );
528 createImages( event->size() );
532 createImages( event->size() );
536void QgsColorWheel::setColorFromPos(
const QPointF pos )
538 const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
540 const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
542 QColor newColor = QColor();
549 if ( mClickedPart == QgsColorWheel::Triangle )
554 const double x = pos.x() - center.x();
555 const double y = pos.y() - center.y();
557 double eventAngleRadians = line.angle() * M_PI / 180.0;
558 const double hueRadians = h * M_PI / 180.0;
559 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
560 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
561 const double length = mWheelImage->width() / 2.0;
562 const double triangleLength = length - mWheelThickness - 1;
564 const double a = 0.5 * triangleLength;
565 double b = std::tan( rad1 ) * a;
566 double r = std::sqrt( x * x + y * y );
567 const double maxR = std::sqrt( a * a + b * b );
571 const double dx = std::tan( rad1 ) * r;
572 double rad2 = std::atan( dx / maxR );
573 rad2 = std::min( rad2, M_PI / 3.0 );
574 rad2 = std::max( rad2, -M_PI / 3.0 );
575 eventAngleRadians += rad2 - rad1;
576 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
577 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
578 b = std::tan( rad1 ) * a;
579 r = std::sqrt( a * a + b * b );
582 const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
583 const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
584 const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
585 const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
586 s = std::min(
static_cast< int >( std::round( std::max( 0.0, newS ) * 255.0 ) ), 255 );
587 l = std::min(
static_cast< int >( std::round( std::max( 0.0, newL ) * 255.0 ) ), 255 );
588 newColor = QColor::fromHsl( h, s, l );
590 newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
592 else if ( mClickedPart == QgsColorWheel::Wheel )
597 const int newHue = line.angle();
598 newColor = QColor::fromHsv( newHue, s, v, alpha );
600 mTriangleDirty =
true;
622 setColorFromPos( event->pos() );
629 if ( event->button() == Qt::LeftButton )
635 const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
637 const double innerLength = mWheelImage->width() / 2.0 - mWheelThickness;
638 if ( line.length() < innerLength )
640 mClickedPart = QgsColorWheel::Triangle;
644 mClickedPart = QgsColorWheel::Wheel;
646 setColorFromPos( event->pos() );
656 if ( event->button() == Qt::LeftButton )
659 mClickedPart = QgsColorWheel::None;
667void QgsColorWheel::createWheel()
674 const int maxSize = std::min( mWheelImage->width(), mWheelImage->height() );
675 const double wheelRadius = maxSize / 2.0;
677 mWheelImage->fill( Qt::transparent );
678 QPainter p( mWheelImage );
679 p.setRenderHint( QPainter::Antialiasing );
680 p.setBrush( mWheelBrush );
681 p.setPen( Qt::NoPen );
684 p.translate( wheelRadius, wheelRadius );
685 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
688 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
689 p.setBrush( QBrush( Qt::black ) );
690 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius - mWheelThickness, wheelRadius - mWheelThickness );
696void QgsColorWheel::createTriangle()
698 if ( !mWheelImage || !mTriangleImage )
703 const QPointF center = QPointF( mWheelImage->width() / 2.0, mWheelImage->height() / 2.0 );
704 mTriangleImage->fill( Qt::transparent );
706 QPainter imagePainter( mTriangleImage );
707 imagePainter.setRenderHint( QPainter::Antialiasing );
710 const double wheelRadius = mWheelImage->width() / 2.0;
711 const double triangleRadius = wheelRadius - mWheelThickness - 1;
714 const QColor pureColor = QColor::fromHsv(
angle, 255, 255 );
716 QColor alphaColor = QColor( pureColor );
717 alphaColor.setAlpha( 0 );
720 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 ) );
721 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
722 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 ) );
723 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
724 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
725 line1.setAngle( line1.angle() +
angle );
726 line2.setAngle( line2.angle() +
angle );
727 line3.setAngle( line3.angle() +
angle );
728 line4.setAngle( line4.angle() +
angle );
729 line5.setAngle( line5.angle() +
angle );
730 const QPointF p1 = line1.p2();
731 const QPointF p2 = line2.p2();
732 const QPointF p3 = line3.p2();
733 const QPointF p4 = line4.p2();
734 const QPointF p5 = line5.p2();
737 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
738 colorGrad.setColorAt( 0, alphaColor );
739 colorGrad.setColorAt( 1, pureColor );
740 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
741 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
742 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
745 triangle << p2 << p1 << p3 << p2;
746 imagePainter.setPen( Qt::NoPen );
748 imagePainter.setBrush( QBrush( Qt::black ) );
749 imagePainter.drawPolygon( triangle );
751 imagePainter.setBrush( QBrush( colorGrad ) );
752 imagePainter.drawPolygon( triangle );
754 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
755 imagePainter.setBrush( QBrush( whiteGrad ) );
756 imagePainter.drawPolygon( triangle );
761 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
762 imagePainter.setBrush( Qt::NoBrush );
763 imagePainter.setPen( QPen( Qt::transparent ) );
764 imagePainter.drawPolygon( triangle );
767 mTriangleDirty =
false;
779 setFocusPolicy( Qt::StrongFocus );
780 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
782 mBoxImage =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
793 return QSize( size, size );
799 QPainter painter(
this );
801 QStyleOptionFrame option;
802 option.initFrom(
this );
803 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
804 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
812 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
815 const double xPos = mMargin + ( width() - 2 * mMargin - 1 ) *
static_cast<double>( xComponentValue() ) /
static_cast<double>( valueRangeX() );
816 const double yPos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) *
static_cast<double>( yComponentValue() ) /
static_cast<double>( valueRangeY() );
818 painter.setBrush( Qt::white );
819 painter.setPen( Qt::NoPen );
821 painter.drawRect( xPos - 1, mMargin, 3, height() - 2 * mMargin - 1 );
822 painter.drawRect( mMargin, yPos - 1, width() - 2 * mMargin - 1, 3 );
823 painter.setPen( Qt::black );
824 painter.drawLine( xPos, mMargin, xPos, height() - mMargin - 1 );
825 painter.drawLine( mMargin, yPos, width() - mMargin - 1, yPos );
874 mBoxImage =
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
875 QgsColorWidget::resizeEvent( event );
882 setColorFromPoint( event->pos() );
889 if ( event->button() == Qt::LeftButton )
892 setColorFromPoint( event->pos() );
902 if ( event->button() == Qt::LeftButton )
912void QgsColorBox::createBox()
914 const int maxValueX = mBoxImage->width();
915 const int maxValueY = mBoxImage->height();
919 int colorComponentValue;
921 for (
int y = 0; y < maxValueY; ++y )
923 QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
925 colorComponentValue = int( valueRangeY() - valueRangeY() * (
double( y ) / maxValueY ) );
926 alterColor( currentColor, yComponent(), colorComponentValue );
927 for (
int x = 0; x < maxValueX; ++x )
929 colorComponentValue = int( valueRangeX() * (
double( x ) / maxValueX ) );
930 alterColor( currentColor, xComponent(), colorComponentValue );
931 scanLine[x] = currentColor.rgb();
937int QgsColorBox::valueRangeX()
const
942int QgsColorBox::valueRangeY()
const
967int QgsColorBox::yComponentValue()
const
992int QgsColorBox::xComponentValue()
const
997void QgsColorBox::setColorFromPoint( QPoint point )
999 int valX = valueRangeX() * ( point.x() - mMargin ) / ( width() - 2 * mMargin - 1 );
1000 valX = std::min( std::max( valX, 0 ), valueRangeX() );
1002 int valY = valueRangeY() - valueRangeY() * ( point.y() - mMargin ) / ( height() - 2 * mMargin - 1 );
1003 valY = std::min( std::max( valY, 0 ), valueRangeY() );
1014 if (
color.hue() >= 0 )
1034 setFocusPolicy( Qt::StrongFocus );
1058 QPainter painter(
this );
1063 QStyleOptionFrame option;
1064 option.initFrom(
this );
1065 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1066 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1072 QStyleOptionFocusRect option;
1073 option.initFrom(
this );
1074 option.state = QStyle::State_KeyboardFocusChange;
1075 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1082 color.setAlpha( 255 );
1089 painter.setPen( pen );
1090 painter.setBrush( Qt::NoBrush );
1093 for (
int c = 0;
c <= maxValue; ++
c )
1095 int colorVal =
static_cast<int>(
componentRange() *
static_cast<double>(
c ) / maxValue );
1102 if (
color.hue() < 0 )
1106 pen.setColor(
color );
1107 painter.setPen( pen );
1111 painter.drawLine( QLineF(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 ) );
1116 painter.drawLine( QLineF( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin ) );
1125 painter.setBrush( checkBrush );
1126 painter.setPen( Qt::NoPen );
1127 painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
1128 QLinearGradient colorGrad;
1132 colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 );
1137 colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 );
1140 transparent.setAlpha( 0 );
1141 colorGrad.setColorAt( 0, transparent );
1143 opaque.setAlpha( 255 );
1144 colorGrad.setColorAt( 1, opaque );
1145 const QBrush colorBrush = QBrush( colorGrad );
1146 painter.setBrush( colorBrush );
1147 painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
1153 painter.setRenderHint( QPainter::Antialiasing );
1154 painter.setBrush( QBrush( Qt::black ) );
1155 painter.setPen( Qt::NoPen );
1157 painter.drawPolygon( mTopTriangle );
1158 painter.translate( 0, height() - mMargin - 2 );
1159 painter.setBrush( QBrush( Qt::white ) );
1160 painter.drawPolygon( mBottomTriangle );
1166 const double ypos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) *
static_cast<double>(
componentValue() ) /
componentRange();
1167 painter.setBrush( Qt::white );
1168 painter.setPen( Qt::NoPen );
1169 painter.drawRect( QRectF( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 ) );
1170 painter.setPen( Qt::black );
1171 painter.drawLine( QLineF( mMargin, ypos, width() - mMargin - 1, ypos ) );
1181 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1186 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1193 if ( margin == mMargin )
1214 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1215 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1223 setColorFromPoint( event->pos() );
1233 if ( event->angleDelta().y() > 0 )
1254 if ( event->button() == Qt::LeftButton )
1257 setColorFromPoint( event->pos() );
1267 if ( event->button() == Qt::LeftButton )
1269 mIsDragging =
false;
1313 QgsColorWidget::keyPressEvent( event );
1325void QgsColorRampWidget::setColorFromPoint( QPointF point )
1331 val =
componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin );
1356 QHBoxLayout *hLayout =
new QHBoxLayout();
1357 hLayout->setContentsMargins( 0, 0, 0, 0 );
1358 hLayout->setSpacing( 5 );
1362 hLayout->addWidget( mRampWidget, 1 );
1364 mSpinBox =
new QSpinBox();
1366 const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( QStringLiteral(
"888%" ) );
1367 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1368 mSpinBox->setMinimum( 0 );
1369 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1374 mSpinBox->setSuffix( QChar( 176 ) );
1378 mSpinBox->setSuffix( tr(
"%" ) );
1380 hLayout->addWidget( mSpinBox );
1381 setLayout( hLayout );
1385 connect( mSpinBox,
static_cast < void ( QSpinBox::* )(
int )
> ( &QSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1392 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1396 mSpinBox->setSuffix( QChar( 176 ) );
1401 mSpinBox->setSuffix( tr(
"%" ) );
1406 mSpinBox->setSuffix( QString() );
1413 mRampWidget->blockSignals(
true );
1415 mRampWidget->blockSignals(
false );
1416 mSpinBox->blockSignals(
true );
1417 mSpinBox->setValue( convertRealToDisplay( value ) );
1418 mSpinBox->blockSignals(
false );
1425 mSpinBox->blockSignals(
true );
1427 mSpinBox->blockSignals(
false );
1430void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1435void QgsColorSliderWidget::spinChanged(
int value )
1437 const int convertedValue = convertDisplayToReal( value );
1443void QgsColorSliderWidget::rampChanged(
int value )
1445 mSpinBox->blockSignals(
true );
1446 mSpinBox->setValue( convertRealToDisplay( value ) );
1447 mSpinBox->blockSignals(
false );
1451int QgsColorSliderWidget::convertRealToDisplay(
const int realValue )
const
1457 return std::round( 100.0 * realValue / 255.0 );
1464int QgsColorSliderWidget::convertDisplayToReal(
const int displayValue )
const
1469 return std::round( 255.0 * displayValue / 100.0 );
1473 return displayValue;
1483 QHBoxLayout *hLayout =
new QHBoxLayout();
1484 hLayout->setContentsMargins( 0, 0, 0, 0 );
1485 hLayout->setSpacing( 0 );
1487 mLineEdit =
new QLineEdit(
nullptr );
1488 hLayout->addWidget( mLineEdit );
1490 mMenuButton =
new QToolButton( mLineEdit );
1492 mMenuButton->setCursor( Qt::ArrowCursor );
1493 mMenuButton->setFocusPolicy( Qt::NoFocus );
1494 mMenuButton->setStyleSheet( QStringLiteral(
"QToolButton { border: none; padding: 0px; }" ) );
1496 setLayout( hLayout );
1498 const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1499 mLineEdit->setStyleSheet( QStringLiteral(
"QLineEdit { padding-right: %1px; } " )
1500 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1502 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1503 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1507 mFormat = settings.
enumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ),
HexRgb );
1521 const QSize sz = mMenuButton->sizeHint();
1522 const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1523 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
1524 ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1527void QgsColorTextWidget::updateText()
1546void QgsColorTextWidget::textChanged()
1548 const QString testString = mLineEdit->text();
1551 if ( !
color.isValid() )
1562 if ( !containsAlpha )
1573void QgsColorTextWidget::showMenu()
1575 QMenu colorContextMenu;
1576 QAction *hexRgbaAction =
nullptr;
1577 QAction *rgbaAction =
nullptr;
1579 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1580 colorContextMenu.addAction( hexRgbAction );
1583 hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1584 colorContextMenu.addAction( hexRgbaAction );
1586 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1587 colorContextMenu.addAction( rgbAction );
1590 rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1591 colorContextMenu.addAction( rgbaAction );
1594 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1595 if ( selectedAction == hexRgbAction )
1599 else if ( hexRgbaAction && selectedAction == hexRgbaAction )
1603 else if ( selectedAction == rgbAction )
1607 else if ( rgbaAction && selectedAction == rgbaAction )
1614 settings.
setEnumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ), mFormat );
1621 mAllowAlpha = allowOpacity;
1630 , mColor2( QColor() )
1635void QgsColorPreviewWidget::drawColor(
const QColor &color, QRect rect, QPainter &painter )
1637 painter.setPen( Qt::NoPen );
1639 if (
color.alpha() < 255 )
1642 painter.setBrush( checkBrush );
1643 painter.drawRect( rect );
1648 const QBrush colorBrush = QBrush(
color );
1649 painter.setBrush( colorBrush );
1650 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1652 QColor opaqueColor = QColor(
color );
1653 opaqueColor.setAlpha( 255 );
1654 const QBrush opaqueBrush = QBrush( opaqueColor );
1655 painter.setBrush( opaqueBrush );
1656 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1661 const QBrush brush = QBrush(
color );
1662 painter.setBrush( brush );
1663 painter.drawRect( rect );
1670 QPainter painter(
this );
1672 if ( mColor2.isValid() )
1675 const int verticalSplit = std::round( height() / 2.0 );
1676 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1677 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1681 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1694 if (
color == mColor2 )
1704 if ( e->button() == Qt::LeftButton )
1706 mDragStartPosition = e->pos();
1713 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1722 if ( mColor2.isValid() )
1725 const int verticalSplit = std::round( height() / 2.0 );
1726 if ( mDragStartPosition.y() >= verticalSplit )
1728 clickedColor = mColor2;
1739 if ( !( e->buttons() & Qt::LeftButton ) )
1746 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1757 if ( mColor2.isValid() )
1760 const int verticalSplit = std::round( height() / 2.0 );
1761 if ( mDragStartPosition.y() >= verticalSplit )
1763 dragColor = mColor2;
1767 QDrag *drag =
new QDrag(
this );
1770 drag->exec( Qt::CopyAction );
1779 : QWidgetAction( parent )
1781 , mColorWidget( colorWidget )
1782 , mSuppressRecurse( false )
1783 , mDismissOnColorSelection( true )
1785 setDefaultWidget( mColorWidget );
1788 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1792void QgsColorWidgetAction::onHover()
1795 if ( mSuppressRecurse )
1802 mSuppressRecurse =
true;
1803 mMenu->setActiveAction(
this );
1804 mSuppressRecurse =
false;
1808void QgsColorWidgetAction::setColor(
const QColor &color )
1811 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)
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
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