24#include <QResizeEvent> 
   26#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 
   27#include <QStyleOptionFrameV3> 
   29#include <QStyleOptionFrame> 
   34#include <QFontMetrics> 
   47#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 
   60  , mCurrentColor( Qt::red )
 
   61  , mComponent( component )
 
   63  setAcceptDrops( 
true );
 
 
   80  QPixmap pixmap( iconSize, iconSize );
 
   81  pixmap.fill( Qt::transparent );
 
   83  painter.begin( &pixmap );
 
   85  painter.fillRect( QRect( 0, 0, iconSize, iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
 
   87  QColor pixmapColor = 
color;
 
   88  pixmapColor.setAlpha( 255 );
 
   89  painter.setBrush( QBrush( pixmapColor ) );
 
   90  painter.setPen( QPen( Qt::white ) );
 
   91  painter.drawRect( QRect( 1, 1, iconSize - 2, iconSize - 2 ) );
 
 
  193  return static_cast<int>( std::round( 
hueF() * 
HUE_MAX ) );
 
 
  217  float_type clippedValue = std::clamp( newValue, 0.f, 1.f );
 
  222    color.getCmykF( &
c, &m, &y, &k, &a );
 
  227        color.setCmykF( clippedValue, m, y, k, a );
 
  230        color.setCmykF( 
c, clippedValue, y, k, a );
 
  233        color.setCmykF( 
c, m, clippedValue, k, a );
 
  236        color.setCmykF( 
c, m, y, clippedValue, a );
 
  245    color.getRgbF( &r, &g, &b, &a );
 
  247    color.getHsvF( &h, &s, &v );
 
  252        color.setRedF( clippedValue );
 
  255        color.setGreenF( clippedValue );
 
  258        color.setBlueF( clippedValue );
 
  261        color.setHsvF( clippedValue, s, v, a );
 
  264        color.setHsvF( h, clippedValue, v, a );
 
  267        color.setHsvF( h, s, clippedValue, a );
 
  270        color.setAlphaF( clippedValue );
 
 
  285      return QColor::Spec::Rgb;
 
  290      return QColor::Spec::Hsv;
 
  296      return QColor::Spec::Cmyk;
 
  299      return QColor::Spec::Invalid;
 
 
  310  static QPixmap sTranspBkgrd;
 
  312  if ( sTranspBkgrd.isNull() )
 
 
  324  if ( mimeColor.isValid() )
 
  327    e->acceptProposedAction();
 
 
  334  bool hasAlpha = 
false;
 
  337  if ( mimeColor.isValid() )
 
  340    e->acceptProposedAction();
 
 
  434  if ( 
color.hue() >= 0 )
 
 
  456  QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
 
  457  const int wheelStops = 20;
 
  458  QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
 
  459  for ( 
int pos = 0; pos <= wheelStops; ++pos )
 
  461    const double relativePos = 
static_cast<double>( pos ) / wheelStops;
 
  462    gradColor.setHsvF( relativePos, 1, 1 );
 
  463    wheelGradient.setColorAt( relativePos, gradColor );
 
  465  mWheelBrush = QBrush( wheelGradient );
 
 
  473  return QSize( size, size );
 
 
  479  QPainter painter( 
this );
 
  481  if ( mWidgetImage.isNull() || mWheelImage.isNull() || mTriangleImage.isNull() )
 
  483    createImages( size() );
 
  487  mWidgetImage.fill( Qt::transparent );
 
  488  QPainter imagePainter( &mWidgetImage );
 
  489  imagePainter.setRenderHint( QPainter::Antialiasing );
 
  498  const QPointF center = QPointF( mWidgetImage.width() / 2.0, mWidgetImage.height() / 2.0 );
 
  499  imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mWheelImage );
 
  503  const double length = mWheelImage.width() / 2.0;
 
  504  QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
 
  505  hueMarkerLine.setAngle( h );
 
  508  imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
 
  510  pen.setWidthF( 2 * devicePixelRatioF() );
 
  512  pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
 
  513  imagePainter.setPen( pen );
 
  514  imagePainter.drawLine( hueMarkerLine );
 
  515  imagePainter.restore();
 
  518  if ( mTriangleDirty )
 
  522  imagePainter.drawImage( QPointF( center.x() - ( mWheelImage.width() / 2.0 ), center.y() - ( mWheelImage.height() / 2.0 ) ), mTriangleImage );
 
  525  const double triangleRadius = length - mWheelThickness * devicePixelRatioF() - 1;
 
  529  const double hueRadians = ( h * M_PI / 180.0 );
 
  530  const double hx = std::cos( hueRadians ) * triangleRadius;
 
  531  const double hy = -std::sin( hueRadians ) * triangleRadius;
 
  532  const double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  533  const double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  534  const double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  535  const double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  536  const double mx = ( sx + vx ) / 2.0;
 
  537  const double  my = ( sy + vy ) / 2.0;
 
  539  const double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) * 
