Krita Source Code Documentation
Loading...
Searching...
No Matches
KisColorButton Class Reference

A pushbutton to display or allow user selection of a color. More...

#include <kis_color_button.h>

+ Inheritance diagram for KisColorButton:

Classes

class  KisColorButtonPrivate
 

Public Slots

void setColor (const KoColor &c)
 

Signals

void changed (const KoColor &newColor)
 

Public Member Functions

KoColor color () const
 
KoColor defaultColor () const
 
bool isAlphaChannelEnabled () const
 
 KisColorButton (const KoColor &c, const KoColor &defaultColor, QWidget *parent=0)
 
 KisColorButton (const KoColor &c, QWidget *parent=0)
 
 KisColorButton (QWidget *parent=0)
 
QSize minimumSizeHint () const override
 
bool paletteViewEnabled () const
 paletteViewEnabled
 
void setAlphaChannelEnabled (bool alpha)
 
void setDefaultColor (const KoColor &c)
 
void setPaletteViewEnabled (bool enable)
 
QSize sizeHint () const override
 
 ~KisColorButton () override
 

Protected Member Functions

void dragEnterEvent (QDragEnterEvent *) override
 
void dropEvent (QDropEvent *) override
 
void keyPressEvent (QKeyEvent *e) override
 
void mouseMoveEvent (QMouseEvent *e) override
 
void mousePressEvent (QMouseEvent *e) override
 
void paintEvent (QPaintEvent *pe) override
 

Properties

bool alphaChannelEnabled
 
KoColor color
 
KoColor defaultColor
 

Private Attributes

KisColorButtonPrivate *const d
 

Detailed Description

A pushbutton to display or allow user selection of a color.

This widget can be used to display or allow user selection of a color.

See also
QColorDialog
KDE Color Button

Definition at line 26 of file kis_color_button.h.

Constructor & Destructor Documentation

◆ KisColorButton() [1/3]

KisColorButton::KisColorButton ( QWidget * parent = 0)
explicit

Creates a color button.

Definition at line 115 of file kis_color_button.cpp.

116 : QPushButton(parent)
117 , d(new KisColorButtonPrivate(this))
118{
119}
KisColorButtonPrivate *const d

◆ KisColorButton() [2/3]

KisColorButton::KisColorButton ( const KoColor & c,
QWidget * parent = 0 )
explicit

Creates a color button with an initial color c.

Definition at line 121 of file kis_color_button.cpp.

122 : QPushButton(parent)
123 , d(new KisColorButtonPrivate(this))
124{
125 d->col = c;
126}

References KisColorButton::KisColorButtonPrivate::col, and d.

◆ KisColorButton() [3/3]

KisColorButton::KisColorButton ( const KoColor & c,
const KoColor & defaultColor,
QWidget * parent = 0 )

Creates a color button with an initial color c and default color defaultColor.

Definition at line 128 of file kis_color_button.cpp.

129 : QPushButton(parent)
130 , d(new KisColorButtonPrivate(this))
131{
132 d->col = c;
134}
void setDefaultColor(const KoColor &c)

References KisColorButton::KisColorButtonPrivate::col, d, defaultColor, and setDefaultColor().

◆ ~KisColorButton()

KisColorButton::~KisColorButton ( )
override

Definition at line 136 of file kis_color_button.cpp.

137{
138 delete d;
139}

References d.

Member Function Documentation

◆ changed

void KisColorButton::changed ( const KoColor & newColor)
signal

Emitted when the color of the widget is changed, either with setColor() or via user selection.

◆ color()

KoColor KisColorButton::color ( ) const

Returns the currently chosen color.

Definition at line 141 of file kis_color_button.cpp.

142{
143 return d->col;
144}

References KisColorButton::KisColorButtonPrivate::col, and d.

◆ defaultColor()

KoColor KisColorButton::defaultColor ( ) const

Returns the default color or an invalid color if no default color is set.

Definition at line 173 of file kis_color_button.cpp.

References d, and KisColorButton::KisColorButtonPrivate::m_defaultColor.

◆ dragEnterEvent()

void KisColorButton::dragEnterEvent ( QDragEnterEvent * event)
overrideprotected

Definition at line 261 of file kis_color_button.cpp.

262{
263 event->setAccepted(_k_canDecode(event->mimeData()) && isEnabled());
264}
bool _k_canDecode(const QMimeData *mimeData)

