19 #include "qgssettings.h" 
   23 #include <QResizeEvent> 
   24 #include <QStyleOptionFrameV3> 
   26 #include <QHBoxLayout> 
   29 #include <QFontMetrics> 
   30 #include <QToolButton> 
   45   , mCurrentColor( Qt::red )
 
   46   , mComponent( component )
 
   48   setAcceptDrops( 
true );
 
   61   pixmap.fill( Qt::transparent );
 
   63   painter.begin( &pixmap );
 
   65   painter.fillRect( QRect( 0, 0, 
iconSize, 
iconSize ), QBrush( QColor( 200, 200, 200 ) ) );
 
   67   QColor pixmapColor = 
color;
 
   68   pixmapColor.setAlpha( 255 );
 
   69   painter.setBrush( QBrush( pixmapColor ) );
 
   70   painter.setPen( QPen( Qt::white ) );
 
  145   color.getHsv( &h, &s, &v, &a );
 
  153       color.setRed( clippedValue );
 
  156       color.setGreen( clippedValue );
 
  159       color.setBlue( clippedValue );
 
  162       color.setHsv( clippedValue, s, v, a );
 
  165       color.setHsv( h, clippedValue, v, a );
 
  168       color.setHsv( h, s, clippedValue, a );
 
  171       color.setAlpha( clippedValue );
 
  180   static QPixmap sTranspBkgrd;
 
  182   if ( sTranspBkgrd.isNull() )
 
  194   if ( mimeColor.isValid() )
 
  197     e->acceptProposedAction();
 
  204   bool hasAlpha = 
false;
 
  207   if ( mimeColor.isValid() )
 
  210     e->acceptProposedAction();
 
  269   valueClipped = std::max( valueClipped, 0 );
 
  281       if ( r == valueClipped )
 
  288       if ( g == valueClipped )
 
  295       if ( b == valueClipped )
 
  302       if ( h == valueClipped )
 
  309       if ( s == valueClipped )
 
  316       if ( v == valueClipped )
 
  323       if ( a == valueClipped )
 
  352   if ( 
color.hue() >= 0 )
 
  374   QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
 
  376   QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
 
  377   for ( 
int pos = 0; pos <= wheelStops; ++pos )
 
  379     double relativePos = 
