kdeui Library API Documentation

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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 /*
00020  * $Id: kcolordrag.cpp,v 1.8 2003/08/04 10:29:52 waba Exp $
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 { /*BASE::virtual_hook( id, data );*/ }
00114 
00115 #include "kcolordrag.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 22 14:23:24 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003