References _k_canDecode().

◆ dropEvent()

void KisColorButton::dropEvent ( QDropEvent * event)
overrideprotected

Definition at line 266 of file kis_color_button.cpp.

267{
268 QColor c = _k_fromMimeData(event->mimeData());
269 if (c.isValid()) {
270 KoColor col;
271 col.fromQColor(c);
272 setColor(col);
273 }
274}
void setColor(const KoColor &c)
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
QColor _k_fromMimeData(const QMimeData *mimeData)

References _k_fromMimeData(), KoColor::fromQColor(), and setColor().

◆ isAlphaChannelEnabled()

bool KisColorButton::isAlphaChannelEnabled ( ) const

Returns true if the user is allowed to change the alpha component.

Definition at line 158 of file kis_color_button.cpp.

References d, and KisColorButton::KisColorButtonPrivate::m_alphaChannel.

◆ keyPressEvent()

void KisColorButton::keyPressEvent ( QKeyEvent * e)
overrideprotected

Definition at line 276 of file kis_color_button.cpp.

277{
278 int key = e->key() | e->modifiers();
279
280 if (QKeySequence::keyBindings(QKeySequence::Copy).contains(key)) {
281 QMimeData *mime = new QMimeData;
282 _k_populateMimeData(mime, color());
283 QApplication::clipboard()->setMimeData(mime, QClipboard::Clipboard);
284 } else if (QKeySequence::keyBindings(QKeySequence::Paste).contains(key)) {
285 QColor color = _k_fromMimeData(QApplication::clipboard()->mimeData(QClipboard::Clipboard));
286 KoColor col;
287 col.fromQColor(color);
288 setColor(col);
289 } else {
290 QPushButton::keyPressEvent(e);
291 }
292}
void _k_populateMimeData(QMimeData *mimeData, const KoColor &color)

References _k_fromMimeData(), _k_populateMimeData(), color, KoColor::fromQColor(), and setColor().

◆ minimumSizeHint()

QSize KisColorButton::minimumSizeHint ( ) const
override

Definition at line 254 of file kis_color_button.cpp.

255{
256 QStyleOptionButton opt;
257 d->initStyleOption(&opt);
258 return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(3, 3), this);
259}
void initStyleOption(QStyleOptionButton *opt) const

References d, and KisColorButton::KisColorButtonPrivate::initStyleOption().

◆ mouseMoveEvent()

void KisColorButton::mouseMoveEvent ( QMouseEvent * e)
overrideprotected

Definition at line 300 of file kis_color_button.cpp.

301{
302 if ((e->buttons() & Qt::LeftButton) &&
303 (e->pos() - d->mPos).manhattanLength() > QApplication::startDragDistance()) {
304 _k_createDrag(color(), this)->exec();
305 setDown(false);
306 }
307}
QDrag * _k_createDrag(const KoColor &color, QObject *dragsource)

References _k_createDrag(), color, d, and KisColorButton::KisColorButtonPrivate::mPos.

◆ mousePressEvent()

void KisColorButton::mousePressEvent ( QMouseEvent * e)
overrideprotected

Definition at line 294 of file kis_color_button.cpp.

295{
296 d->mPos = e->pos();
297 QPushButton::mousePressEvent(e);
298}

References d, and KisColorButton::KisColorButtonPrivate::mPos.

◆ paintEvent()

void KisColorButton::paintEvent ( QPaintEvent * pe)
overrideprotected

Definition at line 196 of file kis_color_button.cpp.

