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

A widget for selecting two related colors. More...

#include <KoDualColorButton.h>

+ Inheritance diagram for KoDualColorButton:

Public Types

enum  Selection { Foreground , Background }
 

Public Slots

QColor getColorFromDisplayRenderer (KoColor c)
 getColorFromDisplayRenderer convenience function to get the right qcolor from the display renderer, including checking whether the display renderer actually exists.
 
void openBackgroundDialog ()
 
void openForegroundDialog ()
 
void setBackgroundColor (const KoColor &color)
 
void setColorSpace (const KoColorSpace *cs)
 setColorSpace set ColorSpace so we can lock the selector. Right now this'll be changed per view-change.
 
void setDisplayRenderer (const KoColorDisplayRendererInterface *displayRenderer=KoDumbColorDisplayRenderer::instance())
 
void setForegroundColor (const KoColor &color)
 
void setPopDialog (bool popDialog)
 
void slotSetForeGroundColorFromDialog (const KoColor color)
 

Signals

void backgroundColorChanged (const KoColor &color)
 
void foregroundColorChanged (const KoColor &color)
 

Public Member Functions

KoColor backgroundColor () const
 
KoColor foregroundColor () const
 
void init (KoDualColorButton *q)
 
 KoDualColorButton (const KoColor &foregroundColor, const KoColor &backgroundColor, const KoColorDisplayRendererInterface *displayRenderer, QWidget *parent=0, QWidget *dialogParent=0)
 
 KoDualColorButton (const KoColor &foregroundColor, const KoColor &backgroundColor, QWidget *parent=0, QWidget *dialogParent=0)
 
bool popDialog () const
 
 Private (const KoColor &fgColor, const KoColor &bgColor, QWidget *_dialogParent, const KoColorDisplayRendererInterface *_displayRenderer)
 
QSize sizeHint () const override
 
void updateArrows ()
 
 ~KoDualColorButton () override
 

Public Attributes

QPixmap arrowBitmap
 
KoColor backgroundColor
 
KisDlgInternalColorSelectorcolorSelectorDialog
 
QWidget * dialogParent
 
QPointer< const KoColorDisplayRendererInterfacedisplayRenderer
 
bool dragFlag
 
QPoint dragPosition
 
KoColor foregroundColor
 
bool miniCtlFlag
 
bool popDialog
 
QPixmap resetPixmap
 
Selection tmpSelection
 

Protected Member Functions

void changeEvent (QEvent *event) override
 
void dragEnterEvent (QDragEnterEvent *event) override
 
void dropEvent (QDropEvent *event) override
 
bool event (QEvent *event) override
 
virtual void metrics (QRect &foregroundRect, QRect &backgroundRect)
 
void mouseMoveEvent (QMouseEvent *event) override
 
void mousePressEvent (QMouseEvent *event) override
 
void mouseReleaseEvent (QMouseEvent *event) override
 
void paintEvent (QPaintEvent *event) override
 

Private Attributes

Private *const d
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Additional Inherited Members

- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Detailed Description

A widget for selecting two related colors.

KoDualColorButton allows the user to select two cascaded colors (usually a foreground and background color). Other features include drag and drop from other KDE color widgets, a reset to black and white control, and a swap colors control.

When the user clicks on the foreground or background rectangle the rectangle is first sunken and the selectionChanged() signal is emitted. Further clicks will present a color dialog and Q_EMIT either the foregroundColorChanged() or backgroundColorChanged() if a new color is selected.

Note: With drag and drop when dropping a color the current selected color will be set, while when dragging a color it will use whatever color rectangle the mouse was pressed inside.

Author
Daniel M. Duley mosfe.nosp@m.t@kd.nosp@m.e.org

Definition at line 30 of file KoDualColorButton.cpp.

Member Enumeration Documentation

◆ Selection

Enumerator
Foreground 
Background 

Definition at line 47 of file KoDualColorButton.h.

Constructor & Destructor Documentation

◆ KoDualColorButton() [1/2]

KoDualColorButton::KoDualColorButton ( const KoColor & foregroundColor,
const KoColor & backgroundColor,
QWidget * parent = 0,
QWidget * dialogParent = 0 )