static_cast<double>( pos ) / wheelStops;
 
  380     gradColor.setHsvF( relativePos, 1, 1 );
 
  381     wheelGradient.setColorAt( relativePos, gradColor );
 
  383   mWheelBrush = QBrush( wheelGradient );
 
  389   delete mTriangleImage;
 
  396   return QSize( size, size );
 
  402   QPainter painter( 
this );
 
  404   if ( !mWidgetImage || !mWheelImage || !mTriangleImage )
 
  406     createImages( size() );
 
  410   mWidgetImage->fill( Qt::transparent );
 
  411   QPainter imagePainter( mWidgetImage );
 
  412   imagePainter.setRenderHint( QPainter::Antialiasing );
 
  421   QPointF center = QPointF( width() / 2.0, height() / 2.0 );
 
  422   imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage );
 
  426   double length = mWheelImage->width() / 2.0;
 
  427   QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
 
  428   hueMarkerLine.setAngle( h );
 
  431   imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
 
  435   pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
 
  436   imagePainter.setPen( pen );
 
  437   imagePainter.drawLine( hueMarkerLine );
 
  438   imagePainter.restore();
 
  441   if ( mTriangleDirty )
 
  445   imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage );
 
  448   double triangleRadius = length - mWheelThickness - 1;
 
  452   double hueRadians = ( h * M_PI / 180.0 );
 
  453   double hx = std::cos( hueRadians ) * triangleRadius;
 
  454   double hy = -std::sin( hueRadians ) * triangleRadius;
 
  455   double sx = -std::cos( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  456   double sy = -std::sin( -hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  457   double vx = -std::cos( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  458   double vy = std::sin( hueRadians + ( M_PI / 3.0 ) ) * triangleRadius;
 
  459   double mx = ( sx + vx ) / 2.0;
 
  460   double  my = ( sy + vy ) / 2.0;
 
  462   double a = ( 1 - 2.0 * std::fabs( lightness - 0.5 ) ) * 
mCurrentColor.hslSaturationF();
 
  463   double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
 
  464   double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
 
  467   pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
 
  468   imagePainter.setPen( pen );
 
  469   imagePainter.setBrush( Qt::NoBrush );
 
  470   imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0, 4.0 );
 
  474   painter.drawImage( QPoint( 0, 0 ), *mWidgetImage );
 
  483     mTriangleDirty = 
true;
 
  489 void QgsColorWheel::createImages( 
const QSizeF size )
 
  491   double wheelSize = std::min( size.width(), size.height() ) - mMargin * 2.0;
 
  492   mWheelThickness = wheelSize / 15.0;
 
  496   mWheelImage = 
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
 
  497   delete mTriangleImage;
 
  498   mTriangleImage = 
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
 
  500   mWidgetImage = 
new QImage( size.width(), size.height(), QImage::Format_ARGB32 );
 
  504   mTriangleDirty = 
true;
 
  510   createImages( event->size() );
 
  511   QgsColorWidget::resizeEvent( event );
 
  514 void QgsColorWheel::setColorFromPos( 
const QPointF pos )
 
  516   QPointF center = QPointF( width() / 2.0, height() / 2.0 );
 
  518   QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
 
  520   QColor newColor = QColor();
 
  527   if ( mClickedPart == QgsColorWheel::Triangle )
 
  532     double x = pos.x() - center.x();
 
  533     double y = pos.y() - center.y();
 
  535     double eventAngleRadians = line.angle() * M_PI / 180.0;
 
  536     double hueRadians = h * M_PI / 180.0;
 
  537     double rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
 
  538     double rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
 
  539     double length = mWheelImage->width() / 2.0;
 
  540     double triangleLength = length - mWheelThickness - 1;
 
  542     double a = 0.5 * triangleLength;
 
  543     double b = std::tan( rad1 ) * a;
 
  544     double r = std::sqrt( x * x + y * y );
 
  545     double maxR = std::sqrt( a * a + b * b );
 
  549       double dx = std::tan( rad1 ) * r;
 
  550       double rad2 = std::atan( dx / maxR );
 
  551       rad2 = std::min( rad2, M_PI / 3.0 );
 
  552       rad2 = std::max( rad2, -M_PI / 3.0 );
 
  553       eventAngleRadians += rad2 - rad1;
 
  554       rad0 = std::fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
 
  555       rad1 = std::fmod( rad0, ( ( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
 
  556       b = std::tan( rad1 ) * a;
 
  557       r = std::sqrt( a * a + b * b );
 
  560     double triangleSideLength = std::sqrt( 3.0 ) * triangleLength;
 
  561     double newL = ( ( -std::sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
 
  562     double widthShare = 1.0 - ( std::fabs( newL - 0.5 ) * 2.0 );
 
  563     double newS = ( ( ( std::cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
 
  564     s = std::min( 
static_cast< int >( std::round( std::max( 0.0, newS ) * 255.0 ) ), 255 );
 
  565     l = std::min( 
static_cast< int >( std::round( std::max( 0.0, newL ) * 255.0 ) ), 255 );
 
  566     newColor = QColor::fromHsl( h, s, l );
 
  568     newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
 
  570   else if ( mClickedPart == QgsColorWheel::Wheel )
 
  575     int newHue = line.angle();
 
  576     newColor = QColor::fromHsv( newHue, s, v, alpha );
 
  578     mTriangleDirty = 
true;
 
  599   setColorFromPos( event->pos() );
 
  608   QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(), event->pos().y() );
 
  610   double innerLength = mWheelImage->width() / 2.0 - mWheelThickness;
 
  611   if ( line.length() < innerLength )
 
  613     mClickedPart = QgsColorWheel::Triangle;
 
  617     mClickedPart = QgsColorWheel::Wheel;
 
  619   setColorFromPos( event->pos() );
 
  625   mClickedPart = QgsColorWheel::None;
 
  628 void QgsColorWheel::createWheel()
 
  635   int maxSize = std::min( mWheelImage->width(),  mWheelImage->height() );
 
  636   double wheelRadius = maxSize / 2.0;
 
  638   mWheelImage->fill( Qt::transparent );
 
  639   QPainter p( mWheelImage );
 
  640   p.setRenderHint( QPainter::Antialiasing );
 
  641   p.setBrush( mWheelBrush );
 
  642   p.setPen( Qt::NoPen );
 
  645   p.translate( wheelRadius, wheelRadius );
 
  646   p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
 
  649   p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
 
  650   p.setBrush( QBrush( Qt::black ) );
 
  651   p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius - mWheelThickness, wheelRadius  - mWheelThickness );
 
  657 void QgsColorWheel::createTriangle()
 
  659   if ( !mWheelImage || !mTriangleImage )
 
  664   QPointF center = QPointF( mWheelImage->width() / 2.0, mWheelImage->height() / 2.0 );
 
  665   mTriangleImage->fill( Qt::transparent );
 
  667   QPainter imagePainter( mTriangleImage );
 
  668   imagePainter.setRenderHint( QPainter::Antialiasing );
 
  671   double wheelRadius = mWheelImage->width() / 2.0;
 
  672   double triangleRadius = wheelRadius - mWheelThickness - 1;
 
  675   QColor pureColor = QColor::fromHsv( 
angle, 255, 255 );
 
  677   QColor alphaColor = QColor( pureColor );
 
  678   alphaColor.setAlpha( 0 );
 
  681   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 ) );
 
  682   QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
 
  683   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 ) );
 
  684   QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * std::cos( M_PI / 3.0 ), center.y() );
 
  685   QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
 
  686   line1.setAngle( line1.angle() + 
angle );
 
  687   line2.setAngle( line2.angle() + 
angle );
 
  688   line3.setAngle( line3.angle() + 
angle );
 
  689   line4.setAngle( line4.angle() + 
angle );
 
  690   line5.setAngle( line5.angle() + 
angle );
 
  691   QPointF p1 = line1.p2();
 
  692   QPointF p2 = line2.p2();
 
  693   QPointF p3 = line3.p2();
 
  694   QPointF p4 = line4.p2();
 
  695   QPointF p5 = line5.p2();
 
  698   QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
 
  699   colorGrad.setColorAt( 0, alphaColor );
 
  700   colorGrad.setColorAt( 1, pureColor );
 
  701   QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
 
  702   whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
 
  703   whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
 
  706   triangle << p2 << p1 << p3 << p2;
 
  707   imagePainter.setPen( Qt::NoPen );
 
  709   imagePainter.setBrush( QBrush( Qt::black ) );
 
  710   imagePainter.drawPolygon( triangle );
 
  712   imagePainter.setBrush( QBrush( colorGrad ) );
 
  713   imagePainter.drawPolygon( triangle );
 
  715   imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
 
  716   imagePainter.setBrush( QBrush( whiteGrad ) );
 
  717   imagePainter.drawPolygon( triangle );
 
  722   imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
 
  723   imagePainter.setBrush( Qt::NoBrush );
 
  724   imagePainter.setPen( QPen( Qt::transparent ) );
 
  725   imagePainter.drawPolygon( triangle );
 
  728   mTriangleDirty = 
false;
 
  740   setFocusPolicy( Qt::StrongFocus );
 
  741   setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
 
  743   mBoxImage = 
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
 
  754   return QSize( size, size );
 
  760   QPainter painter( 
this );
 
  762   QStyleOptionFrame option;
 
  763   option.initFrom( 
this );
 
  764   option.state = hasFocus() ? QStyle::State_Active :  QStyle::State_None;
 
  765   style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
 
  773   painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
 
  776   double xPos = mMargin + ( width() - 2 * mMargin - 1 ) * 
static_cast<double>( xComponentValue() ) / 
static_cast<double>( valueRangeX() );
 
  777   double yPos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) * 
static_cast<double>( yComponentValue() ) / 
static_cast<double>( valueRangeY() );
 
  779   painter.setBrush( Qt::white );
 
  780   painter.setPen( Qt::NoPen );
 
  782   painter.drawRect( xPos - 1, mMargin, 3, height() - 2 * mMargin - 1 );
 
  783   painter.drawRect( mMargin, yPos - 1, width() - 2 * mMargin - 1, 3 );
 
  784   painter.setPen( Qt::black );
 
  785   painter.drawLine( xPos, mMargin, xPos, height() - mMargin - 1 );
 
  786   painter.drawLine( mMargin, yPos, width() - mMargin - 1, yPos );
 
  835   mBoxImage = 
new QImage( event->size().width() - mMargin * 2, event->size().height() - mMargin * 2, QImage::Format_RGB32 );
 
  836   QgsColorWidget::resizeEvent( event );
 
  841   setColorFromPoint( event->pos() );
 
  847   setColorFromPoint( event->pos() );
 
  850 void QgsColorBox::createBox()
 
  852   int maxValueX = mBoxImage->width();
 
  853   int maxValueY = mBoxImage->height();
 
  857   int colorComponentValue;
 
  859   for ( 
int y = 0; y < maxValueY; ++y )
 
  861     QRgb *scanLine = ( QRgb * )mBoxImage->scanLine( y );
 
  863     colorComponentValue = int( valueRangeY() - valueRangeY() * ( 
double( y ) / maxValueY ) );
 
  864     alterColor( currentColor, yComponent(), colorComponentValue );
 
  865     for ( 
int x = 0; x < maxValueX; ++x )
 
  867       colorComponentValue = int( valueRangeX() * ( 
double( x ) / maxValueX ) );
 
  868       alterColor( currentColor, xComponent(), colorComponentValue );
 
  869       scanLine[x] = currentColor.rgb();
 
  875 int QgsColorBox::valueRangeX()
 const 
  880 int QgsColorBox::valueRangeY()
 const 
  905 int QgsColorBox::yComponentValue()
 const 
  930 int QgsColorBox::xComponentValue()
 const 
  935 void QgsColorBox::setColorFromPoint( QPoint point )
 
  937   int valX = valueRangeX() * ( point.x() - mMargin ) / ( width() - 2 * mMargin - 1 );
 
  938   valX = std::min( std::max( valX, 0 ), valueRangeX() );
 
  940   int valY = valueRangeY() - valueRangeY() * ( point.y() - mMargin ) / ( height() - 2 * mMargin - 1 );
 
  941   valY = std::min( std::max( valY, 0 ), valueRangeY() );
 
  952   if ( 
color.hue() >= 0 )
 
  972   setFocusPolicy( Qt::StrongFocus );
 
  996   QPainter painter( 
this );
 
 1001     QStyleOptionFrame option;
 
 1002     option.initFrom( 
this );
 
 1003     option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
 
 1004     style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
 
 1010     QStyleOptionFocusRect option;
 
 1011     option.initFrom( 
this );
 
 1012     option.state = QStyle::State_KeyboardFocusChange;
 
 1013     style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
 
 1020     color.setAlpha( 255 );
 
 1027     painter.setPen( pen );
 
 1028     painter.setBrush( Qt::NoBrush );
 
 1031     for ( 
int c = 0; 
c <= maxValue; ++
c )
 
 1033       int colorVal = 
static_cast<int>( 
componentRange() * 
static_cast<double>( 
c ) / maxValue );
 
 1040       if ( 
color.hue() < 0 )
 
 1044       pen.setColor( 
color );
 
 1045       painter.setPen( pen );
 
 1049         painter.drawLine( QLineF( 
c + mMargin, mMargin, 
c + mMargin, height() - mMargin - 1 ) );
 
 1054         painter.drawLine( QLineF( mMargin, 
c + mMargin, width() - mMargin - 1, 
c + mMargin ) );
 
 1063     painter.setBrush( checkBrush );
 
 1064     painter.setPen( Qt::NoPen );
 
 1065     painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
 
 1066     QLinearGradient colorGrad;
 
 1070       colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 );
 
 1075       colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 );
 
 1078     transparent.setAlpha( 0 );
 
 1079     colorGrad.setColorAt( 0, transparent );
 
 1081     opaque.setAlpha( 255 );
 
 1082     colorGrad.setColorAt( 1, opaque );
 
 1083     QBrush colorBrush = QBrush( colorGrad );
 
 1084     painter.setBrush( colorBrush );
 
 1085     painter.drawRect( QRectF( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ) );
 
 1091     painter.setRenderHint( QPainter::Antialiasing );
 
 1092     painter.setBrush( QBrush( Qt::black ) );
 
 1093     painter.setPen( Qt::NoPen );
 
 1095     painter.drawPolygon( mTopTriangle );
 
 1096     painter.translate( 0, height() - mMargin - 2 );
 
 1097     painter.setBrush( QBrush( Qt::white ) );
 
 1098     painter.drawPolygon( mBottomTriangle );
 
 1104     double ypos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) * 
