kcolordrag.cpp Source File (kdeui Library)

| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | 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  * $Id: kcolordrag.cpp,v 1.5 2002/03/04 00:51:46 lunakl Exp $
    00021  *
    00022  * $Log: kcolordrag.cpp,v $
    00023  * Revision 1.5  2002/03/04 00:51:46  lunakl
    00024  * Keep BC changes (the patch is almost 100KiB of boring stuff
    00025  * ... anybody willing to review? ;) ).
    00026  *
    00027  * Revision 1.4  2001/10/15 18:04:25  gehrmab
    00028  * The preferred location for constructing C++ objects is in the constructor ;-)
    00029  *
    00030  * Revision 1.3  2001/08/26 20:14:19  lunakl
    00031  * Ok, watch closely :
    00032  * const is your friend !
    00033  *
    00034  * static int blah[] = { 1, 2, .... }
    00035  * should be
    00036  * static const int blah[] = { 1, 2, ... }
    00037  *
    00038  * static const char* txt[] = { "blah", "foo", ... }
    00039  * should be
    00040  * static const char* const txt[] = { "blah", "foo", ... }
    00041  *
    00042  * And just in case you wonder about those const_cast< const char** >, that's
    00043  * because QPixmap( const char** xpm ) and QImage( const char** xpm ) got it
    00044  * wrong too. Everybody guessing correctly where the const is missing wins
    00045  * a free cvs update.
    00046  *
    00047  * Revision 1.2  2000/02/12 12:46:41  espen
    00048  * Added a black frame around the drag pixmap. Looks better.
    00049  *
    00050  * Revision 1.1  1999/05/06 02:46:13  steffen
    00051  * Drag&drop for colors. Qt drag&drop is really easy to use. We should have stuff like this all over KDE.
    00052  *
    00053  *
    00054  */
    00055
    00056 #include <qpainter.h>
    00057 #include "kcolordrag.h"
    00058
    00059 static const char * const color_mime_string = "application/x-color";
    00060
    00061 KColorDrag::KColorDrag( const QColor &color, QWidget *dragsource,
    00062                         const char *name)
    00063      : QStoredDrag( color_mime_string, dragsource, name)
    00064 {
    00065      setColor( color);
    00066 }
    00067
    00068 KColorDrag::KColorDrag( QWidget *dragsource, const char *name)
    00069      : QStoredDrag( color_mime_string, dragsource, name)
    00070 {
    00071     setColor( white );
    00072 }
    00073
    00074 void
    00075 KColorDrag::setColor( const QColor &color)
    00076 {
    00077      QByteArray data;
    00078      // A short int for each of R G B Alpha (compatible with gtk);
    00079      unsigned short int  rgba[4];
    00080      data.resize( sizeof(rgba));
    00081      rgba[0] = color.red()   * 0xFF;
    00082      rgba[1] = color.green() * 0xFF;
    00083      rgba[2] = color.blue()  * 0xFF;
    00084      rgba[3] = 0xFFFF; // Alpha not supported yet.
    00085      memcpy( data.data(), rgba, sizeof( rgba));
    00086      setEncodedData( data);
    00087
    00088      QPixmap colorpix( 25, 20);
    00089      colorpix.fill( color);
    00090      QPainter p( &colorpix );
    00091      p.setPen( black );
    00092      p.drawRect(0,0,25,20);
    00093      p.end();
    00094      setPixmap(colorpix, QPoint(-5,-7));
    00095 }
    00096
    00097 bool
    00098 KColorDrag::canDecode( QMimeSource *e)
    00099 {
    00100      return e->provides( color_mime_string);
    00101 }
    00102
    00103 bool
    00104 KColorDrag::decode( QMimeSource *e, QColor &color)
    00105 {
    00106      QByteArray data = e->encodedData( color_mime_string);
    00107      unsigned short int rgba[4];
    00108      if( data.size() != sizeof(rgba)) return false;
    00109      memcpy( rgba, data.data(), sizeof( rgba));
    00110      color.setRgb( rgba[0] / 0xFF, rgba[1] / 0xFF, rgba[2] / 0xFF);
    00111      return true;
    00112 }
    00113
    00114
    00115 KColorDrag*
    00116 KColorDrag::makeDrag( const QColor &color,QWidget *dragsource)
    00117 {
    00118      return new KColorDrag( color, dragsource);
    00119 }
    00120
    00121 void KColorDrag::virtual_hook( int, void* )
    00122 { /*BASE::virtual_hook( id, data );*/ }
    00123
    00124 #include "kcolordrag.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:56:20 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001