mCurrentColor.hslSaturationF();
 
  540  const double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
 
  541  const double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
 
  544  pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
 
  545  imagePainter.setPen( pen );
 
  546  imagePainter.setBrush( Qt::NoBrush );
 
  547  imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0 * devicePixelRatioF(), 4.0 * devicePixelRatioF() );
 
  551  painter.drawImage( QRectF( 0, 0, width(), height() ), mWidgetImage );
 
 
  560    mTriangleDirty = 
true;
 
 
  566void QgsColorWheel::createImages( 
const QSizeF size )
 
  568  const double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
 
  569  mWheelThickness = wheelSize / 15.0;
 
  572  const double pixelRatio = devicePixelRatioF();
 
  573  mWheelImage = QImage( wheelSize * pixelRatio,
 
  574                        wheelSize * pixelRatio, QImage::Format_ARGB32 );
 
  575  mTriangleImage = QImage( wheelSize * pixelRatio,
 
  576                           wheelSize * pixelRatio, QImage::Format_ARGB32 );
 
  577  mWidgetImage = QImage( size.width() * pixelRatio,
 
  578                         size.height() * pixelRatio, QImage::Format_ARGB32 );
 
  582  mTriangleDirty = 
true;
 
  587  QgsColorWidget::resizeEvent( event );
 
  590  if ( event->size().width() > parentWidget()->size().width() )
 
  593      std::min( event->size().width(), parentWidget()->size().width() - 2 ),
 
  594      std::min( event->size().height(), parentWidget()->size().height() - 2 )
 
  597    createImages( newSize );
 
  601    createImages( event->size() );
 
  605  createImages( event->size() );
 
 
  609void QgsColorWheel::setColorFromPos( 
const QPointF pos )
 
  611  const QPointF center = QPointF( width() / 2.0, height() / 2.0 );
 
  613  const QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
 
  615  QColor newColor = QColor();
 
  622  if ( mClickedPart == QgsColorWheel::Triangle )
 
  627    const double x = pos.x() - center.x();
 
  628    const double y = pos.y() - center.y();
 
  630    double eventAngleRadians = line.angle() * M_PI / 180.0;
 
  631    const double hueRadians = h * 2 * M_PI;
 
  632    double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
 
  633    double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
 
  634    const double length = mWheelImage.width() / 2.0 / devicePixelRatioF();
 
  635    const double triangleLength = length - mWheelThickness - 1;
 
  637    const double a = 0.5 * triangleLength;
 
  638    double b = std::tan( rad1 ) * a;
 
  639    double r = std::sqrt( x * x + y * y );
 
  640    const double maxR = std::sqrt( a * a + b * b );
 
  644      const double dx = std::tan( rad1 ) * r;
 
  645      double rad2 = std::atan( dx / maxR );
 
  646      rad2 = std::min( rad2, M_PI / 3.0 );
 
  647      rad2 = std::max( rad2, -M_PI / 3.0 );
 
  648      eventAngleRadians += rad2 - rad1;
 
  649      rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
 
  650      rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
 
  651      b = std::tan( rad1 ) * a;
 
  652      r = std::sqrt( a * a + b * b );
 
  655    const double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
 
  656    const double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
 
  657    const double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
 
  658    const double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
 
  659    s = std::min( std::max( 0.f, 
static_cast<float>( newS ) ), 1.f );
 
  660    l = std::min( std::max( 0.f, 
static_cast<float>( newL ) ), 1.f );
 
  661    newColor = QColor::fromHslF( h, s, l );
 
  663    newColor.setHsvF( h, newColor.hsvSaturationF(), newColor.valueF(), alpha );
 
  665  else if ( mClickedPart == QgsColorWheel::Wheel )
 
  670    const qreal newHue = line.angle() / 
