20 #include <QMouseEvent> 23 #define NUMBER_COLORS_PER_ROW 10 //number of color swatches per row 24 #define SWATCH_SIZE 14 //width/height of color swatches 25 #define SWATCH_SPACING 4 //horizontal/vertical gap between swatches 26 #define LEFT_MARGIN 6 //margin between left edge and first swatch 27 #define RIGHT_MARGIN 6 //margin between right edge and last swatch 28 #define TOP_MARGIN 6 //margin between label and first swatch 29 #define BOTTOM_MARGIN 6 //margin between last swatch row and end of widget 30 #define LABEL_SIZE 20 //label rect height 31 #define LABEL_MARGIN 4 //spacing between label box and text 37 , mDrawBoxDepressed( false )
38 , mCurrentHoverBox( -1 )
40 , mCurrentFocusBox( 0 )
41 , mPressedOnWidget( false )
62 return QSize( mWidth, calculateHeight() );
67 return QSize( mWidth, calculateHeight() );
85 mColors = mScheme->
fetchColors( mContext, mBaseColor );
103 int newBox = swatchForPosition( event->
pos() );
105 mDrawBoxDepressed =
event->buttons() & Qt::LeftButton;
106 if ( newBox != mCurrentHoverBox )
109 mCurrentHoverBox = newBox;
112 updateTooltip( newBox );
118 void QgsColorSwatchGrid::updateTooltip(
const int colorIdx )
120 if ( colorIdx >= 0 && colorIdx < mColors.
length() )
123 QString colorName = mColors.
at( colorIdx ).second;
127 QColor color = mColors.
at( colorIdx ).first;
141 if ( !mDrawBoxDepressed && event->
buttons() & Qt::LeftButton )
143 mCurrentHoverBox = swatchForPosition( event->
pos() );
144 mDrawBoxDepressed =
true;
147 mPressedOnWidget =
true;
152 if ( ! mPressedOnWidget )
157 int box = swatchForPosition( event->
pos() );
158 if ( mDrawBoxDepressed && event->
button() == Qt::LeftButton )
160 mCurrentHoverBox = box;
161 mDrawBoxDepressed =
false;
165 if ( box >= 0 && box < mColors.
length() &&
event->button() == Qt::LeftButton )
175 if ( event->
key() == Qt::Key_Right )
177 mCurrentFocusBox = qMin( mCurrentFocusBox + 1, mColors.
length() - 1 );
179 else if ( event->
key() == Qt::Key_Left )
181 mCurrentFocusBox = qMax( mCurrentFocusBox - 1, 0 );
183 else if ( event->
key() == Qt::Key_Up )
189 if ( currentRow >= 0 )
199 else if ( event->
key() == Qt::Key_Down )
206 if ( box < mColors.
length() )
208 mCurrentFocusBox = box;
216 else if ( event->
key() == Qt::Key_Enter ||
event->key() == Qt::Key_Space )
245 int QgsColorSwatchGrid::calculateHeight()
const 251 void QgsColorSwatchGrid::draw(
QPainter &painter )
255 QColor headerTextColor = pal.
color( QPalette::BrightText );
256 QColor highlight = pal.
color( QPalette::Highlight );
260 painter.
setPen( Qt::NoPen );
264 painter.
setPen( headerTextColor );
266 Qt::AlignLeft | Qt::AlignVCenter, mScheme->
schemeName() );
271 for ( ; colorIt != mColors.
constEnd(); ++colorIt )
280 if ( mCurrentHoverBox == index )
283 swatchRect.
adjust( -1, -1, 1, 1 );
287 if (( *colorIt ).first.alpha() != 255 )
290 painter.
setPen( Qt::NoPen );
295 if ( mCurrentHoverBox == index )
297 if ( mDrawBoxDepressed )
307 else if ( mFocused && index == mCurrentFocusBox )
309 painter.
setPen( highlight );
311 else if (( *colorIt ).first.name() == mBaseColor.
name() )
321 painter.
setBrush(( *colorIt ).first );
328 const QPixmap& QgsColorSwatchGrid::transparentBackground()
332 if ( transpBkgrd.
isNull() )
338 int QgsColorSwatchGrid::swatchForPosition(
QPoint position )
const 364 , mSuppressRecurse( false )
365 , mDismissOnColorSelection( true )
373 connect( mColorSwatchGrid, SIGNAL(
hovered() ),
this, SLOT( onHover() ) );
396 return mColorSwatchGrid->
context();
411 void QgsColorSwatchGridAction::setColor(
const QColor &color )
415 if ( mMenu && mDismissOnColorSelection )
421 void QgsColorSwatchGridAction::onHover()
425 if ( mSuppressRecurse )
432 mSuppressRecurse =
true;
434 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.
void refreshColors()
Reload colors from scheme and redraws the widget.
Abstract base class for color schemes.
void paintEvent(QPaintEvent *event) override
QString context() const
Get the current context for the grid.
virtual QSize sizeHint() const override
const QColor & color(ColorGroup group, ColorRole role) const
const T & at(int i) const
virtual 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.
QString tr(const char *sourceText, const char *disambiguation, int n)
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
QColor baseColor() const
Get the base color for the widget.
QgsNamedColorList * colors()
Gets the list of colors shown in the grid.
void drawRect(const QRectF &rectangle)
#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.
void setPen(const QColor &color)
void setBrush(const QBrush &brush)
void drawText(const QPointF &position, const QString &text)
virtual ~QgsColorSwatchGridAction()
QColor baseColor() const
Get 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.
virtual ~QgsColorSwatchGrid()
void adjust(int dx1, int dy1, int dx2, int dy2)
void setBaseColor(const QColor &baseColor)
Sets the base color for the widget.
void refreshColors()
Reload colors from scheme and redraws the widget.
const QPoint & pos() const
const_iterator constEnd() const
const_iterator constBegin() const
void setContext(const QString &context)
Sets the current context for the grid.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void setContext(const QString &context)
Sets the current context for the color grid.
QString context() const
Get 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