23 #include <QResizeEvent> 
   25 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 
   26 #include <QStyleOptionFrameV3> 
   28 #include <QStyleOptionFrame> 
   31 #include <QHBoxLayout> 
   34 #include <QFontMetrics> 
   35 #include <QToolButton> 
   50   , mCurrentColor( Qt::red )
 
   51   , mComponent( component )
 
   53   setAcceptDrops( 
true );
 
   66   pixmap.fill( Qt::transparent );
 
   68   painter.begin( &pixmap );
 
   70   painter.fillRect( QRect( 0, 0, 
iconSize, 
iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
 
   72   QColor pixmapColor = 
color;
 
   73   pixmapColor.setAlpha( 255 );
 
   74   painter.setBrush( QBrush( pixmapColor ) );
 
   75   painter.setPen( QPen( Qt::white ) );
 
  150   color.getHsv( &h, &s, &v, &a );
 
  158       color.setRed( clippedValue );
 
  161       color.setGreen( clippedValue );
 
  164       color.setBlue( clippedValue );
 
  167       color.setHsv( clippedValue, s, v, a );
 
  170       color.setHsv( h, clippedValue, v, a );
 
  173       color.setHsv( h, s, clippedValue, a );
 
  176       color.setAlpha( clippedValue );
 
  185   static QPixmap sTranspBkgrd;
 
  187   if ( sTranspBkgrd.isNull() )
 
  199   if ( mimeColor.isValid() )
 
  202     e->acceptProposedAction();
 
  209   bool hasAlpha = 
false;
 
  212   if ( mimeColor.isValid() )
 
  215     e->acceptProposedAction();
 
  274   valueClipped = std::max( valueClipped, 0 );
 
  286       if ( r == valueClipped )
 
  293       if ( g == valueClipped )
 
  300       if ( b == valueClipped )
 
  307       if ( h == valueClipped )
 
  314       if ( s == valueClipped )
 
  321       if ( v == valueClipped )
 
  328       if ( a == valueClipped )
 
  357   if ( 
color.hue() >= 0 )
 
  379   QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
 
  380   const int wheelStops = 20;
 
  381   QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
 
  382   for ( 
int pos = 0; pos <= wheelStops; ++pos )
 
  384     const double relativePos = 
static_cast<double>( pos ) / wheelStops;
 
  385     gradColor.setHsvF( relativePos, 1, 1 );
 
  386     wheelGradient.setColorAt( relativePos, gradColor );
 
  388   mWheelBrush = QBrush( wheelGradient );
 
  394   delete mTriangleImage;
 
  401   return QSize( size, size );
 
  407   QPainter painter( 
this );
 
  409   if ( !mWidgetImage || !mWheelImage || !mTriangleImage )
 
  411     createImages( size() );
 
  415   mWidgetImage->fill( Qt::transparent );
 
  416   QPainter imagePainter( mWidgetImage );
 
  417   imagePainter.setRenderHint( QPainter::Antialiasing );
 
  426   const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
 
  427   imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage );
 
  431   const double length = mWheelImage->width() / 2.0;
 
  432   QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
 
  433   hueMarkerLine.setAngle( h );
 
  436   imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
 
  440   pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
 
  441   imagePainter.setPen( pen );
 
  442   imagePainter.drawLine( hueMarkerLine );
 
  443   imagePainter.restore();
 
  446   if ( mTriangleDirty )
 
  450   imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage );
 
  453   const double triangleRadius = length - mWheelThickness - 1;
 
  457   const double hueRadians = ( h * M_PI / 180.0 );
 
  458   const double hx = std::cos( hueRadians ) * triangleRadius;
 
  459   const double hy = -std::sin( hueRadians ) * triangleRadius;
 
  460   const double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  461   const double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  462   const double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  463   const double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  464   const double mx = ( sx + vx ) / 2.0;
 
  465   const double  my = ( sy + vy ) / 2.0;
 
  467   const double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) * 
mCurrentColor.hslSaturationF();
 
  468   const double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
 
  469   const double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
 
  472   pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
 
  473   imagePainter.setPen( pen );
 
  474   imagePainter.setBrush( Qt::NoBrush );
 
  475   imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0, 4.0 );
 
  479   painter.drawImage( QPoint( 0, 0 ), *mWidgetImage );
 
  488     mTriangleDirty = 
