17#include "moc_qgscolorwidgets.cpp"
25#include <QResizeEvent>
27#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
28#include <QStyleOptionFrameV3>
30#include <QStyleOptionFrame>
35#include <QFontMetrics>
48#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
61 , mCurrentColor( Qt::red )
62 , mComponent( component )
64 setAcceptDrops(
true );
81 QPixmap pixmap( iconSize, iconSize );
82 pixmap.fill( Qt::transparent );
84 painter.begin( &pixmap );
86 painter.fillRect( QRect( 0, 0, iconSize, iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
88 QColor pixmapColor =
color;
89 pixmapColor.setAlpha( 255 );
90 painter.setBrush( QBrush( pixmapColor ) );
91 painter.setPen( QPen( Qt::white ) );
92 painter.drawRect( QRect( 1, 1, iconSize - 2, iconSize - 2 ) );
194 return static_cast<int>( std::round(
hueF() *
HUE_MAX ) );
218 float_type clippedValue = std::clamp( newValue, 0.f, 1.f );
223 color.getCmykF( &
c, &m, &y, &k, &a );
228 color.setCmykF( clippedValue, m, y, k, a );
231 color.setCmykF(
c, clippedValue, y, k, a );
234 color.setCmykF(
c, m, clippedValue, k, a );
237 color.setCmykF(
c, m, y, clippedValue, a );
246 color.getRgbF( &r, &g, &b, &a );
248 color.getHsvF( &h, &s, &v );
253 color.setRedF( clippedValue );
256 color.setGreenF( clippedValue );
259 color.setBlueF( clippedValue );
262 color.setHsvF( clippedValue, s, v, a );
265 color.setHsvF( h, clippedValue, v, a );
268 color.setHsvF( h, s, clippedValue, a );
271 color.setAlphaF( clippedValue );
286 return QColor::Spec::Rgb;
291 return QColor::Spec::Hsv;
297 return QColor::Spec::Cmyk;
300 return QColor::Spec::Invalid;
311 static QPixmap sTranspBkgrd;
313 if ( sTranspBkgrd.isNull() )
325 if ( mimeColor.isValid() )
328 e->acceptProposedAction();
335 bool hasAlpha =
false;
338 if ( mimeColor.isValid() )
341 e->acceptProposedAction();
435 if (
color.hue() >= 0 )
457 QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
458 const int wheelStops = 20;
459 QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
460 for (
int pos = 0; pos <= wheelStops; ++pos )
462 const double relativePos =
static_cast<double>( pos ) / wheelStops;
463 gradColor.setHsvF( relativePos, 1, 1 );
464 wheelGradient.setColorAt( relativePos, gradColor );
466 mWheelBrush = QBrush( wheelGradient );
474 return QSize( size, size );
480 QPainter painter(
this );
482 if ( mWidgetImage.isNull() || mWheelImage.isNull() || mTriangleImage.isNull() )
484 createImages( size() );
488 mWidgetImage.fill( Qt::transparent );
489 QPainter imagePainter( &mWidgetImage );
490 imagePainter.setRenderHint( QPainter::Antialiasing );
499 const QPointF center = QPointF( mWidgetImage.width() / 2.0, mWidgetImage.height() / 2.0 );
500 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mWheelImage );
504 const double length = mWheelImage.width() / 2.0;
505 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
506 hueMarkerLine.setAngle( h );
509 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
511 pen.setWidthF( 2 * devicePixelRatioF() );
513 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
514 imagePainter.setPen( pen );
515 imagePainter.drawLine( hueMarkerLine );
516 imagePainter.restore();
519 if ( mTriangleDirty )
523 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mTriangleImage );
526 const double triangleRadius = length - mWheelThickness * devicePixelRatioF() - 1;
530 const double hueRadians = ( h * M_PI / 180.0 );
531 const double hx = std::cos( hueRadians ) * triangleRadius;
532 const double hy = -std::sin( hueRadians ) * triangleRadius;
533 const double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
534 const double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
535 const double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
536 const double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
537 const double mx = ( sx + vx ) / 2.0;
538 const double my = ( sy + vy ) / 2.0;
540 const double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
541 const double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
542 const double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
545 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
546 imagePainter.setPen( pen );
547 imagePainter.setBrush( Qt::NoBrush );
548 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0 * devicePixelRatioF(), 4.0 * devicePixelRatioF() );
552 painter.drawImage( QRectF( 0, 0, width(), height() ), mWidgetImage );
561 mTriangleDirty =
true;
567void QgsColorWheel::createImages(
const QSizeF size )
569 const double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
570 mWheelThickness = wheelSize / 15.0;
573 const double pixelRatio = devicePixelRatioF();
574 mWheelImage = QImage( wheelSize * pixelRatio,
575 wheelSize * pixelRatio, QImage::Format_ARGB32 );
576 mTriangleImage = QImage( wheelSize * pixelRatio,
577 wheelSize * pixelRatio, QImage::Format_ARGB32 );
578 mWidgetImage = QImage( size.width() * pixelRatio,
579 size.height() * pixelRatio, QImage::Format_ARGB32 );
583 mTriangleDirty =
true;
588 QgsColorWidget::resizeEvent( event );
591 if ( event->size().width() > parentWidget()->size().width() )
594 std::min( event->size().width(), parentWidget()->size().width() - 2 ),
595 std::min( event->size().height(), parentWidget()->size().height() - 2 )
598 createImages( newSize );
602 createImages( event->size() );
606 createImages( event->size() );
610void QgsColorWheel::setColorFromPos(
const QPointF pos )
612 const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
614 const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
616 QColor newColor = QColor();
623 if ( mClickedPart == QgsColorWheel::Triangle )
628 const double x = pos.x() - center.x();
629 const double y = pos.y() - center.y();
631 double eventAngleRadians = line.angle() * M_PI / 180.0;
632 const double hueRadians = h * 2 * M_PI;
633 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
634 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
635 const double length = mWheelImage.width() / 2.0 / devicePixelRatioF();
636 const double triangleLength = length - mWheelThickness - 1;
638 const double a = 0.5 * triangleLength;
639 double b = std::tan( rad1 ) * a;
640 double r = std::sqrt( x * x + y * y );
641 const double maxR = std::sqrt( a * a + b * b );
645 const double dx = std::tan( rad1 ) * r;
646 double rad2 = std::atan( dx / maxR );
647 rad2 = std::min( rad2, M_PI / 3.0 );
648 rad2 = std::max( rad2, -M_PI / 3.0 );
649 eventAngleRadians += rad2 - rad1;
650 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
651 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
652 b = std::tan( rad1 ) * a;
653 r = std::sqrt( a * a + b * b );
656 const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
657 const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
658 const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
659 const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
660 s = std::min( std::max( 0.f,
static_cast<float>( newS ) ), 1.f );
661 l = std::min( std::max( 0.f,
static_cast<float>( newL ) ), 1.f );
662 newColor = QColor::fromHslF( h, s, l );
664 newColor.setHsvF( h, newColor.hsvSaturationF(), newColor.valueF(), alpha );
666 else if ( mClickedPart == QgsColorWheel::Wheel )
671 const qreal newHue = line.angle() /
HUE_MAX;
672 newColor = QColor::fromHsvF(
static_cast<float>( newHue ), s, v, alpha );
674 mTriangleDirty =
true;
696 setColorFromPos( event->pos() );
703 if ( event->button() == Qt::LeftButton )
709 const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
711 const double innerLength = mWheelImage.width() / 2.0 / devicePixelRatioF() - mWheelThickness;
712 if ( line.length() < innerLength )
714 mClickedPart = QgsColorWheel::Triangle;
718 mClickedPart = QgsColorWheel::Wheel;
720 setColorFromPos( event->pos() );
730 if ( event->button() == Qt::LeftButton )
733 mClickedPart = QgsColorWheel::None;
741void QgsColorWheel::createWheel()
743 if ( mWheelImage.isNull() )
748 const int maxSize = std::min( mWheelImage.width(), mWheelImage.height() );
749 const double wheelRadius = maxSize / 2.0;
751 mWheelImage.fill( Qt::transparent );
752 QPainter p( &mWheelImage );
753 p.setRenderHint( QPainter::Antialiasing );
754 p.setBrush( mWheelBrush );
755 p.setPen( Qt::NoPen );
758 p.translate( wheelRadius, wheelRadius );
759 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
762 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
763 p.setBrush( QBrush( Qt::black ) );
764 p.drawEllipse( QPointF( 0,
766 wheelRadius - mWheelThickness * devicePixelRatioF(),
767 wheelRadius - mWheelThickness * devicePixelRatioF() );
773void QgsColorWheel::createTriangle()
775 if ( mWheelImage.isNull() || mTriangleImage.isNull() )
780 const QPointF center = QPointF( mWheelImage.width() / 2.0, mWheelImage.height() / 2.0 );
781 mTriangleImage.fill( Qt::transparent );
783 QPainter imagePainter( &mTriangleImage );
784 imagePainter.setRenderHint( QPainter::Antialiasing );
788 const double wheelRadius = mWheelImage.width() / 2.0;
789 const double triangleRadius = wheelRadius - mWheelThickness * devicePixelRatioF() - 1;
792 const QColor pureColor = QColor::fromHsvF( angle, 1., 1. );
794 QColor alphaColor = QColor( pureColor );
795 alphaColor.setAlpha( 0 );
798 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 ) );
799 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
800 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 ) );
801 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
802 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
803 line1.setAngle( line1.angle() + angleDegree );
804 line2.setAngle( line2.angle() + angleDegree );
805 line3.setAngle( line3.angle() + angleDegree );
806 line4.setAngle( line4.angle() + angleDegree );
807 line5.setAngle( line5.angle() + angleDegree );
808 const QPointF p1 = line1.p2();
809 const QPointF p2 = line2.p2();
810 const QPointF p3 = line3.p2();
811 const QPointF p4 = line4.p2();
812 const QPointF p5 = line5.p2();
815 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
816 colorGrad.setColorAt( 0, alphaColor );
817 colorGrad.setColorAt( 1, pureColor );
818 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
819 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
820 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
823 triangle << p2 << p1 << p3 << p2;
824 imagePainter.setPen( Qt::NoPen );
826 imagePainter.setBrush( QBrush( Qt::black ) );
827 imagePainter.drawPolygon( triangle );
829 imagePainter.setBrush( QBrush( colorGrad ) );
830 imagePainter.drawPolygon( triangle );
832 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
833 imagePainter.setBrush( QBrush( whiteGrad ) );
834 imagePainter.drawPolygon( triangle );
839 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
840 imagePainter.setBrush( Qt::NoBrush );
841 imagePainter.setPen( QPen( Qt::transparent ) );
842 imagePainter.drawPolygon( triangle );
845 mTriangleDirty =
false;
857 setFocusPolicy( Qt::StrongFocus );
858 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
860 mBoxImage =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
871 return QSize( size, size );
877 QPainter painter(
this );
879 QStyleOptionFrame option;
880 option.initFrom(
this );
881 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
882 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
890 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
893 const double h = height();
894 const double w = width();
895 const double margin = mMargin;
896 const double xPos = ( mMargin + ( w - 2 * mMargin - 1 ) * xComponentValue() );
897 const double yPos = ( mMargin + ( h - 2 * mMargin - 1 ) - ( h - 2 * mMargin - 1 ) * yComponentValue() );
899 painter.setBrush( Qt::white );
900 painter.setPen( Qt::NoPen );
902 painter.drawRect( QRectF( xPos - 1, mMargin, 3, height() - 2 * margin - 1 ) );
903 painter.drawRect( QRectF( mMargin, yPos - 1, width() - 2 * margin - 1, 3 ) );
904 painter.setPen( Qt::black );
905 painter.drawLine( QLineF( xPos, mMargin, xPos, height() - margin - 1 ) );
906 painter.drawLine( QLineF( mMargin, yPos, width() - margin - 1, yPos ) );
944 mBoxImage =
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
945 QgsColorWidget::resizeEvent( event );
952 setColorFromPoint( event->pos() );
959 if ( event->button() == Qt::LeftButton )
962 setColorFromPoint( event->pos() );
972 if ( event->button() == Qt::LeftButton )
982void QgsColorBox::createBox()
984 const int maxValueX = mBoxImage->width();
985 const int maxValueY = mBoxImage->height();
989 float colorComponentValue;
991 for (
int y = 0; y < maxValueY; ++y )
993 QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
995 colorComponentValue = 1.f -
static_cast<float>( y ) /
static_cast<float>( maxValueY );
996 alterColorF( currentColor, yComponent(), colorComponentValue );
997 for (
int x = 0; x < maxValueX; ++x )
999 colorComponentValue =
static_cast<float>( x ) /
static_cast<float>( maxValueY );
1000 alterColorF( currentColor, xComponent(), colorComponentValue );
1001 scanLine[x] = currentColor.rgb();
1007float QgsColorBox::valueRangeX()
const
1012float QgsColorBox::valueRangeY()
const
1045float QgsColorBox::yComponentValue()
const
1078float QgsColorBox::xComponentValue()
const
1083void QgsColorBox::setColorFromPoint( QPoint point )
1085 const float x =
static_cast<float>( point.x() );
1086 const float y =
static_cast<float>( point.y() );
1087 const float w =
static_cast<float>( width() );
1088 const float h =
static_cast<float>( height() );
1090 float valX = ( x - mMargin ) / ( w - 2 * mMargin - 1 );
1091 float valY = 1.f - ( y - mMargin ) / ( h - 2 * mMargin - 1 );
1102 if (
color.hueF() >= 0 )
1122 setFocusPolicy( Qt::StrongFocus );
1146 QPainter painter(
this );
1151 QStyleOptionFrame option;
1152 option.initFrom(
this );
1153 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1154 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1160 QStyleOptionFocusRect option;
1161 option.initFrom(
this );
1162 option.state = QStyle::State_KeyboardFocusChange;
1163 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1166 float w =
static_cast<float>( width() );
1167 float h =
static_cast<float>( height() );
1168 float margin =
static_cast<float>( mMargin );
1173 color.setAlphaF( 1.f );
1180 painter.setPen( pen );
1181 painter.setBrush( Qt::NoBrush );
1184 for (
int c = 0;
c <= maxValue; ++
c )
1186 float colorVal =
static_cast<float>(
c ) /
static_cast<float>( maxValue );
1190 colorVal = 1.f - colorVal;
1193 if (
color.hueF() < 0 )
1197 pen.setColor(
color );
1198 painter.setPen( pen );
1202 painter.drawLine( QLineF(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 ) );
1207 painter.drawLine( QLineF( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin ) );
1216 painter.setBrush( checkBrush );
1217 painter.setPen( Qt::NoPen );
1218 painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
1219 QLinearGradient colorGrad;
1223 colorGrad = QLinearGradient( margin, 0, w - margin - 1, 0 );
1228 colorGrad = QLinearGradient( 0, margin, 0, h - margin - 1 );
1231 transparent.setAlpha( 0 );
1232 colorGrad.setColorAt( 0, transparent );
1234 opaque.setAlpha( 255 );
1235 colorGrad.setColorAt( 1, opaque );
1236 const QBrush colorBrush = QBrush( colorGrad );
1237 painter.setBrush( colorBrush );
1238 painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
1244 painter.setRenderHint( QPainter::Antialiasing );
1245 painter.setBrush( QBrush( Qt::black ) );
1246 painter.setPen( Qt::NoPen );
1247 painter.translate( margin + ( w - 2 * margin ) *
componentValueF(), margin - 1 );
1248 painter.drawPolygon( mTopTriangle );
1249 painter.translate( 0, h - margin - 2 );
1250 painter.setBrush( QBrush( Qt::white ) );
1251 painter.drawPolygon( mBottomTriangle );
1257 const double ypos = margin + ( h - 2 * margin - 1 ) - ( h - 2 * margin - 1 ) *
componentValueF();
1258 painter.setBrush( Qt::white );
1259 painter.setPen( Qt::NoPen );
1260 painter.drawRect( QRectF( margin, ypos - 1, w - 2 * margin - 1, 3 ) );
1261 painter.setPen( Qt::black );
1262 painter.drawLine( QLineF( margin, ypos, w - margin - 1, ypos ) );
1272 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1277 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1284 if ( margin == mMargin )
1305 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1306 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1314 setColorFromPoint( event->pos() );
1323 const float delta = 1.f /
static_cast<float>(
componentRange() );
1324 if ( event->angleDelta().y() > 0 )
1348 if ( event->button() == Qt::LeftButton )
1351 setColorFromPoint( event->pos() );
1361 if ( event->button() == Qt::LeftButton )
1363 mIsDragging =
false;
1374 const float delta = 1.f /
static_cast<float>(
componentRange() );
1408 QgsColorWidget::keyPressEvent( event );
1423void QgsColorRampWidget::setColorFromPoint( QPointF point )
1427 const float margin =
static_cast<float>( mMargin );
1428 const float w =
static_cast<float>( width() );
1429 const float h =
static_cast<float>( height() );
1433 val = (
static_cast<float>( point.x() ) - margin ) / ( w - 2 * margin );
1437 val = 1.f - (
static_cast<float>( point.y() ) - margin ) / ( h - 2 * margin );
1460 QHBoxLayout *hLayout =
new QHBoxLayout();
1461 hLayout->setContentsMargins( 0, 0, 0, 0 );
1462 hLayout->setSpacing( 5 );
1466 hLayout->addWidget( mRampWidget, 1 );
1471 const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( QStringLiteral(
"888%" ) );
1472 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1473 mSpinBox->setMinimum( 0 );
1474 mSpinBox->setMaximum( convertRealToDisplay( 1.f ) );
1476 hLayout->addWidget( mSpinBox );
1477 setLayout( hLayout );
1481 connect( mSpinBox,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1488 mSpinBox->setMaximum( convertRealToDisplay(
static_cast<float>(
componentRange() ) ) );
1493 mSpinBox->setSuffix( QChar( 176 ) );
1497 mSpinBox->setSuffix( tr(
"%" ) );
1502 mSpinBox->setSuffix( QString() );
1509 mRampWidget->blockSignals(
true );
1511 mRampWidget->blockSignals(
false );
1512 mSpinBox->blockSignals(
true );
1513 mSpinBox->setValue( convertRealToDisplay( value ) );
1514 mSpinBox->blockSignals(
false );
1521 mSpinBox->blockSignals(
true );
1523 mSpinBox->blockSignals(
false );
1526void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1531void QgsColorSliderWidget::spinChanged(
double value )
1533 const float convertedValue = convertDisplayToReal(
static_cast<float>( value ) );
1539void QgsColorSliderWidget::rampChanged(
float value )
1541 mSpinBox->blockSignals(
true );
1542 mSpinBox->setValue( convertRealToDisplay( value ) );
1543 mSpinBox->blockSignals(
false );
1547float QgsColorSliderWidget::convertRealToDisplay(
const float realValue )
const
1552 return realValue * 100.f;
1558 return realValue * 255.f;
1564float QgsColorSliderWidget::convertDisplayToReal(
const float displayValue )
const
1569 return displayValue / 100.f;
1572 return displayValue /
HUE_MAX;
1575 return displayValue / 255.f;
1588 QHBoxLayout *hLayout =
new QHBoxLayout();
1589 hLayout->setContentsMargins( 0, 0, 0, 0 );
1590 hLayout->setSpacing( 0 );
1592 mLineEdit =
new QLineEdit(
nullptr );
1593 hLayout->addWidget( mLineEdit );
1595 mMenuButton =
new QToolButton( mLineEdit );
1597 mMenuButton->setCursor( Qt::ArrowCursor );
1598 mMenuButton->setFocusPolicy( Qt::NoFocus );
1599 mMenuButton->setStyleSheet( QStringLiteral(
"QToolButton { border: none; padding: 0px; }" ) );
1601 setLayout( hLayout );
1603 const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1604 mLineEdit->setStyleSheet( QStringLiteral(
"QLineEdit { padding-right: %1px; } " )
1605 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1607 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1608 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1612 mFormat = settings.
enumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ),
HexRgb );
1626 const QSize sz = mMenuButton->sizeHint();
1627 const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1628 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
1629 ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1632void QgsColorTextWidget::updateText()
1651void QgsColorTextWidget::textChanged()
1653 const QString testString = mLineEdit->text();
1656 if ( !
color.isValid() )
1667 if ( !containsAlpha )
1678void QgsColorTextWidget::showMenu()
1680 QMenu colorContextMenu;
1681 QAction *hexRgbaAction =
nullptr;
1682 QAction *rgbaAction =
nullptr;
1684 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1685 colorContextMenu.addAction( hexRgbAction );
1688 hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1689 colorContextMenu.addAction( hexRgbaAction );
1691 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1692 colorContextMenu.addAction( rgbAction );
1695 rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1696 colorContextMenu.addAction( rgbaAction );
1699 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1700 if ( selectedAction == hexRgbAction )
1704 else if ( hexRgbaAction && selectedAction == hexRgbaAction )
1708 else if ( selectedAction == rgbAction )
1712 else if ( rgbaAction && selectedAction == rgbaAction )
1719 settings.
setEnumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ), mFormat );
1726 mAllowAlpha = allowOpacity;
1735 , mColor2( QColor() )
1740void QgsColorPreviewWidget::drawColor(
const QColor &color, QRect rect, QPainter &painter )
1742 painter.setPen( Qt::NoPen );
1744 if (
color.alpha() < 255 )
1747 painter.setBrush( checkBrush );
1748 painter.drawRect( rect );
1753 const QBrush colorBrush = QBrush(
color );
1754 painter.setBrush( colorBrush );
1755 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1757 QColor opaqueColor = QColor(
color );
1758 opaqueColor.setAlpha( 255 );
1759 const QBrush opaqueBrush = QBrush( opaqueColor );
1760 painter.setBrush( opaqueBrush );
1761 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1766 const QBrush brush = QBrush(
color );
1767 painter.setBrush( brush );
1768 painter.drawRect( rect );
1775 QPainter painter(
this );
1777 if ( mColor2.isValid() )
1780 const int verticalSplit = std::round( height() / 2.0 );
1781 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1782 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1786 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1799 if (
color == mColor2 )
1809 if ( e->button() == Qt::LeftButton )
1811 mDragStartPosition = e->pos();
1818 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1827 if ( mColor2.isValid() )
1830 const int verticalSplit = std::round( height() / 2.0 );
1831 if ( mDragStartPosition.y() >= verticalSplit )
1833 clickedColor = mColor2;
1844 if ( !( e->buttons() & Qt::LeftButton ) )
1851 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1862 if ( mColor2.isValid() )
1865 const int verticalSplit = std::round( height() / 2.0 );
1866 if ( mDragStartPosition.y() >= verticalSplit )
1868 dragColor = mColor2;
1872 QDrag *drag =
new QDrag(
this );
1875 drag->exec( Qt::CopyAction );
1884 : QWidgetAction( parent )
1886 , mColorWidget( colorWidget )
1887 , mSuppressRecurse( false )
1888 , mDismissOnColorSelection( true )
1890 setDefaultWidget( mColorWidget );
1893 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1897void QgsColorWidgetAction::onHover()
1900 if ( mSuppressRecurse )
1907 mSuppressRecurse =
true;
1908 mMenu->setActiveAction(
this );
1909 mSuppressRecurse =
false;
1913void QgsColorWidgetAction::setColor(
const QColor &color )
1916 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.
void setShowClearButton(bool showClearButton)
Sets whether the widget will show a clear button.
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)
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)