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