true;
 
  494 void QgsColorWheel::createImages( 
const QSizeF size )
 
  496   const double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
 
  497   mWheelThickness = wheelSize / 15.0;
 
  501   mWheelImage = 
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
 
  502   delete mTriangleImage;
 
  503   mTriangleImage = 
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
 
  505   mWidgetImage = 
new QImage( size.width(), size.height(), QImage::Format_ARGB32 );
 
  509   mTriangleDirty = 
true;
 
  515   createImages( event->size() );
 
  516   QgsColorWidget::resizeEvent( event );
 
  519 void QgsColorWheel::setColorFromPos( 
const QPointF pos )
 
  521   const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
 
  523   const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
 
  525   QColor newColor = QColor();
 
  532   if ( mClickedPart == QgsColorWheel::Triangle )
 
  537     const double x = pos.x() - center.x();
 
  538     const double y = pos.y() - center.y();
 
  540     double eventAngleRadians = line.angle() * M_PI / 180.0;
 
  541     const double hueRadians = h * M_PI / 180.0;
 
  542     double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
 
  543     double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
 
  544     const double length = mWheelImage->width() / 2.0;
 
  545     const double triangleLength = length - mWheelThickness - 1;
 
  547     const double a = 0.5 * triangleLength;
 
  548     double b = std::tan( rad1 ) * a;
 
  549     double r = std::sqrt( x * x + y * y );
 
  550     const double maxR = std::sqrt( a * a + b * b );
 
  554       const double dx = std::tan( rad1 ) * r;
 
  555       double rad2 = std::atan( dx / maxR );
 
  556       rad2 = std::min( rad2, M_PI / 3.0 );
 
  557       rad2 = std::max( rad2, -M_PI / 3.0 );
 
  558       eventAngleRadians += rad2 - rad1;
 
  559       rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
 
  560       rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
 
  561       b = std::tan( rad1 ) * a;
 
  562       r = std::sqrt( a * a + b * b );
 
  565     const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
 
  566     const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
 
  567     const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
 
  568     const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
 
  569     s = std::min( 
static_cast< int >( std::round( std::max( 0.0, newS ) * 255.0 ) ), 255 );
 
  570     l = std::min( 
static_cast< int >( std::round( std::max( 0.0, newL ) * 255.0 ) ), 255 );
 
  571     newColor = QColor::fromHsl( h, s, l );
 
  573     newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
 
  575   else if ( mClickedPart == QgsColorWheel::Wheel )
 
  580     const int newHue = line.angle();
 
  581     newColor = QColor::fromHsv( newHue, s, v, alpha );
 
  583     mTriangleDirty = 
