1 //===========================================
2 // Lumina-DE source code
3 // Copyright (c) 2014, Susanne Jaeckel, 2015-2016 Ken Moore
4 // Available under the 3-clause BSD license
5 // See the LICENSE file for full details
6 //===========================================
7 #include "LBattery.h"
8 #include "LSession.h"
9
LBattery(QWidget * parent,QString id,bool horizontal)10 LBattery::LBattery(QWidget *parent, QString id, bool horizontal) : LPPlugin(parent, id, horizontal){
11 iconOld = -1;
12 //Setup the widget
13 label = new QLabel(this);
14 label->setScaledContents(true);
15 //label->setAlignment(Qt::AlignCenter);
16 this->layout()->addWidget(label);
17 //Setup the timer
18 timer = new QTimer();
19 timer->setInterval(5000); //update every 5 seconds
20 connect(timer,SIGNAL(timeout()), this, SLOT(updateBattery()) );
21 timer->start();
22 QTimer::singleShot(0,this,SLOT(OrientationChange()) ); //update the sizing/icon
23 sessionsettings = new QSettings("lumina-desktop", "sessionsettings");
24 }
25
~LBattery()26 LBattery::~LBattery(){
27 timer->stop();
28 delete timer;
29 }
30
updateBattery(bool force)31 void LBattery::updateBattery(bool force){
32 // Get current state of charge
33 int charge = LOS::batteryCharge();
34 bool charging = LOS::batteryIsCharging();
35 QString batt_icon = LSession::batteryIconName(charge, charging);
36 if(iconOld != batt_icon){
37 label->setPixmap( QIcon::fromTheme(batt_icon).pixmap(label->size()) );
38 if(charge <= 5 && !charging){
39 //Play some audio warning chime when
40 bool playaudio = sessionsettings->value("PlayBatteryLowAudio",true).toBool();
41 if( playaudio ){
42 QString sfile = LSession::handle()->sessionSettings()->value("audiofiles/batterylow", "").toString();
43 if(sfile.isEmpty()){ sfile = LOS::LuminaShare()+"low-battery.ogg"; }
44 LSession::handle()->playAudioFile(sfile);
45 }
46 }
47 iconOld = batt_icon; //save for later
48 }
49
50 if(charge<=5 && !charging){ label->setStyleSheet("QLabel{ background: red;}"); }
51 else if(charge>98 && charging){ label->setStyleSheet("QLabel{ background: green;}"); }
52 else{ label->setStyleSheet("QLabel{ background: transparent;}"); }
53
54 //Now update the display
55 QString tt;
56 //Make sure the tooltip can be properly translated as necessary (Ken Moore 5/9/14)
57 if(charging){ tt = QString(tr("%1 % (Charging)")).arg(QString::number(charge)); }
58 else{ tt = QString( tr("%1 % (%2 Remaining)") ).arg(QString::number(charge), getRemainingTime() ); }
59 label->setToolTip(tt);
60 }
61
getRemainingTime()62 QString LBattery::getRemainingTime(){
63 int secs = LOS::batterySecondsLeft();
64 if(secs < 0){ return "??"; }
65 QString rem; //remaining
66 if(secs > 3600){
67 int hours = secs/3600;
68 rem.append( QString::number(hours)+"h ");
69 secs = secs - (hours*3600);
70 }
71 if(secs > 60){
72 int min = secs/60;
73 rem.append( QString::number(min)+"m ");
74 secs = secs - (min*60);
75 }
76 if(secs > 0){
77 rem.append(QString::number(secs)+"s");
78 }
79 return rem;
80 }
81