[frameworks/kwidgetsaddons] src: Split off KColorMimeData copy into separate file, for shared internal usage

Friedrich W. H. Kossebau <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit b60b283daed3d0e394b467d0d6b651c4d5ac7cee by Friedrich W. H. Kossebau.
Committed on 24/07/2026 at 20:24.
Pushed by kossebau into branch 'master'.

Split off KColorMimeData copy into separate file, for shared internal usage

M  +2    -0    src/CMakeLists.txt
M  +7    -57   src/kcolorbutton.cpp
A  +67   -0    src/kcolormimedata.cpp     [License: LGPL(v2.0+)]
A  +54   -0    src/kcolormimedata_p.h     [License: LGPL(v2.0+)]

https://invent.kde.org/frameworks/kwidgetsaddons/-/commit/b60b283daed3d0e394b467d0d6b651c4d5ac7cee

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a19ac221..b8ebf8b7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -45,6 +45,8 @@ target_sources(KF6WidgetsAddons PRIVATE
     kcolorbutton.h
     kcolorcombo.cpp
     kcolorcombo.h
+    kcolormimedata.cpp
+    kcolormimedata_p.h
     kcolumnresizer.cpp
     kcolumnresizer.h
     kcontextualhelpbutton.cpp
diff --git a/src/kcolorbutton.cpp b/src/kcolorbutton.cpp
index 9b7e2d69..5be3142f 100644
--- a/src/kcolorbutton.cpp
+++ b/src/kcolorbutton.cpp
@@ -8,6 +8,8 @@
 
 #include "kcolorbutton.h"
 
+#include "kcolormimedata_p.h"
+
 #include <QApplication>
 #include <QClipboard>
 #include <QColorDialog>
@@ -41,58 +43,6 @@ public:
     void initStyleOption(QStyleOptionButton *opt) const;
 };
 
