kcolordrag.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Steffen Hansen (hansen@kde.org) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <tqpainter.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 TQColor &color, TQWidget *dragsource, 00027 const char *name) 00028 : TQStoredDrag( color_mime_string, dragsource, name) 00029 { 00030 setColor( color); 00031 } 00032 00033 KColorDrag::KColorDrag( TQWidget *dragsource, const char *name) 00034 : TQStoredDrag( color_mime_string, dragsource, name) 00035 { 00036 setColor( white ); 00037 } 00038 00039 void 00040 KColorDrag::setColor( const TQColor &color) 00041 { 00042 TQColorDrag tmp(color, 0, 0); 00043 setEncodedData(tmp.encodedData(color_mime_string)); 00044 00045 TQPixmap colorpix( 25, 20); 00046 colorpix.fill( color); 00047 TQPainter p( &colorpix ); 00048 p.setPen( black ); 00049 p.drawRect(0,0,25,20); 00050 p.end(); 00051 setPixmap(colorpix, TQPoint(-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 TQStoredDrag::format(i); 00060 } 00061 00062 TQByteArray KColorDrag::encodedData ( const char * m ) const 00063 { 00064 if (!qstrcmp(m, text_mime_string) ) 00065 { 00066 TQColor color; 00067 TQColorDrag::decode(const_cast<KColorDrag *>(this), color); 00068 TQCString result = TQString(color.name()).latin1(); 00069 ((TQByteArray&)result).resize(result.length()); 00070 return result; 00071 } 00072 return TQStoredDrag::encodedData(m); 00073 } 00074 00075 bool 00076 KColorDrag::canDecode( TQMimeSource *e) 00077 { 00078 if (e->provides(color_mime_string)) 00079 return true; 00080 if (e->provides(text_mime_string)) 00081 { 00082 TQColor dummy; 00083 return decode(e, dummy); 00084 } 00085 return false; 00086 } 00087 00088 bool 00089 KColorDrag::decode( TQMimeSource *e, TQColor &color) 00090 { 00091 if (TQColorDrag::decode(e, color)) 00092 return true; 00093 00094 TQByteArray data = e->encodedData( text_mime_string); 00095 TQString colorName = TQString::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 TQColor &color,TQWidget *dragsource) 00105 { 00106 return new KColorDrag( color, dragsource); 00107 } 00108 00109 void KColorDrag::virtual_hook( int, void* ) 00110 { /*BASE::virtual_hook( id, data );*/ } 00111 00112 #include "kcolordrag.moc"