Constructs a new KoDualColorButton with the supplied foreground and background colors.

Parameters
parentThe parent widget of the KoDualColorButton.
dialogParentThe parent widget of the color selection dialog.

Definition at line 100 of file KoDualColorButton.cpp.

101 : QWidget( parent ),
105{
106 d->init(this);
107}
static KoColorDisplayRendererInterface * instance()

References d.

◆ KoDualColorButton() [2/2]

KoDualColorButton::KoDualColorButton ( const KoColor & foregroundColor,
const KoColor & backgroundColor,
const KoColorDisplayRendererInterface * displayRenderer,
QWidget * parent = 0,
QWidget * dialogParent = 0 )

Definition at line 109 of file KoDualColorButton.cpp.

112 : QWidget( parent ),
116{
117 d->init(this);
118}
QPointer< const KoColorDisplayRendererInterface > displayRenderer

References d.

◆ ~KoDualColorButton()

KoDualColorButton::~KoDualColorButton ( )
override

Destroys the KoDualColorButton.

Definition at line 120 of file KoDualColorButton.cpp.

121{
122 delete d;
123}

References d.

Member Function Documentation

◆ backgroundColor()

KoColor KoDualColorButton::backgroundColor ( ) const

Returns the current background color.

◆ backgroundColorChanged

void KoDualColorButton::backgroundColorChanged ( const KoColor & color)
signal

Emitted when the background color is changed.

◆ changeEvent()

void KoDualColorButton::changeEvent ( QEvent * event)
overrideprotected

Definition at line 404 of file KoDualColorButton.cpp.

405{
406 QWidget::changeEvent(event);
407
408 switch (event->type()) {
409 case QEvent::StyleChange:
410 case QEvent::PaletteChange:
411 d->updateArrows();
412 default:
413 break;
414 }
415}
bool event(QEvent *event) override

References d, and event().

◆ dragEnterEvent()

void KoDualColorButton::dragEnterEvent ( QDragEnterEvent * event)
overrideprotected

Definition at line 233 of file KoDualColorButton.cpp.

234{
235 event->setAccepted( isEnabled() && KColorMimeData::canDecode( event->mimeData() ) );
236}

References event().

◆ dropEvent()

void KoDualColorButton::dropEvent ( QDropEvent * event)
overrideprotected

Definition at line 238 of file KoDualColorButton.cpp.

239{
240 Q_UNUSED(event);
241 /* QColor color = KColorMimeData::fromMimeData( event->mimeData() );
242
243 if ( color.isValid() ) {
244 if ( d->selection == Foreground ) {
245 d->foregroundColor = color;
246 Q_EMIT foregroundColorChanged( color );
247 } else {
248 d->backgroundColor = color;
249 Q_EMIT backgroundColorChanged( color );
250 }
251
252 update();
253 }
254*/
255}

References event().

◆ event()

bool KoDualColorButton::event ( QEvent * event)
overrideprotected

Definition at line 417 of file KoDualColorButton.cpp.

418{
419 if (event->type() == QEvent::ToolTip) {
420 QRect foregroundRect;
421 QRect backgroundRect;
422 metrics( foregroundRect, backgroundRect );
423
424 if (this->mapFromGlobal(QCursor::pos()).x() < backgroundRect.x() ) {
425 if (this->mapFromGlobal(QCursor::pos()).y() < backgroundRect.y()){
426 this->setToolTip(i18n("Foreground color selector"));
427 }
428 else{
429 this->setToolTip(i18n("Set foreground and background colors to black and white"));
430 }
431 }
432 else {
433 if (this->mapFromGlobal(QCursor::pos()).y() < backgroundRect.y() ) {
434 this->setToolTip(i18n("Swap foreground and background colors"));
435 }
436 else{
437 this->setToolTip(i18n("Background color selector"));
438 }
439 }
440 }
441 return QWidget::event(event);
442
443}
virtual void metrics(QRect &foregroundRect, QRect &backgroundRect)

References event(), and metrics().

◆ foregroundColor()

KoColor KoDualColorButton::foregroundColor ( ) const

Returns the current foreground color.

◆ foregroundColorChanged

void KoDualColorButton::foregroundColorChanged ( const KoColor & color)
signal

