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

#include <KisAsyncColorSamplerHelper.h>

+ Inheritance diagram for KisAsyncColorSamplerHelper:

Classes

struct  Private
 

Signals

void sigColorSelected (const KoColor &color)
 
void sigFinalColorSelected (const KoColor &color)
 
void sigRawColorSelected (const KoColor &color)
 
void sigRequestCursor (const QCursor &cursor)
 
void sigRequestCursorReset ()
 
void sigRequestUpdateOutline ()
 

Public Member Functions

void activate (KisToolUtils::ColorSamplerSource sampleSource, bool pickFgColor)
 
QRectF colorPreviewDocRect (const QPointF &docPoint)
 
void continueAction (const QPointF &docPoint)
 
void deactivate ()
 
void endAction ()
 
bool isActive () const
 
 KisAsyncColorSamplerHelper (KisCanvas2 *canvas)
 
void paint (QPainter &gc, const KoViewConverter &converter)
 
void setUpdateGlobalColor (bool value)
 
void startAction (const QPointF &docPoint, int radius, int blend)
 
void updateCursor (KisToolUtils::ColorSamplerSource sampleSource, bool pickFgColor)
 
bool updateGlobalColor () const
 
 ~KisAsyncColorSamplerHelper () override
 

Private Slots

void activateDelayedPreview ()
 
void slotAddSamplingJob (const QPointF &docPoint)
 
void slotCanvasZoomPreviewUpdated (const QImage &canvasImage, QRect canvasRect)
 
void slotColorSamplingFinished (const KoColor &rawColor)
 
void slotUpdateBgColor ()
 

Private Member Functions

void activatePreview ()
 
void paintCircle (QPainter &gc, const QRectF &viewRectF, const QColor &currentColor, const QColor &baseColor)
 
void paintCircleCanvasPreview (QPainter &gc, const QRectF &viewRectF, const QPainterPath &clip)
 
void paintCircleCrosshair (QPainter &gc, const QRectF &viewRectF, const QColor &currentColor)
 
void paintCircleReferenceImagePreview (QPainter &gc, const QRectF &viewRectF, const QPainterPath &clip)
 
void paintRectangle (QPainter &gc, const QRectF &viewRectF, const QColor &currentColor, const QColor &baseColor)
 
void prepareZoomPreview (const QRectF &docRect)
 

Private Attributes

QScopedPointer< Private > m_d
 

Detailed Description

Definition at line 24 of file KisAsyncColorSamplerHelper.h.

Constructor & Destructor Documentation

◆ KisAsyncColorSamplerHelper()

KisAsyncColorSamplerHelper::KisAsyncColorSamplerHelper ( KisCanvas2 * canvas)

Definition at line 278 of file KisAsyncColorSamplerHelper.cpp.

