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 );
137 setToolTip( QString() );
143 if ( !mDrawBoxDepressed && event->buttons() & Qt::LeftButton )
145 mCurrentHoverBox = swatchForPosition( event->pos() );
146 mDrawBoxDepressed =
true;
149 mPressedOnWidget =
true;
154 if ( ! mPressedOnWidget )
159 const int box = swatchForPosition( event->pos() );
160 if ( mDrawBoxDepressed && event->button() == Qt::LeftButton )
162 mCurrentHoverBox = box;
163 mDrawBoxDepressed =
false;
167 if ( box >= 0 && box < mColors.length() && event->button() == Qt::LeftButton )
177 if ( event->key() == Qt::Key_Right )
179 mCurrentFocusBox = std::min< int >( mCurrentFocusBox + 1, mColors.length() - 1 );
181 else if ( event->key() == Qt::Key_Left )
183 mCurrentFocusBox = std::max< int >( mCurrentFocusBox - 1, 0 );
185 else if ( event->key() == Qt::Key_Up )
191 if ( currentRow >= 0 )
198 focusPreviousChild();
201 else if ( event->key() == Qt::Key_Down )
208 if ( box < mColors.length() )
210 mCurrentFocusBox = box;
218 else if ( event->key() == Qt::Key_Enter || event->key() == Qt::Key_Space )
221 emit
colorChanged( mColors.at( mCurrentFocusBox ).first );
226 QWidget::keyPressEvent( event );
247int QgsColorSwatchGrid::calculateHeight()
const
250 return numberRows * ( mSwatchSize ) + ( numberRows - 1 ) * mSwatchSpacing + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin + mSwatchMargin;
253void QgsColorSwatchGrid::draw( QPainter &painter )
255 const QPalette
pal = QPalette( qApp->palette() );
256 const QColor headerBgColor =
pal.color( QPalette::Mid );
257 const QColor headerTextColor =
pal.color( QPalette::BrightText );
258 const QColor highlight =
pal.color( QPalette::Highlight );
261 painter.setBrush( headerBgColor );
262 painter.setPen( Qt::NoPen );
263 painter.drawRect( QRect( 0, 0, width(), mLabelHeight + 0.5 * mLabelMargin ) );
266 painter.setPen( headerTextColor );
267 painter.drawText( QRect( mLabelMargin, 0.25 * mLabelMargin, width() - 2 * mLabelMargin, mLabelHeight ),
268 Qt::AlignLeft | Qt::AlignVCenter, mScheme->
schemeName() );
271 QgsNamedColorList::const_iterator colorIt = mColors.constBegin();
273 for ( ; colorIt != mColors.constEnd(); ++colorIt )
278 QRect swatchRect = QRect( column * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin,
279 row * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin,
280 mSwatchSize, mSwatchSize );
282 if ( mCurrentHoverBox == index )
285 swatchRect.adjust( -1, -1, 1, 1 );
289 if ( ( *colorIt ).first.alpha() != 255 )
291 const QBrush checkBrush = QBrush( transparentBackground() );
292 painter.setPen( Qt::NoPen );
293 painter.setBrush( checkBrush );
294 painter.drawRect( swatchRect );
297 if ( mCurrentHoverBox == index )
299 if ( mDrawBoxDepressed )
301 painter.setPen( QPen( QColor( 100, 100, 100 ), mSwatchOutlineSize ) );
306 painter.setPen( QPen( QColor( 220, 220, 220 ), mSwatchOutlineSize ) );
309 else if ( mFocused && index == mCurrentFocusBox )
311 painter.setPen( highlight );
313 else if ( ( *colorIt ).first.name() == mBaseColor.name() )
316 painter.setPen( QPen( QColor( 75, 75, 75 ), mSwatchOutlineSize ) );
320 painter.setPen( QPen( QColor( 197, 197, 197 ), mSwatchOutlineSize ) );
323 painter.setBrush( ( *colorIt ).first );
324 painter.drawRect( swatchRect );
330QPixmap QgsColorSwatchGrid::transparentBackground()
332 static QPixmap sTranspBkgrd;
334 if ( sTranspBkgrd.isNull() )
340int QgsColorSwatchGrid::swatchForPosition( QPoint position )
const
344 const int column = ( position.x() - mSwatchMargin ) / ( mSwatchSize + mSwatchSpacing );
345 const int xRem = ( position.x() - mSwatchMargin ) % ( mSwatchSize + mSwatchSpacing );
346 const int row = ( position.y() - mSwatchMargin - mLabelHeight ) / ( mSwatchSize + mSwatchSpacing );
347 const int yRem = ( position.y() - mSwatchMargin - mLabelHeight ) % ( mSwatchSize + mSwatchSpacing );
364 : QWidgetAction( parent )
366 , mSuppressRecurse( false )
367 , mDismissOnColorSelection( true )
371 setDefaultWidget( mColorSwatchGrid );
374 connect(
this, &QAction::hovered,
this, &QgsColorSwatchGridAction::onHover );
378 setVisible( !mColorSwatchGrid->
colors()->isEmpty() );
393 return mColorSwatchGrid->
context();
405 setVisible( !mColorSwatchGrid->
colors()->isEmpty() );
408void QgsColorSwatchGridAction::setColor(
const QColor &color )
412 if ( mMenu && mDismissOnColorSelection )
418void QgsColorSwatchGridAction::onHover()
422 if ( mSuppressRecurse )
429 mSuppressRecurse =
true;
430 mMenu->setActiveAction(
this );
431 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