libopie API Documentation

ocolorbutton.cpp

00001 /*
00002                =.            This file is part of the OPIE Project
00003              .=l.            Copyright (c)  2002 Robert Griebl <sandman@handhelds.org>
00004            .>+-=
00005  _;:,     .>    :=|.         This library is free software; you can
00006 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00007 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00008 .="- .-=="i,     .._         License as published by the Free Software
00009  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00010      ._= =}       :          or (at your option) any later version.
00011     .%`+i>       _;_.
00012     .i_,=:_.      -<s.       This library is distributed in the hope that
00013      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00014     : ..    .:,     . . .    without even the implied warranty of
00015     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00016   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00017 ..}^=.=       =       ;      Library General Public License for more
00018 ++=   -.     .`     .:       details.
00019  :     =  ...= . :.=-
00020  -.   .:....=;==+<;          You should have received a copy of the GNU
00021   -_. . .   )=.  =           Library General Public License along with
00022     --        :-=`           this library; see the file COPYING.LIB.
00023                              If not, write to the Free Software Foundation,
00024                              Inc., 59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 #include <opie/colorpopupmenu.h>
00030 #include <opie/ocolorbutton.h>
00031 #include <qcolor.h>
00032 #include <qpixmap.h>
00033 #include <qimage.h>
00034 
00035 #include <qpe/resource.h>
00036 
00037 struct OColorButtonPrivate {
00038     QPopupMenu *m_menu;
00039     QColor m_color;
00040 };
00041 
00042 
00051 OColorButton::OColorButton ( QWidget *parent, const QColor &color, const char *name )
00052     : QPushButton ( parent, name )
00053 {
00054     d = new OColorButtonPrivate;
00055 
00056     d-> m_menu = new OColorPopupMenu ( color, 0, 0 );
00057     setPopup ( d-> m_menu );
00058 //  setPopupDelay ( 0 );
00059     connect ( d-> m_menu, SIGNAL( colorSelected ( const QColor & )), this, SLOT( updateColor ( const QColor & )));
00060 
00061     updateColor ( color );
00062 
00063     QSize s = sizeHint ( ) + QSize ( 12, 0 );
00064     setMinimumSize ( s );
00065     setMaximumSize ( s. width ( ) * 2, s. height ( ));
00066 }
00067 
00071 OColorButton::~OColorButton ( )
00072 {
00073     delete d;
00074 }
00075 
00079 QColor OColorButton::color ( ) const
00080 {
00081     return d-> m_color;
00082 }
00083 
00088 void OColorButton::setColor ( const QColor &c )
00089 {
00090     updateColor ( c );
00091 }
00092 
00096 void OColorButton::updateColor ( const QColor &c )
00097 {
00098     d-> m_color = c;
00099 
00100     QImage img ( 16, 16, 32 );
00101     img. fill ( 0 );
00102 
00103     int r, g, b;
00104     c. rgb ( &r, &g, &b );
00105 
00106     int w = img. width ( );
00107     int h = img. height ( );
00108 
00109     int dx = w * 20 / 100; // 15%
00110     int dy = h * 20 / 100;
00111 
00112     for ( int y = 0; y < h; y++ ) {
00113         for ( int x = 0; x < w; x++ ) {
00114             double alpha = 1.0;
00115 
00116             if ( x < dx )
00117                 alpha *= ( double ( x + 1 ) / dx );
00118             else if ( x >= w - dx )
00119                 alpha *= ( double ( w - x ) / dx );
00120             if ( y < dy )
00121                 alpha *= ( double ( y + 1 ) / dy );
00122             else if ( y >= h - dy )
00123                 alpha *= ( double ( h - y ) / dy );
00124 
00125             int a = int ( alpha * 255.0 );
00126             if ( a < 0 )
00127                 a = 0;
00128             if ( a > 255 )
00129                 a = 255;
00130 
00131             img. setPixel ( x, y, qRgba ( r, g, b, a ));
00132         }
00133     }
00134     img. setAlphaBuffer ( true );
00135 
00136     QPixmap pix;
00137     pix. convertFromImage ( img );
00138     setPixmap ( pix );
00139 
00140     emit colorSelected ( c );
00141 }
00142 
KDE Logo
This file is part of the documentation for OPIE Version 1.1.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:42 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001