KDEUI
kcolormimedata.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Steffen Hansen (hansen@kde.org) 00003 Copyright (C) 2005 Joseph Wenninger (jowenn@kde.org) 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kcolormimedata.h" 00022 00023 #include <QColor> 00024 #include <QDrag> 00025 #include <QMimeData> 00026 #include <QPainter> 00027 00028 void 00029 KColorMimeData::populateMimeData(QMimeData *mimeData, const QColor &color) 00030 { 00031 mimeData->setColorData(color); 00032 mimeData->setText(color.name()); 00033 } 00034 00035 bool 00036 KColorMimeData::canDecode(const QMimeData *mimeData) 00037 { 00038 if (mimeData->hasColor()) 00039 return true; 00040 if (mimeData->hasText()) 00041 { 00042 const QString colorName=mimeData->text(); 00043 if ((colorName.length() >= 4) && (colorName[0] == '#')) 00044 return true; 00045 } 00046 return false; 00047 } 00048 00049 QColor 00050 KColorMimeData::fromMimeData(const QMimeData *mimeData) 00051 { 00052 if (mimeData->hasColor()) 00053 return mimeData->colorData().value<QColor>(); 00054 if (canDecode(mimeData)) 00055 return QColor(mimeData->text()); 00056 return QColor(); 00057 } 00058 00059 00060 QDrag* 00061 KColorMimeData::createDrag(const QColor &color, QWidget *dragsource) 00062 { 00063 QDrag *drag=new QDrag(dragsource); 00064 QMimeData *mime=new QMimeData; 00065 populateMimeData(mime,color); 00066 drag->setMimeData(mime); 00067 QPixmap colorpix( 25, 20 ); 00068 colorpix.fill( color ); 00069 QPainter p( &colorpix ); 00070 p.setPen( Qt::black ); 00071 p.drawRect(0,0,24,19); 00072 p.end(); 00073 drag->setPixmap(colorpix); 00074 drag->setHotSpot(QPoint(-5,-7)); 00075 return drag; 00076 }