HUE_MAX;
 
  671    newColor = QColor::fromHsvF( 
static_cast<float>( newHue ), s, v, alpha );
 
  673    mTriangleDirty = 
true;
 
  695    setColorFromPos( event->pos() );
 
 
  702  if ( event->button() == Qt::LeftButton )
 
  708    const QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
 
  710    const double innerLength = mWheelImage.width() / 2.0 / devicePixelRatioF() - mWheelThickness;
 
  711    if ( line.length() < innerLength )
 
  713      mClickedPart = QgsColorWheel::Triangle;
 
  717      mClickedPart = QgsColorWheel::Wheel;
 
  719    setColorFromPos( event->pos() );
 
 
  729  if ( event->button() == Qt::LeftButton )
 
  732    mClickedPart = QgsColorWheel::None;
 
 
  740void QgsColorWheel::createWheel()
 
  742  if ( mWheelImage.isNull() )
 
  747  const int maxSize = std::min( mWheelImage.width(),  mWheelImage.height() );
 
  748  const double wheelRadius = maxSize / 2.0;
 
  750  mWheelImage.fill( Qt::transparent );
 
  751  QPainter p( &mWheelImage );
 
  752  p.setRenderHint( QPainter::Antialiasing );
 
  753  p.setBrush( mWheelBrush );
 
  754  p.setPen( Qt::NoPen );
 
  757  p.translate( wheelRadius, wheelRadius );
 
  758  p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
 
  761  p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
 
  762  p.setBrush( QBrush( Qt::black ) );
 
  763  p.drawEllipse( QPointF( 0,
 
  765                 wheelRadius - mWheelThickness * devicePixelRatioF(),
 
  766                 wheelRadius  - mWheelThickness * devicePixelRatioF() );
 
  772void QgsColorWheel::createTriangle()
 
  774  if ( mWheelImage.isNull() || mTriangleImage.isNull() )
 
  779  const QPointF center = QPointF( mWheelImage.width() / 2.0, mWheelImage.height() / 2.0 );
 
  780  mTriangleImage.fill( Qt::transparent );
 
  782  QPainter imagePainter( &mTriangleImage );
 
  783  imagePainter.setRenderHint( QPainter::Antialiasing );
 
  787  const double wheelRadius = mWheelImage.width() / 2.0;
 
  788  const double triangleRadius = wheelRadius - mWheelThickness * devicePixelRatioF() - 1;
 
  791  const QColor pureColor = QColor::fromHsvF( angle, 1., 1. );
 
  793  QColor alphaColor = QColor( pureColor );
 
  794  alphaColor.setAlpha( 0 );
 
  797  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 ) );
 
  798  QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
 
  799  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 ) );
 
  800  QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
 
  801  QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
 
  802  line1.setAngle( line1.angle() + angleDegree );
 
  803  line2.setAngle( line2.angle() + angleDegree );
 
  804  line3.setAngle( line3.angle() + angleDegree );
 
  805  line4.setAngle( line4.angle() + angleDegree );
 
  806  line5.setAngle( line5.angle() + angleDegree );
 
  807  const QPointF p1 = line1.p2();
 
  808  const QPointF p2 = line2.p2();
 
  809  const QPointF p3 = line3.p2();
 
  810  const QPointF p4 = line4.p2();
 
  811  const QPointF p5 = line5.p2();
 
  814  QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
 
  815  colorGrad.setColorAt( 0, alphaColor );
 
  816  colorGrad.setColorAt( 1, pureColor );
 
  817  QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
 
  818  whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
 
  819  whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
 
  822  triangle << p2 << p1 << p3 << p2;
 
  823  imagePainter.setPen( Qt::NoPen );
 
  825  imagePainter.setBrush( QBrush( Qt::black ) );
 
  826  imagePainter.drawPolygon( triangle );
 
  828  imagePainter.setBrush( QBrush( colorGrad ) );
 
  829  imagePainter.drawPolygon( triangle );
 
  831  imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
 
  832  imagePainter.setBrush( QBrush( whiteGrad ) );
 
  833  imagePainter.drawPolygon( triangle );
 
  838  imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
 
  839  imagePainter.setBrush( Qt::NoBrush );
 
  840  imagePainter.setPen( QPen( Qt::transparent ) );
 
  841  imagePainter.drawPolygon( triangle );
 
  844  mTriangleDirty = 
