Root/
| 1 | /* |
| 2 | * Copyright 2010-2011 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "batterylayer.h" |
| 21 | |
| 22 | #include "mapwidget.h" |
| 23 | |
| 24 | #include <QtCore/QDebug> |
| 25 | #include <QtCore/QFile> |
| 26 | #include <QtCore/QTextStream> |
| 27 | |
| 28 | BatteryLayer::BatteryLayer(MapWidget *map) : |
| 29 | AbstractLayer(map), |
| 30 | m_updateTimer(new QTimer(this)), |
| 31 | m_percent(0), |
| 32 | m_isCharging(false) |
| 33 | { |
| 34 | m_updateTimer->setInterval(60 * 1000); |
| 35 | connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(repaint())); |
| 36 | m_updateTimer->start(); |
| 37 | |
| 38 | reload(); |
| 39 | } |
| 40 | |
| 41 | void BatteryLayer::keyPressed(QKeyEvent *event) |
| 42 | { |
| 43 | if (event->modifiers() == Qt::NoModifier && |
| 44 | event->key() == Qt::Key_B) { |
| 45 | toggleVisibility(); |
| 46 | reload(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void BatteryLayer::paint(QPainter *painter) |
| 51 | { |
| 52 | int w = map()->width(); |
| 53 | int h = map()->height(); |
| 54 | |
| 55 | painter->setBrush(QBrush(QColor(255, 255, 255, 210))); |
| 56 | painter->drawRoundedRect(w - 111, h - 17, 110, 16, 5, 5); |
| 57 | QColor color; |
| 58 | if (m_isCharging) { |
| 59 | color = QColor(0, 255, 0, 220); |
| 60 | } else { |
| 61 | color = QColor(0, 0, 255, 220); |
| 62 | } |
| 63 | QLinearGradient grad(w - 111, h - 17, w - 111, h - 1); |
| 64 | grad.setColorAt(0, color.lighter()); |
| 65 | grad.setColorAt(1, color.darker()); |
| 66 | painter->setBrush(grad); |
| 67 | |
| 68 | painter->drawRoundedRect(w - 111, h - 17, 10 + m_percent, 16, 5, 5); |
| 69 | painter->drawText(w - 101, h - 15, 90, 14, Qt::AlignCenter, QString("%1%").arg(m_percent)); |
| 70 | } |
| 71 | |
| 72 | void BatteryLayer::repaint() |
| 73 | { |
| 74 | if (isVisible() && reload()) { |
| 75 | map()->update(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool BatteryLayer::reload() |
| 80 | { |
| 81 | int percent = m_percent; |
| 82 | QFile capacity("/sys/class/power_supply/battery/capacity"); |
| 83 | if (capacity.open(QFile::ReadOnly | QFile::Text)) { |
| 84 | QTextStream in(&capacity); |
| 85 | QString l = in.readLine(); |
| 86 | percent = l.toInt(); |
| 87 | } |
| 88 | |
| 89 | bool charging = m_isCharging; |
| 90 | QFile status("/sys/class/power_supply/battery/status"); |
| 91 | if (status.open(QFile::ReadOnly | QFile::Text)) { |
| 92 | QTextStream in(&status); |
| 93 | QString l = in.readLine().toLower(); |
| 94 | charging = (l == "charging"); |
| 95 | } |
| 96 | |
| 97 | if (charging != m_isCharging || percent != m_percent) { |
| 98 | m_isCharging = charging; |
| 99 | m_percent = percent; |
| 100 | |
| 101 | return true; |
| 102 | } else { |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 |
Branches:
master
