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