false;
 
  856  setFocusPolicy( Qt::StrongFocus );
 
  857  setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
 
  859  mBoxImage = 
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
 
 
  870  return QSize( size, size );
 
 
  876  QPainter painter( 
this );
 
  878  QStyleOptionFrame option;
 
  879  option.initFrom( 
this );
 
  880  option.state = hasFocus() ? QStyle::State_Active :  QStyle::State_None;
 
  881  style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
 
  889  painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
 
  892  const double h = height();
 
  893  const double w = width();
 
  894  const double margin = mMargin;
 
  895  const double xPos = ( mMargin + ( w - 2 * mMargin - 1 ) * xComponentValue() );
 
  896  const double yPos = ( mMargin + ( h - 2 * mMargin - 1 ) - ( h - 2 * mMargin - 1 ) * yComponentValue() );
 
  898  painter.setBrush( Qt::white );
 
  899  painter.setPen( Qt::NoPen );
 
  901  painter.drawRect( QRectF( xPos - 1, mMargin, 3, height() - 2 * margin - 1 ) );
 
  902  painter.drawRect( QRectF( mMargin, yPos - 1, width() - 2 * margin - 1, 3 ) );
 
  903  painter.setPen( Qt::black );
 
  904  painter.drawLine( QLineF( xPos, mMargin, xPos, height() - margin - 1 ) );
 
  905  painter.drawLine( QLineF( mMargin, yPos, width() - margin - 1, yPos ) );
 
 
  943  mBoxImage = 
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
 
  944  QgsColorWidget::resizeEvent( event );
 
 
  951    setColorFromPoint( event->pos() );
 
 
  958  if ( event->button() == Qt::LeftButton )
 
  961    setColorFromPoint( event->pos() );
 
 
  971  if ( event->button() == Qt::LeftButton )
 
 
  981void QgsColorBox::createBox()
 
  983  const int maxValueX = mBoxImage->width();
 
  984  const int maxValueY = mBoxImage->height();
 
  988  float colorComponentValue;
 
  990  for ( 
int y = 0; y < maxValueY; ++y )
 
  992    QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
 
  994    colorComponentValue = 1.f - 
static_cast<float>( y ) / 
static_cast<float>( maxValueY );
 
  995    alterColorF( currentColor, yComponent(), colorComponentValue );
 
  996    for ( 
int x = 0; x < maxValueX; ++x )
 
  998      colorComponentValue = 
static_cast<float>( x ) / 
static_cast<float>( maxValueY );
 
  999      alterColorF( currentColor, xComponent(), colorComponentValue );
 
 1000      scanLine[x] = currentColor.rgb();
 
 1006float QgsColorBox::valueRangeX()
 const 
 1011float QgsColorBox::valueRangeY()
 const 
 1044float QgsColorBox::yComponentValue()
 const 
 1077float QgsColorBox::xComponentValue()
 const 
 1082void QgsColorBox::setColorFromPoint( QPoint point )
 
 1084  const float x = 
static_cast<float>( point.x() );
 
 1085  const float y = 
static_cast<float>( point.y() );
 
 1086  const float w = 
static_cast<float>( width() );
 
 1087  const float h = 
static_cast<float>( height() );
 
 1089  float valX = ( x - mMargin ) / ( w - 2 * mMargin - 1 );
 
 1090  float valY = 1.f - ( y - mMargin ) / ( h - 2 * mMargin - 1 );
 
 1101  if ( 
color.hueF() >= 0 )
 
 1121  setFocusPolicy( Qt::StrongFocus );
 
 
 1145  QPainter painter( 
this );
 
 1150    QStyleOptionFrame option;
 
 1151    option.initFrom( 
this );
 
 1152    option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
 
 1153    style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
 
 1159    QStyleOptionFocusRect option;
 
 1160    option.initFrom( 
this );
 
 1161    option.state = QStyle::State_KeyboardFocusChange;
 
 1162    style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
 
 1165  float w = 
static_cast<float>( width() );
 
 1166  float h = 
static_cast<float>( height() );
 
 1167  float margin = 
static_cast<float>( mMargin );
 
 1172    color.setAlphaF( 1.f );
 
 1179    painter.setPen( pen );
 
 1180    painter.setBrush( Qt::NoBrush );
 
 1183    for ( 
int c = 0; 
c <= maxValue; ++
c )
 
 1185      float colorVal = 
static_cast<float>( 
c ) / 
static_cast<float>( maxValue );
 
 1189        colorVal = 1.f - colorVal;
 
 1192      if ( 
color.hueF() < 0 )
 
 1196      pen.setColor( 
color );
 
 1197      painter.setPen( pen );
 
 1201        painter.drawLine( QLineF( 
c + mMargin, mMargin, 
c + mMargin, height() - mMargin - 1 ) );
 
 1206        painter.drawLine( QLineF( mMargin, 
c + mMargin, width() - mMargin - 1, 
c + mMargin ) );
 
 1215    painter.setBrush( checkBrush );
 
 1216    painter.setPen( Qt::NoPen );
 
 1217    painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
 
 1218    QLinearGradient colorGrad;
 
 1222      colorGrad = QLinearGradient( margin, 0, w - margin - 1, 0 );
 
 1227      colorGrad = QLinearGradient( 0, margin, 0, h - margin - 1 );
 
 1230    transparent.setAlpha( 0 );
 
 1231    colorGrad.setColorAt( 0, transparent );
 
 1233    opaque.setAlpha( 255 );
 
 1234    colorGrad.setColorAt( 1, opaque );
 
 1235    const QBrush colorBrush = QBrush( colorGrad );
 
 1236    painter.setBrush( colorBrush );
 
 1237    painter.drawRect( QRectF( margin, margin, w - 2 * margin - 1, h - 2 * margin - 1 ) );
 
 1243    painter.setRenderHint( QPainter::Antialiasing );
 
 1244    painter.setBrush( QBrush( Qt::black ) );
 
 1245    painter.setPen( Qt::NoPen );
 
 1246    painter.translate( margin + ( w - 2 * margin ) * 
componentValueF(), margin - 1 );
 
 1247    painter.drawPolygon( mTopTriangle );
 
 1248    painter.translate( 0, h - margin - 2 );
 
 1249    painter.setBrush( QBrush( Qt::white ) );
 
 1250    painter.drawPolygon( mBottomTriangle );
 
 1256    const double ypos = margin + ( h - 2 * margin - 1 ) - ( h - 2 * margin - 1 ) * 
componentValueF();
 
 1257    painter.setBrush( Qt::white );
 
 1258    painter.setPen( Qt::NoPen );
 
 1259    painter.drawRect( QRectF( margin, ypos - 1, w - 2 * margin - 1, 3 ) );
 
 1260    painter.setPen( Qt::black );
 
 1261    painter.drawLine( QLineF( margin, ypos, w - margin - 1, ypos ) );
 
 
 1271    setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
 
 1276    setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
 
 
 1283  if ( margin == mMargin )
 
 
 1304  mTopTriangle << QPoint( -markerSize, 0 ) <<  QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
 
 1305  mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
 
 
 1313    setColorFromPoint( event->pos() );
 
 
 1322  const float delta = 1.f / 
static_cast<float>( 
componentRange() );
 
 1323  if ( event->angleDelta().y() > 0 )
 
 
 1347  if ( event->button() == Qt::LeftButton )
 
 1350    setColorFromPoint( event->pos() );
 
 
 1360  if ( event->button() == Qt::LeftButton )
 
 1362    mIsDragging = 
false;
 
 
 1373  const float delta = 1.f / 
static_cast<float>( 
componentRange() );
 
 1407    QgsColorWidget::keyPressEvent( event );
 
 
 1422void QgsColorRampWidget::setColorFromPoint( QPointF point )
 
 1426  const float margin = 
static_cast<float>( mMargin );
 
 1427  const float w = 
static_cast<float>( width() );
 
 1428  const float h = 
static_cast<float>( height() );
 
 1432    val = ( 
static_cast<float>( point.x() ) - margin ) / ( w - 2 * margin );
 
 1436    val = 1.f - ( 
static_cast<float>( point.y() ) - margin ) / ( h - 2 * margin );
 
 1459  QHBoxLayout *hLayout = 
