19 #include <QResizeEvent>
20 #include <QStyleOptionFrameV3>
22 #include <QHBoxLayout>
25 #include <QFontMetrics>
26 #include <QToolButton>
39 , mCurrentColor( Qt::red )
40 , mComponent( component )
43 setAcceptDrops(
true );
59 QPixmap pixmap( 50, 50 );
60 pixmap.fill( Qt::transparent );
62 painter.begin( &pixmap );
64 painter.fillRect( QRect( 0, 0, 50, 50 ), QBrush( QColor( 200, 200, 200 ) ) );
66 QColor pixmapColor =
color;
67 pixmapColor.setAlpha( 255 );
68 painter.setBrush( QBrush( pixmapColor ) );
69 painter.setPen( QPen( Qt::white ) );
70 painter.drawRect( QRect( 1, 1, 47, 47 ) );
144 color.getHsv( &h, &s, &v, &a );
147 int clippedValue = qMin( qMax( 0, newValue ),
componentRange( component ) );
152 color.setRed( clippedValue );
155 color.setGreen( clippedValue );
158 color.setBlue( clippedValue );
161 color.setHsv( clippedValue, s, v, a );
164 color.setHsv( h, clippedValue, v, a );
167 color.setHsv( h, s, clippedValue, a );
170 color.setAlpha( clippedValue );
179 static QPixmap transpBkgrd;
181 if ( transpBkgrd.isNull() )
193 if ( mimeColor.isValid() )
196 e->acceptProposedAction();
203 bool hasAlpha =
false;
206 if ( mimeColor.isValid() )
209 e->acceptProposedAction();
249 valueClipped = qMax( valueClipped, 0 );
261 if ( r == valueClipped )
268 if ( g == valueClipped )
275 if ( b == valueClipped )
282 if ( h == valueClipped )
289 if ( s == valueClipped )
296 if ( v == valueClipped )
303 if ( a == valueClipped )
332 if ( color.hue() >= 0 )
353 , mWheelThickness( 18 )
356 , mTriangleImage( 0 )
358 , mWheelDirty( true )
359 , mTriangleDirty( true )
362 QConicalGradient wheelGradient = QConicalGradient( 0, 0, 0 );
364 QColor gradColor = QColor::fromHsvF( 1.0, 1.0, 1.0 );
365 for (
int pos = 0; pos <= wheelStops; ++pos )
367 double relativePos = ( double )pos / wheelStops;
368 gradColor.setHsvF( relativePos, 1, 1 );
369 wheelGradient.setColorAt( relativePos, gradColor );
371 mWheelBrush = QBrush( wheelGradient );
377 delete mTriangleImage;
384 QPainter painter(
this );
387 QStyleOptionFrameV3 option = QStyleOptionFrameV3();
388 option.initFrom(
this );
389 option.state = this->hasFocus() ? QStyle::State_Active : QStyle::State_None;
390 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
392 if ( !mWidgetImage || !mWheelImage || !mTriangleImage )
394 createImages(
size() );
398 mWidgetImage->fill( Qt::transparent );
399 QPainter imagePainter( mWidgetImage );
400 imagePainter.setRenderHint( QPainter::Antialiasing );
409 QPointF center = QPointF( width() / 2.0, height() / 2.0 );
410 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mWheelImage );
414 double length = mWheelImage->width() / 2.0;
415 QLineF hueMarkerLine = QLineF( center.x(), center.y(), center.x() + length, center.y() );
416 hueMarkerLine.setAngle( h );
419 imagePainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
423 pen.setColor( h > 20 && h < 200 ? Qt::black : Qt::white );
424 imagePainter.setPen( pen );
425 imagePainter.drawLine( hueMarkerLine );
426 imagePainter.restore();
429 if ( mTriangleDirty )
433 imagePainter.drawImage( QPointF( center.x() - ( mWheelImage->width() / 2.0 ), center.y() - ( mWheelImage->height() / 2.0 ) ), *mTriangleImage );
436 double triangleRadius = length - mWheelThickness - 1;
440 double hueRadians = ( h *
M_PI / 180.0 );
441 double hx = cos( hueRadians ) * triangleRadius;
442 double hy = -sin( hueRadians ) * triangleRadius;
443 double sx = -cos( -hueRadians + (
M_PI / 3.0 ) ) * triangleRadius;
444 double sy = -sin( -hueRadians + (
M_PI / 3.0 ) ) * triangleRadius;
445 double vx = -cos( hueRadians + (
M_PI / 3.0 ) ) * triangleRadius;
446 double vy = sin( hueRadians + (
M_PI / 3.0 ) ) * triangleRadius;
447 double mx = ( sx + vx ) / 2.0;
448 double my = ( sy + vy ) / 2.0;
450 double a = ( 1 - 2.0 * fabs( lightness - 0.5 ) ) *
mCurrentColor.hslSaturationF();
451 double x = sx + ( vx - sx ) * lightness + ( hx - mx ) * a;
452 double y = sy + ( vy - sy ) * lightness + ( hy - my ) * a;
455 pen.setColor( lightness > 0.7 ? Qt::black : Qt::white );
456 imagePainter.setPen( pen );
457 imagePainter.setBrush( Qt::NoBrush );
458 imagePainter.drawEllipse( QPointF( x + center.x(), y + center.y() ), 4.0, 4.0 );
462 painter.drawImage( QPoint( 0, 0 ), *mWidgetImage );
468 if ( color.hue() >= 0 && color.hue() !=
hue() )
471 mTriangleDirty =
true;
477 void QgsColorWheel::createImages(
const QSizeF
size )
479 double wheelSize = qMin( size.width(), size.height() ) - mMargin * 2.0;
480 mWheelThickness = wheelSize / 15.0;
484 mWheelImage =
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
485 delete mTriangleImage;
486 mTriangleImage =
new QImage( wheelSize, wheelSize, QImage::Format_ARGB32 );
488 mWidgetImage =
new QImage( size.width(), size.height(), QImage::Format_ARGB32 );
492 mTriangleDirty =
true;
498 createImages( event->size() );
502 void QgsColorWheel::setColorFromPos(
const QPointF pos )
504 QPointF center = QPointF( width() / 2.0, height() / 2.0 );
506 QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );
508 QColor newColor = QColor();
515 if ( mClickedPart == QgsColorWheel::Triangle )
520 double x = pos.x() - center.x();
521 double y = pos.y() - center.y();
523 double eventAngleRadians = line.angle() *
M_PI / 180.0;
524 double hueRadians = h *
M_PI / 180.0;
525 double rad0 = fmod( eventAngleRadians + 2.0 *
M_PI - hueRadians, 2.0 *
M_PI );
526 double rad1 = fmod( rad0, (( 2.0 / 3.0 ) *
M_PI ) ) - (
M_PI / 3.0 );
527 double length = mWheelImage->width() / 2.0;
528 double triangleLength = length - mWheelThickness - 1;
530 double a = 0.5 * triangleLength;
531 double b = tan( rad1 ) * a;
532 double r = sqrt( x * x + y * y );
533 double maxR = sqrt( a * a + b * b );
537 double dx = tan( rad1 ) * r;
538 double rad2 = atan( dx / maxR );
539 rad2 = qMin( rad2,
M_PI / 3.0 );
540 rad2 = qMax( rad2, -
M_PI / 3.0 );
541 eventAngleRadians += rad2 - rad1;
542 rad0 = fmod( eventAngleRadians + 2.0 *
M_PI - hueRadians, 2.0 *
M_PI );
543 rad1 = fmod( rad0, (( 2.0 / 3.0 ) *
M_PI ) ) - (
M_PI / 3.0 );
545 r = sqrt( a * a + b * b );
548 double triangleSideLength = sqrt( 3.0 ) * triangleLength;
549 double newL = (( -sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
550 double widthShare = 1.0 - ( fabs( newL - 0.5 ) * 2.0 );
551 double newS = ((( cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
552 s = qMin( qRound( qMax( 0.0, newS ) * 255.0 ), 255 );
553 l = qMin( qRound( qMax( 0.0, newL ) * 255.0 ), 255 );
554 newColor = QColor::fromHsl( h, s, l );
556 newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
558 else if ( mClickedPart == QgsColorWheel::Wheel )
563 int newHue = line.angle();
564 newColor = QColor::fromHsv( newHue, s, v, alpha );
566 mTriangleDirty =
true;
587 setColorFromPos( event->posF() );
595 QLineF line = QLineF( width() / 2.0, height() / 2.0, event->pos().x(),
event->pos().y() );
597 double innerLength = mWheelImage->width() / 2.0 - mWheelThickness;
598 if ( line.length() < innerLength )
600 mClickedPart = QgsColorWheel::Triangle;
604 mClickedPart = QgsColorWheel::Wheel;
606 setColorFromPos( event->posF() );
612 mClickedPart = QgsColorWheel::None;
615 void QgsColorWheel::createWheel()
622 int maxSize = qMin( mWheelImage->width(), mWheelImage->height() );
623 double wheelRadius = maxSize / 2.0;
625 mWheelImage->fill( Qt::transparent );
626 QPainter p( mWheelImage );
627 p.setRenderHint( QPainter::Antialiasing );
628 p.setBrush( mWheelBrush );
629 p.setPen( Qt::NoPen );
632 p.translate( wheelRadius, wheelRadius );
633 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius, wheelRadius );
636 p.setCompositionMode( QPainter::CompositionMode_DestinationOut );
637 p.setBrush( QBrush( Qt::black ) );
638 p.drawEllipse( QPointF( 0.0, 0.0 ), wheelRadius - mWheelThickness, wheelRadius - mWheelThickness );
644 void QgsColorWheel::createTriangle()
646 if ( !mWheelImage || !mTriangleImage )
651 QPointF center = QPointF( mWheelImage->width() / 2.0, mWheelImage->height() / 2.0 );
652 mTriangleImage->fill( Qt::transparent );
654 QPainter imagePainter( mTriangleImage );
655 imagePainter.setRenderHint( QPainter::Antialiasing );
658 double wheelRadius = mWheelImage->width() / 2.0;
659 double triangleRadius = wheelRadius - mWheelThickness - 1;
662 QColor pureColor = QColor::fromHsv( angle, 255, 255 );
664 QColor alphaColor = QColor( pureColor );
665 alphaColor.setAlpha( 0 );
668 QLineF line1 = QLineF( center.x(), center.y(), center.x() - triangleRadius * cos(
M_PI / 3.0 ), center.y() - triangleRadius * sin(
M_PI / 3.0 ) );
669 QLineF line2 = QLineF( center.x(), center.y(), center.x() + triangleRadius, center.y() );
670 QLineF line3 = QLineF( center.x(), center.y(), center.x() - triangleRadius * cos(
M_PI / 3.0 ), center.y() + triangleRadius * sin(
M_PI / 3.0 ) );
671 QLineF line4 = QLineF( center.x(), center.y(), center.x() - triangleRadius * cos(
M_PI / 3.0 ), center.y() );
672 QLineF line5 = QLineF( center.x(), center.y(), ( line2.p2().x() + line1.p2().x() ) / 2.0, ( line2.p2().y() + line1.p2().y() ) / 2.0 );
673 line1.setAngle( line1.angle() +
angle );
674 line2.setAngle( line2.angle() +
angle );
675 line3.setAngle( line3.angle() +
angle );
676 line4.setAngle( line4.angle() +
angle );
677 line5.setAngle( line5.angle() +
angle );
678 QPointF p1 = line1.p2();
679 QPointF p2 = line2.p2();
680 QPointF p3 = line3.p2();
681 QPointF p4 = line4.p2();
682 QPointF p5 = line5.p2();
685 QLinearGradient colorGrad = QLinearGradient( p4.x(), p4.y(), p2.x(), p2.y() );
686 colorGrad.setColorAt( 0, alphaColor );
687 colorGrad.setColorAt( 1, pureColor );
688 QLinearGradient whiteGrad = QLinearGradient( p3.x(), p3.y(), p5.x(), p5.y() );
689 whiteGrad.setColorAt( 0, QColor( 255, 255, 255, 255 ) );
690 whiteGrad.setColorAt( 1, QColor( 255, 255, 255, 0 ) );
693 triangle << p2 << p1 << p3 << p2;
694 imagePainter.setPen( Qt::NoPen );
696 imagePainter.setBrush( QBrush( Qt::black ) );
697 imagePainter.drawPolygon( triangle );
699 imagePainter.setBrush( QBrush( colorGrad ) );
700 imagePainter.drawPolygon( triangle );
702 imagePainter.setCompositionMode( QPainter::CompositionMode_Plus );
703 imagePainter.setBrush( QBrush( whiteGrad ) );
704 imagePainter.drawPolygon( triangle );
709 imagePainter.setCompositionMode( QPainter::CompositionMode_Source );
710 imagePainter.setBrush( Qt::NoBrush );
711 imagePainter.setPen( QPen( Qt::transparent ) );
712 imagePainter.drawPolygon( triangle );
715 mTriangleDirty =
false;
730 setFocusPolicy( Qt::StrongFocus );
731 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
733 mBoxImage =
new QImage( width() - mMargin * 2, height() - mMargin * 2, QImage::Format_RGB32 );
743 return QSize( 200, 200 );
749 QPainter painter(
this );
751 QStyleOptionFrameV3 option;
752 option.initFrom(
this );
753 option.state = hasFocus() ? QStyle::State_Active : QStyle::State_None;
754 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
762 painter.drawImage( QPoint( mMargin, mMargin ), *mBoxImage );
765 double xPos = mMargin + ( width() - 2 * mMargin - 1 ) * (
double )xComponentValue() / ( double )valueRangeX();
766 double yPos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) * (
double )yComponentValue() / ( double )valueRangeY();
768 painter.setBrush( Qt::white );
769 painter.setPen( Qt::NoPen );
771 painter.drawRect( xPos - 1, mMargin, 3, height() - 2 * mMargin - 1 );
772 painter.drawRect( mMargin, yPos - 1, width() - 2 * mMargin - 1, 3 );
773 painter.setPen( Qt::black );
774 painter.drawLine( xPos, mMargin, xPos, height() - mMargin - 1 );
775 painter.drawLine( mMargin, yPos, width() - mMargin - 1, yPos );
824 mBoxImage =
new QImage( event->size().width() - mMargin * 2,
event->size().height() - mMargin * 2, QImage::Format_RGB32 );
830 setColorFromPoint( event->pos() );
835 setColorFromPoint( event->pos() );
838 void QgsColorBox::createBox()
840 int maxValueX = mBoxImage->width();
841 int maxValueY = mBoxImage->height();
845 int colorComponentValue;
847 for (
int y = 0; y < maxValueY; ++y )
849 QRgb* scanLine = ( QRgb* )mBoxImage->scanLine( y );
851 colorComponentValue = int( valueRangeY() - valueRangeY() * (
double( y ) / maxValueY ) );
852 alterColor( currentColor, yComponent(), colorComponentValue );
853 for (
int x = 0; x < maxValueX; ++x )
855 colorComponentValue = int( valueRangeX() * (
double( x ) / maxValueX ) );
856 alterColor( currentColor, xComponent(), colorComponentValue );
857 scanLine[x] = currentColor.rgb();
863 int QgsColorBox::valueRangeX()
const
868 int QgsColorBox::valueRangeY()
const
893 int QgsColorBox::yComponentValue()
const
918 int QgsColorBox::xComponentValue()
const
923 void QgsColorBox::setColorFromPoint(
const QPoint &point )
925 int valX = valueRangeX() * ( point.x() - mMargin ) / ( width() - 2 * mMargin - 1 );
926 valX = qMin( qMax( valX, 0 ), valueRangeX() );
928 int valY = valueRangeY() - valueRangeY() * ( point.y() - mMargin ) / ( height() - 2 * mMargin - 1 );
929 valY = qMin( qMax( valY, 0 ), valueRangeY() );
940 if ( color.hue() >= 0 )
960 , mShowFrame( false )
962 setFocusPolicy( Qt::StrongFocus );
979 return QSize( 200, 28 );
984 return QSize( 18, 200 );
991 QPainter painter(
this );
996 QStyleOptionFrameV3 option;
997 option.initFrom(
this );
998 option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
999 style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );
1005 QStyleOptionFocusRect option;
1006 option.initFrom(
this );
1007 option.state = QStyle::State_KeyboardFocusChange;
1008 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
1015 color.setAlpha( 255 );
1018 painter.setPen( pen );
1019 painter.setBrush( Qt::NoBrush );
1022 for (
int c = 0; c <= maxValue; ++c )
1031 if ( color.hue() < 0 )
1033 color.setHsv(
hue(), color.saturation(), color.value() );
1035 pen.setColor( color );
1036 painter.setPen( pen );
1040 painter.drawLine( c + mMargin, mMargin, c + mMargin, height() - mMargin - 1 );
1045 painter.drawLine( mMargin, c + mMargin, width() - mMargin - 1, c + mMargin );
1054 painter.setBrush( checkBrush );
1055 painter.setPen( Qt::NoPen );
1056 painter.drawRect( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 );
1057 QLinearGradient colorGrad;
1061 colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 );
1066 colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 );
1069 transparent.setAlpha( 0 );
1070 colorGrad.setColorAt( 0, transparent );
1072 opaque.setAlpha( 255 );
1073 colorGrad.setColorAt( 1, opaque );
1074 QBrush colorBrush = QBrush( colorGrad );
1075 painter.setBrush( colorBrush );
1076 painter.drawRect( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 );
1082 painter.setRenderHint( QPainter::Antialiasing );
1083 painter.setBrush( QBrush( Qt::black ) );
1084 painter.setPen( Qt::NoPen );
1086 painter.drawPolygon( mTopTriangle );
1087 painter.translate( 0, height() - mMargin - 2 );
1088 painter.setBrush( QBrush( Qt::white ) );
1089 painter.drawPolygon( mBottomTriangle );
1096 painter.setBrush( Qt::white );
1097 painter.setPen( Qt::NoPen );
1098 painter.drawRect( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 );
1099 painter.setPen( Qt::black );
1100 painter.drawLine( mMargin, ypos, width() - mMargin - 1, ypos );
1110 setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
1115 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::MinimumExpanding );
1122 if ( margin == mMargin )
1132 if ( showFrame == mShowFrame )
1143 mTopTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, markerSize );
1144 mBottomTriangle << QPoint( -markerSize, 0 ) << QPoint( markerSize, 0 ) << QPoint( 0, -markerSize );
1150 setColorFromPoint( event->posF() );
1155 setColorFromPoint( event->posF() );
1206 void QgsColorRampWidget::setColorFromPoint(
const QPointF &point )
1212 val =
componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin );
1238 QHBoxLayout* hLayout =
new QHBoxLayout();
1239 hLayout->setMargin( 0 );
1240 hLayout->setSpacing( 5 );
1244 hLayout->addWidget( mRampWidget, 1 );
1246 mSpinBox =
new QSpinBox();
1248 int largestCharWidth = mSpinBox->fontMetrics().width(
"888%" );
1249 mSpinBox->setMinimumWidth( largestCharWidth + 35 );
1250 mSpinBox->setMinimum( 0 );
1251 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1256 mSpinBox->setSuffix( QChar( 176 ) );
1260 mSpinBox->setSuffix(
tr(
"%" ) );
1262 hLayout->addWidget( mSpinBox );
1263 setLayout( hLayout );
1265 connect( mRampWidget, SIGNAL( valueChanged(
int ) ),
this, SLOT( rampChanged(
int ) ) );
1266 connect( mRampWidget, SIGNAL(
colorChanged(
const QColor ) ),
this, SLOT( rampColorChanged(
const QColor ) ) );
1267 connect( mSpinBox, SIGNAL( valueChanged(
int ) ),
this, SLOT( spinChanged(
int ) ) );
1278 mSpinBox->setMaximum( convertRealToDisplay(
componentRange() ) );
1282 mSpinBox->setSuffix( QChar( 176 ) );
1287 mSpinBox->setSuffix(
tr(
"%" ) );
1292 mSpinBox->setSuffix( QString() );
1299 mRampWidget->blockSignals(
true );
1301 mRampWidget->blockSignals(
false );
1302 mSpinBox->blockSignals(
true );
1303 mSpinBox->setValue( convertRealToDisplay( value ) );
1304 mSpinBox->blockSignals(
false );
1311 mSpinBox->blockSignals(
true );
1313 mSpinBox->blockSignals(
false );
1316 void QgsColorSliderWidget::rampColorChanged(
const QColor &color )
1321 void QgsColorSliderWidget::spinChanged(
int value )
1323 int convertedValue = convertDisplayToReal( value );
1329 void QgsColorSliderWidget::rampChanged(
int value )
1331 mSpinBox->blockSignals(
true );
1332 mSpinBox->setValue( convertRealToDisplay( value ) );
1333 mSpinBox->blockSignals(
false );
1337 int QgsColorSliderWidget::convertRealToDisplay(
const int realValue )
const
1343 return qRound( 100.0 * realValue / 255.0 );
1350 int QgsColorSliderWidget::convertDisplayToReal(
const int displayValue )
const
1355 return qRound( 255.0 * displayValue / 100.0 );
1359 return displayValue;
1372 QHBoxLayout* hLayout =
new QHBoxLayout();
1373 hLayout->setMargin( 0 );
1374 hLayout->setSpacing( 0 );
1376 mLineEdit =
new QLineEdit( 0 );
1377 hLayout->addWidget( mLineEdit );
1379 mMenuButton =
new QToolButton( mLineEdit );
1381 mMenuButton->setCursor( Qt::ArrowCursor );
1382 mMenuButton->setFocusPolicy( Qt::NoFocus );
1383 mMenuButton->setStyleSheet(
"QToolButton { border: none; padding: 0px; }" );
1385 setLayout( hLayout );
1387 int frameWidth = mLineEdit->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1388 mLineEdit->setStyleSheet( QString(
"QLineEdit { padding-right: %1px; } " )
1389 .arg( mMenuButton->sizeHint().width() + frameWidth + 1 ) );
1391 connect( mLineEdit, SIGNAL( editingFinished() ),
this, SLOT( textChanged() ) );
1392 connect( mMenuButton, SIGNAL( clicked() ),
this, SLOT( showMenu() ) );
1396 mFormat = ( ColorTextFormat )settings.value(
"/ColorWidgets/textWidgetFormat", 0 ).toInt();
1415 QSize sz = mMenuButton->sizeHint();
1416 int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
1417 mMenuButton->move( mLineEdit->rect().right() - frameWidth - sz.width(),
1418 ( mLineEdit->rect().bottom() + 1 - sz.height() ) / 2 );
1421 void QgsColorTextWidget::updateText()
1440 void QgsColorTextWidget::textChanged()
1442 QString testString = mLineEdit->text();
1445 if ( !color.isValid() )
1456 if ( !containsAlpha )
1467 void QgsColorTextWidget::showMenu()
1469 QMenu colorContextMenu;
1471 QAction* hexRgbAction =
new QAction(
tr(
"#RRGGBB" ), 0 );
1472 colorContextMenu.addAction( hexRgbAction );
1473 QAction* hexRgbaAction =
new QAction(
tr(
"#RRGGBBAA" ), 0 );
1474 colorContextMenu.addAction( hexRgbaAction );
1475 QAction* rgbAction =
new QAction(
tr(
"rgb( r, g, b )" ), 0 );
1476 colorContextMenu.addAction( rgbAction );
1477 QAction* rgbaAction =
new QAction(
tr(
"rgba( r, g, b, a )" ), 0 );
1478 colorContextMenu.addAction( rgbaAction );
1480 QAction* selectedAction = colorContextMenu.exec( QCursor::pos() );
1481 if ( selectedAction == hexRgbAction )
1483 mFormat = QgsColorTextWidget::HexRgb;
1485 else if ( selectedAction == hexRgbaAction )
1487 mFormat = QgsColorTextWidget::HexRgbA;
1489 else if ( selectedAction == rgbAction )
1491 mFormat = QgsColorTextWidget::Rgb;
1493 else if ( selectedAction == rgbaAction )
1495 mFormat = QgsColorTextWidget::Rgba;
1500 settings.setValue(
"/ColorWidgets/textWidgetFormat", (
int )mFormat );
1512 , mColor2( QColor() )
1522 void QgsColorPreviewWidget::drawColor(
const QColor &color,
const QRect &rect, QPainter& painter )
1524 painter.setPen( Qt::NoPen );
1526 if ( color.alpha() < 255 )
1529 painter.setBrush( checkBrush );
1530 painter.drawRect( rect );
1535 QBrush colorBrush = QBrush( color );
1536 painter.setBrush( colorBrush );
1537 painter.drawRect( floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - floor( rect.width() / 2.0 ), rect.height() );
1539 QColor opaqueColor = QColor( color );
1540 opaqueColor.setAlpha( 255 );
1541 QBrush opaqueBrush = QBrush( opaqueColor );
1542 painter.setBrush( opaqueBrush );
1543 painter.drawRect( rect.left(), rect.top(), ceil( rect.width() / 2.0 ), rect.height() );
1548 QBrush brush = QBrush( color );
1549 painter.setBrush( brush );
1550 painter.drawRect( rect );
1557 QPainter painter(
this );
1559 if ( mColor2.isValid() )
1562 int verticalSplit = qRound( height() / 2.0 );
1563 drawColor(
mCurrentColor, QRect( 0, 0, width(), verticalSplit ), painter );
1564 drawColor( mColor2, QRect( 0, verticalSplit, width(), height() - verticalSplit ), painter );
1568 drawColor(
mCurrentColor, QRect( 0, 0, width(), height() ), painter );
1576 if ( color == mColor2 )
1586 if ( e->button() == Qt::LeftButton )
1588 mDragStartPosition = e->pos();
1595 if (( e->pos() - mDragStartPosition ).manhattanLength() >= QApplication::startDragDistance() )
1604 if ( mColor2.isValid() )
1607 int verticalSplit = qRound( height() / 2.0 );
1608 if ( mDragStartPosition.y() >= verticalSplit )
1610 clickedColor = mColor2;
1621 if ( !( e->buttons() & Qt::LeftButton ) )
1628 if (( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
1639 if ( mColor2.isValid() )
1642 int verticalSplit = qRound( height() / 2.0 );
1643 if ( mDragStartPosition.y() >= verticalSplit )
1645 dragColor = mColor2;
1649 QDrag *drag =
new QDrag(
this );
1652 drag->exec( Qt::CopyAction );