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                              Copyright (C) Robert Griebl <sandman@handhelds.org>
00004               =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00005             .=l.
00006      .>+-=
00007 _;:,   .>  :=|.         This program is free software; you can
00008 .> <`_,  > .  <=          redistribute it and/or  modify it under
00009 :`=1 )Y*s>-.--  :           the terms of the GNU Library General Public
00010 .="- .-=="i,   .._         License as published by the Free Software
00011 - .  .-<_>   .<>         Foundation; either version 2 of the License,
00012   ._= =}    :          or (at your option) any later version.
00013   .%`+i>    _;_.
00014   .i_,=:_.   -<s.       This program is distributed in the hope that
00015   + . -:.    =       it will be useful,  but WITHOUT ANY WARRANTY;
00016   : ..  .:,   . . .    without even the implied warranty of
00017   =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
00018  _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU
00019 ..}^=.=    =    ;      Library General Public License for more
00020 ++=  -.   .`   .:       details.
00021 :   = ...= . :.=-
00022 -.  .:....=;==+<;          You should have received a copy of the GNU
00023  -_. . .  )=. =           Library General Public License along with
00024   --    :-=`           this library; see the file COPYING.LIB.
00025                              If not, write to the Free Software Foundation,
00026                              Inc., 59 Temple Place - Suite 330,
00027                              Boston, MA 02111-1307, USA.
00028 */
00029 
00030 #include "ocolorpopupmenu.h"
00031 #include "ocolorbutton.h"
00032 
00033 /* OPIE */
00034 
00035 /* QT */
00036 #include <qimage.h>
00037 
00038 using namespace Opie;
00039 
00040 struct OColorButtonPrivate
00041 {
00042     QPopupMenu *m_menu;
00043     QColor m_color;
00044 };
00045 
00054 OColorButton::OColorButton ( QWidget *parent, const QColor &color, const char *name )
00055         : QPushButton ( parent, name )
00056 {
00057     d = new OColorButtonPrivate;
00058 
00059     d-> m_menu = new OColorPopupMenu ( color, 0, 0 );
00060     setPopup ( d-> m_menu );
00061     //  setPopupDelay ( 0 );
00062     connect ( d-> m_menu, SIGNAL( colorSelected(const QColor&)), this, SLOT( updateColor(const QColor&)));
00063 
00064 
00065     QSize s = sizeHint ( ) + QSize ( 12, 0 );
00066     setMinimumSize ( s );
00067     setMaximumSize ( s. width ( ) * 2, s. height ( ));
00068     d->m_color = color;
00069 }
00070 
00074 OColorButton::~OColorButton ( )
00075 {
00076     delete d->m_menu;
00077     delete d;
00078 }
00079 
00083 QColor OColorButton::color ( ) const
00084 {
00085     return d-> m_color;
00086 }
00087 
00092 void OColorButton::setColor ( const QColor &c )
00093 {
00094     updateColor ( c, true );
00095 }
00096 
00100 void OColorButton::updateColor ( const QColor &c )
00101 {
00102     updateColor( c, true );
00103 }
00104 
00105 void OColorButton::updateColor ( const QColor &c, bool sendSignal )
00106 {
00107     d-> m_color = c;
00108 
00109     QImage img ( width()-14, height()-6, 32 );
00110     img. fill ( 0 );
00111 
00112     int r, g, b;
00113     c. rgb ( &r, &g, &b );
00114 
00115     int w = img. width ( );
00116     int h = img. height ( );
00117 
00118     int dx = w * 20 / 100; // 15%
00119     int dy = h * 20 / 100;
00120 
00121     for ( int y = 0; y < h; y++ )
00122     {
00123         for ( int x = 0; x < w; x++ )
00124         {
00125             double alpha = 1.0;
00126 
00127             if ( x < dx )
00128                 alpha *= ( double ( x + 1 ) / dx );
00129             else if ( x >= w - dx )
00130                 alpha *= ( double ( w - x ) / dx );
00131             if ( y < dy )
00132                 alpha *= ( double ( y + 1 ) / dy );
00133             else if ( y >= h - dy )
00134                 alpha *= ( double ( h - y ) / dy );
00135 
00136             int a = int ( alpha * 255.0 );
00137             if ( a < 0 )
00138                 a = 0;
00139             if ( a > 255 )
00140                 a = 255;
00141 
00142             img. setPixel ( x, y, qRgba ( r, g, b, a ));
00143         }
00144     }
00145     img. setAlphaBuffer ( true );
00146 
00147     QPixmap pix;
00148     pix. convertFromImage ( img );
00149     setPixmap ( pix );
00150 
00151     if ( sendSignal )
00152         emit colorSelected ( c );
00153 }
00154 
00155 
00159 void OColorButton::resizeEvent( QResizeEvent *ev ) {
00160     QPushButton::resizeEvent( ev );
00161     updateColor( d->m_color, false );
00162 }

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