Emitted when the foreground color is changed.

◆ getColorFromDisplayRenderer

QColor KoDualColorButton::getColorFromDisplayRenderer ( KoColor c)
slot

getColorFromDisplayRenderer convenience function to get the right qcolor from the display renderer, including checking whether the display renderer actually exists.

Parameters
cthe kocolor to convert.
Returns
the qcolor to use for display.

Definition at line 185 of file KoDualColorButton.cpp.

186{
187 QColor col;
188 if (d->displayRenderer) {
189 c.convertTo(d->displayRenderer->getPaintingColorSpace());
190 col = d->displayRenderer->toQColor(c);
191 } else {
192 col = c.toQColor();
193 }
194 return col;
195}
void convertTo(const KoColorSpace *cs, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags)
Definition KoColor.cpp:136
void toQColor(QColor *c) const
a convenience method for the above.
Definition KoColor.cpp:198

References KoColor::convertTo(), d, and KoColor::toQColor().

◆ init()

void KoDualColorButton::init ( KoDualColorButton * q)

◆ metrics()

void KoDualColorButton::metrics ( QRect & foregroundRect,
QRect & backgroundRect )
protectedvirtual

Sets the supplied rectangles to the proper size and position for the current widget size. You can reimplement this to change the layout of the widget. Restrictions are that the swap control will always be at the top right, the reset control will always be at the bottom left, and you must leave at least a 14x14 space in those corners.

Definition at line 202 of file KoDualColorButton.cpp.

203{
204 foregroundRect = QRect( 0, 0, width() - 14, height() - 14 );
205 backgroundRect = QRect( 14, 14, width() - 14, height() - 14 );
206}

◆ mouseMoveEvent()

void KoDualColorButton::mouseMoveEvent ( QMouseEvent * event)
overrideprotected

Definition at line 323 of file KoDualColorButton.cpp.

324{
325 if ( !d->miniCtlFlag ) {
326 int delay = QApplication::startDragDistance();
327
328 if ( event->x() >= d->dragPosition.x() + delay || event->x() <= d->dragPosition.x() - delay ||
329 event->y() >= d->dragPosition.y() + delay || event->y() <= d->dragPosition.y() - delay ) {
330 KColorMimeData::createDrag( d->tmpSelection == Foreground ?
331 getColorFromDisplayRenderer(d->foregroundColor) :
333 this )->exec();
334 d->dragFlag = true;
335 }
336 }
337}
QColor getColorFromDisplayRenderer(KoColor c)
getColorFromDisplayRenderer convenience function to get the right qcolor from the display renderer,...

References d, event(), Foreground, and getColorFromDisplayRenderer().

◆ mousePressEvent()

void KoDualColorButton::mousePressEvent ( QMouseEvent * event)
overrideprotected

Definition at line 278 of file KoDualColorButton.cpp.

279{
280 QRect foregroundRect;
281 QRect backgroundRect;
282
283 metrics( foregroundRect, backgroundRect );
284
285 d->dragPosition = event->pos();
286
287 d->dragFlag = false;
288
289 if ( foregroundRect.contains( d->dragPosition ) ) {
290 d->tmpSelection = Foreground;
291 d->miniCtlFlag = false;
292 }
293 else if( backgroundRect.contains( d->dragPosition ) ) {
294 d->tmpSelection = Background;
295 d->miniCtlFlag = false;
296 }
297 else if ( event->pos().x() > foregroundRect.width() ) {
298 // We handle the swap and reset controls as soon as the mouse is
299 // is pressed and ignore further events on this click (mosfet).
300
301 KoColor tmp = d->foregroundColor;
302 d->foregroundColor = d->backgroundColor;
303 d->backgroundColor = tmp;
304
305 Q_EMIT backgroundColorChanged( d->backgroundColor );
306 Q_EMIT foregroundColorChanged( d->foregroundColor );
307
308 d->miniCtlFlag = true;
309 }
310 else if ( event->pos().x() < backgroundRect.x() ) {
311 d->foregroundColor = d->displayRenderer->approximateFromRenderedQColor(Qt::black);
312 d->backgroundColor = d->displayRenderer->approximateFromRenderedQColor(Qt::white);
313
314 Q_EMIT backgroundColorChanged( d->backgroundColor );
315 Q_EMIT foregroundColorChanged( d->foregroundColor );
316
317 d->miniCtlFlag = true;
318 }
319 update();
320}
void foregroundColorChanged(const KoColor &color)
void backgroundColorChanged(const KoColor &color)
bool update(QSpinBox *spinBox)

