00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "ocolorpopupmenu.h"
00031 #include "ocolorbutton.h"
00032
00033
00034
00035
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
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;
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 }