void QgsColorWidget::alterColor( QColor& color, const QgsColorWidget::ColorComponent component, const int newValue ) const { int h, s, v, a; color.getHsv( &h, &s, &v, &a ); //clip value to sensible range int clippedValue = qMin( qMax( 0, newValue ), componentRange( component ) ); switch ( component ) { case QgsColorWidget::Red: color.setRed( clippedValue ); return; case QgsColorWidget::Green: color.setGreen( clippedValue ); return; case QgsColorWidget::Blue: color.setBlue( clippedValue ); return; case QgsColorWidget::Hue: color.setHsv( clippedValue, s, v, a ); return; case QgsColorWidget::Saturation: color.setHsv( h, clippedValue, v, a ); return; case QgsColorWidget::Value: color.setHsv( h, s, clippedValue, a ); return; case QgsColorWidget::Alpha: color.setAlpha( clippedValue ); return; default: return; } }
// Accumulate locations used for inputs, outputs, and uniforms, and check for collisions // as the accumulation is done. // // Returns < 0 if no collision, >= 0 if collision and the value returned is a colliding value. // // typeCollision is set to true if there is no direct collision, but the types in the same location // are different. // int TIntermediate::addUsedLocation(const TQualifier& qualifier, const TType& type, bool& typeCollision) { typeCollision = false; int set; if (qualifier.isPipeInput()) set = 0; else if (qualifier.isPipeOutput()) set = 1; else if (qualifier.storage == EvqUniform) set = 2; else if (qualifier.storage == EvqBuffer) set = 3; else return -1; int size; if (qualifier.isUniformOrBuffer()) { if (type.isArray()) size = type.getCumulativeArraySize(); else size = 1; } else { // Strip off the outer array dimension for those having an extra one. if (type.isArray() && qualifier.isArrayedIo(language)) { TType elementType(type, 0); size = computeTypeLocationSize(elementType); } else size = computeTypeLocationSize(type); } TRange locationRange(qualifier.layoutLocation, qualifier.layoutLocation + size - 1); TRange componentRange(0, 3); if (qualifier.hasComponent()) { componentRange.start = qualifier.layoutComponent; componentRange.last = componentRange.start + type.getVectorSize() - 1; } TIoRange range(locationRange, componentRange, type.getBasicType(), qualifier.hasIndex() ? qualifier.layoutIndex : 0); // check for collisions, except for vertex inputs on desktop if (! (profile != EEsProfile && language == EShLangVertex && qualifier.isPipeInput())) { for (size_t r = 0; r < usedIo[set].size(); ++r) { if (range.overlap(usedIo[set][r])) { // there is a collision; pick one return std::max(locationRange.start, usedIo[set][r].location.start); } else if (locationRange.overlap(usedIo[set][r].location) && type.getBasicType() != usedIo[set][r].basicType) { // aliased-type mismatch typeCollision = true; return std::max(locationRange.start, usedIo[set][r].location.start); } } } usedIo[set].push_back(range); return -1; // no collision }
void QgsColorRampWidget::setColorFromPoint( QPointF point ) { int oldValue = componentValue(); int val; if ( mOrientation == QgsColorRampWidget::Horizontal ) { val = componentRange() * ( point.x() - mMargin ) / ( width() - 2 * mMargin ); } else { val = componentRange() - componentRange() * ( point.y() - mMargin ) / ( height() - 2 * mMargin ); } val = qMax( 0, qMin( val, componentRange() ) ); setComponentValue( val ); if ( componentValue() != oldValue ) { //value has changed emit colorChanged( mCurrentColor ); emit valueChanged( componentValue() ); } }
void QgsColorRampWidget::keyPressEvent( QKeyEvent *event ) { int oldValue = componentValue(); if (( mOrientation == QgsColorRampWidget::Horizontal && ( event->key() == Qt::Key_Right || event->key() == Qt::Key_Up ) ) || ( mOrientation == QgsColorRampWidget::Vertical && ( event->key() == Qt::Key_Left || event->key() == Qt::Key_Up ) ) ) { setComponentValue( componentValue() + 1 ); } else if (( mOrientation == QgsColorRampWidget::Horizontal && ( event->key() == Qt::Key_Left || event->key() == Qt::Key_Down ) ) || ( mOrientation == QgsColorRampWidget::Vertical && ( event->key() == Qt::Key_Right || event->key() == Qt::Key_Down ) ) ) { setComponentValue( componentValue() - 1 ); } else if (( mOrientation == QgsColorRampWidget::Horizontal && event->key() == Qt::Key_PageDown ) || ( mOrientation == QgsColorRampWidget::Vertical && event->key() == Qt::Key_PageUp ) ) { setComponentValue( componentValue() + 10 ); } else if (( mOrientation == QgsColorRampWidget::Horizontal && event->key() == Qt::Key_PageUp ) || ( mOrientation == QgsColorRampWidget::Vertical && event->key() == Qt::Key_PageDown ) ) { setComponentValue( componentValue() - 10 ); } else if (( mOrientation == QgsColorRampWidget::Horizontal && event->key() == Qt::Key_Home ) || ( mOrientation == QgsColorRampWidget::Vertical && event->key() == Qt::Key_End ) ) { setComponentValue( 0 ); } else if (( mOrientation == QgsColorRampWidget::Horizontal && event->key() == Qt::Key_End ) || ( mOrientation == QgsColorRampWidget::Vertical && event->key() == Qt::Key_Home ) ) { //set to maximum value setComponentValue( componentRange() ); } else { QgsColorWidget::keyPressEvent( event ); return; } if ( componentValue() != oldValue ) { //value has changed emit colorChanged( mCurrentColor ); emit valueChanged( componentValue() ); } }
void QgsColorSliderWidget::setComponent( const QgsColorWidget::ColorComponent component ) { QgsColorWidget::setComponent( component ); mRampWidget->setComponent( component ); mSpinBox->setMaximum( convertRealToDisplay( componentRange() ) ); if ( component == QgsColorWidget::Hue ) { //degrees suffix for hue mSpinBox->setSuffix( QChar( 176 ) ); } else if ( component == QgsColorWidget::Saturation || component == QgsColorWidget::Value || component == QgsColorWidget::Alpha ) { //saturation, value and alpha are in % mSpinBox->setSuffix( tr( "%" ) ); } else { //clear suffix mSpinBox->setSuffix( QString() ); } }
QgsColorSliderWidget::QgsColorSliderWidget( QWidget *parent, const ColorComponent component ) : QgsColorWidget( parent, component ) , mRampWidget( nullptr ) , mSpinBox( nullptr ) { QHBoxLayout* hLayout = new QHBoxLayout(); hLayout->setMargin( 0 ); hLayout->setSpacing( 5 ); mRampWidget = new QgsColorRampWidget( nullptr, component ); mRampWidget->setColor( mCurrentColor ); hLayout->addWidget( mRampWidget, 1 ); mSpinBox = new QSpinBox(); //set spinbox to a reasonable width int largestCharWidth = mSpinBox->fontMetrics().width( "888%" ); mSpinBox->setMinimumWidth( largestCharWidth + 35 ); mSpinBox->setMinimum( 0 ); mSpinBox->setMaximum( convertRealToDisplay( componentRange() ) ); mSpinBox->setValue( convertRealToDisplay( componentValue() ) ); if ( component == QgsColorWidget::Hue ) { //degrees suffix for hue mSpinBox->setSuffix( QChar( 176 ) ); } else if ( component == QgsColorWidget::Saturation || component == QgsColorWidget::Value || component == QgsColorWidget::Alpha ) { mSpinBox->setSuffix( tr( "%" ) ); } hLayout->addWidget( mSpinBox ); setLayout( hLayout ); connect( mRampWidget, SIGNAL( valueChanged( int ) ), this, SLOT( rampChanged( int ) ) ); connect( mRampWidget, SIGNAL( colorChanged( const QColor ) ), this, SLOT( rampColorChanged( const QColor ) ) ); connect( mSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( spinChanged( int ) ) ); }
int QgsColorBox::valueRangeY() const { return componentRange( yComponent() ); }
void QgsColorWidget::setComponentValue( const int value ) { if ( mComponent == QgsColorWidget::Multiple ) { return; } //clip value to valid range int valueClipped = qMin( value, componentRange() ); valueClipped = qMax( valueClipped, 0 ); int r, g, b, a; mCurrentColor.getRgb( &r, &g, &b, &a ); int h, s, v; mCurrentColor.getHsv( &h, &s, &v ); //overwrite hue with explicit hue if required h = hue(); switch ( mComponent ) { case QgsColorWidget::Red: if ( r == valueClipped ) { return; } mCurrentColor.setRed( valueClipped ); break; case QgsColorWidget::Green: if ( g == valueClipped ) { return; } mCurrentColor.setGreen( valueClipped ); break; case QgsColorWidget::Blue: if ( b == valueClipped ) { return; } mCurrentColor.setBlue( valueClipped ); break; case QgsColorWidget::Hue: if ( h == valueClipped ) { return; } mCurrentColor.setHsv( valueClipped, s, v, a ); break; case QgsColorWidget::Saturation: if ( s == valueClipped ) { return; } mCurrentColor.setHsv( h, valueClipped, v, a ); break; case QgsColorWidget::Value: if ( v == valueClipped ) { return; } mCurrentColor.setHsv( h, s, valueClipped, a ); break; case QgsColorWidget::Alpha: if ( a == valueClipped ) { return; } mCurrentColor.setAlpha( valueClipped ); break; default: return; } //update recorded hue if ( mCurrentColor.hue() >= 0 ) { mExplicitHue = mCurrentColor.hue(); } update(); }
int QgsColorWidget::componentRange() const { return componentRange( mComponent ); }
void QgsColorRampWidget::paintEvent( QPaintEvent *event ) { Q_UNUSED( event ); QPainter painter( this ); if ( mShowFrame ) { //draw frame QStyleOptionFrameV3 option; option.initFrom( this ); option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None; style()->drawPrimitive( QStyle::PE_Frame, &option, &painter ); } if ( hasFocus() ) { //draw focus rect QStyleOptionFocusRect option; option.initFrom( this ); option.state = QStyle::State_KeyboardFocusChange; style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter ); } if ( mComponent != QgsColorWidget::Alpha ) { int maxValue = ( mOrientation == QgsColorRampWidget::Horizontal ? width() : height() ) - 1 - 2 * mMargin; QColor color = QColor( mCurrentColor ); color.setAlpha( 255 ); QPen pen; pen.setWidth( 0 ); painter.setPen( pen ); painter.setBrush( Qt::NoBrush ); //draw background ramp for ( int c = 0; c <= maxValue; ++c ) { int colorVal = componentRange() * ( double )c / maxValue; //vertical sliders are reversed if ( mOrientation == QgsColorRampWidget::Vertical ) { colorVal = componentRange() - colorVal; } alterColor( color, mComponent, colorVal ); if ( color.hue() < 0 ) { color.setHsv( hue(), color.saturation(), color.value() ); } pen.setColor( color ); painter.setPen( pen ); if ( mOrientation == QgsColorRampWidget::Horizontal ) { //horizontal painter.drawLine( c + mMargin, mMargin, c + mMargin, height() - mMargin - 1 ); } else { //vertical painter.drawLine( mMargin, c + mMargin, width() - mMargin - 1, c + mMargin ); } } } else if ( mComponent == QgsColorWidget::Alpha ) { //alpha ramps are drawn differently //start with the checkboard pattern QBrush checkBrush = QBrush( transparentBackground() ); painter.setBrush( checkBrush ); painter.setPen( Qt::NoPen ); painter.drawRect( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ); QLinearGradient colorGrad; if ( mOrientation == QgsColorRampWidget::Horizontal ) { //horizontal colorGrad = QLinearGradient( mMargin, 0, width() - mMargin - 1, 0 ); } else { //vertical colorGrad = QLinearGradient( 0, mMargin, 0, height() - mMargin - 1 ); } QColor transparent = QColor( mCurrentColor ); transparent.setAlpha( 0 ); colorGrad.setColorAt( 0, transparent ); QColor opaque = QColor( mCurrentColor ); opaque.setAlpha( 255 ); colorGrad.setColorAt( 1, opaque ); QBrush colorBrush = QBrush( colorGrad ); painter.setBrush( colorBrush ); painter.drawRect( mMargin, mMargin, width() - 2 * mMargin - 1, height() - 2 * mMargin - 1 ); } if ( mOrientation == QgsColorRampWidget::Horizontal ) { //draw marker triangles for horizontal ramps painter.setRenderHint( QPainter::Antialiasing ); painter.setBrush( QBrush( Qt::black ) ); painter.setPen( Qt::NoPen ); painter.translate( mMargin + ( width() - 2 * mMargin ) * ( double )componentValue() / componentRange(), mMargin - 1 ); painter.drawPolygon( mTopTriangle ); painter.translate( 0, height() - mMargin - 2 ); painter.setBrush( QBrush( Qt::white ) ); painter.drawPolygon( mBottomTriangle ); painter.end(); } else { //draw cross lines for vertical ramps int ypos = mMargin + ( height() - 2 * mMargin - 1 ) - ( height() - 2 * mMargin - 1 ) * ( double )componentValue() / componentRange(); painter.setBrush( Qt::white ); painter.setPen( Qt::NoPen ); painter.drawRect( mMargin, ypos - 1, width() - 2 * mMargin - 1, 3 ); painter.setPen( Qt::black ); painter.drawLine( mMargin, ypos, width() - mMargin - 1, ypos ); } }