References Background, backgroundColorChanged(), d, event(), Foreground, foregroundColorChanged(), and metrics().

◆ mouseReleaseEvent()

void KoDualColorButton::mouseReleaseEvent ( QMouseEvent * event)
overrideprotected

Definition at line 339 of file KoDualColorButton.cpp.

340{
341 d->dragFlag = false;
342
343 if ( d->miniCtlFlag )
344 return;
345
346 d->miniCtlFlag = false;
347
348 QRect foregroundRect;
349 QRect backgroundRect;
350 metrics( foregroundRect, backgroundRect );
351
352 KConfigGroup cfg = KSharedConfig::openConfig()->group("colorselector");
353 bool usePlatformDialog = cfg.readEntry("UsePlatformColorDialog", false);
354
355 if (foregroundRect.contains( event->pos())) {
356 if (d->tmpSelection == Foreground) {
357 if (d->popDialog) {
358 if (usePlatformDialog) {
359 QColor c = d->foregroundColor.toQColor();
360 c = QColorDialog::getColor(c, this);
361 if (c.isValid()) {
362 d->foregroundColor = d->displayRenderer->approximateFromRenderedQColor(c);
363 Q_EMIT foregroundColorChanged(d->foregroundColor);
364 }
365 }
366 else {
367 d->colorSelectorDialog->setPreviousColor(d->foregroundColor);
368 d->colorSelectorDialog->show();
369 }
370 }
371 }
372 else {
373 d->foregroundColor = d->backgroundColor;
374 Q_EMIT foregroundColorChanged( d->foregroundColor );
375 }
376 }
377 else if (backgroundRect.contains( event->pos())) {
378 if(d->tmpSelection == Background ) {
379 if( d->popDialog) {
380 if (usePlatformDialog) {
381 QColor c = d->backgroundColor.toQColor();
382 c = QColorDialog::getColor(c, this);
383 if (c.isValid()) {
384 d->backgroundColor = d->displayRenderer->approximateFromRenderedQColor(c);
385 Q_EMIT backgroundColorChanged(d->backgroundColor);
386 }
387 }
388 else {
389 KoColor c = d->backgroundColor;
390 c = KisDlgInternalColorSelector::getModalColorDialog(c, this, d->colorSelectorDialog->windowTitle());
391 d->backgroundColor = c;
392 Q_EMIT backgroundColorChanged(d->backgroundColor);
393 }
394 }
395 } else {
396 d->backgroundColor = d->foregroundColor;
397 Q_EMIT backgroundColorChanged( d->backgroundColor );
398 }
399 }
400
401 update();
402}
static KoColor getModalColorDialog(const KoColor color, QWidget *parent=0, QString caption=QString())
getModalColorDialog Execute this dialog modally. The function returns the KoColor you want.

References Background, backgroundColorChanged(), d, event(), Foreground, foregroundColorChanged(), KisDlgInternalColorSelector::getModalColorDialog(), and metrics().

◆ openBackgroundDialog

void KoDualColorButton::openBackgroundDialog ( )
slot

Definition at line 271 of file KoDualColorButton.cpp.

271 {
272 KoColor c = d->backgroundColor;
273 c = KisDlgInternalColorSelector::getModalColorDialog(c, this, d->colorSelectorDialog->windowTitle());
274 d->backgroundColor = c;
275 Q_EMIT backgroundColorChanged(d->backgroundColor);
276}

References backgroundColorChanged(), d, and KisDlgInternalColorSelector::getModalColorDialog().

◆ openForegroundDialog

void KoDualColorButton::openForegroundDialog ( )
slot

Definition at line 265 of file KoDualColorButton.cpp.

265 {
266 d->colorSelectorDialog->setPreviousColor(d->foregroundColor);
267 d->colorSelectorDialog->show();
268 update();
269}

References d.

◆ paintEvent()

