File indexing completed on 2024-12-08 06:39:04
0001 /* This file is part of the KDE libraries 0002 SPDX-FileCopyrightText: 1999 Steffen Hansen <hansen@kde.org> 0003 SPDX-FileCopyrightText: 2005 Joseph Wenninger <jowenn@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kcolormimedata.h" 0009 0010 #include <QColor> 0011 #include <QDrag> 0012 #include <QMimeData> 0013 #include <QPainter> 0014 0015 void KColorMimeData::populateMimeData(QMimeData *mimeData, const QColor &color) 0016 { 0017 mimeData->setColorData(color); 0018 mimeData->setText(color.name()); 0019 } 0020 0021 bool KColorMimeData::canDecode(const QMimeData *mimeData) 0022 { 0023 if (mimeData->hasColor()) { 0024 return true; 0025 } 0026 if (mimeData->hasText()) { 0027 const QString colorName = mimeData->text(); 0028 if ((colorName.length() >= 4) && (colorName[0] == QLatin1Char('#'))) { 0029 return true; 0030 } 0031 } 0032 return false; 0033 } 0034 0035 QColor KColorMimeData::fromMimeData(const QMimeData *mimeData) 0036 { 0037 if (mimeData->hasColor()) { 0038 return mimeData->colorData().value<QColor>(); 0039 } 0040 if (canDecode(mimeData)) { 0041 return QColor(mimeData->text()); 0042 } 0043 return QColor(); 0044 } 0045 0046 QDrag *KColorMimeData::createDrag(const QColor &color, QObject *dragsource) 0047 { 0048 QDrag *drag = new QDrag(dragsource); 0049 QMimeData *mime = new QMimeData; 0050 populateMimeData(mime, color); 0051 drag->setMimeData(mime); 0052 QPixmap colorpix(25, 20); 0053 colorpix.fill(color); 0054 QPainter p(&colorpix); 0055 p.setPen(Qt::black); 0056 p.drawRect(0, 0, 24, 19); 0057 p.end(); 0058 drag->setPixmap(colorpix); 0059 drag->setHotSpot(QPoint(-5, -7)); 0060 return drag; 0061 }