22 #include <QResizeEvent> 23 #include <QStyleOptionFrameV3> 25 #include <QHBoxLayout> 28 #include <QFontMetrics> 29 #include <QToolButton> 42 , mCurrentColor( Qt::red )
43 , mComponent( component )
45 setAcceptDrops(
true );
56 QPixmap pixmap( 50, 50 );
57 pixmap.fill( Qt::transparent );
59 painter.begin( &pixmap );
61 painter.fillRect( QRect( 0, 0, 50, 50 ), QBrush( QColor( 200, 200, 200 ) ) );
63 QColor pixmapColor =
color;
64 pixmapColor.setAlpha( 255 );
65 painter.setBrush( QBrush( pixmapColor ) );
66 painter.setPen( QPen( Qt::white ) );
67 painter.drawRect( QRect( 1, 1, 47, 47 ) );
141 color.getHsv( &h, &s, &v, &a );
144 int clippedValue = std::min( std::max( 0, newValue ),
componentRange( component ) );
149 color.setRed( clippedValue );
152 color.setGreen( clippedValue );
155 color.setBlue( clippedValue );
158 color.setHsv( clippedValue, s, v, a );
161 color.setHsv( h, clippedValue, v, a );
164 color.setHsv( h, s, clippedValue, a );
167 color.setAlpha( clippedValue );
176 static QPixmap sTranspBkgrd;
178 if ( sTranspBkgrd.isNull() )
190 if ( mimeColor.isValid() )
193 e->acceptProposedAction();
200 bool hasAlpha =
false;
203 if ( mimeColor.isValid() )
206 e->acceptProposedAction();
265 valueClipped = std::max( valueClipped, 0 );
277 if ( r == valueClipped )
284 if ( g == valueClipped )
291 if ( b == valueClipped )
298 if ( h == valueClipped )
305 if ( s == valueClipped )
312 if ( v == valueClipped )
319 if ( a == valueClipped )
348 if ( color.hue() >= 0 )
370 QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
372 QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
373 for (
int pos = 0; pos <= wheelStops; ++pos )
375 double relativePos = ( double )pos / wheelStops;
376 gradColor.setHsvF( relativePos, 1, 1 );
377 wheelGradient.setColorAt( relativePos, gradColor );
379 mWheelBrush = QBrush( wheelGradient );
385 delete mTriangleImage;
391 int size =
Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral(
"XXXXXXXXXXXXXXXXXXXXXX" ) );
392 return QSize( size, size );
398 QPainter painter(
this );
400 if ( !mWidgetImage || !mWheelImage || !mTriangleImage )
402 createImages( size() );
406 mWidgetImage->fill( Qt::transparent );
407 QPainter imagePainter( mWidgetImage );
408 imagePainter.setRenderHint( QPainter::Antialiasing );
417 QPointF center = QPointF( width() / 2.0, height() / 2.0 );
418 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage );
422 double length = mWheelImage->width() / 2.0;
423 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
424 hueMarkerLine.setAngle( h );
427 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
431 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
432 imagePainter.setPen( pen );
433 imagePainter.drawLine( hueMarkerLine );
434 imagePainter.restore();
437 if ( mTriangleDirty )
441 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage );
444 double triangleRadius = length - mWheelThickness - 1;
448 double hueRadians = ( h * M_PI / 180.0 );
449 double hx = std::cos( hueRadians ) * triangleRadius;
450 double hy = -std::sin( hueRadians ) * triangleRadius;
451 double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
452 double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
453 double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
454 double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
455 double mx = ( sx + vx ) / 2.0;
456 double my = ( sy + vy ) / 2.0;
458 double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
459 double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
460 double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
463 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
464 imagePainter.setPen( pen );
465 imagePainter.setBrush( Qt::NoBrush );
466 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0, 4.0 );
470 painter.drawImage( QPoint( 0, 0 ), *mWidgetImage );
476 if ( color.hue() >= 0 && color.hue() !=
hue() )
479 mTriangleDirty =
true;
485 void QgsColorWheel::createImages(
const QSizeF size )
487 double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
488 mWheelThickness = wheelSize / 15.0;
492 mWheelImage =
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
493 delete mTriangleImage;
494 mTriangleImage =
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
496 mWidgetImage =
new QImage( size.width(), size.height(), QImage::Format_ARGB32 );
500 mTriangleDirty =
true;
506 createImages( event->size() );
507 QgsColorWidget::resizeEvent( event );
510 void QgsColorWheel::setColorFromPos(
const QPointF pos )
512 QPointF center = QPointF( width() / 2.0, height() / 2.0 );
514 QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
516 QColor newColor = QColor();
523 if ( mClickedPart == QgsColorWheel::Triangle )
528 double x = pos.x() - center.x();
529 double y = pos.y() - center.y();
531 double eventAngleRadians = line.angle() * M_PI / 180.0;
532 double hueRadians = h * M_PI / 180.0;
533 double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
534 double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
535 double length = mWheelImage->width() / 2.0;
536 double triangleLength = length - mWheelThickness - 1;
538 double a = 0.5 * triangleLength;
539 double b = std::tan( rad1 ) * a;
540 double r = std::sqrt( x * x + y * y );
541 double maxR = std::sqrt( a * a + b * b );
545 double dx = std::tan( rad1 ) * r;
546 double rad2 = std::atan( dx / maxR );
547 rad2 = std::min( rad2, M_PI / 3.0 );
548 rad2 = std::max( rad2, -M_PI / 3.0 );
549 eventAngleRadians += rad2 - rad1;
550 rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
551 rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
552 b = std::tan( rad1 ) * a;
553 r = std::sqrt( a * a + b * b );
556 double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
557 double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
558 double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
559 double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
560 s = std::min( static_cast< int >( std::round( std::max( 0.0, newS ) * 255.0 ) ), 255 );
561 l = std::min( static_cast< int >( std::round( std::max( 0.0, newL ) * 255.0 ) ), 255 );
562 newColor = QColor::fromHsl( h, s, l );
564 newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
566 else if ( mClickedPart == QgsColorWheel::Wheel )
571 int newHue = line.angle();
572 newColor = QColor::fromHsv( newHue, s, v, alpha );
574 mTriangleDirty =
true;
595 setColorFromPos( event->pos() );
604 QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(),
event->pos().y() );
606 double innerLength = mWheelImage->width() / 2.0 - mWheelThickness;
607 if ( line.length() < innerLength )
609 mClickedPart = QgsColorWheel::Triangle;
613 mClickedPart = QgsColorWheel::Wheel;
615 setColorFromPos( event->pos() );
621 mClickedPart = QgsColorWheel::None;
624 void QgsColorWheel::createWheel()
631 int maxSize = std::min( mWheelImage->width(), mWheelImage->height() );
632 double wheelRadius = maxSize / 2.0;
634 mWheelImage->fill( Qt::transparent );
635 QPainter p( mWheelImage );
636 p.setRenderHint( QPainter::Antialiasing );
637 p.setBrush( mWheelBrush );
638 p.setPen( Qt::NoPen );
641 p.translate( wheelRadius, wheelRadius );
642 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
645 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
646 p.setBrush( QBrush( Qt::black ) );
647 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius - mWheelThickness, wheelRadius - mWheelThickness );
653 void QgsColorWheel::createTriangle()
655 if ( !mWheelImage || !mTriangleImage )
660 QPointF center = QPointF( mWheelImage->width() / 2.0, mWheelImage->height() / 2.0 );
661 mTriangleImage->fill( Qt::transparent );
663 QPainter imagePainter( mTriangleImage );
664 imagePainter.setRenderHint( QPainter::Antialiasing );
667 double wheelRadius = mWheelImage->width() / 2.0;
668 double triangleRadius = wheelRadius - mWheelThickness - 1;
671 QColor pureColor = QColor::fromHsv( angle, 255, 255 );
673 QColor alphaColor = QColor( pureColor );
674 alphaColor.setAlpha( 0 );
677 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 ) );
678 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
679 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 ) );
680 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
681 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
682 line1.setAngle( line1.angle() +
angle );
683 line2.setAngle( line2.angle() +
angle );
684 line3.setAngle( line3.angle() +
angle );
685 line4.setAngle( line4.angle() +
angle );
686 line5.setAngle( line5.angle() +
angle );
687 QPointF p1 = line1.p2();
688 QPointF p2 = line2.p2();
689 QPointF p3 = line3.p2();
690 QPointF p4 = line4.p2();
691 QPointF p5 = line5.p2();
694 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
695 colorGrad.setColorAt( 0, alphaColor );
696 colorGrad.setColorAt( 1, pureColor );
697 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
698 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
699 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
702 triangle << p2 << p1 << p3 << p2;
703 imagePainter.setPen( Qt::NoPen );
705 imagePainter.setBrush( QBrush( Qt::black ) );
706 imagePainter.drawPolygon( triangle );
708 imagePainter.setBrush( QBrush( colorGrad ) );
709 imagePainter.drawPolygon( triangle );
711 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
712 imagePainter.setBrush( QBrush( whiteGrad ) );
713 imagePainter.drawPolygon( triangle );
718 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
719 imagePainter.setBrush( Qt::NoBrush );
720 imagePainter.setPen( QPen( Qt::transparent ) );
721 imagePainter.drawPolygon( triangle );
724 mTriangleDirty =
false;
736 setFocusPolicy( Qt::StrongFocus );
737 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
739 mBoxImage =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
749 int size =
Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral(
"XXXXXXXXXXXXXXXXXXXXXX" ) );
750 return QSize( size, size );
756 QPainter painter(
this );
758 QStyleOptionFrame option;
759 option.initFrom(
this );
760 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
761 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
769 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
772 double xPos = mMargin + ( width() - 2 * mMargin - 1 ) * (
double )xComponentValue() / ( double )valueRangeX();
773 double yPos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) * (
double )yComponentValue() / ( double )valueRangeY();
775 painter.setBrush( Qt::white );
776 painter.setPen( Qt::NoPen );
778 painter.drawRect( xPos - 1, mMargin, 3, height() - 2 * mMargin - 1 );
779 painter.drawRect( mMargin, yPos - 1, width() - 2 * mMargin - 1, 3 );
780 painter.setPen( Qt::black );
781 painter.drawLine( xPos, mMargin, xPos, height() - mMargin - 1 );
782 painter.drawLine( mMargin, yPos, width() - mMargin - 1, yPos );
831 mBoxImage =
new QImage( event->size().width() - mMargin * 2,
event->size().height() - mMargin * 2, QImage::Format_RGB32 );
832 QgsColorWidget::resizeEvent( event );
837 setColorFromPoint( event->pos() );
843 setColorFromPoint( event->pos() );
846 void QgsColorBox::createBox()
848 int maxValueX = mBoxImage->width();
849 int maxValueY = mBoxImage->height();
853 int colorComponentValue;
855 for (
int y = 0; y < maxValueY; ++y )
857 QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
859 colorComponentValue = int( valueRangeY() - valueRangeY() * (
double( y ) / maxValueY ) );
860 alterColor( currentColor, yComponent(), colorComponentValue );
861 for (
int x = 0; x < maxValueX; ++x )
863 colorComponentValue = int( valueRangeX() * (
double( x ) / maxValueX ) );
864 alterColor( currentColor, xComponent(), colorComponentValue );
865 scanLine[x] = currentColor.rgb();
871 int QgsColorBox::valueRangeX()
const 876 int QgsColorBox::valueRangeY()
const 901 int QgsColorBox::yComponentValue()
const 926 int QgsColorBox::xComponentValue()
const 931 void QgsColorBox::setColorFromPoint( QPoint point )
933 int valX = valueRangeX() * ( point.x() - mMargin ) / ( width() - 2 * mMargin - 1 );
934 valX = std::min( std::max( valX, 0 ), valueRangeX() );
936 int valY = valueRangeY() - valueRangeY() * ( point.y() - mMargin ) / ( height() - 2 * mMargin - 1 );
937 valY = std::min( std::max( valY, 0 ), valueRangeY() );
948 if ( color.hue() >= 0 )
968 setFocusPolicy( Qt::StrongFocus );
992 QPainter painter(
this );
997 QStyleOptionFrame option;
998 option.initFrom(
this );
999 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
1000 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1006 QStyleOptionFocusRect option;
1007 option.initFrom(
this );
1008 option.state = QStyle::State_KeyboardFocusChange;
1009 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1016 color.setAlpha( 255 );
1019 painter.setPen( pen );
1020 painter.setBrush( Qt::NoBrush );
1023 for (
int c = 0;
c <= maxValue; ++
c )
1032 if ( color.hue() < 0 )
1034 color.setHsv(
hue(), color.saturation(), color.value() );
1036 pen.setColor( color );
1037 painter.setPen( pen );
1041 painter.drawLine(
c + mMargin, mMargin,
c + mMargin, height() - mMargin - 1 );
1046 painter.drawLine( mMargin,
c + mMargin, width() - mMargin - 1,
c + mMargin );
1055 painter.setBrush( checkBrush );
1056 painter.setPen( Qt::NoPen );
1057 painter.drawRect( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 );
1058 QLinearGradient colorGrad;
1062 colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 );
1067 colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 );
1070 transparent.setAlpha( 0 );
1071 colorGrad.setColorAt( 0, transparent );
1073 opaque.setAlpha( 255 );
1074 colorGrad.setColorAt( 1, opaque );
1075 QBrush colorBrush = QBrush( colorGrad );
1076 painter.setBrush( colorBrush );
1077 painter.drawRect( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 );
1083 painter.setRenderHint( QPainter::Antialiasing );
1084 painter.setBrush( QBrush( Qt::black ) );
1085 painter.setPen( Qt::NoPen );
1087 painter.drawPolygon( mTopTriangle );
1088 painter.translate( 0, height() - mMargin - 2 );
1089 painter.setBrush( QBrush( Qt::white ) );
1090 painter.drawPolygon( mBottomTriangle );
1097 painter.setBrush( Qt::white );
1098 painter.setPen( Qt::NoPen );
1099 painter.drawRect( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 );
1100 painter.setPen( Qt::black );
1101 painter.drawLine( mMargin, ypos, width() - mMargin - 1, ypos );
1111 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1116 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1123 if ( margin == mMargin )
1133 if ( showFrame == mShowFrame )
1144 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1145 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1151 setColorFromPoint( event->pos() );
1159 if ( event->delta() > 0 )
1180 setColorFromPoint( event->pos() );
1219 QgsColorWidget::keyPressEvent( event );
1231 void QgsColorRampWidget::setColorFromPoint( QPointF point )
1237 val =
componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin );
1262 QHBoxLayout *hLayout =
new QHBoxLayout();
1263 hLayout->setMargin( 0 );
1264 hLayout->setSpacing( 5 );
1268 hLayout->addWidget( mRampWidget, 1 );
1270 mSpinBox =
new QSpinBox();
1272 int largestCharWidth = mSpinBox->fontMetrics().width( QStringLiteral(
"888%" ) );
1273 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1274 mSpinBox->setMinimum( 0 );
1275 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1280 mSpinBox->setSuffix( QChar( 176 ) );
1284 mSpinBox->setSuffix( tr(
"%" ) );
1286 hLayout->addWidget( mSpinBox );
1287 setLayout( hLayout );
1291 connect( mSpinBox, static_cast <
void ( QSpinBox::* )(
int ) > ( &QSpinBox::valueChanged ),
this, &QgsColorSliderWidget::spinChanged );
1298 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1302 mSpinBox->setSuffix( QChar( 176 ) );
1307 mSpinBox->setSuffix( tr(
"%" ) );
1312 mSpinBox->setSuffix( QString() );
1319 mRampWidget->blockSignals(
true );
1321 mRampWidget->blockSignals(
false );
1322 mSpinBox->blockSignals(
true );
1323 mSpinBox->setValue( convertRealToDisplay( value ) );
1324 mSpinBox->blockSignals(
false );
1331 mSpinBox->blockSignals(
true );
1333 mSpinBox->blockSignals(
false );
1336 void QgsColorSliderWidget::rampColorChanged(
const QColor &
color )
1341 void QgsColorSliderWidget::spinChanged(
int value )
1343 int convertedValue = convertDisplayToReal( value );
1349 void QgsColorSliderWidget::rampChanged(
int value )
1351 mSpinBox->blockSignals(
true );
1352 mSpinBox->setValue( convertRealToDisplay( value ) );
1353 mSpinBox->blockSignals(
false );
1357 int QgsColorSliderWidget::convertRealToDisplay(
const int realValue )
const 1363 return std::round( 100.0 * realValue / 255.0 );
1370 int QgsColorSliderWidget::convertDisplayToReal(
const int displayValue )
const 1375 return std::round( 255.0 * displayValue / 100.0 );
1379 return displayValue;
1389 QHBoxLayout *hLayout =
new QHBoxLayout();
1390 hLayout->setMargin( 0 );
1391 hLayout->setSpacing( 0 );
1393 mLineEdit =
new QLineEdit(
nullptr );
1394 hLayout->addWidget( mLineEdit );
1396 mMenuButton =
new QToolButton( mLineEdit );
1398 mMenuButton->setCursor( Qt::ArrowCursor );
1399 mMenuButton->setFocusPolicy( Qt::NoFocus );
1400 mMenuButton->setStyleSheet( QStringLiteral(
"QToolButton { border: none; padding: 0px; }" ) );
1402 setLayout( hLayout );
1404 int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1405 mLineEdit->setStyleSheet( QStringLiteral(
"QLineEdit { padding-right: %1px; } " )
1406 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1408 connect( mLineEdit, &QLineEdit::editingFinished,
this, &QgsColorTextWidget::textChanged );
1409 connect( mMenuButton, &QAbstractButton::clicked,
this, &QgsColorTextWidget::showMenu );
1413 mFormat = settings.
enumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ),
HexRgb );
1427 QSize sz = mMenuButton->sizeHint();
1428 int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1429 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
1430 ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1433 void QgsColorTextWidget::updateText()
1452 void QgsColorTextWidget::textChanged()
1454 QString testString = mLineEdit->text();
1457 if ( !color.isValid() )
1468 if ( !containsAlpha )
1479 void QgsColorTextWidget::showMenu()
1481 QMenu colorContextMenu;
1483 QAction *hexRgbAction =
new QAction( tr(
"#RRGGBB" ),
nullptr );
1484 colorContextMenu.addAction( hexRgbAction );
1485 QAction *hexRgbaAction =
new QAction( tr(
"#RRGGBBAA" ),
nullptr );
1486 colorContextMenu.addAction( hexRgbaAction );
1487 QAction *rgbAction =
new QAction( tr(
"rgb( r, g, b )" ),
nullptr );
1488 colorContextMenu.addAction( rgbAction );
1489 QAction *rgbaAction =
new QAction( tr(
"rgba( r, g, b, a )" ),
nullptr );
1490 colorContextMenu.addAction( rgbaAction );
1492 QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
1493 if ( selectedAction == hexRgbAction )
1497 else if ( selectedAction == hexRgbaAction )
1501 else if ( selectedAction == rgbAction )
1505 else if ( selectedAction == rgbaAction )
1512 settings.
setEnumValue( QStringLiteral(
"ColorWidgets/textWidgetFormat" ), mFormat );
1524 , mColor2( QColor() )
1529 void QgsColorPreviewWidget::drawColor(
const QColor &
color, QRect rect, QPainter &painter )
1531 painter.setPen( Qt::NoPen );
1533 if (
color.alpha() < 255 )
1536 painter.setBrush( checkBrush );
1537 painter.drawRect( rect );
1542 QBrush colorBrush = QBrush(
color );
1543 painter.setBrush( colorBrush );
1544 painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
1546 QColor opaqueColor = QColor(
color );
1547 opaqueColor.setAlpha( 255 );
1548 QBrush opaqueBrush = QBrush( opaqueColor );
1549 painter.setBrush( opaqueBrush );
1550 painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
1555 QBrush brush = QBrush(
color );
1556 painter.setBrush( brush );
1557 painter.drawRect( rect );
1564 QPainter painter(
this );
1566 if ( mColor2.isValid() )
1569 int verticalSplit = std::round( height() / 2.0 );
1570 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1571 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1575 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1588 if ( color == mColor2 )
1598 if ( e->button() == Qt::LeftButton )
1600 mDragStartPosition = e->pos();
1607 if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1616 if ( mColor2.isValid() )
1619 int verticalSplit = std::round( height() / 2.0 );
1620 if ( mDragStartPosition.y() >= verticalSplit )
1622 clickedColor = mColor2;
1633 if ( !( e->buttons() & Qt::LeftButton ) )
1640 if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1651 if ( mColor2.isValid() )
1654 int verticalSplit = std::round( height() / 2.0 );
1655 if ( mDragStartPosition.y() >= verticalSplit )
1657 dragColor = mColor2;
1661 QDrag *drag =
new QDrag(
this );
1664 drag->exec( Qt::CopyAction );
1673 : QWidgetAction( parent )
1675 , mColorWidget( colorWidget )
1676 , mSuppressRecurse( false )
1677 , mDismissOnColorSelection( true )
1679 setDefaultWidget( mColorWidget );
1682 connect(
this, &QAction::hovered,
this, &QgsColorWidgetAction::onHover );
1686 void QgsColorWidgetAction::onHover()
1689 if ( mSuppressRecurse )
1696 mSuppressRecurse =
true;
1697 mMenu->setActiveAction(
this );
1698 mSuppressRecurse =
false;
1702 void QgsColorWidgetAction::setColor(
const QColor &color )
1706 if ( mMenu && mDismissOnColorSelection )
static const double UI_SCALE_FACTOR
UI scaling factor.
This class is a composition of two QSettings instances:
void paintEvent(QPaintEvent *event) override
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void mouseReleaseEvent(QMouseEvent *event) override
QSize sizeHint() const override
void mousePressEvent(QMouseEvent *event) override
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)
static QPixmap getThemePixmap(const QString &name)
Helper to get a theme icon as a pixmap.
void mousePressEvent(QMouseEvent *event) override
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
~QgsColorWheel() override
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...
void setComponent(ColorComponent component) override
Sets the color component which the widget controls.
void resizeEvent(QResizeEvent *event) override
QgsColorWheel(QWidget *parent=nullptr)
Constructs a new color wheel widget.
void setColor(const QColor &color, bool emitSignals=false) override
#RRGGBBAA in hexadecimal, with alpha
void paintEvent(QPaintEvent *event) override
void resizeEvent(QResizeEvent *event) override
void setEnumValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on an enum.
void setColor(const QColor &color, bool emitSignals=false) override
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
QgsColorTextWidget(QWidget *parent=nullptr)
Construct a new color line edit widget.
QSize sizeHint() const override
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
void mouseMoveEvent(QMouseEvent *event) override
QgsColorBox(QWidget *parent=nullptr, ColorComponent component=Value)
Construct a new color box widget.
void mouseMoveEvent(QMouseEvent *event) override
void resizeEvent(QResizeEvent *event) override
void setColor(const QColor &color, bool emitSignals=false) override
Sets the color for the widget.
Rgba( r, g, b, a ) format, with alpha.
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.