197{
198 QPainter painter(this);
199 QStyle *style = QWidget::style();
200
201 //First, we need to draw the bevel.
202 QStyleOptionButton butOpt;
203 d->initStyleOption(&butOpt);
204 style->drawControl(QStyle::CE_PushButtonBevel, &butOpt, &painter, this);
205
206 //OK, now we can muck around with drawing out pretty little color box
207 //First, sort out where it goes
208 QRect labelRect = style->subElementRect(QStyle::SE_PushButtonContents,
209 &butOpt, this);
210 int shift = style->pixelMetric(QStyle::PM_ButtonMargin, &butOpt, this) / 2;
211 labelRect.adjust(shift, shift, -shift, -shift);
212 int x, y, w, h;
213 labelRect.getRect(&x, &y, &w, &h);
214
215 if (isChecked() || isDown()) {
216 x += style->pixelMetric(QStyle::PM_ButtonShiftHorizontal, &butOpt, this);
217 y += style->pixelMetric(QStyle::PM_ButtonShiftVertical, &butOpt, this);
218 }
219
220 QColor fillCol = isEnabled() ? d->col.toQColor() : palette().color(backgroundRole());
221 qDrawShadePanel(&painter, x, y, w, h, palette(), true, 1, NULL);
222 if (fillCol.isValid()) {
223 const QRect rect(x + 1, y + 1, w - 2, h - 2);
224 if (fillCol.alpha() < 255) {
225 QPixmap chessboardPattern(16, 16);
226 QPainter patternPainter(&chessboardPattern);
227 patternPainter.fillRect(0, 0, 8, 8, Qt::black);
228 patternPainter.fillRect(8, 8, 8, 8, Qt::black);
229 patternPainter.fillRect(0, 8, 8, 8, Qt::white);
230 patternPainter.fillRect(8, 0, 8, 8, Qt::white);
231 patternPainter.end();
232 painter.fillRect(rect, QBrush(chessboardPattern));
233 }
234 painter.fillRect(rect, fillCol);
235 }
236
237 if (hasFocus()) {
238 QRect focusRect = style->subElementRect(QStyle::SE_PushButtonFocusRect, &butOpt, this);
239 QStyleOptionFocusRect focusOpt;
240 focusOpt.initFrom(this);
241 focusOpt.rect = focusRect;
242 focusOpt.backgroundColor = palette().window().color();
243 style->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOpt, &painter, this);
244 }
245}
void toQColor(QColor *c) const
a convenience method for the above.
Definition KoColor.cpp:198
rgba palette[MAX_PALETTE]
Definition palette.c:35

References KisColorButton::KisColorButtonPrivate::col, d, KisColorButton::KisColorButtonPrivate::initStyleOption(), palette, and KoColor::toQColor().

◆ paletteViewEnabled()

bool KisColorButton::paletteViewEnabled ( ) const

paletteViewEnabled

Returns
whether the palette is enabled.

Definition at line 168 of file kis_color_button.cpp.

References d, and KisColorButton::KisColorButtonPrivate::m_palette.

◆ setAlphaChannelEnabled()

void KisColorButton::setAlphaChannelEnabled ( bool alpha)

When set to true, allow the user to change the alpha component of the color. The default value is false.

Definition at line 153 of file kis_color_button.cpp.

154{
155 d->m_alphaChannel = alpha;
156}

References d, and KisColorButton::KisColorButtonPrivate::m_alphaChannel.

◆ setColor

void KisColorButton::setColor ( const KoColor & c)
slot

Sets the current color to c.

Definition at line 146 of file kis_color_button.cpp.

147{
148 d->col = c;
149 update();
150 Q_EMIT changed(d->col);
151}
void changed(const KoColor &newColor)
bool update(QSpinBox *spinBox)

References changed(), KisColorButton::KisColorButtonPrivate::col, and d.

◆ setDefaultColor()

void KisColorButton::setDefaultColor ( const KoColor & c)

◆ setPaletteViewEnabled()

void KisColorButton::setPaletteViewEnabled ( bool enable)

Allow having a palette.

Definition at line 163 of file kis_color_button.cpp.

164{
165 d->m_palette = enable;
166}

References d, and KisColorButton::KisColorButtonPrivate::m_palette.

◆ sizeHint()

QSize KisColorButton::sizeHint ( ) const
override

Definition at line 247 of file kis_color_button.cpp.

248{
249 QStyleOptionButton opt;
250 d->initStyleOption(&opt);
251 return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(40, 15), this);
252}

References d, and KisColorButton::KisColorButtonPrivate::initStyleOption().

Member Data Documentation

◆ d

KisColorButtonPrivate* const KisColorButton::d
private

Definition at line 118 of file kis_color_button.h.

Property Documentation

◆ alphaChannelEnabled

bool KisColorButton::alphaChannelEnabled
readwrite

Definition at line 36 of file kis_color_button.h.

◆ color

KoColor KisColorButton::color
readwrite

QtCreator treats KoColor as a QColor in incorrect way, so just disable using them in QtCreator https://bugs.kde.org/show_bug.cgi?id=368483

Definition at line 34 of file kis_color_button.h.

◆ defaultColor

KoColor KisColorButton::defaultColor
readwrite

Definition at line 35 of file kis_color_button.h.


The documentation for this class was generated from the following files: