25#define NUMBER_COLORS_PER_ROW 10  
   31  , mDrawBoxDepressed( false )
 
   32  , mCurrentHoverBox( -1 )
 
   34  , mCurrentFocusBox( 0 )
 
   35  , mPressedOnWidget( false )
 
   38  setMouseTracking( 
true );
 
   40  setFocusPolicy( Qt::StrongFocus );
 
   41  setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
 
   46  mSwatchOutlineSize = std::max( fontMetrics().horizontalAdvance( 
'.' ) * 0.4, 1.0 );
 
   48  mSwatchSpacing = mSwatchSize * 0.3;
 
   49  mSwatchMargin = mLabelMargin;
 
 
   59  return QSize( mWidth, calculateHeight() );
 
 
   64  return QSize( mWidth, calculateHeight() );
 
 
   82  mColors = mScheme->
fetchColors( mContext, mBaseColor );
 
 
   92  QPainter painter( 
this );
 
 
  100  const int newBox = swatchForPosition( event->pos() );
 
  102  mDrawBoxDepressed = 
event->buttons() & Qt::LeftButton;
 
  103  if ( newBox != mCurrentHoverBox )
 
  106    mCurrentHoverBox = newBox;
 
  109    updateTooltip( newBox );
 
 
  115void QgsColorSwatchGrid::updateTooltip( 
const int colorIdx )
 
  117  if ( colorIdx >= 0 && colorIdx < mColors.length() )
 
  119    const QColor color = mColors.at( colorIdx ).first;
 
  122    const QString colorName = mColors.at( colorIdx ).second;
 
  125    if ( !colorName.isEmpty() )
 
  126      info += QStringLiteral( 
"<h3>%1</h3><p>" ).arg( colorName );
 
  136    setToolTip( QString() );
 
  142  if ( !mDrawBoxDepressed && event->buttons() & Qt::LeftButton )
 
  144    mCurrentHoverBox = swatchForPosition( event->pos() );
 
  145    mDrawBoxDepressed = 
true;
 
  148  mPressedOnWidget = 
true;
 
 
  153  if ( ! mPressedOnWidget )
 
  158  const int box = swatchForPosition( event->pos() );
 
  159  if ( mDrawBoxDepressed && event->button() == Qt::LeftButton )
 
  161    mCurrentHoverBox = box;
 
  162    mDrawBoxDepressed = 
false;
 
  166  if ( box >= 0 && box < mColors.length() && event->button() == Qt::LeftButton )
 
 
  176  if ( event->key() == Qt::Key_Right )
 
  178    mCurrentFocusBox = std::min< int >( mCurrentFocusBox + 1, mColors.length() - 1 );
 
  180  else if ( event->key() == Qt::Key_Left )
 
  182    mCurrentFocusBox = std::max< int >( mCurrentFocusBox - 1, 0 );
 
  184  else if ( event->key() == Qt::Key_Up )
 
  190    if ( currentRow >= 0 )
 
  197      focusPreviousChild();
 
  200  else if ( event->key() == Qt::Key_Down )
 
  207    if ( box < mColors.length() )
 
  209      mCurrentFocusBox = box;
 
  217  else if ( event->key() == Qt::Key_Enter || event->key() == Qt::Key_Space )
 
  220    emit 
colorChanged( mColors.at( mCurrentFocusBox ).first );
 
  225    QWidget::keyPressEvent( event );
 
 
  246int QgsColorSwatchGrid::calculateHeight()
 const 
  249  return numberRows * ( mSwatchSize ) + ( numberRows - 1 ) * mSwatchSpacing + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin + mSwatchMargin;
 
  252void QgsColorSwatchGrid::draw( QPainter &painter )
 
  254  const QPalette 
pal = QPalette( qApp->palette() );
 
  255  const QColor headerBgColor = 
pal.color( QPalette::Mid );
 
  256  const QColor headerTextColor = 
pal.color( QPalette::BrightText );
 
  257  const QColor highlight = 