void KoDualColorButton::paintEvent ( QPaintEvent * event)
overrideprotected

Definition at line 208 of file KoDualColorButton.cpp.

209{
210 QRect foregroundRect;
211 QRect backgroundRect;
212
213 QPainter painter( this );
214
215 metrics( foregroundRect, backgroundRect );
216
217 QBrush defBrush = palette().brush( QPalette::Button );
218 QBrush foregroundBrush( getColorFromDisplayRenderer(d->foregroundColor), Qt::SolidPattern );
219 QBrush backgroundBrush( getColorFromDisplayRenderer(d->backgroundColor), Qt::SolidPattern );
220
221 qDrawShadeRect( &painter, backgroundRect, palette(), false, 1, 0,
222 isEnabled() ? &backgroundBrush : &defBrush );
223
224 qDrawShadeRect( &painter, foregroundRect, palette(), false, 1, 0,
225 isEnabled() ? &foregroundBrush : &defBrush );
226
227 painter.setPen( palette().color( QPalette::Shadow ) );
228
229 painter.drawPixmap( foregroundRect.right() + 2, 1, d->arrowBitmap );
230 painter.drawPixmap( 1, foregroundRect.bottom() + 2, d->resetPixmap );
231}
rgba palette[MAX_PALETTE]
Definition palette.c:35

References d, getColorFromDisplayRenderer(), metrics(), and palette.

◆ popDialog()

bool KoDualColorButton::popDialog ( ) const

Returns if a dialog with a color chooser will be popped up when clicking If false then you could/should connect to the pleasePopDialog signal and pop your own dialog. Just set the current color afterwards.

◆ Private()

KoDualColorButton::Private ( const KoColor & fgColor,
const KoColor & bgColor,
QWidget * _dialogParent,
const KoColorDisplayRendererInterface * _displayRenderer )
inline

Definition at line 33 of file KoDualColorButton.cpp.

36 : dialogParent(_dialogParent)
37 , dragFlag( false )
38 , miniCtlFlag( false )
39 , foregroundColor(fgColor)
40 , backgroundColor(bgColor)
41
42 , displayRenderer(_displayRenderer)
43 {
45 resetPixmap = QPixmap( (const char **)dcolorreset_xpm );
46
47 popDialog = true;
48 }

◆ setBackgroundColor

void KoDualColorButton::setBackgroundColor ( const KoColor & color)
slot

Sets the background color.

Definition at line 159 of file KoDualColorButton.cpp.

160{
161 d->backgroundColor = color;
162 update();
163}

References d.

◆ setColorSpace

void KoDualColorButton::setColorSpace ( const KoColorSpace * cs)
slot

setColorSpace set ColorSpace so we can lock the selector. Right now this'll be changed per view-change.

Parameters
cs

Definition at line 180 of file KoDualColorButton.cpp.

181{
182 d->colorSelectorDialog->lockUsedColorSpace(cs);
183}

References d.

◆ setDisplayRenderer

void KoDualColorButton::setDisplayRenderer ( const KoColorDisplayRendererInterface * displayRenderer = KoDumbColorDisplayRenderer::instance())
slot

Definition at line 165 of file KoDualColorButton.cpp.

166{
167 if (d->displayRenderer && d->displayRenderer != KoDumbColorDisplayRenderer::instance()) {
168 d->displayRenderer->disconnect(this);
169 }
170 if (displayRenderer) {
171 d->displayRenderer = displayRenderer;
172 d->colorSelectorDialog->setDisplayRenderer(displayRenderer);
173 connect(d->displayRenderer, SIGNAL(destroyed()), this, SLOT(setDisplayRenderer()), Qt::UniqueConnection);
174 connect(d->displayRenderer, SIGNAL(displayConfigurationChanged()), this, SLOT(update()));
175 } else {
176 d->displayRenderer = KoDumbColorDisplayRenderer::instance();
177 }
178}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer=KoDumbColorDisplayRenderer::instance())

References connect(), d, displayRenderer, KoDumbColorDisplayRenderer::instance(), and setDisplayRenderer().

◆ setForegroundColor

void KoDualColorButton::setForegroundColor ( const KoColor & color)
slot

Sets the foreground color.