true;
 
  604   setColorFromPos( event->pos() );
 
  613   const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
 
  615   const double innerLength = mWheelImage->width() / 2.0 - mWheelThickness;
 
  616   if ( line.length() < innerLength )
 
  618     mClickedPart = QgsColorWheel::Triangle;
 
  622     mClickedPart = QgsColorWheel::Wheel;
 
  624   setColorFromPos( event->pos() );
 
  630   mClickedPart = QgsColorWheel::None;
 
  633 void QgsColorWheel::createWheel()
 
  640   const int maxSize = std::min( mWheelImage->width(),  mWheelImage->height() );
 
  641   const double wheelRadius = maxSize / 2.0;
 
  643   mWheelImage->fill( Qt::transparent );
 
  644   QPainter p( mWheelImage );
 
  645   p.setRenderHint( QPainter::Antialiasing );
 
  646   p.setBrush( mWheelBrush );
 
  647   p.setPen( Qt::NoPen );
 
  650   p.translate( wheelRadius, wheelRadius );
 
  651   p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
 
  654   p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
 
  655   p.setBrush( QBrush( Qt::black ) );
 
  656   p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius - mWheelThickness, wheelRadius  - mWheelThickness );
 
  662 void QgsColorWheel::createTriangle()
 
  664   if ( !mWheelImage || !mTriangleImage )
 
  669   const QPointF center = QPointF( mWheelImage->width() / 2.0, mWheelImage->height() / 2.0 );
 
  670   mTriangleImage->fill( Qt::transparent );
 
  672   QPainter imagePainter( mTriangleImage );
 
  673   imagePainter.setRenderHint( QPainter::Antialiasing );
 
  676   const double wheelRadius = mWheelImage->width() / 2.0;
 
  677   const double triangleRadius = wheelRadius - mWheelThickness - 1;
 
  680   const QColor pureColor = QColor::fromHsv( 
angle, 255, 255 );
 
  682   QColor alphaColor = QColor( pureColor );
 
  683   alphaColor.setAlpha( 0 );
 
  686   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 ) );
 
  687   QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
 
  688   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 ) );
 
  689   QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
 
  690   QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
 
  691   line1.setAngle( line1.angle() + 
angle );
 
  692   line2.setAngle( line2.angle() + 
angle );
 
  693   line3.setAngle( line3.angle() + 
angle );
 
  694   line4.setAngle( line4.angle() + 
angle );
 
  695   line5.setAngle( line5.angle() + 
angle );
 
  696   const QPointF p1 = line1.p2();
 
  697   const QPointF p2 = line2.p2();
 
  698   const QPointF p3 = line3.p2();
 
  699   const QPointF p4 = line4.p2();
 
  700   const QPointF p5 = line5.p2();
 
  703   QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
 
  704   colorGrad.setColorAt( 0, alphaColor );
 
  705   colorGrad.setColorAt( 1, pureColor );
 
  706   QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
 
  707   whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
 
  708   whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
 
  711   triangle << p2 << p1 << p3 << p2;
 
  712   imagePainter.setPen( Qt::NoPen );
 
  714   imagePainter.setBrush( QBrush( Qt::black ) );
 
  715   imagePainter.drawPolygon( triangle );
 
  717   imagePainter.setBrush( QBrush( colorGrad ) );
 
  718   imagePainter.drawPolygon( triangle );
 
  720   imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
 
  721   imagePainter.setBrush( QBrush( whiteGrad ) );
 
  722   imagePainter.drawPolygon( triangle );
 
  727   imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
 
  728   imagePainter.setBrush( Qt::NoBrush );
 
  729   imagePainter.setPen( QPen( Qt::transparent ) );
 
  730   imagePainter.drawPolygon( triangle );
 
  733   mTriangleDirty = 
false;
 
  745   setFocusPolicy( Qt::StrongFocus );
 
  746   setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
 
  748   mBoxImage = 
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
 
  759   return QSize( size, size );
 
  765   QPainter painter( 
this );
 
  767   QStyleOptionFrame option;
 
  768   option.initFrom( 
this );
 
  769   option.state = hasFocus() ? QStyle::State_Active :  QStyle::State_None;
 
  770   style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
 
  778   painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
 
  781   const double xPos = mMargin + ( width() - 2 * mMargin - 1 ) * 
static_cast<double>( xComponentValue() ) / 
static_cast<double>( valueRangeX() );
 
  782   const double yPos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) * 
static_cast<double>( yComponentValue() ) / 
static_cast<double>( valueRangeY() );
 
  784   painter.setBrush( Qt::white );
 
  785   painter.setPen( Qt::NoPen );
 
  787   painter.drawRect( xPos - 1, mMargin, 3, height() - 2 * mMargin - 1 );
 
  788   painter.drawRect( mMargin, yPos - 1, width() - 2 * mMargin - 1, 3 );
 
  789   painter.setPen( Qt::black );
 
  790   painter.drawLine( xPos, mMargin, xPos, height() - mMargin - 1 );
 
  791   painter.drawLine( mMargin, yPos, width() - mMargin - 1, yPos );
 
  840   mBoxImage = 
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
 
  841   QgsColorWidget::resizeEvent( event );
 
  846   setColorFromPoint( event->pos() );
 
  852   setColorFromPoint( event->pos() );
 
  855 void QgsColorBox::createBox()
 
  857   const int maxValueX = mBoxImage->width();
 
  858   const int maxValueY = mBoxImage->height();
 
  862   int colorComponentValue;
 
  864   for ( 
int y = 0; y < maxValueY; ++y )
 
  866     QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
 
  868     colorComponentValue = int( valueRangeY() - valueRangeY() * ( 
double( y ) / maxValueY ) );
 
  869     alterColor( currentColor, yComponent(), colorComponentValue );
 
  870     for ( 
int x = 0; x < maxValueX; ++x )
 
  872       colorComponentValue = int( valueRangeX() * ( 
double( x ) / maxValueX ) );
 
  873       alterColor( currentColor, xComponent(), colorComponentValue );
 
  874       scanLine[x] = currentColor.rgb();
 
  880 int QgsColorBox::valueRangeX()
 const 
  885 int QgsColorBox::valueRangeY()
 const 
  910 int QgsColorBox::yComponentValue()
 const 
  935 int QgsColorBox::xComponentValue()
 const 
  940 void QgsColorBox::setColorFromPoint( QPoint point )
 
  942   int valX = valueRangeX() * ( point.x() - mMargin ) / ( width() - 2 * mMargin - 1 );
 
  943   valX = std::min( std::max( valX, 0 ), valueRangeX() );
 
  945   int valY = valueRangeY() - valueRangeY() * ( point.y() - mMargin ) / ( height() - 2 * mMargin - 1 );
 
  946   valY = std::min( std::max( valY, 0 ), valueRangeY() );
 
  957   if ( 
color.hue() >= 0 )
 
  977   setFocusPolicy( Qt::StrongFocus );
 
 1001   QPainter painter( 
this );
 
 1006     QStyleOptionFrame option;
 
 1007     option.initFrom( 
this );
 
 1008     option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
 
 1009     style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
 
 1015     QStyleOptionFocusRect option;
 
 1016     option.initFrom( 
this );
 
 1017     option.state = QStyle::State_KeyboardFocusChange;
 
 1018     style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
 
 1025     color.setAlpha( 255 );
 
 1032     painter.setPen( pen );
 
 1033     painter.setBrush( Qt::NoBrush );
 
 1036     for ( 
int c = 0; 
c <= maxValue; ++
c )
 
 1038       int colorVal = 
