27#include "moc_qgscolorswatchgrid.cpp"
29#define NUMBER_COLORS_PER_ROW 10
37 setMouseTracking(
true );
39 setFocusPolicy( Qt::StrongFocus );
40 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
45 mSwatchOutlineSize = std::max( fontMetrics().horizontalAdvance(
'.' ) * 0.4, 1.0 );
47 mSwatchSpacing = mSwatchSize * 0.3;
48 mSwatchMargin = mLabelMargin;
58 return QSize( mWidth, calculateHeight() );
63 return QSize( mWidth, calculateHeight() );
81 mColors = mScheme->fetchColors( mContext, mBaseColor );
91 QPainter painter(
this );
99 const int newBox = swatchForPosition( event->pos() );
101 mDrawBoxDepressed =
event->buttons() & Qt::LeftButton;
102 if ( newBox != mCurrentHoverBox )
105 mCurrentHoverBox = newBox;
108 updateTooltip( newBox );
114void QgsColorSwatchGrid::updateTooltip(
const int colorIdx )
116 if ( colorIdx >= 0 && colorIdx < mColors.length() )
118 const QColor color = mColors.at( colorIdx ).first;
121 const QString colorName = mColors.at( colorIdx ).second;
124 if ( !colorName.isEmpty() )
125 info += QStringLiteral(
"<h3>%1</h3><p>" ).arg( colorName );
134 setToolTip( QString() );
140 if ( !mDrawBoxDepressed && event->buttons() & Qt::LeftButton )
142 mCurrentHoverBox = swatchForPosition( event->pos() );
143 mDrawBoxDepressed =
true;
146 mPressedOnWidget =
true;
151 if ( !mPressedOnWidget )
156 const int box = swatchForPosition( event->pos() );
157 if ( mDrawBoxDepressed && event->button() == Qt::LeftButton )
159 mCurrentHoverBox = box;
160 mDrawBoxDepressed =
false;
164 if ( box >= 0 && box < mColors.length() && event->button() == Qt::LeftButton )
174 if ( event->key() == Qt::Key_Right )
176 mCurrentFocusBox = std::min<int>( mCurrentFocusBox + 1, mColors.length() - 1 );
178 else if ( event->key() == Qt::Key_Left )
180 mCurrentFocusBox = std::max<int>( mCurrentFocusBox - 1, 0 );
182 else if ( event->key() == Qt::Key_Up )
188 if ( currentRow >= 0 )
195 focusPreviousChild();
198 else if ( event->key() == Qt::Key_Down )
205 if ( box < mColors.length() )
207 mCurrentFocusBox = box;
215 else if ( event->key() == Qt::Key_Enter || event->key() == Qt::Key_Space )
218 emit
colorChanged( mColors.at( mCurrentFocusBox ).first );
223 QWidget::keyPressEvent( event );
244int QgsColorSwatchGrid::calculateHeight()
const
247 return numberRows * ( mSwatchSize ) + ( numberRows - 1 ) * mSwatchSpacing + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin + mSwatchMargin;
250void QgsColorSwatchGrid::draw( QPainter &painter )
252 const QPalette pal = QPalette( qApp->palette() );
253 const QColor headerBgColor = pal.color( QPalette::Mid );
254 const QColor headerTextColor = pal.color( QPalette::BrightText );
255 const QColor highlight = pal.color( QPalette::Highlight );
258 painter.setBrush( headerBgColor );
259 painter.setPen( Qt::NoPen );
260 painter.drawRect( QRect( 0, 0, width(), mLabelHeight + 0.5 * mLabelMargin ) );
263 painter.setPen( headerTextColor );
264 painter.drawText( QRect( mLabelMargin, 0.25 * mLabelMargin, width() - 2 * mLabelMargin, mLabelHeight ), Qt::AlignLeft | Qt::AlignVCenter, mScheme->schemeName() );
267 QgsNamedColorList::const_iterator colorIt = mColors.constBegin();
269 for ( ; colorIt != mColors.constEnd(); ++colorIt )
274 QRect swatchRect = QRect( column * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin, row * ( mSwatchSize + mSwatchSpacing ) + mSwatchMargin + mLabelHeight + 0.5 * mLabelMargin, mSwatchSize, mSwatchSize );
276 if ( mCurrentHoverBox == index )
279 swatchRect.adjust( -1, -1, 1, 1 );
283 if ( ( *colorIt ).first.alpha() != 255 )
285 const QBrush checkBrush = QBrush( transparentBackground() );
286 painter.setPen( Qt::NoPen );
287 painter.setBrush( checkBrush );
288 painter.drawRect( swatchRect );
291 if ( mCurrentHoverBox == index )
293 if ( mDrawBoxDepressed )
295 painter.setPen( QPen( QColor( 100, 100, 100 ), mSwatchOutlineSize ) );
300 painter.setPen( QPen( QColor( 220, 220, 220 ), mSwatchOutlineSize ) );
303 else if ( mFocused && index == mCurrentFocusBox )
305 painter.setPen( highlight );
307 else if ( ( *colorIt ).first.name() == mBaseColor.name() )
310 painter.setPen( QPen( QColor( 75, 75, 75 ), mSwatchOutlineSize ) );
314 painter.setPen( QPen( QColor( 197, 197, 197 ), mSwatchOutlineSize ) );
317 painter.setBrush( ( *colorIt ).first );
318 painter.drawRect( swatchRect );
324QPixmap QgsColorSwatchGrid::transparentBackground()
326 static QPixmap sTranspBkgrd;
328 if ( sTranspBkgrd.isNull() )
334int QgsColorSwatchGrid::swatchForPosition( QPoint position )
const
338 const int column = ( position.x() - mSwatchMargin ) / ( mSwatchSize + mSwatchSpacing );
339 const int xRem = ( position.x() - mSwatchMargin ) % ( mSwatchSize + mSwatchSpacing );
340 const int row = ( position.y() - mSwatchMargin - mLabelHeight ) / ( mSwatchSize + mSwatchSpacing );
341 const int yRem = ( position.y() - mSwatchMargin - mLabelHeight ) % ( mSwatchSize + mSwatchSpacing );
358 : QWidgetAction( parent )
363 setDefaultWidget( mColorSwatchGrid );
366 connect(
this, &QAction::hovered,
this, &QgsColorSwatchGridAction::onHover );
370 setVisible( !mColorSwatchGrid->colors()->isEmpty() );
375 mColorSwatchGrid->setBaseColor(
baseColor );
380 return mColorSwatchGrid->baseColor();
385 return mColorSwatchGrid->context();
390 mColorSwatchGrid->setContext(
context );
395 mColorSwatchGrid->refreshColors();
397 setVisible( !mColorSwatchGrid->colors()->isEmpty() );
400void QgsColorSwatchGridAction::setColor(
const QColor &color )
404 if ( mMenu && mDismissOnColorSelection )
410void QgsColorSwatchGridAction::onHover()
414 if ( mSuppressRecurse )
421 mSuppressRecurse =
true;
422 mMenu->setActiveAction(
this );
423 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.
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
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