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

brightnessapplet.cpp

Go to the documentation of this file.
00001 /*
00002                      This file is part of the Opie Project
00003 
00004               =.             (C) 2004 Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de>
00005             .=l.             Based on Qtopia 1.7 Brightnessapplet (C) 2003-2004 TrollTech
00006      .>+-=
00007 _;:,   .>  :=|.         This program is free software; you can
00008 .> <`_,  > .  <=          redistribute it and/or  modify it under
00009 :`=1 )Y*s>-.--  :           the terms of the GNU General Public
00010 .="- .-=="i,   .._         License as published by the Free Software
00011 - .  .-<_>   .<>         Foundation; either version 2 of the License,
00012   ._= =}    :          or (at your option) any later version.
00013   .%`+i>    _;_.
00014   .i_,=:_.   -<s.       This program is distributed in the hope that
00015   + . -:.    =       it will be useful,  but WITHOUT ANY WARRANTY;
00016   : ..  .:,   . . .    without even the implied warranty of
00017   =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
00018  _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU
00019 ..}^=.=    =    ;      General Public License for more
00020 ++=  -.   .`   .:       details.
00021 :   = ...= . :.=-
00022 -.  .:....=;==+<;          You should have received a copy of the GNU
00023  -_. . .  )=. =           General Public License along with
00024   --    :-=`           this application; see the file COPYING.LIB.
00025                              If not, write to the Free Software Foundation,
00026                              Inc., 59 Temple Place - Suite 330,
00027                              Boston, MA 02111-1307, USA.
00028 
00029 */
00030 
00031 #include "brightnessapplet.h"
00032 
00033 /* OPIE */
00034 #include <opie2/odebug.h>
00035 #include <opie2/odevice.h>
00036 #include <opie2/oresource.h>
00037 #include <opie2/otaskbarapplet.h>
00038 #include <qpe/applnk.h>
00039 #include <qpe/config.h>
00040 #include <qpe/power.h>
00041 #include <qpe/qcopenvelope_qws.h>
00042 using namespace Opie::Core;
00043 using namespace Opie::Ui;
00044 
00045 /* QT */
00046 #include <qpainter.h>
00047 #include <qlabel.h>
00048 #include <qslider.h>
00049 #include <qlayout.h>
00050 
00051 /* STD */
00052 #include <assert.h>
00053 
00054 /* XPM */
00055 static const char * const light_on_xpm[] = {
00056 "9 16 5 1",
00057 "       c None",
00058 ".      c #FFFFFFFF0000",
00059 "X      c #000000000000",
00060 "o      c #FFFFFFFFFFFF",
00061 "O      c #FFFF6C6C0000",
00062 "         ",
00063 "   XXX   ",
00064 "  XoooX  ",
00065 " Xoooo.X ",
00066 "Xoooooo.X",
00067 "Xoooo...X",
00068 "Xooo.o..X",
00069 " Xooo..X ",
00070 " Xoo...X ",
00071 "  Xoo.X  ",
00072 "  Xoo.XX ",
00073 "  XOOOXX ",
00074 "  XOOOXX ",
00075 "   XOXX  ",
00076 "    XX   ",
00077 "         "};
00078 
00079 
00080 /* XPM */
00081 static const char * const light_off_xpm[] = {
00082 "9 16 4 1",
00083 "       c None",
00084 ".      c #000000000000",
00085 "X      c #6B6B6C6C6C6C",
00086 "o      c #FFFF6C6C0000",
00087 "         ",
00088 "         ",
00089 "   ...   ",
00090 "  .   .  ",
00091 " .    X. ",
00092 ".      X.",
00093 ".    XXX.",
00094 ".   X XX.",
00095 " .   XX. ",
00096 " .  XXX. ",
00097 "  .  X.  ",
00098 "  .  X.. ",
00099 "  .ooo.. ",
00100 "  .ooo.. ",
00101 "   .o..  ",
00102 "    ..   "};
00103 
00104 BrightnessAppletControl::BrightnessAppletControl( OTaskbarApplet* parent, const char* name )
00105                     :QFrame( parent, name, WStyle_StaysOnTop | WType_Popup )
00106 {
00107     setFrameStyle( QFrame::PopupPanel | QFrame::Raised );
00108     QGridLayout *gl = new QGridLayout( this, 3, 2, 6, 3 );
00109     //gl->setRowStretch( 1, 100 );
00110 
00111     int maxbright = ODevice::inst()->displayBrightnessResolution();
00112     slider = new QSlider(this);
00113     slider->setMaxValue(maxbright);
00114     slider->setOrientation(QSlider::Vertical);
00115     slider->setTickmarks(QSlider::Right);
00116     slider->setTickInterval(QMAX(1, maxbright / 16));
00117     slider->setLineStep(QMAX(1, maxbright / 16));
00118     slider->setPageStep(QMAX(1, maxbright / 16));
00119     gl->addMultiCellWidget( slider, 0, 2, 0, 0 );
00120 
00121     QPixmap onPm( (const char **)light_on_xpm );
00122     QLabel *l = new QLabel( this );
00123     l->setPixmap( onPm );
00124     gl->addWidget( l, 0, 1 );
00125 
00126     QPixmap offPm( (const char **)light_off_xpm );
00127     l = new QLabel( this );
00128     l->setPixmap( offPm );
00129     gl->addWidget( l, 2, 1 );
00130 
00131     setFixedHeight( 100 );
00132     setFixedWidth( gl->sizeHint().width() );
00133     setFocusPolicy(QWidget::NoFocus);
00134 }
00135 
00136 
00137 BrightnessAppletControl::~BrightnessAppletControl()
00138 {
00139 }
00140 
00141 
00142 void BrightnessAppletControl::hideEvent( QHideEvent* e )
00143 {    
00144     BrightnessApplet* applet = static_cast<BrightnessApplet*>( parent() );
00145     applet->writeSystemBrightness( applet->calcBrightnessValue() );
00146     QFrame::hideEvent( e );
00147 }
00148 
00149 BrightnessApplet::BrightnessApplet( QWidget *parent, const char *name )
00150              :OTaskbarApplet( parent, name )
00151 {
00152     setFixedHeight( AppLnk::smallIconSize() );
00153     setFixedWidth( AppLnk::smallIconSize() );
00154     _pixmap = Opie::Core::OResource::loadPixmap( "brightnessapplet/icon", Opie::Core::OResource::SmallIcon );
00155    _control = new BrightnessAppletControl( this, "control" );
00156 }
00157 
00158 
00159 void BrightnessApplet::writeSystemBrightness(int brightness)
00160 {
00161     PowerStatus ps = PowerStatusManager::readStatus();
00162 
00163     Config cfg( "apm" );
00164     if (ps.acStatus() == PowerStatus::Online) {
00165         cfg.setGroup("AC");
00166     } else {
00167         cfg.setGroup("Battery");
00168     }
00169     cfg.writeEntry("Brightness", brightness);
00170     odebug << "writing brightness " << brightness << oendl;
00171     cfg.write();
00172 }
00173 
00174 
00175 int BrightnessApplet::readSystemBrightness(void)
00176 {
00177     PowerStatus ps = PowerStatusManager::readStatus();
00178     Config cfg( "apm" );
00179 
00180     if (ps.acStatus() == PowerStatus::Online) {
00181         cfg.setGroup("AC");
00182     } else {
00183         cfg.setGroup("Battery");
00184     }
00185     
00186     odebug << "reading brightness " << cfg.readNumEntry("Brightness", 128) << oendl;
00187 
00188     return cfg.readNumEntry("Brightness", 128);
00189 }
00190 
00191 
00192 BrightnessApplet::~BrightnessApplet()
00193 {
00194 }
00195 
00196 
00197 int BrightnessApplet::position()
00198 {
00199     return 7;
00200 }
00201 
00202 
00203 void BrightnessApplet::paintEvent( QPaintEvent* )
00204 {
00205     QPainter p(this);
00206     p.drawPixmap(0, 0, _pixmap );
00207 }
00208 
00209 
00210 int BrightnessApplet::calcBrightnessValue()
00211 {
00212     int v = _control->slider->maxValue() - _control->slider->value();
00213     return (v * 255 + _control->slider->maxValue() / 2) / _control->slider->maxValue();
00214 }
00215 
00216 
00217 void BrightnessApplet::sliderMoved( int /*value*/ )
00218 {
00219 #ifndef QT_NO_COP
00220     QCopEnvelope e("QPE/System", "setBacklight(int)");
00221     e << calcBrightnessValue();
00222 #else
00223 #error This Applet makes no sense without QCOP
00224 #endif // QT_NO_COP
00225 }
00226 
00227 
00228 void BrightnessApplet::mousePressEvent( QMouseEvent* )
00229 {
00230     if ( !_control->isVisible() )
00231     {
00232         int v = 255 - readSystemBrightness();
00233         popup( _control );
00234         _control->slider->setValue((_control->slider->maxValue() * v + 128) / 255);
00235         connect(_control->slider, SIGNAL(valueChanged(int)), this, SLOT(sliderMoved(int)));
00236     }
00237 }
00238 
00239 
00240 EXPORT_OPIE_APPLET_v1( BrightnessApplet )
00241 

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