279 : m_d(new Private(canvas))
280{
281 using namespace std::placeholders; // For _1 placeholder
282 std::function<void(QPointF)> callback =
284 m_d->samplingCompressor.reset(
286
287 m_d->activationDelayTimer.setInterval(100);
288 m_d->activationDelayTimer.setSingleShot(true);
289 connect(&m_d->activationDelayTimer, SIGNAL(timeout()), this, SLOT(activateDelayedPreview()));
290 connect(m_d->canvas->displayColorConverter(), SIGNAL(displayConfigurationChanged()), this, SLOT(slotUpdateBgColor()));
292}
void slotAddSamplingJob(const QPointF &docPoint)
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
KisSignalCompressorWithParam< QPointF > SamplingCompressor

References activateDelayedPreview(), KisSignalCompressor::FIRST_ACTIVE, m_d, slotAddSamplingJob(), slotUpdateBgColor(), and void().

◆ ~KisAsyncColorSamplerHelper()

KisAsyncColorSamplerHelper::~KisAsyncColorSamplerHelper ( )
override

Definition at line 294 of file KisAsyncColorSamplerHelper.cpp.

295{
297}
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130

References KIS_SAFE_ASSERT_RECOVER_NOOP, and m_d.

Member Function Documentation

◆ activate()

void KisAsyncColorSamplerHelper::activate ( KisToolUtils::ColorSamplerSource sampleSource,
bool pickFgColor )

Definition at line 304 of file KisAsyncColorSamplerHelper.cpp.

305{
306
308 m_d->isActive = true;
309
310 m_d->sampleResourceId =
311 pickFgColor ?
314
315 m_d->sampleSource = sampleSource;
316 m_d->haveSample = false;
317
318 KisConfig cfg(true);
319 m_d->style = cfg.colorSamplerPreviewStyle();
320
321 m_d->circlePreviewDiameter = cfg.colorSamplerPreviewCircleDiameter();
322 m_d->circlePreviewThickness = cfg.colorSamplerPreviewCircleThickness()/100.0; // saved in percentages
323 m_d->circlePreviewOutlineEnabled = cfg.colorSamplerPreviewCircleOutlineEnabled();
324 m_d->circlePreviewExtraCircles = cfg.colorSamplerPreviewCircleExtraCirclesEnabled();
325 m_d->circleZoomPreviewEnabled = cfg.colorSamplerZoomPreviewEnabled();
326 m_d->circleZoomPreviewScale = cfg.colorSamplerZoomPreviewScale();
327 m_d->circlePreviewPosition = cfg.colorSamplerPreviewCirclePosition();
328
329 m_d->activationDelayTimer.start();
330}
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
@ BackgroundColor
The active background color selected for this canvas.
@ ForegroundColor
The active foreground color selected for this canvas.

References KoCanvasResource::BackgroundColor, KisConfig::colorSamplerPreviewCircleDiameter(), KisConfig::colorSamplerPreviewCircleExtraCirclesEnabled(), KisConfig::colorSamplerPreviewCircleOutlineEnabled(), KisConfig::colorSamplerPreviewCirclePosition(), KisConfig::colorSamplerPreviewCircleThickness(), KisConfig::colorSamplerPreviewStyle(), KisConfig::colorSamplerZoomPreviewEnabled(), KisConfig::colorSamplerZoomPreviewScale(), KoCanvasResource::ForegroundColor, KIS_SAFE_ASSERT_RECOVER_RETURN, and m_d.

◆ activateDelayedPreview

void KisAsyncColorSamplerHelper::activateDelayedPreview ( )
privateslot

Definition at line 332 of file KisAsyncColorSamplerHelper.cpp.

333{
334 // the event may come after we have started or even
335 // finished color picking if the user is quick
336 if (!m_d->isActive || m_d->showPreview) {
337 return;
338 }
339
341
343}

References activatePreview(), m_d, and sigRequestUpdateOutline().

◆ activatePreview()

void KisAsyncColorSamplerHelper::activatePreview ( )
private

Definition at line 345 of file KisAsyncColorSamplerHelper.cpp.

346{
347 m_d->activationDelayTimer.stop();
348 m_d->showPreview = true;
349
350 const KoColor currentColor =
351 m_d->canvas->resourceManager()->koColorResource(m_d->sampleResourceId);
352 const QColor previewColor = m_d->canvas->displayColorConverter()->convertColorToDisplayColorSpace(currentColor, true);
353
354 m_d->currentColor = previewColor;
355 m_d->baseColor = previewColor;
356 m_d->cache = QPixmap();
357
358 updateCursor(m_d->sampleSource, m_d->sampleResourceId == KoCanvasResource::ForegroundColor);
359}
void updateCursor(KisToolUtils::ColorSamplerSource sampleSource, bool pickFgColor)
KoColor currentColor(ResourceProvider *provider, ColorRole role)

References KoCanvasResource::ForegroundColor, m_d, and updateCursor().

◆ colorPreviewDocRect()

QRectF KisAsyncColorSamplerHelper::colorPreviewDocRect ( const QPointF & docPoint)

Definition at line 469 of file KisAsyncColorSamplerHelper.cpp.

470{
471 if (!m_d->showPreview) return QRectF();
472
473 m_d->sampleDocPoint = docPoint;
474
475 KisConfig cfg(true);
476 m_d->style = cfg.colorSamplerPreviewStyle();
477 QRectF previewDocRect = m_d->colorPreviewDocRect(docPoint);
478
479 if (previewDocRect != m_d->previewDocRect) prepareZoomPreview(previewDocRect);
480
481 m_d->previewDocRect = previewDocRect;
482
483 return m_d->previewDocRect;
484}
void prepareZoomPreview(const QRectF &docRect)

References KisConfig::colorSamplerPreviewStyle(), m_d, and prepareZoomPreview().

◆ continueAction()

void KisAsyncColorSamplerHelper::continueAction ( const QPointF & docPoint)

Definition at line 452 of file KisAsyncColorSamplerHelper.cpp.

453{
455 m_d->samplingCompressor->start(docPoint);
456}

References KIS_SAFE_ASSERT_RECOVER_RETURN, and m_d.

◆ deactivate()

void KisAsyncColorSamplerHelper::deactivate ( )

Definition at line 408 of file KisAsyncColorSamplerHelper.cpp.

409{
410 KIS_SAFE_ASSERT_RECOVER(!m_d->strokeId) {
411 endAction();
412 }
413
414 m_d->activationDelayTimer.stop();
415
416 m_d->showPreview = false;
417 m_d->haveSample = false;
418
419 m_d->previewDocRect = QRectF();
420 m_d->currentColor = QColor();
421 m_d->baseColor = QColor();
422 m_d->cache = QPixmap();
423
424 // Reset the cached zoom preview image and rect
425 m_d->cacheCanvasPreviewImage = QImage();
426 m_d->cacheCanvasPreviewRect = QRect();
427 m_d->zoomPreviewHasPainted = false;
428 m_d->zoomDocRectF = QRectF();
429
430 m_d->isActive = false;
431
432 Q_EMIT sigRequestCursorReset();
434}
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126

References endAction(), KIS_SAFE_ASSERT_RECOVER, m_d, sigRequestCursorReset(), and sigRequestUpdateOutline().

◆ endAction()

void KisAsyncColorSamplerHelper::endAction ( )

Definition at line 458 of file KisAsyncColorSamplerHelper.cpp.

459{
461
462 m_d->strokesFacade()->addJob(m_d->strokeId,
464
465 m_d->strokesFacade()->endStroke(m_d->strokeId);
466 m_d->strokeId.clear();
467}

References KIS_SAFE_ASSERT_RECOVER_RETURN, and m_d.

◆ isActive()

bool KisAsyncColorSamplerHelper::isActive ( ) const

Definition at line 299 of file KisAsyncColorSamplerHelper.cpp.

300{
301 return m_d->isActive;
302}

References m_d.

◆ paint()

void KisAsyncColorSamplerHelper::paint ( QPainter & gc,
const KoViewConverter & converter )

Definition at line 527 of file KisAsyncColorSamplerHelper.cpp.

528{
529 if (!m_d->showPreview) {
530 return;
531 }
532
533 QRectF viewRectF = converter.documentToView(m_d->previewDocRect);
534 QColor currentColor = colorWithAlpha(m_d->currentColor, OPACITY_OPAQUE_U8);
535 QColor baseColor = m_d->haveSample ? colorWithAlpha(m_d->baseColor, OPACITY_OPAQUE_U8) : currentColor;
536
537 switch (m_d->style) {
541 paintRectangle(gc, viewRectF, currentColor, baseColor);
542 break;
543 default:
544 // See comment in colorPreviewDocRect.
545 if (m_d->haveSample) {
546 paintCircle(gc, viewRectF, currentColor, baseColor);
547 } else {
548 paintRectangle(gc, viewRectF, currentColor, baseColor);
549 }
550 break;
551 }
552}
const quint8 OPACITY_OPAQUE_U8
void paintRectangle(QPainter &gc, const QRectF &viewRectF, const QColor &currentColor, const QColor &baseColor)
void paintCircle(QPainter &gc, const QRectF &viewRectF, const QColor &currentColor, const QColor &baseColor)
virtual QPointF documentToView(const QPointF &documentPoint) const

References KoViewConverter::documentToView(), m_d, OPACITY_OPAQUE_U8, paintCircle(), paintRectangle(), KisConfig::RectangleAbove, KisConfig::RectangleLeft, and KisConfig::RectangleRight.

◆ paintCircle()

void KisAsyncColorSamplerHelper::paintCircle ( QPainter & gc,
const QRectF & viewRectF,
const QColor & currentColor,
const QColor & baseColor )
private

Definition at line 604 of file KisAsyncColorSamplerHelper.cpp.

608{
609 if (!m_d->haveSample) {
610 return;
611 }
612
613 gc.save();
614
615 qreal dpr = gc.device()->devicePixelRatioF();
616 QSizeF cacheSizeF = viewRectF.size() * dpr;
617 QSize cacheSize(qCeil(cacheSizeF.width()), qCeil(cacheSizeF.height()));
618 bool needsNewCache = m_d->cache.isNull() || m_d->cache.size() != cacheSize;
619 if (needsNewCache) {
620 m_d->cache = QPixmap(cacheSize);
621 m_d->cache.fill(Qt::transparent);
622 }
623
624 qreal canvasRotationAngle = m_d->canvas->rotationAngle();
625 if (m_d->canvas->xAxisMirrored()) {
626 canvasRotationAngle = -canvasRotationAngle;
627 }
628
629 QPainter cachePainter(&m_d->cache);
630 cachePainter.setRenderHint(QPainter::Antialiasing);
631
632 QRectF cacheRect = m_d->cache.rect();
633
634 // The color sampler preview is an outline and those rotate along
635 // with the canvas. That's undesirable for the sampler preview
636 // though, so we calculate the transformation to counter the rotation here
637 QPointF cacheCenter = cacheRect.center();
638 QTransform tf;
639 tf.translate(cacheCenter.x(), cacheCenter.y());
640 tf.rotate(-canvasRotationAngle);
641 tf.translate(-cacheCenter.x(), -cacheCenter.y());
642
643 bool needsDualColor = currentColor != baseColor;
644 if (needsNewCache || (needsDualColor && !qFuzzyCompare(m_d->cacheRotation, canvasRotationAngle))) {
645 m_d->cacheRotation = canvasRotationAngle;
646
647 QColor backgroundColor = colorWithAlpha(m_d->backgroundColor, OPACITY_OPAQUE_U8 / 2 + 1);
648 qreal penWidth = m_d->circlePreviewDiameter > 100 ? (2.0 * dpr) : (1.0 * dpr);
649 QPen pen = QPen(backgroundColor, penWidth);
650 if (m_d->circlePreviewOutlineEnabled) {
651 cachePainter.setPen(pen);
652 } else {
653 cachePainter.setPen(Qt::NoPen);
654 }
655
656 QRectF outerRect = cacheRect.marginsRemoved(QMarginsF(penWidth, penWidth, penWidth, penWidth));
657
658 if (needsDualColor) {
659 // The color sampler preview is an outline and those rotate along
660 // with the canvas. That's undesirable for the sampler preview
661 // though, so we un-rotate its contents here accordingly.
662
663 QPainterPath clipPath;
664 clipPath.addPolygon(tf.map(QPolygonF(QRectF(0, 0, cacheRect.width(), cacheRect.height() / 2.0 + 1.0))));
665 cachePainter.setClipPath(clipPath);
666
667 bool flipped = m_d->canvas->yAxisMirrored();
668 cachePainter.setBrush(flipped ? baseColor : currentColor);
669 cachePainter.drawEllipse(outerRect);
670
671 cachePainter.setBrush(baseColor);
672 clipPath.clear();
673 clipPath.addPolygon(
674 tf.map(QRectF(0, cacheRect.height() / 2.0, cacheRect.width(), cacheRect.height() / 2.0)));
675 cachePainter.setClipPath(clipPath);
676
677 cachePainter.setBrush(flipped ? currentColor : baseColor);
678 cachePainter.drawEllipse(outerRect);
679
680 cachePainter.setClipPath(QPainterPath(), Qt::NoClip);
681 } else {
682 cachePainter.setBrush(currentColor);
683 cachePainter.drawEllipse(outerRect);
684 }
685
686 qreal innerX = cacheRect.width() * (1.0 - m_d->circlePreviewThickness);
687 qreal innerY = cacheRect.height() * (1.0 - m_d->circlePreviewThickness);
688 QRectF innerRect = cacheRect.marginsRemoved(QMarginsF(innerX, innerY, innerX, innerY));
689 QPainterPath innerEllipse;
690 innerEllipse.addEllipse(innerRect);
691
692 QPainterPath innerPath;
693 innerPath.addPath(innerEllipse);
694
695 if (m_d->circlePreviewThickness < 0.5 && m_d->circlePreviewExtraCircles) {
696 qreal extraMargin = 0.1*m_d->circlePreviewThickness*innerRect.width(); // looks better
697 QPointF leftCenter = QPointF(innerRect.left() - extraMargin, innerRect.top() + innerRect.height()/2.0);
698 QPointF rightCenter = QPointF(innerRect.right() + extraMargin, innerRect.top() + innerRect.height()/2.0);
699
700 innerPath.setFillRule(Qt::OddEvenFill);
701 innerPath.addEllipse(leftCenter, m_d->circlePreviewThickness*cacheRect.width(), m_d->circlePreviewThickness*cacheRect.width());
702 innerPath.addEllipse(rightCenter, m_d->circlePreviewThickness*cacheRect.width(), m_d->circlePreviewThickness*cacheRect.width());
703
704 innerPath = innerPath.intersected(innerEllipse);
705 }
706
707 m_d->cacheCircleInnerClip = innerPath;
708
709 // Draw inner circle outline if enabled
710 if (m_d->circlePreviewOutlineEnabled) {
711 cachePainter.setBrush(Qt::transparent);
712 cachePainter.setPen(pen);
713 cachePainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
714 cachePainter.drawPath(tf.map(m_d->cacheCircleInnerClip));
715 }
716 }
717
718 cachePainter.setPen(Qt::NoPen);
719 cachePainter.setBrush(m_d->backgroundColor);
720
721 // Clear the center
722 cachePainter.setCompositionMode(QPainter::CompositionMode_Clear);
723 cachePainter.drawPath(tf.map(m_d->cacheCircleInnerClip));
724
725 // If canvas preview image is null and it's being fetched (which means canvas preview is needed),
726 // don't paint preview until it's done to avoid flickering
727 bool shouldPaintPreview = !(m_d->cacheCanvasPreviewImage.isNull() && m_d->canvasPreviewFetchCookie);
728
729 // But if zoom preview has already been painted, keeps painting to avoid flickering
730 if (m_d->circleZoomPreviewEnabled && (shouldPaintPreview || m_d->zoomPreviewHasPainted)) {
731 paintCircleCanvasPreview(cachePainter, cacheRect, tf.map(m_d->cacheCircleInnerClip));
732 paintCircleReferenceImagePreview(cachePainter, cacheRect, tf.map(m_d->cacheCircleInnerClip));
733
734 // Draw crosshair if preview is offseted
735 if (m_d->circlePreviewPosition != KisConfig::ColorSamplerPreviewCirclePosition::Center) {
736 paintCircleCrosshair(cachePainter, cacheRect, currentColor);
737 }
738
739 // Fill empty spaces to hide the underlying canvas
740 cachePainter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
741 cachePainter.drawPath(tf.map(m_d->cacheCircleInnerClip));
742
743 m_d->zoomPreviewHasPainted = true;
744 }
745
746 gc.drawPixmap(viewRectF.toRect(), m_d->cache);
747
748 gc.restore();
749}
void paintCircleCrosshair(QPainter &gc, const QRectF &viewRectF, const QColor &currentColor)
void paintCircleReferenceImagePreview(QPainter &gc, const QRectF &viewRectF, const QPainterPath &clip)
void paintCircleCanvasPreview(QPainter &gc, const QRectF &viewRectF, const QPainterPath &clip)
static bool qFuzzyCompare(half p1, half p2)

References KisConfig::Center, m_d, OPACITY_OPAQUE_U8, paintCircleCanvasPreview(), paintCircleCrosshair(), paintCircleReferenceImagePreview(), and qFuzzyCompare().

◆ paintCircleCanvasPreview()

void KisAsyncColorSamplerHelper::paintCircleCanvasPreview ( QPainter & gc,
const QRectF & viewRectF,
const QPainterPath & clip )
private

Definition at line 771 of file KisAsyncColorSamplerHelper.cpp.

771 {
772 QRect canvasPixelRect = m_d->cacheCanvasPreviewRect;
773 QImage cachedImage = m_d->cacheCanvasPreviewImage;
774
775 if (cachedImage.isNull()) return;
776
777 gc.save();
778 gc.setCompositionMode(QPainter::CompositionMode_SourceOver);
779
780 gc.setClipPath(clip);
781
782 // QtDoc: The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
783 // Since the piece of canvas is (zoomPreviewScale) times smaller than cacheRect
784 // drawImage will scale it up that many times, thus achieving the zoom effect
785 gc.drawImage(viewRectF, cachedImage, canvasPixelRect);
786
787 gc.restore();
788}

References m_d.

◆ paintCircleCrosshair()

void KisAsyncColorSamplerHelper::paintCircleCrosshair ( QPainter & gc,
const QRectF & viewRectF,
const QColor & currentColor )
private

Definition at line 751 of file KisAsyncColorSamplerHelper.cpp.

752{
753 QColor crosshairColor = Qt::black;
754 qreal luminance = KisPaintingTweaks::luminosityCoarse(currentColor);
755 if (luminance < 0.5) crosshairColor = Qt::white;
756
757 gc.save();
758
759 gc.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
760 gc.setCompositionMode(QPainter::CompositionMode_SourceOver);
761 gc.setPen(crosshairColor);
762
763 QTransform tf;
764 tf.translate(viewRectF.center().x(), viewRectF.center().y());
765 tf.rotate(-m_d->cacheRotation);
766 gc.drawPath(tf.map(m_d->crosshairForOffsettedCircle()));
767
768 gc.restore();
769}
qreal luminosityCoarse(const QColor &c, bool sRGBtrc)
luminosityCoarse This calculates the luminosity of the given QColor. It uses a very coarse (10 step) ...

References KisPaintingTweaks::luminosityCoarse(), and m_d.

◆ paintCircleReferenceImagePreview()

void KisAsyncColorSamplerHelper::paintCircleReferenceImagePreview ( QPainter & gc,
const QRectF & viewRectF,
const QPainterPath & clip )
private

Definition at line 792 of file KisAsyncColorSamplerHelper.cpp.

792 {
793 KisDocument *document = m_d->canvas->viewManager()->document();
794 if (!document || !document->referenceImagesLayer()) return;
795
796 gc.save();
797
798 gc.setCompositionMode(QPainter::CompositionMode_SourceOver);
799 gc.setClipPath(clip);
800
801 QRectF zoomDocRectF = m_d->zoomDocRectF;
802
803 QList<KoShape*> shapeList = document->referenceImagesLayer()->shapeManager()->shapesAt(zoomDocRectF);
804 if (shapeList.size() > 1) std::sort(shapeList.begin(), shapeList.end(), KoShape::compareShapeZIndex);
805
806 Q_FOREACH(KoShape *shape, shapeList) {
807 KisReferenceImage *refImage = dynamic_cast<KisReferenceImage*>(shape);
808 if (!refImage) continue;
809
810 if (refImage->transparency() == 1.0) continue;
811
812 gc.save();
813
814 QImage image = refImage->getCachedImage();
815
816 QPoint refPixelPoint = refImage->documentToPixelFloored(zoomDocRectF.center());
817
818 // Avoid using KisReferenceImage::documentToPixel because it use absoluteTransformation.invert()
819 // Which will take rotation into account and cause some quirks when ref image is rotated
820 qreal xScale = refImage->size().width() / image.width();
821 qreal yScale = refImage->size().height() / image.height();
822
823 // This only happens when refImage size is 0, so painting it probably won't make senses
824 if (xScale == 0 || yScale == 0) continue;
825
826 QRect refPixelRect = QRect(refPixelPoint, QSize(zoomDocRectF.width() / xScale, zoomDocRectF.height() / yScale));
827
828 refPixelRect = m_d->standardizeZoomPreviewPixelRect(refPixelRect);
829
830 refPixelRect.moveCenter(refPixelPoint);
831
832 // Rotate the painter, draw, rotate back is seemingly easier than
833 // trying to rotate the image and the shenanighens that follow
834 gc.translate(viewRectF.center());
835 gc.rotate(refImage->rotation());
836 gc.translate(-viewRectF.center());
837
838 gc.drawImage(viewRectF, image, refPixelRect);
839
840 gc.restore();
841 }
842
843 gc.restore();
844}
KisViewManager * viewManager() const
The KisReferenceImage class represents a single reference image.
QPoint documentToPixelFloored(const QPointF &docPoint)
KisDocument * document() const
virtual QSizeF size() const
Get the size of the shape in pt.
Definition KoShape.cpp:735
qreal rotation() const
Definition KoShape.cpp:715
static bool compareShapeZIndex(KoShape *s1, KoShape *s2)
Definition KoShape.cpp:388
qreal transparency(bool recursive=false) const
Definition KoShape.cpp:645
KisCanvas2 * canvas

References Private::canvas, KoShape::compareShapeZIndex(), KisViewManager::document(), KisReferenceImage::documentToPixelFloored(), KisReferenceImage::getCachedImage(), m_d, KoShape::rotation(), KoShape::size(), KoShape::transparency(), and KisCanvas2::viewManager().

◆ paintRectangle()

void KisAsyncColorSamplerHelper::paintRectangle ( QPainter & gc,
const QRectF & viewRectF,
const QColor & currentColor,
const QColor & baseColor )
private

Definition at line 554 of file KisAsyncColorSamplerHelper.cpp.

558{
559 qreal dpr = gc.device()->devicePixelRatioF();
560 QSizeF cacheSizeF = viewRectF.size() * dpr;
561 QSize cacheSize(qCeil(cacheSizeF.width()), qCeil(cacheSizeF.height()));
562 bool needsNewCache = m_d->cache.isNull() || m_d->cache.size() != cacheSize;
563 if (needsNewCache) {
564 m_d->cache = QPixmap(cacheSize);
565 m_d->cache.fill(Qt::transparent);
566 }
567
568 qreal canvasRotationAngle = m_d->canvas->rotationAngle();
569 bool canvasMirror = m_d->canvas->xAxisMirrored();
570 if (needsNewCache || !qFuzzyCompare(canvasRotationAngle, m_d->cacheRotation) || canvasMirror != m_d->cacheMirror) {
571 m_d->cacheRotation = canvasRotationAngle;
572 m_d->cacheMirror = canvasMirror;
573
574 QPainter cachePainter(&m_d->cache);
575 cachePainter.setRenderHint(QPainter::Antialiasing);
576
577 qreal size = Private::PREVIEW_RECT_SIZE * dpr;
578 QRectF rect(0.0, 0.0, m_d->haveSample ? size * 2.0 : size, size);
579 rect.moveTopLeft(-rect.center());
580
581 QTransform tf;
582 QPointF offset = QRectF(m_d->cache.rect()).center();
583 tf.translate(offset.x(), offset.y());
584 tf.rotate(canvasMirror ? canvasRotationAngle : -canvasRotationAngle);
585 cachePainter.setTransform(tf);
586
587 if (m_d->haveSample) {
588 qreal centerX = rect.center().x();
589 QRectF currentRect(rect.topLeft(), QPointF(centerX + 1.0, rect.bottom()));
590 QRectF baseRect(QPointF(centerX, rect.top()), rect.bottomRight());
591 if (m_d->canvas->xAxisMirrored()) {
592 std::swap(currentRect, baseRect);
593 }
594 cachePainter.fillRect(currentRect, currentColor);
595 cachePainter.fillRect(baseRect, baseColor);
596 } else {
597 cachePainter.fillRect(rect, currentColor);
598 }
599 }
600
601 gc.drawPixmap(viewRectF.toRect(), m_d->cache);
602}
int size(const Forest< T > &forest)
Definition KisForest.h:1232

References m_d, KisAsyncColorSamplerHelper::Private::PREVIEW_RECT_SIZE, and qFuzzyCompare().

◆ prepareZoomPreview()

void KisAsyncColorSamplerHelper::prepareZoomPreview ( const QRectF & docRect)
private

Definition at line 486 of file KisAsyncColorSamplerHelper.cpp.

486 {
487 // This can be called before startAction(), so there may not be a strokeId
488 if (!m_d->circleZoomPreviewEnabled || !m_d->strokeId) return;
489
490 KisImageSP image = m_d->canvas->image();
491 if (!image) return;
492
493 // Prepare zoomed doc rect
494 QRectF zoomDocRectF = docRect;
495
496 zoomDocRectF.setSize(zoomDocRectF.size() / m_d->circleZoomPreviewScale);
497 zoomDocRectF.moveCenter(m_d->sampleDocPoint);
498
499 m_d->zoomDocRectF = zoomDocRectF;
500
501 // Prepare canvas preview because it takes time
502 QRectF canvasPixelRectF = image->documentToPixel(zoomDocRectF);
503 // Floor manually to avoid width != height when using toRect()
504 QRect canvasPixelRect = QRect(canvasPixelRectF.topLeft().toPoint(), QSize(qFloor(canvasPixelRectF.width()), qFloor(canvasPixelRectF.height())));
505
506 canvasPixelRect = m_d->standardizeZoomPreviewPixelRect(canvasPixelRect);
507 // Make sure the center is the pixel currently sampled because standardizing may change the shape
508 canvasPixelRect.moveCenter(image->documentToImagePixelFloored(zoomDocRectF.center()));
509
510 if (!image->bounds().intersects(canvasPixelRect) && !image->wrapAroundModeActive()) {
511 m_d->cacheCanvasPreviewRect = QRect();
512 m_d->cacheCanvasPreviewImage = QImage();
513 return;
514 }
515
516 // If not already have a job fetching canvas image, then do it
517 if (!m_d->canvasPreviewFetchCookie && m_d->cacheCanvasPreviewRect != canvasPixelRect) {
519 new KisColorSamplerStrokeStrategy::GenerateCanvasZoomPreviewData(image->projection(), canvasPixelRect, m_d->canvas->displayColorConverter());
520
521 m_d->canvasPreviewFetchCookie = data->cookie();
522
523 m_d->strokesFacade()->addJob(m_d->strokeId, data);
524 }
525}
KisImageWSP image() const
bool wrapAroundModeActive() const
QPointF documentToPixel(const QPointF &documentCoord) const
KisPaintDeviceSP projection() const
QPoint documentToImagePixelFloored(const QPointF &documentCoord) const
QRect bounds() const override

References KisImage::bounds(), Private::canvas, KisColorSamplerStrokeStrategy::GenerateCanvasZoomPreviewData::cookie(), KisImage::documentToImagePixelFloored(), KisImage::documentToPixel(), KisCanvas2::image(), m_d, KisImage::projection(), and KisImage::wrapAroundModeActive().

◆ setUpdateGlobalColor()

void KisAsyncColorSamplerHelper::setUpdateGlobalColor ( bool value)

Definition at line 398 of file KisAsyncColorSamplerHelper.cpp.

399{
400 m_d->updateGlobalColor = value;
401}
float value(const T *src, size_t ch)

References m_d, and value().

◆ sigColorSelected

void KisAsyncColorSamplerHelper::sigColorSelected ( const KoColor & color)
signal

Notifies about the "palette" color picked from the layer, that is, with the alpha channel set to OPACITY_OPAQUE.

◆ sigFinalColorSelected

void KisAsyncColorSamplerHelper::sigFinalColorSelected ( const KoColor & color)
signal

Notifies about the "palette" color picked from the layer, that is, with the alpha channel set to OPACITY_OPAQUE.

This notification is emitted only once at the very end of the color picking stroke.

◆ sigRawColorSelected

void KisAsyncColorSamplerHelper::sigRawColorSelected ( const KoColor & color)
signal

Notifies about the raw color picked from the layer, including its alpha channel.

◆ sigRequestCursor

void KisAsyncColorSamplerHelper::sigRequestCursor ( const QCursor & cursor)
signal

◆ sigRequestCursorReset

void KisAsyncColorSamplerHelper::sigRequestCursorReset ( )
signal

◆ sigRequestUpdateOutline

void KisAsyncColorSamplerHelper::sigRequestUpdateOutline ( )
signal

◆ slotAddSamplingJob

void KisAsyncColorSamplerHelper::slotAddSamplingJob ( const QPointF & docPoint)
privateslot

The actual sampling is delayed by a compressor, so we can get this event when the stroke is already closed

Definition at line 853 of file KisAsyncColorSamplerHelper.cpp.

854{
859 if (!m_d->strokeId) return;
860
861 KisImageSP image = m_d->canvas->image();
862 const QPoint imagePoint = image->documentToImagePixelFloored(docPoint);
863
864 if (m_d->sampleSource == KisToolUtils::SampleSourceImage) {
865 KisSharedPtr<KisReferenceImagesLayer> referencesLayer = m_d->canvas->imageView()->document()->referenceImagesLayer();
866 if (referencesLayer && m_d->canvas->referenceImagesDecoration()->visible()) {
867 KoColor color = referencesLayer->getPixelForDocPoint(docPoint);
868 if (color.opacityU8() > 0) {
870 return;
871 }
872 }
873 }
874
875 KisPaintDeviceSP device;
876
877 switch (m_d->sampleSource) {
879 device = m_d->canvas->imageView()->currentNode()->parent()->colorSampleSourceDevice();
880 break;
882 device = m_d->canvas->imageView()->currentNode()->colorSampleSourceDevice();
883 break;
885 default:
886 device = image->projection();
887 break;
888 }
889
890 if (device) {
891 // Used for color sampler blending.
892 const KoColor currentColor =
893 m_d->canvas->resourceManager()->koColorResource(m_d->sampleResourceId);
894
895 m_d->strokesFacade()->addJob(m_d->strokeId,
896 new KisColorSamplerStrokeStrategy::Data(device, imagePoint, currentColor));
897 } else {
898 QString message = i18n("Color sampler does not work on this layer.");
899 m_d->canvas->viewManager()->showFloatingMessage(message, koIcon("object-locked"));
900 }
901}
void slotColorSamplingFinished(const KoColor &rawColor)
quint8 opacityU8() const
Definition KoColor.cpp:341
#define koIcon(name)
Use these macros for icons without any issues.
Definition kis_icon.h:25

References Private::canvas, KisImage::documentToImagePixelFloored(), KisCanvas2::image(), koIcon, m_d, KoColor::opacityU8(), KisImage::projection(), KisToolUtils::SampleSourceGroup, KisToolUtils::SampleSourceImage, KisToolUtils::SampleSourceNode, and slotColorSamplingFinished().

◆ slotCanvasZoomPreviewUpdated

void KisAsyncColorSamplerHelper::slotCanvasZoomPreviewUpdated ( const QImage & canvasImage,
QRect canvasRect )
privateslot

Definition at line 846 of file KisAsyncColorSamplerHelper.cpp.

846 {
847 m_d->cacheCanvasPreviewRect = canvasRect;
848 m_d->cacheCanvasPreviewImage = canvasImage;
849
851}

References m_d, and sigRequestUpdateOutline().

◆ slotColorSamplingFinished

void KisAsyncColorSamplerHelper::slotColorSamplingFinished ( const KoColor & rawColor)
privateslot

Definition at line 903 of file KisAsyncColorSamplerHelper.cpp.

904{
905 KoColor color(rawColor);
906
907 color.setOpacity(OPACITY_OPAQUE_U8);
908
909 if (m_d->updateGlobalColor) {
910 m_d->canvas->resourceManager()->setResource(m_d->sampleResourceId, color);
911 }
912
913 Q_EMIT sigRawColorSelected(rawColor);
914 Q_EMIT sigColorSelected(color);
915
916 if (!m_d->showPreview) return;
917
918 const QColor previewColor = m_d->canvas->displayColorConverter()->convertColorToDisplayColorSpace(color, true);
919
920 if (!m_d->haveSample || m_d->currentColor != previewColor) {
921 m_d->haveSample = true;
922 m_d->currentColor = previewColor;
923 m_d->cache = QPixmap();
924 }
925
927}
void sigColorSelected(const KoColor &color)
void sigRawColorSelected(const KoColor &color)

References m_d, OPACITY_OPAQUE_U8, KoColor::setOpacity(), sigColorSelected(), sigRawColorSelected(), and sigRequestUpdateOutline().

◆ slotUpdateBgColor

void KisAsyncColorSamplerHelper::slotUpdateBgColor ( )
privateslot

Definition at line 929 of file KisAsyncColorSamplerHelper.cpp.

930{
931 KoColor bgColor;
932 bgColor.fromQColor(qApp->palette().color(QPalette::Base));
933 m_d->backgroundColor = m_d->canvas->displayColorConverter()->convertColorToDisplayColorSpace(bgColor);
934}
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213

References KoColor::fromQColor(), and m_d.

◆ startAction()

void KisAsyncColorSamplerHelper::startAction ( const QPointF & docPoint,
int radius,
int blend )

Definition at line 436 of file KisAsyncColorSamplerHelper.cpp.

437{
445
447 m_d->haveSample = true;
448 m_d->strokeId = m_d->strokesFacade()->startStroke(strategy);
449 m_d->samplingCompressor->start(docPoint);
450}
void sigFinalColorSelected(const KoColor &color)
void slotCanvasZoomPreviewUpdated(const QImage &canvasImage, QRect canvasRect)
void sigCanvasZoomPreviewUpdated(const QImage &canvasImage, const QRect &canvasRect)
void sigFinalColorSelected(const KoColor &color)
void sigColorUpdated(const KoColor &color)

References activatePreview(), m_d, KisColorSamplerStrokeStrategy::sigCanvasZoomPreviewUpdated(), KisColorSamplerStrokeStrategy::sigColorUpdated(), sigFinalColorSelected(), KisColorSamplerStrokeStrategy::sigFinalColorSelected(), slotCanvasZoomPreviewUpdated(), and slotColorSamplingFinished().

◆ updateCursor()

void KisAsyncColorSamplerHelper::updateCursor ( KisToolUtils::ColorSamplerSource sampleSource,
bool pickFgColor )

Definition at line 361 of file KisAsyncColorSamplerHelper.cpp.

362{
363 const int sampleResourceId =
364 pickFgColor ?
367
368 QCursor cursor;
369
370 switch (sampleSource) {
372 if (sampleResourceId == KoCanvasResource::ForegroundColor) {
374 } else {
376 }
377 break;
379 if (sampleResourceId == KoCanvasResource::ForegroundColor) {
381 } else {
383 }
384 break;
386 default:
387 if (sampleResourceId == KoCanvasResource::ForegroundColor) {
389 } else {
391 }
392 break;
393 }
394
395 Q_EMIT sigRequestCursor(cursor);
396}
void sigRequestCursor(const QCursor &cursor)
static QCursor samplerImageBackgroundCursor()
static QCursor samplerGroupForegroundCursor()
static QCursor samplerLayerBackgroundCursor()
static QCursor samplerLayerForegroundCursor()
static QCursor samplerGroupBackgroundCursor()
static QCursor samplerImageForegroundCursor()

References KoCanvasResource::BackgroundColor, KoCanvasResource::ForegroundColor, KisCursor::samplerGroupBackgroundCursor(), KisCursor::samplerGroupForegroundCursor(), KisCursor::samplerImageBackgroundCursor(), KisCursor::samplerImageForegroundCursor(), KisCursor::samplerLayerBackgroundCursor(), KisCursor::samplerLayerForegroundCursor(), KisToolUtils::SampleSourceGroup, KisToolUtils::SampleSourceImage, KisToolUtils::SampleSourceNode, and sigRequestCursor().

◆ updateGlobalColor()

bool KisAsyncColorSamplerHelper::updateGlobalColor ( ) const

Definition at line 403 of file KisAsyncColorSamplerHelper.cpp.

404{
405 return m_d->updateGlobalColor;
406}

References m_d.

Member Data Documentation

◆ m_d

QScopedPointer<Private> KisAsyncColorSamplerHelper::m_d
private

Definition at line 92 of file KisAsyncColorSamplerHelper.h.


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