Exemple #1
0
QPixmap QgsColorButton::createMenuIcon( const QColor &color, const bool showChecks )
{
  const int iconSize = QgsGuiUtils::scaleIconSize( 16 );

  //create an icon pixmap
  QPixmap pixmap( iconSize, iconSize );
  pixmap.fill( Qt::transparent );

  QPainter p;
  p.begin( &pixmap );

  //start with checkboard pattern
  if ( showChecks )
  {
    QBrush checkBrush = QBrush( transparentBackground() );
    p.setPen( Qt::NoPen );
    p.setBrush( checkBrush );
    p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
  }

  //draw color over pattern
  p.setBrush( QBrush( color ) );

  //draw border
  p.setPen( QColor( 197, 197, 197 ) );
  p.drawRect( 0, 0, iconSize - 1, iconSize - 1 );
  p.end();
  return pixmap;
}
Exemple #2
0
void QgsColorPreviewWidget::drawColor( const QColor &color, QRect rect, QPainter& painter )
{
  painter.setPen( Qt::NoPen );
  //if color has an alpha, start with a checkboard pattern
  if ( color.alpha() < 255 )
  {
    QBrush checkBrush = QBrush( transparentBackground() );
    painter.setBrush( checkBrush );
    painter.drawRect( rect );

    //draw half of widget showing solid color, the other half showing color with alpha

    //ensure at least a 1px overlap to avoid artifacts
    QBrush colorBrush = QBrush( color );
    painter.setBrush( colorBrush );
    painter.drawRect( floor( rect.width() / 2.0 ) + rect.left(), rect.top(), rect.width() - floor( rect.width() / 2.0 ), rect.height() );

    QColor opaqueColor = QColor( color );
    opaqueColor.setAlpha( 255 );
    QBrush opaqueBrush = QBrush( opaqueColor );
    painter.setBrush( opaqueBrush );
    painter.drawRect( rect.left(), rect.top(), ceil( rect.width() / 2.0 ), rect.height() );
  }
  else
  {
    //no alpha component, just draw a solid rectangle
    QBrush brush = QBrush( color );
    painter.setBrush( brush );
    painter.drawRect( rect );
  }
}
void QgsColorSwatchGrid::updateTooltip( const int colorIdx )
{
  if ( colorIdx >= 0 && colorIdx < mColors.length() )
  {
    QColor color = mColors.at( colorIdx ).first;

    //if color has an associated name from the color scheme, use that
    QString colorName = mColors.at( colorIdx ).second;

    // create very large preview swatch, because the grid itself has only tiny preview icons
    int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 23 );
    int height = static_cast< int >( width / 1.61803398875 ); // golden ratio
    int margin = static_cast< int >( height * 0.1 );
    QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
    icon.fill( Qt::transparent );

    QPainter p;
    p.begin( &icon );

    //start with checkboard pattern
    QBrush checkBrush = QBrush( transparentBackground() );
    p.setPen( Qt::NoPen );
    p.setBrush( checkBrush );
    p.drawRect( margin, margin, width, height );

    //draw color over pattern
    p.setBrush( QBrush( mColors.at( colorIdx ).first ) );

    //draw border
    p.setPen( QColor( 197, 197, 197 ) );
    p.drawRect( margin, margin, width, height );
    p.end();

    QByteArray data;
    QBuffer buffer( &data );
    icon.save( &buffer, "PNG", 100 );

    QString info;
    if ( !colorName.isEmpty() )
      info += QStringLiteral( "<h3>%1</h3><p>" ).arg( colorName );

    info += QStringLiteral( "<b>HEX</b> %1<br>"
                            "<b>RGB</b> %2<br>"
                            "<b>HSV</b> %3,%4,%5<p>" ).arg( color.name(),
                                QgsSymbolLayerUtils::encodeColor( color ) )
            .arg( color.hue() ).arg( color.saturation() ).arg( color.value() );
    info += QStringLiteral( "<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ) );

    setToolTip( info );

  }
  else
  {
    //clear tooltip
    setToolTip( QString() );
  }
}
void QgsGradientStopEditor::paintEvent( QPaintEvent *event )
{
  Q_UNUSED( event );
  QPainter painter( this );

  QRect frameRect( rect().x() + MARGIN_X, rect().y(),
                   rect().width() - 2 * MARGIN_X,
                   rect().height() - MARGIN_BOTTOM );

  //draw frame
  QStyleOptionFrame option;
  option.initFrom( this );
  option.state = hasFocus() ? QStyle::State_KeyboardFocusChange : QStyle::State_None;
  option.rect = frameRect;
  style()->drawPrimitive( QStyle::PE_Frame, &option, &painter );

  if ( hasFocus() )
  {
    //draw focus rect
    QStyleOptionFocusRect option;
    option.initFrom( this );
    option.state = QStyle::State_KeyboardFocusChange;
    option.rect = frameRect;
    style()->drawPrimitive( QStyle::PE_FrameFocusRect, &option, &painter );
  }

  //start with the checkboard pattern
  QBrush checkBrush = QBrush( transparentBackground() );
  painter.setBrush( checkBrush );
  painter.setPen( Qt::NoPen );

  QRect box( frameRect.x() + FRAME_MARGIN, frameRect.y() + FRAME_MARGIN,
             frameRect.width() - 2 * FRAME_MARGIN,
             frameRect.height() - 2 * FRAME_MARGIN );

  painter.drawRect( box );

  // draw gradient preview on top of checkerboard
  for ( int i = 0; i < box.width() + 1; ++i )
  {
    QPen pen( mGradient.color( static_cast< double >( i ) / box.width() ) );
    painter.setPen( pen );
    painter.drawLine( box.left() + i, box.top(), box.left() + i, box.height() + 1 );
  }

  // draw stop markers
  int markerTop = frameRect.bottom() + 1;
  drawStopMarker( painter, QPoint( box.left(), markerTop ), mGradient.color1(), mSelectedStop == 0 );
  drawStopMarker( painter, QPoint( box.right(), markerTop ), mGradient.color2(), mSelectedStop == mGradient.count() - 1 );
  int i = 1;
  Q_FOREACH ( const QgsGradientStop& stop, mStops )
  {
    int x = stop.offset * box.width() + box.left();
    drawStopMarker( painter, QPoint( x, markerTop ), stop.color, mSelectedStop == i );
    ++i;
  }
Exemple #5
0
bool QgsColorButton::event( QEvent *e )
{
  if ( e->type() == QEvent::ToolTip )
  {
    QColor c = linkedProjectColor();
    bool isProjectColor = c.isValid();
    if ( !isProjectColor )
      c = mColor;

    QString name = c.name();
    int hue = c.hue();
    int value = c.value();
    int saturation = c.saturation();

    // create very large preview swatch
    int width = static_cast< int >( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 23 );
    int height = static_cast< int >( width / 1.61803398875 ); // golden ratio

    int margin = static_cast< int >( height * 0.1 );
    QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
    icon.fill( Qt::transparent );

    QPainter p;
    p.begin( &icon );

    //start with checkboard pattern
    QBrush checkBrush = QBrush( transparentBackground() );
    p.setPen( Qt::NoPen );
    p.setBrush( checkBrush );
    p.drawRect( margin, margin, width, height );

    //draw color over pattern
    p.setBrush( QBrush( c ) );

    //draw border
    p.setPen( QColor( 197, 197, 197 ) );
    p.drawRect( margin, margin, width, height );
    p.end();

    QByteArray data;
    QBuffer buffer( &data );
    icon.save( &buffer, "PNG", 100 );

    QString info = ( isProjectColor ? QStringLiteral( "<p>%1: %2</p>" ).arg( tr( "Linked color" ), mLinkedColorName ) : QString() )
                   + QStringLiteral( "<b>HEX</b> %1<br>"
                                     "<b>RGB</b> %2<br>"
                                     "<b>HSV</b> %3,%4,%5<p>"
                                     "<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ), name,
                                         QgsSymbolLayerUtils::encodeColor( c ) )
                   .arg( hue ).arg( saturation ).arg( value );
    setToolTip( info );
  }
  return QToolButton::event( e );
}
void QgsColorSwatchGrid::draw( QPainter &painter )
{
  QPalette pal = QPalette( qApp->palette() );
  QColor headerBgColor = pal.color( QPalette::Mid );
  QColor headerTextColor = pal.color( QPalette::BrightText );
  QColor highlight = pal.color( QPalette::Highlight );

  //draw header background
  painter.setBrush( headerBgColor );
  painter.setPen( Qt::NoPen );
  painter.drawRect( QRect( 0, 0, width(), mLabelHeight + 0.5 * mLabelMargin ) );

  //draw header text
  painter.setPen( headerTextColor );
  painter.drawText( QRect( mLabelMargin, 0.25 * mLabelMargin, width() - 2 * mLabelMargin, mLabelHeight ),
                    Qt::AlignLeft | Qt::AlignVCenter, mScheme->schemeName() );

  //draw color swatches
  QgsNamedColorList::const_iterator colorIt = mColors.constBegin();
  int index = 0;
  for ( ; colorIt != mColors.constEnd(); ++colorIt )
  {
    int row = index / NUMBER_COLORS_PER_ROW;
    int column = index % NUMBER_COLORS_PER_ROW;

    QRect swatchRect = QRect( column * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin,
                              row * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin,
                              mSwatchSize, mSwatchSize );

    if ( mCurrentHoverBox == index )
    {
      //hovered boxes are slightly larger
      swatchRect.adjust( -1, -1, 1, 1 );
    }

    //start with checkboard pattern for semi-transparent colors
    if ( ( *colorIt ).first.alpha() != 255 )
    {
      QBrush checkBrush = QBrush( transparentBackground() );
      painter.setPen( Qt::NoPen );
      painter.setBrush( checkBrush );
      painter.drawRect( swatchRect );
    }

    if ( mCurrentHoverBox == index )
    {
      if ( mDrawBoxDepressed )
      {
        painter.setPen( QPen( QColor( 100, 100, 100 ), mSwatchOutlineSize ) );
      }
      else
      {
        //hover color
        painter.setPen( QPen( QColor( 220, 220, 220 ), mSwatchOutlineSize ) );
      }
    }
    else if ( mFocused && index == mCurrentFocusBox )
    {
      painter.setPen( highlight );
    }
    else if ( ( *colorIt ).first.name() == mBaseColor.name() )
    {
      //currently active color
      painter.setPen( QPen( QColor( 75, 75, 75 ), mSwatchOutlineSize ) );
    }
    else
    {
      painter.setPen( QPen( QColor( 197, 197, 197 ), mSwatchOutlineSize ) );
    }

    painter.setBrush( ( *colorIt ).first );
    painter.drawRect( swatchRect );

    index++;
  }
}
Exemple #7
0
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 );
  }
}
Exemple #8
0
void QgsColorButton::setButtonBackground( const QColor &color )
{
  QColor backgroundColor = color;
  bool isProjectColor = false;
  if ( !backgroundColor.isValid() && !mLinkedColorName.isEmpty() )
  {
    backgroundColor = linkedProjectColor();
    isProjectColor = backgroundColor.isValid();
    if ( !isProjectColor )
    {
      mLinkedColorName.clear(); //color has been deleted, renamed, etc...
      emit unlinked();
    }
  }
  if ( !backgroundColor.isValid() )
  {
    backgroundColor = mColor;
  }

  QSize currentIconSize;
  //icon size is button size with a small margin
  if ( menu() )
  {
    if ( !mIconSize.isValid() )
    {
      //calculate size of push button part of widget (ie, without the menu drop-down button part)
      QStyleOptionToolButton opt;
      initStyleOption( &opt );
      QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
                         this );
      //make sure height of icon looks good under different platforms
#ifdef Q_OS_WIN
      mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
#else
      mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
#endif
    }
    currentIconSize = mIconSize;
  }
  else
  {
    //no menu
#ifdef Q_OS_WIN
    currentIconSize = QSize( width() - 10, height() - 6 );
#else
    currentIconSize = QSize( width() - 10, height() - 12 );
#endif
  }

  if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
  {
    return;
  }

  //create an icon pixmap
  QPixmap pixmap( currentIconSize );
  pixmap.fill( Qt::transparent );

  if ( backgroundColor.isValid() )
  {
    QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
    QPainter p;
    p.begin( &pixmap );
    p.setRenderHint( QPainter::Antialiasing );
    p.setPen( Qt::NoPen );
    if ( mAllowOpacity && backgroundColor.alpha() < 255 )
    {
      //start with checkboard pattern
      QBrush checkBrush = QBrush( transparentBackground() );
      p.setBrush( checkBrush );
      p.drawRoundedRect( rect, 3, 3 );
    }

    //draw semi-transparent color on top
    p.setBrush( backgroundColor );
    p.drawRoundedRect( rect, 3, 3 );
    p.end();
  }

  setIconSize( currentIconSize );
  setIcon( pixmap );
}
Exemple #9
0
void QgsColorSwatchGrid::draw( QPainter &painter )
{
  QPalette pal = QPalette( qApp->palette() );
  QColor headerBgColor = pal.color( QPalette::Mid );
  QColor headerTextColor = pal.color( QPalette::BrightText );
  QColor highlight = pal.color( QPalette::Highlight );

  //draw header background
  painter.setBrush( headerBgColor );
  painter.setPen( Qt::NoPen );
  painter.drawRect( QRect( 0, 0, width(), LABEL_SIZE ) );

  //draw header text
  painter.setPen( headerTextColor );
  painter.drawText( QRect( LABEL_MARGIN, 0, width() - 2 * LABEL_MARGIN, LABEL_SIZE ),
                    Qt::AlignLeft | Qt::AlignVCenter, mScheme->schemeName() );

  //draw color swatches
  QgsNamedColorList::iterator colorIt = mColors.begin();
  int index = 0;
  for ( ; colorIt != mColors.end(); ++colorIt )
  {
    int row = index / NUMBER_COLORS_PER_ROW;
    int column = index % NUMBER_COLORS_PER_ROW;

    QRect swatchRect = QRect( column * ( SWATCH_SIZE + SWATCH_SPACING ) + LEFT_MARGIN,
                              row * ( SWATCH_SIZE + SWATCH_SPACING ) + TOP_MARGIN + LABEL_SIZE,
                              SWATCH_SIZE, SWATCH_SIZE );

    if ( mCurrentHoverBox == index )
    {
      //hovered boxes are slightly larger
      swatchRect.adjust( -1, -1, 1, 1 );
    }

    //start with checkboard pattern for semi-transparent colors
    if (( *colorIt ).first.alpha() != 255 )
    {
      QBrush checkBrush = QBrush( transparentBackground() );
      painter.setPen( Qt::NoPen );
      painter.setBrush( checkBrush );
      painter.drawRect( swatchRect );
    }

    if ( mCurrentHoverBox == index )
    {
      if ( mDrawBoxDepressed )
      {
        painter.setPen( QColor( 100, 100, 100 ) );
      }
      else
      {
        //hover color
        painter.setPen( QColor( 220, 220, 220 ) );
      }
    }
    else if ( mFocused && index == mCurrentFocusBox )
    {
      painter.setPen( highlight );
    }
    else if (( *colorIt ).first.name() == mBaseColor.name() )
    {
      //currently active color
      painter.setPen( QColor( 75, 75, 75 ) );
    }
    else
    {
      painter.setPen( QColor( 197, 197, 197 ) );
    }

    painter.setBrush(( *colorIt ).first );
    painter.drawRect( swatchRect );

    index++;
  }
}