28#include <QFontMetrics>
35#include <QResizeEvent>
36#include <QStyleOptionFrame>
39#include "moc_qgscolorwidgets.cpp"
57 setAcceptDrops(
true );
74 QPixmap pixmap( iconSize, iconSize );
75 pixmap.fill( Qt::transparent );
77 painter.begin( &pixmap );
79 painter.fillRect( QRect( 0, 0, iconSize, iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
81 QColor pixmapColor =
color;
82 pixmapColor.setAlpha( 255 );
83 painter.setBrush( QBrush( pixmapColor ) );
84 painter.setPen( QPen( Qt::white ) );
85 painter.drawRect( QRect( 1, 1, iconSize - 2, iconSize - 2 ) );
187 return static_cast<int>( std::round(
hueF() *
HUE_MAX ) );
211 float_type clippedValue = std::clamp( newValue, 0.f, 1.f );
216 color.getCmykF( &
c, &m, &y, &k, &a );
221 color.setCmykF( clippedValue, m, y, k, a );
224 color.setCmykF(
c, clippedValue, y, k, a );
227 color.setCmykF(
c, m, clippedValue, k, a );
230 color.setCmykF(
c, m, y, clippedValue, a );
239 color.getRgbF( &r, &g, &b, &a );
241 color.getHsvF( &h, &s, &v );
246 color.setRedF( clippedValue );
249 color.setGreenF( clippedValue );
252 color.setBlueF( clippedValue );
255 color.setHsvF( clippedValue, s, v, a );
258 color.setHsvF( h, clippedValue, v, a );
261 color.setHsvF( h, s, clippedValue, a );
264 color.setAlphaF( clippedValue );
279 return QColor::Spec::Rgb;
284 return QColor::Spec::Hsv;
290 return QColor::Spec::Cmyk;
293 return QColor::Spec::Invalid;
304 static QPixmap sTranspBkgrd;
306 if ( sTranspBkgrd.isNull() )
318 if ( mimeColor.isValid() )
321 e->acceptProposedAction();
328 bool hasAlpha =
false;
331 if ( mimeColor.isValid() )
334 e->acceptProposedAction();
428 if (
color.hue() >= 0 )
450 QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
451 const int wheelStops = 20;
452 QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
453 for (
int pos = 0; pos <= wheelStops; ++pos )
455 const double relativePos =
static_cast<double>( pos ) / wheelStops;
456 gradColor.setHsvF( relativePos, 1, 1 );
457 wheelGradient.setColorAt( relativePos, gradColor );
459 mWheelBrush = QBrush( wheelGradient );
467 return QSize( size, size );
473 QPainter painter(
this );
475 if ( mWidgetImage.isNull() || mWheelImage.isNull() || mTriangleImage.isNull() )
477 createImages( size() );
481 mWidgetImage.fill( Qt::transparent );
482 QPainter imagePainter( &mWidgetImage );
483 imagePainter.setRenderHint( QPainter::Antialiasing );
492 const QPointF center = QPointF( mWidgetImage.width() / 2.0, mWidgetImage.height() / 2.0 );
493 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mWheelImage );
497 const double length = mWheelImage.width() / 2.0;
498 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
499 hueMarkerLine.setAngle( h );
502 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
504 pen.setWidthF( 2 * devicePixelRatioF() );
506 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
507 imagePainter.setPen( pen );
508 imagePainter.drawLine( hueMarkerLine );
509 imagePainter.restore();
512 if ( mTriangleDirty )
516 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mTriangleImage );
519 const double triangleRadius = length - mWheelThickness * devicePixelRatioF() - 1;
523 const double hueRadians = ( h * M_PI / 180.0 );
524 const double hx = std::cos( hueRadians ) * triangleRadius;
525 const double hy = -std::sin( hueRadians ) * triangleRadius;
526 const double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
527 const double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
528 const double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
529 const double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
530 const double mx = ( sx + vx ) / 2.0;
531 const double my = ( sy + vy ) / 2.0;
533 const double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
534 const double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
535 const double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
538 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
539 imagePainter.setPen( pen );
540 imagePainter.setBrush( Qt::NoBrush );
541 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0 * devicePixelRatioF(), 4.0 * devicePixelRatioF() );
545 painter.drawImage( QRectF( 0, 0, width(), height() ), mWidgetImage );
554 mTriangleDirty =
true;
560void QgsColorWheel::createImages(
const QSizeF size )
562 const double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
563 mWheelThickness = wheelSize / 15.0;
566 const double pixelRatio = devicePixelRatioF();
567 mWheelImage = QImage( wheelSize * pixelRatio, wheelSize * pixelRatio, QImage::Format_ARGB32 );
568 mTriangleImage = QImage( wheelSize * pixelRatio, wheelSize * pixelRatio, QImage::Format_ARGB32 );
569 mWidgetImage = QImage( size.width() * pixelRatio, size.height() * pixelRatio, QImage::Format_ARGB32 );
573 mTriangleDirty =
true;
578 QgsColorWidget::resizeEvent( event );
581 if ( event->size().width() > parentWidget()->size().width() )
584 std::min( event->size().width(), parentWidget()->size().width() - 2 ),
585 std::min( event->size().height(), parentWidget()->size().height() - 2 )
588 createImages( newSize );
592 createImages( event->size() );
596 createImages( event->size() );
600void QgsColorWheel::setColorFromPos(
const QPointF pos )
602 const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
604 const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
606 QColor newColor = QColor();
613 if ( mClickedPart == QgsColorWheel::Triangle )
618 const double x = pos.x() - center.x();
619 const double y = pos.y() - center.y();
621 double eventAngleRadians = line.angle() * M_PI / 180.0;
622 const double hueRadians = h * 2 * M_PI;
623 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
624 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
625 const double length = mWheelImage.width() / 2.0 / devicePixelRatioF();
626 const double triangleLength = length - mWheelThickness - 1;
628 const double a = 0.5 * triangleLength;
629 double b = std::tan( rad1 ) * a;
630 double r = std::sqrt( x * x + y * y );
631 const double maxR = std::sqrt( a * a + b * b );
635 const double dx = std::tan( rad1 ) * r;
636 double rad2 = std::atan( dx / maxR );
637 rad2 = std::min( rad2, M_PI / 3.0 );
638 rad2 = std::max( rad2, -M_PI / 3.0 );
639 eventAngleRadians += rad2 - rad1;
640 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
641 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
642 b = std::tan( rad1 ) * a;
643 r = std::sqrt( a * a + b * b );
646 const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
647 const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
648 const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
649 const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
650 s = std::min( std::max( 0.f,
static_cast<float>( newS ) ), 1.f );
651 l = std::min( std::max( 0.f,
static_cast<float>( newL ) ), 1.f );
652 newColor = QColor::fromHslF( h, s, l );
654 newColor.setHsvF( h, newColor.hsvSaturationF(), newColor.valueF(), alpha );
656 else if ( mClickedPart == QgsColorWheel::Wheel )
661 const qreal newHue = line.angle() /
HUE_MAX;
662 newColor = QColor::fromHsvF(
static_cast<float>( newHue ), s, v, alpha );
664 mTriangleDirty =
true;
686 setColorFromPos( event->pos() );
693 if ( event->button() == Qt::LeftButton )
699 const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
701 const double innerLength = mWheelImage.width() / 2.0 / devicePixelRatioF() - mWheelThickness;
702 if ( line.length() < innerLength )
704 mClickedPart = QgsColorWheel::Triangle;
708 mClickedPart = QgsColorWheel::Wheel;
710 setColorFromPos( event->pos() );
720 if ( event->button() == Qt::LeftButton )
723 mClickedPart = QgsColorWheel::None;
731void QgsColorWheel::createWheel()
733 if ( mWheelImage.isNull() )
738 const int maxSize = std::min( mWheelImage.width(), mWheelImage.height() );
739 const double wheelRadius = maxSize / 2.0;
741 mWheelImage.fill( Qt::transparent );
742 QPainter p( &mWheelImage );
743 p.setRenderHint( QPainter::Antialiasing );
744 p.setBrush( mWheelBrush );
745 p.setPen( Qt::NoPen );
748 p.translate( wheelRadius, wheelRadius );
749 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
752 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
753 p.setBrush( QBrush( Qt::black ) );
754 p.drawEllipse( QPointF( 0, 0 ), wheelRadius - mWheelThickness * devicePixelRatioF(), wheelRadius - mWheelThickness * devicePixelRatioF() );
760void QgsColorWheel::createTriangle()
762 if ( mWheelImage.isNull() || mTriangleImage.isNull() )
767 const QPointF center = QPointF( mWheelImage.width() / 2.0, mWheelImage.height() / 2.0 );
768 mTriangleImage.fill( Qt::transparent );
770 QPainter imagePainter( &mTriangleImage );
771 imagePainter.setRenderHint( QPainter::Antialiasing );
775 const double wheelRadius = mWheelImage.width() / 2.0;
776 const double triangleRadius = wheelRadius - mWheelThickness * devicePixelRatioF() - 1;
779 const QColor pureColor = QColor::fromHsvF( angle, 1., 1. );
781 QColor alphaColor = QColor( pureColor );
782 alphaColor.setAlpha( 0 );
785 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 ) );
786 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
787 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 ) );
788 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
789 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
790 line1.setAngle( line1.angle() + angleDegree );
791 line2.setAngle( line2.angle() + angleDegree );
792 line3.setAngle( line3.angle() + angleDegree );
793 line4.setAngle( line4.angle() + angleDegree );
794 line5.setAngle( line5.angle() + angleDegree );
795 const QPointF p1 = line1.p2();
796 const QPointF p2 = line2.p2();
797 const QPointF p3 = line3.p2();
798 const QPointF p4 = line4.p2();
799 const QPointF p5 = line5.p2();
802 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
803 colorGrad.setColorAt( 0, alphaColor );
804 colorGrad.setColorAt( 1, pureColor );
805 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
806 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
807 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
810 triangle << p2 << p1 << p3 << p2;
811 imagePainter.setPen( Qt::NoPen );
813 imagePainter.setBrush( QBrush( Qt::black ) );
814 imagePainter.drawPolygon( triangle );
816 imagePainter.setBrush( QBrush( colorGrad ) );
817 imagePainter.drawPolygon( triangle );
819 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
820 imagePainter.setBrush( QBrush( whiteGrad ) );
821 imagePainter.drawPolygon( triangle );
826 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
827 imagePainter.setBrush( Qt::NoBrush );
828 imagePainter.setPen( QPen( Qt::transparent ) );
829 imagePainter.drawPolygon( triangle );
832 mTriangleDirty =
false;
843 setFocusPolicy( Qt::StrongFocus );
844 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
846 mBoxImage =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
857 return QSize( size, size );
863 QPainter painter(
this );
865 QStyleOptionFrame option;
866 option.initFrom(
this );
867 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
868 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
876 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
879 const double h = height();
880 const double w = width();
881 const double margin = mMargin;
882 const double xPos = ( mMargin + ( w - 2 * mMargin - 1 ) * xComponentValue() );
883 const double yPos = ( mMargin + ( h - 2 * mMargin - 1 ) - ( h - 2 * mMargin - 1 ) * yComponentValue() );
885 painter.setBrush( Qt::white );
886 painter.setPen( Qt::NoPen );
888 painter.drawRect( QRectF( xPos - 1, mMargin, 3, height() - 2 * margin - 1 ) );
889 painter.drawRect( QRectF( mMargin, yPos - 1, width() - 2 * margin - 1, 3 ) );
890 painter.setPen( Qt::black );
891 painter.drawLine( QLineF( xPos, mMargin, xPos, height() - margin - 1 ) );
892 painter.drawLine( QLineF( mMargin, yPos, width() - margin - 1, yPos ) );
910 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 =
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
920 QgsColorWidget::resizeEvent( event );
927 setColorFromPoint( event->pos() );
934 if ( event->button() == Qt::LeftButton )
937 setColorFromPoint( event->pos() );
947 if ( event->button() == Qt::LeftButton )
957void QgsColorBox::createBox()
959 const int maxValueX = mBoxImage->width();
960 const int maxValueY = mBoxImage->height();
964 float colorComponentValue;
966 for (
int y = 0; y < maxValueY; ++y )
968 QRgb *scanLine = ( QRgb * ) mBoxImage->scanLine( y );
970 colorComponentValue = 1.f -
static_cast<float>( y ) /
static_cast<float>( maxValueY );
971 alterColorF( currentColor, yComponent(), colorComponentValue );
972 for (
int x = 0; x < maxValueX; ++x )
974 colorComponentValue =
static_cast<float>( x ) /
static_cast<float>( maxValueY );
975 alterColorF( currentColor, xComponent(), colorComponentValue );
976 scanLine[x] = currentColor.rgb();
982float QgsColorBox::valueRangeX()
const
987float QgsColorBox::valueRangeY()
const
1020float QgsColorBox::yComponentValue()
const
1053float QgsColorBox::xComponentValue()
const
1058void QgsColorBox::setColorFromPoint( QPoint point )
1060 const float x =
static_cast<float>( point.x() );
1061 const float y =
static_cast<float>( point.y() );
1062 const float w =
static_cast<float>( width() );
1063 const float h =
static_cast<float>( height() );
1065 float valX = ( x - mMargin ) / ( w - 2 * mMargin - 1 );
1066 float valY = 1.f - ( y - mMargin ) / ( h - 2 * mMargin - 1 );
1077 if (
color.hueF() >= 0 )
1095 setFocusPolicy( Qt::StrongFocus );
1119 QPainter painter(
this );
1124 QStyleOptionFrame option;
1125 option.initFrom(
this );
1126 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1127 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1133 QStyleOptionFocusRect option;
1134 option.initFrom(
this );
1135 option.state = QStyle::State_KeyboardFocusChange;
1136 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1139 float w =
static_cast<float>( width() );
1140 float h =
static_cast<float>( height() );
1141 float margin =
static_cast<float>( mMargin );
1146 color.setAlphaF( 1.f );
1153 painter.setPen( pen );
1154 painter.setBrush( Qt::NoBrush );
1157 for (
int c = 0;
c <= maxValue; ++
c )
1159 float colorVal =
static_cast<float>(
c ) /
static_cast<float>( maxValue );
1163 colorVal = 1.f - colorVal;
1166 if (
color.hueF() < 0 )
1170 pen.setColor(
color );
1171 painter.setPen( pen );
1175 painter.drawLine( QLineF(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 ) );
1180 painter.drawLine( QLineF( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin ) );
1189 painter.setBrush( checkBrush );
1190 painter.setPen( Qt::NoPen );
1191 painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
1192 QLinearGradient colorGrad;
1196 colorGrad = QLinearGradient( margin, 0, w - margin - 1, 0 );
1201 colorGrad = QLinearGradient( 0, margin, 0, h - margin - 1 );
1204 transparent.setAlpha( 0 );
1205 colorGrad.setColorAt( 0, transparent );
1207 opaque.setAlpha( 255 );
1208 colorGrad.setColorAt( 1, opaque );
1209 const QBrush colorBrush = QBrush( colorGrad );
1210 painter.setBrush( colorBrush );
1211 painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
1217 painter.setRenderHint( QPainter::Antialiasing );
1218 painter.setBrush( QBrush( Qt::black ) );
1219 painter.setPen( Qt::NoPen );
1220 painter.translate( margin + ( w - 2 * margin ) *
componentValueF(), margin - 1 );
1221 painter.drawPolygon( mTopTriangle );
1222 painter.translate( 0, h - margin - 2 );
1223 painter.setBrush( QBrush( Qt::white ) );
1224 painter.drawPolygon( mBottomTriangle );
1230 const double ypos = margin + ( h - 2 * margin - 1 ) - ( h - 2 * margin - 1 ) *
componentValueF();
1231 painter.setBrush( Qt::white );
1232 painter.setPen( Qt::NoPen );
1233 painter.drawRect( QRectF( margin, ypos - 1, w - 2 * margin - 1, 3 ) );
1234 painter.setPen( Qt::black );
1235 painter.drawLine( QLineF( margin, ypos, w - margin - 1, ypos ) );
1245 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1250 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1257 if ( margin == mMargin )
1278 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1279 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1287 setColorFromPoint( event->pos() );
1296 const float delta = 1.f /
static_cast<float>(
componentRange() );
1297 if ( event->angleDelta().y() > 0 )
1321 if ( event->button() == Qt::LeftButton )
1324 setColorFromPoint( event->pos() );
1334 if ( event->button() == Qt::LeftButton )
1336 mIsDragging =
false;
1347 const float delta = 1.f /
static_cast<float>(
componentRange() );
1381 QgsColorWidget::keyPressEvent( event );
1396void QgsColorRampWidget::setColorFromPoint( QPointF point )
1400 const float margin =
static_cast<float>( mMargin );
1401 const float w =
static_cast<float>( width() );
1402 const float h =
static_cast<float>( height() );
1406 val = (
static_cast<float>( point.x() ) - margin ) / ( w - 2 * margin );
1410 val = 1.f - (
static_cast<float>( point.y() ) - margin ) / ( h - 2 * margin );
1433 QHBoxLayout *hLayout =
new QHBoxLayout();
1434 hLayout->setContentsMargins( 0, 0, 0, 0 );
1435 hLayout->setSpacing( 5 );
1439 hLayout->addWidget( mRampWidget, 1 );
1442 mSpinBox->setShowClearButton(
false );
1444 const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( u
"888%"_s );
1445 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1446 mSpinBox->setMinimum( 0 );
1447 mSpinBox->setMaximum( convertRealToDisplay( 1.f ) );
1449 hLayout->addWidget( mSpinBox );
1450 setLayout( hLayout );
1454 connect( mSpinBox,
static_cast<void ( QDoubleSpinBox::* )(
double )
>( &QDoubleSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1461 mSpinBox->setMaximum( convertRealToDisplay(
static_cast<float>(
componentRange() ) ) );
1466 mSpinBox->setSuffix( QChar( 176 ) );
1470 mSpinBox->setSuffix( tr(
"%" ) );
1475 mSpinBox->setSuffix( QString() );
1482 mRampWidget->blockSignals(
true );
1483 mRampWidget->setComponentValueF( value );
1484 mRampWidget->blockSignals(
false );
1485 mSpinBox->blockSignals(
true );
1486 mSpinBox->setValue( convertRealToDisplay( value ) );
1487 mSpinBox->blockSignals(
false );
1493 mRampWidget->setColor(
color );
1494 mSpinBox->blockSignals(
true );
1496 mSpinBox->blockSignals(
false );
1499void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1504void QgsColorSliderWidget::spinChanged(
double value )
1506 const float convertedValue = convertDisplayToReal(
static_cast<float>( value ) );
1508 mRampWidget->setComponentValueF( convertedValue );
1512void QgsColorSliderWidget::rampChanged(
float value )
1514 mSpinBox->blockSignals(
true );
1515 mSpinBox->setValue( convertRealToDisplay( value ) );
1516 mSpinBox->blockSignals(
false );
1520float QgsColorSliderWidget::convertRealToDisplay(
const float realValue )
const
1525 return realValue * 100.f;
1531 return realValue * 255.f;
1537float QgsColorSliderWidget::convertDisplayToReal(
const float displayValue )
const
1542 return displayValue / 100.f;
1545 return displayValue /
HUE_MAX;
1548 return displayValue / 255.f;
1561 QHBoxLayout *hLayout =
new QHBoxLayout();
1562 hLayout->setContentsMargins( 0, 0, 0, 0 );
1563 hLayout->setSpacing( 0 );
1565 mLineEdit =
new QLineEdit(
nullptr );
1566 hLayout->addWidget( mLineEdit );
1568 mMenuButton =
new QToolButton( mLineEdit );
1570 mMenuButton->setCursor( Qt::ArrowCursor );
1571 mMenuButton->setFocusPolicy( Qt::NoFocus );
1572 mMenuButton->setStyleSheet( u
"QToolButton { border: none; padding: 0px; }"_s );
1574 setLayout( hLayout );
1576 const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1577 mLineEdit->setStyleSheet( u
"QLineEdit { padding-right: %1px; } "_s
1578 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1580 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1581 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1585 mFormat = settings.
enumValue( u
"ColorWidgets/textWidgetFormat"_s,
HexRgb );
1599 const QSize sz = mMenuButton->sizeHint();
1600 const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1601 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(), ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1604void QgsColorTextWidget::updateText()
1623void QgsColorTextWidget::textChanged()
1625 const QString testString = mLineEdit->text();
1628 if ( !
color.isValid() )
1639 if ( !containsAlpha )
1650void QgsColorTextWidget::showMenu()
1652 QMenu colorContextMenu;
1653 QAction *hexRgbaAction =
nullptr;
1654 QAction *rgbaAction =
nullptr;
1656 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1657 colorContextMenu.addAction( hexRgbAction );
1660 hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1661 colorContextMenu.addAction( hexRgbaAction );
1663 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1664 colorContextMenu.addAction( rgbAction );
1667 rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1668 colorContextMenu.addAction( rgbaAction );
1671 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1672 if ( selectedAction == hexRgbAction )
1676 else if ( hexRgbaAction && selectedAction == hexRgbaAction )
1680 else if ( selectedAction == rgbAction )
1684 else if ( rgbaAction && selectedAction == rgbaAction )
1690 QgsSettings settings;
1691 settings.
setEnumValue( u
"ColorWidgets/textWidgetFormat"_s, mFormat );
1698 mAllowAlpha = allowOpacity;
1707 , mColor2( QColor() )
1711void QgsColorPreviewWidget::drawColor(
const QColor &color, QRect rect, QPainter &painter )
1713 painter.setPen( Qt::NoPen );
1715 if (
color.alpha() < 255 )
1718 painter.setBrush( checkBrush );
1719 painter.drawRect( rect );
1724 const QBrush colorBrush = QBrush(
color );
1725 painter.setBrush( colorBrush );
1726 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1728 QColor opaqueColor = QColor(
color );
1729 opaqueColor.setAlpha( 255 );
1730 const QBrush opaqueBrush = QBrush( opaqueColor );
1731 painter.setBrush( opaqueBrush );
1732 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1737 const QBrush brush = QBrush(
color );
1738 painter.setBrush( brush );
1739 painter.drawRect( rect );
1746 QPainter painter(
this );
1748 if ( mColor2.isValid() )
1751 const int verticalSplit = std::round( height() / 2.0 );
1752 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1753 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1757 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1770 if (
color == mColor2 )
1780 if ( e->button() == Qt::LeftButton )
1782 mDragStartPosition = e->pos();
1789 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1798 if ( mColor2.isValid() )
1801 const int verticalSplit = std::round( height() / 2.0 );
1802 if ( mDragStartPosition.y() >= verticalSplit )
1804 clickedColor = mColor2;
1814 if ( !( e->buttons() & Qt::LeftButton ) )
1821 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1832 if ( mColor2.isValid() )
1835 const int verticalSplit = std::round( height() / 2.0 );
1836 if ( mDragStartPosition.y() >= verticalSplit )
1838 dragColor = mColor2;
1842 QDrag *drag =
new QDrag(
this );
1845 drag->exec( Qt::CopyAction );
1854 : QWidgetAction( parent )
1858 setDefaultWidget( mColorWidget );
1861 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1865void QgsColorWidgetAction::onHover()
1868 if ( mSuppressRecurse )
1875 mSuppressRecurse =
true;
1876 mMenu->setActiveAction(
this );
1877 mSuppressRecurse =
false;
1881void QgsColorWidgetAction::setColor(
const QColor &color )
1884 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).