homescreenapp/examples/batterywidgetplugin/src/batterywidget.cpp
changeset 62 341166945d65
child 81 7dd137878ff8
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Example of home screen widget
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //#define OWN_TESTING
       
    20 
       
    21 
       
    22 #include <QGraphicsLinearLayout>
       
    23 #include <QPainter>
       
    24 #include <QTimer>
       
    25 #include "BatteryWidget.h"
       
    26 
       
    27 
       
    28 /*!
       
    29     \ingroup group_helloworld_widget
       
    30     \class BatteryWidget
       
    31     \brief Example implementation for home screen widget. 
       
    32     
       
    33     BatteryWidget is derived from the HbWidget. It implements
       
    34     needed functions for the home screen widget.
       
    35     BatteryWidget uses <a href="http://doc.qt.nokia.com/qtmobility-1.0/qsystemdeviceinfo.html">QSystemDeviceInfo</a> 
       
    36     to get device's battery status. 
       
    37     
       
    38     Battery widget demonstrates the following home screen widget features:
       
    39     <ol>
       
    40     <li> widget shape, see \ref ssection_shape </li>
       
    41     <li> root path property, see \ref sssection_rootPath and
       
    42          widget resources, see \ref section_widget_resources </li>
       
    43     <li> error signal, see \ref sssection_error </li>
       
    44     <li> onInitialize slot, see \ref sssection_oninitialize </li>
       
    45     <li> onShow slot, see \ref sssection_onshow </li>
       
    46     <li> onHide slot, see \ref sssection_onhide </li>
       
    47     </ol>
       
    48     
       
    49 */
       
    50 
       
    51 
       
    52 /*!
       
    53     \fn BatteryWidget::error()
       
    54     Signal emitted if onInitialize fails to load batterywidget image.
       
    55 */
       
    56 
       
    57 
       
    58 /*!
       
    59     Constructs a widget which is a child of \a parent, with widget flags set to \a flags.
       
    60 */
       
    61 BatteryWidget::BatteryWidget(QGraphicsItem* parent, Qt::WindowFlags flags)
       
    62     : HbWidget(parent, flags)
       
    63 {
       
    64     mDeviceInfo = new QSystemDeviceInfo(this);
       
    65     mChargingTimer = new QTimer(this);
       
    66     mBatteryValue = mDeviceInfo->batteryLevel();
       
    67 }
       
    68 
       
    69 /*!
       
    70     Destructor
       
    71 */
       
    72 BatteryWidget::~BatteryWidget()
       
    73 {
       
    74 }
       
    75 
       
    76 /*!
       
    77     Returns rootPath property.
       
    78 */
       
    79 // Start of snippet 1
       
    80 QString BatteryWidget::rootPath() const
       
    81 {
       
    82     return mRootPath;
       
    83 }
       
    84 // End of snippet 1
       
    85 
       
    86 /*!
       
    87     Sets rootPath property as \a rootPath.
       
    88 */
       
    89 // Start of snippet 2
       
    90 void BatteryWidget::setRootPath(QString &rootPath)
       
    91 {
       
    92     mRootPath = rootPath;
       
    93 }
       
    94 // End of snippet 2
       
    95 
       
    96 
       
    97 /*!
       
    98     Return bounding rect. 
       
    99 */
       
   100 QRectF BatteryWidget::boundingRect() const
       
   101 {   
       
   102     QRectF currRect = rect();
       
   103     currRect.setHeight(mBatteryBackgroundBitmap.height());
       
   104     currRect.setWidth(mBatteryBackgroundBitmap.width());
       
   105     return currRect;
       
   106 }
       
   107 
       
   108 /*!
       
   109     Return shape.
       
   110 */
       
   111 // Start of snippet 3
       
   112 QPainterPath BatteryWidget::shape() const
       
   113 {   
       
   114     QPainterPath path;
       
   115     QRectF ownRect = boundingRect();
       
   116     path.moveTo(ownRect.bottomRight());
       
   117     QRectF ownRect2 = ownRect.adjusted(-2,-1,1, ownRect.height());
       
   118     path.arcTo(ownRect2, 0, 180);
       
   119     path.lineTo(ownRect2.right(), ownRect2.center().y());
       
   120     return path;
       
   121 }
       
   122 // End of snippet 3
       
   123 
       
   124 /*!
       
   125     Paint battery image.
       
   126 */
       
   127 void BatteryWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
   128 {
       
   129     Q_UNUSED(option);
       
   130     Q_UNUSED(widget);
       
   131     if (!mUnifiedBatteryBitmap.isNull()) {
       
   132         painter->drawPixmap(QPoint(0,0),mUnifiedBatteryBitmap);
       
   133         }
       
   134 }
       
   135 
       
   136 /*!
       
   137     Called when widget is shown in the home screen.
       
   138 */
       
   139 // Start of snippet 4
       
   140 void BatteryWidget::onShow()
       
   141 {
       
   142     stateChanged();
       
   143     prepareGeometryChange();    
       
   144     resize(mBatteryBackgroundBitmap.size());
       
   145 }
       
   146 // End of snippet 4
       
   147 
       
   148 /*!
       
   149     Called when widget is hidden from the home screen.
       
   150 */
       
   151 // Start of snippet 5
       
   152 void BatteryWidget::onHide()
       
   153 {
       
   154     mDeviceInfo->disconnect(this);
       
   155     mChargingTimer->stop();
       
   156     mChargingTimer->disconnect(this);
       
   157 }
       
   158 // End of snippet 5
       
   159 
       
   160 /*!
       
   161     Called when widget is initalized. Widget can start using its resources,
       
   162     for example rootPath is set by system before it calls this.
       
   163 */
       
   164 // Start of snippet 6
       
   165 void BatteryWidget::onInitialize()
       
   166 {
       
   167     mBatteryBackgroundBitmap.load(mRootPath + "/batterywidget_bg.png");
       
   168     if (mBatteryBackgroundBitmap.isNull()) {
       
   169         // emit error if loading of the image failed. This will remove widget from the home screen.
       
   170         emit error();
       
   171     }
       
   172 }
       
   173 // End of snippet 6
       
   174 
       
   175 /*!
       
   176     Called when widget is unintialized, nothing to do here
       
   177 */
       
   178 void BatteryWidget::onUninitialize()
       
   179 {
       
   180 }
       
   181 
       
   182 /*!
       
   183     Called when phone's battery level is changed.
       
   184 */
       
   185 void BatteryWidget::batteryLevelChanged ( int level )
       
   186 {
       
   187     mBatteryValue=level;
       
   188     drawBatteryImage();
       
   189     update();
       
   190 }
       
   191 
       
   192 /*!
       
   193     Called when phone's power state is changed.
       
   194 */
       
   195 void BatteryWidget::powerStateChanged(QSystemDeviceInfo::PowerState powerState)
       
   196 {
       
   197     Q_UNUSED(powerState);
       
   198     stateChanged();    
       
   199 }
       
   200 
       
   201 /*!
       
   202     Animates pointer when phone is charging. (TODO: animation fw?)
       
   203 */
       
   204 void BatteryWidget::updateChargingBatteryValue()
       
   205 {
       
   206     mBatteryValue+=4;
       
   207     if (mBatteryValue>100) mBatteryValue = 1;
       
   208     drawBatteryImage();
       
   209     update();
       
   210 }
       
   211 
       
   212 /*!
       
   213     Draws pointer on top of battery image
       
   214 */
       
   215 void BatteryWidget::drawBatteryImage()
       
   216 {
       
   217     QRectF rect = mBatteryBackgroundBitmap.rect();
       
   218     
       
   219     mUnifiedBatteryBitmap = QPixmap(rect.size().toSize());
       
   220     mUnifiedBatteryBitmap.fill(QColor(Qt::transparent));
       
   221     
       
   222     QPainter painter(&mUnifiedBatteryBitmap);
       
   223     
       
   224     painter.drawPixmap(rect.toRect(), mBatteryBackgroundBitmap);
       
   225     
       
   226     static const QPoint pointerHand[3] = {
       
   227         QPoint(3, 3),
       
   228         QPoint(-3, 3),
       
   229         QPoint(0, -80)
       
   230     };
       
   231 
       
   232     painter.translate(QPointF(rect.width()/2,rect.height()-10));
       
   233     painter.rotate(180.0 * (mBatteryValue)/100 + 270);
       
   234     
       
   235     painter.setPen(Qt::NoPen);
       
   236     painter.setBrush(Qt::red);
       
   237     painter.drawConvexPolygon(pointerHand, 3);
       
   238 }
       
   239 
       
   240 /*!
       
   241     Handles battery widget states.
       
   242 */
       
   243 void BatteryWidget::stateChanged()
       
   244 {
       
   245     mDeviceInfo->disconnect(this);
       
   246     mChargingTimer->stop();
       
   247     mChargingTimer->disconnect(this);
       
   248     connect(mDeviceInfo, SIGNAL(powerStateChanged(QSystemDeviceInfo::PowerState)), SLOT(powerStateChanged(QSystemDeviceInfo::PowerState)));
       
   249 #ifdef OWN_TESTING
       
   250     connect(mChargingTimer, SIGNAL(timeout()), this, SLOT(updateChargingBatteryValue()));
       
   251     mChargingTimer->start(1000);
       
   252 #else
       
   253     if (mDeviceInfo->currentPowerState() == QSystemDeviceInfo::WallPowerChargingBattery) {
       
   254         connect(mChargingTimer, SIGNAL(timeout()), this, SLOT(updateChargingBatteryValue()));
       
   255         mChargingTimer->start(500);
       
   256     } else {
       
   257         connect(mDeviceInfo, SIGNAL(batteryStatusChanged(QSystemDeviceInfo::BatteryStatus)), SLOT(batteryStatusChanged(QSystemDeviceInfo::BatteryStatus)));
       
   258         connect(mDeviceInfo, SIGNAL(batteryLevelChanged(int)), SLOT(batteryLevelChanged(int)));
       
   259         mBatteryValue = mDeviceInfo->batteryLevel();
       
   260     }
       
   261 #endif    
       
   262     drawBatteryImage();
       
   263     update();
       
   264 }
       
   265