| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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 #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 { /*BASE::virtual_hook( id, data );*/ }
    00111
    00112 #include "kcolordrag.moc"

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:20:14 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003