static_cast<double>( 
componentValue() ) / 
componentRange();
 
 1105     painter.setBrush( Qt::white );
 
 1106     painter.setPen( Qt::NoPen );
 
 1107     painter.drawRect( QRectF( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 ) );
 
 1108     painter.setPen( Qt::black );
 
 1109     painter.drawLine( QLineF( mMargin, ypos, width() - mMargin - 1, ypos ) );
 
 1119     setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
 
 1124     setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
 
 1131   if ( margin == mMargin )
 
 1152   mTopTriangle << QPoint( -markerSize, 0 ) <<  QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
 
 1153   mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
 
 1159   setColorFromPoint( event->pos() );
 
 1167   if ( event->delta() > 0 )
 
 1188   setColorFromPoint( event->pos() );
 
 1227     QgsColorWidget::keyPressEvent( event );
 
 1239 void QgsColorRampWidget::setColorFromPoint( QPointF point )
 
 1245     val = 
componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin );
 
 1270   QHBoxLayout *hLayout = 
new QHBoxLayout();
 
 1271   hLayout->setContentsMargins( 0, 0, 0, 0 );
 
 1272   hLayout->setSpacing( 5 );
 
 1276   hLayout->addWidget( mRampWidget, 1 );
 
 1278   mSpinBox = 
