Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

oledbox.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003               =.             (C) 2002-2005 the Opie Team <opie-devel@handhelds.org>
00004             .=l.
00005            .>+-=
00006  _;:,     .>    :=|.         This program is free software; you can
00007 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00008 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00009 .="- .-=="i,     .._         License as published by the Free Software
00010  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00011      ._= =}       :          or (at your option) any later version.
00012     .%`+i>       _;_.
00013     .i_,=:_.      -<s.       This program is distributed in the hope that
00014      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00015     : ..    .:,     . . .    without even the implied warranty of
00016     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00017   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00018 ..}^=.=       =       ;      Library General Public License for more
00019 ++=   -.     .`     .:       details.
00020  :     =  ...= . :.=-
00021  -.   .:....=;==+<;          You should have received a copy of the GNU
00022   -_. . .   )=.  =           Library General Public License along with
00023     --        :-=`           this library; see the file COPYING.LIB.
00024                              If not, write to the Free Software Foundation,
00025                              Inc., 59 Temple Place - Suite 330,
00026                              Boston, MA 02111-1307, USA.
00027 
00028 */
00029 
00030 #include "oledbox.h"
00031 
00032 /* QT */
00033 #include <qbitmap.h>
00034 #include <qpainter.h>
00035 
00036 namespace Opie {
00037 namespace Ui   {
00038 
00039 /* XPM */
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         // First of all we want to know what area should be updated
00179         // Initialize coordinates, width, and height of the LED
00180         int width = pix-> width ( );
00181 
00182         // Make sure the LED is round!
00183         if ( width > pix-> height ( ))
00184                 width = pix-> height ( );
00185         width -= 2; // leave one pixel border
00186         if ( width < 0 )
00187                 width = 0;
00188 
00189         // maybe we could stop HERE, if width <=0 ?
00190 
00191         // start painting widget
00192         //
00193         paint.begin( pix );
00194 
00195         // Set the color of the LED according to given parameters
00196         color = col;
00197 
00198         // Set the brush to SolidPattern, this fills the entire area
00199         // of the ellipse which is drawn first
00200         brush.setStyle( QBrush::SolidPattern );
00201         brush.setColor( color );
00202         paint.setBrush( brush );                // Assign the brush to the painter
00203 
00204         // Draws a "flat" LED with the given color:
00205         paint.drawEllipse( 1, 1, width, width );
00206 
00207         // Draw the bright light spot of the LED now, using modified "old"
00208         // painter routine taken from KDEUIs KLed widget:
00209 
00210         // Setting the new width of the pen is essential to avoid "pixelized"
00211         // shadow like it can be observed with the old LED code
00212         pen.setWidth( 2 );
00213 
00214         // shrink the light on the LED to a size about 2/3 of the complete LED
00215         int pos = width / 5 + 1;
00216         int light_width = width;
00217         light_width *= 2;
00218         light_width /= 3;
00219 
00220         // Calculate the LEDs "light factor":
00221         int light_quote = ( 130 * 2 / ( light_width ? light_width : 1 ) ) + 100;
00222 
00223         // Now draw the bright spot on the LED:
00224         while ( light_width )
00225         {
00226                 color = color.light( light_quote );                      // make color lighter
00227                 pen.setColor( color );                                   // set color as pen color
00228                 paint.setPen( pen );                                     // select the pen for drawing
00229                 paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle)
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         // Drawing of bright spot finished, now draw a thin border
00243         // around the LED which resembles a shadow with light coming
00244         // from the upper left.
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 );              // Switch off the brush
00255         paint.setBrush( brush );                        // This avoids filling of the ellipse
00256 
00257         // Set the initial color value to 200 (bright) and start
00258         // drawing the shadow border at 45 (45*16 = 720).
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;                     // set color to a darker value
00271                         if ( shadow_color < 100 )
00272                                 shadow_color = 100;
00273                 }
00274                 else if ( ( angle > 2320 ) && ( angle < 5760 ) ) {
00275                         shadow_color += 25;                     // set color to a brighter value
00276                         if ( shadow_color > 255 )
00277                                 shadow_color = 255;
00278                 }
00279                 else {
00280                         shadow_color -= 25;                     // set color to a darker value again
00281                         if ( shadow_color < 100 )
00282                                 shadow_color = 100;
00283                 } // end if ( angle < 2320 )
00284         }   // end for ( angle = 720; angle < 6480; angle += 160 )
00285         paint.end();
00286         //
00287         // painting done
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 };

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