static_cast<int>( 
componentRange() * 
static_cast<double>( 
c ) / maxValue );
 
 1045       if ( 
color.hue() < 0 )
 
 1049       pen.setColor( 
color );
 
 1050       painter.setPen( pen );
 
 1054         painter.drawLine( QLineF( 
c + mMargin, mMargin, 
c + mMargin, height() - mMargin - 1 ) );
 
 1059         painter.drawLine( QLineF( mMargin, 
c + mMargin, width() - mMargin - 1, 
c + mMargin ) );
 
 1068     painter.setBrush( checkBrush );
 
 1069     painter.setPen( Qt::NoPen );
 
 1070     painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
 
 1071     QLinearGradient colorGrad;
 
 1075       colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 );
 
 1080       colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 );
 
 1083     transparent.setAlpha( 0 );
 
 1084     colorGrad.setColorAt( 0, transparent );
 
 1086     opaque.setAlpha( 255 );
 
 1087     colorGrad.setColorAt( 1, opaque );
 
 1088     const QBrush colorBrush = QBrush( colorGrad );
 
 1089     painter.setBrush( colorBrush );
 
 1090     painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
 
 1096     painter.setRenderHint( QPainter::Antialiasing );
 
 1097     painter.setBrush( QBrush( Qt::black ) );
 
 1098     painter.setPen( Qt::NoPen );
 
 1100     painter.drawPolygon( mTopTriangle );
 
 1101     painter.translate( 0, height() - mMargin - 2 );
 
 1102     painter.setBrush( QBrush( Qt::white ) );
 
 1103     painter.drawPolygon( mBottomTriangle );
 
 1109     const double ypos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) * 
static_cast<double>( 
componentValue() ) / 
componentRange();
 
 1110     painter.setBrush( Qt::white );
 
 1111     painter.setPen( Qt::NoPen );
 
 1112     painter.drawRect( QRectF( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 ) );
 
 1113     painter.setPen( Qt::black );
 
 1114     painter.drawLine( QLineF( mMargin, ypos, width() - mMargin - 1, ypos ) );
 
 1124     setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
 
 1129     setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
 
 1136   if ( margin == mMargin )
 
 1157   mTopTriangle << QPoint( -markerSize, 0 ) <<  QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
 
 1158   mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
 
 1164   setColorFromPoint( event->pos() );
 
 1172   if ( event->angleDelta().y() > 0 )
 
 1193   setColorFromPoint( event->pos() );
 
 1232     QgsColorWidget::keyPressEvent( event );
 
 1244 void QgsColorRampWidget::setColorFromPoint( QPointF point )
 
 1250     val = 
componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin );
 
 1275   QHBoxLayout *hLayout = 