new QSpinBox();
 
 1280   int largestCharWidth = mSpinBox->fontMetrics().horizontalAdvance( QStringLiteral( 
"888%" ) );
 
 1281   mSpinBox->setMinimumWidth( largestCharWidth + 35 );
 
 1282   mSpinBox->setMinimum( 0 );
 
 1283   mSpinBox->setMaximum( convertRealToDisplay( 
componentRange() ) );
 
 1288     mSpinBox->setSuffix( QChar( 176 ) );
 
 1292     mSpinBox->setSuffix( tr( 
"%" ) );
 
 1294   hLayout->addWidget( mSpinBox );
 
 1295   setLayout( hLayout );
 
 1299   connect( mSpinBox, 
static_cast < void ( QSpinBox::* )( 
int ) 
> ( &QSpinBox::valueChanged ), 
this, &QgsColorSliderWidget::spinChanged );
 
 1306   mSpinBox->setMaximum( convertRealToDisplay( 
componentRange() ) );
 
 1310     mSpinBox->setSuffix( QChar( 176 ) );
 
 1315     mSpinBox->setSuffix( tr( 
"%" ) );
 
 1320     mSpinBox->setSuffix( QString() );
 
 1327   mRampWidget->blockSignals( 
true );
 
 1329   mRampWidget->blockSignals( 
false );
 
 1330   mSpinBox->blockSignals( 
true );
 
 1331   mSpinBox->setValue( convertRealToDisplay( value ) );
 
 1332   mSpinBox->blockSignals( 
false );
 
 1339   mSpinBox->blockSignals( 
true );
 
 1341   mSpinBox->blockSignals( 
false );
 
 1344 void QgsColorSliderWidget::rampColorChanged( 
const QColor &color )
 
 1349 void QgsColorSliderWidget::spinChanged( 
int value )
 
 1351   int convertedValue = convertDisplayToReal( value );
 
 1357 void QgsColorSliderWidget::rampChanged( 
int value )
 
 1359   mSpinBox->blockSignals( 
true );
 
 1360   mSpinBox->setValue( convertRealToDisplay( value ) );
 
 1361   mSpinBox->blockSignals( 
false );
 
 1365 int QgsColorSliderWidget::convertRealToDisplay( 
const int realValue )
 const 
 1371     return std::round( 100.0 * realValue / 255.0 );
 
 1378 int QgsColorSliderWidget::convertDisplayToReal( 
const int displayValue )
 const 
 1383     return std::round( 255.0 * displayValue / 100.0 );
 
 1387   return displayValue;
 
 1397   QHBoxLayout *hLayout = 
