21 #include <QMouseEvent> 25 #define NUMBER_COLORS_PER_ROW 10 //number of color swatches per row 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 );
47 mSwatchOutlineSize = std::max( fontMetrics().width( QStringLiteral(
"." ) ) * 0.4, 1.0 );
49 mSwatchSpacing = mSwatchSize * 0.3;
50 mSwatchMargin = mLabelMargin;
60 return QSize( mWidth, calculateHeight() );
65 return QSize( mWidth, calculateHeight() );
83 mColors = mScheme->
fetchColors( mContext, mBaseColor );
93 QPainter painter(
this );
101 int newBox = swatchForPosition( event->pos() );
103 mDrawBoxDepressed =
event->buttons() & Qt::LeftButton;
104 if ( newBox != mCurrentHoverBox )
107 mCurrentHoverBox = newBox;
110 updateTooltip( newBox );
116 void QgsColorSwatchGrid::updateTooltip(
const int colorIdx )
118 if ( colorIdx >= 0 && colorIdx < mColors.length() )
120 QColor color = mColors.at( colorIdx ).first;
123 QString colorName = mColors.at( colorIdx ).second;
127 int height =
static_cast< int >( width / 1.61803398875 );
128 int margin =
static_cast< int >( height * 0.1 );
129 QImage icon = QImage( width + 2 * margin, height + 2 * margin, QImage::Format_ARGB32 );
130 icon.fill( Qt::transparent );
136 QBrush checkBrush = QBrush( transparentBackground() );
137 p.setPen( Qt::NoPen );
138 p.setBrush( checkBrush );
139 p.drawRect( margin, margin, width, height );
142 p.setBrush( QBrush( mColors.at( colorIdx ).first ) );
145 p.setPen( QColor( 197, 197, 197 ) );
146 p.drawRect( margin, margin, width, height );
150 QBuffer buffer( &data );
151 icon.save( &buffer,
"PNG", 100 );
154 if ( !colorName.isEmpty() )
155 info += QStringLiteral(
"<h3>%1</h3><p>" ).arg( colorName );
157 info += QStringLiteral(
"<b>HEX</b> %1<br>" 159 "<b>HSV</b> %3,%4,%5<p>" ).arg( color.name(),
161 .arg( color.hue() ).arg( color.saturation() ).arg( color.value() );
162 info += QStringLiteral(
"<img src='data:image/png;base64, %0'>" ).arg( QString( data.toBase64() ) );
170 setToolTip( QString() );
176 if ( !mDrawBoxDepressed && event->buttons() & Qt::LeftButton )
178 mCurrentHoverBox = swatchForPosition( event->pos() );
179 mDrawBoxDepressed =
true;
182 mPressedOnWidget =
true;
187 if ( ! mPressedOnWidget )
192 int box = swatchForPosition( event->pos() );
193 if ( mDrawBoxDepressed && event->button() == Qt::LeftButton )
195 mCurrentHoverBox = box;
196 mDrawBoxDepressed =
false;
200 if ( box >= 0 && box < mColors.length() &&
event->button() == Qt::LeftButton )
210 if ( event->key() == Qt::Key_Right )
212 mCurrentFocusBox = std::min( mCurrentFocusBox + 1, mColors.length() - 1 );
214 else if ( event->key() == Qt::Key_Left )
216 mCurrentFocusBox = std::max( mCurrentFocusBox - 1, 0 );
218 else if ( event->key() == Qt::Key_Up )
224 if ( currentRow >= 0 )
231 focusPreviousChild();
234 else if ( event->key() == Qt::Key_Down )
241 if ( box < mColors.length() )
243 mCurrentFocusBox = box;
251 else if ( event->key() == Qt::Key_Enter ||
event->key() == Qt::Key_Space )
254 emit
colorChanged( mColors.at( mCurrentFocusBox ).first );
259 QWidget::keyPressEvent( event );
280 int QgsColorSwatchGrid::calculateHeight()
const 283 return numberRows * ( mSwatchSize ) + ( numberRows - 1 ) * mSwatchSpacing + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin + mSwatchMargin;
286 void QgsColorSwatchGrid::draw( QPainter &painter )
288 QPalette
pal = QPalette( qApp->palette() );
289 QColor headerBgColor = pal.color( QPalette::Mid );
290 QColor headerTextColor = pal.color( QPalette::BrightText );
291 QColor highlight = pal.color( QPalette::Highlight );
294 painter.setBrush( headerBgColor );
295 painter.setPen( Qt::NoPen );
296 painter.drawRect( QRect( 0, 0, width(), mLabelHeight + 0.5 * mLabelMargin ) );
299 painter.setPen( headerTextColor );
300 painter.drawText( QRect( mLabelMargin, 0.25 * mLabelMargin, width() - 2 * mLabelMargin, mLabelHeight ),
301 Qt::AlignLeft | Qt::AlignVCenter, mScheme->
schemeName() );
304 QgsNamedColorList::const_iterator colorIt = mColors.constBegin();
306 for ( ; colorIt != mColors.constEnd(); ++colorIt )
311 QRect swatchRect = QRect( column * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin,
312 row * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin,
313 mSwatchSize, mSwatchSize );
315 if ( mCurrentHoverBox == index )
318 swatchRect.adjust( -1, -1, 1, 1 );
322 if ( ( *colorIt ).first.alpha() != 255 )
324 QBrush checkBrush = QBrush( transparentBackground() );
325 painter.setPen( Qt::NoPen );
326 painter.setBrush( checkBrush );
327 painter.drawRect( swatchRect );
330 if ( mCurrentHoverBox == index )
332 if ( mDrawBoxDepressed )
334 painter.setPen( QPen( QColor( 100, 100, 100 ), mSwatchOutlineSize ) );
339 painter.setPen( QPen( QColor( 220, 220, 220 ), mSwatchOutlineSize ) );
342 else if ( mFocused && index == mCurrentFocusBox )
344 painter.setPen( highlight );
346 else if ( ( *colorIt ).first.name() == mBaseColor.name() )
349 painter.setPen( QPen( QColor( 75, 75, 75 ), mSwatchOutlineSize ) );
353 painter.setPen( QPen( QColor( 197, 197, 197 ), mSwatchOutlineSize ) );
356 painter.setBrush( ( *colorIt ).first );
357 painter.drawRect( swatchRect );
363 QPixmap QgsColorSwatchGrid::transparentBackground()
365 static QPixmap sTranspBkgrd;
367 if ( sTranspBkgrd.isNull() )
373 int QgsColorSwatchGrid::swatchForPosition( QPoint position )
const 377 int column = ( position.x() - mSwatchMargin ) / ( mSwatchSize + mSwatchSpacing );
378 int xRem = ( position.x() - mSwatchMargin ) % ( mSwatchSize + mSwatchSpacing );
379 int row = ( position.y() - mSwatchMargin - mLabelHeight ) / ( mSwatchSize + mSwatchSpacing );
380 int yRem = ( position.y() - mSwatchMargin - mLabelHeight ) % ( mSwatchSize + mSwatchSpacing );
397 : QWidgetAction( parent )
399 , mSuppressRecurse( false )
400 , mDismissOnColorSelection( true )
404 setDefaultWidget( mColorSwatchGrid );
407 connect(
this, &QAction::hovered,
this, &QgsColorSwatchGridAction::onHover );
411 setVisible( !mColorSwatchGrid->
colors()->isEmpty() );
426 return mColorSwatchGrid->
context();
438 setVisible( !mColorSwatchGrid->
colors()->isEmpty() );
441 void QgsColorSwatchGridAction::setColor(
const QColor &color )
445 if ( mMenu && mDismissOnColorSelection )
451 void QgsColorSwatchGridAction::onHover()
455 if ( mSuppressRecurse )
462 mSuppressRecurse =
true;
463 mMenu->setActiveAction(
this );
464 mSuppressRecurse =
false;
void mouseReleaseEvent(QMouseEvent *event) override
void focusOutEvent(QFocusEvent *event) override
QgsColorSwatchGrid(QgsColorScheme *scheme, const QString &context=QString(), QWidget *parent=nullptr)
Construct a new color swatch grid.
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
static const double UI_SCALE_FACTOR
UI scaling factor.
void refreshColors()
Reload colors from scheme and redraws the widget.
Abstract base class for color schemes.
void paintEvent(QPaintEvent *event) override
QString context() const
Gets the current context for the grid.
QSize sizeHint() const override
QSize minimumSizeHint() const override
void colorChanged(const QColor &color)
Emitted when a color has been selected from the widget.
A grid of color swatches, which allows for user selection.
static QPixmap getThemePixmap(const QString &name)
Helper to get a theme icon as a pixmap.
QColor baseColor() const
Gets the base color for the widget.
QgsNamedColorList * colors()
Gets the list of colors shown in the grid.
static QString encodeColor(const QColor &color)
#define NUMBER_COLORS_PER_ROW
QgsColorSwatchGridAction(QgsColorScheme *scheme, QMenu *menu=nullptr, const QString &context=QString(), QWidget *parent=nullptr)
Construct a new color swatch grid action.
void hovered()
Emitted when mouse hovers over widget.
QColor baseColor() const
Gets the base color for the color grid.
void mousePressEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void keyPressEvent(QKeyEvent *event) override
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 widget.
void refreshColors()
Reload colors from scheme and redraws the widget.
void setContext(const QString &context)
Sets the current context for the grid.
void setContext(const QString &context)
Sets the current context for the color grid.
QString context() const
Gets the current context for the color grid.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.
void focusInEvent(QFocusEvent *event) override