28#include <QFontMetrics>
35#include <QResizeEvent>
37#include <QStyleOptionFrame>
40#include "moc_qgscolorwidgets.cpp"
42using namespace Qt::StringLiterals;
60 setAcceptDrops(
true );
77 QPixmap pixmap( iconSize, iconSize );
78 pixmap.fill( Qt::transparent );
80 painter.begin( &pixmap );
82 painter.fillRect( QRect( 0, 0, iconSize, iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
84 QColor pixmapColor =
color;
85 pixmapColor.setAlpha( 255 );
86 painter.setBrush( QBrush( pixmapColor ) );
87 painter.setPen( QPen( Qt::white ) );
88 painter.drawRect( QRect( 1, 1, iconSize - 2, iconSize - 2 ) );
190 return static_cast<int>( std::round(
hueF() *
HUE_MAX ) );
214 float_type clippedValue = std::clamp( newValue, 0.f, 1.f );
219 color.getCmykF( &
c, &m, &y, &k, &a );
224 color.setCmykF( clippedValue, m, y, k, a );
227 color.setCmykF(
c, clippedValue, y, k, a );
230 color.setCmykF(
c, m, clippedValue, k, a );
233 color.setCmykF(
c, m, y, clippedValue, a );
242 color.getRgbF( &r, &g, &b, &a );
244 color.getHsvF( &h, &s, &v );
249 color.setRedF( clippedValue );
252 color.setGreenF( clippedValue );
255 color.setBlueF( clippedValue );
258 color.setHsvF( clippedValue, s, v, a );
261 color.setHsvF( h, clippedValue, v, a );
264 color.setHsvF( h, s, clippedValue, a );
267 color.setAlphaF( clippedValue );
282 return QColor::Spec::Rgb;
287 return QColor::Spec::Hsv;
293 return QColor::Spec::Cmyk;
296 return QColor::Spec::Invalid;
307 static QPixmap sTranspBkgrd;
309 if ( sTranspBkgrd.isNull() )
321 if ( mimeColor.isValid() )
324 e->acceptProposedAction();
331 bool hasAlpha =
false;
334 if ( mimeColor.isValid() )
337 e->acceptProposedAction();
431 if (
color.hue() >= 0 )
453 QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
454 const int wheelStops = 20;
455 QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
456 for (
int pos = 0; pos <= wheelStops; ++pos )
458 const double relativePos =
static_cast<double>( pos ) / wheelStops;
459 gradColor.setHsvF( relativePos, 1, 1 );
460 wheelGradient.setColorAt( relativePos, gradColor );
462 mWheelBrush = QBrush( wheelGradient );
470 return QSize( size, size );
476 QPainter painter(
this );
478 if ( mWidgetImage.isNull() || mWheelImage.isNull() || mTriangleImage.isNull() )
480 createImages( size() );
484 mWidgetImage.fill( Qt::transparent );
485 QPainter imagePainter( &mWidgetImage );
486 imagePainter.setRenderHint( QPainter::Antialiasing );
495 const QPointF center = QPointF( mWidgetImage.width() / 2.0, mWidgetImage.height() / 2.0 );
496 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mWheelImage );
500 const double length = mWheelImage.width() / 2.0;
501 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
502 hueMarkerLine.setAngle( h );
505 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
507 pen.setWidthF( 2 * devicePixelRatioF() );
509 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
510 imagePainter.setPen( pen );
511 imagePainter.drawLine( hueMarkerLine );
512 imagePainter.restore();
515 if ( mTriangleDirty )
519 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mTriangleImage );
522 const double triangleRadius = length - mWheelThickness * devicePixelRatioF() - 1;
526 const double hueRadians = ( h * M_PI / 180.0 );
527 const double hx = std::cos( hueRadians ) * triangleRadius;
528 const double hy = -std::sin( hueRadians ) * triangleRadius;
529 const double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
530 const double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
531 const double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
532 const double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
533 const double mx = ( sx + vx ) / 2.0;
534 const double my = ( sy + vy ) / 2.0;
536 const double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
537 const double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
538 const double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
541 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
542 imagePainter.setPen( pen );
543 imagePainter.setBrush( Qt::NoBrush );
544 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0 * devicePixelRatioF(), 4.0 * devicePixelRatioF() );
548 painter.drawImage( QRectF( 0, 0, width(), height() ), mWidgetImage );
557 mTriangleDirty =
true;
563void QgsColorWheel::createImages(
const QSizeF size )
565 const double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
566 mWheelThickness = wheelSize / 15.0;
569 const double pixelRatio = devicePixelRatioF();
570 mWheelImage = QImage( wheelSize * pixelRatio, wheelSize * pixelRatio, QImage::Format_ARGB32 );
571 mTriangleImage = QImage( wheelSize * pixelRatio, wheelSize * pixelRatio, QImage::Format_ARGB32 );
572 mWidgetImage = QImage( size.width() * pixelRatio, size.height() * pixelRatio, QImage::Format_ARGB32 );
576 mTriangleDirty =
true;
581 QgsColorWidget::resizeEvent( event );
584 if ( event->size().width() > parentWidget()->size().width() )
587 std::min( event->size().width(), parentWidget()->size().width() - 2 ),
588 std::min( event->size().height(), parentWidget()->size().height() - 2 )
591 createImages( newSize );
595 createImages( event->size() );
599 createImages( event->size() );
603void QgsColorWheel::setColorFromPos(
const QPointF pos )
605 const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
607 const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
609 QColor newColor = QColor();
616 if ( mClickedPart == QgsColorWheel::Triangle )
621 const double x = pos.x() - center.x();
622 const double y = pos.y() - center.y();
624 double eventAngleRadians = line.angle() * M_PI / 180.0;
625 const double hueRadians = h * 2 * M_PI;
626 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
627 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
628 const double length = mWheelImage.width() / 2.0 / devicePixelRatioF();
629 const double triangleLength = length - mWheelThickness - 1;
631 const double a = 0.5 * triangleLength;
632 double b = std::tan( rad1 ) * a;
633 double r = std::sqrt( x * x + y * y );
634 const double maxR = std::sqrt( a * a + b * b );
638 const double dx = std::tan( rad1 ) * r;
639 double rad2 = std::atan( dx / maxR );
640 rad2 = std::min( rad2, M_PI / 3.0 );
641 rad2 = std::max( rad2, -M_PI / 3.0 );
642 eventAngleRadians += rad2 - rad1;
643 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
644 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
645 b = std::tan( rad1 ) * a;
646 r = std::sqrt( a * a + b * b );
649 const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
650 const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
651 const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
652 const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
653 s = std::min( std::max( 0.f,
static_cast<float>( newS ) ), 1.f );
654 l = std::min( std::max( 0.f,
static_cast<float>( newL ) ), 1.f );
655 newColor = QColor::fromHslF( h, s, l );
657 newColor.setHsvF( h, newColor.hsvSaturationF(), newColor.valueF(), alpha );
659 else if ( mClickedPart == QgsColorWheel::Wheel )
664 const qreal newHue = line.angle() /
HUE_MAX;
665 newColor = QColor::fromHsvF(
static_cast<float>( newHue ), s, v, alpha );
667 mTriangleDirty =
true;
689 setColorFromPos( event->pos() );
696 if ( event->button() == Qt::LeftButton )
702 const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
704 const double innerLength = mWheelImage.width() / 2.0 / devicePixelRatioF() - mWheelThickness;
705 if ( line.length() < innerLength )
707 mClickedPart = QgsColorWheel::Triangle;
711 mClickedPart = QgsColorWheel::Wheel;
713 setColorFromPos( event->pos() );
723 if ( event->button() == Qt::LeftButton )
726 mClickedPart = QgsColorWheel::None;
734void QgsColorWheel::createWheel()
736 if ( mWheelImage.isNull() )
741 const int maxSize = std::min( mWheelImage.width(), mWheelImage.height() );
742 const double wheelRadius = maxSize / 2.0;
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, 0.0 ), wheelRadius, wheelRadius );
755 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
756 p.setBrush( QBrush( Qt::black ) );
757 p.drawEllipse( QPointF( 0, 0 ), wheelRadius - mWheelThickness * devicePixelRatioF(), wheelRadius - mWheelThickness * devicePixelRatioF() );
763void QgsColorWheel::createTriangle()
765 if ( mWheelImage.isNull() || mTriangleImage.isNull() )
770 const QPointF center = QPointF( mWheelImage.width() / 2.0, mWheelImage.height() / 2.0 );
771 mTriangleImage.fill( Qt::transparent );
773 QPainter imagePainter( &mTriangleImage );
774 imagePainter.setRenderHint( QPainter::Antialiasing );
778 const double wheelRadius = mWheelImage.width() / 2.0;
779 const double triangleRadius = wheelRadius - mWheelThickness * 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 =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
860 return QSize( size, size );
866 QPainter painter(
this );
868 QStyleOptionFrame option;
869 option.initFrom(
this );
870 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
871 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
879 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
882 const double h = height();
883 const double w = width();
884 const double margin = mMargin;
885 const double xPos = ( mMargin + ( w - 2 * mMargin - 1 ) * xComponentValue() );
886 const double yPos = ( mMargin + ( h - 2 * mMargin - 1 ) - ( h - 2 * mMargin - 1 ) * yComponentValue() );
888 painter.setBrush( Qt::white );
889 painter.setPen( Qt::NoPen );
891 painter.drawRect( QRectF( xPos - 1, mMargin, 3, height() - 2 * margin - 1 ) );
892 painter.drawRect( QRectF( mMargin, yPos - 1, width() - 2 * margin - 1, 3 ) );
893 painter.setPen( Qt::black );
894 painter.drawLine( QLineF( xPos, mMargin, xPos, height() - margin - 1 ) );
895 painter.drawLine( QLineF( mMargin, yPos, width() - margin - 1, yPos ) );
913 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() ) ) );
922 mBoxImage =
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
923 QgsColorWidget::resizeEvent( event );
930 setColorFromPoint( event->pos() );
937 if ( event->button() == Qt::LeftButton )
940 setColorFromPoint( event->pos() );
950 if ( event->button() == Qt::LeftButton )
960void QgsColorBox::createBox()
962 const int maxValueX = mBoxImage->width();
963 const int maxValueY = mBoxImage->height();
967 float colorComponentValue;
969 for (
int y = 0; y < maxValueY; ++y )
971 QRgb *scanLine = ( QRgb * ) mBoxImage->scanLine( y );
973 colorComponentValue = 1.f -
static_cast<float>( y ) /
static_cast<float>( maxValueY );
974 alterColorF( currentColor, yComponent(), colorComponentValue );
975 for (
int x = 0; x < maxValueX; ++x )
977 colorComponentValue =
static_cast<float>( x ) /
static_cast<float>( maxValueY );
978 alterColorF( currentColor, xComponent(), colorComponentValue );
979 scanLine[x] = currentColor.rgb();
985float QgsColorBox::valueRangeX()
const
990float QgsColorBox::valueRangeY()
const
1023float QgsColorBox::yComponentValue()
const
1056float QgsColorBox::xComponentValue()
const
1061void QgsColorBox::setColorFromPoint( QPoint point )
1063 const float x =
static_cast<float>( point.x() );
1064 const float y =
static_cast<float>( point.y() );
1065 const float w =
static_cast<float>( width() );
1066 const float h =
static_cast<float>( height() );
1068 float valX = ( x - mMargin ) / ( w - 2 * mMargin - 1 );
1069 float valY = 1.f - ( y - mMargin ) / ( h - 2 * mMargin - 1 );
1080 if (
color.hueF() >= 0 )
1098 setFocusPolicy( Qt::StrongFocus );
1122 QPainter painter(
this );
1127 QStyleOptionFrame option;
1128 option.initFrom(
this );
1129 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1130 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1136 QStyleOptionFocusRect option;
1137 option.initFrom(
this );
1138 option.state = QStyle::State_KeyboardFocusChange;
1139 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1142 float w =
static_cast<float>( width() );
1143 float h =
static_cast<float>( height() );
1144 float margin =
static_cast<float>( mMargin );
1149 color.setAlphaF( 1.f );
1156 painter.setPen( pen );
1157 painter.setBrush( Qt::NoBrush );
1160 for (
int c = 0;
c <= maxValue; ++
c )
1162 float colorVal =
static_cast<float>(
c ) /
static_cast<float>( maxValue );
1166 colorVal = 1.f - colorVal;
1169 if (
color.hueF() < 0 )
1173 pen.setColor(
color );
1174 painter.setPen( pen );
1178 painter.drawLine( QLineF(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 ) );
1183 painter.drawLine( QLineF( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin ) );
1192 painter.setBrush( checkBrush );
1193 painter.setPen( Qt::NoPen );
1194 painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
1195 QLinearGradient colorGrad;
1199 colorGrad = QLinearGradient( margin, 0, w - margin - 1, 0 );
1204 colorGrad = QLinearGradient( 0, margin, 0, h - margin - 1 );
1207 transparent.setAlpha( 0 );
1208 colorGrad.setColorAt( 0, transparent );
1210 opaque.setAlpha( 255 );
1211 colorGrad.setColorAt( 1, opaque );
1212 const QBrush colorBrush = QBrush( colorGrad );
1213 painter.setBrush( colorBrush );
1214 painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
1220 painter.setRenderHint( QPainter::Antialiasing );
1221 painter.setBrush( QBrush( Qt::black ) );
1222 painter.setPen( Qt::NoPen );
1223 painter.translate( margin + ( w - 2 * margin ) *
componentValueF(), margin - 1 );
1224 painter.drawPolygon( mTopTriangle );
1225 painter.translate( 0, h - margin - 2 );
1226 painter.setBrush( QBrush( Qt::white ) );
1227 painter.drawPolygon( mBottomTriangle );
1233 const double ypos = margin + ( h - 2 * margin - 1 ) - ( h - 2 * margin - 1 ) *
componentValueF();
1234 painter.setBrush( Qt::white );
1235 painter.setPen( Qt::NoPen );
1236 painter.drawRect( QRectF( margin, ypos - 1, w - 2 * margin - 1, 3 ) );
1237 painter.setPen( Qt::black );
1238 painter.drawLine( QLineF( margin, ypos, w - margin - 1, ypos ) );
1248 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1253 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1260 if ( margin == mMargin )
1281 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1282 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1290 setColorFromPoint( event->pos() );
1299 const float delta = 1.f /
static_cast<float>(
componentRange() );
1300 if ( event->angleDelta().y() > 0 )
1324 if ( event->button() == Qt::LeftButton )
1327 setColorFromPoint( event->pos() );
1337 if ( event->button() == Qt::LeftButton )
1339 mIsDragging =
false;
1350 const float delta = 1.f /
static_cast<float>(
componentRange() );
1384 QgsColorWidget::keyPressEvent( event );
1399void QgsColorRampWidget::setColorFromPoint( QPointF point )
1403 const float margin =
static_cast<float>( mMargin );
1404 const float w =
static_cast<float>( width() );
1405 const float h =
static_cast<float>( height() );
1409 val = (
static_cast<float>( point.x() ) - margin ) / ( w - 2 * margin );
1413 val = 1.f - (
static_cast<float>( point.y() ) - margin ) / ( h - 2 * margin );
1436 QHBoxLayout *hLayout =
new QHBoxLayout();
1437 hLayout->setContentsMargins( 0, 0, 0, 0 );
1438 hLayout->setSpacing( 5 );
1442 hLayout->addWidget( mRampWidget, 1 );
1445 mSpinBox->setShowClearButton(
false );
1447 const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( u
"888%"_s );
1448 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1449 mSpinBox->setMinimum( 0 );
1450 mSpinBox->setMaximum( convertRealToDisplay( 1.f ) );
1452 hLayout->addWidget( mSpinBox );
1453 setLayout( hLayout );
1457 connect( mSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1464 mSpinBox->setMaximum( convertRealToDisplay(
static_cast<float>(
componentRange() ) ) );
1469 mSpinBox->setSuffix( QChar( 176 ) );
1473 mSpinBox->setSuffix( tr(
"%" ) );
1478 mSpinBox->setSuffix( QString() );
1485 mRampWidget->blockSignals(
true );
1486 mRampWidget->setComponentValueF( value );
1487 mRampWidget->blockSignals(
false );
1488 mSpinBox->blockSignals(
true );
1489 mSpinBox->setValue( convertRealToDisplay( value ) );
1490 mSpinBox->blockSignals(
false );
1496 mRampWidget->setColor(
color );
1497 mSpinBox->blockSignals(
true );
1499 mSpinBox->blockSignals(
false );
1502void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1507void QgsColorSliderWidget::spinChanged(
double value )
1509 const float convertedValue = convertDisplayToReal(
static_cast<float>( value ) );
1511 mRampWidget->setComponentValueF( convertedValue );
1515void QgsColorSliderWidget::rampChanged(
float value )
1517 mSpinBox->blockSignals(
true );
1518 mSpinBox->setValue( convertRealToDisplay( value ) );
1519 mSpinBox->blockSignals(
false );
1523float QgsColorSliderWidget::convertRealToDisplay(
const float realValue )
const
1528 return realValue * 100.f;
1534 return realValue * 255.f;
1540float QgsColorSliderWidget::convertDisplayToReal(
const float displayValue )
const
1545 return displayValue / 100.f;
1548 return displayValue /
HUE_MAX;
1551 return displayValue / 255.f;
1564 QHBoxLayout *hLayout =
new QHBoxLayout();
1565 hLayout->setContentsMargins( 0, 0, 0, 0 );
1566 hLayout->setSpacing( 0 );
1568 mLineEdit =
new QLineEdit(
nullptr );
1569 hLayout->addWidget( mLineEdit );
1571 mMenuButton =
new QToolButton( mLineEdit );
1573 mMenuButton->setCursor( Qt::ArrowCursor );
1574 mMenuButton->setFocusPolicy( Qt::NoFocus );
1575 mMenuButton->setStyleSheet( u
"QToolButton { border: none; padding: 0px; }"_s );
1577 setLayout( hLayout );
1579 const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1580 mLineEdit->setStyleSheet( u
"QLineEdit { padding-right: %1px; } "_s
1581 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1583 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1584 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1588 mFormat = settings.
enumValue( u
"ColorWidgets/textWidgetFormat"_s,
HexRgb );
1602 const QSize sz = mMenuButton->sizeHint();
1603 const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1604 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(), ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1607void QgsColorTextWidget::updateText()
1626void QgsColorTextWidget::textChanged()
1628 const QString testString = mLineEdit->text();
1631 if ( !
color.isValid() )
1642 if ( !containsAlpha )
1653void QgsColorTextWidget::showMenu()
1655 QMenu colorContextMenu;
1656 QAction *hexRgbaAction =
nullptr;
1657 QAction *rgbaAction =
nullptr;
1659 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1660 colorContextMenu.addAction( hexRgbAction );
1663 hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1664 colorContextMenu.addAction( hexRgbaAction );
1666 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1667 colorContextMenu.addAction( rgbAction );
1670 rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1671 colorContextMenu.addAction( rgbaAction );
1674 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1675 if ( selectedAction == hexRgbAction )
1679 else if ( hexRgbaAction && selectedAction == hexRgbaAction )
1683 else if ( selectedAction == rgbAction )
1687 else if ( rgbaAction && selectedAction == rgbaAction )
1693 QgsSettings settings;
1694 settings.
setEnumValue( u
"ColorWidgets/textWidgetFormat"_s, mFormat );
1701 mAllowAlpha = allowOpacity;
1710 , mColor2( QColor() )
1714void QgsColorPreviewWidget::drawColor(
const QColor &color, QRect rect, QPainter &painter )
1716 painter.setPen( Qt::NoPen );
1718 if (
color.alpha() < 255 )
1721 painter.setBrush( checkBrush );
1722 painter.drawRect( rect );
1727 const QBrush colorBrush = QBrush(
color );
1728 painter.setBrush( colorBrush );
1729 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1731 QColor opaqueColor = QColor(
color );
1732 opaqueColor.setAlpha( 255 );
1733 const QBrush opaqueBrush = QBrush( opaqueColor );
1734 painter.setBrush( opaqueBrush );
1735 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1740 const QBrush brush = QBrush(
color );
1741 painter.setBrush( brush );
1742 painter.drawRect( rect );
1749 QPainter painter(
this );
1751 if ( mColor2.isValid() )
1754 const int verticalSplit = std::round( height() / 2.0 );
1755 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1756 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1760 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1773 if (
color == mColor2 )
1783 if ( e->button() == Qt::LeftButton )
1785 mDragStartPosition = e->pos();
1792 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1801 if ( mColor2.isValid() )
1804 const int verticalSplit = std::round( height() / 2.0 );
1805 if ( mDragStartPosition.y() >= verticalSplit )
1807 clickedColor = mColor2;
1817 if ( !( e->buttons() & Qt::LeftButton ) )
1824 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1835 if ( mColor2.isValid() )
1838 const int verticalSplit = std::round( height() / 2.0 );
1839 if ( mDragStartPosition.y() >= verticalSplit )
1841 dragColor = mColor2;
1845 QDrag *drag =
new QDrag(
this );
1848 drag->exec( Qt::CopyAction );
1857 : QWidgetAction( parent )
1861 setDefaultWidget( mColorWidget );
1864 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1868void QgsColorWidgetAction::onHover()
1871 if ( mSuppressRecurse )
1878 mSuppressRecurse =
true;
1879 mMenu->setActiveAction(
this );
1880 mSuppressRecurse =
false;
1884void QgsColorWidgetAction::setColor(
const QColor &color )
1887 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
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
Stores settings for use within QGIS.
void setEnumValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on an enum.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
static QColor parseColorWithAlpha(const QString &colorStr, bool &containsAlpha, bool strictEval=false)
Attempts to parse a string as a color using a variety of common formats, including hex codes,...
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored).
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#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).