kdeui
kcolordrag.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qpainter.h>
00021 #include "kcolordrag.h"
00022
00023 static const char * const color_mime_string = "application/x-color";
00024 static const char * const text_mime_string = "text/plain";
00025
00026 KColorDrag::KColorDrag( const QColor &color, QWidget *dragsource,
00027 const char *name)
00028 : QStoredDrag( color_mime_string, dragsource, name)
00029 {
00030 setColor( color);
00031 }
00032
00033 KColorDrag::KColorDrag( QWidget *dragsource, const char *name)
00034 : QStoredDrag( color_mime_string, dragsource, name)
00035 {
00036 setColor( white );
00037 }
00038
00039 void
00040 KColorDrag::setColor( const QColor &color)
00041 {
00042 QColorDrag tmp(color, 0, 0);
00043 setEncodedData(tmp.encodedData(color_mime_string));
00044
00045 QPixmap colorpix( 25, 20);
00046 colorpix.fill( color);
00047 QPainter p( &colorpix );
00048 p.setPen( black );
00049 p.drawRect(0,0,25,20);
00050 p.end();
00051 setPixmap(colorpix, QPoint(-5,-7));
00052 }
00053
00054 const char *KColorDrag::format(int i) const
00055 {
00056 if (i==1)
00057 return text_mime_string;
00058 else
00059 return QStoredDrag::format(i);
00060 }
00061
00062 QByteArray KColorDrag::encodedData ( const char * m ) const
00063 {
00064 if (!qstrcmp(m, text_mime_string) )
00065 {
00066 QColor color;
00067 QColorDrag::decode(const_cast<KColorDrag *>(this), color);
00068 QCString result = color.name().latin1();
00069 ((QByteArray&)result).resize(result.length());
00070 return result;
00071 }
00072 return QStoredDrag::encodedData(m);
00073 }
00074
00075 bool
00076 KColorDrag::canDecode( QMimeSource *e)
00077 {
00078 if (e->provides(color_mime_string))
00079 return true;
00080 if (e->provides(text_mime_string))
00081 {
00082 QColor dummy;
00083 return decode(e, dummy);
00084 }
00085 return false;
00086 }
00087
00088 bool
00089 KColorDrag::decode( QMimeSource *e, QColor &color)
00090 {
00091 if (QColorDrag::decode(e, color))
00092 return true;
00093
00094 QByteArray data = e->encodedData( text_mime_string);
00095 QString colorName = QString::fromLatin1(data.data(), data.size());
00096 if ((colorName.length() < 4) || (colorName[0] != '#'))
00097 return false;
00098 color.setNamedColor(colorName);
00099 return color.isValid();
00100 }
00101
00102
00103 KColorDrag*
00104 KColorDrag::makeDrag( const QColor &color,QWidget *dragsource)
00105 {
00106 return new KColorDrag( color, dragsource);
00107 }
00108
00109 void KColorDrag::virtual_hook( int, void* )
00110 { }
00111
00112 #include "kcolordrag.moc"