[LyX/master] Move color swatch functions to ColorCache class

Koji Yokota <[email protected]> Fri, 05 Jun 2026 16:29:46 +0000
Newsgroups gmane.editors.lyx.cvs
Message-ID <[email protected]>
commit 8a25395fef3e5d1b5e562490c90dc4ff6a3b56ad
Author: Koji Yokota <[email protected]>
Date:   Fri Jun 5 20:24:48 2026 +0900

    Move color swatch functions to ColorCache class
---
 src/frontends/qt/ColorCache.cpp | 31 +++++++++++++++++++++++++++++++
 src/frontends/qt/ColorCache.h   |  3 +++
 src/frontends/qt/GuiPrefs.cpp   | 33 +++------------------------------
 src/frontends/qt/GuiPrefs.h     |  4 +---
 4 files changed, 38 insertions(+), 33 deletions(-)

diff --git a/src/frontends/qt/ColorCache.cpp b/src/frontends/qt/ColorCache.cpp
index 210f5c68c4..4955aaf3ce 100644
--- a/src/frontends/qt/ColorCache.cpp
+++ b/src/frontends/qt/ColorCache.cpp
@@ -18,6 +18,7 @@
 #include "Color.h"
 
 #include <cmath>
+#include <QPainter>
 
 namespace lyx {
 
@@ -180,6 +181,36 @@ bool ColorCache::isLightColor(RGBColor const & rgb)
 	return (L + 0.05) / (0.0 + 0.05) > (1.0 + 0.05) / (L + 0.05);
 }
 
+QPixmap ColorCache::setSwatchBackground(int width, int height)
+{
+	QPixmap background(width, height);
+	background.fill(Qt::white);
+
+	// paint a triangle
+	QPainter painter(&background);
+	painter.setRenderHint(QPainter::Antialiasing);
+	QPolygon polygon;
+	polygon << QPoint(width, 0)
+	        << QPoint(0, height)
+	        << QPoint(0, 0);
+	painter.setPen(QColor(Qt::black));
+	painter.setBrush(QColor(Qt::black));
+	painter.drawPolygon(polygon);
+	painter.end();
+
+	return background;
+}
+
+QPixmap ColorCache::mergePixmaps(QPixmap const *fg, QPixmap const *bg)
+{
+	// make a copy to leave bg untouched
+	QPixmap merged(*bg);
+	QPainter painter(&merged);
+	painter.drawPixmap(0, 0, *bg);
+	painter.drawPixmap(0, 0, *fg);
+	return merged;
+}
+
 
 QColor const rgb2qcolor(RGBColor const & rgb)
 {
diff --git a/src/frontends/qt/ColorCache.h b/src/frontends/qt/ColorCache.h
index ced8177b87..17e70a737d 100644
--- a/src/frontends/qt/ColorCache.h
+++ b/src/frontends/qt/ColorCache.h
@@ -60,6 +60,9 @@ public:
 	/// clear all colors
 	void clear() { initialized_ = false; }
 
+	static QPixmap setSwatchBackground(int width, int height);
+	static QPixmap mergePixmaps(QPixmap const *fg, QPixmap const *bg);
+
 private:
 	///
 	void init();
diff --git a/src/frontends/qt/GuiPrefs.cpp b/src/frontends/qt/GuiPrefs.cpp
index 29aa070489..f1357e42ce 100644
--- a/src/frontends/qt/GuiPrefs.cpp
+++ b/src/frontends/qt/GuiPrefs.cpp
@@ -4721,25 +4721,8 @@ ColorSwatchDelegate::ColorSwatchDelegate(QObject *parent)
 {
 	pane_ = static_cast<PrefColors*>(parent);
 
-	// construct the background image of swatches
-	bg_pixmap_ = new QPixmap(pane_->swatch_width_, pane_->swatch_height_);
-	bg_pixmap_->fill(Qt::white);
-	QPainter painter(bg_pixmap_);
-	painter.setRenderHint(QPainter::Antialiasing);
-	QPolygon polygon;
-	polygon << QPoint(pane_->swatch_width_, 0)
-	        << QPoint(0, pane_->swatch_height_)
-	        << QPoint(0, 0);
-	painter.setPen(QColor(Qt::black));
-	painter.setBrush(QColor(Qt::black));
-	painter.drawPolygon(polygon);
-	painter.end();
-}
-
-
-ColorSwatchDelegate::~ColorSwatchDelegate()
-{
-	delete bg_pixmap_;
+	bg_pixmap_ = ColorCache::setSwatchBackground(pane_->swatch_width_,
+	                                             pane_->swatch_height_);
 }
 
 
@@ -4765,7 +4748,7 @@ void ColorSwatchDelegate::paint(QPainter *painter,
 		        value<QColor>();
 		if (index.flags().testFlag(Qt::ItemIsEnabled)) {
 			pixmap.fill(color);
-			pixmap = mergePixmaps(&pixmap, bg_pixmap_);
+			pixmap = ColorCache::mergePixmaps(&pixmap, &bg_pixmap_);
 		} else
 			pixmap.fill(Qt::transparent);
 		style->drawItemPixmap(painter, opt.rect, Qt::AlignCenter, pixmap);
@@ -4776,16 +4759,6 @@ void ColorSwatchDelegate::paint(QPainter *painter,
 }
 
 
-QPixmap ColorSwatchDelegate::mergePixmaps(QPixmap const *fg, QPixmap const *bg) const
-{
-	// make a copy to leave bg untouched
-	QPixmap merged(*bg);
-	QPainter painter(&merged);
-	painter.drawPixmap(0, 0, *bg);
-	painter.drawPixmap(0, 0, *fg);
-	return merged;
-}
-
 } // namespace frontend
 } // namespace lyx
 
diff --git a/src/frontends/qt/GuiPrefs.h b/src/frontends/qt/GuiPrefs.h
index 441adc81c3..cae84feae4 100644
--- a/src/frontends/qt/GuiPrefs.h
+++ b/src/frontends/qt/GuiPrefs.h
@@ -735,15 +735,13 @@ class ColorSwatchDelegate : public QStyledItemDelegate
 
 public:
 	ColorSwatchDelegate(QObject *parent = nullptr);
-	~ColorSwatchDelegate();
 
 	void paint(QPainter *painter, const QStyleOptionViewItem &option,
 	           const QModelIndex &index) const override;
 
 private:
-	QPixmap mergePixmaps(QPixmap const * fg, QPixmap const * bg) const;
 	PrefColors* pane_;
-	QPixmap* bg_pixmap_;
+	QPixmap bg_pixmap_;
 	QFont font_;
 
 	// To give impression that a button is pressed, flip the direction of
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs