ocolorbutton.cpp
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 #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
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;
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
This file is part of the documentation for OPIE Version 1.1.