new QHBoxLayout();
 
 1276   hLayout->setContentsMargins( 0, 0, 0, 0 );
 
 1277   hLayout->setSpacing( 5 );
 
 1281   hLayout->addWidget( mRampWidget, 1 );
 
 1283   mSpinBox = 
new QSpinBox();
 
 1285   const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( QStringLiteral( 
"888%" ) );
 
 1286   mSpinBox->setMinimumWidth( largestCharWidth + 35 );
 
 1287   mSpinBox->setMinimum( 0 );
 
 1288   mSpinBox->setMaximum( convertRealToDisplay( 
componentRange() ) );
 
 1293     mSpinBox->setSuffix( QChar( 176 ) );
 
 1297     mSpinBox->setSuffix( tr( 
"%" ) );
 
 1299   hLayout->addWidget( mSpinBox );
 
 1300   setLayout( hLayout );
 
 1304   connect( mSpinBox, 
static_cast < void ( QSpinBox::* )( 
int ) 
> ( &QSpinBox::valueChanged ), 
this, &QgsColorSliderWidget::spinChanged );
 
 1311   mSpinBox->setMaximum( convertRealToDisplay( 
componentRange() ) );
 
 1315     mSpinBox->setSuffix( QChar( 176 ) );
 
 1320     mSpinBox->setSuffix( tr( 
"%" ) );
 
 1325     mSpinBox->setSuffix( QString() );
 
 1332   mRampWidget->blockSignals( 
true );
 
 1334   mRampWidget->blockSignals( 
false );
 
 1335   mSpinBox->blockSignals( 
true );
 
 1336   mSpinBox->setValue( convertRealToDisplay( value ) );
 
 1337   mSpinBox->blockSignals( 
false );
 
 1344   mSpinBox->blockSignals( 
true );
 
 1346   mSpinBox->blockSignals( 
false );
 
 1349 void QgsColorSliderWidget::rampColorChanged( 
const QColor &color )
 
 1354 void QgsColorSliderWidget::spinChanged( 
int value )
 
 1356   const int convertedValue = convertDisplayToReal( value );
 
 1362 void QgsColorSliderWidget::rampChanged( 
int value )
 
 1364   mSpinBox->blockSignals( 
true );
 
 1365   mSpinBox->setValue( convertRealToDisplay( value ) );
 
 1366   mSpinBox->blockSignals( 
false );
 
 1370 int QgsColorSliderWidget::convertRealToDisplay( 
const int realValue )
 const 
 1376     return std::round( 100.0 * realValue / 255.0 );
 
 1383 int QgsColorSliderWidget::convertDisplayToReal( 
const int displayValue )
 const 
 1388     return std::round( 255.0 * displayValue / 100.0 );
 
 1392   return displayValue;
 
 1402   QHBoxLayout *hLayout = 
new QHBoxLayout();
 
 1403   hLayout->setContentsMargins( 0, 0, 0, 0 );
 
 1404   hLayout->setSpacing( 0 );
 
 1406   mLineEdit = 
new QLineEdit( 
nullptr );
 
 1407   hLayout->addWidget( mLineEdit );
 
 1409   mMenuButton = 
