Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ocolorbutton.cpp

Go to the documentation of this file.
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 
00032 #include <qpe/resource.h>
00033 
00034 struct OColorButtonPrivate {
00035     QPopupMenu *m_menu;
00036     QColor m_color;
00037 };
00038 
00039 
00048 OColorButton::OColorButton ( QWidget *parent, const QColor &color, const char *name )
00049         : QPushButton ( parent, name )
00050 {
00051         d = new OColorButtonPrivate;
00052 
00053         d-> m_menu = new OColorPopupMenu ( color, 0, 0 );
00054         setPopup ( d-> m_menu );
00055 //      setPopupDelay ( 0 );
00056         connect ( d-> m_menu, SIGNAL( colorSelected(const QColor&)), this, SLOT( updateColor(const QColor&)));
00057 
00058         updateColor ( color );
00059 
00060         QSize s = sizeHint ( ) + QSize ( 12, 0 );
00061         setMinimumSize ( s );
00062         setMaximumSize ( s. width ( ) * 2, s. height ( ));
00063 }
00064 
00068 OColorButton::~OColorButton ( )
00069 {
00070         delete d;
00071 }
00072 
00076 QColor OColorButton::color ( ) const
00077 {
00078         return d-> m_color;
00079 }
00080 
00085 void OColorButton::setColor ( const QColor &c )
00086 {
00087         updateColor ( c );
00088 }
00089 
00093 void OColorButton::updateColor ( const QColor &c )
00094 {
00095         d-> m_color = c;
00096 
00097         QImage img ( 16, 16, 32 );
00098         img. fill ( 0 );
00099 
00100         int r, g, b;
00101         c. rgb ( &r, &g, &b );
00102 
00103         int w = img. width ( );
00104         int h = img. height ( );
00105 
00106         int dx = w * 20 / 100; // 15%
00107         int dy = h * 20 / 100;
00108 
00109         for ( int y = 0; y < h; y++ ) {
00110                 for ( int x = 0; x < w; x++ ) {
00111                         double alpha = 1.0;
00112 
00113                         if ( x < dx )
00114                                 alpha *= ( double ( x + 1 ) / dx );
00115                         else if ( x >= w - dx )
00116                                 alpha *= ( double ( w - x ) / dx );
00117                         if ( y < dy )
00118                                 alpha *= ( double ( y + 1 ) / dy );
00119                         else if ( y >= h - dy )
00120                                 alpha *= ( double ( h - y ) / dy );
00121 
00122                         int a = int ( alpha * 255.0 );
00123                         if ( a < 0 )
00124                                 a = 0;
00125                         if ( a > 255 )
00126                                 a = 255;
00127 
00128                         img. setPixel ( x, y, qRgba ( r, g, b, a ));
00129                 }
00130         }
00131         img. setAlphaBuffer ( true );
00132 
00133         QPixmap pix;
00134         pix. convertFromImage ( img );
00135         setPixmap ( pix );
00136 
00137         emit colorSelected ( c );
00138 }
00139 

Generated on Sat Nov 5 16:16:30 2005 for OPIE by  doxygen 1.4.2