new QHBoxLayout();
 
 1460  hLayout->setContentsMargins( 0, 0, 0, 0 );
 
 1461  hLayout->setSpacing( 5 );
 
 1465  hLayout->addWidget( mRampWidget, 1 );
 
 1470  const int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( QStringLiteral( 
"888%" ) );
 
 1471  mSpinBox->setMinimumWidth( largestCharWidth + 35 );
 
 1472  mSpinBox->setMinimum( 0 );
 
 1473  mSpinBox->setMaximum( convertRealToDisplay( 1.f ) );
 
 1475  hLayout->addWidget( mSpinBox );
 
 1476  setLayout( hLayout );
 
 1480  connect( mSpinBox, 
static_cast < void ( QDoubleSpinBox::* )( 
double ) 
> ( &QDoubleSpinBox::valueChanged ), 
this, &QgsColorSliderWidget::spinChanged );
 
 
 1487  mSpinBox->setMaximum( convertRealToDisplay( 
static_cast<float>( 
componentRange() ) ) );
 
 1492      mSpinBox->setSuffix( QChar( 176 ) );
 
 1496      mSpinBox->setSuffix( tr( 
"%" ) );
 
 1501      mSpinBox->setSuffix( QString() );
 
 
 1508  mRampWidget->blockSignals( 
true );
 
 1510  mRampWidget->blockSignals( 
false );
 
 1511  mSpinBox->blockSignals( 
true );
 
 1512  mSpinBox->setValue( convertRealToDisplay( value ) );
 
 1513  mSpinBox->blockSignals( 
false );
 
 
 1520  mSpinBox->blockSignals( 
true );
 
 1522  mSpinBox->blockSignals( 
false );
 
 
 1525void QgsColorSliderWidget::rampColorChanged( 
const QColor &color )
 
 1530void QgsColorSliderWidget::spinChanged( 
double value )
 
 1532  const float convertedValue = convertDisplayToReal( 
static_cast<float>( value ) );
 
 1538void QgsColorSliderWidget::rampChanged( 
float value )
 
 1540  mSpinBox->blockSignals( 
true );
 
 1541  mSpinBox->setValue( convertRealToDisplay( value ) );
 
 1542  mSpinBox->blockSignals( 
false );
 
 1546float QgsColorSliderWidget::convertRealToDisplay( 
const float realValue )
 const 
 1551      return realValue * 100.f;
 
 1557      return realValue * 255.f;
 
 1563float QgsColorSliderWidget::convertDisplayToReal( 
const float displayValue )
 const 
 1568      return displayValue / 100.f;
 
 1571      return displayValue / 
HUE_MAX;
 
 1574      return displayValue / 255.f;
 
 1587  QHBoxLayout *hLayout = 
new QHBoxLayout();
 
 1588  hLayout->setContentsMargins( 0, 0, 0, 0 );
 
 1589  hLayout->setSpacing( 0 );
 
 1591  mLineEdit = 
new QLineEdit( 
nullptr );
 
 1592  hLayout->addWidget( mLineEdit );
 
 1594  mMenuButton = 