-/////////////////////////////////////////////////////////////////////
-// Functions duplicated from KColorMimeData
-// Should be kept in sync
-void populateMimeData(QMimeData *mimeData, const QColor &color)
-{
-    mimeData->setColorData(color);
-    mimeData->setText(color.name());
-}
-
-bool canDecode(const QMimeData *mimeData)
-{
-    if (mimeData->hasColor()) {
-        return true;
-    }
-    if (mimeData->hasText()) {
-        const QString colorName = mimeData->text();
-        if ((colorName.length() >= 4) && (colorName[0] == QLatin1Char('#'))) {
-            return true;
-        }
-    }
-    return false;
-}
-
-QColor fromMimeData(const QMimeData *mimeData)
-{
-    if (mimeData->hasColor()) {
-        return mimeData->colorData().value<QColor>();
-    }
-    if (canDecode(mimeData)) {
-        return QColor(mimeData->text());
-    }
-    return QColor();
-}
-
-QDrag *createDrag(const QColor &color, QObject *dragsource)
-{
-    QDrag *drag = new QDrag(dragsource);
-    QMimeData *mime = new QMimeData;
-    populateMimeData(mime, color);
-    drag->setMimeData(mime);
-    QPixmap colorpix(25, 20);
-    colorpix.fill(color);
-    QPainter p(&colorpix);
-    p.setPen(Qt::black);
-    p.drawRect(0, 0, 24, 19);
-    p.end();
-    drag->setPixmap(colorpix);
-    drag->setHotSpot(QPoint(-5, -7));
-    return drag;
-}
-/////////////////////////////////////////////////////////////////////
-
 KColorButtonPrivate::KColorButtonPrivate(KColorButton *qq)
     : q(qq)
 {
@@ -241,12 +191,12 @@ QSize KColorButton::minimumSizeHint() const
 
 void KColorButton::dragEnterEvent(QDragEnterEvent *event)
 {
-    event->setAccepted(canDecode(event->mimeData()) && isEnabled());
+    event->setAccepted(KColorMimeData::canDecode(event->mimeData()) && isEnabled());
 }
 
 void KColorButton::dropEvent(QDropEvent *event)
 {
-    QColor c = fromMimeData(event->mimeData());
+    QColor c = KColorMimeData::fromMimeData(event->mimeData());
     if (c.isValid()) {
         setColor(c);
     }
@@ -258,10 +208,10 @@ void KColorButton::keyPressEvent(QKeyEvent *e)
 
     if (QKeySequence::keyBindings(QKeySequence::Copy).contains(key)) {
         QMimeData *mime = new QMimeData;
-        populateMimeData(mime, color());
+        KColorMimeData::populateMimeData(mime, color());
         QApplication::clipboard()->setMimeData(mime, QClipboard::Clipboard);
     } else if (QKeySequence::keyBindings(QKeySequence::Paste).contains(key)) {
-        QColor color = fromMimeData(QApplication::clipboard()->mimeData(QClipboard::Clipboard));
+        QColor color = KColorMimeData::fromMimeData(QApplication::clipboard()->mimeData(QClipboard::Clipboard));
         setColor(color);
     } else {
         QPushButton::keyPressEvent(e);
@@ -277,7 +227,7 @@ void KColorButton::mousePressEvent(QMouseEvent *e)
 void KColorButton::mouseMoveEvent(QMouseEvent *e)
 {
     if ((e->buttons() & Qt::LeftButton) && (e->pos() - d->mPos).manhattanLength() > QApplication::startDragDistance()) {
-        createDrag(color(), this)->exec(Qt::CopyAction);
+        KColorMimeData::createDrag(color(), this)->exec(Qt::CopyAction);
         setDown(false);
     }
 }
diff --git a/src/kcolormimedata.cpp b/src/kcolormimedata.cpp
new file mode 100644
index 00000000..960bf09e
--- /dev/null
+++ b/src/kcolormimedata.cpp
@@ -0,0 +1,67 @@
+/*
+    This file is part of the KDE libraries
+    SPDX-FileCopyrightText: 1999 Steffen Hansen <[email protected]>
+    SPDX-FileCopyrightText: 2005 Joseph Wenninger <[email protected]>
+
+    SPDX-License-Identifier: LGPL-2.0-or-later
+*/
+
+#include "kcolormimedata_p.h"
+
+#include <QColor>
+#include <QDrag>
+#include <QMimeData>
+#include <QPainter>
+
+namespace KColorMimeData
+{
+
+void populateMimeData(QMimeData *mimeData, const QColor &color)
+{
+    mimeData->setColorData(color);
+    mimeData->setText(color.name());
+}
+
+bool canDecode(const QMimeData *mimeData)
+{
+    if (mimeData->hasColor()) {
+        return true;
+    }
+    if (mimeData->hasText()) {
+        const QString colorName = mimeData->text();
+        if ((colorName.length() >= 4) && (colorName[0] == QLatin1Char('#'))) {
+            return true;
+        }
+    }
+    return false;
+}
+
+QColor fromMimeData(const QMimeData *mimeData)
+{
+    if (mimeData->hasColor()) {
+        return mimeData->colorData().value<QColor>();
+    }
+    if (canDecode(mimeData)) {
+        return QColor(mimeData->text());
+    }
+    return QColor();
+}
+
+QDrag *createDrag(const QColor &color, QObject *dragsource)
+{
+    QDrag *drag = new QDrag(dragsource);
+    QMimeData *mime = new QMimeData;
+    populateMimeData(mime, color);
+    drag->setMimeData(mime);
+    QPixmap colorpix(25, 20);
+    colorpix.fill(color);
+    QPainter p(&colorpix);
+    p.setPen(Qt::black);
+    p.drawRect(0, 0, 24, 19);
+    p.end();
+    drag->setPixmap(colorpix);
+    drag->setHotSpot(QPoint(-5, -7));
+    return drag;
+}
+
+}
diff --git a/src/kcolormimedata_p.h b/src/kcolormimedata_p.h
new file mode 100644
index 00000000..d5ddb6e2
--- /dev/null
+++ b/src/kcolormimedata_p.h
@@ -0,0 +1,54 @@
+/*
+    This file is part of the KDE libraries
+    SPDX-FileCopyrightText: 1999 Steffen Hansen <[email protected]>
+    SPDX-FileCopyrightText: 2005 Joseph Wenninger <[email protected]>
+
+    SPDX-License-Identifier: LGPL-2.0-or-later
+*/
+
+#ifndef KCOLORMIMEDATA_P_H
+#define KCOLORMIMEDATA_P_H
+
+class QColor;
+class QDrag;
+class QMimeData;
+class QObject;
+
+/*!
+ * \internal
+ * \namespace KColorMimeData
+ * Functions duplicated from KGuiAddons's KColorMimeData
+ * Should be kept in sync.
+ */
+namespace KColorMimeData
+{
+/*!
+ * Sets the color and text representation fields for the specified color in the mimedata object:
+ * application/x-color and text/plain types are set
+ */
+void populateMimeData(QMimeData *mimeData, const QColor &color);
+
+/*!
+ * Returns true if the MIME data \a mimeData contains a color object.
+ * First checks for application/x-color and if that fails, for a text/plain entry, which
+ * represents a color in the format \#hexnumbers
+ */
+bool canDecode(const QMimeData *mimeData);
+
+/*!
+ * Decodes the MIME data \a mimeData and returns the resulting color.
+ * First tries application/x-color and if that fails, a text/plain entry, which
+ * represents a color in the format \#hexnumbers. If this fails too,
+ * an invalid QColor object is returned, use QColor::isValid() to test it.
+ */
+QColor fromMimeData(const QMimeData *mimeData);
+
+/*!
+ * Creates a color drag object. Either you have to start this drag or delete it
+ * The drag object's mime data has the application/x-color and text/plain type set and a pixmap
+ * filled with the specified color, which is going to be displayed next to the mouse cursor
+ */
+QDrag *createDrag(const QColor &color, QObject *dragsource);
+}
+
+#endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.