17#include "moc_qgscolorswatchgrid.cpp"
26#define NUMBER_COLORS_PER_ROW 10
32 , mDrawBoxDepressed( false )
33 , mCurrentHoverBox( -1 )
35 , mCurrentFocusBox( 0 )
36 , mPressedOnWidget( false )
39 setMouseTracking(
true );
41 setFocusPolicy( Qt::StrongFocus );
42 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
47 mSwatchOutlineSize = std::max( fontMetrics().horizontalAdvance(
'.' ) * 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 const int newBox = swatchForPosition( event->pos() );
103 mDrawBoxDepressed =
event->buttons() & Qt::LeftButton;
104 if ( newBox != mCurrentHoverBox )
107 mCurrentHoverBox = newBox;
110 updateTooltip( newBox );
116void QgsColorSwatchGrid::updateTooltip(
const int colorIdx )
118 if ( colorIdx >= 0 && colorIdx < mColors.length() )
120 const QColor color = mColors.at( colorIdx ).first;
123 const QString colorName = mColors.at( colorIdx ).second;
126 if ( !colorName.isEmpty() )
127 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 ), Qt::AlignLeft | Qt::AlignVCenter, mScheme->
schemeName() );
269 QgsNamedColorList::const_iterator colorIt = mColors.constBegin();
271 for ( ; colorIt != mColors.constEnd(); ++colorIt )
276 QRect swatchRect = QRect( column * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin, row * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin, mSwatchSize, mSwatchSize );
278 if ( mCurrentHoverBox == index )
281 swatchRect.adjust( -1, -1, 1, 1 );
285 if ( ( *colorIt ).first.alpha() != 255 )
287 const QBrush checkBrush = QBrush( transparentBackground() );
288 painter.setPen( Qt::NoPen );
289 painter.setBrush( checkBrush );
290 painter.drawRect( swatchRect );
293 if ( mCurrentHoverBox == index )
295 if ( mDrawBoxDepressed )
297 painter.setPen( QPen( QColor( 100, 100, 100 ), mSwatchOutlineSize ) );
302 painter.setPen( QPen( QColor( 220, 220, 220 ), mSwatchOutlineSize ) );
305 else if ( mFocused && index == mCurrentFocusBox )
307 painter.setPen( highlight );
309 else if ( ( *colorIt ).first.name() == mBaseColor.name() )
312 painter.setPen( QPen( QColor( 75, 75, 75 ), mSwatchOutlineSize ) );
316 painter.setPen( QPen( QColor( 197, 197, 197 ), mSwatchOutlineSize ) );
319 painter.setBrush( ( *colorIt ).first );
320 painter.drawRect( swatchRect );
326QPixmap QgsColorSwatchGrid::transparentBackground()
328 static QPixmap sTranspBkgrd;
330 if ( sTranspBkgrd.isNull() )
336int QgsColorSwatchGrid::swatchForPosition( QPoint position )
const
340 const int column = ( position.x() - mSwatchMargin ) / ( mSwatchSize + mSwatchSpacing );
341 const int xRem = ( position.x() - mSwatchMargin ) % ( mSwatchSize + mSwatchSpacing );
342 const int row = ( position.y() - mSwatchMargin - mLabelHeight ) / ( mSwatchSize + mSwatchSpacing );
343 const int yRem = ( position.y() - mSwatchMargin - mLabelHeight ) % ( mSwatchSize + mSwatchSpacing );
360 : QWidgetAction( parent )
362 , mSuppressRecurse( false )
363 , mDismissOnColorSelection( true )
367 setDefaultWidget( mColorSwatchGrid );
370 connect(
this, &QAction::hovered,
this, &QgsColorSwatchGridAction::onHover );
374 setVisible( !mColorSwatchGrid->
colors()->isEmpty() );
389 return mColorSwatchGrid->
context();
401 setVisible( !mColorSwatchGrid->
colors()->isEmpty() );
404void QgsColorSwatchGridAction::setColor(
const QColor &color )
408 if ( mMenu && mDismissOnColorSelection )
414void QgsColorSwatchGridAction::onHover()
418 if ( mSuppressRecurse )
425 mSuppressRecurse =
true;
426 mMenu->setActiveAction(
this );
427 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