new QToolButton( mLineEdit );
 
 1596  mMenuButton->setCursor( Qt::ArrowCursor );
 
 1597  mMenuButton->setFocusPolicy( Qt::NoFocus );
 
 1598  mMenuButton->setStyleSheet( QStringLiteral( 
"QToolButton { border: none; padding: 0px; }" ) );
 
 1600  setLayout( hLayout );
 
 1602  const int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
 
 1603  mLineEdit->setStyleSheet( QStringLiteral( 
"QLineEdit { padding-right: %1px; } " )
 
 1604                            .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
 
 1606  connect( mLineEdit, &QLineEdit::editingFinished, 
this, &QgsColorTextWidget::textChanged );
 
 1607  connect( mMenuButton, &QAbstractButton::clicked, 
this, &QgsColorTextWidget::showMenu );
 
 1611  mFormat = settings.
enumValue( QStringLiteral( 
"ColorWidgets/textWidgetFormat" ), 
HexRgb );
 
 
 1625  const QSize sz = mMenuButton->sizeHint();
 
 1626  const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
 
 1627  mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
 
 1628                     ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
 
 
 1631void QgsColorTextWidget::updateText()
 
 1650void QgsColorTextWidget::textChanged()
 
 1652  const QString testString = mLineEdit->text();
 
 1655  if ( !
color.isValid() )
 
 1666    if ( !containsAlpha )
 
 1677void QgsColorTextWidget::showMenu()
 
 1679  QMenu colorContextMenu;
 
 1680  QAction *hexRgbaAction = 
nullptr;
 
 1681  QAction *rgbaAction = 
nullptr;
 
 1683  QAction *hexRgbAction = 
new QAction( tr( 
"#RRGGBB" ), 
nullptr );
 
 1684  colorContextMenu.addAction( hexRgbAction );
 
 1687    hexRgbaAction = 
new QAction( tr( 
"#RRGGBBAA" ), 
nullptr );
 
 1688    colorContextMenu.addAction( hexRgbaAction );
 
 1690  QAction *rgbAction = 
new QAction( tr( 
"rgb( r, g, b )" ), 
nullptr );
 
 1691  colorContextMenu.addAction( rgbAction );
 
 1694    rgbaAction = 
new QAction( tr( 
"rgba( r, g, b, a )" ), 
nullptr );
 
 1695    colorContextMenu.addAction( rgbaAction );
 
 1698  QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
 
 1699  if ( selectedAction == hexRgbAction )
 
 1703  else if ( hexRgbaAction && selectedAction == hexRgbaAction )
 
 1707  else if ( selectedAction == rgbAction )
 
 1711  else if ( rgbaAction && selectedAction == rgbaAction )
 
 1718  settings.
setEnumValue( QStringLiteral( 
"ColorWidgets/textWidgetFormat" ), mFormat );
 
 1725  mAllowAlpha = allowOpacity;
 
 
 1734  , mColor2( QColor() )
 
 
 1739void QgsColorPreviewWidget::drawColor( 
const QColor &color, QRect rect, QPainter &painter )
 
 1741  painter.setPen( Qt::NoPen );
 
 1743  if ( 
color.alpha() < 255 )
 
 1746    painter.setBrush( checkBrush );
 
 1747    painter.drawRect( rect );
 
 1752    const QBrush colorBrush = QBrush( 
color );
 
 1753    painter.setBrush( colorBrush );
 
 1754    painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
 
 1756    QColor opaqueColor = QColor( 
color );
 
 1757    opaqueColor.setAlpha( 255 );
 
 1758    const QBrush opaqueBrush = QBrush( opaqueColor );
 
 1759    painter.setBrush( opaqueBrush );
 
 1760    painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
 
 1765    const QBrush brush = QBrush( 
color );
 
 1766    painter.setBrush( brush );
 
 1767    painter.drawRect( rect );
 
 1774  QPainter painter( 
this );
 
 1776  if ( mColor2.isValid() )
 
 1779    const int verticalSplit = std::round( height() / 2.0 );
 
 1780    drawColor( 
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
 
 1781    drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
 
 1785    drawColor( 
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
 
 
 1798  if ( 
color == mColor2 )
 
 
 1808  if ( e->button() == Qt::LeftButton )
 
 1810    mDragStartPosition = e->pos();
 
 
 1817  if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
 
 1826  if ( mColor2.isValid() )
 
 1829    const int verticalSplit = std::round( height() / 2.0 );
 
 1830    if ( mDragStartPosition.y() >= verticalSplit )
 
 1832      clickedColor = mColor2;
 
 
 1843  if ( !( e->buttons() & Qt::LeftButton ) )
 
 1850  if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
 
 1861  if ( mColor2.isValid() )
 
 1864    const int verticalSplit = std::round( height() / 2.0 );
 
 1865    if ( mDragStartPosition.y() >= verticalSplit )
 
 1867      dragColor = mColor2;
 
 1871  QDrag *drag = 
new QDrag( 
this );
 
 1874  drag->exec( Qt::CopyAction );
 
 
 1883  : QWidgetAction( parent )
 
 1885  , mColorWidget( colorWidget )
 
 1886  , mSuppressRecurse( false )
 
 1887  , mDismissOnColorSelection( true )
 
 1889  setDefaultWidget( mColorWidget );
 
 1892  connect( 
this, &QAction::hovered, 
this, &QgsColorWidgetAction::onHover );
 
 
 1896void QgsColorWidgetAction::onHover()
 
 1899  if ( mSuppressRecurse )
 
 1906    mSuppressRecurse = 
true;
 
 1907    mMenu->setActiveAction( 
this );
 
 1908    mSuppressRecurse = 
false;
 
 1912void QgsColorWidgetAction::setColor( 
const QColor &color )
 
 1915  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)