new QHBoxLayout();
 
 1398   hLayout->setContentsMargins( 0, 0, 0, 0 );
 
 1399   hLayout->setSpacing( 0 );
 
 1401   mLineEdit = 
new QLineEdit( 
nullptr );
 
 1402   hLayout->addWidget( mLineEdit );
 
 1404   mMenuButton = 
new QToolButton( mLineEdit );
 
 1406   mMenuButton->setCursor( Qt::ArrowCursor );
 
 1407   mMenuButton->setFocusPolicy( Qt::NoFocus );
 
 1408   mMenuButton->setStyleSheet( QStringLiteral( 
"QToolButton { border: none; padding: 0px; }" ) );
 
 1410   setLayout( hLayout );
 
 1412   int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
 
 1413   mLineEdit->setStyleSheet( QStringLiteral( 
"QLineEdit { padding-right: %1px; } " )
 
 1414                             .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
 
 1416   connect( mLineEdit, &QLineEdit::editingFinished, 
this, &QgsColorTextWidget::textChanged );
 
 1417   connect( mMenuButton, &QAbstractButton::clicked, 
this, &QgsColorTextWidget::showMenu );
 
 1420   QgsSettings settings;
 
 1421   mFormat = settings.enumValue( QStringLiteral( 
"ColorWidgets/textWidgetFormat" ), 
HexRgb );
 
 1435   QSize sz = mMenuButton->sizeHint();
 
 1436   int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
 
 1437   mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
 
 1438                      ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
 
 1441 void QgsColorTextWidget::updateText()
 
 1460 void QgsColorTextWidget::textChanged()
 
 1462   QString testString = mLineEdit->text();
 
 1465   if ( !
color.isValid() )
 
 1476     if ( !containsAlpha )
 
 1487 void QgsColorTextWidget::showMenu()
 
 1489   QMenu colorContextMenu;
 
 1491   QAction *hexRgbAction = 
new QAction( tr( 
"#RRGGBB" ), 
nullptr );
 
 1492   colorContextMenu.addAction( hexRgbAction );
 
 1493   QAction *hexRgbaAction = 
new QAction( tr( 
"#RRGGBBAA" ), 
nullptr );
 
 1494   colorContextMenu.addAction( hexRgbaAction );
 
 1495   QAction *rgbAction = 
new QAction( tr( 
"rgb( r, g, b )" ), 
nullptr );
 
 1496   colorContextMenu.addAction( rgbAction );
 
 1497   QAction *rgbaAction = 
new QAction( tr( 
"rgba( r, g, b, a )" ), 
nullptr );
 
 1498   colorContextMenu.addAction( rgbaAction );
 
 1500   QAction *selectedAction = colorContextMenu.exec( QCursor::pos() );
 
 1501   if ( selectedAction == hexRgbAction )
 
 1505   else if ( selectedAction == hexRgbaAction )
 
 1509   else if ( selectedAction == rgbAction )
 
 1513   else if ( selectedAction == rgbaAction )
 
 1519   QgsSettings settings;
 
 1520   settings.setEnumValue( QStringLiteral( 
"ColorWidgets/textWidgetFormat" ), mFormat );
 
 1532   , mColor2( QColor() )
 
 1537 void QgsColorPreviewWidget::drawColor( 
const QColor &color, QRect rect, QPainter &painter )
 
 1539   painter.setPen( Qt::NoPen );
 
 1541   if ( 
color.alpha() < 255 )
 
 1544     painter.setBrush( checkBrush );
 
 1545     painter.drawRect( rect );
 
 1550     QBrush colorBrush = QBrush( 
color );
 
 1551     painter.setBrush( colorBrush );
 
 1552     painter.drawRect( std::floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - std::floor( rect.width() / 2.0 ), rect.height() );
 
 1554     QColor opaqueColor = QColor( 
color );
 
 1555     opaqueColor.setAlpha( 255 );
 
 1556     QBrush opaqueBrush = QBrush( opaqueColor );
 
 1557     painter.setBrush( opaqueBrush );
 
 1558     painter.drawRect( rect.left(), rect.top(), std::ceil( rect.width() / 2.0 ), rect.height() );
 
 1563     QBrush brush = QBrush( 
color );
 
 1564     painter.setBrush( brush );
 
 1565     painter.drawRect( rect );
 
 1572   QPainter painter( 
this );
 
 1574   if ( mColor2.isValid() )
 
 1577     int verticalSplit = std::round( height() / 2.0 );
 
 1578     drawColor( 
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
 
 1579     drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
 
 1583     drawColor( 
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
 
 1596   if ( 
color == mColor2 )
 
 1606   if ( e->button() == Qt::LeftButton )
 
 1608     mDragStartPosition = e->pos();
 
 1615   if ( ( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
 
 1624   if ( mColor2.isValid() )
 
 1627     int verticalSplit = std::round( height() / 2.0 );
 
 1628     if ( mDragStartPosition.y() >= verticalSplit )
 
 1630       clickedColor = mColor2;
 
 1641   if ( !( e->buttons() & Qt::LeftButton ) )
 
 1648   if ( ( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
 
 1659   if ( mColor2.isValid() )
 
 1662     int verticalSplit = std::round( height() / 2.0 );
 
 1663     if ( mDragStartPosition.y() >= verticalSplit )
 
 1665       dragColor = mColor2;
 
 1669   QDrag *drag = 
new QDrag( 
this );
 
 1672   drag->exec( Qt::CopyAction );
 
 1681   : QWidgetAction( parent )
 
 1683   , mColorWidget( colorWidget )
 
 1684   , mSuppressRecurse( false )
 
 1685   , mDismissOnColorSelection( true )
 
 1687   setDefaultWidget( mColorWidget );
 
 1690   connect( 
this, &QAction::hovered, 
this, &QgsColorWidgetAction::onHover );
 
 1694 void QgsColorWidgetAction::onHover()
 
 1697   if ( mSuppressRecurse )
 
 1704     mSuppressRecurse = 
true;
 
 1705     mMenu->setActiveAction( 
this );
 
 1706     mSuppressRecurse = 
false;
 
 1710 void QgsColorWidgetAction::setColor( 
const QColor &color )
 
 1713   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
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