Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Replace color sampler preview with a circle

Carsten Hartenfelsb010ff0b3330
diff --git a/libs/ui/kis_config.cc b/libs/ui/kis_config.cc
index d4f9c8bac0..ea05e50492 100644
--- a/libs/ui/kis_config.cc
+++ b/libs/ui/kis_config.cc
@@ -520,16 +520,6 @@ void KisConfig::setEraserOutlineStyle(OutlineStyle style)
m_cfg.writeEntry("eraserOutlineStyle", (int)style);
}
-QRect KisConfig::colorPreviewRect() const
-{
- return m_cfg.readEntry("colorPreviewRect", QVariant(QRect(32, 32, 48, 48))).toRect();
-}
-
-void KisConfig::setColorPreviewRect(const QRect &rect)
-{
- m_cfg.writeEntry("colorPreviewRect", QVariant(rect));
-}
-
bool KisConfig::useDirtyPresets(bool defaultValue) const
{
return (defaultValue ? false : m_cfg.readEntry("useDirtyPresets", true));
diff --git a/libs/ui/kis_config.h b/libs/ui/kis_config.h
index d693625f83..fa74b459db 100644
--- a/libs/ui/kis_config.h
+++ b/libs/ui/kis_config.h
@@ -135,9 +135,6 @@ public:
OutlineStyle eraserOutlineStyle(bool defaultValue = false) const;
void setEraserOutlineStyle(OutlineStyle style);
- QRect colorPreviewRect() const;
- void setColorPreviewRect(const QRect &rect);
-
/**
* Controls if the canvas should have a separate color managed surface.
* Even though the surface can be created on any platform the proper
diff --git a/libs/ui/tool/KisAsyncColorSamplerHelper.cpp b/libs/ui/tool/KisAsyncColorSamplerHelper.cpp
index 1ccde3a096..e312c4ce47 100644
--- a/libs/ui/tool/KisAsyncColorSamplerHelper.cpp
+++ b/libs/ui/tool/KisAsyncColorSamplerHelper.cpp
@@ -1,12 +1,16 @@
/*
* SPDX-FileCopyrightText: 2022 Dmitry Kazakov <[email protected]>
+ * SPDX-FileCopyrightText: 2025 Carsten Hartenfels <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "KisAsyncColorSamplerHelper.h"
+#include <QApplication>
#include <QPainter>
+#include <QPalette>
+#include <QPixmap>
#include "KoCanvasResourcesIds.h"
#include "KoCanvasResourceProvider.h"
@@ -15,7 +19,6 @@
#include "kis_cursor.h"
#include "kis_signal_compressor_with_param.h"
#include "kis_image_interfaces.h"
-#include "kis_config.h"
#include "kis_canvas2.h"
#include "KisViewManager.h"
#include "KisDocument.h"
@@ -26,22 +29,18 @@
namespace {
-std::pair<QRectF,QRectF> colorPreviewDocRectImpl(const QPointF &outlineDocPoint, bool colorPreviewShowComparePlate, const KoViewConverter *converter)
+QRectF colorPreviewDocRectImpl(const QPointF &outlineDocPoint, const KoViewConverter *converter)
{
- KisConfig cfg(true);
- const QRectF colorPreviewViewRect = cfg.colorPreviewRect();
-
- const QRectF colorPreviewBaseColorViewRect =
- colorPreviewShowComparePlate ?
- colorPreviewViewRect.translated(colorPreviewViewRect.width(), 0) :
- QRectF();
-
+ constexpr qreal SIZE = 200.0;
+ const QRectF colorPreviewViewRect = QRectF(-SIZE / 2.0, -SIZE / 2.0, SIZE, SIZE);
const QRectF colorPreviewDocumentRect = converter->viewToDocument(colorPreviewViewRect);
- const QRectF colorPreviewBaseColorDocumentRect =
- converter->viewToDocument(colorPreviewBaseColorViewRect);
+ return colorPreviewDocumentRect.translated(outlineDocPoint);
+}
- return std::make_pair(colorPreviewDocumentRect.translated(outlineDocPoint),
- colorPreviewBaseColorDocumentRect.translated(outlineDocPoint));
+QColor colorWithAlpha(QColor color, int alpha)
+{
+ color.setAlpha(alpha);
+ return color;
}
}
@@ -59,7 +58,7 @@ struct KisAsyncColorSamplerHelper::Private
bool isActive {false};
bool showPreview {false};
- bool showComparePlate {false};
+ bool showComparison {false};
KisStrokeId strokeId;
typedef KisSignalCompressorWithParam<QPointF> SamplingCompressor;
@@ -67,12 +66,13 @@ struct KisAsyncColorSamplerHelper::Private
QTimer activationDelayTimer;
- QRectF currentColorDocRect;
- QRectF baseColorDocRect;
+ QRectF previewDocRect;
QColor currentColor;
QColor baseColor;
+ QPixmap cache;
+
KisStrokesFacade *strokesFacade() const {
return canvas->image().data();
}
@@ -118,7 +118,7 @@ void KisAsyncColorSamplerHelper::activate(bool sampleCurrentLayer, bool pickFgCo
KoCanvasResource::BackgroundColor;
m_d->sampleCurrentLayer = sampleCurrentLayer;
- m_d->showComparePlate = false;
+ m_d->showComparison = false;
m_d->activationDelayTimer.start();
}
@@ -137,6 +137,7 @@ void KisAsyncColorSamplerHelper::activateDelayedPreview()
m_d->currentColor = previewColor;
m_d->baseColor = previewColor;
+ m_d->cache = QPixmap();
updateCursor(m_d->sampleCurrentLayer, m_d->sampleResourceId == KoCanvasResource::ForegroundColor);
@@ -188,12 +189,12 @@ void KisAsyncColorSamplerHelper::deactivate()
m_d->activationDelayTimer.stop();
m_d->showPreview = false;
- m_d->showComparePlate = false;
+ m_d->showComparison = false;
- m_d->currentColorDocRect = QRectF();
+ m_d->previewDocRect = QRectF();
m_d->currentColor = QColor();
m_d->baseColor = QColor();
- m_d->baseColorDocRect = QRectF();
+ m_d->cache = QPixmap();
m_d->isActive = false;
@@ -234,23 +235,63 @@ QRectF KisAsyncColorSamplerHelper::colorPreviewDocRect(const QPointF &docPoint)
{
if (!m_d->showPreview) return QRectF();
- std::tie(m_d->currentColorDocRect, m_d->baseColorDocRect) =
- colorPreviewDocRectImpl(docPoint, m_d->showComparePlate, &m_d->converter());
-
- return m_d->currentColorDocRect | m_d->baseColorDocRect;
+ m_d->previewDocRect = colorPreviewDocRectImpl(docPoint, &m_d->converter());
+ return m_d->previewDocRect;
}
void KisAsyncColorSamplerHelper::paint(QPainter &gc, const KoViewConverter &converter)
{
- if (!m_d->showPreview) return;
+ if (!m_d->showPreview) {
+ return;
+ }
- const QRectF viewRect = converter.documentToView(m_d->currentColorDocRect);
- gc.fillRect(viewRect, m_d->currentColor);
+ QRectF viewRectF = converter.documentToView(m_d->previewDocRect);
+ qreal dpr = gc.device()->devicePixelRatioF();
+ QSizeF cacheSizeF = viewRectF.size() * dpr;
+ QSize cacheSize(qCeil(cacheSizeF.width()), qCeil(cacheSizeF.height()));
+ if (m_d->cache.isNull() || m_d->cache.size() != cacheSize) {
+ m_d->cache = QPixmap(cacheSize);
+ m_d->cache.fill(Qt::transparent);
+ QPainter cachePainter(&m_d->cache);
+ cachePainter.setRenderHint(QPainter::Antialiasing);
+
+ QColor backgroundColor = colorWithAlpha(qApp->palette().color(QPalette::Base), OPACITY_OPAQUE_U8 / 2 + 1);
+ qreal penWidth = 2.0 * dpr;
+ QPen pen = QPen(backgroundColor, penWidth);
+ cachePainter.setPen(pen);
+
+ QRectF cacheRect = m_d->cache.rect();
+ QRectF outerRect = cacheRect.marginsRemoved(QMarginsF(penWidth, penWidth, penWidth, penWidth));
+ QColor currentColor = colorWithAlpha(m_d->currentColor, OPACITY_OPAQUE_U8);
+ QColor baseColor = m_d->showComparison ? colorWithAlpha(m_d->baseColor, OPACITY_OPAQUE_U8) : currentColor;
+
+ cachePainter.setBrush(currentColor);
+ if (currentColor == baseColor) {
+ cachePainter.drawEllipse(outerRect);
+ } else {
+ cachePainter.setClipRect(QRectF(0, 0, cacheRect.width(), cacheRect.height() / 2.0));
+ cachePainter.drawEllipse(outerRect);
+
+ cachePainter.setBrush(baseColor);
+ cachePainter.setClipRect(QRectF(0, cacheRect.height() / 2.0, cacheRect.width(), cacheRect.height() / 2.0));
+ cachePainter.drawEllipse(outerRect);
+ cachePainter.setClipRect(QRectF(), Qt::NoClip);
+ }
+
+ qreal innerX = cacheRect.width() / 8.0;
+ qreal innerY = cacheRect.height() / 8.0;
+ QRectF innerRect = cacheRect.marginsRemoved(QMarginsF(innerX, innerY, innerX, innerY));
- if (m_d->showComparePlate) {
- const QRectF baseColorRect = converter.documentToView(m_d->baseColorDocRect);
- gc.fillRect(baseColorRect, m_d->baseColor);
+ cachePainter.setPen(Qt::NoPen);
+ cachePainter.setCompositionMode(QPainter::CompositionMode_Clear);
+ cachePainter.drawEllipse(innerRect);
+
+ cachePainter.setBrush(Qt::transparent);
+ cachePainter.setPen(pen);
+ cachePainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
+ cachePainter.drawEllipse(innerRect);
}
+ gc.drawPixmap(viewRectF.toRect(), m_d->cache);
}
void KisAsyncColorSamplerHelper::slotAddSamplingJob(const QPointF &docPoint)
@@ -310,8 +351,11 @@ void KisAsyncColorSamplerHelper::slotColorSamplingFinished(const KoColor &rawCol
const QColor previewColor = m_d->canvas->displayColorConverter()->toQColor(color);
- m_d->showComparePlate = true;
- m_d->currentColor = previewColor;
+ if (!m_d->showComparison || m_d->currentColor != previewColor) {
+ m_d->showComparison = true;
+ m_d->currentColor = previewColor;
+ m_d->cache = QPixmap();
+ }
Q_EMIT sigRequestUpdateOutline();
}