new QToolButton( mLineEdit );
 
 1411   mMenuButton->setCursor( Qt::ArrowCursor );
 
 1412   mMenuButton->setFocusPolicy( Qt::NoFocus );
 
 1413   mMenuButton->setStyleSheet( QStringLiteral( 
"QToolButton { border: none; padding: 0px; }" ) );
 
 1415   setLayout( hLayout );
 
 1417   const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
 
 1418   mLineEdit->setStyleSheet( QStringLiteral( 
"QLineEdit { padding-right: %1px; } " )
 
 1419                             .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
 
 1421   connect( mLineEdit, &QLineEdit::editingFinished, 
this, &QgsColorTextWidget::textChanged );
 
 1422   connect( mMenuButton, &QAbstractButton::clicked, 
this, &QgsColorTextWidget::showMenu );
 
 1426   mFormat = settings.
enumValue( QStringLiteral( 
"ColorWidgets/textWidgetFormat" ), 
HexRgb );
 
 1440   const QSize sz = mMenuButton->sizeHint();
 
 1441   const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
 
 1442   mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
 
 1443                      ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
 
 1446 void QgsColorTextWidget::updateText()
 
 1465 void QgsColorTextWidget::textChanged()
 
 1467   const QString testString = mLineEdit->text();
 
 1470   if ( !
color.isValid() )
 
 1481     if ( !containsAlpha )
 
 1492 void QgsColorTextWidget::showMenu()
 
 1494   QMenu colorContextMenu;
 
 1496   QAction *hexRgbAction = 
new QAction( tr( 
"#RRGGBB" ), 
nullptr );
 
 1497   colorContextMenu.addAction( hexRgbAction );
 
 1498   QAction *hexRgbaAction = 
new QAction( tr( 
"#RRGGBBAA" ), 
nullptr );
 
 1499   colorContextMenu.addAction( hexRgbaAction );
 
 1500   QAction *rgbAction = 
new QAction( tr( 
"rgb( r, g, b )" ), 
nullptr );
 
 1501   colorContextMenu.addAction( rgbAction );
 
 1502   QAction *rgbaAction = 
new QAction( tr( 
"rgba( r, g, b, a )" ), 
nullptr );
 
 1503   colorContextMenu.addAction( rgbaAction );
 
 1505   QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
 
 1506   if ( selectedAction == hexRgbAction )
 
 1510   else if ( selectedAction == hexRgbaAction )
 
 1514   else if ( selectedAction == rgbAction )
 
 1518   else if ( selectedAction == rgbaAction )
 
 1525   settings.
setEnumValue( QStringLiteral( 
"ColorWidgets/textWidgetFormat" ), mFormat );
 
 1537   , mColor2( QColor() )
 
 1542 void QgsColorPreviewWidget::drawColor( 
const QColor &color, QRect rect, QPainter &painter )
 
 1544   painter.setPen( Qt::NoPen );
 
 1546   if ( 
color.alpha() < 255 )
 
 1549     painter.setBrush( checkBrush );
 
 1550     painter.drawRect( rect );
 
 1555     const QBrush colorBrush = QBrush( 
color );
 
 1556     painter.setBrush( colorBrush );
 
 1557     painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
 
 1559     QColor opaqueColor = QColor( 
color );
 
 1560     opaqueColor.setAlpha( 255 );
 
 1561     const QBrush opaqueBrush = QBrush( opaqueColor );
 
 1562     painter.setBrush( opaqueBrush );
 
 1563     painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
 
 1568     const QBrush brush = QBrush( 
color );
 
 1569     painter.setBrush( brush );
 
 1570     painter.drawRect( rect );
 
 1577   QPainter painter( 
this );
 
 1579   if ( mColor2.isValid() )
 
 1582     const int verticalSplit = std::round( height() / 2.0 );
 
 1583     drawColor( 
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
 
 1584     drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
 
 1588     drawColor( 
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
 
 1601   if ( 
color == mColor2 )
 
 1611   if ( e->button() == Qt::LeftButton )
 
 1613     mDragStartPosition = e->pos();
 
 1620   if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
 
 1629   if ( mColor2.isValid() )
 
 1632     const int verticalSplit = std::round( height() / 2.0 );
 
 1633     if ( mDragStartPosition.y() >= verticalSplit )
 
 1635       clickedColor = mColor2;
 
 1646   if ( !( e->buttons() & Qt::LeftButton ) )
 
 1653   if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
 
 1664   if ( mColor2.isValid() )
 
 1667     const int verticalSplit = std::round( height() / 2.0 );
 
 1668     if ( mDragStartPosition.y() >= verticalSplit )
 
 1670       dragColor = mColor2;
 
 1674   QDrag *drag = 
new QDrag( 
this );
 
 1677   drag->exec( Qt::CopyAction );
 
 1686   : QWidgetAction( parent )
 
 1688   , mColorWidget( colorWidget )
 
 1689   , mSuppressRecurse( false )
 
 1690   , mDismissOnColorSelection( true )
 
 1692   setDefaultWidget( mColorWidget );
 
 1695   connect( 
this, &QAction::hovered, 
this, &QgsColorWidgetAction::onHover );
 
 1699 void QgsColorWidgetAction::onHover()
 
 1702   if ( mSuppressRecurse )
 
 1709     mSuppressRecurse = 
true;
 
 1710     mMenu->setActiveAction( 
this );
 
 1711     mSuppressRecurse = 
false;
 
 1715 void QgsColorWidgetAction::setColor( 
const QColor &color )
 
 1718   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 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 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
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)
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
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