1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 1999 Steffen Hansen <hansen@kde.org>
3 SPDX-FileCopyrightText: 2005 Joseph Wenninger <jowenn@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kcolormimedata.h"
9
10#include <QColor>
11#include <QDrag>
12#include <QMimeData>
13#include <QPainter>
14
15void KColorMimeData::populateMimeData(QMimeData *mimeData, const QColor &color)
16{
17 mimeData->setColorData(color);
18 mimeData->setText(color.name());
19}
20
21bool KColorMimeData::canDecode(const QMimeData *mimeData)
22{
23 if (mimeData->hasColor()) {
24 return true;
25 }
26 if (mimeData->hasText()) {
27 const QString colorName = mimeData->text();
28 if ((colorName.length() >= 4) && (colorName[0] == QLatin1Char('#'))) {
29 return true;
30 }
31 }
32 return false;
33}
34
35QColor KColorMimeData::fromMimeData(const QMimeData *mimeData)
36{
37 if (mimeData->hasColor()) {
38 return mimeData->colorData().value<QColor>();
39 }
40 if (canDecode(mimeData)) {
41 return QColor(mimeData->text());
42 }
43 return QColor();
44}
45
46QDrag *KColorMimeData::createDrag(const QColor &color, QObject *dragsource)
47{
48 QDrag *drag = new QDrag(dragsource);
49 QMimeData *mime = new QMimeData;
50 populateMimeData(mimeData: mime, color);
51 drag->setMimeData(mime);
52 QPixmap colorpix(25, 20);
53 colorpix.fill(fillColor: color);
54 QPainter p(&colorpix);
55 p.setPen(Qt::black);
56 p.drawRect(x: 0, y: 0, w: 24, h: 19);
57 p.end();
58 drag->setPixmap(colorpix);
59 drag->setHotSpot(QPoint(-5, -7));
60 return drag;
61}
62

source code of kguiaddons/src/colors/kcolormimedata.cpp