30#include <QFontMetrics>
38#include <QResizeEvent>
40#include <QStyleOptionFrame>
43#include "moc_qgscolorwidgets.cpp"
45using namespace Qt::StringLiterals;
66 setAcceptDrops(
true );
83 QPixmap pixmap( iconSize, iconSize );
84 pixmap.fill( Qt::transparent );
86 painter.begin( &pixmap );
88 painter.fillRect( QRect( 0, 0, iconSize, iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
90 QColor pixmapColor =
color;
91 pixmapColor.setAlpha( 255 );
92 painter.setBrush( QBrush( pixmapColor ) );
93 painter.setPen( QPen( Qt::white ) );
94 painter.drawRect( QRect( 1, 1, iconSize - 2, iconSize - 2 ) );
196 return static_cast<int>( std::round(
hueF() *
HUE_MAX ) );
220 float_type clippedValue = std::clamp( newValue, 0.f, 1.f );
225 color.getCmykF( &
c, &m, &y, &k, &a );
230 color.setCmykF( clippedValue, m, y, k, a );
233 color.setCmykF(
c, clippedValue, y, k, a );
236 color.setCmykF(
c, m, clippedValue, k, a );
239 color.setCmykF(
c, m, y, clippedValue, a );
248 color.getRgbF( &r, &g, &b, &a );
250 color.getHsvF( &h, &s, &v );
255 color.setRedF( clippedValue );
258 color.setGreenF( clippedValue );
261 color.setBlueF( clippedValue );
264 color.setHsvF( clippedValue, s, v, a );
267 color.setHsvF( h, clippedValue, v, a );
270 color.setHsvF( h, s, clippedValue, a );
273 color.setAlphaF( clippedValue );
288 return QColor::Spec::Rgb;
293 return QColor::Spec::Hsv;
299 return QColor::Spec::Cmyk;
302 return QColor::Spec::Invalid;
313 static QPixmap sTranspBkgrd;
315 if ( sTranspBkgrd.isNull() )
327 if ( mimeColor.isValid() )
330 e->acceptProposedAction();
337 bool hasAlpha =
false;
340 if ( mimeColor.isValid() )
343 e->acceptProposedAction();
437 if (
color.hue() >= 0 )
459 QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
460 const int wheelStops = 20;
461 QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
462 for (
int pos = 0; pos <= wheelStops; ++pos )
464 const double relativePos =
static_cast<double>( pos ) / wheelStops;
465 gradColor.setHsvF( relativePos, 1, 1 );
466 wheelGradient.setColorAt( relativePos, gradColor );
468 mWheelBrush = QBrush( wheelGradient );
479 return QSize( size, size );
485 QPainter painter(
this );
487 if ( mWidgetImage.isNull() || mWheelImage.isNull() || mTriangleImage.isNull() )
489 createImages( size() );
493 mWidgetImage.fill( Qt::transparent );
494 QPainter imagePainter( &mWidgetImage );
495 imagePainter.setRenderHint( QPainter::Antialiasing );
504 const QPointF center = QPointF( mWidgetImage.width() / 2.0 / mWidgetImage.devicePixelRatioF(), mWidgetImage.height() / 2.0 / mWidgetImage.devicePixelRatioF() );
505 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 / mWidgetImage.devicePixelRatioF() ), center.y() - ( mWheelImage.height() / 2.0 / mWidgetImage.devicePixelRatioF() ) ), mWheelImage );
509 const double length = mWheelImage.width() / 2.0 / mWidgetImage.devicePixelRatioF();
510 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
511 hueMarkerLine.setAngle( h );
514 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
516 pen.setWidthF( 2 * mWidgetImage.devicePixelRatioF() );
518 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
519 imagePainter.setPen( pen );
520 imagePainter.drawLine( hueMarkerLine );
521 imagePainter.restore();
524 if ( mTriangleDirty )
528 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 / mWidgetImage.devicePixelRatioF() ), center.y() - ( mWheelImage.height() / 2.0 / mWidgetImage.devicePixelRatioF() ) ), mTriangleImage );
531 const double triangleRadius = length - ( mWheelThickness + 1 ) * mWheelImage.devicePixelRatioF();
535 const double hueRadians = ( h * M_PI / 180.0 );
536 const double hx = std::cos( hueRadians ) * triangleRadius;
537 const double hy = -std::sin( hueRadians ) * triangleRadius;
538 const double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
539 const double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
540 const double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
541 const double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
542 const double mx = ( sx + vx ) / 2.0;
543 const double my = ( sy + vy ) / 2.0;
545 const double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
546 const double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
547 const double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
550 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
551 imagePainter.setPen( pen );
552 imagePainter.setBrush( Qt::NoBrush );
553 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0 * mWheelImage.devicePixelRatioF(), 4.0 * mWheelImage.devicePixelRatioF() );
557 painter.drawImage( QRectF( 0, 0, width(), height() ), mWidgetImage );
566 mTriangleDirty =
true;
572void QgsColorWheel::createImages(
const QSizeF size )
574 const double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
575 mWheelThickness = wheelSize / 15.0;
578 const double pixelRatio = devicePixelRatioF();
579 mWheelImage = QImage( wheelSize * pixelRatio, wheelSize * pixelRatio, QImage::Format_ARGB32 );
580 mWheelImage.setDevicePixelRatio( pixelRatio );
581 mTriangleImage = QImage( wheelSize * pixelRatio, wheelSize * pixelRatio, QImage::Format_ARGB32 );
582 mTriangleImage.setDevicePixelRatio( pixelRatio );
583 mWidgetImage = QImage( size.width() * pixelRatio, size.height() * pixelRatio, QImage::Format_ARGB32 );
584 mWidgetImage.setDevicePixelRatio( pixelRatio );
588 mTriangleDirty =
true;
597void QgsColorWheel::setColorFromPos(
const QPointF pos )
599 const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
601 const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
603 QColor newColor = QColor();
610 if ( mClickedPart == QgsColorWheel::Triangle )
615 const double x = pos.x() - center.x();
616 const double y = pos.y() - center.y();
618 double eventAngleRadians = line.angle() * M_PI / 180.0;
619 const double hueRadians = h * 2 * M_PI;
620 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
621 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
622 const double length = mWheelImage.width() / 2.0 / mWheelImage.devicePixelRatioF();
623 const double triangleLength = length - mWheelThickness - 1;
625 const double a = 0.5 * triangleLength;
626 double b = std::tan( rad1 ) * a;
627 double r = std::sqrt( x * x + y * y );
628 const double maxR = std::sqrt( a * a + b * b );
632 const double dx = std::tan( rad1 ) * r;
633 double rad2 = std::atan( dx / maxR );
634 rad2 = std::min( rad2, M_PI / 3.0 );
635 rad2 = std::max( rad2, -M_PI / 3.0 );
636 eventAngleRadians += rad2 - rad1;
637 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
638 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
639 b = std::tan( rad1 ) * a;
640 r = std::sqrt( a * a + b * b );
643 const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
644 const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
645 const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
646 const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
647 s = std::min( std::max( 0.f,
static_cast<float>( newS ) ), 1.f );
648 l = std::min( std::max( 0.f,
static_cast<float>( newL ) ), 1.f );
649 newColor = QColor::fromHslF( h, s, l );
651 newColor.setHsvF( h, newColor.hsvSaturationF(), newColor.valueF(), alpha );
653 else if ( mClickedPart == QgsColorWheel::Wheel )
658 const qreal newHue = line.angle() /
HUE_MAX;
659 newColor = QColor::fromHsvF(
static_cast<float>( newHue ), s, v, alpha );
661 mTriangleDirty =
true;
683 setColorFromPos( event->pos() );
690 if ( event->button() == Qt::LeftButton )
696 const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
698 const double innerLength = mWheelImage.width() / 2.0 / mWheelImage.devicePixelRatioF() - mWheelThickness * mWheelImage.devicePixelRatioF();
699 if ( line.length() < innerLength )
701 mClickedPart = QgsColorWheel::Triangle;
705 mClickedPart = QgsColorWheel::Wheel;
707 setColorFromPos( event->pos() );
717 if ( event->button() == Qt::LeftButton )
720 mClickedPart = QgsColorWheel::None;
728void QgsColorWheel::invalidateImages()
731 mWidgetImage = QImage();
734void QgsColorWheel::createWheel()
736 if ( mWheelImage.isNull() )
741 const int maxSize = std::min( mWheelImage.width(), mWheelImage.height() );
742 const double wheelRadius = maxSize / 2.0 / mWheelImage.devicePixelRatioF();
744 mWheelImage.fill( Qt::transparent );
745 QPainter p( &mWheelImage );
746 p.setRenderHint( QPainter::Antialiasing );
747 p.setBrush( mWheelBrush );
748 p.setPen( Qt::NoPen );
751 p.translate( wheelRadius, wheelRadius );
752 p.drawEllipse( QPointF( 0, 0 ), wheelRadius, wheelRadius );
755 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
756 p.setBrush( QBrush( Qt::black ) );
757 p.drawEllipse( QPointF( 0, 0 ), wheelRadius - mWheelThickness * mWheelImage.devicePixelRatioF(), wheelRadius - mWheelThickness * mWheelImage.devicePixelRatioF() );
763void QgsColorWheel::createTriangle()
765 if ( mWheelImage.isNull() || mTriangleImage.isNull() )
770 const QPointF center = QPointF( mWheelImage.width() / 2.0 / mWheelImage.devicePixelRatioF(), mWheelImage.height() / 2.0 / mWheelImage.devicePixelRatioF() );
771 mTriangleImage.fill( Qt::transparent );
773 QPainter imagePainter( &mTriangleImage );
774 imagePainter.setRenderHint( QPainter::Antialiasing );
778 const double wheelRadius = mWheelImage.width() / 2.0 / mWheelImage.devicePixelRatioF();
779 const double triangleRadius = wheelRadius - mWheelThickness * mWheelImage.devicePixelRatioF() - 1;
782 const QColor pureColor = QColor::fromHsvF( angle, 1., 1. );
784 QColor alphaColor = QColor( pureColor );
785 alphaColor.setAlpha( 0 );
788 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 ) );
789 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
790 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 ) );
791 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
792 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
793 line1.setAngle( line1.angle() + angleDegree );
794 line2.setAngle( line2.angle() + angleDegree );
795 line3.setAngle( line3.angle() + angleDegree );
796 line4.setAngle( line4.angle() + angleDegree );
797 line5.setAngle( line5.angle() + angleDegree );
798 const QPointF p1 = line1.p2();
799 const QPointF p2 = line2.p2();
800 const QPointF p3 = line3.p2();
801 const QPointF p4 = line4.p2();
802 const QPointF p5 = line5.p2();
805 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
806 colorGrad.setColorAt( 0, alphaColor );
807 colorGrad.setColorAt( 1, pureColor );
808 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
809 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
810 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
813 triangle << p2 << p1 << p3 << p2;
814 imagePainter.setPen( Qt::NoPen );
816 imagePainter.setBrush( QBrush( Qt::black ) );
817 imagePainter.drawPolygon( triangle );
819 imagePainter.setBrush( QBrush( colorGrad ) );
820 imagePainter.drawPolygon( triangle );
822 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
823 imagePainter.setBrush( QBrush( whiteGrad ) );
824 imagePainter.drawPolygon( triangle );
829 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
830 imagePainter.setBrush( Qt::NoBrush );
831 imagePainter.setPen( QPen( Qt::transparent ) );
832 imagePainter.drawPolygon( triangle );
835 mTriangleDirty =
false;
846 setFocusPolicy( Qt::StrongFocus );
847 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
849 mBoxImage = std::make_unique<QImage>( width() -
static_cast<int>( mMargin * 2 ), height() -
static_cast<int>( mMargin * 2 ), QImage::Format_RGB32 );
858 return QSize( size, size );
864 QPainter painter(
this );
866 QStyleOptionFrame option;
867 option.initFrom(
this );
868 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
869 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
877 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
880 const double h = height();
881 const double w = width();
882 const double margin = mMargin;
883 const double xPos = ( mMargin + ( w - 2 * mMargin - 1 ) * xComponentValue() );
884 const double yPos = ( mMargin + ( h - 2 * mMargin - 1 ) - ( h - 2 * mMargin - 1 ) * yComponentValue() );
886 painter.setBrush( Qt::white );
887 painter.setPen( Qt::NoPen );
889 painter.drawRect( QRectF( xPos - 1, mMargin, 3, height() - 2 * margin - 1 ) );
890 painter.drawRect( QRectF( mMargin, yPos - 1, width() - 2 * margin - 1, 3 ) );
891 painter.setPen( Qt::black );
892 painter.drawLine( QLineF( xPos, mMargin, xPos, height() - margin - 1 ) );
893 painter.drawLine( QLineF( mMargin, yPos, width() - margin - 1, yPos ) );
911 mDirty |= ( (
mComponent ==
QgsColorWidget::Red && !
qgsDoubleNear(
mCurrentColor.redF(),
color.redF() ) ) || (
mComponent ==
QgsColorWidget::Green && !
qgsDoubleNear(
mCurrentColor.greenF(),
color.greenF() ) ) || (
mComponent ==
QgsColorWidget::Blue && !
qgsDoubleNear(
mCurrentColor.blueF(),
color.blueF() ) ) || (
mComponent ==
QgsColorWidget::Hue &&
color.hsvHueF() >= 0 && !
qgsDoubleNear(
hueF(),
color.hsvHueF() ) ) || (
mComponent ==
QgsColorWidget::Saturation && !
qgsDoubleNear(
mCurrentColor.hsvSaturationF(),
color.hsvSaturationF() ) ) || (
mComponent ==
QgsColorWidget::Value && !
qgsDoubleNear(
mCurrentColor.valueF(),
color.valueF() ) ) || (
mComponent ==
QgsColorWidget::Cyan && !
qgsDoubleNear(
mCurrentColor.cyanF(),
color.cyanF() ) ) || (
mComponent ==
QgsColorWidget::Magenta && !
qgsDoubleNear(
mCurrentColor.magentaF(),
color.magentaF() ) ) || (
mComponent ==
QgsColorWidget::Yellow && !
qgsDoubleNear(
mCurrentColor.yellowF(),
color.yellowF() ) ) || (
mComponent ==
QgsColorWidget::Black && !
qgsDoubleNear(
mCurrentColor.blackF(),
color.blackF() ) ) );
919 mBoxImage = std::make_unique<QImage>( event->size().width() -
static_cast<int>( mMargin * 2 ), event->size().height() -
static_cast<int>( mMargin * 2 ), QImage::Format_RGB32 );
921 QgsColorWidget::resizeEvent( event );
928 setColorFromPoint( event->pos() );
935 if ( event->button() == Qt::LeftButton )
938 setColorFromPoint( event->pos() );
948 if ( event->button() == Qt::LeftButton )
958void QgsColorBox::createBox()
960 const int maxValueX = mBoxImage->width();
961 const int maxValueY = mBoxImage->height();
965 float colorComponentValue;
967 for (
int y = 0; y < maxValueY; ++y )
969 QRgb *scanLine = ( QRgb * ) mBoxImage->scanLine( y );
971 colorComponentValue = 1.f -
static_cast<float>( y ) /
static_cast<float>( maxValueY );
972 alterColorF( currentColor, yComponent(), colorComponentValue );
973 for (
int x = 0; x < maxValueX; ++x )
975 colorComponentValue =
static_cast<float>( x ) /
static_cast<float>( maxValueY );
976 alterColorF( currentColor, xComponent(), colorComponentValue );
977 scanLine[x] = currentColor.rgb();
983float QgsColorBox::valueRangeX()
const
988float QgsColorBox::valueRangeY()
const
1021float QgsColorBox::yComponentValue()
const
1054float QgsColorBox::xComponentValue()
const
1059void QgsColorBox::setColorFromPoint( QPoint point )
1061 const float x =
static_cast<float>( point.x() );
1062 const float y =
static_cast<float>( point.y() );
1063 const float w =
static_cast<float>( width() );
1064 const float h =
static_cast<float>( height() );
1066 float valX = ( x - mMargin ) / ( w - 2 * mMargin - 1 );
1067 float valY = 1.f - ( y - mMargin ) / ( h - 2 * mMargin - 1 );
1078 if (
color.hueF() >= 0 )
1096 setFocusPolicy( Qt::StrongFocus );
1120 QPainter painter(
this );
1125 QStyleOptionFrame option;
1126 option.initFrom(
this );
1127 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1128 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1134 QStyleOptionFocusRect option;
1135 option.initFrom(
this );
1136 option.state = QStyle::State_KeyboardFocusChange;
1137 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1140 float w =
static_cast<float>( width() );
1141 float h =
static_cast<float>( height() );
1142 float margin =
static_cast<float>( mMargin );
1147 color.setAlphaF( 1.f );
1154 painter.setPen( pen );
1155 painter.setBrush( Qt::NoBrush );
1158 for (
int c = 0;
c <= maxValue; ++
c )
1160 float colorVal =
static_cast<float>(
c ) /
static_cast<float>( maxValue );
1164 colorVal = 1.f - colorVal;
1167 if (
color.hueF() < 0 )
1171 pen.setColor(
color );
1172 painter.setPen( pen );
1176 painter.drawLine( QLineF(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 ) );
1181 painter.drawLine( QLineF( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin ) );
1190 painter.setBrush( checkBrush );
1191 painter.setPen( Qt::NoPen );
1192 painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
1193 QLinearGradient colorGrad;
1197 colorGrad = QLinearGradient( margin, 0, w - margin - 1, 0 );
1202 colorGrad = QLinearGradient( 0, margin, 0, h - margin - 1 );
1205 transparent.setAlpha( 0 );
1206 colorGrad.setColorAt( 0, transparent );
1208 opaque.setAlpha( 255 );
1209 colorGrad.setColorAt( 1, opaque );
1210 const QBrush colorBrush = QBrush( colorGrad );
1211 painter.setBrush( colorBrush );
1212 painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
1218 painter.setRenderHint( QPainter::Antialiasing );
1219 painter.setBrush( QBrush( Qt::black ) );
1220 painter.setPen( Qt::NoPen );
1221 painter.translate( margin + ( w - 2 * margin ) *
componentValueF(), margin - 1 );
1222 painter.drawPolygon( mTopTriangle );
1223 painter.translate( 0, h - margin - 2 );
1224 painter.setBrush( QBrush( Qt::white ) );
1225 painter.drawPolygon( mBottomTriangle );
1231 const double ypos = margin + ( h - 2 * margin - 1 ) - ( h - 2 * margin - 1 ) *
componentValueF();
1232 painter.setBrush( Qt::white );
1233 painter.setPen( Qt::NoPen );
1234 painter.drawRect( QRectF( margin, ypos - 1, w - 2 * margin - 1, 3 ) );
1235 painter.setPen( Qt::black );
1236 painter.drawLine( QLineF( margin, ypos, w - margin - 1, ypos ) );
1246 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1251 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1258 if ( margin == mMargin )
1279 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1280 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1288 setColorFromPoint( event->pos() );
1297 const float delta = 1.f /
static_cast<float>(
componentRange() );
1298 if ( event->angleDelta().y() > 0 )
1322 if ( event->button() == Qt::LeftButton )
1325 setColorFromPoint( event->pos() );
1335 if ( event->button() == Qt::LeftButton )
1337 mIsDragging =
false;
1348 const float delta = 1.f /
static_cast<float>(
componentRange() );
1378 QgsColorWidget::keyPressEvent( event );
1393void QgsColorRampWidget::setColorFromPoint( QPointF point )
1397 const float margin =
static_cast<float>( mMargin );
1398 const float w =
static_cast<float>( width() );
1399 const float h =
static_cast<float>( height() );
1403 val = (
static_cast<float>( point.x() ) - margin ) / ( w - 2 * margin );
1407 val = 1.f - (
static_cast<float>( point.y() ) - margin ) / ( h - 2 * margin );
1430 QHBoxLayout *hLayout =
new QHBoxLayout();
1431 hLayout->setContentsMargins( 0, 0, 0, 0 );
1432 hLayout->setSpacing( 5 );
1436 hLayout->addWidget( mRampWidget, 1 );
1439 mSpinBox->setShowClearButton(
false );
1441 const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( u
"888.88%"_s );
1442 mSpinBox->setFixedWidth( largestCharWidth + 35 );
1443 mSpinBox->setMinimum( 0 );
1444 mSpinBox->setMaximum( convertRealToDisplay( 1.f ) );
1446 hLayout->addWidget( mSpinBox );
1447 setLayout( hLayout );
1451 connect( mSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1458 mSpinBox->setMaximum( convertRealToDisplay(
static_cast<float>(
componentRange() ) ) );
1463 mSpinBox->setSuffix( QChar( 176 ) );
1467 mSpinBox->setSuffix( tr(
"%" ) );
1472 mSpinBox->setSuffix( QString() );
1479 mRampWidget->blockSignals(
true );
1480 mRampWidget->setComponentValueF( value );
1481 mRampWidget->blockSignals(
false );
1482 mSpinBox->blockSignals(
true );
1483 mSpinBox->setValue( convertRealToDisplay( value ) );
1484 mSpinBox->blockSignals(
false );
1490 mRampWidget->setColor(
color );
1491 mSpinBox->blockSignals(
true );
1493 mSpinBox->blockSignals(
false );
1496void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1501void QgsColorSliderWidget::spinChanged(
double value )
1503 const float convertedValue = convertDisplayToReal(
static_cast<float>( value ) );
1505 mRampWidget->setComponentValueF( convertedValue );
1509void QgsColorSliderWidget::rampChanged(
float value )
1511 mSpinBox->blockSignals(
true );
1512 mSpinBox->setValue( convertRealToDisplay( value ) );
1513 mSpinBox->blockSignals(
false );
1517float QgsColorSliderWidget::convertRealToDisplay(
const float realValue )
const
1522 return realValue * 100.f;
1528 return realValue * 255.f;
1534float QgsColorSliderWidget::convertDisplayToReal(
const float displayValue )
const
1539 return displayValue / 100.f;
1542 return displayValue /
HUE_MAX;
1545 return displayValue / 255.f;
1558 QHBoxLayout *hLayout =
new QHBoxLayout();
1559 hLayout->setContentsMargins( 0, 0, 0, 0 );
1560 hLayout->setSpacing( 0 );
1562 mLineEdit =
new QLineEdit(
nullptr );
1563 hLayout->addWidget( mLineEdit );
1565 mMenuButton =
new QToolButton( mLineEdit );
1567 mMenuButton->setCursor( Qt::ArrowCursor );
1568 mMenuButton->setFocusPolicy( Qt::NoFocus );
1569 mMenuButton->setStyleSheet( u
"QToolButton { border: none; padding: 0px; }"_s );
1571 setLayout( hLayout );
1573 const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1574 mLineEdit->setStyleSheet( u
"QLineEdit { padding-right: %1px; } "_s.arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1576 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1577 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1594 const QSize sz = mMenuButton->sizeHint();
1595 const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1596 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(), ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1599void QgsColorTextWidget::updateText()
1618void QgsColorTextWidget::textChanged()
1620 const QString testString = mLineEdit->text();
1623 if ( !
color.isValid() )
1634 if ( !containsAlpha )
1645void QgsColorTextWidget::showMenu()
1647 QMenu colorContextMenu;
1648 QAction *hexRgbaAction =
nullptr;
1649 QAction *rgbaAction =
nullptr;
1651 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1652 colorContextMenu.addAction( hexRgbAction );
1655 hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1656 colorContextMenu.addAction( hexRgbaAction );
1658 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1659 colorContextMenu.addAction( rgbAction );
1662 rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1663 colorContextMenu.addAction( rgbaAction );
1666 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1667 if ( selectedAction == hexRgbAction )
1671 else if ( hexRgbaAction && selectedAction == hexRgbaAction )
1675 else if ( selectedAction == rgbAction )
1679 else if ( rgbaAction && selectedAction == rgbaAction )
1692 mAllowAlpha = allowOpacity;
1701 , mColor2( QColor() )
1704void QgsColorPreviewWidget::drawColor(
const QColor &color, QRect rect, QPainter &painter )
1706 painter.setPen( Qt::NoPen );
1708 if (
color.alpha() < 255 )
1711 painter.setBrush( checkBrush );
1712 painter.drawRect( rect );
1717 const QBrush colorBrush = QBrush(
color );
1718 painter.setBrush( colorBrush );
1719 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1721 QColor opaqueColor = QColor(
color );
1722 opaqueColor.setAlpha( 255 );
1723 const QBrush opaqueBrush = QBrush( opaqueColor );
1724 painter.setBrush( opaqueBrush );
1725 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1730 const QBrush brush = QBrush(
color );
1731 painter.setBrush( brush );
1732 painter.drawRect( rect );
1739 QPainter painter(
this );
1741 if ( mColor2.isValid() )
1744 const int verticalSplit = std::round( height() / 2.0 );
1745 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1746 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1750 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1763 if (
color == mColor2 )
1773 if ( e->button() == Qt::LeftButton )
1775 mDragStartPosition = e->pos();
1782 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1791 if ( mColor2.isValid() )
1794 const int verticalSplit = std::round( height() / 2.0 );
1795 if ( mDragStartPosition.y() >= verticalSplit )
1797 clickedColor = mColor2;
1807 if ( !( e->buttons() & Qt::LeftButton ) )
1814 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1825 if ( mColor2.isValid() )
1828 const int verticalSplit = std::round( height() / 2.0 );
1829 if ( mDragStartPosition.y() >= verticalSplit )
1831 dragColor = mColor2;
1835 QDrag *drag =
new QDrag(
this );
1838 drag->exec( Qt::CopyAction );
1847 : QWidgetAction( parent )
1851 setDefaultWidget( mColorWidget );
1854 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1858void QgsColorWidgetAction::onHover()
1861 if ( mSuppressRecurse )
1868 mSuppressRecurse =
true;
1869 mMenu->setActiveAction(
this );
1870 mSuppressRecurse =
false;
1874void QgsColorWidgetAction::setColor(
const QColor &color )
1877 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.
static const QgsSettingsEntryEnumFlag< ColorTextFormat > * settingsTextFormat
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
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
A utility class for dynamic handling of changes to screen properties.
void screenDpiChanged(double dpi)
Emitted whenever the screen dpi associated with the widget is changed.
A template class for enum and flag settings entry.
static QgsSettingsTreeNode * sTreeColorWidgets
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 std::unique_ptr< 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
#define Q_NOWARN_DEPRECATED_POP
#define BUILTIN_UNREACHABLE
#define Q_NOWARN_DEPRECATED_PUSH
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).