The internal color selector might Q_EMIT the color of a different profile, so we should break this cycling dependency somehow.

Definition at line 145 of file KoDualColorButton.cpp.

146{
147 d->foregroundColor = color;
148 {
153 KisSignalsBlocker b(d->colorSelectorDialog);
154 d->colorSelectorDialog->slotColorUpdated(color);
155 }
156 update();
157}

References d.

◆ setPopDialog

void KoDualColorButton::setPopDialog ( bool popDialog)
slot

Sets if a dialog with a color chooser should be popped up when clicking If you set this to false then you could connect to the pleasePopDialog signal and pop your own dialog. Just set the current color afterwards.

Definition at line 197 of file KoDualColorButton.cpp.

198{
199 d->popDialog = popDialog;
200}

References d, and popDialog.

◆ sizeHint()

QSize KoDualColorButton::sizeHint ( ) const
override

Returns the minimum size needed to display the widget and all its controls.

Definition at line 140 of file KoDualColorButton.cpp.

141{
142 return QSize(34, 34);
143}

◆ slotSetForeGroundColorFromDialog

void KoDualColorButton::slotSetForeGroundColorFromDialog ( const KoColor color)
slot

Definition at line 257 of file KoDualColorButton.cpp.

258{
259 d->foregroundColor = color;
260 update();
261 Q_EMIT foregroundColorChanged(d->foregroundColor);
262}

References d, and foregroundColorChanged().

◆ updateArrows()

void KoDualColorButton::updateArrows ( )
inline

Definition at line 50 of file KoDualColorButton.cpp.

50 {
51 arrowBitmap = QPixmap(12,12);
52 arrowBitmap.fill(Qt::transparent);
53
54 QPainter p(&arrowBitmap);
55 p.setPen(dialogParent->palette().windowText().color());
56
57 // arrow pointing left
58 p.drawLine(0, 3, 7, 3);
59 p.drawLine(1, 2, 1, 4);
60 p.drawLine(2, 1, 2, 5);
61 p.drawLine(3, 0, 3, 6);
62
63 // arrow pointing down
64 p.drawLine(8, 4, 8, 11);
65 p.drawLine(5, 8, 11, 8);
66 p.drawLine(6, 9, 10, 9);
67 p.drawLine(7, 10, 9, 10);
68 }
const Params2D p

References p.

Member Data Documentation

◆ arrowBitmap

QPixmap KoDualColorButton::arrowBitmap

Definition at line 72 of file KoDualColorButton.cpp.

◆ backgroundColor

KoColor KoDualColorButton::backgroundColor
readwrite

Definition at line 76 of file KoDualColorButton.cpp.

◆ colorSelectorDialog

KisDlgInternalColorSelector* KoDualColorButton::colorSelectorDialog

Definition at line 77 of file KoDualColorButton.cpp.

◆ d

Private* const KoDualColorButton::d
private

Definition at line 167 of file KoDualColorButton.h.

◆ dialogParent

QWidget* KoDualColorButton::dialogParent

Definition at line 70 of file KoDualColorButton.cpp.

◆ displayRenderer

QPointer<const KoColorDisplayRendererInterface> KoDualColorButton::displayRenderer

Definition at line 81 of file KoDualColorButton.cpp.

◆ dragFlag

bool KoDualColorButton::dragFlag

Definition at line 74 of file KoDualColorButton.cpp.

◆ dragPosition

QPoint KoDualColorButton::dragPosition

Definition at line 78 of file KoDualColorButton.cpp.

◆ foregroundColor

KoColor KoDualColorButton::foregroundColor
readwrite

Definition at line 75 of file KoDualColorButton.cpp.

◆ miniCtlFlag

bool KoDualColorButton::miniCtlFlag

Definition at line 74 of file KoDualColorButton.cpp.

◆ popDialog

bool KoDualColorButton::popDialog
readwrite

Definition at line 80 of file KoDualColorButton.cpp.

◆ resetPixmap

QPixmap KoDualColorButton::resetPixmap

Definition at line 73 of file KoDualColorButton.cpp.

◆ tmpSelection

Selection KoDualColorButton::tmpSelection

Definition at line 79 of file KoDualColorButton.cpp.


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