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 #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
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;
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