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 "oledbox.h"
00031
00032
00033 #include <qbitmap.h>
00034 #include <qpainter.h>
00035
00036 namespace Opie {
00037 namespace Ui {
00038
00039
00040 static const char * ledborder_xpm[] = {
00041 "16 16 11 1",
00042 " c None",
00043 ". c #626562",
00044 "+ c #7B7D7B",
00045 "@ c #949594",
00046 "# c #ACAEAC",
00047 "$ c #CDCACD",
00048 "% c #CDCECD",
00049 "; c #E6E6E6",
00050 "> c #FFFFFF",
00051 ", c #E6E2E6",
00052 "' c #FFFAFF",
00053 " .++@@# ",
00054 " ...++@@##$ ",
00055 " .....+@##$$% ",
00056 " ..... #$%%% ",
00057 " ... %%; ",
00058 ".... ;;;;",
00059 "++. ;>>",
00060 "+++ >>>",
00061 "@@@ >>>",
00062 "@@# >>>",
00063 "#### >>>>",
00064 " #$$ >>> ",
00065 " $$,,' >>>>> ",
00066 " ,,,''>>>>>>> ",
00067 " ,''>>>>>>> ",
00068 " '>>>>> "};
00069
00070
00071 QPixmap *OLedBox::s_border_pix = 0;
00072
00073 OLedBox::OLedBox ( const QColor &col, QWidget *parent, const char *name ) : QWidget ( parent, name )
00074 {
00075 m_color = col;
00076 m_on = false;
00077 m_readonly = true;
00078
00079 m_pix [ 0 ] = m_pix [ 1 ] = 0;
00080
00081 setBackgroundMode ( PaletteBackground );
00082
00083 if ( !s_border_pix )
00084 s_border_pix = new QPixmap( ledborder_xpm );
00085 }
00086
00087 OLedBox::~OLedBox ( )
00088 {
00089 delete m_pix [ 0 ];
00090 delete m_pix [ 1 ];
00091 }
00092
00093 QSize OLedBox::sizeHint ( ) const
00094 {
00095 return QSize ( 16, 16 );
00096 }
00097
00098 bool OLedBox::isOn ( ) const
00099 {
00100 return m_on;
00101 }
00102
00103 QColor OLedBox::color ( ) const
00104 {
00105 return m_color;
00106 }
00107
00108 void OLedBox::setOn ( bool b )
00109 {
00110 if ( m_on != b ) {
00111 m_on = b;
00112 update ( );
00113 }
00114 }
00115
00116 void OLedBox::toggle ( )
00117 {
00118 setOn ( !isOn ( ) );
00119 }
00120
00121 void OLedBox::setColor ( const QColor &col )
00122 {
00123 if ( m_color != col ) {
00124 m_color = col;
00125
00126 delete m_pix [ 0 ];
00127 delete m_pix [ 1 ];
00128 m_pix[0] = m_pix[1] = 0;
00129 update ( );
00130 }
00131 }
00132
00133 void OLedBox::mousePressEvent ( QMouseEvent *e )
00134 {
00135 if ( ! m_readonly &&
00136 e-> button ( ) == LeftButton ) {
00137 m_on = !m_on;
00138 update ( );
00139 emit toggled ( m_on );
00140 }
00141 }
00142
00143
00144 void OLedBox::resizeEvent ( QResizeEvent * )
00145 {
00146 delete m_pix [ 0 ];
00147 delete m_pix [ 1 ];
00148 m_pix[0] = m_pix[1] = 0;
00149
00150 update ( );
00151 }
00152
00153 void OLedBox::paintEvent ( QPaintEvent *e )
00154 {
00155 int ind = m_on ? 1 : 0;
00156
00157 if ( !m_pix [ ind ] ) {
00158 m_pix [ ind ] = new QPixmap ( size ( ));
00159
00160 drawLed ( m_pix [ ind ], m_on ? m_color : m_color. dark ( 300 ) );
00161 }
00162 if ( !e-> erased ( ))
00163 erase ( );
00164
00165 QPainter p ( this );
00166 p. drawPixmap ( 0, 0, *m_pix [ ind ] );
00167 }
00168
00169 void OLedBox::drawLed ( QPixmap *pix, const QColor &col )
00170 {
00171 QPainter paint;
00172 QColor color;
00173 QBrush brush;
00174 QPen pen;
00175
00176 pix-> fill ( black );
00177
00178
00179
00180 int width = pix-> width ( );
00181
00182
00183 if ( width > pix-> height ( ))
00184 width = pix-> height ( );
00185 width -= 2;
00186 if ( width < 0 )
00187 width = 0;
00188
00189
00190
00191
00192
00193 paint.begin( pix );
00194
00195
00196 color = col;
00197
00198
00199
00200 brush.setStyle( QBrush::SolidPattern );
00201 brush.setColor( color );
00202 paint.setBrush( brush );
00203
00204
00205 paint.drawEllipse( 1, 1, width, width );
00206
00207
00208
00209
00210
00211
00212 pen.setWidth( 2 );
00213
00214
00215 int pos = width / 5 + 1;
00216 int light_width = width;
00217 light_width *= 2;
00218 light_width /= 3;
00219
00220
00221 int light_quote = ( 130 * 2 / ( light_width ? light_width : 1 ) ) + 100;
00222
00223
00224 while ( light_width )
00225 {
00226 color = color.light( light_quote );
00227 pen.setColor( color );
00228 paint.setPen( pen );
00229 paint.drawEllipse( pos, pos, light_width, light_width );
00230 light_width--;
00231 if ( !light_width )
00232 break;
00233 paint.drawEllipse( pos, pos, light_width, light_width );
00234 light_width--;
00235 if ( !light_width )
00236 break;
00237 paint.drawEllipse( pos, pos, light_width, light_width );
00238 pos++;
00239 light_width--;
00240 }
00241
00242
00243
00244
00245
00246 #ifndef QT_CAN_DRAW_ARCS
00247 paint.drawPixmap ( 0, 0, *s_border_pix );
00248 paint.end ( );
00249
00250 pix->setMask ( pix-> createHeuristicMask ( ));
00251
00252 #else
00253 pen.setWidth( 3 );
00254 brush.setStyle( QBrush::NoBrush );
00255 paint.setBrush( brush );
00256
00257
00258
00259 int shadow_color = 200, angle;
00260
00261 for ( angle = 720; angle < 6480; angle += 240 )
00262 {
00263 color.setRgb( shadow_color, shadow_color, shadow_color );
00264 pen.setColor( color );
00265 paint.setPen( pen );
00266 paint.drawArc( 0, 0, width+2, width+2, angle, 240 );
00267 paint.drawArc( 1, 1, width, width, angle, 240 );
00268 paint.drawArc( 2, 2, width-2, width-2, angle, 240 );
00269 if ( angle < 2320 ) {
00270 shadow_color -= 25;
00271 if ( shadow_color < 100 )
00272 shadow_color = 100;
00273 }
00274 else if ( ( angle > 2320 ) && ( angle < 5760 ) ) {
00275 shadow_color += 25;
00276 if ( shadow_color > 255 )
00277 shadow_color = 255;
00278 }
00279 else {
00280 shadow_color -= 25;
00281 if ( shadow_color < 100 )
00282 shadow_color = 100;
00283 }
00284 }
00285 paint.end();
00286
00287
00288
00289 QBitmap mask ( pix-> width ( ), pix-> height ( ), true );
00290 QPainter mp ( &mask );
00291 mp. setPen ( Qt::NoPen );
00292 mp. setBrush ( Qt::color1 );
00293 mp. drawEllipse ( 0, 0, width + 2, width + 2 );
00294 mp. end ( );
00295
00296 pix-> setMask ( mask );
00297 #endif
00298 }
00299
00300 };
00301 };