pal.color( QPalette::Highlight );
 
  260  painter.setBrush( headerBgColor );
 
  261  painter.setPen( Qt::NoPen );
 
  262  painter.drawRect( QRect( 0, 0, width(), mLabelHeight + 0.5 * mLabelMargin ) );
 
  265  painter.setPen( headerTextColor );
 
  266  painter.drawText( QRect( mLabelMargin, 0.25 * mLabelMargin, width() - 2 * mLabelMargin, mLabelHeight ),
 
  267                    Qt::AlignLeft | Qt::AlignVCenter, mScheme->
schemeName() );
 
  270  QgsNamedColorList::const_iterator colorIt = mColors.constBegin();
 
  272  for ( ; colorIt != mColors.constEnd(); ++colorIt )
 
  277    QRect swatchRect = QRect( column * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin,
 
  278                              row * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin,
 
  279                              mSwatchSize, mSwatchSize );
 
  281    if ( mCurrentHoverBox == index )
 
  284      swatchRect.adjust( -1, -1, 1, 1 );
 
  288    if ( ( *colorIt ).first.alpha() != 255 )
 
  290      const QBrush checkBrush = QBrush( transparentBackground() );
 
  291      painter.setPen( Qt::NoPen );
 
  292      painter.setBrush( checkBrush );
 
  293      painter.drawRect( swatchRect );
 
  296    if ( mCurrentHoverBox == index )
 
  298      if ( mDrawBoxDepressed )
 
  300        painter.setPen( QPen( QColor( 100, 100, 100 ), mSwatchOutlineSize ) );
 
  305        painter.setPen( QPen( QColor( 220, 220, 220 ), mSwatchOutlineSize ) );
 
  308    else if ( mFocused && index == mCurrentFocusBox )
 
  310      painter.setPen( highlight );
 
  312    else if ( ( *colorIt ).first.name() == mBaseColor.name() )
 
  315      painter.setPen( QPen( QColor( 75, 75, 75 ), mSwatchOutlineSize ) );
 
  319      painter.setPen( QPen( QColor( 197, 197, 197 ), mSwatchOutlineSize ) );
 
  322    painter.setBrush( ( *colorIt ).first );
 
  323    painter.drawRect( swatchRect );
 
  329QPixmap QgsColorSwatchGrid::transparentBackground()
 
  331  static QPixmap sTranspBkgrd;
 
  333  if ( sTranspBkgrd.isNull() )
 
  339int QgsColorSwatchGrid::swatchForPosition( QPoint position )
 const 
  343  const int column = ( position.x() - mSwatchMargin ) / ( mSwatchSize + mSwatchSpacing );
 
  344  const int xRem = ( position.x() - mSwatchMargin ) % ( mSwatchSize + mSwatchSpacing );
 
  345  const int row = ( position.y() - mSwatchMargin - mLabelHeight ) / ( mSwatchSize + mSwatchSpacing );
 
  346  const int yRem = ( position.y() - mSwatchMargin - mLabelHeight ) % ( mSwatchSize + mSwatchSpacing );
 
  363  : QWidgetAction( parent )
 
  365  , mSuppressRecurse( false )
 
  366  , mDismissOnColorSelection( true )
 
  370  setDefaultWidget( mColorSwatchGrid );
 
  373  connect( 
this, &QAction::hovered, 
this, &QgsColorSwatchGridAction::onHover );
 
  377  setVisible( !mColorSwatchGrid->
colors()->isEmpty() );
 
 
  392  return mColorSwatchGrid->
context();
 
 
  404  setVisible( !mColorSwatchGrid->
colors()->isEmpty() );
 
 
  407void QgsColorSwatchGridAction::setColor( 
const QColor &color )
 
  411  if ( mMenu && mDismissOnColorSelection )
 
  417void QgsColorSwatchGridAction::onHover()
 
  421  if ( mSuppressRecurse )
 
  428    mSuppressRecurse = 
true;
 
  429    mMenu->setActiveAction( 
this );
 
  430    mSuppressRecurse = 
false;
 
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.
 
Abstract base class for color schemes.
 
virtual QString schemeName() const =0
Gets the name for the color scheme.
 
virtual QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor())=0
Gets a list of colors from the scheme.
 
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.
 
QString context() const
Gets the current context for the color grid.
 
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
 
QgsColorSwatchGridAction(QgsColorScheme *scheme, QMenu *menu=nullptr, const QString &context=QString(), QWidget *parent=nullptr)
Construct a new color swatch grid action.
 
void refreshColors()
Reload colors from scheme and redraws the widget.
 
QColor baseColor() const
Gets the base color for the color grid.
 
void setContext(const QString &context)
Sets the current context for the color grid.
 
A grid of color swatches, which allows for user selection.
 
void mouseMoveEvent(QMouseEvent *event) override
 
void mousePressEvent(QMouseEvent *event) override
 
QColor baseColor() const
Gets the base color for the widget.
 
QgsColorSwatchGrid(QgsColorScheme *scheme, const QString &context=QString(), QWidget *parent=nullptr)
Construct a new color swatch grid.
 
void paintEvent(QPaintEvent *event) override
 
QgsNamedColorList * colors()
Gets the list of colors shown in the grid.
 
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
 
void setBaseColor(const QColor &baseColor)
Sets the base color for the widget.
 
QSize minimumSizeHint() const override
 
void mouseReleaseEvent(QMouseEvent *event) override
 
QSize sizeHint() const override
 
void refreshColors()
Reload colors from scheme and redraws the widget.
 
void focusInEvent(QFocusEvent *event) override
 
void keyPressEvent(QKeyEvent *event) override
 
void focusOutEvent(QFocusEvent *event) override
 
void hovered()
Emitted when mouse hovers over widget.
 
void setContext(const QString &context)
Sets the current context for the grid.
 
QString context() const
Gets the current context for the grid.
 
#define NUMBER_COLORS_PER_ROW