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 )
44 setMouseTracking(
true );
46 setFocusPolicy( Qt::StrongFocus );
47 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
62 return QSize( mWidth, calculateHeight() );
67 return QSize( mWidth, calculateHeight() );
85 mColors = mScheme->
fetchColors( mContext, mBaseColor );
95 QPainter painter(
this );
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;
124 if ( colorName.isEmpty() )
127 QColor color = mColors.at( colorIdx ).first;
128 colorName = QString(
tr(
"rgb(%1, %2, %3)" ) ).arg( color.red() ).arg( color.green() ).arg( color.blue() );
130 setToolTip( colorName );
135 setToolTip( QString() );
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 )
196 focusPreviousChild();
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 )
219 emit
colorChanged( mColors.at( mCurrentFocusBox ).first );
224 QWidget::keyPressEvent( event );
245 int QgsColorSwatchGrid::calculateHeight()
const 251 void QgsColorSwatchGrid::draw( QPainter &painter )
253 QPalette
pal = QPalette( qApp->palette() );
254 QColor headerBgColor = pal.color( QPalette::Mid );
255 QColor headerTextColor = pal.color( QPalette::BrightText );
256 QColor highlight = pal.color( QPalette::Highlight );
259 painter.setBrush( headerBgColor );
260 painter.setPen( Qt::NoPen );
261 painter.drawRect( QRect( 0, 0, width(),
LABEL_SIZE ) );
264 painter.setPen( headerTextColor );
266 Qt::AlignLeft | Qt::AlignVCenter, mScheme->
schemeName() );
269 QgsNamedColorList::iterator colorIt = mColors.begin();
271 for ( ; colorIt != mColors.end(); ++colorIt )
280 if ( mCurrentHoverBox == index )
283 swatchRect.adjust( -1, -1, 1, 1 );
287 if (( *colorIt ).first.alpha() != 255 )
289 QBrush checkBrush = QBrush( transparentBackground() );
290 painter.setPen( Qt::NoPen );
291 painter.setBrush( checkBrush );
292 painter.drawRect( swatchRect );
295 if ( mCurrentHoverBox == index )
297 if ( mDrawBoxDepressed )
299 painter.setPen( QColor( 100, 100, 100 ) );
304 painter.setPen( QColor( 220, 220, 220 ) );
307 else if ( mFocused && index == mCurrentFocusBox )
309 painter.setPen( highlight );
311 else if (( *colorIt ).first.name() == mBaseColor.name() )
314 painter.setPen( QColor( 75, 75, 75 ) );
318 painter.setPen( QColor( 197, 197, 197 ) );
321 painter.setBrush(( *colorIt ).first );
322 painter.drawRect( swatchRect );
328 const QPixmap& QgsColorSwatchGrid::transparentBackground()
330 static QPixmap transpBkgrd;
332 if ( transpBkgrd.isNull() )
338 int QgsColorSwatchGrid::swatchForPosition(
const QPoint &position )
const 362 : QWidgetAction( parent )
364 , mSuppressRecurse( false )
368 setDefaultWidget( mColorSwatchGrid );
369 connect( mColorSwatchGrid, SIGNAL(
colorChanged( QColor ) ),
this, SLOT( setColor( QColor ) ) );
371 connect(
this, SIGNAL( hovered() ),
this, SLOT( onHover() ) );
372 connect( mColorSwatchGrid, SIGNAL( hovered() ),
this, SLOT( onHover() ) );
375 setVisible( mColorSwatchGrid->
colors()->count() > 0 );
395 return mColorSwatchGrid->
context();
407 setVisible( mColorSwatchGrid->
colors()->count() > 0 );
410 void QgsColorSwatchGridAction::setColor(
const QColor &color )
420 void QgsColorSwatchGridAction::onHover()
424 if ( mSuppressRecurse )
431 mSuppressRecurse =
true;
432 mMenu->setActiveAction(
this );
433 mSuppressRecurse =
false;
void mouseReleaseEvent(QMouseEvent *event) override
void focusOutEvent(QFocusEvent *event) override
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
virtual QSize sizeHint() const override
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.
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
QgsNamedColorList * colors()
Gets the list of colors shown in the grid.
#define NUMBER_COLORS_PER_ROW
void hovered()
Emitted when mouse hovers over widget.
QString context() const
Get the current context for the grid.
QgsColorSwatchGridAction(QgsColorScheme *scheme, QMenu *menu=0, QString context=QString(), QWidget *parent=0)
Construct a new color swatch grid action.
virtual ~QgsColorSwatchGridAction()
void mousePressEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
QColor baseColor() const
Get the base color for the color grid.
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 setBaseColor(const QColor &baseColor)
Sets the base color for the widget.
QgsColorSwatchGrid(QgsColorScheme *scheme, QString context=QString(), QWidget *parent=0)
Construct a new color swatch grid.
void refreshColors()
Reload colors from scheme and redraws the widget.
void setContext(const QString &context)
Sets the current context for the grid.
QColor baseColor() const
Get the base color for the widget.
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